Search in sources :

Example 16 with HttpRequest

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

the class TestSyncHibernate method saveBean.

private String saveBean(String path) {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.POST, path);
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
    return response.getRedirectUrl();
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse)

Example 17 with HttpRequest

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

the class IntegColoradoEdu method main.

public static void main(String[] args) {
    boolean isHttp = false;
    String host = "www.colorado.edu";
    int port = 443;
    if (isHttp)
        port = 80;
    HttpRequest req = createRequest(host);
    log.info("starting socket");
    ChunkedResponseListener listener = new ChunkedResponseListener();
    HttpSocket socket = IntegGoogleHttps.createSocket(isHttp, host, port);
    socket.connect(new InetSocketAddress(host, port)).thenAccept(p -> socket.send(req, listener)).exceptionally(e -> reportException(socket, e));
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Header(org.webpieces.httpparser.api.common.Header) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) KnownHeaderName(org.webpieces.httpparser.api.common.KnownHeaderName) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) InetSocketAddress(java.net.InetSocketAddress) HttpResponseListener(org.webpieces.httpclient.api.HttpResponseListener) HttpSocket(org.webpieces.httpclient.api.HttpSocket) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) HttpData(org.webpieces.httpparser.api.dto.HttpData) KnownHttpMethod(org.webpieces.httpparser.api.dto.KnownHttpMethod) HttpSocket(org.webpieces.httpclient.api.HttpSocket) InetSocketAddress(java.net.InetSocketAddress)

Example 18 with HttpRequest

use of org.webpieces.httpparser.api.dto.HttpRequest 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 = parseVersion(firstLinePieces[2], firstLine);
    HttpRequestLine httpRequestLine = new HttpRequestLine();
    httpRequestLine.setMethod(method);
    httpRequestLine.setUri(uri);
    httpRequestLine.setVersion(version);
    HttpRequest request = new HttpRequest();
    request.setRequestLine(httpRequestLine);
    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 19 with HttpRequest

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

the class TestRequestBody method testPartialBody.

@Test
public void testPartialBody() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    byte[] first = new byte[data.length - 20];
    byte[] second = new byte[data.length - first.length];
    System.arraycopy(data, 0, first, 0, first.length);
    System.arraycopy(data, first.length, second, 0, second.length);
    DataWrapper wrap1 = dataGen.wrapByteArray(first);
    DataWrapper wrap2 = dataGen.wrapByteArray(second);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(ParsingState.BODY, memento.getUnParsedState().getCurrentlyParsing());
    Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
    HttpRequest httpRequest = memento.getParsedMessages().get(0).getHttpRequest();
    Assert.assertEquals(request.getRequest(), httpRequest);
    HttpData data1 = memento.getParsedMessages().get(1).getHttpData();
    Assert.assertEquals(2, memento.getParsedMessages().size());
    memento = parser.parse(memento, wrap2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    HttpData data2 = memento.getParsedMessages().get(0).getHttpData();
    DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 20 with HttpRequest

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

the class IntegColoradoEdu method createRequest.

private static HttpRequest createRequest(String host) {
    //		GET / HTTP/1.1
    //		Host: www.colorado.edu
    //		User-Agent: curl/7.43.0
    //		Accept: */*
    HttpRequestLine requestLine = new HttpRequestLine();
    requestLine.setMethod(KnownHttpMethod.GET);
    requestLine.setUri(new HttpUri("/"));
    HttpRequest req = new HttpRequest();
    req.setRequestLine(requestLine);
    req.addHeader(new Header(KnownHeaderName.HOST, host));
    req.addHeader(new Header(KnownHeaderName.ACCEPT, "*/*"));
    req.addHeader(new Header(KnownHeaderName.USER_AGENT, "webpieces/0.9"));
    return req;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) HttpUri(org.webpieces.httpparser.api.dto.HttpUri)

Aggregations

HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)157 Test (org.junit.Test)136 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)120 FullResponse (org.webpieces.webserver.test.FullResponse)120 WebserverForTest (org.webpieces.webserver.WebserverForTest)114 Header (org.webpieces.httpparser.api.common.Header)33 DataWrapper (org.webpieces.data.api.DataWrapper)17 PlatformOverridesForTest (org.webpieces.webserver.test.PlatformOverridesForTest)15 HttpData (org.webpieces.httpparser.api.dto.HttpData)13 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)13 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)13 CompletableFuture (java.util.concurrent.CompletableFuture)11 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)8 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)8 Http2Response (com.webpieces.hpack.api.dto.Http2Response)6 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)6 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)4 StreamWriter (com.webpieces.http2engine.api.StreamWriter)3 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)3