Search in sources :

Example 46 with Http2Request

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

the class TestSplitHeaders method testBytesParsedBySplittingHeaders.

@Test
public void testBytesParsedBySplittingHeaders() {
    Http2Request req = new Http2Request();
    req.setStreamId(1);
    req.addHeader(new Http2Header(Http2HeaderName.SCHEME, "/"));
    req.addHeader(new Http2Header(Http2HeaderName.METHOD, "/"));
    req.addHeader(new Http2Header(Http2HeaderName.PATH, "/"));
    req.addHeader(new Http2Header(Http2HeaderName.AUTHORITY, "/"));
    for (int i = 0; i < 20; i++) {
        req.addHeader(new Http2Header("someHeader" + i, "value"));
    }
    DataWrapper data = parser.marshal(req);
    byte[] first = data.readBytesAt(0, 60);
    DataWrapper data1 = dataGen.wrapByteArray(first);
    UnmarshalState state = parser.unmarshal(data1);
    Assert.assertEquals(0, state.getParsedFrames().size());
    Assert.assertEquals(0, state.getNumBytesJustParsed());
    Assert.assertEquals(first.length, state.getLeftOverDataSize());
    byte[] second = data.readBytesAt(60, data.getReadableSize() - 60);
    DataWrapper data2 = dataGen.wrapByteArray(second);
    state = parser.unmarshal(data2);
    Assert.assertEquals(1, state.getParsedFrames().size());
    Assert.assertEquals(data.getReadableSize(), state.getNumBytesJustParsed());
    Assert.assertEquals(0, state.getLeftOverDataSize());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) UnmarshalState(com.webpieces.hpack.api.UnmarshalState) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Test(org.junit.Test)

Example 47 with Http2Request

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

the class Requests method createRequest.

public static Http2Request createRequest(String uri, DataWrapper body) {
    Http2Request req = new Http2Request();
    req.addHeader(new Http2Header(Http2HeaderName.AUTHORITY, "yourdomain.com"));
    req.addHeader(new Http2Header(Http2HeaderName.SCHEME, "https"));
    req.addHeader(new Http2Header(Http2HeaderName.METHOD, "GET"));
    req.addHeader(new Http2Header(Http2HeaderName.PATH, uri));
    req.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, body.getReadableSize() + ""));
    return req;
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 48 with Http2Request

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

the class TestHttp1Backpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createChunkedResponse(1);
    HttpChunk response2 = Requests.createHttpChunk(250);
    HttpLastChunk lastChunk = new HttpLastChunk();
    List<ByteBuffer> buffers = create4BuffersWith3Messags(response1, response2, lastChunk);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since it never made it to the client.
    Assert.assertTrue(fut1.isDone());
    // this keeps bytes in-memory that is less than bufPool.getSuggestedSize. ie. < 5k
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    MockStreamWriter mockWriter = new MockStreamWriter();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases the response BUT 'some' data was with response so can't ack yet
    requestFuture.complete(mockWriter);
    // not resolved yet since requestFuture AND streamWriterFutuer BOTH need to be resolved
    Assert.assertFalse(fut2.isDone());
    // This is BECAUSE 68 bytes resulted in TWO messages and both futures can ack the 68 bytes.
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 49 with Http2Request

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

the class Http2ToHttp11 method translateRequest.

public static HttpRequest translateRequest(Http2Request headers) {
    HttpRequestLine requestLine = new HttpRequestLine();
    HttpRequest req = new HttpRequest();
    req.setRequestLine(requestLine);
    for (Http2Header header : headers.getHeaders()) {
        insertInfo(req, header);
    }
    Http2HeaderStruct headerMap = headers.getHeaderLookupStruct();
    Http2Header method = headerMap.getHeader(Http2HeaderName.METHOD);
    if (method == null)
        throw new IllegalArgumentException(Http2HeaderName.METHOD.name() + "is a required header to translate to http1");
    req.getRequestLine().setMethod(new HttpRequestMethod(method.getValue()));
    Http2Header host = headerMap.getHeader(Http2HeaderName.AUTHORITY);
    if (host == null)
        throw new IllegalArgumentException(Http2HeaderName.AUTHORITY.name() + "is a required header to translate to http1");
    Http2Header path = headerMap.getHeader(Http2HeaderName.PATH);
    if (path == null)
        throw new IllegalArgumentException(Http2HeaderName.PATH.name() + "is a required header to translate to http1");
    HttpUri httpUri = new HttpUri(path.getValue());
    req.getRequestLine().setUri(httpUri);
    return req;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestMethod(org.webpieces.httpparser.api.dto.HttpRequestMethod) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) Http2HeaderStruct(com.webpieces.http2.api.dto.highlevel.Http2HeaderStruct)

Example 50 with Http2Request

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

the class AbstractFrontendHttp2Test method sendRequestToServer.

protected PassedIn sendRequestToServer(int streamId, boolean eos) {
    Http2Request request1 = Http2Requests.createRequest(streamId, eos);
    mockChannel.send(request1);
    PassedIn req = mockListener.getSingleRequest();
    Assert.assertEquals(request1, req.request);
    return req;
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)

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