use of com.webpieces.http2parser.api.dto.lib.Http2Header in project webpieces by deanhiller.
the class ResponseCreator method addCommonHeaders.
public void addCommonHeaders(Http2Request request, Http2Response response, boolean isInternalError, boolean isDynamicPartOfWebsite) {
String connHeader = request.getSingleHeaderValue(Http2HeaderName.CONNECTION);
DateTime now = DateTime.now().toDateTime(DateTimeZone.UTC);
String dateStr = formatter.print(now) + " GMT";
//in general, nearly all these headers are desired..
Http2Header date = new Http2Header(Http2HeaderName.DATE, dateStr);
response.addHeader(date);
if (isDynamicPartOfWebsite) {
List<RouterCookie> cookies = createCookies(isInternalError);
for (RouterCookie c : cookies) {
Http2Header cookieHeader = create(c);
response.addHeader(cookieHeader);
}
}
if (connHeader == null)
return;
else if (!"keep-alive".equals(connHeader))
return;
//just re-use the connHeader from the request...
response.addHeader(request.getHeaderLookupStruct().getHeader(Http2HeaderName.CONNECTION));
}
use of com.webpieces.http2parser.api.dto.lib.Http2Header 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);
}
use of com.webpieces.http2parser.api.dto.lib.Http2Header in project webpieces by deanhiller.
the class StaticFileReader method runAsyncFileRead.
private CompletableFuture<Void> runAsyncFileRead(RequestInfo info, RenderStaticResponse renderStatic) throws IOException {
boolean isFile = true;
String fullFilePath = renderStatic.getFilePath();
if (fullFilePath == null) {
isFile = false;
fullFilePath = renderStatic.getDirectory() + renderStatic.getRelativePath();
}
String extension = null;
int lastDirIndex = fullFilePath.lastIndexOf("/");
int lastDot = fullFilePath.lastIndexOf(".");
if (lastDot > lastDirIndex) {
extension = fullFilePath.substring(lastDot + 1);
}
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 timeMs = config.getStaticFileCacheTimeSeconds();
if (timeMs != null)
response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeMs));
Path file;
Compression compr = compressionLookup.createCompressionStream(info.getRouterRequest().encodings, extension, tuple.mimeType);
//during startup as I don't feel like paying a cpu penalty for compressing while live
if (compr != null && compr.getCompressionType().equals(routerConfig.getStartupCompression())) {
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compr.getCompressionType()));
File routesCache = renderStatic.getTargetCache();
File fileReference;
if (isFile) {
String fileName = fullFilePath.substring(lastDirIndex + 1);
fileReference = new File(routesCache, fileName);
} else {
fileReference = new File(routesCache, renderStatic.getRelativePath());
}
fullFilePath = fileReference.getAbsolutePath();
file = fetchFile("Compressed File from cache=", fullFilePath + ".gz");
} else {
file = fetchFile("File=", fullFilePath);
}
AsynchronousFileChannel asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
CompletableFuture<StreamWriter> future;
try {
log.info(() -> "sending chunked file via async read=" + file);
long length = file.toFile().length();
AtomicLong remaining = new AtomicLong(length);
future = info.getResponseSender().sendResponse(response).thenCompose(s -> readLoop(s, info.getPool(), file, asyncFile, 0, remaining));
} catch (Throwable e) {
future = new CompletableFuture<StreamWriter>();
future.completeExceptionally(e);
}
return //our finally block for failures
future.handle((s, exc) -> handleClose(info, s, exc)).thenAccept(s -> empty());
}
use of com.webpieces.http2parser.api.dto.lib.Http2Header in project webpieces by deanhiller.
the class Http2Translations method translateResponse.
public static HttpResponse translateResponse(Http2Response headers) {
HttpResponseStatus status = new HttpResponseStatus();
HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
statusLine.setStatus(status);
HttpResponse response = new HttpResponse();
response.setStatusLine(statusLine);
for (Http2Header header : headers.getHeaders()) {
if (header.getKnownName() == Http2HeaderName.STATUS) {
fillStatus(header, status);
} else if ("reason".equals(header.getName())) {
fillReason(header, status);
} else if (header.getKnownName() == Http2HeaderName.SCHEME) {
//do nothing and drop it
} else {
Header http1Header = convertHeader(header);
response.addHeader(http1Header);
}
}
if (headers.isEndOfStream() && headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
//firefox really needs content length
response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
}
if (status.getCode() == null)
throw new IllegalArgumentException("The header :status is required to send the response");
return response;
}
use of com.webpieces.http2parser.api.dto.lib.Http2Header in project webpieces by deanhiller.
the class Http2Requests method createRequest.
public static Http2Request createRequest(int streamId, boolean eos) {
List<Http2Header> headers = new ArrayList<>();
headers.add(new Http2Header(Http2HeaderName.METHOD, "GET"));
headers.add(new Http2Header(Http2HeaderName.AUTHORITY, "somehost.com"));
headers.add(new Http2Header(Http2HeaderName.PATH, "/"));
headers.add(new Http2Header(Http2HeaderName.SCHEME, "http"));
headers.add(new Http2Header(Http2HeaderName.HOST, "somehost.com"));
headers.add(new Http2Header(Http2HeaderName.ACCEPT, "*/*"));
headers.add(new Http2Header(Http2HeaderName.ACCEPT_ENCODING, "gzip, deflate"));
headers.add(new Http2Header(Http2HeaderName.USER_AGENT, "webpieces/1.15.0"));
Http2Request request = new Http2Request(headers);
request.setStreamId(streamId);
request.setEndOfStream(eos);
return request;
}
Aggregations