Search in sources :

Example 1 with HttpResponseStatus

use of org.webpieces.httpparser.api.dto.HttpResponseStatus in project webpieces by deanhiller.

the class HttpParserImpl method parseResponse.

private HttpMessage parseResponse(MementoImpl memento, List<String> lines) {
    //remove first line...
    String firstLine = lines.remove(0);
    //In the case of response, a reason may contain spaces so we must split on first and second
    //whitespace only
    int indexOf = firstLine.indexOf(" ");
    if (indexOf < 0)
        throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
    String versionStr = firstLine.substring(0, indexOf).trim();
    String tail = firstLine.substring(indexOf).trim();
    int indexOf2 = tail.indexOf(" ");
    if (indexOf2 < 0)
        throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
    String codeStr = tail.substring(0, indexOf2).trim();
    String reason = tail.substring(indexOf2).trim();
    HttpVersion version2 = parseVersion(versionStr, firstLine);
    HttpResponseStatus status = new HttpResponseStatus();
    Integer codeVal = toInteger(codeStr, firstLine);
    if (codeVal <= 0 || codeVal >= 1000)
        throw new IllegalArgumentException("invalid status code.  response line=" + firstLine);
    status.setCode(codeVal);
    status.setReason(reason);
    HttpResponseStatusLine httpRequestLine = new HttpResponseStatusLine();
    httpRequestLine.setStatus(status);
    httpRequestLine.setVersion(version2);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(httpRequestLine);
    parseHeaders(lines, response);
    memento.addMessage(response);
    return response;
}
Also used : HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Example 2 with HttpResponseStatus

use of org.webpieces.httpparser.api.dto.HttpResponseStatus in project webpieces by deanhiller.

the class TestChunkedParsing method create400Response.

static HttpResponse create400Response() {
    Header header1 = new Header();
    header1.setName(KnownHeaderName.AGE);
    header1.setValue("CooolValue");
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_400_BADREQUEST);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse resp = new HttpResponse();
    resp.setStatusLine(statusLine);
    resp.addHeader(header1);
    return resp;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 3 with HttpResponseStatus

use of org.webpieces.httpparser.api.dto.HttpResponseStatus in project webpieces by deanhiller.

the class Requests method createNobodyResponse.

public static HttpResponse createNobodyResponse() {
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_200_OK);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse resp = new HttpResponse();
    resp.setStatusLine(statusLine);
    resp.addHeader(new Header(KnownHeaderName.CONNECTION, "keep"));
    resp.addHeader(new Header(KnownHeaderName.AGE, "hh"));
    resp.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    return resp;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 4 with HttpResponseStatus

use of org.webpieces.httpparser.api.dto.HttpResponseStatus in project webpieces by deanhiller.

the class Requests method createResponse.

public static HttpResponse createResponse(int id) {
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_200_OK);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse resp = new HttpResponse();
    resp.setStatusLine(statusLine);
    resp.addHeader(new Header("Server", id + ""));
    resp.addHeader(new Header(KnownHeaderName.CONNECTION, "keep"));
    resp.addHeader(new Header(KnownHeaderName.AGE, "hh"));
    return resp;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 5 with HttpResponseStatus

use of org.webpieces.httpparser.api.dto.HttpResponseStatus in project webpieces by deanhiller.

the class Http2Translations method translateResponse.

public static HttpResponse translateResponse(Http2Response headers) {
    HttpResponseStatus status = new HttpResponseStatus();
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(statusLine);
    for (Http2Header header : headers.getHeaders()) {
        if (header.getKnownName() == Http2HeaderName.STATUS) {
            fillStatus(header, status);
        } else if ("reason".equals(header.getName())) {
            fillReason(header, status);
        } else if (header.getKnownName() == Http2HeaderName.SCHEME) {
        //do nothing and drop it
        } else {
            Header http1Header = convertHeader(header);
            response.addHeader(http1Header);
        }
    }
    if (headers.isEndOfStream() && headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
        //firefox really needs content length
        response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    }
    if (status.getCode() == null)
        throw new IllegalArgumentException("The header :status is required to send the response");
    return response;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Aggregations

HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)8 HttpResponseStatusLine (org.webpieces.httpparser.api.dto.HttpResponseStatusLine)8 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 Header (org.webpieces.httpparser.api.common.Header)5 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)3 Test (org.junit.Test)1 HttpVersion (org.webpieces.httpparser.api.dto.HttpVersion)1