Search in sources :

Example 46 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class Http2Requests method createTrailers.

public static Http2Trailers createTrailers(int streamId) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.SERVER, "trailing"));
    Http2Trailers trailers = new Http2Trailers(headers);
    trailers.setEndOfStream(true);
    trailers.setStreamId(streamId);
    return trailers;
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers)

Example 47 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class ProxyHttpStream method incomingRequest.

@Override
public StreamRef incomingRequest(Http2Request request, ResponseStream stream) {
    String expect = request.getSingleHeaderValue("Expect");
    XFuture<StreamWriter> future = XFuture.completedFuture(null);
    if (expect != null && "100-continue".equals(expect.toLowerCase())) {
        Http2Response continueResponse = new Http2Response();
        continueResponse.setEndOfStream(false);
        continueResponse.addHeader(new Http2Header(Http2HeaderName.STATUS, "100"));
        future = stream.process(continueResponse);
    }
    // This is only for streaming to backpressure clients IF we responded OR cancelled so we don't
    // waste CPU on a client stream coming in
    XFuture<ProxyWriter> futureWriter = new XFuture<>();
    ProxyResponseStream proxy = new ProxyResponseStream(request, stream, futureWriter);
    StreamRef streamRef = openStream.incomingRequest(request, proxy);
    XFuture<StreamWriter> writer = future.thenCompose(w -> {
        return createProxy(streamRef.getWriter(), futureWriter);
    });
    return new ProxyStreamRef(writer, streamRef);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 48 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header 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 49 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header 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 50 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header 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)

Aggregations

Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)72 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)26 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)23 ArrayList (java.util.ArrayList)23 DataWrapper (org.webpieces.data.api.DataWrapper)9 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 XFuture (org.webpieces.util.futures.XFuture)7 InetSocketAddress (java.net.InetSocketAddress)6 Header (org.webpieces.httpparser.api.common.Header)6 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)5 Http2Frame (com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)5 StreamRef (com.webpieces.http2.api.streaming.StreamRef)5 List (java.util.List)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ResponseStreamHandle (com.webpieces.http2.api.streaming.ResponseStreamHandle)4 Executor (java.util.concurrent.Executor)4 Executors (java.util.concurrent.Executors)4