Search in sources :

Example 1 with HttpVersion

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

the class ParserUtil method parseVersion.

public HttpVersion parseVersion(String versionString, String firstLine) {
    if (!versionString.startsWith("HTTP/")) {
        throw new ParseException("Invalid version in http request first line not prefixed with HTTP/.  line=" + firstLine);
    }
    String ver = versionString.substring(5, versionString.length());
    HttpVersion version = new HttpVersion();
    version.setVersion(ver);
    return version;
}
Also used : ParseException(org.webpieces.httpparser.api.ParseException) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Example 2 with HttpVersion

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

the class Translations2 method translate.

public static HttpFullRequest translate(FullRequest request) {
    // String scheme = request.getHeaders().getScheme();
    Http2Request headers = request.getHeaders();
    String authority = headers.getAuthority();
    String path = headers.getPath();
    String methodString = headers.getMethodString();
    if (methodString == null)
        throw new IllegalArgumentException("http2 :method header is required");
    else if (authority == null) {
        throw new IllegalArgumentException("http1 required host header so http2 message must have :authority header set");
    }
    HttpRequestLine reqLine = new HttpRequestLine();
    reqLine.setUri(new HttpUri(path));
    reqLine.setMethod(new HttpRequestMethod(methodString));
    reqLine.setVersion(new HttpVersion());
    HttpRequest httpReq = new HttpRequest();
    httpReq.setRequestLine(reqLine);
    DataWrapper data = request.getPayload();
    // translate all other headers here as well...
    for (Http2Header header : headers.getHeaders()) {
        if (// All standard headers go elsewhere except HOST which we do below
        !header.getName().startsWith(":"))
            httpReq.addHeader(new Header(header.getName(), header.getValue()));
    }
    httpReq.addHeader(new Header(KnownHeaderName.HOST, authority));
    HttpFullRequest req = new HttpFullRequest(httpReq, data);
    return req;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequestMethod(org.webpieces.httpparser.api.dto.HttpRequestMethod) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Example 3 with HttpVersion

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

the class HttpParserImpl method parseRequest.

private HttpMessage parseRequest(MementoImpl memento, List<String> lines) {
    // remove first line...
    String firstLine = lines.remove(0);
    String[] firstLinePieces = firstLine.split("\\s+");
    if (firstLinePieces.length != 3) {
        throw new ParseException("Unable to parse invalid http request due to first line being invalid=" + firstLine + " all Lines=" + lines);
    }
    HttpRequestMethod method = new HttpRequestMethod(firstLinePieces[0]);
    HttpUri uri = new HttpUri(firstLinePieces[1]);
    HttpVersion version = parserUtil.parseVersion(firstLinePieces[2], firstLine);
    HttpRequestLine httpRequestLine = new HttpRequestLine();
    httpRequestLine.setMethod(method);
    httpRequestLine.setUri(uri);
    httpRequestLine.setVersion(version);
    HttpRequest request = new HttpRequest();
    request.setRequestLine(httpRequestLine);
    parserUtil.parseHeaders(lines, request);
    memento.addMessage(request);
    return request;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestMethod(org.webpieces.httpparser.api.dto.HttpRequestMethod) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) ParseException(org.webpieces.httpparser.api.ParseException) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Example 4 with HttpVersion

use of org.webpieces.httpparser.api.dto.HttpVersion 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 = parserUtil.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);
    parserUtil.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 5 with HttpVersion

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

the class HttpParserImpl method parseVersion.

private HttpVersion parseVersion(String versionString, String firstLine) {
    if (!versionString.startsWith("HTTP/")) {
        throw new ParseException("Invalid version in http request first line not prefixed with HTTP/.  line=" + firstLine);
    }
    String ver = versionString.substring(5, versionString.length());
    HttpVersion version = new HttpVersion();
    version.setVersion(ver);
    return version;
}
Also used : ParseException(org.webpieces.httpparser.api.ParseException) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Aggregations

HttpVersion (org.webpieces.httpparser.api.dto.HttpVersion)5 ParseException (org.webpieces.httpparser.api.ParseException)3 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)2 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)2 HttpRequestMethod (org.webpieces.httpparser.api.dto.HttpRequestMethod)2 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)2 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)1 Header (org.webpieces.httpparser.api.common.Header)1 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)1 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)1 HttpResponseStatusLine (org.webpieces.httpparser.api.dto.HttpResponseStatusLine)1