Search in sources :

Example 16 with HttpVersion

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion in project xipki by xipki.

the class HttpRestServlet method service.

@Override
public FullHttpResponse service(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) {
    HttpVersion version = request.protocolVersion();
    HttpMethod method = request.method();
    if (method != HttpMethod.POST && method != HttpMethod.GET) {
        return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
    AuditEvent event = new AuditEvent(new Date());
    try {
        Rest rest = responderManager.getRest();
        HttpRequestMetadataRetriever httpRetriever = new HttpRequestMetadataRetrieverImpl(request, servletUri, sslSession, sslReverseProxyMode);
        byte[] requestBytes = readContent(request);
        RestResponse response = rest.service(servletUri.getPath(), event, requestBytes, httpRetriever);
        HttpResponseStatus status = HttpResponseStatus.valueOf(response.getStatusCode());
        FullHttpResponse resp = createResponse(version, status, response.getContentType(), response.getBody());
        for (String headerName : response.getHeaders().keySet()) {
            resp.headers().add(headerName, response.getHeaders().get(headerName));
        }
        return resp;
    } finally {
        event.finish();
        auditServiceRegister.getAuditService().logEvent(event);
    }
}
Also used : HttpRequestMetadataRetriever(org.xipki.ca.server.api.HttpRequestMetadataRetriever) Rest(org.xipki.ca.server.api.Rest) RestResponse(org.xipki.ca.server.api.RestResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) AuditEvent(org.xipki.audit.AuditEvent) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Date(java.util.Date)

Example 17 with HttpVersion

use of org.apache.flink.shaded.netty4.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 18 with HttpVersion

use of org.apache.flink.shaded.netty4.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)

Example 19 with HttpVersion

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion in project moco by dreamhead.

the class AbstractProxyResponseHandler method setupNormalResponse.

private HttpResponse setupNormalResponse(final org.apache.http.HttpResponse remoteResponse) throws IOException {
    HttpVersion httpVersion = HttpVersion.valueOf(remoteResponse.getProtocolVersion().toString());
    HttpResponseStatus status = HttpResponseStatus.valueOf(remoteResponse.getStatusLine().getStatusCode());
    FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, status);
    response.setStatus(status);
    Arrays.stream(remoteResponse.getAllHeaders()).filter(this::isResponseHeader).forEach(header -> response.headers().set(header.getName(), header.getValue()));
    HttpEntity entity = remoteResponse.getEntity();
    if (entity != null) {
        byte[] content = toByteArray(entity);
        if (content.length > 0) {
            ByteBuf buffer = Unpooled.copiedBuffer(content);
            response.content().writeBytes(buffer);
        }
    }
    return newResponse(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpEntity(org.apache.http.HttpEntity) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Aggregations

HttpVersion (io.netty.handler.codec.http.HttpVersion)19 HttpMethod (io.netty.handler.codec.http.HttpMethod)10 ByteBuf (io.netty.buffer.ByteBuf)8 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)7 EOFException (java.io.EOFException)7 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)5 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)5 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)4 Map (java.util.Map)4 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 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2