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