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);
}
}
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);
}
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);
}
}
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;
});
}
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;
});
}
Aggregations