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);
}
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;
}
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;
}
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));
}
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);
}
Aggregations