Search in sources :

Example 1 with Headers

use of ratpack.core.http.Headers in project ratpack by ratpack.

the class InferringPublicAddress method getHostData.

private HostAndPort getHostData(Request request) {
    Headers headers = request.getHeaders();
    String hostPortString = Strings.emptyToNull(headers.get(HOST.toString()));
    return hostPortString != null ? HostAndPort.fromString(hostPortString) : null;
}
Also used : Headers(ratpack.core.http.Headers)

Example 2 with Headers

use of ratpack.core.http.Headers in project ratpack by ratpack.

the class NettyHandlerAdapter method newRequest.

private void newRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest) throws Exception {
    if (!nettyRequest.decoderResult().isSuccess()) {
        LOGGER.debug("Failed to decode HTTP request.", nettyRequest.decoderResult().cause());
        sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        return;
    }
    Headers requestHeaders = new NettyHeadersBackedHeaders(nettyRequest.headers());
    // Find the content length we will use this as an indicator of a body
    long contentLength = HttpUtil.getContentLength(nettyRequest, -1L);
    String transferEncoding = requestHeaders.get(HttpHeaderNames.TRANSFER_ENCODING);
    // If there is a content length or transfer encoding that indicates there is a body
    boolean hasBody = contentLength > 0 || transferEncoding != null;
    RequestBody requestBody = hasBody ? new RequestBody(contentLength, nettyRequest, ctx) : null;
    Channel channel = ctx.channel();
    if (requestBody != null) {
        channel.attr(BODY_ACCUMULATOR_KEY).set(requestBody);
    }
    InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
    InetSocketAddress socketAddress = (InetSocketAddress) channel.localAddress();
    ConnectionIdleTimeout connectionIdleTimeout = ConnectionIdleTimeout.of(channel);
    DefaultRequest request = new DefaultRequest(clock.instant(), requestHeaders, nettyRequest.method(), nettyRequest.protocolVersion(), nettyRequest.uri(), remoteAddress, socketAddress, serverRegistry.get(ServerConfig.class), requestBody, connectionIdleTimeout, channel.attr(CLIENT_SSL_KEY).get());
    HttpHeaders nettyHeaders = new DefaultHttpHeaders();
    MutableHeaders responseHeaders = new NettyHeadersBackedMutableHeaders(nettyHeaders);
    AtomicBoolean transmitted = new AtomicBoolean(false);
    DefaultResponseTransmitter responseTransmitter = new DefaultResponseTransmitter(transmitted, channel, clock, nettyRequest, request, nettyHeaders, requestBody);
    ctx.channel().attr(RESPONSE_TRANSMITTER_KEY).set(responseTransmitter);
    Action<Action<Object>> subscribeHandler = thing -> {
        transmitted.set(true);
        ctx.channel().attr(CHANNEL_SUBSCRIBER_ATTRIBUTE_KEY).set(thing);
    };
    DefaultContext.RequestConstants requestConstants = new DefaultContext.RequestConstants(applicationConstants, request, channel, responseTransmitter, subscribeHandler);
    Response response = new DefaultResponse(responseHeaders, ctx.alloc(), responseTransmitter);
    requestConstants.response = response;
    DefaultContext.start(channel.eventLoop(), requestConstants, serverRegistry, handlers, execution -> {
        if (!transmitted.get()) {
            Handler lastHandler = requestConstants.handler;
            StringBuilder description = new StringBuilder();
            description.append("No response sent for ").append(request.getMethod().getName()).append(" request to ").append(request.getUri());
            if (lastHandler != null) {
                description.append(" (last handler: ");
                if (lastHandler instanceof DescribingHandler) {
                    ((DescribingHandler) lastHandler).describeTo(description);
                } else {
                    DescribingHandlers.describeTo(lastHandler, description);
                }
                description.append(")");
            }
            String message = description.toString();
            LOGGER.warn(message);
            response.getHeaders().clear();
            ByteBuf body;
            if (development) {
                CharBuffer charBuffer = CharBuffer.wrap(message);
                body = ByteBufUtil.encodeString(ctx.alloc(), charBuffer, CharsetUtil.UTF_8);
                response.contentType(HttpHeaderConstants.PLAIN_TEXT_UTF8);
            } else {
                body = Unpooled.EMPTY_BUFFER;
            }
            response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, body.readableBytes());
            execution.getController().fork().eventLoop(execution.getEventLoop()).start(e -> responseTransmitter.transmit(HttpResponseStatus.INTERNAL_SERVER_ERROR, body));
        }
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) ServerConfig(ratpack.core.server.ServerConfig) LoggerFactory(org.slf4j.LoggerFactory) DecodingErrorLevel(ratpack.core.server.DecodingErrorLevel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DescribingHandlers(ratpack.core.handling.internal.DescribingHandlers) ChainHandler(ratpack.core.handling.internal.ChainHandler) ratpack.core.http.internal(ratpack.core.http.internal) Unpooled(io.netty.buffer.Unpooled) SSLEngine(javax.net.ssl.SSLEngine) DefaultContext(ratpack.core.handling.internal.DefaultContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) Locale(java.util.Locale) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) CharsetUtil(io.netty.util.CharsetUtil) Handler(ratpack.core.handling.Handler) DescribingHandler(ratpack.core.handling.internal.DescribingHandler) io.netty.channel(io.netty.channel) DecoderException(io.netty.handler.codec.DecoderException) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Logger(org.slf4j.Logger) MutableHeaders(ratpack.core.http.MutableHeaders) CharBuffer(java.nio.CharBuffer) ClosedChannelException(java.nio.channels.ClosedChannelException) Headers(ratpack.core.http.Headers) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) ByteBufUtil(io.netty.buffer.ByteBufUtil) io.netty.handler.codec.http(io.netty.handler.codec.http) Response(ratpack.core.http.Response) Registry(ratpack.exec.registry.Registry) SslHandler(io.netty.handler.ssl.SslHandler) Handlers(ratpack.core.handling.Handlers) Action(ratpack.func.Action) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Clock(java.time.Clock) ExecController(ratpack.exec.ExecController) DefaultRenderController(ratpack.core.render.internal.DefaultRenderController) Action(ratpack.func.Action) MutableHeaders(ratpack.core.http.MutableHeaders) Headers(ratpack.core.http.Headers) InetSocketAddress(java.net.InetSocketAddress) CharBuffer(java.nio.CharBuffer) ByteBuf(io.netty.buffer.ByteBuf) ServerConfig(ratpack.core.server.ServerConfig) MutableHeaders(ratpack.core.http.MutableHeaders) DescribingHandler(ratpack.core.handling.internal.DescribingHandler) ChainHandler(ratpack.core.handling.internal.ChainHandler) Handler(ratpack.core.handling.Handler) DescribingHandler(ratpack.core.handling.internal.DescribingHandler) SslHandler(io.netty.handler.ssl.SslHandler) Response(ratpack.core.http.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultContext(ratpack.core.handling.internal.DefaultContext)

Example 3 with Headers

use of ratpack.core.http.Headers in project ratpack by ratpack.

the class RequestActionSupport method toReceivedResponse.

protected ReceivedResponse toReceivedResponse(HttpResponse msg, ByteBuf responseBuffer) {
    responseBuffer.touch();
    final Headers headers = new NettyHeadersBackedHeaders(msg.headers());
    String contentType = headers.get(HttpHeaderConstants.CONTENT_TYPE);
    final ByteBufBackedTypedData typedData = new ByteBufBackedTypedData(responseBuffer, DefaultMediaType.get(contentType));
    final Status status = new DefaultStatus(msg.status());
    return new DefaultReceivedResponse(status, headers, typedData);
}
Also used : Status(ratpack.core.http.Status) Headers(ratpack.core.http.Headers)

Example 4 with Headers

use of ratpack.core.http.Headers in project ratpack by ratpack.

the class InferringPublicAddress method determineScheme.

private String determineScheme(Request request) {
    Headers headers = request.getHeaders();
    String forwardedSsl = headers.get(X_FORWARDED_SSL.toString());
    String forwardedProto = headers.get(X_FORWARDED_PROTO.toString());
    if (ON.toString().equalsIgnoreCase(forwardedSsl)) {
        return HTTPS_SCHEME;
    }
    return forwardedProto != null ? forwardedProto : defaultScheme;
}
Also used : Headers(ratpack.core.http.Headers)

Example 5 with Headers

use of ratpack.core.http.Headers in project ratpack by ratpack.

the class InferringPublicAddress method getForwardedHostData.

private HostAndPort getForwardedHostData(Request request) {
    Headers headers = request.getHeaders();
    String forwardedHostHeader = Strings.emptyToNull(headers.get(X_FORWARDED_HOST.toString()));
    String hostPortString = forwardedHostHeader != null ? Iterables.getFirst(FORWARDED_HOST_SPLITTER.split(forwardedHostHeader), null) : null;
    return hostPortString != null ? HostAndPort.fromString(hostPortString) : null;
}
Also used : Headers(ratpack.core.http.Headers)

Aggregations

Headers (ratpack.core.http.Headers)5 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufUtil (io.netty.buffer.ByteBufUtil)1 Unpooled (io.netty.buffer.Unpooled)1 io.netty.channel (io.netty.channel)1 DecoderException (io.netty.handler.codec.DecoderException)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 SslHandler (io.netty.handler.ssl.SslHandler)1 SslHandshakeCompletionEvent (io.netty.handler.ssl.SslHandshakeCompletionEvent)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 AttributeKey (io.netty.util.AttributeKey)1 CharsetUtil (io.netty.util.CharsetUtil)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CharBuffer (java.nio.CharBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Clock (java.time.Clock)1 Locale (java.util.Locale)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1