use of org.webpieces.httpparser.api.common.Header 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());
}
use of org.webpieces.httpparser.api.common.Header 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());
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Http1_1StreamImpl method validateHeader.
private void validateHeader(HttpResponse response) {
Header contentLenHeader = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (contentLenHeader == null)
throw new IllegalArgumentException("Content Length header required and missing and should be set to zero");
else if (contentLenHeader.getValue() == null)
throw new IllegalArgumentException("Content Length header found but it's value is null");
int len = Integer.parseInt(contentLenHeader.getValue());
if (len != 0)
throw new IllegalArgumentException("Content Length header found but it's value is 0 while response.isEndOfStream is true. this is contradictory");
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Http2Translations method translateResponse.
public static HttpResponse translateResponse(Http2Response headers) {
HttpResponseStatus status = new HttpResponseStatus();
HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
statusLine.setStatus(status);
HttpResponse response = new HttpResponse();
response.setStatusLine(statusLine);
for (Http2Header header : headers.getHeaders()) {
if (header.getKnownName() == Http2HeaderName.STATUS) {
fillStatus(header, status);
} else if ("reason".equals(header.getName())) {
fillReason(header, status);
} else if (header.getKnownName() == Http2HeaderName.SCHEME) {
//do nothing and drop it
} else {
Header http1Header = convertHeader(header);
response.addHeader(http1Header);
}
}
if (headers.isEndOfStream() && headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
//firefox really needs content length
response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
}
if (status.getCode() == null)
throw new IllegalArgumentException("The header :status is required to send the response");
return response;
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamSecond.
@Test
public void testSendTwoRequestsStreamSecond() throws InterruptedException, ExecutionException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
req2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
byte[] buf = new byte[10];
DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.write(data1);
DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.write(data2);
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);
}
Aggregations