aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/http/HttpRequest.java
blob: 7c27d245ad9b667cae979e553979f857d607caa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package model.http;

import model.util.Uri;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public record HttpRequest(String method, String target, String version, ArrayList<Header> headers, String body) {

    private static final String CRLF = "\r\n";
    private static final String DefaultVersion = "HTTP 0.9";
    private static final Header[] DefaultHeaders = {new Header("User-Agent", "apus")};

    public HttpRequest(String method, String target, String version, ArrayList<Header> headers) {
        this(method, target, version, headers, "");
    }

    // note: we disregard the port, that is taken care of by the connecting socket
    public HttpRequest(String method, Uri uri, String version, String body) {
        // ah, java
        this(method, uri.path(), DefaultVersion, new ArrayList<>(Stream.concat(Stream.of(new Header("", "")), Stream.of(DefaultHeaders)).collect(Collectors.toList())));
    }

    public HttpRequest(String method, Uri uri, String version) {
        this(method, uri, version, "");
    }

    public HttpRequest(String method, Uri uri) {
        this(method, uri, DefaultVersion);
    }

    // yeah, i probably should just use a hashmap
    public String host() throws MalformedRequestException {
        for (Header header : this.headers()) {
            if (header.key().equals("Host")) {
                return header.value();
            }
        }
        throw new MalformedRequestException();
    }

    public static class MalformedRequestException extends Exception {}

    public String raw() {
        String buffer = "";
        buffer += this.method() + " " + this.target() + " " + this.version() + CRLF;
        // hmm maybe i shouldn't use a hashmap
        for (Header header : this.headers()) {
            buffer += header.key() + " " + header.value() + CRLF;
        }
        if (!this.body().equals("")) {
            buffer += this.body() + CRLF;
        }
        buffer += CRLF;
        return buffer;
    }
}