Search in sources :

Example 31 with Http2Response

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

the class Http11ToHttp2 method responseToHeaders.

public static Http2Response responseToHeaders(HttpResponse response) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.STATUS, response.getStatusLine().getStatus().getCode().toString()));
    for (Header header : response.getHeaders()) {
        if (header.getKnownName() == KnownHeaderName.TRANSFER_ENCODING) {
            if ("chunked".equals(header.getValue())) {
                // skip as http2 does not allow this header
                continue;
            }
        }
        headers.add(new Http2Header(header.getName(), header.getValue()));
    }
    Http2Response resp = new Http2Response(headers);
    Header header = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (header != null) {
        int len = Integer.parseInt(header.getValue());
        if (len == 0) {
            resp.setEndOfStream(true);
        } else {
            resp.setEndOfStream(false);
        }
    } else if (response.isHasChunkedTransferHeader()) {
        resp.setEndOfStream(false);
    } else
        resp.setEndOfStream(true);
    return resp;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList)

Example 32 with Http2Response

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

the class Http2ToHttp11 method translateResponse.

public static HttpResponse translateResponse(Http2Response headers) {
    HttpResponseStatus status = new HttpResponseStatus();
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(statusLine);
    for (Http2Header header : headers.getHeaders()) {
        if (header.getKnownName() == Http2HeaderName.STATUS) {
            fillStatus(header, status);
        } else if ("reason".equals(header.getName())) {
            fillReason(header, status);
        } else if (header.getKnownName() == Http2HeaderName.SCHEME) {
        // do nothing and drop it
        } else {
            Header http1Header = convertHeader(header);
            response.addHeader(http1Header);
        }
    }
    if (headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
        if (headers.isEndOfStream()) {
            // firefox really needs content length!!! if end of stream
            response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
        } else {
            response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
        }
    }
    if (status.getCode() == null)
        throw new IllegalArgumentException("The header :status is required to send the response");
    // Do a queue instead with last few frames that can be enabled
    log.debug("response=" + response);
    return response;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 33 with Http2Response

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

the class TestHttp11Errors method testCloseBeforeFirstRequestCompletes.

@Test
public void testCloseBeforeFirstRequestCompletes() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    XFuture<StreamWriter> futA = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefA = new MockStreamRef(futA);
    mockListener.addMockStreamToReturn(mockStreamRefA);
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    XFuture<StreamWriter> futB = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefB = new MockStreamRef(futB);
    mockListener.addMockStreamToReturn(mockStreamRefB);
    XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
    Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
    XFuture<StreamWriter> future = in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    Assert.assertFalse(mockStreamRefA.isCancelled());
    Assert.assertFalse(mockStreamRefB.isCancelled());
    mockChannel.simulateClose();
    // this request is done, nothing to cancel
    Assert.assertTrue(mockStreamRefA.isCancelled());
    Assert.assertFalse(mockStreamRefB.isCancelled());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 34 with Http2Response

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

the class TestS4FrameSizeAndHeaders method fillHeaders.

private void fillHeaders(Http2Response response1) {
    String value = "heaheaheaheaheaheahahoz.zhxheh,h,he,he,heaheaeaheaheahoahoahozzoqorqzro.zo.zrszaroatroathoathoathoathoatoh";
    for (int i = 0; i < 10; i++) {
        value = value + value;
        response1.addHeader(new Http2Header("eaheahaheaheaeha" + i, value));
    }
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 35 with Http2Response

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

the class TestS4FrameSizeAndHeaders method createInterleavedFrames.

private List<Http2Frame> createInterleavedFrames() {
    Http2Response response1 = new Http2Response();
    response1.setStreamId(1);
    response1.setEndOfStream(true);
    fillHeaders(response1);
    HeaderEncoding encoding = new HeaderEncoding();
    List<Http2Frame> frames1 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response1);
    Http2Response response2 = new Http2Response();
    response2.setStreamId(3);
    response1.setEndOfStream(true);
    response2.addHeader(new Http2Header(Http2HeaderName.ACCEPT, "value"));
    List<Http2Frame> frames2 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response2);
    List<Http2Frame> frames = new ArrayList<>();
    frames.addAll(frames1);
    frames.add(1, frames2.get(0));
    return frames;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) HeaderEncoding(com.webpieces.hpack.impl.HeaderEncoding) Encoder(com.twitter.hpack.Encoder) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)

Aggregations

Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)66 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)32 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)30 Test (org.junit.Test)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)24 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)16 StreamRef (com.webpieces.http2.api.streaming.StreamRef)15 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 DataWrapper (org.webpieces.data.api.DataWrapper)13 Header (org.webpieces.httpparser.api.common.Header)13 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 ArrayList (java.util.ArrayList)10 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)10 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)9 HttpData (org.webpieces.httpparser.api.dto.HttpData)8 XFuture (org.webpieces.util.futures.XFuture)8 RouterService (org.webpieces.router.api.RouterService)5 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)4 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)4