Search in sources :

Example 6 with HttpVersion

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;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Header(com.netflix.zuul.message.Header) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 7 with HttpVersion

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 8 with HttpVersion

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();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with HttpVersion

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;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 10 with HttpVersion

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;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Aggregations

HttpVersion (io.netty.handler.codec.http.HttpVersion)22 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 ByteBuf (io.netty.buffer.ByteBuf)8 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)8 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)7 EOFException (java.io.EOFException)7 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)6 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)6 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)5 Map (java.util.Map)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 AuditEvent (org.xipki.audit.AuditEvent)3 ResponderAndPath (org.xipki.ocsp.api.ResponderAndPath)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 Unpooled (io.netty.buffer.Unpooled)2 Channel (io.netty.channel.Channel)2