Search in sources :

Example 31 with Header

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

the class FullResponse method uncompressBodyAndAssertContainsString.

public void uncompressBodyAndAssertContainsString(String text) {
    Header header = getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_ENCODING);
    if (header == null)
        throw new IllegalStateException("Body is not compressed as no CONTENT_ENCODING header field exists");
    else if (!"gzip".equals(header.getValue()))
        throw new IllegalStateException("Body has wrong compression type=" + header.getValue() + " in CONTENT_ENCODING header field");
    DataWrapper wrapper = getBody();
    byte[] compressed = wrapper.createByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(compressed);
    byte[] out = new byte[10000];
    DataWrapper output = dataGen.emptyWrapper();
    try (GZIPInputStream str = new GZIPInputStream(in)) {
        int read = 0;
        while ((read = str.read(out)) > 0) {
            ByteBuffer buffer = ByteBuffer.wrap(out, 0, read);
            DataWrapper byteWrapper = dataGen.wrapByteBuffer(buffer);
            output = dataGen.chainDataWrappers(output, byteWrapper);
            out = new byte[10000];
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Charset charset = extractCharset();
    String bodyAsString = output.createStringFrom(0, output.getReadableSize(), charset);
    if (!bodyAsString.contains(text))
        throw new IllegalStateException("Expected compressed body to contain='" + text + "' but body was=" + bodyAsString);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.webpieces.httpparser.api.common.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 32 with Header

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

the class TestHttp11Basic method testSendTwoRequestsStreamFirst.

@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.write(data1);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.write(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    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) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 33 with Header

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

the class TestHttp11Basic method testUploadWithBody.

@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
    String bodyStr = "hi there, how are you";
    DataWrapper dataWrapper = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpData body = new HttpData(dataWrapper, true);
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
    mockChannel.write(req);
    mockChannel.write(body);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
    DataWrapper data = frame.getData();
    Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
    Assert.assertTrue(frame.isEndOfStream());
    HttpResponse resp = Requests.createNobodyResponse();
    Http2Response http2Resp = Http2Translations.responseToHeaders(resp);
    CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
    fut.get(2, TimeUnit.SECONDS);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) StreamWriter(com.webpieces.http2engine.api.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 34 with Header

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

the class Http2Translations method insertInfo.

private static void insertInfo(HttpRequest req, Http2Header header) {
    if (headersToSkip.contains(header.getName()))
        return;
    if (header.getKnownName() == Http2HeaderName.AUTHORITY) {
        //this keeps header order which is sometimes important
        req.addHeader(new Header(KnownHeaderName.HOST, header.getValue()));
        return;
    }
    String name = translateName(header.getName());
    req.addHeader(new Header(name, header.getValue()));
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header)

Example 35 with Header

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

the class Http2Translations method responseToHeaders.

public static Http2Response responseToHeaders(HttpResponse response) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.STATUS, response.getStatusLine().getStatus().getCode().toString()));
    for (Header header : response.getHeaders()) {
        if (header.getKnownName() == KnownHeaderName.TRANSFER_ENCODING) {
            if ("chunked".equals(header.getValue())) {
                //skip as http2 does not allow this header
                continue;
            }
        }
        headers.add(new Http2Header(header.getName(), header.getValue()));
    }
    Http2Response resp = new Http2Response(headers);
    Header header = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (header != null) {
        int len = Integer.parseInt(header.getValue());
        if (len == 0) {
            resp.setEndOfStream(true);
        } else {
            resp.setEndOfStream(false);
        }
    } else {
        resp.setEndOfStream(false);
    }
    return resp;
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) ArrayList(java.util.ArrayList)

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