Search in sources :

Example 1 with DataFrame

use of com.webpieces.http2parser.api.dto.DataFrame in project webpieces by deanhiller.

the class TestC5_1StreamStates method testSection5_1ReceiveBadFrameAfterReceiveRstStreamFrame.

/**
	 * An endpoint MUST NOT send frames other than PRIORITY on a closed stream. An endpoint 
	 * that receives any frame other than PRIORITY after receiving a ----RST_STREAM---- MUST 
	 * treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an 
	 * endpoint that receives any frames after receiving a frame with the 
	 * END_STREAM flag set MUST treat that as a connection error (Section 5.4.1) of 
	 * type STREAM_CLOSED, unless the frame is permitted as described below.
	 * 
	 */
@Test
public void testSection5_1ReceiveBadFrameAfterReceiveRstStreamFrame() {
    MockStreamWriter mockWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(CompletableFuture.<StreamWriter>completedFuture(mockWriter));
    Http2Request request = sendRequestToServer(listener1);
    sendResetFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    mockChannel.write(dataFrame);
    //remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, goAway.getKnownErrorCode());
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream1:(CLOSED_STREAM) " + "Stream must have been closed as it no longer exists.  high mark=1  " + "your frame=DataFrame{streamId=1, endStream=false, data.len=0, padding=0}", msg);
    Assert.assertTrue(mockChannel.isClosed());
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
    //send new request on closed connection
    Http2Request request1 = Requests.createRequest();
    CompletableFuture<StreamWriter> future = httpSocket.openStream().process(request1, listener1);
    ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
    Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2engine.api.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ConnectionClosedException(com.webpieces.http2engine.api.ConnectionClosedException) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

Example 2 with DataFrame

use of com.webpieces.http2parser.api.dto.DataFrame in project webpieces by deanhiller.

the class ProxyResponse method sendChunkedResponse.

private CompletableFuture<Void> sendChunkedResponse(Http2Response resp, byte[] bytes, final Compression compression) {
    boolean compressed = false;
    Compression usingCompression;
    if (compression == null) {
        usingCompression = new NoCompression();
    } else {
        usingCompression = compression;
        compressed = true;
        resp.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, usingCompression.getCompressionType()));
    }
    log.info("sending RENDERHTML response. size=" + bytes.length + " code=" + resp + " for domain=" + routerRequest.domain + " path" + routerRequest.relativePath + " responseSender=" + stream);
    boolean isCompressed = compressed;
    // Send the headers and get the responseid.
    return stream.sendResponse(resp).thenCompose(writer -> {
        List<DataFrame> frames = possiblyCompress(bytes, usingCompression, isCompressed);
        CompletableFuture<StreamWriter> future = CompletableFuture.completedFuture(writer);
        for (int i = 0; i < frames.size(); i++) {
            DataFrame f = frames.get(i);
            if (i == frames.size() - 1)
                f.setEndOfStream(true);
            future = future.thenCompose(v -> {
                return writer.processPiece(f);
            });
        }
        return future;
    }).thenApply(w -> null);
}
Also used : TemplateUtil(org.webpieces.templating.api.TemplateUtil) BufferPool(org.webpieces.data.api.BufferPool) BootstrapModalTag(org.webpieces.templating.impl.tags.BootstrapModalTag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseEncodingTuple(org.webpieces.webserver.impl.ResponseCreator.ResponseEncodingTuple) Inject(javax.inject.Inject) View(org.webpieces.router.api.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest) Charset(java.nio.charset.Charset) Http2Request(com.webpieces.hpack.api.dto.Http2Request) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) CompressionLookup(org.webpieces.router.impl.compression.CompressionLookup) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) MissingPropertyException(groovy.lang.MissingPropertyException) RenderContentResponse(org.webpieces.router.api.dto.RenderContentResponse) OutputStream(java.io.OutputStream) Logger(org.webpieces.util.logging.Logger) RenderStaticResponse(org.webpieces.router.api.dto.RenderStaticResponse) StatusCode(com.webpieces.http2parser.api.dto.StatusCode) StringWriter(java.io.StringWriter) Set(java.util.Set) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) IOException(java.io.IOException) ResponseStreamer(org.webpieces.router.api.ResponseStreamer) Http2HeaderName(com.webpieces.http2parser.api.dto.lib.Http2HeaderName) List(java.util.List) WebServerConfig(org.webpieces.webserver.api.WebServerConfig) StreamWriter(com.webpieces.http2engine.api.StreamWriter) TemplateService(org.webpieces.templating.api.TemplateService) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) LoggerFactory(org.webpieces.util.logging.LoggerFactory) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) Http2Response(com.webpieces.hpack.api.dto.Http2Response) ResponseStream(org.webpieces.frontend2.api.ResponseStream) RenderResponse(org.webpieces.router.api.dto.RenderResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Compression(org.webpieces.router.impl.compression.Compression) Compression(org.webpieces.router.impl.compression.Compression) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) List(java.util.List) DataFrame(com.webpieces.http2parser.api.dto.DataFrame)

Example 3 with DataFrame

use of com.webpieces.http2parser.api.dto.DataFrame in project webpieces by deanhiller.

the class StaticFileReader method sendHttpChunk.

private CompletableFuture<StreamWriter> sendHttpChunk(StreamWriter writer, BufferPool pool, ByteBuffer buf, Path file, boolean isEos) {
    DataWrapper data = wrapperFactory.wrapByteBuffer(buf);
    DataFrame frame = new DataFrame();
    frame.setEndOfStream(isEos);
    frame.setData(data);
    return writer.processPiece(frame).thenApply(w -> {
        buf.position(buf.limit());
        pool.releaseBuffer(buf);
        return w;
    });
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) DataFrame(com.webpieces.http2parser.api.dto.DataFrame)

Example 4 with DataFrame

use of com.webpieces.http2parser.api.dto.DataFrame 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 5 with DataFrame

use of com.webpieces.http2parser.api.dto.DataFrame in project webpieces by deanhiller.

the class ProxyResponse method possiblyCompress.

private List<DataFrame> possiblyCompress(byte[] bytes, Compression usingCompression, boolean isCompressed) {
    ChunkedStream chunkedStream = new ChunkedStream(config.getMaxBodySize(), isCompressed);
    try (OutputStream chainStream = usingCompression.createCompressionStream(chunkedStream)) {
        //IF wrapped in compression above(ie. not NoCompression), sending the WHOLE byte[] in comes out in
        //pieces that get sent out as it is being compressed
        //and http chunks are sent under the covers(in ChunkedStream)
        chainStream.write(bytes);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (!chunkedStream.isClosed())
        throw new IllegalStateException("ChunkedStream should have been closed");
    List<DataFrame> frames = chunkedStream.getFrames();
    return frames;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) DataFrame(com.webpieces.http2parser.api.dto.DataFrame)

Aggregations

DataFrame (com.webpieces.http2parser.api.dto.DataFrame)16 DataWrapper (org.webpieces.data.api.DataWrapper)11 Http2Request (com.webpieces.hpack.api.dto.Http2Request)7 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)6 Test (org.junit.Test)6 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)4 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)3 Http2Push (com.webpieces.hpack.api.dto.Http2Push)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ResponseStream (org.webpieces.frontend2.api.ResponseStream)2 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 Http2Trailers (com.webpieces.hpack.api.dto.Http2Trailers)1 StreamHandle (com.webpieces.http2engine.api.StreamHandle)1 Http2ClientEngine (com.webpieces.http2engine.api.client.Http2ClientEngine)1 Http2ClientEngineFactory (com.webpieces.http2engine.api.client.Http2ClientEngineFactory)1