Skip to content

Commit baa3981

Browse files
committed
daniel har hjälp mig med ändringar
1 parent c74b88b commit baa3981

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

src/main/java/org/example/ConnectionHandler.java

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,23 @@
1515
import java.net.Socket;
1616

1717
public class ConnectionHandler implements AutoCloseable {
18-
19-
Socket client;
20-
String uri;
18+
private final Socket client;
2119
private final List<Filter> filters;
2220

2321
public ConnectionHandler(Socket client) {
2422
this.client = client;
2523
this.filters = buildFilters();
2624
}
27-
private List<Filter> buildFilters() {
25+
26+
private List<Filter> buildFilters() {
2827
List<Filter> list = new ArrayList<>();
2928
AppConfig config = ConfigLoader.get();
3029
AppConfig.IpFilterConfig ipFilterConfig = config.ipFilter();
3130
if (Boolean.TRUE.equals(ipFilterConfig.enabled())) {
3231
list.add(createIpFilterFromConfig(ipFilterConfig));
3332
}
34-
// Add more filters here...
3533
return list;
36-
}
37-
34+
}
3835

3936
public void runConnectionHandler() throws IOException {
4037
HttpParser parser = new HttpParser();
@@ -53,6 +50,7 @@ public void runConnectionHandler() throws IOException {
5350
String clientIp = client.getInetAddress().getHostAddress();
5451
request.setAttribute("clientIp", clientIp);
5552

53+
// Apply security filters
5654
HttpResponseBuilder response = applyFilters(request);
5755

5856
int statusCode = response.getStatusCode();
@@ -64,31 +62,49 @@ public void runConnectionHandler() throws IOException {
6462
return;
6563
}
6664

67-
resolveTargetFile(parser.getUri());
65+
// Sanitize URI here (clean it)
66+
String sanitizedUri = sanitizeUri(parser.getUri());
67+
String cacheKey = "www:" + sanitizedUri;
68+
69+
// Check cache FIRST
70+
byte[] cachedBytes = FileCache.get(cacheKey);
71+
if (cachedBytes != null) {
72+
System.out.println("✓ Cache HIT: " + sanitizedUri);
73+
HttpResponseBuilder cachedResp = new HttpResponseBuilder();
74+
cachedResp.setContentTypeFromFilename(sanitizedUri);
75+
cachedResp.setBody(cachedBytes);
76+
client.getOutputStream().write(cachedResp.build());
77+
client.getOutputStream().flush();
78+
return;
79+
}
80+
81+
// Cache miss - StaticFileHandler reads and caches
82+
System.out.println("✗ Cache MISS: " + sanitizedUri);
6883
StaticFileHandler sfh = new StaticFileHandler();
69-
sfh.sendGetRequest(client.getOutputStream(), uri);
84+
sfh.sendGetRequest(client.getOutputStream(), sanitizedUri);
85+
}
86+
87+
private String sanitizeUri(String uri) {
88+
if (uri == null || uri.isEmpty()) return "index.html";
89+
90+
int endIndex = Math.min(
91+
uri.indexOf('?') < 0 ? uri.length() : uri.indexOf('?'),
92+
uri.indexOf('#') < 0 ? uri.length() : uri.indexOf('#')
93+
);
94+
95+
return uri.substring(0, endIndex)
96+
.replace("\0", "")
97+
.replaceAll("^/+", "")
98+
.replaceAll("^$", "index.html");
7099
}
71100

72101
private HttpResponseBuilder applyFilters(HttpRequest request) {
73102
HttpResponseBuilder response = new HttpResponseBuilder();
74-
75103
FilterChainImpl chain = new FilterChainImpl(filters);
76104
chain.doFilter(request, response);
77-
78105
return response;
79106
}
80107

81-
private void resolveTargetFile(String uri) {
82-
if (uri.matches("/$")) { //matches(/)
83-
this.uri = "index.html";
84-
} else if (uri.matches("^(?!.*\\.html$).*$")) {
85-
this.uri = uri.concat(".html");
86-
} else {
87-
this.uri = uri;
88-
}
89-
90-
}
91-
92108
@Override
93109
public void close() throws Exception {
94110
client.close();
@@ -97,19 +113,16 @@ public void close() throws Exception {
97113
private IpFilter createIpFilterFromConfig(AppConfig.IpFilterConfig config) {
98114
IpFilter filter = new IpFilter();
99115

100-
// Set mode
101116
if ("ALLOWLIST".equalsIgnoreCase(config.mode())) {
102117
filter.setMode(IpFilter.FilterMode.ALLOWLIST);
103118
} else {
104119
filter.setMode(IpFilter.FilterMode.BLOCKLIST);
105120
}
106121

107-
// Add blocked IPs
108122
for (String ip : config.blockedIps()) {
109123
filter.addBlockedIp(ip);
110124
}
111125

112-
// Add allowed IPs
113126
for (String ip : config.allowedIps()) {
114127
filter.addAllowedIp(ip);
115128
}

0 commit comments

Comments
 (0)