Beispiel #1
0
  /* ------------------------------------------------------------ */
  public void startConnection(HttpDestination destination) throws IOException {
    SocketChannel channel = null;
    try {
      channel = SocketChannel.open();
      Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
      channel.socket().setTcpNoDelay(true);

      if (_httpClient.isConnectBlocking()) {
        channel.socket().connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
        channel.configureBlocking(false);
        _selectorManager.register(channel, destination);
      } else {
        channel.configureBlocking(false);
        channel.connect(address.toSocketAddress());
        _selectorManager.register(channel, destination);
        ConnectTimeout connectTimeout = new ConnectTimeout(channel, destination);
        _httpClient.schedule(connectTimeout, _httpClient.getConnectTimeout());
        _connectingChannels.put(channel, connectTimeout);
      }
    } catch (UnresolvedAddressException ex) {
      if (channel != null) channel.close();
      destination.onConnectionFailed(ex);
    } catch (IOException ex) {
      if (channel != null) channel.close();
      destination.onConnectionFailed(ex);
    }
  }
Beispiel #2
0
      private void tunnel(HttpDestination destination, final Connection connection) {
        String target = destination.getOrigin().getAddress().asString();
        Origin.Address proxyAddress = destination.getConnectAddress();
        HttpClient httpClient = destination.getHttpClient();
        Request connect =
            httpClient
                .newRequest(proxyAddress.getHost(), proxyAddress.getPort())
                .scheme(HttpScheme.HTTP.asString())
                .method(HttpMethod.CONNECT)
                .path(target)
                .header(HttpHeader.HOST, target)
                .timeout(httpClient.getConnectTimeout(), TimeUnit.MILLISECONDS);

        connection.send(
            connect,
            new Response.CompleteListener() {
              @Override
              public void onComplete(Result result) {
                if (result.isFailed()) {
                  tunnelFailed(result.getFailure());
                } else {
                  Response response = result.getResponse();
                  if (response.getStatus() == 200) {
                    tunnelSucceeded();
                  } else {
                    tunnelFailed(
                        new HttpResponseException(
                            "Received " + response + " for " + result.getRequest(), response));
                  }
                }
              }
            });
      }