Search in sources :

Example 6 with Http2Response

use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.

the class TestCBasicRequestResponse method testWithNoData.

@Test
public void testWithNoData() throws InterruptedException, ExecutionException, TimeoutException {
    FullRequest request1 = new FullRequest();
    request1.setHeaders(Requests.createRequest());
    CompletableFuture<FullResponse> future = httpSocket.send(request1);
    Assert.assertFalse(future.isDone());
    Http2Headers frame = (Http2Headers) mockChannel.getFrameAndClear();
    Assert.assertEquals(1, frame.getStreamId());
    Http2Response resp = Requests.createResponse(request1.getHeaders().getStreamId());
    resp.setEndOfStream(true);
    mockChannel.write(resp);
    FullResponse response = future.get(2, TimeUnit.SECONDS);
    Assert.assertEquals(0, response.getPayload().getReadableSize());
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Headers(com.webpieces.hpack.api.dto.Http2Headers) FullRequest(org.webpieces.http2client.api.dto.FullRequest) Test(org.junit.Test)

Example 7 with Http2Response

use of com.webpieces.hpack.api.dto.Http2Response 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 8 with Http2Response

use of com.webpieces.hpack.api.dto.Http2Response in project webpieces by deanhiller.

the class ProxyResponse method sendRedirect.

@Override
public CompletableFuture<Void> sendRedirect(RedirectResponse httpResponse) {
    log.debug(() -> "Sending redirect response. req=" + request);
    Http2Response response = createRedirect(httpResponse);
    log.info("sending REDIRECT response responseSender=" + stream);
    return stream.sendResponse(response).thenApply(w -> {
        channelCloser.closeIfNeeded(request, stream);
        return null;
    });
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response)

Example 9 with Http2Response

use of com.webpieces.hpack.api.dto.Http2Response 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 10 with Http2Response

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

Aggregations

Http2Response (com.webpieces.hpack.api.dto.Http2Response)36 Test (org.junit.Test)17 StreamWriter (com.webpieces.http2engine.api.StreamWriter)13 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)13 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)12 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)12 Http2Request (com.webpieces.hpack.api.dto.Http2Request)9 Http2Msg (com.webpieces.http2parser.api.dto.lib.Http2Msg)8 Header (org.webpieces.httpparser.api.common.Header)8 ArrayList (java.util.ArrayList)7 DataWrapper (org.webpieces.data.api.DataWrapper)7 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)6 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)6 HttpData (org.webpieces.httpparser.api.dto.HttpData)5 Http2Headers (com.webpieces.hpack.api.dto.Http2Headers)3 Http2Push (com.webpieces.hpack.api.dto.Http2Push)3 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ResponseStream (org.webpieces.frontend2.api.ResponseStream)3