Search in sources :

Example 51 with Http2Response

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));
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) BufferPool(org.webpieces.data.api.BufferPool) StatusCode(org.webpieces.http.StatusCode) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RouterConfig(org.webpieces.router.api.RouterConfig) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FutureHelper(org.webpieces.util.futures.FutureHelper) NotFoundException(org.webpieces.http.exception.NotFoundException) VirtualFile(org.webpieces.util.file.VirtualFile) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) ResponseCreator(org.webpieces.router.impl.proxyout.ResponseCreator) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) RenderStaticResponse(org.webpieces.router.impl.dto.RenderStaticResponse) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) NotFoundException(org.webpieces.http.exception.NotFoundException) ResponseCreator(org.webpieces.router.impl.proxyout.ResponseCreator)

Example 52 with Http2Response

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;
    }
}
Also used : Compression(org.webpieces.router.impl.compression.Compression) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) MimeTypes(org.webpieces.router.impl.compression.MimeTypes)

Example 53 with Http2Response

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);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request)

Example 54 with Http2Response

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);
    }
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException)

Example 55 with Http2Response

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;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Aggregations

Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)66 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)32 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)30 Test (org.junit.Test)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)24 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)16 StreamRef (com.webpieces.http2.api.streaming.StreamRef)15 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 DataWrapper (org.webpieces.data.api.DataWrapper)13 Header (org.webpieces.httpparser.api.common.Header)13 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 ArrayList (java.util.ArrayList)10 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)10 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)9 HttpData (org.webpieces.httpparser.api.dto.HttpData)8 XFuture (org.webpieces.util.futures.XFuture)8 RouterService (org.webpieces.router.api.RouterService)5 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)4 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)4