Search in sources :

Example 1 with ResponseStreamHandle

use of com.webpieces.http2.api.streaming.ResponseStreamHandle in project webpieces by deanhiller.

the class EchoStreamingClient method stream.

public StreamRef stream(ResponseStreamHandle handle) {
    ProxyStreamHandle h = (ProxyStreamHandle) handle;
    Http2Request req = Current.request().originalRequest;
    Http2Response response = h.createBaseResponse(req, "application/ndjson", 200, "OK");
    try {
        StreamWriter writer = h.process(response).get();
        return new ProxyStreamRef(new EchoWriter(writer));
    } catch (InterruptedException | ExecutionException e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ResponseStreamHandle

use of com.webpieces.http2.api.streaming.ResponseStreamHandle 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 3 with ResponseStreamHandle

use of com.webpieces.http2.api.streaming.ResponseStreamHandle in project webpieces by deanhiller.

the class ProxyRequestStreamHandle method process.

@Override
public StreamRef process(Http2Request request, ResponseStreamHandle responseListener) {
    ProxyResponseStream proxyResponse = new ProxyResponseStream(responseListener, frontendSocket);
    Map<String, Object> context = Context.copyContext();
    // clear context so server uses a clean context
    Context.restoreContext(new HashMap<>());
    try {
        StreamRef streamRef = stream.incomingRequest(request, proxyResponse);
        return new MockProxyStreamRef(streamRef);
    } finally {
        // client still in this context
        Context.restoreContext(context);
    }
}
Also used : StreamRef(com.webpieces.http2.api.streaming.StreamRef)

Example 4 with ResponseStreamHandle

use of com.webpieces.http2.api.streaming.ResponseStreamHandle 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 5 with ResponseStreamHandle

use of com.webpieces.http2.api.streaming.ResponseStreamHandle in project webpieces by deanhiller.

the class Level8NotifyClntListeners method sendPushToApp.

public XFuture<Void> sendPushToApp(ClientPushStream stream, Http2Push fullPromise) {
    ResponseStreamHandle listener = stream.getOriginalResponseListener();
    PushStreamHandle pushHandle = listener.openPushStream();
    stream.setPushStreamHandle(pushHandle);
    return pushHandle.process(fullPromise).thenApply(l -> {
        stream.setPushPromiseListener(l);
        return null;
    });
}
Also used : PushStreamHandle(com.webpieces.http2.api.streaming.PushStreamHandle) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle)

Aggregations

ResponseStreamHandle (com.webpieces.http2.api.streaming.ResponseStreamHandle)5 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)5 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)4 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)2 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)2 PushStreamHandle (com.webpieces.http2.api.streaming.PushStreamHandle)2 StreamRef (com.webpieces.http2.api.streaming.StreamRef)2 RequestContext (org.webpieces.ctx.api.RequestContext)2 XFuture (org.webpieces.util.futures.XFuture)2 HeaderType (com.webpieces.http2.api.dto.lowlevel.lib.HeaderType)1 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)1 PushPromiseListener (com.webpieces.http2.api.streaming.PushPromiseListener)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1