Skip to content

Commit

Permalink
Fixes #5633 - Allow to configure HttpClient request authority.
Browse files Browse the repository at this point in the history
Introduced HttpDestination.send(Request, Response.CompleteListener) to send a request using the given destination.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Nov 11, 2020
1 parent 23f75a3 commit a308881
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,13 @@ protected Origin createOrigin(String scheme, String host, int port, Object tag)
return new Origin(scheme, host, port, tag);
}

protected HttpDestination resolveDestination(Origin origin)
/**
* <p>Returns, creating it if absent, the destination with the given origin.</p>
*
* @param origin the origin that identifies the destination
* @return the destination for the given origin
*/
public HttpDestination resolveDestination(Origin origin)
{
return destinations.computeIfAbsent(origin, o ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ protected void normalizeRequest(Request request)
if (version.getVersion() <= 11)
{
if (!headers.containsKey(HttpHeader.HOST.asString()))
headers.put(getHttpDestination().getHostField());
{
URI uri = request.getURI();
if (uri != null)
headers.put(HttpHeader.HOST, uri.getAuthority());
else
headers.put(getHttpDestination().getHostField());
}
}

// Add content headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,13 @@ public void failed(Throwable x)
abort(x);
}

public void send(Request request, Response.CompleteListener listener)
{
((HttpRequest)request).sendAsync(this, listener);
}

protected void send(HttpRequest request, List<Response.ResponseListener> listeners)
{
if (!getScheme().equalsIgnoreCase(request.getScheme()))
throw new IllegalArgumentException("Invalid request scheme " + request.getScheme() + " for destination " + this);
if (!getHost().equalsIgnoreCase(request.getHost()))
throw new IllegalArgumentException("Invalid request host " + request.getHost() + " for destination " + this);
int port = request.getPort();
if (port >= 0 && getPort() != port)
throw new IllegalArgumentException("Invalid request port " + port + " for destination " + this);
send(new HttpExchange(this, request, listeners));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.LongConsumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -722,7 +723,7 @@ public Request timeout(long timeout, TimeUnit unit)
public ContentResponse send() throws InterruptedException, TimeoutException, ExecutionException
{
FutureResponseListener listener = new FutureResponseListener(this);
send(this, listener);
send(listener);

try
{
Expand Down Expand Up @@ -761,15 +762,20 @@ public ContentResponse send() throws InterruptedException, TimeoutException, Exe
@Override
public void send(Response.CompleteListener listener)
{
send(this, listener);
sendAsync(client::send, listener);
}

private void send(HttpRequest request, Response.CompleteListener listener)
void sendAsync(HttpDestination destination, Response.CompleteListener listener)
{
sendAsync(destination::send, listener);
}

private void sendAsync(BiConsumer<HttpRequest, List<Response.ResponseListener>> sender, Response.CompleteListener listener)
{
if (listener != null)
responseListeners.add(listener);
sent();
client.send(request, responseListeners);
sender.accept(this, responseListeners);
}

void sent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.Origin;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Destination;
import org.eclipse.jetty.client.api.Response;
Expand All @@ -44,7 +46,9 @@
import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http2.FlowControlStrategy;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
Expand Down Expand Up @@ -733,6 +737,46 @@ protected void service(String target, Request jettyRequest, HttpServletRequest r
assertEquals(1, scenario.client.getDestinations().size());
}

@ParameterizedTest
@ArgumentsSource(TransportProvider.class)
public void testRequestWithDifferentDestination(Transport transport) throws Exception
{
init(transport);

String requestScheme = HttpScheme.HTTPS.is(scenario.getScheme()) ? "http" : "https";
String requestHost = "otherHost.com";
int requestPort = 8888;
scenario.start(new EmptyServerHandler()
{
@Override
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
HttpURI uri = jettyRequest.getHttpURI();
assertEquals(requestHost, uri.getHost());
assertEquals(requestPort, uri.getPort());
if (scenario.transport == Transport.H2C || scenario.transport == Transport.H2)
assertEquals(requestScheme, request.getScheme());
}
});

Origin origin = new Origin(scenario.getScheme(), "localhost", scenario.getNetworkConnectorLocalPortInt().get());
HttpDestination destination = scenario.client.resolveDestination(origin);

org.eclipse.jetty.client.api.Request request = scenario.client.newRequest(requestHost, requestPort)
.scheme(requestScheme)
.path("/path");

CountDownLatch resultLatch = new CountDownLatch(1);
destination.send(request, result ->
{
assertTrue(result.isSucceeded());
assertEquals(HttpStatus.OK_200, result.getResponse().getStatus());
resultLatch.countDown();
});

assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}

private void sleep(long time) throws IOException
{
try
Expand Down

0 comments on commit a308881

Please sign in to comment.