use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class FullResponse method uncompressBodyAndAssertContainsString.
public void uncompressBodyAndAssertContainsString(String text) {
Header header = getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_ENCODING);
if (header == null)
throw new IllegalStateException("Body is not compressed as no CONTENT_ENCODING header field exists");
else if (!"gzip".equals(header.getValue()))
throw new IllegalStateException("Body has wrong compression type=" + header.getValue() + " in CONTENT_ENCODING header field");
DataWrapper wrapper = getBody();
byte[] compressed = wrapper.createByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(compressed);
byte[] out = new byte[10000];
DataWrapper output = dataGen.emptyWrapper();
try (GZIPInputStream str = new GZIPInputStream(in)) {
int read = 0;
while ((read = str.read(out)) > 0) {
ByteBuffer buffer = ByteBuffer.wrap(out, 0, read);
DataWrapper byteWrapper = dataGen.wrapByteBuffer(buffer);
output = dataGen.chainDataWrappers(output, byteWrapper);
out = new byte[10000];
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Charset charset = extractCharset();
String bodyAsString = output.createStringFrom(0, output.getReadableSize(), charset);
if (!bodyAsString.contains(text))
throw new IllegalStateException("Expected compressed body to contain='" + text + "' but body was=" + bodyAsString);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirst.
@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
byte[] buf = new byte[10];
DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.write(data1);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.write(data2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
//send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
in1.stream.sendResponse(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
in2.stream.sendResponse(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestHttp11Basic method testUploadWithBody.
@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
String bodyStr = "hi there, how are you";
DataWrapper dataWrapper = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpData body = new HttpData(dataWrapper, true);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
mockChannel.write(req);
mockChannel.write(body);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2Translations.translateRequest(in1.request);
Assert.assertEquals(req, req1);
DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
DataWrapper data = frame.getData();
Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
Assert.assertTrue(frame.isEndOfStream());
HttpResponse resp = Requests.createNobodyResponse();
Http2Response http2Resp = Http2Translations.responseToHeaders(resp);
CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
fut.get(2, TimeUnit.SECONDS);
HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
Assert.assertEquals(resp, respToClient);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Http2Translations method insertInfo.
private static void insertInfo(HttpRequest req, Http2Header header) {
if (headersToSkip.contains(header.getName()))
return;
if (header.getKnownName() == Http2HeaderName.AUTHORITY) {
//this keeps header order which is sometimes important
req.addHeader(new Header(KnownHeaderName.HOST, header.getValue()));
return;
}
String name = translateName(header.getName());
req.addHeader(new Header(name, header.getValue()));
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Http2Translations method responseToHeaders.
public static Http2Response responseToHeaders(HttpResponse response) {
List<Http2Header> headers = new ArrayList<>();
headers.add(new Http2Header(Http2HeaderName.STATUS, response.getStatusLine().getStatus().getCode().toString()));
for (Header header : response.getHeaders()) {
if (header.getKnownName() == KnownHeaderName.TRANSFER_ENCODING) {
if ("chunked".equals(header.getValue())) {
//skip as http2 does not allow this header
continue;
}
}
headers.add(new Http2Header(header.getName(), header.getValue()));
}
Http2Response resp = new Http2Response(headers);
Header header = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (header != null) {
int len = Integer.parseInt(header.getValue());
if (len == 0) {
resp.setEndOfStream(true);
} else {
resp.setEndOfStream(false);
}
} else {
resp.setEndOfStream(false);
}
return resp;
}
Aggregations