Search in sources :

Example 1 with HttpChunkExtension

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

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

DataWrapper (org.webpieces.data.api.DataWrapper)2 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)2 HttpChunkExtension (org.webpieces.httpparser.api.dto.HttpChunkExtension)2 HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Header (org.webpieces.httpparser.api.common.Header)1