Search in sources :

Example 11 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp11Errors method testCloseBeforeFirstRequestCompletes.

@Test
public void testCloseBeforeFirstRequestCompletes() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    XFuture<StreamWriter> futA = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefA = new MockStreamRef(futA);
    mockListener.addMockStreamToReturn(mockStreamRefA);
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    XFuture<StreamWriter> futB = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefB = new MockStreamRef(futB);
    mockListener.addMockStreamToReturn(mockStreamRefB);
    XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
    Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
    XFuture<StreamWriter> future = in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    Assert.assertFalse(mockStreamRefA.isCancelled());
    Assert.assertFalse(mockStreamRefB.isCancelled());
    mockChannel.simulateClose();
    // this request is done, nothing to cancel
    Assert.assertTrue(mockStreamRefA.isCancelled());
    Assert.assertFalse(mockStreamRefB.isCancelled());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 12 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp1Backpressure method testBasicBackpressure.

@Test
public void testBasicBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 250);
    HttpData response2 = Requests.createHttpData(250);
    List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since data is cached in parser (client doesn't receive this data)
    Assert.assertTrue(fut1.isDone());
    // This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
    // in a full HttpData packet actually...
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter mockWriter = new MockStreamWriter();
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases BOTH packets above to be processed!!!(not just the one)
    requestFuture.complete(mockWriter);
    Assert.assertFalse(fut2.isDone());
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the last buffer in
    XFuture<Void> future3 = new XFuture<Void>();
    mockWriter.addProcessResponse(future3);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    future3.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) HttpData(org.webpieces.httpparser.api.dto.HttpData) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) Test(org.junit.Test)

Example 13 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestWriteReads method testBasicReadWrite.

@Test
public void testBasicReadWrite() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    MockResponseListener listener2 = new MockResponseListener();
    request.getHeaderLookupStruct().getHeader("serverid").setValue("2");
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    StreamRef streamRef1 = handle.process(request, listener2);
    XFuture<StreamWriter> writer2 = streamRef1.getWriter();
    Assert.assertTrue(writer2.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 0);
    listener.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut1 = mockChannel.writeResponse(response1);
    // throws if exception occurred and ensures future resolved
    fut1.get(2, TimeUnit.SECONDS);
    Http2Response msg = listener.getIncomingMsg();
    Assert.assertEquals(response1, msg);
    Http2Response response2 = Requests.createResponse(2, 0);
    listener2.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut2 = mockChannel.writeResponse(response2);
    // throws if exception occurred and ensures future resolved
    fut2.get(2, TimeUnit.SECONDS);
    Http2Response msg2 = listener2.getIncomingMsg();
    Assert.assertEquals(response2, msg2);
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Example 14 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class CompressionChunkingHandle method process.

@Override
public XFuture<StreamWriter> process(Http2Response response) {
    if (lastResponseSent != null)
        throw new IllegalStateException("You already sent a response.  " + "do not call Actions.redirect or Actions.render more than once.  previous response=" + lastResponseSent + " 2nd response=" + response);
    lastResponseSent = response;
    Compression compression = checkForCompression(response);
    ChunkedStream chunkedStream = new ChunkedStream(webSettings.getMaxBodySizeToSend());
    Http2Response finalResp = response;
    if (Current.isContextSet()) {
        // in some exceptional cases like incoming cookies failing to parse, there will be no context
        List<OverwritePlatformResponse> callbacks = Current.getContext().getCallbacks();
        for (OverwritePlatformResponse callback : callbacks) {
            finalResp = (Http2Response) callback.modifyOrReplace(finalResp);
        }
    }
    boolean closeAfterResponding = false;
    if (closeAfterResponding(originalRequest))
        closeAfterResponding = true;
    boolean shouldClose = closeAfterResponding;
    return handler.process(response).thenApply(w -> possiblyClose(shouldClose, response, w)).thenApply(w -> new ProxyStreamWriter(shouldClose, compression, chunkedStream, w));
}
Also used : SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Inject(javax.inject.Inject) RouterRequest(org.webpieces.ctx.api.RouterRequest) StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) Map(java.util.Map) DataWrapper(org.webpieces.data.api.DataWrapper) CompressionLookup(org.webpieces.router.impl.compression.CompressionLookup) OutputStream(java.io.OutputStream) WebSettings(org.webpieces.router.impl.routeinvoker.WebSettings) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) IOException(java.io.IOException) List(java.util.List) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) OverwritePlatformResponse(org.webpieces.ctx.api.OverwritePlatformResponse) MimeTypes(org.webpieces.router.impl.compression.MimeTypes) RouterResponseHandler(org.webpieces.router.api.RouterResponseHandler) Http2MsgType(com.webpieces.http2.api.dto.lowlevel.lib.Http2MsgType) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) PushStreamHandle(com.webpieces.http2.api.streaming.PushStreamHandle) Current(org.webpieces.ctx.api.Current) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Compression(org.webpieces.router.impl.compression.Compression) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Compression(org.webpieces.router.impl.compression.Compression) OverwritePlatformResponse(org.webpieces.ctx.api.OverwritePlatformResponse)

Example 15 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class ProxyStreamHandle method sendRedirectAndClearCookie.

public XFuture<StreamWriter> sendRedirectAndClearCookie(RouterRequest req, String badCookieName) {
    RedirectResponse httpResponse = new RedirectResponse(false, req.isHttps, req.domain, req.port, req.relativePath);
    Http2Response response = responseCreator.createRedirect(originalHttp2Request, httpResponse);
    responseCreator.addDeleteCookie(response, badCookieName);
    log.info("sending REDIRECT(due to bad cookie) response responseSender=" + this);
    return process(response);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) RedirectResponse(org.webpieces.router.impl.dto.RedirectResponse)

Aggregations

StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)60 Test (org.junit.Test)43 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)37 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)32 StreamRef (com.webpieces.http2.api.streaming.StreamRef)25 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)24 DataWrapper (org.webpieces.data.api.DataWrapper)19 XFuture (org.webpieces.util.futures.XFuture)17 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)15 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)13 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 MockStreamRef (org.webpieces.httpfrontend2.api.mock2.MockStreamRef)8 Header (org.webpieces.httpparser.api.common.Header)8 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)8 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)7 ByteBuffer (java.nio.ByteBuffer)7 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)7