Skip to content

Commit 524f33c

Browse files
* Move HTTP handling to a dedicated ConnectionHandler (#50)
* * Move HTTP handling logic from TcpServer to a dedicated ConnectionTask class * Implement virtual thread execution for better scalability * Add regex-based URI routing to support clean URLs and default to index.html * Ensure sockets are properly closed Co-authored-by: Caroline Nordbrandt <caroline_nordbradt@hotmail.com> * change thrown exception to runtime instead with appropiet message in tcpServer. --------- Co-authored-by: Caroline Nordbrandt <caroline_nordbradt@hotmail.com>
1 parent aaeba6d commit 524f33c

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.example;
2+
3+
import org.example.httpparser.HttpParser;
4+
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
8+
public class ConnectionHandler implements AutoCloseable {
9+
10+
Socket client;
11+
String uri;
12+
13+
public ConnectionHandler(Socket client) {
14+
this.client = client;
15+
}
16+
17+
public void runConnectionHandler() throws IOException {
18+
StaticFileHandler sfh = new StaticFileHandler();
19+
HttpParser parser = new HttpParser();
20+
parser.setReader(client.getInputStream());
21+
parser.parseRequest();
22+
parser.parseHttp();
23+
resolveTargetFile(parser.getUri());
24+
sfh.sendGetRequest(client.getOutputStream(), uri);
25+
}
26+
27+
private void resolveTargetFile(String uri) {
28+
if (uri.matches("/$")) { //matches(/)
29+
this.uri = "index.html";
30+
} else if (uri.matches("^(?!.*\\.html$).*$")) {
31+
this.uri = uri.concat(".html");
32+
} else {
33+
this.uri = uri;
34+
}
35+
36+
}
37+
38+
@Override
39+
public void close() throws Exception {
40+
client.close();
41+
}
42+
}

src/main/java/org/example/StaticFileHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private void handleGetRequest(String uri) throws IOException {
1919

2020
File file = new File(WEB_ROOT, uri);
2121
fileBytes = Files.readAllBytes(file.toPath());
22+
2223
}
2324

2425
public void sendGetRequest(OutputStream outputStream, String uri) throws IOException{

src/main/java/org/example/TcpServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ public void start() {
1919
while (true) {
2020
Socket clientSocket = serverSocket.accept(); // block
2121
System.out.println("Client connected: " + clientSocket.getRemoteSocketAddress());
22-
clientSocket.close();
22+
Thread.ofVirtual().start(() -> handleClient(clientSocket));
2323
}
2424
} catch (IOException e) {
2525
throw new RuntimeException("Failed to start TCP server", e);
2626
}
2727
}
28-
}
28+
29+
private void handleClient(Socket client) {
30+
try (ConnectionHandler connectionHandler = new ConnectionHandler(client)) {
31+
connectionHandler.runConnectionHandler();
32+
} catch (Exception e) {
33+
throw new RuntimeException("Error handling client connection " + e);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)