Search in sources :

Example 11 with Http2Request

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

the class TestS5_1StreamStates method testSection5_1BadFrameReceivedInReservedRemoteState.

/**
	 * reserved local
	 * 
	 * A PRIORITY or WINDOW_UPDATE frame MAY be received in this state. Receiving any 
	 * type of frame other than RST_STREAM, PRIORITY, or WINDOW_UPDATE on a stream 
	 * in this state MUST be treated as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
	 */
@Test
public void testSection5_1BadFrameReceivedInReservedRemoteState() {
    Http2Request request = Http2Requests.createRequest(1, true);
    mockChannel.send(request);
    PassedIn in = mockListener.getSingleRequest();
    ResponseStream stream = in.stream;
    Http2Push push = Http2Requests.createPush(request.getStreamId());
    stream.openPushStream().process(push);
    Http2Msg pushMsg = mockChannel.getFrameAndClear();
    Assert.assertEquals(push, pushMsg);
    //send bad frame in this state
    DataFrame data = Http2Requests.createData1(push.getPromisedStreamId(), false);
    mockChannel.send(data);
    Cancel info = mockListener.getCancelInfo();
    Assert.assertEquals(stream, info.stream);
    //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.assertEquals("ConnectionException: HttpSocket[Http2ChannelCache1]:stream2:(BAD_FRAME_RECEIVED_FOR_THIS_STATE) " + "No transition defined on statemachine for event=RECV_DATA when in state=Reserved(local)", msg);
    Assert.assertTrue(mockChannel.isClosed());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.hpack.api.dto.Http2Request) Cancel(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.Cancel) Http2Push(com.webpieces.hpack.api.dto.Http2Push) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Http2Msg(com.webpieces.http2parser.api.dto.lib.Http2Msg) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) ResponseStream(org.webpieces.frontend2.api.ResponseStream) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

Example 12 with Http2Request

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

the class TestS5_1StreamStates method testSection5_1_1BadEvenStreamId.

/**
	 * 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.
	 */
@Test
public void testSection5_1_1BadEvenStreamId() {
    Http2Request request = Http2Requests.createRequest(2, true);
    mockChannel.send(request);
    //no request comes in
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //no cancels
    Assert.assertEquals(0, 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.  Even stream ids not allowed in requests to a server request="));
    Assert.assertTrue(mockChannel.isClosed());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.hpack.api.dto.Http2Request) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

Example 13 with Http2Request

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

the class TestHttp2Errors method testFarEndClosedSocket.

@Test
public void testFarEndClosedSocket() 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 in1 = mockListener.getSingleRequest();
    mockChannel.send(request2);
    PassedIn in2 = mockListener.getSingleRequest();
    mockChannel.close();
    List<Cancel> cancels = mockListener.getCancels();
    Assert.assertEquals(in1.stream, cancels.get(0).stream);
    Assert.assertEquals(in2.stream, cancels.get(1).stream);
}
Also used : Http2Request(com.webpieces.hpack.api.dto.Http2Request) Cancel(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.Cancel) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 14 with Http2Request

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

the class Http2Translations method requestToHeaders.

private static Http2Request requestToHeaders(HttpRequest request, boolean fromSslChannel) {
    HttpRequestLine requestLine = request.getRequestLine();
    List<Header> requestHeaders = request.getHeaders();
    LinkedList<Http2Header> headerList = new LinkedList<>();
    // add special headers
    headerList.add(new Http2Header(":method", requestLine.getMethod().getMethodAsString()));
    UrlInfo urlInfo = requestLine.getUri().getUriBreakdown();
    headerList.add(new Http2Header(":path", urlInfo.getFullPath()));
    // Figure out scheme
    if (urlInfo.getPrefix() != null) {
        headerList.add(new Http2Header(":scheme", urlInfo.getPrefix()));
    } else if (fromSslChannel) {
        headerList.add(new Http2Header(":scheme", "https"));
    } else {
        headerList.add(new Http2Header(":scheme", "http"));
    }
    // Figure out authority
    Header hostHeader = request.getHeaderLookupStruct().getHeader(KnownHeaderName.HOST);
    if (hostHeader == null)
        throw new IllegalArgumentException("Host header is required in http1.1");
    // Add regular headers
    for (Header header : requestHeaders) {
        if (header.getKnownName() == KnownHeaderName.HOST) {
            //keeps headers in order of http1 headers
            String h = hostHeader.getValue();
            headerList.add(new Http2Header(":authority", h));
            continue;
        }
        headerList.add(new Http2Header(header.getName().toLowerCase(), header.getValue()));
    }
    Http2Request headers = new Http2Request(headerList);
    Header contentLen = request.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (request.isHasChunkedTransferHeader()) {
        headers.setEndOfStream(false);
    } else if (contentLenGreaterThanZero(contentLen)) {
        headers.setEndOfStream(false);
    } else
        headers.setEndOfStream(true);
    return headers;
}
Also used : UrlInfo(org.webpieces.httpparser.api.dto.UrlInfo) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) Http2Request(com.webpieces.hpack.api.dto.Http2Request) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) LinkedList(java.util.LinkedList)

Example 15 with Http2Request

use of com.webpieces.hpack.api.dto.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());
    CompletableFuture<StreamWriter> future = requestAndStream2.stream.sendResponse(resp2);
    Assert.assertTrue(future.isDone());
    Http2Response frame2 = (Http2Response) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, frame2);
    Http2Response resp1 = Http2Requests.createResponse(request1.getStreamId());
    CompletableFuture<StreamWriter> future1 = requestAndStream1.stream.sendResponse(resp1);
    Assert.assertTrue(future1.isDone());
    Http2Response frame1 = (Http2Response) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, frame1);
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2engine.api.StreamWriter) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Aggregations

Http2Request (com.webpieces.hpack.api.dto.Http2Request)31 Test (org.junit.Test)22 StreamWriter (com.webpieces.http2engine.api.StreamWriter)13 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)13 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)12 DataWrapper (org.webpieces.data.api.DataWrapper)11 Http2Response (com.webpieces.hpack.api.dto.Http2Response)10 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)10 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)9 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)9 Http2Msg (com.webpieces.http2parser.api.dto.lib.Http2Msg)7 Http2Push (com.webpieces.hpack.api.dto.Http2Push)5 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)5 ShutdownStream (com.webpieces.http2engine.api.error.ShutdownStream)5 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)4 MockStreamWriter (org.webpieces.httpfrontend2.api.mock2.MockStreamWriter)4 PushPromiseListener (com.webpieces.http2engine.api.PushPromiseListener)3 CancelReason (com.webpieces.http2parser.api.dto.CancelReason)3 InetSocketAddress (java.net.InetSocketAddress)3 ArrayList (java.util.ArrayList)3