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