Search in sources :

Example 56 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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 57 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.

the class ProxyStreamHandle method sendRenderHtml.

public XFuture<Void> sendRenderHtml(RenderResponse resp) {
    Http2Request request = originalHttp2Request;
    if (log.isInfoEnabled())
        log.info("About to send render html response for request=" + request + " controller=" + resp.view.getControllerName() + "." + resp.view.getMethodName());
    View view = resp.view;
    String packageStr = view.getPackageName();
    // For this type of View, the template is the name of the method..
    String templateClassName = view.getRelativeOrAbsolutePath();
    int lastIndexOf = templateClassName.lastIndexOf(".");
    String extension = null;
    if (lastIndexOf > 0) {
        extension = templateClassName.substring(lastIndexOf + 1);
    }
    String templatePath = templateClassName;
    if (!templatePath.startsWith("/")) {
        // relative path so need to form absolute path...
        if (lastIndexOf > 0) {
            templateClassName = templateClassName.substring(0, lastIndexOf);
        }
        templatePath = getTemplatePath(packageStr, templateClassName, extension);
    }
    // TODO: stream this out with chunked response instead??....
    StringWriter out = new StringWriter();
    try {
        templatingService.loadAndRunTemplate(templatePath, out, resp.pageArgs);
    } catch (MissingPropException e) {
        Set<String> keys = resp.pageArgs.keySet();
        throw new ControllerPageArgsException("Controller.method=" + view.getControllerName() + "." + view.getMethodName() + " did\nnot" + " return enough arguments for the template =" + templatePath + ".  specifically, the method\nreturned these" + " arguments=" + keys + "  There is a chance in your html you forgot the '' around a variable name\n" + "such as #{set 'key'}# but you put #{set key}# which is 'usually' not the correct way\n" + "The missing properties are as follows....\n" + e.getMessage(), e);
    }
    String content = out.toString();
    StatusCode statusCode;
    switch(resp.routeType) {
        case HTML:
            statusCode = StatusCode.HTTP_200_OK;
            break;
        case NOT_FOUND:
            statusCode = StatusCode.HTTP_404_NOT_FOUND;
            break;
        case INTERNAL_SERVER_ERROR:
            statusCode = StatusCode.HTTP_500_INTERNAL_SERVER_ERROR;
            break;
        default:
            throw new IllegalStateException("did add case for state=" + resp.routeType);
    }
    // The real mime type is looked up based on extension so htm or html results in text/html
    if (extension == null) {
        extension = "txt";
    }
    String finalExt = extension;
    return futureUtil.catchBlockWrap(() -> createResponseAndSend(request, statusCode, content, finalExt, "text/plain"), (t) -> convert(t));
}
Also used : Set(java.util.Set) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StringWriter(java.io.StringWriter) ControllerPageArgsException(org.webpieces.router.api.exceptions.ControllerPageArgsException) View(org.webpieces.router.impl.dto.View) StatusCode(org.webpieces.http.StatusCode) MissingPropException(org.webpieces.ctx.api.MissingPropException)

Example 58 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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 59 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.

the class ProxyStreamHandle method sendRenderContent.

public XFuture<Void> sendRenderContent(RenderContentResponse resp) {
    Http2Request request = originalHttp2Request;
    ResponseEncodingTuple tuple = responseCreator.createContentResponse(request, resp.getStatusCode(), resp.getReason(), resp.getMimeType());
    return maybeCompressAndSend(request, null, tuple, resp.getPayload());
}
Also used : ResponseEncodingTuple(org.webpieces.router.impl.proxyout.ResponseCreator.ResponseEncodingTuple) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request)

Example 60 with Http2Request

use of com.webpieces.http2.api.dto.highlevel.Http2Request 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

Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)71 Test (org.junit.Test)39 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)36 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)36 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)33 StreamRef (com.webpieces.http2.api.streaming.StreamRef)32 DataWrapper (org.webpieces.data.api.DataWrapper)18 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)14 XFuture (org.webpieces.util.futures.XFuture)13 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)10 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)10 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)9 InetSocketAddress (java.net.InetSocketAddress)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 MockStreamWriter (org.webpieces.httpfrontend2.api.mock2.MockStreamWriter)7 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)6