Search in sources :

Example 1 with HttpLastChunk

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

the class TestHttp11Basic method testPostWithChunking.

@Test
public void testPostWithChunking() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    HttpChunk chunk = new HttpChunk();
    String bodyStr = "hi here and there";
    DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    chunk.setBody(data);
    mockChannel.sendToSvr(chunk);
    DataFrame singleFrame = (DataFrame) mockStreamWriter.getSingleFrame();
    Assert.assertTrue(!singleFrame.isEndOfStream());
    DataWrapper data2 = singleFrame.getData();
    String result = data2.createStringFromUtf8(0, data2.getReadableSize());
    Assert.assertEquals(bodyStr, result);
    HttpLastChunk last = new HttpLastChunk();
    mockChannel.sendToSvr(last);
    DataFrame lastEmpty = (DataFrame) mockStreamWriter.getSingleFrame();
    Assert.assertTrue(lastEmpty.isEndOfStream());
    Assert.assertEquals(0, lastEmpty.getData().getReadableSize());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 2 with HttpLastChunk

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

the class TestHttp11Backpressure method create4SplitPayloads.

private List<ByteBuffer> create4SplitPayloads() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    HttpChunk chunk = new HttpChunk();
    String bodyStr = "hi here and there";
    DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    chunk.setBody(data);
    HttpLastChunk lastChunk = new HttpLastChunk();
    HttpStatefulParser parser = HttpParserFactory.createStatefulParser("a", new SimpleMeterRegistry(), new TwoPools("pl", new SimpleMeterRegistry()));
    ByteBuffer buf1 = parser.marshalToByteBuffer(req);
    ByteBuffer buf2 = parser.marshalToByteBuffer(chunk);
    ByteBuffer buf3 = parser.marshalToByteBuffer(lastChunk);
    byte[] part1 = new byte[10];
    byte[] part2 = new byte[buf1.remaining()];
    buf1.get(part1);
    int toWrite = buf1.remaining();
    buf1.get(part2, 0, toWrite);
    buf2.get(part2, toWrite, part2.length - toWrite);
    byte[] part3 = new byte[buf2.remaining() + 2];
    int toWrite2 = buf2.remaining();
    buf2.get(part3, 0, toWrite2);
    buf3.get(part3, toWrite2, 2);
    byte[] part4 = new byte[buf3.remaining()];
    buf3.get(part4);
    List<ByteBuffer> buffers = new ArrayList<>();
    buffers.add(ByteBuffer.wrap(part1));
    buffers.add(ByteBuffer.wrap(part2));
    buffers.add(ByteBuffer.wrap(part3));
    buffers.add(ByteBuffer.wrap(part4));
    return buffers;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) TwoPools(org.webpieces.data.api.TwoPools) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) HttpStatefulParser(org.webpieces.httpparser.api.HttpStatefulParser) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 3 with HttpLastChunk

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

the class HttpParserImpl method createHttpChunk.

private HttpChunk createHttpChunk(MementoImpl memento, int i) {
    DataWrapper dataToRead = memento.getLeftOverData();
    // split off the header AND /r/n (ie. the +2)
    List<? extends DataWrapper> split = dataGen.split(dataToRead, i + 2);
    DataWrapper chunkMetaData = split.get(0);
    memento.setLeftOverData(split.get(1));
    List<HttpChunkExtension> extensions = new ArrayList<>();
    int chunkSize = parseExtensions(chunkMetaData, extensions);
    HttpChunk chunk = new HttpChunk();
    if (chunkSize == 0)
        chunk = new HttpLastChunk();
    chunk.setExtensions(extensions);
    // must read in all the data of the chunk AND /r/n
    int size = 2 + chunkSize;
    memento.setNumBytesLeftToReadOnChunk(size);
    return chunk;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) ArrayList(java.util.ArrayList) HttpChunkExtension(org.webpieces.httpparser.api.dto.HttpChunkExtension) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 4 with HttpLastChunk

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

the class HttpParserImpl method marshalToBytes.

public byte[] marshalToBytes(MarshalState s, HttpPayload payload) {
    MarshalStateImpl state = (MarshalStateImpl) s;
    if (state.getParsingDataSize() != null) {
        return parseData(state, payload);
    } else if (HttpData.class == payload.getClass()) {
        // in case server just sends it's received HttpData to a client, we have to translate it back to HttpChunk
        HttpChunk chunk = parserUtil.translateData((HttpData) payload);
        return parserUtil.chunkedBytes(chunk);
    } else if (HttpLastData.class == payload.getClass()) {
        HttpLastChunk lastChunk = parserUtil.translate((HttpLastData) payload);
        return parserUtil.chunkedBytes(lastChunk);
    } else if (payload.getMessageType() == HttpMessageType.CHUNK || payload.getMessageType() == HttpMessageType.LAST_CHUNK) {
        return parserUtil.chunkedBytes((HttpChunk) payload);
    }
    HttpMessage msg = (HttpMessage) payload;
    String result = marshalHeaders(payload);
    Header header = msg.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (header != null && !header.getValue().equals("0")) {
        String value = header.getValue();
        int lengthOfBodyFromHeader = toInteger(value, "" + header);
        state.setParsingDataSize(lengthOfBodyFromHeader);
    }
    byte[] stringPiece = result.getBytes(ParserUtil.ISO8859_1);
    return stringPiece;
}
Also used : HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpMessage(org.webpieces.httpparser.api.dto.HttpMessage) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 5 with HttpLastChunk

use of org.webpieces.httpparser.api.dto.HttpLastChunk 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

HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)9 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)8 Header (org.webpieces.httpparser.api.common.Header)7 DataWrapper (org.webpieces.data.api.DataWrapper)6 Test (org.junit.Test)5 HttpChunkExtension (org.webpieces.httpparser.api.dto.HttpChunkExtension)4 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)2 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)1 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)1 StreamRef (com.webpieces.http2.api.streaming.StreamRef)1 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 TwoPools (org.webpieces.data.api.TwoPools)1 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)1 MockStreamWriter (org.webpieces.httpclient.api.mocks.MockStreamWriter)1 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)1