Search in sources :

Example 26 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestHttp11Basic method testSendTwoRequests.

@Test
public void testSendTwoRequests() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
    in2.stream.sendResponse(headers2);
    HttpPayload payload2 = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, payload2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 27 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class Http2Translations method requestToHeaders.

private static Http2Request requestToHeaders(HttpRequest request, boolean fromSslChannel) {
    HttpRequestLine requestLine = request.getRequestLine();
    List<Header> requestHeaders = request.getHeaders();
    LinkedList<Http2Header> headerList = new LinkedList<>();
    // add special headers
    headerList.add(new Http2Header(":method", requestLine.getMethod().getMethodAsString()));
    UrlInfo urlInfo = requestLine.getUri().getUriBreakdown();
    headerList.add(new Http2Header(":path", urlInfo.getFullPath()));
    // Figure out scheme
    if (urlInfo.getPrefix() != null) {
        headerList.add(new Http2Header(":scheme", urlInfo.getPrefix()));
    } else if (fromSslChannel) {
        headerList.add(new Http2Header(":scheme", "https"));
    } else {
        headerList.add(new Http2Header(":scheme", "http"));
    }
    // Figure out authority
    Header hostHeader = request.getHeaderLookupStruct().getHeader(KnownHeaderName.HOST);
    if (hostHeader == null)
        throw new IllegalArgumentException("Host header is required in http1.1");
    // Add regular headers
    for (Header header : requestHeaders) {
        if (header.getKnownName() == KnownHeaderName.HOST) {
            //keeps headers in order of http1 headers
            String h = hostHeader.getValue();
            headerList.add(new Http2Header(":authority", h));
            continue;
        }
        headerList.add(new Http2Header(header.getName().toLowerCase(), header.getValue()));
    }
    Http2Request headers = new Http2Request(headerList);
    Header contentLen = request.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (request.isHasChunkedTransferHeader()) {
        headers.setEndOfStream(false);
    } else if (contentLenGreaterThanZero(contentLen)) {
        headers.setEndOfStream(false);
    } else
        headers.setEndOfStream(true);
    return headers;
}
Also used : UrlInfo(org.webpieces.httpparser.api.dto.UrlInfo) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) Http2Request(com.webpieces.hpack.api.dto.Http2Request) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) LinkedList(java.util.LinkedList)

Example 28 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestHttps method simulateLogin.

private Header simulateLogin() {
    HttpRequest req1 = Requests.createRequest(KnownHttpMethod.POST, "/postLogin");
    httpsSocket.send(req1);
    FullResponse response1 = ResponseExtract.assertSingleResponse(httpsSocket);
    Header header = response1.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
    String value = header.getValue();
    value = value.replace("; path=/; HttpOnly", "");
    Header cookie = new Header(KnownHeaderName.COOKIE, value);
    return cookie;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header)

Example 29 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestScopes method testGetStaticFileDoesNotClearFlashMessage.

@Test
public void testGetStaticFileDoesNotClearFlashMessage() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/flashmessage");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    Header header = response.createCookieRequestHeader();
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/public/fonts.css");
    req2.addHeader(header);
    http11Socket.send(req2);
    FullResponse response2 = ResponseExtract.assertSingleResponse(http11Socket);
    response2.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    Header cookie = response2.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
    Assert.assertNull("static routes should not be clearing cookies or things go south", cookie);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 30 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestScopes method testSessionScopeModificationByClient.

@Test
public void testSessionScopeModificationByClient() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/home");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("age=30");
    Header cookie = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
    String value = cookie.getValue();
    value = value.replace("age=30", "age=60");
    value = value.replace("; path=/; HttpOnly", "");
    //modify cookie and rerequest...
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/displaySession");
    req2.addHeader(new Header(KnownHeaderName.COOKIE, value));
    http11Socket.send(req2);
    response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
    Header cookie2 = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
    String deleteCookie = cookie2.getValue();
    Assert.assertEquals("webSession=; Max-Age=0; path=/; HttpOnly", deleteCookie);
    Header header = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.LOCATION);
    String url = header.getValue();
    Assert.assertEquals("http://myhost.com/displaySession", url);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Aggregations

Header (org.webpieces.httpparser.api.common.Header)64 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)32 Test (org.junit.Test)24 DataWrapper (org.webpieces.data.api.DataWrapper)18 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)16 FullResponse (org.webpieces.webserver.test.FullResponse)16 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)13 WebserverForTest (org.webpieces.webserver.WebserverForTest)12 HttpData (org.webpieces.httpparser.api.dto.HttpData)11 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)10 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)10 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)10 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)9 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)9 Http2Response (com.webpieces.hpack.api.dto.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)5 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4