Search in sources :

Example 1 with Http2Header

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

the class TestC4FrameSizeAndHeaders method createInterleavedFrames.

private List<Http2Frame> createInterleavedFrames() {
    Http2Response response1 = new Http2Response();
    response1.setStreamId(1);
    response1.setEndOfStream(true);
    fillHeaders(response1);
    HeaderEncoding encoding = new HeaderEncoding();
    List<Http2Frame> frames1 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response1);
    Http2Response response2 = new Http2Response();
    response2.setStreamId(3);
    response1.setEndOfStream(true);
    response2.addHeader(new Http2Header(Http2HeaderName.ACCEPT, "value"));
    List<Http2Frame> frames2 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response2);
    List<Http2Frame> frames = new ArrayList<>();
    frames.addAll(frames1);
    frames.add(1, frames2.get(0));
    return frames;
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) HeaderEncoding(com.webpieces.hpack.impl.HeaderEncoding) Encoder(com.twitter.hpack.Encoder) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) ArrayList(java.util.ArrayList) Http2Frame(com.webpieces.http2parser.api.dto.lib.Http2Frame)

Example 2 with Http2Header

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

the class TestC4FrameSizeAndHeaders method fillHeaders.

private void fillHeaders(Http2Response response1) {
    String value = "heaheaheaheaheaheahahoz.zhxheh,h,he,he,heaheaeaheaheahoahoahozzoqorqzro.zo.zrszaroatroathoathoathoathoatoh";
    for (int i = 0; i < 10; i++) {
        value = value + value;
        response1.addHeader(new Http2Header("eaheahaheaheaeha" + i, value));
    }
}
Also used : Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header)

Example 3 with Http2Header

use of com.webpieces.http2parser.api.dto.lib.Http2Header 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);
}
Also used : TemplateUtil(org.webpieces.templating.api.TemplateUtil) BufferPool(org.webpieces.data.api.BufferPool) BootstrapModalTag(org.webpieces.templating.impl.tags.BootstrapModalTag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseEncodingTuple(org.webpieces.webserver.impl.ResponseCreator.ResponseEncodingTuple) Inject(javax.inject.Inject) View(org.webpieces.router.api.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest) Charset(java.nio.charset.Charset) Http2Request(com.webpieces.hpack.api.dto.Http2Request) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) CompressionLookup(org.webpieces.router.impl.compression.CompressionLookup) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) MissingPropertyException(groovy.lang.MissingPropertyException) RenderContentResponse(org.webpieces.router.api.dto.RenderContentResponse) OutputStream(java.io.OutputStream) Logger(org.webpieces.util.logging.Logger) RenderStaticResponse(org.webpieces.router.api.dto.RenderStaticResponse) StatusCode(com.webpieces.http2parser.api.dto.StatusCode) StringWriter(java.io.StringWriter) Set(java.util.Set) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) IOException(java.io.IOException) ResponseStreamer(org.webpieces.router.api.ResponseStreamer) Http2HeaderName(com.webpieces.http2parser.api.dto.lib.Http2HeaderName) List(java.util.List) WebServerConfig(org.webpieces.webserver.api.WebServerConfig) StreamWriter(com.webpieces.http2engine.api.StreamWriter) TemplateService(org.webpieces.templating.api.TemplateService) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) LoggerFactory(org.webpieces.util.logging.LoggerFactory) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) Http2Response(com.webpieces.hpack.api.dto.Http2Response) ResponseStream(org.webpieces.frontend2.api.ResponseStream) RenderResponse(org.webpieces.router.api.dto.RenderResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Compression(org.webpieces.router.impl.compression.Compression) Compression(org.webpieces.router.impl.compression.Compression) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) List(java.util.List) DataFrame(com.webpieces.http2parser.api.dto.DataFrame)

Example 4 with Http2Header

use of com.webpieces.http2parser.api.dto.lib.Http2Header 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;
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header)

Example 5 with Http2Header

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

the class RequestStreamWriter method handleCompleteRequest.

CompletableFuture<Void> handleCompleteRequest() {
    for (Http2Header h : requestHeaders.getHeaders()) {
        if (!headersSupported.contains(h.getKnownName()))
            log.error("This webserver has not thought about supporting header=" + h.getName() + " quite yet.  value=" + h.getValue() + " Please let us know and we can quickly add support");
    }
    RouterRequest routerRequest = new RouterRequest();
    routerRequest.orginalRequest = requestHeaders;
    //TODO(dhiller): figure out the firewall way to config when firewall terminates the ssl and we receive http
    //or the secure routes will not show up
    //We could add configuration to checking the terminating server socket locally as the firewall could
    //be defined to terminate ssl and drive to a specific port then.  the info is in stream.getSocket.getSvrSocketAddress
    routerRequest.isHttps = stream.getSocket().isHttps();
    String domain = requestHeaders.getAuthority();
    if (domain == null) {
        throw new IllegalArgumentException("Must contain Host(http1.1) or :authority(http2) header");
    }
    int port = 80;
    if (routerRequest.isHttps)
        port = 443;
    //if there is a firewall this port is wrong....and the above or below is right
    //int port = socketInfo.getLocalBoundAddress().getPort();
    int index2 = domain.indexOf(":");
    //TODO(dhiller): find when user is used and test implement
    if (index2 >= 0) {
        port = Integer.parseInt(domain.substring(index2 + 1));
        domain = domain.substring(0, index2);
    }
    String methodString = requestHeaders.getMethodString();
    HttpMethod method = HttpMethod.lookup(methodString);
    if (method == null)
        throw new UnsupportedOperationException("method not supported=" + methodString);
    parseCookies(requestHeaders, routerRequest);
    parseAcceptLang(requestHeaders, routerRequest);
    parseAccept(requestHeaders, routerRequest);
    routerRequest.encodings = headerParser.parseAcceptEncoding(requestHeaders);
    String referHeader = requestHeaders.getSingleHeaderValue(Http2HeaderName.REFERER);
    if (referHeader != null)
        routerRequest.referrer = referHeader;
    String xRequestedWithHeader = requestHeaders.getSingleHeaderValue(Http2HeaderName.X_REQUESTED_WITH);
    if ("XMLHttpRequest".equals(xRequestedWithHeader))
        routerRequest.isAjaxRequest = true;
    String fullPath = requestHeaders.getPath();
    if (fullPath == null)
        throw new IllegalArgumentException(":path header(http2) or path in request line(http1.1) is required");
    parseBody(requestHeaders, routerRequest);
    routerRequest.method = method;
    routerRequest.domain = domain;
    routerRequest.port = port;
    int index = fullPath.indexOf("?");
    if (index > 0) {
        routerRequest.relativePath = fullPath.substring(0, index);
        String postfix = fullPath.substring(index + 1);
        facade.urlEncodeParse(postfix, routerRequest);
    } else {
        routerRequest.queryParams = new HashMap<>();
        routerRequest.relativePath = fullPath;
    }
    //http1.1 so no...
    routerRequest.isSendAheadNextResponses = false;
    if (routerRequest.relativePath.contains("?"))
        throw new UnsupportedOperationException("not supported yet");
    ProxyResponse streamer = facade.createProxyResponse();
    try {
        streamer.init(routerRequest, requestHeaders, stream, facade.getBufferPool());
        return facade.incomingCompleteRequest(routerRequest, streamer);
    } catch (BadCookieException e) {
        log.warn("This occurs if secret key changed, or you booted another webapp with different key on same port or someone modified the cookie", e);
        streamer.sendRedirectAndClearCookie(routerRequest, e.getCookieName());
        return CompletableFuture.completedFuture(null);
    }
}
Also used : BadCookieException(org.webpieces.router.api.exceptions.BadCookieException) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpMethod(org.webpieces.ctx.api.HttpMethod) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Aggregations

Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)36 ArrayList (java.util.ArrayList)16 Http2Response (com.webpieces.hpack.api.dto.Http2Response)13 Http2Request (com.webpieces.hpack.api.dto.Http2Request)6 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)6 Http2Push (com.webpieces.hpack.api.dto.Http2Push)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4 Http2Frame (com.webpieces.http2parser.api.dto.lib.Http2Frame)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Header (org.webpieces.httpparser.api.common.Header)4 Logger (org.webpieces.util.logging.Logger)4 LoggerFactory (org.webpieces.util.logging.LoggerFactory)4 List (java.util.List)3 BufferPool (org.webpieces.data.api.BufferPool)3 Encoder (com.twitter.hpack.Encoder)2 Http2Headers (com.webpieces.hpack.api.dto.Http2Headers)2 Http2Trailers (com.webpieces.hpack.api.dto.Http2Trailers)2 HeaderEncoding (com.webpieces.hpack.impl.HeaderEncoding)2 PushPromiseListener (com.webpieces.http2engine.api.PushPromiseListener)2 PushStreamHandle (com.webpieces.http2engine.api.PushStreamHandle)2