Skip to content

Commit 2251d0a

Browse files
committed
Fix bugs and add DOT Files handling
1 parent 27f8206 commit 2251d0a

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package express.middleware;
2+
3+
public enum DotFiles {
4+
IGNORE, DENY, ALLOW
5+
}

src/express/middleware/FileProvider.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import express.http.HttpRequest;
44
import express.http.request.Request;
55
import express.http.response.Response;
6+
import express.utils.Status;
67
import express.utils.Utils;
78

89
import java.io.File;
@@ -45,6 +46,17 @@ public void handle(Request req, Response res) {
4546

4647
if (reqFile != null && reqFile.exists()) {
4748

49+
if (reqFile.getName().charAt(0) == '.') {
50+
switch (OPTIONS.getDotFiles()) {
51+
case IGNORE:
52+
res.setStatus(Status._404);
53+
return;
54+
case DENY:
55+
res.setStatus(Status._403);
56+
return;
57+
}
58+
}
59+
4860
if (OPTIONS.getExtensions() != null) {
4961
String reqEx = Utils.getExtension(reqFile);
5062

@@ -57,6 +69,8 @@ public void handle(Request req, Response res) {
5769
break;
5870
}
5971
}
72+
73+
res.setStatus(Status._403);
6074
} else {
6175
finish(reqFile, req, res);
6276
}
@@ -114,7 +128,7 @@ private String getURIFilename(String path) {
114128
int s = path.lastIndexOf('/');
115129
int e = path.lastIndexOf('.');
116130

117-
if (e == -1 && s <= 1)
131+
if (e == -1 && s == -1)
118132
return "index";
119133

120134
if (e == -1)
@@ -123,7 +137,9 @@ private String getURIFilename(String path) {
123137
if (s == -1)
124138
s = 0;
125139

126-
return path.substring(s + 1, e);
140+
String name = path.substring(s + 1, e);
141+
142+
return name.isEmpty() ? "index" : name;
127143
}
128144

129145
/**

src/express/middleware/FileProviderOptions.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
public class FileProviderOptions {
66

7-
87
private String[] extensions;
98
private HttpRequest handler;
109
private boolean fallBackSearching;
1110
private boolean lastModified;
1211
private long maxAge;
12+
private DotFiles dotFiles;
1313

1414
{
1515
// Initalize some values.
@@ -18,6 +18,7 @@ public class FileProviderOptions {
1818
this.fallBackSearching = false;
1919
this.lastModified = true;
2020
this.maxAge = 0;
21+
this.dotFiles = DotFiles.IGNORE;
2122
}
2223

2324
public FileProviderOptions() { }
@@ -120,4 +121,23 @@ public FileProviderOptions setHandler(HttpRequest handler) {
120121
this.handler = handler;
121122
return this;
122123
}
124+
125+
/**
126+
* @return How Dot-files will be currently handled.
127+
*/
128+
public DotFiles getDotFiles() {
129+
return dotFiles;
130+
}
131+
132+
/**
133+
* Set how ".foo" file will be handled.
134+
* Ignore: Act if these files doen't exists, response with 404
135+
* Deny: Deny the file, response with 303 and do nothing.
136+
* Allow: No special threadment for this files.
137+
*
138+
* @param dotFiles The handling type.
139+
*/
140+
public void setDotFiles(DotFiles dotFiles) {
141+
this.dotFiles = dotFiles;
142+
}
123143
}

0 commit comments

Comments
 (0)