Java Code Examples for io.netty.handler.codec.http.HttpHeaders#setKeepAlive()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#setKeepAlive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpdyHttpDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private static FullHttpResponse createHttpResponse(ChannelHandlerContext ctx, int spdyVersion,
                                                   SpdyHeadersFrame responseFrame,
                                                   boolean validateHeaders) throws Exception {

    // Create the first line of the response from the name/value pairs
    HttpResponseStatus status = SpdyHeaders.getStatus(spdyVersion, responseFrame);
    HttpVersion version = SpdyHeaders.getVersion(spdyVersion, responseFrame);
    SpdyHeaders.removeStatus(spdyVersion, responseFrame);
    SpdyHeaders.removeVersion(spdyVersion, responseFrame);

    FullHttpResponse res = new DefaultFullHttpResponse(version, status, ctx.alloc().buffer(), validateHeaders);
    for (Map.Entry<String, String> e: responseFrame.headers()) {
        res.headers().add(e.getKey(), e.getValue());
    }

    // The Connection and Keep-Alive headers are no longer valid
    HttpHeaders.setKeepAlive(res, true);

    // Transfer-Encoding header is not valid
    res.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);
    res.headers().remove(HttpHeaders.Names.TRAILER);

    return res;
}
 
Example 2
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/***************************************************************************
 * Reading
 **************************************************************************/

@Override
protected ConnectionState readHTTPInitial(HttpRequest httpRequest) {
    LOG.debug("Received raw request: {}", httpRequest);

    // if we cannot parse the request, immediately return a 400 and close the connection, since we do not know what state
    // the client thinks the connection is in
    if (httpRequest.getDecoderResult().isFailure()) {
        LOG.debug("Could not parse request from client. Decoder result: {}", httpRequest.getDecoderResult().toString());

        FullHttpResponse response = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.BAD_REQUEST,
                "Unable to parse HTTP request");
        HttpHeaders.setKeepAlive(response, false);

        respondWithShortCircuitResponse(response);

        return DISCONNECT_REQUESTED;
    }

    boolean authenticationRequired = authenticationRequired(httpRequest);

    if (authenticationRequired) {
        LOG.debug("Not authenticated!!");
        return AWAITING_PROXY_AUTHENTICATION;
    } else {
        return doReadHTTPInitial(httpRequest);
    }
}
 
Example 3
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Responds to the client with the specified "short-circuit" response. The response will be sent through the
 * {@link HttpFilters#proxyToClientResponse(HttpObject)} filter method before writing it to the client. The client
 * will not be disconnected, unless the response includes a "Connection: close" header, or the filter returns
 * a null HttpResponse (in which case no response will be written to the client and the connection will be
 * disconnected immediately). If the response is not a Bad Gateway or Gateway Timeout response, the response's headers
 * will be modified to reflect proxying, including adding a Via header, Date header, etc.
 *
 * @param httpResponse the response to return to the client
 * @return true if the connection will be kept open, or false if it will be disconnected.
 */
private boolean respondWithShortCircuitResponse(HttpResponse httpResponse) {
    // we are sending a response to the client, so we are done handling this request
    this.currentRequest = null;

    HttpResponse filteredResponse = (HttpResponse) currentFilters.proxyToClientResponse(httpResponse);
    if (filteredResponse == null) {
        disconnect();
        return false;
    }

    // allow short-circuit messages to close the connection. normally the Connection header would be stripped when modifying
    // the message for proxying, so save the keep-alive status before the modifications are made.
    boolean isKeepAlive = HttpHeaders.isKeepAlive(httpResponse);

    // if the response is not a Bad Gateway or Gateway Timeout, modify the headers "as if" the short-circuit response were proxied
    int statusCode = httpResponse.getStatus().code();
    if (statusCode != HttpResponseStatus.BAD_GATEWAY.code() && statusCode != HttpResponseStatus.GATEWAY_TIMEOUT.code()) {
        modifyResponseHeadersToReflectProxying(httpResponse);
    }

    // restore the keep alive status, if it was overwritten when modifying headers for proxying
    HttpHeaders.setKeepAlive(httpResponse, isKeepAlive);

    write(httpResponse);

    if (ProxyUtils.isLastChunk(httpResponse)) {
        writeEmptyBuffer();
    }

    if (!HttpHeaders.isKeepAlive(httpResponse)) {
        disconnect();
        return false;
    }

    return true;
}
 
Example 4
Source File: ProxyToServerConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnectionState readHTTPInitial(HttpResponse httpResponse) {
    LOG.debug("Received raw response: {}", httpResponse);

    if (httpResponse.getDecoderResult().isFailure()) {
        LOG.debug("Could not parse response from server. Decoder result: {}", httpResponse.getDecoderResult().toString());

        // create a "substitute" Bad Gateway response from the server, since we couldn't understand what the actual
        // response from the server was. set the keep-alive on the substitute response to false so the proxy closes
        // the connection to the server, since we don't know what state the server thinks the connection is in.
        FullHttpResponse substituteResponse = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.BAD_GATEWAY,
                "Unable to parse response from server");
        HttpHeaders.setKeepAlive(substituteResponse, false);
        httpResponse = substituteResponse;
    }

    currentFilters.serverToProxyResponseReceiving();

    rememberCurrentResponse(httpResponse);
    respondWith(httpResponse);

    if (ProxyUtils.isChunked(httpResponse)) {
        return AWAITING_CHUNK;
    } else {
        currentFilters.serverToProxyResponseReceived();

        return AWAITING_INITIAL;
    }
}
 
Example 5
Source File: ProxyToServerConnection.java    From yfs with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnectionState readHTTPInitial(HttpResponse httpResponse) {
    LOG.debug("Received raw response: {}", httpResponse);

    if (httpResponse.getDecoderResult().isFailure()) {
        LOG.debug("Could not parse response from server. Decoder result: {}", httpResponse.getDecoderResult().toString());

        // create a "substitute" Bad Gateway response from the server, since we couldn't understand what the actual
        // response from the server was. set the keep-alive on the substitute response to false so the proxy closes
        // the connection to the server, since we don't know what state the server thinks the connection is in.
        FullHttpResponse substituteResponse = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.BAD_GATEWAY,
                "Unable to parse response from server");
        HttpHeaders.setKeepAlive(substituteResponse, false);
        httpResponse = substituteResponse;
    }

    currentFilters.serverToProxyResponseReceiving();

    rememberCurrentResponse(httpResponse);
    respondWith(httpResponse);

    if (ProxyUtils.isChunked(httpResponse)) {
        return AWAITING_CHUNK;
    } else {
        currentFilters.serverToProxyResponseReceived();

        return AWAITING_INITIAL;
    }
}
 
Example 6
Source File: SpdyHttpDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static FullHttpRequest createHttpRequest(int spdyVersion, SpdyHeadersFrame requestFrame)
        throws Exception {
    // Create the first line of the request from the name/value pairs
    SpdyHeaders headers     = requestFrame.headers();
    HttpMethod  method      = SpdyHeaders.getMethod(spdyVersion, requestFrame);
    String      url         = SpdyHeaders.getUrl(spdyVersion, requestFrame);
    HttpVersion httpVersion = SpdyHeaders.getVersion(spdyVersion, requestFrame);
    SpdyHeaders.removeMethod(spdyVersion, requestFrame);
    SpdyHeaders.removeUrl(spdyVersion, requestFrame);
    SpdyHeaders.removeVersion(spdyVersion, requestFrame);

    FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, url);

    // Remove the scheme header
    SpdyHeaders.removeScheme(spdyVersion, requestFrame);

    // Replace the SPDY host header with the HTTP host header
    String host = headers.get(SpdyHeaders.HttpNames.HOST);
    headers.remove(SpdyHeaders.HttpNames.HOST);
    req.headers().set(HttpHeaders.Names.HOST, host);

    for (Map.Entry<String, String> e: requestFrame.headers()) {
        req.headers().add(e.getKey(), e.getValue());
    }

    // The Connection and Keep-Alive headers are no longer valid
    HttpHeaders.setKeepAlive(req, true);

    // Transfer-Encoding header is not valid
    req.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);

    return req;
}
 
Example 7
Source File: HttpStreamDecoder.java    From netty-http2 with Apache License 2.0 5 votes vote down vote up
private StreamedHttpResponse createHttpResponse(HttpHeadersFrame httpHeadersFrame)
        throws Exception {
    // Create the first line of the request from the name/value pairs
    HttpResponseStatus status = HttpResponseStatus.valueOf(Integer.parseInt(
            httpHeadersFrame.headers().get(":status")));

    httpHeadersFrame.headers().remove(":status");

    StreamedHttpResponse response = new StreamedHttpResponse(HttpVersion.HTTP_1_1, status);
    for (Map.Entry<String, String> e : httpHeadersFrame.headers()) {
        String name = e.getKey();
        String value = e.getValue();
        if (name.charAt(0) != ':') {
            response.headers().add(name, value);
        }
    }

    // Set the Stream-ID as a header
    response.headers().set("X-SPDY-Stream-ID", httpHeadersFrame.getStreamId());

    // The Connection and Keep-Alive headers are no longer valid
    HttpHeaders.setKeepAlive(response, true);

    // Transfer-Encoding header is not valid
    response.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);
    response.headers().remove(HttpHeaders.Names.TRAILER);

    if (httpHeadersFrame.isLast()) {
        response.getContent().close();
        response.setDecoderResult(DecoderResult.SUCCESS);
    } else {
        response.setDecoderResult(DecoderResult.UNFINISHED);
    }

    return response;
}
 
Example 8
Source File: HttpStreamDecoder.java    From netty-http2 with Apache License 2.0 4 votes vote down vote up
private StreamedHttpRequest createHttpRequest(HttpHeadersFrame httpHeadersFrame)
        throws Exception {
    // Create the first line of the request from the name/value pairs
    HttpMethod method = HttpMethod.valueOf(httpHeadersFrame.headers().get(":method"));
    String url = httpHeadersFrame.headers().get(":path");

    httpHeadersFrame.headers().remove(":method");
    httpHeadersFrame.headers().remove(":path");

    StreamedHttpRequest request = new StreamedHttpRequest(HttpVersion.HTTP_1_1, method, url);

    // Remove the scheme header
    httpHeadersFrame.headers().remove(":scheme");

    // Replace the SPDY host header with the HTTP host header
    String host = httpHeadersFrame.headers().get(":authority");
    httpHeadersFrame.headers().remove(":authority");
    httpHeadersFrame.headers().set("host", host);

    for (Map.Entry<String, String> e : httpHeadersFrame.headers()) {
        String name = e.getKey();
        String value = e.getValue();
        if (name.charAt(0) != ':') {
            request.headers().add(name, value);
        }
    }

    // Set the Stream-ID as a header
    request.headers().set("X-SPDY-Stream-ID", httpHeadersFrame.getStreamId());

    // The Connection and Keep-Alive headers are no longer valid
    HttpHeaders.setKeepAlive(request, true);

    // Transfer-Encoding header is not valid
    request.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);

    if (httpHeadersFrame.isLast()) {
        request.getContent().close();
        request.setDecoderResult(DecoderResult.SUCCESS);
    } else {
        request.setDecoderResult(DecoderResult.UNFINISHED);
    }

    return request;
}