Java Code Examples for io.netty.handler.codec.http.HttpMethod#CONNECT

The following examples show how to use io.netty.handler.codec.http.HttpMethod#CONNECT . 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: HttpProxyHandler.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    final String host = NetUtil.toSocketAddressString(raddr);
    FullHttpRequest req = new DefaultFullHttpRequest(
            HttpVersion.HTTP_1_1, HttpMethod.CONNECT,
            host,
            Unpooled.EMPTY_BUFFER, false);

    req.headers().set(HttpHeaderNames.HOST, host);

    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }

    if (headers != null) {
        req.headers().add(headers);
    }

    return req;
}
 
Example 2
Source File: ProxyTunnelInitHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void handledAdded_writesRequest() {
    Promise<Channel> promise = GROUP.next().newPromise();
    ProxyTunnelInitHandler handler = new ProxyTunnelInitHandler(mockChannelPool, REMOTE_HOST, promise);
    handler.handlerAdded(mockCtx);

    ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
    verify(mockChannel).writeAndFlush(requestCaptor.capture());

    String uri = REMOTE_HOST.getHost() + ":" + REMOTE_HOST.getPort();
    HttpRequest expectedRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT, uri,
                                                             Unpooled.EMPTY_BUFFER, false);
    expectedRequest.headers().add(HttpHeaderNames.HOST, uri);

    assertThat(requestCaptor.getValue()).isEqualTo(expectedRequest);
}
 
Example 3
Source File: ThriftUnmarshaller.java    From xio with Apache License 2.0 6 votes vote down vote up
private static HttpMethod build(Http1Method method) {
  if (method != null) {
    switch (method) {
      case CONNECT:
        return HttpMethod.CONNECT;
      case DELETE:
        return HttpMethod.DELETE;
      case GET:
        return HttpMethod.GET;
      case HEAD:
        return HttpMethod.HEAD;
      case OPTIONS:
        return HttpMethod.OPTIONS;
      case PATCH:
        return HttpMethod.PATCH;
      case POST:
        return HttpMethod.POST;
      case PUT:
        return HttpMethod.PUT;
      case TRACE:
        return HttpMethod.TRACE;
    }
  }
  return null;
}
 
Example 4
Source File: ProxyTunnelInitHandler.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private HttpRequest connectRequest() {
    String uri = getUri();
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT, uri,
                                                     Unpooled.EMPTY_BUFFER, false);
    request.headers().add(HttpHeaderNames.HOST, uri);
    return request;
}
 
Example 5
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void getContent_throws_RequestContentDeserializationException_if_an_error_occurs_during_deserialization() throws IOException {
    // given
    RequestInfo<TestContentObject> requestInfoSpy = spy((RequestInfo<TestContentObject>) RequestInfoImpl.dummyInstanceForUnknownRequests());
    ObjectMapper objectMapperSpy = spy(new ObjectMapper());
    byte[] rawBytes = new byte[]{};
    doReturn(rawBytes).when(requestInfoSpy).getRawContentBytes();
    RuntimeException expectedRootCause = new RuntimeException("splat");
    doThrow(expectedRootCause).when(objectMapperSpy).readValue(any(byte[].class), any(TypeReference.class));
    HttpMethod method = HttpMethod.CONNECT;
    String path = UUID.randomUUID().toString();
    TypeReference<TestContentObject> typeRef = new TypeReference<TestContentObject>() {};
    doReturn(method).when(requestInfoSpy).getMethod();
    doReturn(path).when(requestInfoSpy).getPath();

    // when
    requestInfoSpy.setupContentDeserializer(objectMapperSpy, typeRef);
    Throwable actualEx = catchThrowable(() -> requestInfoSpy.getContent());

    // then
    assertThat(actualEx, notNullValue());
    assertThat(actualEx, instanceOf(RequestContentDeserializationException.class));
    RequestContentDeserializationException rcde = (RequestContentDeserializationException)actualEx;
    assertThat(rcde.desiredObjectType, sameInstance(typeRef));
    assertThat(rcde.httpMethod, is(String.valueOf(method)));
    assertThat(rcde.requestPath, is(path));
    assertThat(actualEx.getCause(), is(expectedRootCause));
}
 
Example 6
Source File: HTTPSProxyAuthConduitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void requestReceivedFromClient(FlowContext flowContext,
                                      HttpRequest httpRequest) {
    if (httpRequest.getMethod() != HttpMethod.CONNECT) {
        count.incrementAndGet();
    }
}
 
Example 7
Source File: HTTPSProxyConduitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void requestReceivedFromClient(FlowContext flowContext,
                                      HttpRequest httpRequest) {
    if (httpRequest.getMethod() != HttpMethod.CONNECT) {
        count.incrementAndGet();
    }
}
 
Example 8
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 3 votes vote down vote up
/**
 * Returns true if the specified request is a request to an origin server, rather than to a proxy server. If this
 * request is being MITM'd, this method always returns false. The format of requests to a proxy server are defined
 * in RFC 7230, section 5.3.2 (all other requests are considered requests to an origin server):
 <pre>
     When making a request to a proxy, other than a CONNECT or server-wide
     OPTIONS request (as detailed below), a client MUST send the target
     URI in absolute-form as the request-target.
     [...]
     An example absolute-form of request-line would be:
     GET http://www.example.org/pub/WWW/TheProject.html HTTP/1.1
     To allow for transition to the absolute-form for all requests in some
     future version of HTTP, a server MUST accept the absolute-form in
     requests, even though HTTP/1.1 clients will only send them in
     requests to proxies.
 </pre>
 *
 * @param httpRequest the request to evaluate
 * @return true if the specified request is a request to an origin server, otherwise false
 */
private boolean isRequestToOriginServer(HttpRequest httpRequest) {
    // while MITMing, all HTTPS requests are requests to the origin server, since the client does not know
    // the request is being MITM'd by the proxy
    if (httpRequest.getMethod() == HttpMethod.CONNECT || isMitming()) {
        return false;
    }

    // direct requests to the proxy have the path only without a scheme
    String uri = httpRequest.getUri();
    return !HTTP_SCHEME.matcher(uri).matches();
}