Skip to content

Commit

Permalink
Merge pull request #4791 from eclipse/jetty-9.4.x-4787-mutable_host_p…
Browse files Browse the repository at this point in the history
…ort_client_request

Fixes #4787 - Make org.eclipse.jetty.client.HttpRequest's host name w…
  • Loading branch information
sbordet authored Apr 27, 2020
2 parents 3c1f42c + 78e697d commit 8929791
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public class HttpRequest implements Request
private final AtomicReference<Throwable> aborted = new AtomicReference<>();
private final HttpClient client;
private final HttpConversation conversation;
private final String host;
private final int port;
private URI uri;
private String scheme;
private String host;
private int port;
private String path;
private String query;
private URI uri;
private String method = HttpMethod.GET.asString();
private HttpVersion version = HttpVersion.HTTP_1_1;
private long idleTimeout = -1;
Expand Down Expand Up @@ -135,12 +135,28 @@ public String getHost()
return host;
}

@Override
public Request host(String host)
{
this.host = host;
this.uri = null;
return this;
}

@Override
public int getPort()
{
return port;
}

@Override
public Request port(int port)
{
this.port = port;
this.uri = null;
return this;
}

@Override
public String getMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,39 @@
public interface Request
{
/**
* @return the scheme of this request, such as "http" or "https"
* @return the URI scheme of this request, such as "http" or "https"
*/
String getScheme();

/**
* @param scheme the scheme of this request, such as "http" or "https"
* @param scheme the URI scheme of this request, such as "http" or "https"
* @return this request object
*/
Request scheme(String scheme);

/**
* @return the host of this request, such as "127.0.0.1" or "google.com"
* @return the URI host of this request, such as "127.0.0.1" or "google.com"
*/
String getHost();

/**
* @return the port of this request such as 80 or 443
* @param host the URI host of this request, such as "127.0.0.1" or "google.com"
* @return this request object
*/
Request host(String host);

/**
* @return the URI port of this request such as 80 or 443
*/
int getPort();

/**
*
* @param port the URI port of this request such as 80 or 443
* @return this request object
*/
Request port(int port);

/**
* @return the method of this request, such as GET or POST, as a String
*/
Expand All @@ -89,26 +102,26 @@ public interface Request
Request method(String method);

/**
* @return the path of this request, such as "/" or "/path" - without the query
* @return the URI path of this request, such as "/" or "/path" - without the query
* @see #getQuery()
*/
String getPath();

/**
* Specifies the path - and possibly the query - of this request.
* Specifies the URI path - and possibly the query - of this request.
* If the query part is specified, parameter values must be properly
* {@link URLEncoder#encode(String, String) UTF-8 URL encoded}.
* For example, if the value for parameter "currency" is the euro symbol &euro; then the
* query string for this parameter must be "currency=%E2%82%AC".
* For transparent encoding of parameter values, use {@link #param(String, String)}.
*
* @param path the path of this request, such as "/" or "/path?param=1"
* @param path the URI path of this request, such as "/" or "/path?param=1"
* @return this request object
*/
Request path(String path);

/**
* @return the query string of this request such as "param=1"
* @return the URI query string of this request such as "param=1"
* @see #getPath()
* @see #getParams()
*/
Expand All @@ -131,12 +144,12 @@ public interface Request
Request version(HttpVersion version);

/**
* @return the query parameters of this request
* @return the URI query parameters of this request
*/
Fields getParams();

/**
* Adds a query parameter with the given name and value.
* Adds a URI query parameter with the given name and value.
* The value is {@link URLEncoder#encode(String, String) UTF-8 URL encoded}.
*
* @param name the name of the query parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ public void testIDNRedirect(Scenario scenario) throws Exception
}
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testHostPort(Scenario scenario) throws Exception
{
start(scenario, new EmptyServerHandler());

Request request = client.newRequest("domain.com", 80)
.scheme(scenario.getScheme())
.host("localhost")
.port(connector.getLocalPort())
.timeout(1, TimeUnit.SECONDS);

assertEquals("localhost", request.getHost());
assertEquals(connector.getLocalPort(), request.getPort());

ContentResponse response = request.send();

assertEquals(HttpStatus.OK_200, response.getStatus());
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testPath(Scenario scenario) throws Exception
Expand Down

0 comments on commit 8929791

Please sign in to comment.