Merge pull request #4791 from eclipse/jetty-9.4.x-4787-mutable_host_port_client_request

Fixes #4787 - Make org.eclipse.jetty.client.HttpRequest's host name w…
This commit is contained in:
Simone Bordet 2020-04-27 23:29:29 +02:00 committed by GitHub
commit 8929791e6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 13 deletions

View File

@ -69,12 +69,12 @@ public class HttpRequest implements Request
private final AtomicReference<Throwable> aborted = new AtomicReference<>(); private final AtomicReference<Throwable> aborted = new AtomicReference<>();
private final HttpClient client; private final HttpClient client;
private final HttpConversation conversation; private final HttpConversation conversation;
private final String host;
private final int port;
private URI uri;
private String scheme; private String scheme;
private String host;
private int port;
private String path; private String path;
private String query; private String query;
private URI uri;
private String method = HttpMethod.GET.asString(); private String method = HttpMethod.GET.asString();
private HttpVersion version = HttpVersion.HTTP_1_1; private HttpVersion version = HttpVersion.HTTP_1_1;
private long idleTimeout = -1; private long idleTimeout = -1;
@ -135,12 +135,28 @@ public class HttpRequest implements Request
return host; return host;
} }
@Override
public Request host(String host)
{
this.host = host;
this.uri = null;
return this;
}
@Override @Override
public int getPort() public int getPort()
{ {
return port; return port;
} }
@Override
public Request port(int port)
{
this.port = port;
this.uri = null;
return this;
}
@Override @Override
public String getMethod() public String getMethod()
{ {

View File

@ -51,26 +51,39 @@ import org.eclipse.jetty.util.Fields;
public interface Request 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(); 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 * @return this request object
*/ */
Request scheme(String scheme); 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(); 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(); 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 * @return the method of this request, such as GET or POST, as a String
*/ */
@ -89,26 +102,26 @@ public interface Request
Request method(String method); 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() * @see #getQuery()
*/ */
String getPath(); 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 * If the query part is specified, parameter values must be properly
* {@link URLEncoder#encode(String, String) UTF-8 URL encoded}. * {@link URLEncoder#encode(String, String) UTF-8 URL encoded}.
* For example, if the value for parameter "currency" is the euro symbol &euro; then the * 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". * query string for this parameter must be "currency=%E2%82%AC".
* For transparent encoding of parameter values, use {@link #param(String, String)}. * 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 * @return this request object
*/ */
Request path(String path); 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 #getPath()
* @see #getParams() * @see #getParams()
*/ */
@ -131,12 +144,12 @@ public interface Request
Request version(HttpVersion version); Request version(HttpVersion version);
/** /**
* @return the query parameters of this request * @return the URI query parameters of this request
*/ */
Fields getParams(); 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}. * The value is {@link URLEncoder#encode(String, String) UTF-8 URL encoded}.
* *
* @param name the name of the query parameter * @param name the name of the query parameter

View File

@ -131,6 +131,26 @@ public class HttpClientURITest extends AbstractHttpClientServerTest
} }
} }
@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 @ParameterizedTest
@ArgumentsSource(ScenarioProvider.class) @ArgumentsSource(ScenarioProvider.class)
public void testPath(Scenario scenario) throws Exception public void testPath(Scenario scenario) throws Exception