use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirst.
@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
byte[] buf = new byte[10];
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.sendToSvr(data1);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
DataWrapper dataWrapper2 = DATA_GEN.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.sendToSvr(data2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
XFuture<Void> fut = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut.isDone());
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 = Http11ToHttp2.responseToHeaders(resp1);
in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
fut.get(2, TimeUnit.SECONDS);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http11ToHttp2.responseToHeaders(resp2);
in2.stream.process(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestBasicHttp2Server method testBasicIntegration.
@Test
public void testBasicIntegration() throws InterruptedException, ExecutionException {
MockStreamWriter mockSw = new MockStreamWriter();
mockListener.addMockStreamToReturn(mockSw);
MockStreamWriter mockSw2 = new MockStreamWriter();
mockListener.addMockStreamToReturn(mockSw2);
Http2Request request1 = Http2Requests.createRequest(1, true);
Http2Request request2 = Http2Requests.createRequest(3, true);
mockChannel.send(request1);
PassedIn requestAndStream1 = mockListener.getSingleRequest();
mockChannel.send(request2);
PassedIn requestAndStream2 = mockListener.getSingleRequest();
// each stream given to webapp is a unique one....
Assert.assertTrue(requestAndStream1.stream != requestAndStream2.stream);
Assert.assertEquals(request1, requestAndStream1.request);
Assert.assertEquals(request2, requestAndStream2.request);
Assert.assertEquals(1, request1.getStreamId());
Assert.assertEquals(3, request2.getStreamId());
Http2Response resp2 = Http2Requests.createResponse(request2.getStreamId());
XFuture<StreamWriter> future = requestAndStream2.stream.process(resp2);
Assert.assertTrue(future.isDone());
Http2Response frame2 = (Http2Response) mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, frame2);
Http2Response resp1 = Http2Requests.createResponse(request1.getStreamId());
XFuture<StreamWriter> future1 = requestAndStream1.stream.process(resp1);
Assert.assertTrue(future1.isDone());
Http2Response frame1 = (Http2Response) mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, frame1);
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestS4FrameSizeAndHeaders method testSection4_2FrameTooLarge.
/**
* An endpoint MUST send an error code of FRAME_SIZE_ERROR if a frame
* exceeds the size defined in SETTINGS_MAX_FRAME_SIZE, exceeds any
* limit defined for the frame type, or is too small to contain
* mandatory frame data. A frame size error in a frame that could alter
* the state of the entire connection MUST be treated as a connection
* error (Section 5.4.1); this includes any frame carrying a header
* block (Section 4.3) (that is, HEADERS, PUSH_PROMISE, and
* CONTINUATION), SETTINGS, and any frame with a stream identifier of 0.
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void testSection4_2FrameTooLarge() throws InterruptedException, ExecutionException, TimeoutException {
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
MockStreamRef mockStream = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStream);
int streamId = 1;
PassedIn info = sendRequestToServer(streamId, false);
ResponseStream stream = info.stream;
Http2Request request = info.request;
Assert.assertFalse(mockStream.isCancelled());
// send data that goes with request
DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
byte[] buf = new byte[localSettings.getMaxFrameSize() + 4];
dataFrame.setData(DATA_GEN.wrapByteArray(buf));
// endOfStream=false
mockChannel.send(dataFrame);
// remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.FRAME_SIZE_ERROR, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertEquals("ConnectionException: stream1:(EXCEEDED_MAX_FRAME_SIZE) Frame size=16389 was greater than max=16385", msg);
Assert.assertTrue(mockChannel.isClosed());
Assert.assertTrue(mockListener.isClosed());
Assert.assertTrue(mockStream.isCancelled());
CancelReason failResp = mockStream.getCancelInfo();
ShutdownStream reset = (ShutdownStream) failResp;
Assert.assertEquals(CancelReasonCode.EXCEEDED_MAX_FRAME_SIZE, reset.getCause().getReasonCode());
// send response with request not complete but failed as well anyways
Http2Response response = Http2Requests.createResponse(request.getStreamId());
XFuture<StreamWriter> future = stream.process(response);
ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class Http11StreamImpl method process.
@Override
public XFuture<StreamWriter> process(Http2Response headers) {
closeCheck(headers);
HttpResponse response = Http2ToHttp11.translateResponse(headers);
if (http2Request.getKnownMethod() == Http2Method.CONNECT) {
// bytes so we don't care about parsing anymore(ie. SSL or http)..
return write(response).thenApply(c -> new Http11ChunkedWriter(http1Req, http2Request));
} else 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));
}
return write(response).thenApply(c -> new Http11ChunkedWriter(http1Req, http2Request));
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class ProxyHttpStream method incomingRequest.
@Override
public StreamRef incomingRequest(Http2Request request, ResponseStream stream) {
String expect = request.getSingleHeaderValue("Expect");
XFuture<StreamWriter> future = XFuture.completedFuture(null);
if (expect != null && "100-continue".equals(expect.toLowerCase())) {
Http2Response continueResponse = new Http2Response();
continueResponse.setEndOfStream(false);
continueResponse.addHeader(new Http2Header(Http2HeaderName.STATUS, "100"));
future = stream.process(continueResponse);
}
// This is only for streaming to backpressure clients IF we responded OR cancelled so we don't
// waste CPU on a client stream coming in
XFuture<ProxyWriter> futureWriter = new XFuture<>();
ProxyResponseStream proxy = new ProxyResponseStream(request, stream, futureWriter);
StreamRef streamRef = openStream.incomingRequest(request, proxy);
XFuture<StreamWriter> writer = future.thenCompose(w -> {
return createProxy(streamRef.getWriter(), futureWriter);
});
return new ProxyStreamRef(writer, streamRef);
}
Aggregations