Changed http codes (404 vs 501) + generic fixes

This commit is contained in:
Benedikt Galbavy 2023-12-28 01:04:18 +01:00
parent 28eb5cf44d
commit 8170a24eec
7 changed files with 7 additions and 9 deletions

View File

@ -33,8 +33,8 @@ public class Main {
/* trading */
router.addRoute(HttpMethod.GET, "/tradings", new TestService(), new int[]{});
router.addRoute(HttpMethod.POST, "/tradings", new TestService(), new int[]{});
router.addRoute(HttpMethod.DELETE, "/tradings/{tradingdealid}", new TestService(), new int[]{2});
router.addRoute(HttpMethod.POST, "/tradings/{tradingdealid}", new TestService(), new int[]{2});
router.addRoute(HttpMethod.DELETE, "/tradings/{tradingDealId}", new TestService(), new int[]{2});
router.addRoute(HttpMethod.POST, "/tradings/{tradingDealId}", new TestService(), new int[]{2});
Server server = new Server(10001, 10, router);
server.start();

View File

@ -5,6 +5,7 @@ import at.nanopenguin.mtcg.http.HttpRequest;
import at.nanopenguin.mtcg.http.Response;
public class InternalErrorService implements Service {
/* For error in http server */
@Override
public Response handleRequest(HttpRequest request) {

View File

@ -8,6 +8,6 @@ public class TestService implements Service {
@Override
public Response handleRequest(HttpRequest request) {
return new Response(HttpStatus.OK, "application/json", "");
return new Response(HttpStatus.NOT_IMPLEMENTED, "application/json", "");
}
}

View File

@ -9,7 +9,7 @@ public class HttpRequest {
private final HttpMethod method;
private final String path;
private final String version;
private Map<String, String> httpHeaders = new HashMap<String, String>();
private final Map<String, String> httpHeaders = new HashMap<>();
private final String body;
public HttpRequest(BufferedReader br) throws IOException {

View File

@ -36,7 +36,7 @@ public class RequestHandler implements Runnable {
if (service == null) {
System.out.println("service does not exist");
response = new Response(HttpStatus.NOT_IMPLEMENTED, "text/plain", "");
response = new Response(HttpStatus.NOT_FOUND, "text/plain", "");
break responseBuilder;
}
System.out.println("creating response");

View File

@ -19,10 +19,8 @@ public class Response {
String localDatetime = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("UTC")));
return "HTTP/1.1 " + this.httpStatus.statusCode + " " + this.httpStatus.statusMessage + "\r\n" +
"Cache-Control: max-age=0\r\n" +
"Connection: close\r\n" +
"Date: " + localDatetime + "\r\n" +
"Expires: " + localDatetime + "\r\n" +
"Content-Type: " + this.contentType + "\r\n" +
"Content-Length: " + this.content.length() + "\r\n" +
"\r\n" +

View File

@ -1,6 +1,5 @@
package at.nanopenguin.mtcg.http;
import at.nanopenguin.mtcg.application.InternalErrorService;
import at.nanopenguin.mtcg.application.Service;
import java.util.*;
@ -34,7 +33,7 @@ public class Router {
System.out.println("resolving route " + route);
String[] routeComponents = route.split("/");
String pathVariable = null;
String pathVariable = null; // might be useful later, idk how services
int i = 1;