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 */ /* trading */
router.addRoute(HttpMethod.GET, "/tradings", new TestService(), new int[]{}); router.addRoute(HttpMethod.GET, "/tradings", new TestService(), new int[]{});
router.addRoute(HttpMethod.POST, "/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.DELETE, "/tradings/{tradingDealId}", new TestService(), new int[]{2});
router.addRoute(HttpMethod.POST, "/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 server = new Server(10001, 10, router);
server.start(); server.start();

View File

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

View File

@ -8,6 +8,6 @@ public class TestService implements Service {
@Override @Override
public Response handleRequest(HttpRequest request) { 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 HttpMethod method;
private final String path; private final String path;
private final String version; 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; private final String body;
public HttpRequest(BufferedReader br) throws IOException { public HttpRequest(BufferedReader br) throws IOException {

View File

@ -36,7 +36,7 @@ public class RequestHandler implements Runnable {
if (service == null) { if (service == null) {
System.out.println("service does not exist"); 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; break responseBuilder;
} }
System.out.println("creating response"); 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"))); 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" + return "HTTP/1.1 " + this.httpStatus.statusCode + " " + this.httpStatus.statusMessage + "\r\n" +
"Cache-Control: max-age=0\r\n" +
"Connection: close\r\n" + "Connection: close\r\n" +
"Date: " + localDatetime + "\r\n" + "Date: " + localDatetime + "\r\n" +
"Expires: " + localDatetime + "\r\n" +
"Content-Type: " + this.contentType + "\r\n" + "Content-Type: " + this.contentType + "\r\n" +
"Content-Length: " + this.content.length() + "\r\n" + "Content-Length: " + this.content.length() + "\r\n" +
"\r\n" + "\r\n" +

View File

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