From d3a72b541b13d83d0a5562289c04fb951f3e367d Mon Sep 17 00:00:00 2001 From: JJ Date: Tue, 21 Mar 2023 18:16:05 -0700 Subject: Initial skeleton of support for HTTP --- src/main/model/http/HttpResponse.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/model/http/HttpResponse.java (limited to 'src/main/model/http/HttpResponse.java') diff --git a/src/main/model/http/HttpResponse.java b/src/main/model/http/HttpResponse.java new file mode 100644 index 0000000..65f5bc0 --- /dev/null +++ b/src/main/model/http/HttpResponse.java @@ -0,0 +1,34 @@ +package model.http; + +import java.util.ArrayList; + +public record HttpResponse(String version, int status, String reason, String body, ArrayList
headers) { + // for constructing local error responses + public HttpResponse(int status, String reason) { + this("HTTP 1.1", status, reason, "", new ArrayList
()); + } + + // probably inefficient but eh + public static HttpResponse parse(String response) throws InvalidResponseException { + try { + var split = response.split("\\r\\n\\r\\n"); + var lines = split[0].split("\\r\\n"); + var body = split[1]; + var start = lines[0].split(" ", 3); + var version = start[0]; + var status = Integer.parseInt(start[1]); + var reason = start[2]; + + var headers = new ArrayList
(); + for (int i = 1; i < lines.length; i++) { + split = lines[i].split(": ", 2); + headers.add(new Header(split[0], split[1])); + } + return new HttpResponse(version, status, reason, body, headers); + } catch (IndexOutOfBoundsException | NumberFormatException e) { + throw new InvalidResponseException(); + } + } + + public static class InvalidResponseException extends Exception {} +} -- cgit v1.2.3-70-g09d2