Search in sources :

Example 1 with RequestStreamHandle

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

the class ResponseCacher method run.

public XFuture<FullResponse> run(FullRequest request) {
    SingleResponseListener responseListener = new SingleResponseListener();
    RequestStreamHandle streamHandle = openStreamFunc.get();
    Http2Request req = request.getHeaders();
    if (request.getPayload() == null) {
        request.getHeaders().setEndOfStream(true);
        streamHandle.process(req, responseListener);
        return responseListener.fetchResponseFuture();
    } else if (request.getTrailingHeaders() == null) {
        request.getHeaders().setEndOfStream(false);
        DataFrame data = createData(request, true);
        StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
        return streamRef.getWriter().thenCompose(writer -> {
            data.setStreamId(req.getStreamId());
            return writer.processPiece(data);
        }).thenCompose(writer -> responseListener.fetchResponseFuture());
    }
    request.getHeaders().setEndOfStream(false);
    DataFrame data = createData(request, false);
    Http2Trailers trailers = request.getTrailingHeaders();
    trailers.setEndOfStream(true);
    StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
    return streamRef.getWriter().thenCompose(writer -> writeStuff(writer, req, data, trailers, responseListener));
}
Also used : FullRequest(org.webpieces.http2client.api.dto.FullRequest) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) StreamRef(com.webpieces.http2.api.streaming.StreamRef) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Supplier(java.util.function.Supplier) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 2 with RequestStreamHandle

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

the class TestStreaming method testAsyncJsonGet.

@Test
public void testAsyncJsonGet() {
    String bodyStr = "asdlfkjsldfkjlsfkjdlksjfffffflsdkjfffffldksjflkdjsfldsjf";
    byte[] bytes = bodyStr.getBytes();
    DataWrapper body = gen.wrapByteArray(bytes);
    Http2Request request = Requests.createRequest("/json/streaming", body);
    RequestStreamHandle stream = http2Socket.openStream();
    StreamRef ref = stream.process(request, mockResponseListener);
    XFuture<StreamWriter> writer = ref.getWriter();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) AbstractHttp2Test(org.webpieces.webserver.test.http2.AbstractHttp2Test) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 3 with RequestStreamHandle

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

the class TestCancelStream method testClientCancelWithKeepAlive.

// @Test
// public void testRequestResponseXFutureCancelNoKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
// 
// @Test
// public void testRequestResponseXFutureCancelWithKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
@Test
public void testClientCancelWithKeepAlive() {
    XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
    MockResponseListener mockListener = new MockResponseListener();
    Http2Request req = Requests.createRequest(false);
    req.addHeader(new Http2Header(Http2HeaderName.CONNECTION, "keep-alive"));
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    RequestStreamHandle requestStream = httpSocket.openStream();
    StreamRef ref = requestStream.process(req, mockListener);
    CancelReason reason = new RstStreamFrame();
    XFuture<Void> cancelDone = ref.cancel(reason);
    Assert.assertTrue(cancelDone.isDone());
    // Assert the socket is NOT closed
    Assert.assertFalse(mockChannel.isClosed());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) InetSocketAddress(java.net.InetSocketAddress) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Example 4 with RequestStreamHandle

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

the class TestCancelStream method testServerCloseSocket.

@Test
public void testServerCloseSocket() {
    XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
    MockResponseListener mockListener = new MockResponseListener();
    Http2Request req = Requests.createRequest(false);
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    RequestStreamHandle requestStream = httpSocket.openStream();
    StreamRef ref = requestStream.process(req, mockListener);
    Assert.assertFalse(mockListener.isCancelled());
    mockChannel.simulateClose();
    Assert.assertTrue(mockListener.isCancelled());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) InetSocketAddress(java.net.InetSocketAddress) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Example 5 with RequestStreamHandle

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

the class TestCancelStream method testClientCancelNoKeepAlive.

@Test
public void testClientCancelNoKeepAlive() {
    XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
    MockResponseListener mockListener = new MockResponseListener();
    Http2Request req = Requests.createRequest(false);
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    RequestStreamHandle requestStream = httpSocket.openStream();
    StreamRef ref = requestStream.process(req, mockListener);
    CancelReason reason = new RstStreamFrame();
    XFuture<Void> cancelDone = ref.cancel(reason);
    Assert.assertTrue(cancelDone.isDone());
    // Assert the socket is NOT closed
    Assert.assertTrue(mockChannel.isClosed());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) InetSocketAddress(java.net.InetSocketAddress) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Aggregations

RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 StreamRef (com.webpieces.http2.api.streaming.StreamRef)11 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)10 Test (org.junit.Test)9 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)7 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)6 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)4 XFuture (org.webpieces.util.futures.XFuture)4 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 DataListener (org.webpieces.nio.api.handlers.DataListener)3 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)2 DataWrapper (org.webpieces.data.api.DataWrapper)2 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)2 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)2 MockStreamWriter (org.webpieces.httpclient.api.mocks.MockStreamWriter)2 Http2Trailers (com.webpieces.http2.api.dto.highlevel.Http2Trailers)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1