Skip to content

Commit

Permalink
Support InetAddress throught request execution
Browse files Browse the repository at this point in the history
  • Loading branch information
bpitman committed Aug 25, 2023
1 parent b209e7b commit 9f75260
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 8 deletions.
17 changes: 17 additions & 0 deletions httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;

Expand Down Expand Up @@ -94,6 +95,22 @@ public interface HttpRequest extends HttpMessage {
*/
void setAuthority(URIAuthority authority);

/**
* Returns address of this request message.
*
* @return the address or {@code null}.
*
* @since 5.3
*/
InetAddress getAddress();

/**
* Sets address of this request message.
*
* @since 5.3
*/
void setAddress(InetAddress address);

/**
* Returns request URI of this request message. It may be an absolute or relative URI.
* Applicable to HTTP/1.1 version or earlier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http.impl;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.apache.hc.core5.function.Resolver;
Expand Down Expand Up @@ -56,7 +57,10 @@ public InetSocketAddress resolve(final HttpHost host) {
port = 443;
}
}
return new InetSocketAddress(host.getHostName(), port);
final InetAddress address = host.getAddress();
return (address == null)
? new InetSocketAddress(host.getHostName(), port)
: new InetSocketAddress(address, port);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.apache.hc.core5.http.impl.bootstrap;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -289,7 +290,10 @@ public void execute(
if (authority == null) {
throw new ProtocolException("Request authority not specified");
}
final HttpHost target = new HttpHost(scheme, authority);
final InetAddress address = request.getAddress();
final HttpHost target = (address == null)
? new HttpHost(scheme, authority)
: new HttpHost(scheme, address, authority.getPort());
connect(target, timeout, null, new FutureCallback<AsyncClientEndpoint>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http.io.support;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -237,6 +238,15 @@ public ClassicRequestBuilder setAuthority(final URIAuthority authority) {
return this;
}

/**
* @since 5.3
*/
@Override
public ClassicRequestBuilder setAddress(final InetAddress address) {
super.setAddress(address);
return this;
}

/**
* @since 5.1
*/
Expand Down Expand Up @@ -377,7 +387,7 @@ public ClassicHttpRequest build() {
throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
}

final BasicClassicHttpRequest result = new BasicClassicHttpRequest(method, getScheme(), getAuthority(), path);
final BasicClassicHttpRequest result = new BasicClassicHttpRequest(method, getScheme(), getAuthority(), getAddress(), path);
result.setVersion(getVersion());
result.setHeaders(getHeaders());
result.setEntity(entityCopy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.net.URIAuthority;

import java.net.InetAddress;
import java.net.URI;

/**
Expand All @@ -46,6 +47,22 @@ public class BasicClassicHttpRequest extends BasicHttpRequest implements Classic

private HttpEntity entity;

/**
* Creates request message with the given method, host and request path.
*
* @param method request method.
* @param scheme request scheme.
* @param authority request authority.
* @param address request address.
* @param path request path.
*
* @since 5.3
*/
public BasicClassicHttpRequest(final String method, final String scheme, final URIAuthority authority, final InetAddress address, final String path) {
super(method, scheme, authority, address, path);
}


/**
* Creates request message with the given method, host and request path.
*
Expand All @@ -57,7 +74,7 @@ public class BasicClassicHttpRequest extends BasicHttpRequest implements Classic
* @since 5.1
*/
public BasicClassicHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
super(method, scheme, authority, path);
this(method, scheme, authority, null, path);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http.message;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -52,6 +53,7 @@ public class BasicHttpRequest extends HeaderGroup implements HttpRequest {
private String path;
private String scheme;
private URIAuthority authority;
private InetAddress address;
private ProtocolVersion version;
private URI requestUri;
private boolean absoluteRequestUri;
Expand All @@ -62,18 +64,34 @@ public class BasicHttpRequest extends HeaderGroup implements HttpRequest {
* @param method request method.
* @param scheme request scheme.
* @param authority request authority.
* @param address request address.
* @param path request path.
*
* @since 5.1
* @since 5.3
*/
public BasicHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
public BasicHttpRequest(final String method, final String scheme, final URIAuthority authority, final InetAddress address, final String path) {
super();
this.method = Args.notNull(method, "Method name");
this.scheme = scheme;
this.authority = authority;
this.address = address;
this.path = path;
}

/**
* Creates request message with the given method, host and request path.
*
* @param method request method.
* @param scheme request scheme.
* @param authority request authority.
* @param path request path.
*
* @since 5.1
*/
public BasicHttpRequest(final String method, final String scheme, final URIAuthority authority, final String path) {
this(method, scheme, authority, null, path);
}

/**
* Creates request message with the given method and request path.
*
Expand Down Expand Up @@ -106,6 +124,7 @@ public BasicHttpRequest(final String method, final HttpHost host, final String p
this.method = Args.notNull(method, "Method name");
this.scheme = host != null ? host.getSchemeName() : null;
this.authority = host != null ? new URIAuthority(host) : null;
this.address = host != null ? host.getAddress() : null;
this.path = path;
}

Expand Down Expand Up @@ -157,6 +176,7 @@ public BasicHttpRequest(final Method method, final HttpHost host, final String p
this.method = Args.notNull(method, "Method").name();
this.scheme = host != null ? host.getSchemeName() : null;
this.authority = host != null ? new URIAuthority(host) : null;
this.address = host != null ? host.getAddress() : null;
this.path = path;
}

Expand Down Expand Up @@ -237,6 +257,22 @@ public void setAuthority(final URIAuthority authority) {
this.requestUri = null;
}

/**
* @since 5.3
*/
@Override
public InetAddress getAddress() {
return this.address;
}

/**
* @since 5.3
*/
@Override
public void setAddress(final InetAddress address) {
this.address = address;
}

/**
* Sets a flag that the {@link #getRequestUri()} method should return the request URI
* in an absolute form.
Expand Down Expand Up @@ -275,6 +311,7 @@ public void setUri(final URI requestUri) {
} else {
this.authority = null;
}
this.address = null; // resolve this later
final StringBuilder buf = new StringBuilder();
final String rawPath = requestUri.getRawPath();
if (!TextUtils.isBlank(rawPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http.message;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;

Expand Down Expand Up @@ -79,6 +80,22 @@ public void setAuthority(final URIAuthority authority) {
getMessage().setAuthority(authority);
}

/**
* @since 5.3
*/
@Override
public InetAddress getAddress() {
return getMessage().getAddress();
}

/**
* @since 5.3
*/
@Override
public void setAddress(final InetAddress address) {
getMessage().setAddress(address);
}

@Override
public String getRequestUri() {
return getMessage().getRequestUri();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public AsyncRequestProducer build() {
throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
}

final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), path);
final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), getAddress(), path);
request.setVersion(getVersion());
request.setHeaders(getHeaders());
request.setAbsoluteRequestUri(isAbsoluteRequestUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.http.support;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -56,6 +57,7 @@ public abstract class AbstractRequestBuilder<T> extends AbstractMessageBuilder<T
final private String method;
private String scheme;
private URIAuthority authority;
private InetAddress address;
private String path;
private Charset charset;
private List<NameValuePair> parameters;
Expand Down Expand Up @@ -94,6 +96,7 @@ protected void digest(final HttpRequest request) {
}
setScheme(request.getScheme());
setAuthority(request.getAuthority());
setAddress(request.getAddress());
setPath(request.getPath());
this.parameters = null;
super.digest(request);
Expand Down Expand Up @@ -127,11 +130,27 @@ public AbstractRequestBuilder<T> setAuthority(final URIAuthority authority) {
return this;
}

/**
* @since 5.3
*/
public InetAddress getAddress() {
return address;
}

/**
* @since 5.3
*/
public AbstractRequestBuilder<T> setAddress(final InetAddress address) {
this.address = address;
return this;
}

public AbstractRequestBuilder<T> setHttpHost(final HttpHost httpHost) {
if (httpHost == null) {
return this;
}
this.authority = new URIAuthority(httpHost);
this.address = httpHost.getAddress();
this.scheme = httpHost.getSchemeName();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public BasicHttpRequest build() {
// should never happen
}
}
final BasicHttpRequest result = new BasicHttpRequest(getMethod(), getScheme(), getAuthority(), path);
final BasicHttpRequest result = new BasicHttpRequest(getMethod(), getScheme(), getAuthority(), getAddress(), path);
result.setVersion(getVersion());
result.setHeaders(getHeaders());
result.setAbsoluteRequestUri(isAbsoluteRequestUri());
Expand Down

0 comments on commit 9f75260

Please sign in to comment.