Search in sources :

Example 16 with Http2Response

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

the class TestSimpleRoutes method testBasicRoute.

@Test
public void testBasicRoute() {
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response resp = mockStream.getLastResponse();
    Assert.assertEquals("http://" + req.getAuthority() + "/something", resp.getSingleHeaderValue(Http2HeaderName.LOCATION));
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockStreamHandle(org.webpieces.router.api.error.MockStreamHandle) Test(org.junit.Test)

Example 17 with Http2Response

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

the class DefaultCorsProcessor method processOptionsCors.

@Override
public void processOptionsCors(Http2Request request, List<HttpMethod> methods, ResponseStreamHandle responseStream) {
    Http2Header originHeader = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ORIGIN);
    if (originHeader == null)
        throw new IllegalStateException("Should only use this for CORS which requires an Origin header");
    else if (!allowedDomains.contains("*") && !allowedDomains.contains(originHeader.getValue())) {
        send403Response(responseStream, request);
        return;
    }
    Http2Response response = new Http2Response();
    Http2Header methodHeader = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ACCESS_CONTROL_REQUEST_METHOD);
    HttpMethod lookup = HttpMethod.lookup(methodHeader.getValue());
    Http2Header headersRequested = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ACCESS_CONTROL_REQUEST_HEADERS);
    if (!methods.contains(lookup)) {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
    } else if (hasInvalidHeader(allowedHeaders, headersRequested)) {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
    } else {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "204"));
    }
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_ORIGIN, originHeader.getValue()));
    if (allowedDomains.contains("*")) {
        // since all domains, we must tell caches that Origin header in response will vary
        // since it responds with the domain that requested it
        response.addHeader(new Http2Header(Http2HeaderName.VARY, "Origin"));
    }
    if (isAllowCredsCookies) {
        response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"));
    }
    String allowedMethodsStr = methods.stream().map(e -> e.getCode()).collect(Collectors.joining(", "));
    String allowedHeadersStr = String.join(", ", allowedHeaders);
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_METHODS, allowedMethodsStr));
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_HEADERS, allowedHeadersStr));
    if (exposeTheseResponseHeadersToBrowserStr != null) {
        response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_EXPOSE_HEADERS, exposeTheseResponseHeadersToBrowserStr));
    }
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_MAX_AGE, maxAgeSeconds + ""));
    response.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, "0"));
    sendResponse(responseStream, response);
}
Also used : SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) Set(java.util.Set) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Collectors(java.util.stream.Collectors) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) HeaderType(com.webpieces.http2.api.dto.lowlevel.lib.HeaderType) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) OverwritePlatformResponse(org.webpieces.ctx.api.OverwritePlatformResponse) RequestContext(org.webpieces.ctx.api.RequestContext) Locale(java.util.Locale) 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) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpMethod(org.webpieces.ctx.api.HttpMethod)

Example 18 with Http2Response

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

the class MockStreamHandle method getLastResponse.

public Http2Response getLastResponse() {
    if (tooManyResponses)
        throw new IllegalStateException("There was too many responses.  this mock only can take one response at a time");
    Http2Response temp = lastResponse;
    lastResponse = null;
    return temp;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response)

Example 19 with Http2Response

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

the class Level8NotifyClntListeners method sendResponseToApp.

public XFuture<Void> sendResponseToApp(Stream stream, Http2Response response) {
    if (stream instanceof ClientStream) {
        ClientStream str = (ClientStream) stream;
        ResponseStreamHandle listener = str.getResponseListener();
        return listener.process(response).thenApply(w -> {
            str.setResponseWriter(w);
            return null;
        });
    }
    ClientPushStream str = (ClientPushStream) stream;
    PushPromiseListener pushListener = str.getPushPromiseListener();
    return pushListener.processPushResponse(response).thenApply(w -> {
        str.setPushResponseWriter(w);
        return null;
    });
}
Also used : PushPromiseListener(com.webpieces.http2.api.streaming.PushPromiseListener) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle)

Example 20 with Http2Response

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

the class TestCBasicRequestResponse method testWithNoData.

@Test
public void testWithNoData() throws InterruptedException, ExecutionException, TimeoutException {
    FullRequest request1 = new FullRequest();
    request1.setHeaders(Requests.createRequest());
    XFuture<FullResponse> future = httpSocket.send(request1);
    Assert.assertFalse(future.isDone());
    Http2Headers frame = (Http2Headers) mockChannel.getFrameAndClear();
    Assert.assertEquals(1, frame.getStreamId());
    Http2Response resp = Requests.createResponse(request1.getHeaders().getStreamId());
    resp.setEndOfStream(true);
    mockChannel.write(resp);
    FullResponse response = future.get(2, TimeUnit.SECONDS);
    Assert.assertEquals(0, response.getPayload().getReadableSize());
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) FullRequest(org.webpieces.http2client.api.dto.FullRequest) Test(org.junit.Test)

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