Example usage for io.netty.handler.codec.http HttpMethod CONNECT

List of usage examples for io.netty.handler.codec.http HttpMethod CONNECT

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod CONNECT.

Prototype

HttpMethod CONNECT

To view the source code for io.netty.handler.codec.http HttpMethod CONNECT.

Click Source Link

Document

This specification reserves the method name CONNECT for use with a proxy that can dynamically switch to being a tunnel

Usage

From source file:com.rackspacecloud.blueflood.http.RouteMatcher.java

License:Apache License

public void route(ChannelHandlerContext context, FullHttpRequest request) {
    final String method = request.getMethod().name();
    final String URI = request.getUri();

    // Method not implemented for any resource. So return 501.
    if (method == null || !implementedVerbs.contains(method)) {
        route(context, request, unsupportedVerbsHandler);
        return;//w  w w  . jav a 2s.c  om
    }

    final Pattern pattern = getMatchingPatternForURL(URI);

    // No methods registered for this pattern i.e. URL isn't registered. Return 404.
    if (pattern == null) {
        route(context, request, noRouteHandler);
        return;
    }

    final Set<String> supportedMethods = getSupportedMethods(pattern);
    if (supportedMethods == null) {
        log.warn("No supported methods registered for a known pattern " + pattern);
        route(context, request, noRouteHandler);
        return;
    }

    // The method requested is not available for the resource. Return 405.
    if (!supportedMethods.contains(method)) {
        route(context, request, unsupportedMethodHandler);
        return;
    }
    PatternRouteBinding binding = null;
    if (method.equals(HttpMethod.GET.name())) {
        binding = getBindings.get(pattern);
    } else if (method.equals(HttpMethod.PUT.name())) {
        binding = putBindings.get(pattern);
    } else if (method.equals(HttpMethod.POST.name())) {
        binding = postBindings.get(pattern);
    } else if (method.equals(HttpMethod.DELETE.name())) {
        binding = deleteBindings.get(pattern);
    } else if (method.equals(HttpMethod.PATCH.name())) {
        binding = deleteBindings.get(pattern);
    } else if (method.equals(HttpMethod.OPTIONS.name())) {
        binding = optionsBindings.get(pattern);
    } else if (method.equals(HttpMethod.HEAD.name())) {
        binding = headBindings.get(pattern);
    } else if (method.equals(HttpMethod.TRACE.name())) {
        binding = traceBindings.get(pattern);
    } else if (method.equals(HttpMethod.CONNECT.name())) {
        binding = connectBindings.get(pattern);
    }

    if (binding != null) {
        request = updateRequestHeaders(request, binding);
        route(context, request, binding.handler);
    } else {
        throw new RuntimeException("Cannot find a valid binding for URL " + URI);
    }
}

From source file:com.rackspacecloud.blueflood.http.RouteMatcher.java

License:Apache License

public void connect(String pattern, HttpRequestHandler handler) {
    addBinding(pattern, HttpMethod.CONNECT.name(), handler, connectBindings);
}

From source file:com.xx_dev.apn.proxy.ApnProxySchemaHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext uaChannelCtx, final Object msg) throws Exception {

    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;

        String originalHost = HostNamePortUtil.getHostName(httpRequest);
        int originalPort = HostNamePortUtil.getPort(httpRequest);

        ApnProxyRemote apnProxyRemote = ApnProxyRemoteChooser.chooseRemoteAddr(originalHost, originalPort);

        Channel uaChannel = uaChannelCtx.channel();

        ApnProxyConnectionAttribute apnProxyConnectionAttribute = ApnProxyConnectionAttribute.build(
                uaChannel.remoteAddress().toString(), httpRequest.getMethod().name(), httpRequest.getUri(),
                httpRequest.getProtocolVersion().text(),
                httpRequest.headers().get(HttpHeaders.Names.USER_AGENT), apnProxyRemote);

        uaChannelCtx.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);
        uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);

        if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
            if (uaChannelCtx.pipeline().get(ApnProxyUserAgentForwardHandler.HANDLER_NAME) != null) {
                uaChannelCtx.pipeline().remove(ApnProxyUserAgentForwardHandler.HANDLER_NAME);
            }//from   w  w  w  .  ja  v  a2 s .  c o m
            if (uaChannelCtx.pipeline().get(ApnProxyUserAgentTunnelHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(ApnProxyUserAgentTunnelHandler.HANDLER_NAME,
                        new ApnProxyUserAgentTunnelHandler());
            }
        } else {
            if (uaChannelCtx.pipeline().get(ApnProxyUserAgentForwardHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(ApnProxyUserAgentForwardHandler.HANDLER_NAME,
                        new ApnProxyUserAgentForwardHandler());
            }
        }
    }

    LoggerUtil.debug(logger, uaChannelCtx.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "UA msg", msg);

    uaChannelCtx.fireChannelRead(msg);
}

From source file:com.xx_dev.apn.proxy.utils.HostNamePortUtil.java

License:Apache License

public static String getHostName(HttpRequest httpRequest) {
    String originalHostHeader = httpRequest.headers().get(HttpHeaders.Names.HOST);

    if (StringUtils.isBlank(originalHostHeader) && httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
        originalHostHeader = httpRequest.getUri();
    }//from w  w w . j av  a2 s  .  c  o  m

    if (StringUtils.isNotBlank(originalHostHeader)) {
        String originalHost = StringUtils.split(originalHostHeader, ": ")[0];
        return originalHost;
    } else {
        String uriStr = httpRequest.getUri();
        try {
            URI uri = new URI(uriStr);

            String schema = uri.getScheme();

            String originalHost = uri.getHost();

            return originalHost;
        } catch (URISyntaxException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }
}

From source file:com.xx_dev.apn.proxy.utils.HostNamePortUtil.java

License:Apache License

public static int getPort(HttpRequest httpRequest) {
    int originalPort = 80;

    if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
        originalPort = 443;//www . j a va2  s . co  m
    }

    String originalHostHeader = httpRequest.headers().get(HttpHeaders.Names.HOST);

    if (StringUtils.isBlank(originalHostHeader) && httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
        originalHostHeader = httpRequest.getUri();
    }

    if (StringUtils.isNotBlank(originalHostHeader)) {
        if (StringUtils.split(originalHostHeader, ": ").length == 2) {
            originalPort = Integer.parseInt(StringUtils.split(originalHostHeader, ": ")[1]);
        }
    } else {
        String uriStr = httpRequest.getUri();
        try {
            URI uri = URI.create(uriStr);

            if (uri.getPort() > 0) {
                originalPort = uri.getPort();
            }
        } catch (IllegalArgumentException e) {
            logger.error(e.getMessage(), e);
            originalPort = -1;
        }
    }

    return originalPort;
}

From source file:io.advantageous.conekt.http.impl.ClientConnection.java

License:Open Source License

void handleResponseEnd(LastHttpContent trailer) {
    currentResponse.handleEnd(trailer);//  ww w . j  a  v  a2  s . co  m

    // We don't signal response end for a 100-continue response as a real response will follow
    // Also we keep the connection open for an HTTP CONNECT
    if (currentResponse.statusCode() != 100
            && requestForResponse.getRequest().getMethod() != HttpMethod.CONNECT) {

        boolean close = false;
        // See https://tools.ietf.org/html/rfc7230#section-6.3
        String responseConnectionHeader = currentResponse.getHeader(HttpHeaders.Names.CONNECTION);
        HttpVersion protocolVersion = requestForResponse.getRequest().getProtocolVersion();
        String requestConnectionHeader = requestForResponse.getRequest().headers()
                .get(HttpHeaders.Names.CONNECTION);
        // We don't need to protect against concurrent changes on forceClose as it only goes from false -> true
        if (HttpHeaders.Values.CLOSE.equalsIgnoreCase(responseConnectionHeader)
                || HttpHeaders.Values.CLOSE.equalsIgnoreCase(requestConnectionHeader)) {
            // In all cases, if we have a close connection option then we SHOULD NOT treat the connection as persistent
            close = true;
        } else if (protocolVersion == HttpVersion.HTTP_1_0
                && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader)) {
            // In the HTTP/1.0 case both request/response need a keep-alive connection header the connection to be persistent
            // currently Conekt forces the Connection header if keepalive is enabled for 1.0
            close = true;
        }
        listener.responseEnded(this, close);
    }
    currentResponse = null;
}

From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private Handler<HttpClientResponse> checkConnect(io.advantageous.conekt.http.HttpMethod method,
        Handler<HttpClientResponse> handler) {
    if (method == io.advantageous.conekt.http.HttpMethod.CONNECT) {
        // special handling for CONNECT
        handler = connectHandler(handler);
    }/* w w  w.  j  a  va 2 s .co  m*/
    return handler;
}

From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private HttpMethod toNettyHttpMethod(io.advantageous.conekt.http.HttpMethod method) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }/*from  www  .  j  av  a  2 s  . c om*/
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default:
        throw new IllegalArgumentException();
    }
}

From source file:io.vertx.core.http.impl.HttpUtils.java

License:Open Source License

static HttpMethod toNettyHttpMethod(io.vertx.core.http.HttpMethod method, String rawMethod) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }/*from   w w  w .  j a  v  a  2  s  . c  o  m*/
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default: {
        return HttpMethod.valueOf(rawMethod);
    }
    }
}

From source file:no.nb.nna.broprox.harvester.proxy.RecorderFilterSourceAdapter.java

License:Apache License

@Override
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext clientCtx) {
    String uri = originalRequest.uri();

    MDC.put("uri", uri);
    LOG.trace("Method: {}, Client Context: {}", originalRequest.method(), clientCtx);

    if (originalRequest.method() == HttpMethod.CONNECT) {
        if (clientCtx != null) {
            String prefix = "https://" + uri.replaceFirst(":443$", "");
            clientCtx.channel().attr(CONNECTED_URL).set(prefix);
        }//from   w w w  . ja v  a  2s  .c o  m
        return HttpFiltersAdapter.NOOP_FILTER;
    }

    String connectedUrl = clientCtx.channel().attr(CONNECTED_URL).get();
    if (connectedUrl == null) {
        return new RecorderFilter(uri, originalRequest, clientCtx, db, contentWriterClient, sessionRegistry,
                cache);
    }
    return new RecorderFilter(connectedUrl + uri, originalRequest, clientCtx, db, contentWriterClient,
            sessionRegistry, cache);
}