use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.
the class Http1_1StreamImpl method sendResponse.
@Override
public CompletableFuture<StreamWriter> sendResponse(Http2Response headers) {
closeCheck();
HttpResponse response = Http2Translations.translateResponse(headers);
if (headers.isEndOfStream()) {
validateHeader(response);
remove(headers);
return write(response).thenApply(w -> {
permitQueue.releasePermit();
return new NoWritesWriter();
});
} else if (contentLengthGreaterThanZero(headers)) {
return write(response).thenApply(w -> new ContentLengthResponseWriter(headers));
}
response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
return write(response).thenApply(c -> new Http11ChunkedWriter());
}
use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.
the class MockTcpChannel method write.
@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
DataWrapper data = dataGen.wrapByteBuffer(b);
memento = parser.parse(memento, data);
List<HttpPayload> parsedMessages = memento.getParsedMessages();
for (HttpPayload payload : parsedMessages) {
if (payload instanceof HttpResponse) {
sendResponse((HttpResponse) payload);
} else {
sendData((HttpData) payload);
}
}
return CompletableFuture.completedFuture(this);
}
use of org.webpieces.httpparser.api.dto.HttpResponse 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.dto.HttpResponse 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.dto.HttpResponse in project webpieces by deanhiller.
the class HttpSocketImpl method send.
@Override
public CompletableFuture<HttpResponse> send(HttpRequest request) {
CompletableFuture<HttpResponse> future = new CompletableFuture<HttpResponse>();
HttpResponseListener l = new CompletableListener(future);
send(request, l);
return future;
}
Aggregations