Search in sources :

Example 41 with Header

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

the class TestRequestBody method createPostRequestWithBody.

private HttpDummyRequest createPostRequestWithBody() {
    byte[] payload = new byte[30];
    for (int i = 0; i < payload.length; i++) {
        payload[i] = (byte) i;
    }
    HttpRequest request = TestRequestParsing.createPostRequest();
    Header header = new Header();
    header.setName(KnownHeaderName.CONTENT_LENGTH);
    header.setValue("" + payload.length);
    DataWrapper data = dataGen.wrapByteArray(payload);
    request.addHeader(header);
    HttpData httpData = new HttpData(data, true);
    return new HttpDummyRequest(request, httpData);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 42 with Header

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

the class HeaderPriorityParserImpl method parseCookiesFromRequest.

@Override
public Map<String, String> parseCookiesFromRequest(HttpRequest req) {
    Header cookieHeader = req.getHeaderLookupStruct().getHeader(KnownHeaderName.COOKIE);
    if (cookieHeader == null)
        return new HashMap<>();
    String value = cookieHeader.getValue();
    String[] split = value.trim().split(";");
    Map<String, String> map = new HashMap<>();
    for (String keyValPair : split) {
        //there are many = signs but the first one is the cookie name...the other are embedded key=value pairs
        int index = keyValPair.indexOf("=");
        String name = keyValPair.substring(0, index).trim();
        String val = keyValPair.substring(index + 1).trim();
        map.put(name, val);
    }
    return map;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HashMap(java.util.HashMap)

Example 43 with Header

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

the class HeaderPriorityParserImpl method parseAcceptEncoding.

@Override
public List<String> parseAcceptEncoding(HttpRequest req) {
    Header langHeader = req.getHeaderLookupStruct().getHeader(KnownHeaderName.ACCEPT_ENCODING);
    if (langHeader == null)
        return new ArrayList<>();
    List<String> headerItems = parsePriorityItems(langHeader.getValue(), s -> s);
    return headerItems;
}
Also used : Header(org.webpieces.httpparser.api.common.Header)

Example 44 with Header

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

the class TestChunkedParsing method testSplitChunkBodyAndResponseAfter.

@Test
public void testSplitChunkBodyAndResponseAfter() {
    String chunkedData = "1E\r\n012345678901234567890123456789\r\n7\r\nchunks.\r\n0\r\n\r\n";
    HttpResponse resp = TestResponseParsing.createOkResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
    byte[] chunked = chunkedData.getBytes();
    HttpResponse resp400 = create400Response();
    byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
    int lengthOfChunked1stHalf = 15;
    int lengthOfChunked2ndHalf = chunked.length - 15;
    byte[] firstPiece = new byte[bytes.length + lengthOfChunked1stHalf];
    byte[] secondPiece = new byte[chunked.length - lengthOfChunked1stHalf + tail.length];
    System.arraycopy(bytes, 0, firstPiece, 0, bytes.length);
    System.arraycopy(chunked, 0, firstPiece, bytes.length, lengthOfChunked1stHalf);
    System.arraycopy(chunked, lengthOfChunked1stHalf, secondPiece, 0, lengthOfChunked2ndHalf);
    System.arraycopy(tail, 0, secondPiece, lengthOfChunked2ndHalf, tail.length);
    DataWrapper first = dataGen.wrapByteArray(firstPiece);
    DataWrapper second = dataGen.wrapByteArray(secondPiece);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, first);
    Assert.assertEquals(ParsingState.CHUNK, memento.getUnParsedState().getCurrentlyParsing());
    Assert.assertEquals(11, memento.getUnParsedState().getCurrentUnparsedSize());
    List<HttpPayload> msgs = memento.getParsedMessages();
    Assert.assertEquals(1, msgs.size());
    Assert.assertEquals(resp, msgs.get(0));
    memento = parser.parse(memento, second);
    List<HttpPayload> parsedMessages = memento.getParsedMessages();
    Assert.assertEquals(4, parsedMessages.size());
    Assert.assertEquals(resp400, parsedMessages.get(3));
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Example 45 with Header

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

the class TestChunkedParsing method testMarshalOut.

@Test
public void testMarshalOut() {
    DataWrapper payload1 = dataGen.wrapByteArray("0123456789".getBytes());
    HttpChunk chunk = new HttpChunk();
    chunk.addExtension(new HttpChunkExtension("asdf", "value"));
    chunk.addExtension(new HttpChunkExtension("something"));
    chunk.setBody(payload1);
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, chunk));
    String str = new String(payload);
    Assert.assertEquals("a;asdf=value;something\r\n0123456789\r\n", str);
    HttpLastChunk lastChunk = new HttpLastChunk();
    lastChunk.addExtension(new HttpChunkExtension("this", "that"));
    lastChunk.addHeader(new Header("customer", "value"));
    String lastPayload = parser.marshalToString(lastChunk);
    Assert.assertEquals("0;this=that\r\ncustomer: value\r\n\r\n", lastPayload);
    byte[] lastBytes = unwrap(parser.marshalToByteBuffer(state, lastChunk));
    String lastPayloadFromBytes = new String(lastBytes, HttpParserFactory.iso8859_1);
    Assert.assertEquals("0;this=that\r\ncustomer: value\r\n\r\n", lastPayloadFromBytes);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) HttpChunkExtension(org.webpieces.httpparser.api.dto.HttpChunkExtension) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

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