Search in sources :

Example 1 with HttpChunk

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

the class HttpParserImpl method readInChunkBody.

//Returns true if body read in and false otherwise
private void readInChunkBody(MementoImpl memento, boolean stripAndCompareLastTwo) {
    HttpChunk message = memento.getHalfParsedChunk();
    DataWrapper dataToRead = memento.getLeftOverData();
    int readableSize = dataToRead.getReadableSize();
    int numBytesNeeded = memento.getNumBytesLeftToReadOnChunk();
    if (numBytesNeeded <= readableSize) {
        List<? extends DataWrapper> split = dataGen.split(dataToRead, numBytesNeeded);
        DataWrapper data = split.get(0);
        if (stripAndCompareLastTwo) {
            List<? extends DataWrapper> splitPieces = dataGen.split(data, data.getReadableSize() - 2);
            data = splitPieces.get(0);
            DataWrapper trailer = splitPieces.get(1);
            String trailerStr = trailer.createStringFrom(0, trailer.getReadableSize(), iso8859_1);
            if (!TRAILER_STR.equals(trailerStr))
                throw new IllegalStateException("The chunk did not end with \\r\\n .  The format is invalid");
        }
        message.setBody(data);
        memento.setLeftOverData(split.get(1));
        memento.setNumBytesLeftToReadOnChunk(0);
        memento.addMessage(message);
        //clear any cached message we were waiting for more data for
        memento.setHalfParsedChunk(null);
        return;
    }
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 2 with HttpChunk

use of org.webpieces.httpparser.api.dto.HttpChunk 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 (payload.getMessageType() == HttpMessageType.CHUNK || payload.getMessageType() == HttpMessageType.LAST_CHUNK) {
        return 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(iso8859_1);
    return stringPiece;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HttpMessage(org.webpieces.httpparser.api.dto.HttpMessage) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 3 with HttpChunk

use of org.webpieces.httpparser.api.dto.HttpChunk 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<>();
    String chunkMetaStr = chunkMetaData.createStringFrom(0, chunkMetaData.getReadableSize(), iso8859_1);
    String hexSize = chunkMetaStr.trim();
    if (chunkMetaStr.contains(";")) {
        String[] extensionsArray = chunkMetaStr.split(";");
        hexSize = extensionsArray[0];
        for (int n = 1; n < extensionsArray.length; n++) {
            HttpChunkExtension ext = createExtension(extensionsArray[n]);
            extensions.add(ext);
        }
    }
    int chunkSize = Integer.parseInt(hexSize, 16);
    HttpChunk chunk = new HttpChunk();
    if (chunkSize == 0)
        chunk = new HttpLastChunk();
    //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 HttpChunk

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

the class TestHttp11Basic method testFileDownloadWithChunking.

@Test
public void testFileDownloadWithChunking() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    HttpResponse resp = Requests.createResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    Http2Response headers = Http2Translations.responseToHeaders(resp);
    CompletableFuture<StreamWriter> future = in1.stream.sendResponse(headers);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    DataFrame dataFrame = new DataFrame();
    dataFrame.setEndOfStream(true);
    String bodyStr = "hi here and there";
    DataWrapper data = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    dataFrame.setData(data);
    writer.processPiece(dataFrame);
    List<HttpPayload> frames = mockChannel.getFramesAndClear();
    Assert.assertEquals(2, frames.size());
    HttpChunk chunk = (HttpChunk) frames.get(0);
    DataWrapper body = chunk.getBodyNonNull();
    String result = body.createStringFromUtf8(0, body.getReadableSize());
    Assert.assertEquals(bodyStr, result);
    HttpLastChunk last = (HttpLastChunk) frames.get(1);
    Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) 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) DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 5 with HttpChunk

use of org.webpieces.httpparser.api.dto.HttpChunk 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.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    HttpChunk chunk = new HttpChunk();
    String bodyStr = "hi here and there";
    DataWrapper data = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    chunk.setBody(data);
    mockChannel.write(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.write(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.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Aggregations

HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)11 Test (org.junit.Test)7 DataWrapper (org.webpieces.data.api.DataWrapper)7 Header (org.webpieces.httpparser.api.common.Header)6 HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)6 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)5 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)5 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)2 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)2 HttpChunkExtension (org.webpieces.httpparser.api.dto.HttpChunkExtension)2 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)2 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 StreamWriter (com.webpieces.http2engine.api.StreamWriter)1 ArrayList (java.util.ArrayList)1 HttpMessage (org.webpieces.httpparser.api.dto.HttpMessage)1