Search in sources :

Example 31 with Http2Request

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

the class ResponseCreator method addCommonHeaders2.

public Http2Response addCommonHeaders2(Http2Request request, String responseMimeType, int statusCode, String statusReason) {
    Http2Response response = new Http2Response();
    response.addHeader(new Http2Header(Http2HeaderName.STATUS, statusCode + ""));
    if (statusReason != null)
        response.addHeader(new Http2Header("reason", statusReason));
    String connHeader = request.getSingleHeaderValue(Http2HeaderName.CONNECTION);
    String dateStr = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC));
    Http2Header versionHeader = new Http2Header(Http2HeaderName.SERVER, version);
    response.addHeader(versionHeader);
    // in general, nearly all these headers are desired..
    Http2Header date = new Http2Header(Http2HeaderName.DATE, dateStr);
    response.addHeader(date);
    if (responseMimeType != null)
        response.addHeader(new Http2Header(Http2HeaderName.CONTENT_TYPE, responseMimeType));
    if (connHeader == null)
        return response;
    else if (!"keep-alive".equals(connHeader))
        return response;
    // just re-use the connHeader from the request...
    response.addHeader(request.getHeaderLookupStruct().getHeader(Http2HeaderName.CONNECTION));
    return response;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 32 with Http2Request

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

the class TestHttp2Errors method testFarEndClosedSocket.

@Test
public void testFarEndClosedSocket() throws InterruptedException, ExecutionException {
    MockStreamWriter mockSw = new MockStreamWriter();
    MockStreamRef ref1 = new MockStreamRef(mockSw);
    mockListener.addMockStreamToReturn(ref1);
    MockStreamWriter mockSw2 = new MockStreamWriter();
    MockStreamRef ref2 = new MockStreamRef(mockSw2);
    mockListener.addMockStreamToReturn(ref2);
    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();
    Assert.assertTrue(ref1.isCancelled());
    Assert.assertTrue(ref2.isCancelled());
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 33 with Http2Request

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

the class TestS5x1StreamStates 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() {
    MockStreamWriter mockWriter = new MockStreamWriter();
    XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
    MockStreamRef mockStream = new MockStreamRef(futA);
    mockListener.addMockStreamToReturn(mockStream);
    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);
    Assert.assertTrue(mockStream.isCancelled());
    // 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 : MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) 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) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 34 with Http2Request

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

the class TestS5x1StreamStates 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());
    Assert.assertTrue(mockListener.isClosed());
    // 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.http2.api.dto.highlevel.Http2Request) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Example 35 with Http2Request

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

the class TestSBasicRequestResponse method testWithDataAndTrailingHeaders.

@Test
public void testWithDataAndTrailingHeaders() throws InterruptedException, ExecutionException, TimeoutException {
    MockStreamWriter mockSw = new MockStreamWriter();
    mockSw.setDefaultRetValToThis();
    mockListener.addMockStreamToReturn(mockSw);
    Http2Request request1 = Http2Requests.createRequest(1, false);
    DataFrame data1 = Http2Requests.createData1(request1.getStreamId(), false);
    Http2Trailers trailing = Http2Requests.createTrailers(request1.getStreamId());
    mockChannel.send(request1);
    PassedIn incoming1 = mockListener.getSingleRequest();
    Assert.assertEquals(request1, incoming1.request);
    mockChannel.send(data1);
    DataFrame incoming2 = (DataFrame) mockSw.getSingleFrame();
    Assert.assertEquals(3, incoming2.getData().getReadableSize());
    // clear window update frames
    Assert.assertEquals(2, mockChannel.getFramesAndClear().size());
    mockChannel.send(trailing);
    Http2Headers incoming = (Http2Headers) mockSw.getSingleFrame();
    Assert.assertEquals(trailing, incoming);
    Http2Response resp = Http2Requests.createResponse(request1.getStreamId(), false);
    XFuture<StreamWriter> future = incoming1.stream.process(resp);
    Http2Msg response = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, response);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    DataFrame data2 = Http2Requests.createData2(request1.getStreamId(), false);
    writer.processPiece(data2);
    DataFrame dataResp = (DataFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(1, dataResp.getData().getReadableSize());
    Http2Trailers trailingResp = Http2Requests.createTrailers(request1.getStreamId());
    writer.processPiece(trailingResp);
    Http2Headers trailers = (Http2Headers) mockChannel.getFrameAndClear();
    Assert.assertEquals(trailingResp, trailers);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

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