Search in sources :

Example 41 with Http2Request

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

the class TestC4FrameSizeAndHeaders 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.
 */
@Test
public void testSection4_2FrameTooLarge() {
    MockStreamWriter mockStreamWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(mockStreamWriter));
    Http2Request request = sendRequestToServer(listener1);
    sendResponseFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    byte[] buf = new byte[localSettings.getMaxFrameSize() + 4];
    dataFrame.setData(DATA_GEN.wrapByteArray(buf));
    // endOfStream=false
    mockChannel.write(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());
    ShutdownStream failResp = (ShutdownStream) listener1.getSingleRstStream();
    Assert.assertEquals(CancelReasonCode.EXCEEDED_MAX_FRAME_SIZE, failResp.getCause().getReasonCode());
    // send new request on closed connection
    Http2Request request1 = Requests.createRequest();
    StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef.getWriter();
    ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
    Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Also used : MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test)

Example 42 with Http2Request

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

the class TestC4FrameSizeAndHeaders method testSection4_3InterleavedFrames.

/**
 * Each header block is processed as a discrete unit. Header blocks
 * MUST be transmitted as a contiguous sequence of frames, with no interleaved
 * frames of any other type or from any other stream. The last frame in a
 * sequence of HEADERS or CONTINUATION frames has the END_HEADERS flag set. The
 * last frame in a sequence of PUSH_PROMISE or CONTINUATION frames has the
 * END_HEADERS flag set. This allows a header block to be logically equivalent to a single frame.
 *
 * Header block fragments can only be sent as the payload of HEADERS, PUSH_PROMISE, or
 * CONTINUATION frames because these frames carry data that can modify the
 * compression context maintained by a receiver. An endpoint receiving
 * HEADERS, PUSH_PROMISE, or CONTINUATION frames needs to reassemble header
 * blocks and perform decompression even if the frames are to be discarded. A receiver
 * MUST terminate the connection with a connection error (Section 5.4.1) of
 * type COMPRESSION_ERROR if it does not decompress a header block.
 */
@Test
public void testSection4_3InterleavedFrames() {
    MockStreamWriter mockStreamWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(mockStreamWriter));
    Http2Request request = sendRequestToServer(listener1);
    // has to be 1 since we use 1 in the response
    Assert.assertEquals(1, request.getStreamId());
    List<Http2Frame> frames = createInterleavedFrames();
    // for this test, need interleaved
    Assert.assertTrue(frames.size() >= 3);
    mockChannel.writeFrame(frames.get(0));
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
    mockChannel.writeFrame(frames.get(1));
    ShutdownStream reset = (ShutdownStream) listener1.getSingleRstStream();
    Assert.assertEquals(CancelReasonCode.HEADERS_MIXED_WITH_FRAMES, reset.getCause().getReasonCode());
    // 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("Headers/continuations from two different streams per spec cannot be interleaved. "));
    Assert.assertTrue(mockChannel.isClosed());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Example 43 with Http2Request

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

the class IntegColoradoEdu method main.

public static void main(String[] args) throws InterruptedException {
    boolean isHttp = true;
    String host = "www.colorado.edu";
    int port = 443;
    if (isHttp)
        port = 80;
    Http2Request req = createRequest(host);
    log.info("starting socket");
    ChunkedResponseListener listener = new ChunkedResponseListener();
    InetSocketAddress addr = new InetSocketAddress(host, port);
    Http2Socket socket = IntegSingleRequest.createHttpClient("oneTimerHttp2Socket", isHttp, addr);
    socket.connect(addr).thenAccept(socet -> socket.openStream().process(req, listener)).exceptionally(e -> reportException(socket, e));
    Thread.sleep(100000);
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) InetSocketAddress(java.net.InetSocketAddress) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) PushPromiseListener(com.webpieces.http2.api.streaming.PushPromiseListener) XFuture(org.webpieces.util.futures.XFuture) 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) Http2Socket(org.webpieces.http2client.api.Http2Socket) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) InetSocketAddress(java.net.InetSocketAddress) Http2Socket(org.webpieces.http2client.api.Http2Socket)

Example 44 with Http2Request

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

the class IntegSingleRequest method start.

public void start() throws InterruptedException {
    log.info("starting test to download / page from google");
    boolean isHttp = true;
    String host = "www.google.com";
    // String host = "localhost"; //jetty
    // String host = "api.push.apple.com";
    // String host = "gcm-http.googleapis.com";
    // String host = "nghttp2.org";
    int port = 443;
    if (isHttp)
        port = 80;
    if ("localhost".equals(host)) {
        port = 8443;
        if (isHttp)
            port = 8080;
    }
    List<Http2Header> req = createRequest(host, isHttp);
    Http2Request request = new Http2Request(req);
    request.setEndOfStream(true);
    InetSocketAddress addr = new InetSocketAddress(host, port);
    Http2Socket socket = createHttpClient("testRunSocket", isHttp, addr);
    socket.connect(addr).thenAccept(v -> socket.openStream().process(request, new ChunkedResponseListener())).exceptionally(e -> reportException(socket, e));
    Thread.sleep(10000000);
}
Also used : BufferPool(org.webpieces.data.api.BufferPool) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) LoggerFactory(org.slf4j.LoggerFactory) BackpressureConfig(org.webpieces.nio.api.BackpressureConfig) Http2ClientFactory(org.webpieces.http2client.api.Http2ClientFactory) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) ChannelManager(org.webpieces.nio.api.ChannelManager) Metrics(io.micrometer.core.instrument.Metrics) ArrayList(java.util.ArrayList) SSLEngine(javax.net.ssl.SSLEngine) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) PushPromiseListener(com.webpieces.http2.api.streaming.PushPromiseListener) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) HpackParser(com.webpieces.hpack.api.HpackParser) TwoPools(org.webpieces.data.api.TwoPools) Http2Socket(org.webpieces.http2client.api.Http2Socket) HpackParserFactory(com.webpieces.hpack.api.HpackParserFactory) InjectionConfig(com.webpieces.http2engine.api.client.InjectionConfig) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) InetSocketAddress(java.net.InetSocketAddress) Proxy2StreamRef(org.webpieces.http2client.impl.Proxy2StreamRef) Executors(java.util.concurrent.Executors) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) Http2Client(org.webpieces.http2client.api.Http2Client) List(java.util.List) SocketListener(org.webpieces.http2client.SocketListener) 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) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) InetSocketAddress(java.net.InetSocketAddress) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Socket(org.webpieces.http2client.api.Http2Socket)

Example 45 with Http2Request

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

the class AbstractTest method sendResponseFromServer.

protected void sendResponseFromServer(MockResponseListener listener1, Http2Request request) {
    Http2Response resp1 = Requests.createResponse(request.getStreamId());
    // endOfStream=false
    mockChannel.write(resp1);
    Http2Response response1 = listener1.getSingleReturnValueIncomingResponse();
    Assert.assertEquals(resp1, response1);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response)

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