use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.
the class TestC5_1StreamStates method testSection5_1ReceiveValidFramesAfterSendRstStreamFrame.
/**
* If this state is reached as a result of sending a RST_STREAM frame, the
* peer that receives the RST_STREAM might have already sent — or enqueued for
* sending — frames on the stream that cannot be withdrawn. An endpoint MUST ignore
* frames that it receives on closed streams after it has sent a RST_STREAM frame. An
* endpoint MAY choose to limit the period over which it ignores frames and
* treat frames that arrive after this time as being in error.
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void testSection5_1ReceiveValidFramesAfterSendRstStreamFrame() throws InterruptedException, ExecutionException, TimeoutException {
MockResponseListener listener1 = new MockResponseListener();
listener1.setIncomingRespDefault(CompletableFuture.<StreamWriter>completedFuture(null));
Http2Request request1 = Requests.createRequest();
StreamHandle stream = httpSocket.openStream();
CompletableFuture<StreamWriter> future = stream.process(request1, listener1);
@SuppressWarnings("unused") StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Http2Msg req = mockChannel.getFrameAndClear();
Assert.assertEquals(request1, req);
RstStreamFrame rst = new RstStreamFrame(request1.getStreamId(), Http2ErrorCode.CANCEL);
CompletableFuture<Void> cancel = stream.cancel(rst);
cancel.get(2, TimeUnit.SECONDS);
Http2Msg svrRst = mockChannel.getFrameAndClear();
Assert.assertEquals(rst, svrRst);
//simulate server responding before receiving the cancel
Http2Response resp1 = Requests.createEosResponse(request1.getStreamId());
//endOfStream=true
mockChannel.write(resp1);
// Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
// Assert.assertFalse(mockChannel.isClosed());
//
// Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
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