use of com.webpieces.hpack.api.dto.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());
CompletableFuture<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());
}
use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.
the class ProxyResponse method sendChunkedResponse.
private CompletableFuture<Void> sendChunkedResponse(Http2Response resp, byte[] bytes, final Compression compression) {
boolean compressed = false;
Compression usingCompression;
if (compression == null) {
usingCompression = new NoCompression();
} else {
usingCompression = compression;
compressed = true;
resp.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, usingCompression.getCompressionType()));
}
log.info("sending RENDERHTML response. size=" + bytes.length + " code=" + resp + " for domain=" + routerRequest.domain + " path" + routerRequest.relativePath + " responseSender=" + stream);
boolean isCompressed = compressed;
// Send the headers and get the responseid.
return stream.sendResponse(resp).thenCompose(writer -> {
List<DataFrame> frames = possiblyCompress(bytes, usingCompression, isCompressed);
CompletableFuture<StreamWriter> future = CompletableFuture.completedFuture(writer);
for (int i = 0; i < frames.size(); i++) {
DataFrame f = frames.get(i);
if (i == frames.size() - 1)
f.setEndOfStream(true);
future = future.thenCompose(v -> {
return writer.processPiece(f);
});
}
return future;
}).thenApply(w -> null);
}
use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.
the class ProxyResponse method sendRedirect.
@Override
public CompletableFuture<Void> sendRedirect(RedirectResponse httpResponse) {
log.debug(() -> "Sending redirect response. req=" + request);
Http2Response response = createRedirect(httpResponse);
log.info("sending REDIRECT response responseSender=" + stream);
return stream.sendResponse(response).thenApply(w -> {
channelCloser.closeIfNeeded(request, stream);
return null;
});
}
use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.
the class ProxyResponse method createRedirect.
private Http2Response createRedirect(RedirectResponse httpResponse) {
Http2Response response = new Http2Response();
if (httpResponse.isAjaxRedirect) {
response.addHeader(new Http2Header(Http2HeaderName.STATUS, BootstrapModalTag.AJAX_REDIRECT_CODE + ""));
response.addHeader(new Http2Header("reason", "Ajax Redirect"));
} else {
response.addHeader(new Http2Header(Http2HeaderName.STATUS, StatusCode.HTTP_303_SEEOTHER.getCodeString()));
response.addHeader(new Http2Header("reason", StatusCode.HTTP_303_SEEOTHER.getReason()));
}
String url = httpResponse.redirectToPath;
if (url.startsWith("http")) {
//do nothing
} else if (httpResponse.domain != null && httpResponse.isHttps != null) {
String prefix = "http://";
if (httpResponse.isHttps)
prefix = "https://";
String portPostfix = "";
if (httpResponse.port != 443 && httpResponse.port != 80)
portPostfix = ":" + httpResponse.port;
url = prefix + httpResponse.domain + portPostfix + httpResponse.redirectToPath;
} else if (httpResponse.domain != null) {
throw new IllegalReturnValueException("Controller is returning a domain without returning isHttps=true or" + " isHttps=false so we can form the entire redirect. Either drop the domain or set isHttps");
} else if (httpResponse.isHttps != null) {
throw new IllegalReturnValueException("Controller is returning isHttps=" + httpResponse.isHttps + " but there is" + "no domain set so we can't form the full redirect. Either drop setting isHttps or set the domain");
}
Http2Header location = new Http2Header(Http2HeaderName.LOCATION, url);
response.addHeader(location);
responseCreator.addCommonHeaders(request, response, false, true);
//Firefox requires a content length of 0 on redirect(chrome doesn't)!!!...
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, 0 + ""));
return response;
}
use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.
the class ResponseCreator method createContentResponseImpl.
private ResponseEncodingTuple createContentResponseImpl(Http2Request request, int statusCode, String reason, boolean isDynamicPartOfWebsite, MimeTypeResult mimeType) {
Http2Response response = new Http2Response();
response.setEndOfStream(false);
response.addHeader(new Http2Header(Http2HeaderName.STATUS, statusCode + ""));
response.addHeader(new Http2Header("reason", reason));
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_TYPE, mimeType.mime));
boolean isInternalError = false;
if (statusCode == 500)
isInternalError = true;
addCommonHeaders(request, response, isInternalError, isDynamicPartOfWebsite);
return new ResponseEncodingTuple(response, mimeType);
}
Aggregations