@Override
  public void process(final HttpRequest request, final HttpContext context)
      throws HttpException, IOException {
    Args.notNull(request, "HTTP request");

    final String method = request.getRequestLine().getMethod();
    if (method.equalsIgnoreCase("CONNECT")) {
      request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
      return;
    }

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    // Obtain the client connection (required)
    final RouteInfo route = clientContext.getHttpRoute();
    if (route == null) {
      this.log.debug("Connection route not set in the context");
      return;
    }

    if (route.getHopCount() == 1 || route.isTunnelled()) {
      if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) {
        request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
      }
    }
    if (route.getHopCount() == 2 && !route.isTunnelled()) {
      if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) {
        request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
      }
    }
  }
  @Override
  public void process(final HttpRequest request, final HttpContext context)
      throws HttpException, IOException {
    Args.notNull(request, "HTTP request");

    if (!request.containsHeader(HTTP.EXPECT_DIRECTIVE)) {
      if (request instanceof HttpEntityEnclosingRequest) {
        final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        // Do not send the expect header if request body is known to be empty
        if (entity != null
            && entity.getContentLength() != 0
            && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
          final HttpClientContext clientContext = HttpClientContext.adapt(context);
          final RequestConfig config = clientContext.getRequestConfig();
          if (config.isExpectContinueEnabled()) {
            request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
          }
        }
      }
    }
  }