Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit ed57e93

Browse files
committed
Fix #77
1 parent 344e971 commit ed57e93

File tree

6 files changed

+127
-41
lines changed

6 files changed

+127
-41
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/handler/DirectoryEntry.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,16 @@ public final File getFile(final String path){
245245
final File parentFile = new File(dabs + relative).getParentFile();
246246
final String pabs = parentFile.getAbsolutePath();
247247

248-
// if is in top level directory (both cases) or if is a child of the directory folder (walk case)
249-
// null otherwise
250-
return pabs.equals(dabs) || (isWalkthrough && pabs.startsWith(dabs)) ? new File(dabs + relative) : null;
248+
// if not top level directory or if not child of directory folder, then return null file
249+
if(!pabs.equals(dabs) && (!isWalkthrough || !pabs.startsWith(dabs))) return null;
250+
251+
final String fileName = new File(dabs + relative).getName();
252+
253+
// for each file in parent directory, run adapter to find file that matches adapted name
254+
for(final File file : Objects.requireNonNullElse(parentFile.listFiles(), new File[0]))
255+
if(!file.isDirectory() && adapter.getName(file).equals(fileName))
256+
return file;
257+
return null;
251258
}
252259
}
253260

src/test/java/handlers/FileHandlerTests.java

+94-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void addFileTests() throws IOException{
3939
Files.write(file.toPath(), init.getBytes());
4040
else
4141
Assert.fail("Failed to create new file for testing: " + file.getPath());
42-
}catch(Exception e){
42+
}catch(final Exception ignored){
4343
Assert.fail("Failed to create new file for testing: " + file.getPath());
4444
}
4545

@@ -63,7 +63,7 @@ public void addFileTests() throws IOException{
6363
.thenApply(HttpResponse::body).get();
6464

6565
Assert.assertEquals("Client data did not match server data for " + file.getName(),init,response);
66-
}catch(InterruptedException | ExecutionException e){
66+
}catch(final InterruptedException | ExecutionException ignored){
6767
Assert.fail("Failed to read context of " + file.getName());
6868
}
6969

@@ -72,7 +72,7 @@ public void addFileTests() throws IOException{
7272
final String after = String.valueOf(System.currentTimeMillis());
7373
try{
7474
Files.write(file.toPath(), after.getBytes());
75-
}catch(Exception e){
75+
}catch(final Exception ignored){
7676
Assert.fail("Failed to second write file " + file.getPath());
7777
}
7878

@@ -81,7 +81,7 @@ public void addFileTests() throws IOException{
8181
.thenApply(HttpResponse::body).get();
8282

8383
Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? init : after,response);
84-
}catch(InterruptedException | ExecutionException e){
84+
}catch(final InterruptedException | ExecutionException ignored){
8585
Assert.fail("Failed to read context " + file.getName());
8686
}
8787
});
@@ -194,7 +194,7 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
194194

195195
Assert.assertNotNull("Client did not find data for " + path,response);
196196
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
197-
}catch(ExecutionException e){
197+
}catch(final ExecutionException ignored){
198198
Assert.fail("Client did not find data for " + path);
199199
}
200200
}
@@ -216,7 +216,93 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
216216
.thenApply(HttpResponse::body).get();
217217

218218
Assert.assertNull("Client found data for blocked path " + path,response);
219-
}catch(ExecutionException e){
219+
}catch(final ExecutionException e){
220+
exception = e;
221+
}
222+
Assert.assertNotNull("Client found data for blocked path",exception);
223+
}
224+
225+
server.stop();
226+
}
227+
228+
@Test
229+
public void addDirectoryTestsNoWalkAdapter() throws IOException, InterruptedException{
230+
final int port = 25022;
231+
final SimpleHttpServer server = SimpleHttpServer.create(port);
232+
final FileHandlerAdapter adapter = new FileHandlerAdapter() {
233+
@Override
234+
public byte[] getBytes(final File file, final byte[] bytes){
235+
return bytes;
236+
}
237+
238+
@Override
239+
public String getName(final File file){
240+
return file.getName().substring(0,file.getName().lastIndexOf('.'));
241+
}
242+
};
243+
final FileHandler handler = new FileHandler(adapter);
244+
final String contextNoName = "alt";
245+
final String contextWName = "altn";
246+
final String dirNewName = "dirName";
247+
248+
final String testRealFile = "test", testFileContent = "readme";
249+
final String noReadDir = "dirnoread";
250+
final String noReadFile = "innerNoRead";
251+
252+
final File noWalk = new File("src/test/resources/directory/nowalk");
253+
254+
final String context = "";
255+
256+
handler.addDirectory(noWalk); // test file & directory read
257+
handler.addDirectory(contextNoName,noWalk);
258+
handler.addDirectory(noWalk,dirNewName);
259+
handler.addDirectory(contextWName,noWalk,dirNewName);
260+
261+
server.createContext(context,handler);
262+
server.start();
263+
264+
final String[] validPathsToTest = { // valid reads
265+
noWalk.getName() + '/' + testRealFile,
266+
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
267+
dirNewName + '/' + testRealFile,
268+
contextWName + '/' + dirNewName + '/' + testRealFile
269+
};
270+
271+
for(final String path : validPathsToTest){
272+
String url = "http://localhost:" + port + '/' + path;
273+
HttpRequest request = HttpRequest.newBuilder()
274+
.uri(URI.create(url))
275+
.build();
276+
277+
try{
278+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
279+
.thenApply(HttpResponse::body).get();
280+
281+
Assert.assertNotNull("Client did not find data for " + path,response);
282+
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
283+
}catch(final ExecutionException ignored){
284+
Assert.fail("Client did not find data for " + path);
285+
}
286+
}
287+
288+
final String[] invalidPathsToTest = {
289+
noWalk.getName() + '/' + noReadDir,
290+
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
291+
};
292+
293+
for(final String path : invalidPathsToTest){
294+
String url = "http://localhost:" + port + '/' + path;
295+
HttpRequest request = HttpRequest.newBuilder()
296+
.uri(URI.create(url))
297+
.build();
298+
299+
Exception exception = null;
300+
try{
301+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
302+
.thenApply(HttpResponse::body).get();
303+
304+
Assert.assertNull("Client found data for blocked path " + path,response);
305+
}catch(final ExecutionException e){
220306
exception = e;
221307
}
222308
Assert.assertNotNull("Client found data for blocked path",exception);
@@ -261,7 +347,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
261347

262348
Assert.assertNotNull("Client did not find data for " + path,response);
263349
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
264-
}catch(ExecutionException e){
350+
}catch(final ExecutionException ignored){
265351
Assert.fail("Client did not find data for " + path);
266352
}
267353
}
@@ -282,7 +368,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
282368
.thenApply(HttpResponse::body).get();
283369

284370
Assert.assertNull("Client found data for blocked path " + path,response);
285-
}catch(ExecutionException e){
371+
}catch(final ExecutionException e){
286372
exception = e;
287373
}
288374
Assert.assertNotNull("Client found data for blocked path",exception);

src/test/java/handlers/SSEHandlerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String call(){
4747
try{
4848
while((ln = IN.readLine()) != null)
4949
OUT.append(ln).append('\n');
50-
}catch(IOException e){
50+
}catch(final IOException ignored){
5151
Assert.fail("Unable to read input stream from client");
5252
}
5353
return OUT.toString();

src/test/java/handlers/ThrottledHandlerTests.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void sessionThrottler() throws IOException{
2727
new ThrottledHandler(
2828
(SimpleHttpHandler) exchange -> {
2929
try{ Thread.sleep(TimeUnit.SECONDS.toMillis(3));
30-
}catch(InterruptedException ignored){ }
30+
}catch(final InterruptedException ignored){ }
3131
exchange.send(exchange.toString());
3232
},
3333
new SessionThrottler(sessionHandler){
@@ -51,14 +51,14 @@ public int getMaxConnections(final HttpSession session, final HttpExchange excha
5151
try{
5252
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
5353
.thenApply(HttpResponse::statusCode).get();
54-
}catch(InterruptedException | ExecutionException ignored){ }
54+
}catch(final InterruptedException | ExecutionException ignored){ }
5555
}).start();
5656

5757
Exception exception = null;
5858
try{
5959
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
6060
.thenApply(HttpResponse::statusCode).get();
61-
}catch(InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
61+
}catch(final InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
6262
exception = e;
6363
}
6464
Assert.assertTrue("Second request returned a result for a throttled thread (connection not allowed)",exception instanceof ExecutionException);
@@ -80,7 +80,7 @@ public void serverSessionThrottler() throws IOException{
8080
new ThrottledHandler(
8181
(SimpleHttpHandler) exchange -> {
8282
try{ Thread.sleep(TimeUnit.SECONDS.toMillis(3));
83-
}catch(InterruptedException ignored){ }
83+
}catch(final InterruptedException ignored){ }
8484
exchange.send(exchange.toString());
8585
},
8686
new ServerSessionThrottler(sessionHandler){
@@ -104,14 +104,14 @@ public int getMaxConnections(final HttpSession session, final HttpExchange excha
104104
try{
105105
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
106106
.thenApply(HttpResponse::statusCode).get();
107-
}catch(InterruptedException | ExecutionException ignored){ }
107+
}catch(final InterruptedException | ExecutionException ignored){ }
108108
}).start();
109109

110110
Exception exception = null;
111111
try{
112112
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
113113
.thenApply(HttpResponse::statusCode).get();
114-
}catch(InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
114+
}catch(final InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
115115
exception = e;
116116
}
117117
Assert.assertTrue("Second request returned a result for a throttled thread (connection not allowed)",exception instanceof ExecutionException);
@@ -131,7 +131,7 @@ public void exchangeThrottler() throws IOException{
131131
new ThrottledHandler(
132132
(SimpleHttpHandler) exchange -> {
133133
try{ Thread.sleep(TimeUnit.SECONDS.toMillis(3));
134-
}catch(InterruptedException ignored){ }
134+
}catch(final InterruptedException ignored){ }
135135
exchange.send(exchange.toString());
136136
},
137137
new ExchangeThrottler(){
@@ -155,14 +155,14 @@ public int getMaxConnections(final HttpExchange exchange){
155155
try{
156156
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
157157
.thenApply(HttpResponse::statusCode).get();
158-
}catch(InterruptedException | ExecutionException ignored){ }
158+
}catch(final InterruptedException | ExecutionException ignored){ }
159159
}).start();
160160

161161
Exception exception = null;
162162
try{
163163
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
164164
.thenApply(HttpResponse::statusCode).get();
165-
}catch(InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
165+
}catch(final InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
166166
exception = e;
167167
}
168168
Assert.assertTrue("Second request returned a result for a throttled thread (connection not allowed)",exception instanceof ExecutionException);
@@ -182,7 +182,7 @@ public void serverExchangeThrottler() throws IOException{
182182
new ThrottledHandler(
183183
(SimpleHttpHandler) exchange -> {
184184
try{ Thread.sleep(TimeUnit.SECONDS.toMillis(3));
185-
}catch(InterruptedException ignored){ }
185+
}catch(final InterruptedException ignored){ }
186186
exchange.send(exchange.toString());
187187
},
188188
new ServerExchangeThrottler(){
@@ -206,14 +206,14 @@ public int getMaxConnections(final HttpExchange exchange){
206206
try{
207207
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
208208
.thenApply(HttpResponse::statusCode).get();
209-
}catch(InterruptedException | ExecutionException ignored){ }
209+
}catch(final InterruptedException | ExecutionException ignored){ }
210210
}).start();
211211

212212
Exception exception = null;
213213
try{
214214
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
215215
.thenApply(HttpResponse::statusCode).get();
216-
}catch(InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
216+
}catch(final InterruptedException | ExecutionException e){ // JDK failure to recognize HttpTimeoutException as valid catch
217217
exception = e;
218218
}
219219
Assert.assertTrue("Second request returned a result for a throttled thread (connection not allowed)",exception instanceof ExecutionException);

src/test/java/simplehttpserver/SimpleHttpServerBindTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public void testPortRange() throws IOException{
1616

1717
Exception exception = null;
1818
try{ server.bind(-1);
19-
}catch(IllegalArgumentException | IOException e){ exception = e; }
19+
}catch(final IllegalArgumentException | IOException e){ exception = e; }
2020
Assert.assertTrue("Bind server to bad port (-1) should throw an exception", exception instanceof IllegalArgumentException);
2121

2222
exception = null;
2323
try{ server.bind(65536);
24-
}catch(IllegalArgumentException | IOException e){ exception = e; }
24+
}catch(final IllegalArgumentException | IOException e){ exception = e; }
2525
Assert.assertTrue("Bind server to bad port (65536) should throw an exception", exception instanceof IllegalArgumentException);
2626

2727
exception = null;
2828
try{ server.bind(port);
29-
}catch(IllegalArgumentException | IOException e){ exception = e; }
29+
}catch(final IllegalArgumentException | IOException e){ exception = e; }
3030
Assert.assertNull("Bind server to valid port (" + port + ") should not throw an exception",exception);
3131

3232
Assert.assertNotNull("Server address should not be null for successful bind",server.getAddress());

src/test/java/simplehttpserver/SimpleHttpServerContextTests.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,13 @@ public void removeNullContext() throws IOException{
3939

4040
Exception exception = null;
4141
try{ server.removeContext((String) null);
42-
}catch(NullPointerException e){ exception = e; }
42+
}catch(final NullPointerException e){ exception = e; }
4343
Assert.assertNotNull("Null string context should throw NPE", exception);
4444

45-
/* broken
46-
exception = null;
47-
try{ server.removeContext((HttpContext) null);
48-
}catch(NullPointerException e){ exception = e; }
49-
Assert.assertNotNull("Null http context should throw NPE", exception);
50-
*/
51-
5245
String context = "";
5346
exception = null;
5447
try{ server.removeContext(context);
55-
}catch(IllegalArgumentException e){ exception = e; }
48+
}catch(final IllegalArgumentException e){ exception = e; }
5649
Assert.assertNotNull("Server should throw IllegalArgumentException when removing a context that doesn't exist", exception);
5750
}
5851

@@ -63,12 +56,12 @@ public void removeContext() throws IOException{
6356
final String context = "";
6457
server.createContext(context);
6558
try{ server.removeContext(context);
66-
}catch(IllegalArgumentException e){
59+
}catch(final IllegalArgumentException ignored){
6760
Assert.fail("Server should not throw exception when removing existing string context");
6861
}
6962

7063
try{ server.removeContext(server.createContext(context));
71-
}catch(IllegalArgumentException e){
64+
}catch(final IllegalArgumentException ignored){
7265
Assert.fail("Server should not throw exception when removing existing http context");
7366
}
7467
}
@@ -79,15 +72,15 @@ public void removeNativeContext() throws IOException{
7972
String context = "/";
8073

8174
try{ server.removeContext(server.getHttpServer().createContext(context));
82-
}catch(IllegalArgumentException ignored){
75+
}catch(final IllegalArgumentException ignored){
8376
Assert.fail("Removing a context added by the native http server should not throw an exception");
8477
}
8578

8679
server.createContext(context);
8780
server.getHttpServer().removeContext(context);
8881
try{
8982
server.createContext(context);
90-
}catch(IllegalArgumentException ignored){
83+
}catch(final IllegalArgumentException ignored){
9184
Assert.fail("Server should be able to create a new context if removed by native http server");
9285
}
9386
}
@@ -120,7 +113,7 @@ public void createRootContext() throws IOException{
120113
Exception exception = null;
121114
try{
122115
server.createContext(context,handler);
123-
}catch(IllegalArgumentException e){ exception = e; }
116+
}catch(final IllegalArgumentException e){ exception = e; }
124117
Assert.assertNotNull("Server should throw IllegalArgumentException when adding RootHandler to non-root context",exception);
125118

126119
final String[] testRoots = {"/","\\",""};

0 commit comments

Comments
 (0)