Search in sources :

Example 51 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.

the class TestS5x1StreamStates method testSection5_1_1TooLowStreamIdAfterHighStreamId.

/**
 * The identifier of a newly established stream MUST be numerically
 * greater than all streams that the initiating endpoint has opened
 * or reserved. This governs streams that are opened using a HEADERS
 * frame and streams that are reserved using PUSH_PROMISE. An endpoint
 * that receives an unexpected stream identifier MUST respond with
 * a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
 *
 * This is in conflict with another part of the spec!!!!! and so we pretend
 * the stream is closed(as in all likely hood, the stream was closed)!!!
 * and do not shutdown the whole connection for a case like this.
 *
 * The part it is in conflict with is closed state and receiving messages
 * in closed state.  The only way to resolve conflict would be to KEEP around
 * state that a connection is closed.  SORRY, the connection is closed so we
 * clean up all memory!!!
 */
@Test
public void testSection5_1_1TooLowStreamIdAfterHighStreamId() {
    MockStreamWriter mockWriter = new MockStreamWriter();
    XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
    MockStreamRef mockStream = new MockStreamRef(futA);
    mockListener.addMockStreamToReturn(mockStream);
    Http2Request request1 = Http2Requests.createRequest(5, true);
    mockChannel.send(request1);
    mockListener.getSingleRequest();
    Http2Request request = Http2Requests.createRequest(3, true);
    mockChannel.send(request);
    // WE DO NOT DO THIS which spec wants(or another test we have starts failing)
    // we leave this here in case you want to comment back in and debug that.
    // //no request comes in
    // Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    // //cancel the first stream since whole connection is going down.
    // Assert.assertEquals(1, mockListener.getNumCancelsThatCameIn());
    // 
    // //remote receives goAway
    // GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    // Assert.assertEquals(Http2ErrorCode.PROTOCOL_ERROR, goAway.getKnownErrorCode());
    // DataWrapper debugData = goAway.getDebugData();
    // String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    // Assert.assertTrue(msg.contains("Bad stream id.  Event stream ids not allowed in requests to a server frame="));
    // Assert.assertTrue(mockChannel.isClosed());
    // no request comes in
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    // we do not close the channel
    Assert.assertFalse(mockListener.isClosed());
    // our existing streams stays valid and open
    Assert.assertFalse(mockStream.isCancelled());
    // remote receives goAway
    RstStreamFrame frame = (RstStreamFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, frame.getKnownErrorCode());
    Assert.assertTrue(!mockChannel.isClosed());
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 52 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 53 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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());
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) ResponseStream(org.webpieces.frontend2.api.ResponseStream) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 54 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.

the class Layer2Http11Handler method processInitialPieceOfRequest.

private XFuture<Void> processInitialPieceOfRequest(FrontendSocketImpl socket, HttpRequest http1Req, Http2Request headers) {
    int id = counter.getAndAdd(2);
    PermitQueue permitQueue = socket.getPermitQueue();
    return permitQueue.runRequest(() -> {
        Http11StreamImpl currentStream = new Http11StreamImpl(id, socket, httpParser, permitQueue, http1Req, headers);
        HttpStream streamHandle = httpListener.openStream(socket);
        currentStream.setStreamHandle(streamHandle);
        socket.setCurrentStream(currentStream);
        if (!headers.isEndOfStream()) {
            // in this case, we are NOT at the end of the request so we must let the next piece of
            // data run right after the request
            // TODO(dhiller): Replace this section with futureUtil.trySuccessFinally
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> {
                // must release the permit so the next data piece(which may be cached) can come in
                permitQueue.releasePermit();
                return null;
            });
        } else {
            // in this case, since this is the END of the request, we cannot release the permit in the
            // permit queue as we do not want to let the next request to start until the full response is
            // sent back to the client
            currentStream.setSentFullRequest(true);
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> null);
        }
    });
}
Also used : PermitQueue(org.webpieces.util.locking.PermitQueue) StreamRef(com.webpieces.http2.api.streaming.StreamRef) HttpStream(org.webpieces.frontend2.api.HttpStream)

Example 55 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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));
}
Also used : Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) ByteBuffer(java.nio.ByteBuffer) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpStream(org.webpieces.frontend2.api.HttpStream) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) Map(java.util.Map) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Method(com.webpieces.http2.api.dto.lowlevel.Http2Method) HttpData(org.webpieces.httpparser.api.dto.HttpData) FrontendSocket(org.webpieces.frontend2.api.FrontendSocket) PermitQueue(org.webpieces.util.locking.PermitQueue) Header(org.webpieces.httpparser.api.common.Header) Logger(org.slf4j.Logger) HttpParser(org.webpieces.httpparser.api.HttpParser) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) KnownHeaderName(org.webpieces.httpparser.api.common.KnownHeaderName) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Http2ToHttp11(org.webpieces.http2translations.api.Http2ToHttp11) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) StreamRef(com.webpieces.http2.api.streaming.StreamRef) 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) ResponseStream(org.webpieces.frontend2.api.ResponseStream) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse)

Aggregations

Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)71 Test (org.junit.Test)39 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)36 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)36 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)33 StreamRef (com.webpieces.http2.api.streaming.StreamRef)32 DataWrapper (org.webpieces.data.api.DataWrapper)18 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)14 XFuture (org.webpieces.util.futures.XFuture)13 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)10 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)10 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)9 InetSocketAddress (java.net.InetSocketAddress)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 MockStreamWriter (org.webpieces.httpfrontend2.api.mock2.MockStreamWriter)7 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)6