Search in sources :

Example 21 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project netty by netty.

the class SpdyHttpDecoder method createHttpRequest.

private static FullHttpRequest createHttpRequest(SpdyHeadersFrame requestFrame, ByteBufAllocator alloc) throws Exception {
    // Create the first line of the request from the name/value pairs
    SpdyHeaders headers = requestFrame.headers();
    HttpMethod method = HttpMethod.valueOf(headers.getAsString(METHOD));
    String url = headers.getAsString(PATH);
    HttpVersion httpVersion = HttpVersion.valueOf(headers.getAsString(VERSION));
    headers.remove(METHOD);
    headers.remove(PATH);
    headers.remove(VERSION);
    boolean release = true;
    ByteBuf buffer = alloc.buffer();
    try {
        FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, url, buffer);
        // Remove the scheme header
        headers.remove(SCHEME);
        // Replace the SPDY host header with the HTTP host header
        CharSequence host = headers.get(HOST);
        headers.remove(HOST);
        req.headers().set(HttpHeaderNames.HOST, host);
        for (Map.Entry<CharSequence, CharSequence> e : requestFrame.headers()) {
            req.headers().add(e.getKey(), e.getValue());
        }
        // The Connection and Keep-Alive headers are no longer valid
        HttpUtil.setKeepAlive(req, true);
        // Transfer-Encoding header is not valid
        req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        release = false;
        return req;
    } finally {
        if (release) {
            buffer.release();
        }
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 22 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project graylog2-server by Graylog2.

the class HttpHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    final Channel channel = ctx.channel();
    final boolean keepAlive = HttpUtil.isKeepAlive(request);
    final HttpVersion httpRequestVersion = request.protocolVersion();
    final String origin = request.headers().get(HttpHeaderNames.ORIGIN);
    // to allow for future changes, let's be at least a little strict in what we accept here.
    if (HttpMethod.OPTIONS.equals(request.method())) {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.OK, origin);
        return;
    } else if (!HttpMethod.POST.equals(request.method())) {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.METHOD_NOT_ALLOWED, origin);
        return;
    }
    final boolean correctPath = "/gelf".equals(request.uri());
    if (correctPath && request instanceof FullHttpRequest) {
        final FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
        final ByteBuf buffer = fullHttpRequest.content();
        // send on to raw message handler
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.ACCEPTED, origin);
        ctx.fireChannelRead(buffer.retain());
    } else {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.NOT_FOUND, origin);
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) 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