All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rawhttp.cli.HttpResponses Maven / Gradle / Ivy

package rawhttp.cli;

import rawhttp.core.EagerHttpResponse;
import rawhttp.core.HttpVersion;
import rawhttp.core.RawHttpHeaders;
import rawhttp.core.RawHttpResponse;
import rawhttp.core.StatusLine;
import rawhttp.core.body.EagerBodyReader;

import static java.nio.charset.StandardCharsets.US_ASCII;

final class HttpResponses {

    private static final StatusLine STATUS_200_HTTP1_0;
    private static final StatusLine STATUS_200_HTTP1_1;
    private static final StatusLine STATUS_405_HTTP1_0;
    private static final StatusLine STATUS_405_HTTP1_1;

    private static final RawHttpResponse OK_RESPONSE_HTTP1_0;
    private static final RawHttpResponse OK_RESPONSE_HTTP1_1;
    private static final EagerHttpResponse METHOD_NOT_ALLOWED_RESPONSE_HTTP1_0;
    private static final EagerHttpResponse METHOD_NOT_ALLOWED_RESPONSE_HTTP1_1;

    static {
        STATUS_200_HTTP1_0 = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
        STATUS_200_HTTP1_1 = new StatusLine(HttpVersion.HTTP_1_1, 200, "OK");
        STATUS_405_HTTP1_0 = new StatusLine(HttpVersion.HTTP_1_0, 405, "Method Not Allowed");
        STATUS_405_HTTP1_1 = new StatusLine(HttpVersion.HTTP_1_1, 405, "Method Not Allowed");

        final RawHttpHeaders basicHeaders = RawHttpHeaders.newBuilderSkippingValidation()
                .with("Content-Type", "text/plain")
                .with("Cache-Control", "no-cache")
                .with("Pragma", "no-cache")
                .build();

        OK_RESPONSE_HTTP1_0 = new EagerHttpResponse<>(null, null, STATUS_200_HTTP1_0, basicHeaders, null);
        OK_RESPONSE_HTTP1_1 = OK_RESPONSE_HTTP1_0.withStatusLine(STATUS_200_HTTP1_1);

        byte[] status405body = "Method not allowed.".getBytes(US_ASCII);

        METHOD_NOT_ALLOWED_RESPONSE_HTTP1_0 = new EagerHttpResponse<>(null, null,
                STATUS_405_HTTP1_0, RawHttpHeaders.newBuilderSkippingValidation(basicHeaders)
                .overwrite("Content-Length", Integer.toString(status405body.length))
                .build(), new EagerBodyReader(status405body));

        METHOD_NOT_ALLOWED_RESPONSE_HTTP1_1 = METHOD_NOT_ALLOWED_RESPONSE_HTTP1_0
                .withStatusLine(STATUS_405_HTTP1_1);
    }

    static RawHttpResponse getOkResponse(HttpVersion httpVersion) {
        if (httpVersion.isOlderThan(HttpVersion.HTTP_1_1)) {
            return OK_RESPONSE_HTTP1_0;
        } else {
            return OK_RESPONSE_HTTP1_1;
        }
    }

    static EagerHttpResponse getMethodNotAllowedResponse(HttpVersion httpVersion) {
        if (httpVersion.isOlderThan(HttpVersion.HTTP_1_1)) {
            return METHOD_NOT_ALLOWED_RESPONSE_HTTP1_0;
        } else {
            return METHOD_NOT_ALLOWED_RESPONSE_HTTP1_1;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy