i hate tcp warmups

This commit is contained in:
Daniel
2024-10-23 20:32:48 +03:00
parent 037889fa10
commit 5b09f83d9b
2 changed files with 47 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
package org.lumijiez.core.http;
import org.lumijiez.logging.Logger;
import java.io.BufferedReader;
import java.io.IOException;
@@ -13,17 +15,30 @@ public class HttpRequest {
}
private void parseRequest(BufferedReader in) throws IOException {
String requestLine = in.readLine();
if (requestLine != null) {
String requestLine;
while ((requestLine = in.readLine()) != null) {
if (!requestLine.trim().isEmpty()) {
String[] tokens = requestLine.split(" ");
if (tokens.length >= 3) {
if (tokens.length == 3) {
this.method = tokens[0];
this.path = tokens[1];
this.httpVersion = tokens[2];
break;
} else {
Logger.error("HTTP", "Invalid line format: " + requestLine);
throw new IOException("Invalid request line format.");
}
}
}
// String headerLine;
// while ((headerLine = in.readLine()) != null && !headerLine.trim().isEmpty()) {
// Logger.info("HTTP-DEBUG", "Header: " + headerLine);
// }
}
public String getMethod() {
return method;
}

View File

@@ -62,16 +62,37 @@ public class HttpServer {
}
protected void handleClient(Socket clientSocket) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
in.mark(32000);
if (!in.ready()) {
clientSocket.close();
return;
}
String firstLine = in.readLine();
if (firstLine == null || firstLine.trim().isEmpty()) {
clientSocket.close();
return;
}
in.reset();
HttpRequest request = new HttpRequest(in);
HttpResponse response = new HttpResponse(out);
if (request.getMethod() != null && request.getPath() != null) {
Logger.info("HTTP", "Incoming: " + request.getMethod() + " " + request.getPath());
router.handleRequest(request, response);
}
clientSocket.close();
} catch (IOException e) {
Logger.error("HTTP", "Error handling client: " + e.getMessage());
try {
clientSocket.close();
} catch (IOException ignored) {}
}
}