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);
}
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));
}
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);
}
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());
}
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;
}
Aggregations