Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1325

Using a HttpRequest that is not an HttpUriRequest when host in URI is not target host results in an invalid request

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.3 Alpha1
    • 4.3 Beta1
    • HttpClient (classic)
    • None

    Description

      Using the following code:
      CloseableHttpClient httpClient = HttpClientBuilder.create().build();
      HttpHost httpHost = new HttpHost("127.0.0.1", 80, "http");
      HttpRequest request = new BasicHttpRequest("GET", "http://www.foo.com/test");
      httpClient.execute(httpHost, request);

      HttpClient sends this request:
      GET http://www.foo.com/test HTTP/1.1
      Host: 127.0.0.1:80
      Connection: Keep-Alive
      User-Agent: Apache-HttpClient/4.3-alpha2-SNAPSHOT (java 1.5)
      Accept-Encoding: gzip,deflate

      The host header is not consistent with the request URI. This is due to org.apache.http.impl.execchain.ProtocolExec:

      HttpHost target = null;
      if (virtualHost != null)

      { target = virtualHost; }

      else {
      final HttpRequest original = request.getOriginal();
      if (original instanceof HttpUriRequest) {
      final URI uri = ((HttpUriRequest) original).getURI();
      if (uri.isAbsolute())

      { target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); }

      }
      }
      if (target == null)

      { target = route.getTargetHost(); }

      So for a HttpUriRequest the host is taken from the URI but for other requests it is taken from the target server.
      I think it should be taken from the target server only if it cannot be determined from the URI (non-absolute URI).

      I will fix this.

      Attachments

        Activity

          People

            Unassigned Unassigned
            fx.bonnet Francois-Xavier Bonnet
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: