use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class XFileReader method runFileRead.
public XFuture<Void> runFileRead(RequestInfo info, RenderStaticResponse renderStatic, ProxyStreamHandle handle) throws IOException {
VirtualFile fullFilePath = renderStatic.getFilePath();
if (!fullFilePath.exists()) {
throw new NotFoundException("File not found=" + fullFilePath);
} else if (fullFilePath.isDirectory()) {
throw new NotFoundException("File not found (it was a directory that can't be rendered)=" + fullFilePath);
}
String fileName = getNameToUse(fullFilePath);
String extension = null;
int lastDot = fileName.lastIndexOf(".");
if (lastDot > 0) {
extension = fileName.substring(lastDot + 1);
}
ResponseCreator.ResponseEncodingTuple tuple = responseCreator.createResponse(info.getRequest(), StatusCode.HTTP_200_OK, extension, "application/octet-stream", false);
Http2Response response = tuple.response;
// On startup, we protect developers from breaking clients. In http, all files that change
// must also change the hash automatically and the %%{ }%% tag generates those hashes so the
// files loaded are always the latest
Long timeSeconds = config.getStaticFileCacheTimeSeconds();
if (timeSeconds != null)
response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeSeconds));
ChunkReader reader = createFileReader(response, renderStatic, fileName, fullFilePath, info, extension, tuple, handle);
if (log.isDebugEnabled())
log.debug("sending chunked file via async read=" + reader);
ProxyStreamHandle stream = info.getResponseSender();
return futureUtil.finallyBlock(() -> stream.process(response).thenCompose(s -> readLoop(s, info.getPool(), reader, 0)), () -> handleClose(info, reader));
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class CompressionChunkingHandle method checkForCompression.
private Compression checkForCompression(Http2Response response) {
if (routerRequest == null) {
// as that is what the client accepts for compression
return new NoCompression();
}
if (compressionOff) {
// a file that is already compressed. In this case, don't compress on top of their cached compression
return new NoCompression();
}
Http2Header header = response.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_TYPE);
String contentType = response.getSingleHeaderValue(Http2HeaderName.CONTENT_TYPE);
if (// could be a redirect or something with 0 bytes anyways or we don't know what it is so don't compress
contentType == null)
return new NoCompression();
MimeTypes.MimeTypeResult mimeType = mimeTypes.createMimeType(header.getValue());
Compression compression = compressionLookup.createCompressionStream(routerRequest.encodings, mimeType);
if (compression == null) {
return new NoCompression();
} else {
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compression.getCompressionType()));
return compression;
}
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class ProxyStreamHandle method sendRedirect.
public XFuture<Void> sendRedirect(RedirectResponse httpResponse) {
Http2Request request = originalHttp2Request;
if (log.isDebugEnabled())
log.debug("Sending redirect response. req=" + request);
Http2Response response = responseCreator.createRedirect(request, httpResponse);
log.info("sending REDIRECT response responseSender=" + this);
return process(response).thenApply(s -> null);
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class EScopedRouter method send403Response.
private void send403Response(ProxyStreamHandle handler, String reason) {
Http2Response response = new Http2Response();
response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
response.addHeader(new Http2Header("Webpieces-Reason", reason));
XFuture<StreamWriter> process = handler.process(response);
try {
process.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
throw SneakyThrow.sneak(e);
}
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class ResponseCreator method createRedirect.
public Http2Response createRedirect(Http2Request request, RedirectResponse httpResponse) {
Http2Response response;
if (httpResponse.isAjaxRedirect) {
response = addCommonHeaders(request, null, true, Constants.AJAX_REDIRECT_CODE, "Ajax Redirect");
} else {
response = addCommonHeaders(request, null, true, StatusCode.HTTP_303_SEE_OTHER.getCode(), StatusCode.HTTP_303_SEE_OTHER.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);
// Firefox requires a content length of 0 on redirect(chrome doesn't)!!!...
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, 0 + ""));
return response;
}
Aggregations