Search in sources :

Example 16 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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 17 with Http2Request

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

the class DefaultCorsProcessor method isAccessAllowed.

@Override
public AccessResult isAccessAllowed(RequestContext ctx) {
    Http2Request request = ctx.getRequest().originalRequest;
    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())) {
        return new AccessResult("Domain not allowed");
    }
    // method is allowed since we are here OR else CORSProcessor is not called
    List<Http2Header> headers = request.getHeaders();
    for (Http2Header header : headers) {
        if (DEFAULTS.contains(header.getName())) {
            continue;
        } else if (!isAllowCredsCookies && header.getKnownName() == Http2HeaderName.COOKIE) {
            return new AccessResult("Credentials / Cookies not supported on this CORS request");
        } else if (!allowedHeaders.contains("*") && !allowedHeaders.contains(header.getName().toLowerCase())) {
            return new AccessResult("Header '" + header.getName() + "' not supported on this CORS request");
        }
    }
    ctx.addModifyResponse(new OverwriteForCorsResponse(originHeader));
    return new AccessResult();
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 18 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) Executors(java.util.concurrent.Executors) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) Http2Client(org.webpieces.http2client.api.Http2Client) List(java.util.List) 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) 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 19 with Http2Request

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

the class TestCancelStream method testClientCancelWithKeepAlive.

// @Test
// public void testRequestResponseXFutureCancelNoKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
// 
// @Test
// public void testRequestResponseXFutureCancelWithKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
@Test
public void testClientCancelWithKeepAlive() {
    XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
    MockResponseListener mockListener = new MockResponseListener();
    Http2Request req = Requests.createRequest(false);
    req.addHeader(new Http2Header(Http2HeaderName.CONNECTION, "keep-alive"));
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    RequestStreamHandle requestStream = httpSocket.openStream();
    StreamRef ref = requestStream.process(req, mockListener);
    CancelReason reason = new RstStreamFrame();
    XFuture<Void> cancelDone = ref.cancel(reason);
    Assert.assertTrue(cancelDone.isDone());
    // Assert the socket is NOT closed
    Assert.assertFalse(mockChannel.isClosed());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) InetSocketAddress(java.net.InetSocketAddress) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Example 20 with Http2Request

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

the class TestCancelStream method testServerCloseSocket.

@Test
public void testServerCloseSocket() {
    XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
    MockResponseListener mockListener = new MockResponseListener();
    Http2Request req = Requests.createRequest(false);
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    RequestStreamHandle requestStream = httpSocket.openStream();
    StreamRef ref = requestStream.process(req, mockListener);
    Assert.assertFalse(mockListener.isCancelled());
    mockChannel.simulateClose();
    Assert.assertTrue(mockListener.isCancelled());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) InetSocketAddress(java.net.InetSocketAddress) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

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