use of io.netty.handler.codec.http.HttpVersion in project zuul by Netflix.
the class ClientResponseWriter method buildHttpResponse.
private HttpResponse buildHttpResponse(final HttpResponseMessage zuulResp) {
final HttpRequestInfo zuulRequest = zuulResp.getInboundRequest();
HttpVersion responseHttpVersion;
final String inboundProtocol = zuulRequest.getProtocol();
if (inboundProtocol.startsWith("HTTP/1")) {
responseHttpVersion = HttpVersion.valueOf(inboundProtocol);
} else {
// Default to 1.1. We do this to cope with HTTP/2 inbound requests.
responseHttpVersion = HttpVersion.HTTP_1_1;
}
// Create the main http response to send, with body.
final DefaultHttpResponse nativeResponse = new DefaultHttpResponse(responseHttpVersion, HttpResponseStatus.valueOf(zuulResp.getStatus()), false, false);
// Now set all of the response headers - note this is a multi-set in keeping with HTTP semantics
final HttpHeaders nativeHeaders = nativeResponse.headers();
for (Header entry : zuulResp.getHeaders().entries()) {
nativeHeaders.add(entry.getKey(), entry.getValue());
}
// Netty does not automatically add Content-Length or Transfer-Encoding: chunked. So we add here if missing.
if (!HttpUtil.isContentLengthSet(nativeResponse) && !HttpUtil.isTransferEncodingChunked(nativeResponse)) {
nativeResponse.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
}
final HttpRequest nativeReq = (HttpRequest) zuulResp.getContext().get(CommonContextKeys.NETTY_HTTP_REQUEST);
if (!closeConnection && HttpUtil.isKeepAlive(nativeReq)) {
HttpUtil.setKeepAlive(nativeResponse, true);
} else {
// Send a Connection: close response header (only needed for HTTP/1.0 but no harm in doing for 1.1 too).
nativeResponse.headers().set("Connection", "close");
}
// TODO - temp hack for http/2 handling.
if (nativeReq.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
String streamId = nativeReq.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
nativeResponse.headers().set(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
}
return nativeResponse;
}
use of io.netty.handler.codec.http.HttpVersion in project crate by crate.
the class SqlHttpHandler method sendResponse.
private void sendResponse(Session session, ChannelHandlerContext ctx, FullHttpRequest request, Map<String, List<String>> parameters, XContentBuilder result, @Nullable Throwable t) {
final HttpVersion httpVersion = request.protocolVersion();
final DefaultFullHttpResponse resp;
final ByteBuf content;
if (t == null) {
content = Netty4Utils.toByteBuf(BytesReference.bytes(result));
resp = new DefaultFullHttpResponse(httpVersion, HttpResponseStatus.OK, content);
resp.headers().add(HttpHeaderNames.CONTENT_TYPE, result.contentType().mediaType());
} else {
var throwable = SQLExceptions.prepareForClientTransmission(getAccessControl.apply(session.sessionContext()), t);
HttpError httpError = HttpError.fromThrowable(throwable);
String mediaType;
boolean includeErrorTrace = paramContainFlag(parameters, "error_trace");
try (XContentBuilder contentBuilder = httpError.toXContent(includeErrorTrace)) {
content = Netty4Utils.toByteBuf(BytesReference.bytes(contentBuilder));
mediaType = contentBuilder.contentType().mediaType();
} catch (IOException e) {
throw new RuntimeException(e);
}
resp = new DefaultFullHttpResponse(httpVersion, httpError.httpResponseStatus(), content);
resp.headers().add(HttpHeaderNames.CONTENT_TYPE, mediaType);
}
Netty4CorsHandler.setCorsResponseHeaders(request, resp, corsConfig);
resp.headers().add(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(content.readableBytes()));
boolean closeConnection = isCloseConnection(request);
ChannelPromise promise = ctx.newPromise();
if (closeConnection) {
promise.addListener(ChannelFutureListener.CLOSE);
} else {
Headers.setKeepAlive(httpVersion, resp);
}
ctx.writeAndFlush(resp, promise);
}
use of io.netty.handler.codec.http.HttpVersion in project riposte by Nike-Inc.
the class RequestInfoImpl method getMultipartParts.
/**
* {@inheritDoc}
*/
@Override
@Nullable
public synchronized List<InterfaceHttpData> getMultipartParts() {
if (!isMultipartRequest() || !isCompleteRequestWithAllChunks())
return null;
if (multipartData == null) {
byte[] contentBytes = getRawContentBytes();
HttpVersion httpVersion = getProtocolVersion();
HttpMethod httpMethod = getMethod();
// default them to something if null somehow slips through.
if (httpVersion == null) {
httpVersion = HttpVersion.HTTP_1_0;
}
if (httpMethod == null) {
httpMethod = HttpMethod.POST;
}
HttpRequest fullHttpRequestForMultipartDecoder = (contentBytes == null) ? new DefaultFullHttpRequest(httpVersion, httpMethod, getUri()) : new DefaultFullHttpRequest(httpVersion, httpMethod, getUri(), Unpooled.wrappedBuffer(contentBytes));
fullHttpRequestForMultipartDecoder.headers().add(getHeaders());
multipartData = new HttpPostMultipartRequestDecoder(new DefaultHttpDataFactory(false), fullHttpRequestForMultipartDecoder, getContentCharset());
}
return multipartData.getBodyHttpDatas();
}
use of io.netty.handler.codec.http.HttpVersion in project wso2-synapse by wso2.
the class HttpResponseProcessor method writeResponse.
private boolean writeResponse(HttpRequestContext requestContext, OutgoingMessage outgoing, ChannelHandlerContext ctx) {
boolean keepAlive = requestContext.isKeepAlive();
HttpVersion httpVersion = consumerContext.getHttpVersion();
HttpResponseStatus httpResponseStatus = outgoing.getStatusCode();
FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, httpResponseStatus, Unpooled.copiedBuffer(outgoing.getBody(), CharsetUtil.UTF_8));
populateHttpHeaders(response, outgoing);
populateCookies(response, outgoing);
if (!keepAlive) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
ctx.write(response);
}
return keepAlive;
}
use of io.netty.handler.codec.http.HttpVersion in project wso2-synapse by wso2.
the class HttpResponseProcessor method write404NotFoundResponse.
private boolean write404NotFoundResponse(HttpRequestContext requestContext, ChannelHandlerContext ctx) {
boolean keepAlive = requestContext.isKeepAlive();
HttpVersion httpVersion = consumerContext.getHttpVersion();
FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, NOT_FOUND);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
ctx.write(response);
}
return keepAlive;
}
Aggregations