public final class HttpClientConf extends Object implements Cloneable
Each config property can be set by the setter method, for example
conf.proxy("127.0.0.1", 8080);
The setters return `this` for method chaining; this class is best used in the builder pattern, for example
HttpClient client = new HttpClientConf() .proxy("127.0.0.1", 8080) .trafficDump(System.err::print) ... .newClient();
tunnels
sslContext
,
sslEngineConf
proxy
,
requestHeader
,
autoDecompress
,
autoRedirectMax
,
cookieStorage
,
etc.
trafficDump
keepAliveTimeout
, etc.
Constructor and Description |
---|
HttpClientConf()
Create an HttpClientConf with default values.
|
Instance Methods | |
---|---|
HttpClient |
newClient()
Create an HttpClient with this configuration.
|
HttpClientConf |
clone()
Return an independent copy of this object.
|
HttpClientConf |
selectorIds(int... selectorIds)
Ids of selectors for HttpClient.
|
HttpClientConf |
socketConf(ConsumerX<SocketChannel> action)
Action to configure each TCP socket.
|
HttpClientConf |
sslContext(SSLContext sslContext)
SSLContext for SSL connections.
|
HttpClientConf |
sslEngineConf(ConsumerX<SSLEngine> action)
Action to configure each SSLEngine.
|
HttpClientConf |
tunnels(TcpTunnel... tunnels)
Tcp tunnels for every HTTP connection.
|
HttpClientConf |
proxy(HttpProxy proxy)
Http proxy.
|
HttpClientConf |
proxy(String host,
int port)
Http proxy.
|
HttpClientConf |
requestHeader(String name,
String value)
An an additional header for every outgoing request.
|
HttpClientConf |
userPassSupplier(Function<TcpAddress,Async<UserPass>> userPassSupplier)
Username/password supplier for server authentications.
|
HttpClientConf |
userPass(String host,
String username,
String password)
Username/password for a single host.
|
HttpClientConf |
autoDecompress(boolean autoDecompress)
Whether to automatically decompress response bodies.
|
HttpClientConf |
autoRedirectMax(int autoRedirectMax)
Max number of redirects that will be followed.
|
HttpClientConf |
cookieStorage(CookieStorage cookieStorage)
Storage for cookies.
|
HttpClientConf |
keepAliveTimeout(Duration keepAliveTimeout)
Timeout for keep-alive connections.
|
HttpClientConf |
await100Timeout(Duration await100Timeout)
Timeout for awaiting "100 Continue" responses.
|
HttpClientConf |
responseHeadFieldMaxLength(int responseHeadFieldMaxLength)
Max length for any header value in a response.
|
HttpClientConf |
responseHeadTotalMaxLength(int responseHeadTotalMaxLength)
Max length of a response head.
|
HttpClientConf |
trafficDump(Consumer<CharSequence> trafficDump)
Where to dump http traffic, for debugging purpose.
|
int[] |
get_selectorIds() |
ConsumerX<SocketChannel> |
get_socketConf() |
SSLContext |
get_sslContext() |
ConsumerX<SSLEngine> |
get_sslEngineConf() |
TcpTunnel[] |
get_tunnels() |
HttpProxy |
get_proxy() |
HeaderMap |
get_requestHeaders() |
Function<TcpAddress,Async<UserPass>> |
get_userPassSupplier() |
boolean |
get_autoDecompress() |
int |
get_autoRedirectMax() |
CookieStorage |
get_cookieStorage() |
Duration |
get_keepAliveTimeout() |
Duration |
get_await100Timeout() |
int |
get_responseHeadFieldMaxLength() |
int |
get_responseHeadTotalMaxLength() |
Consumer<CharSequence> |
get_trafficDump() |
public HttpClientConf()
public HttpClient newClient()
Calling this method
`conf.newClient()`
is equivalent to calling
`new HttpClient(conf)`
.
public HttpClientConf clone()
public HttpClientConf selectorIds(int... selectorIds)
default: [0, 1, ... N-1] where N is the number of processors
Conceptually there are infinite number of selectors, each associated with a dedicated thread. A client may choose to use any one or several selectors. Different servers/clients can share selectors or use different selectors.
public HttpClientConf socketConf(ConsumerX<SocketChannel> action)
default action:
enable
TCP_NODELAY
App may want to configure more options on each socket, for example
conf.socketConf( socketChannel -> { socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); ... });
The SocketChannel is in non-blocking model; it must not be changed to blocking mode.
public HttpClientConf sslContext(SSLContext sslContext)
default: null
If null
, SSLContext.getDefault()
is used.
See JSSE Guide.
See also SslConf
for creating SSLContext.
For example, to trust all server certificates (including self-signed ones)
conf.sslContext( new SslConf().trustAll().createContext() )
public HttpClientConf sslEngineConf(ConsumerX<SSLEngine> action)
default action:
do nothing
Example:
conf.sslEngineConf( engine-> { engine.setEnabledCipherSuites(...); });
public HttpClientConf tunnels(TcpTunnel... tunnels)
default: none
A chain of tunnels can be specified; every HTTP connection will tunnel through all of them in the order specified
Example:
conf.tunnels( new Socks5Tunnel("localhost", 9876), new ConnectTunnel("some-proxy.com", 8080) );
Note that ConnectTunnel
is commonly known as "HTTPS proxy",
but in our API it is considered a tunnel instead of a proxy.
public HttpClientConf proxy(HttpProxy proxy)
default: null
If non-null, httpClient.send(request)
will forward the request to the proxy instead of the request host.
See also proxy(host, port)
Note that this has nothing to do with "HTTPS proxy";
see tunnels(...)
instead.
public HttpClientConf proxy(String host, int port)
public HttpClientConf requestHeader(String name, String value)
HttpClient maintains a map of additional headers.
send(request)
will add those headers to the request before sending it out.
If a header already exists in the request, it will not be affected by the additional headers.
The map of additional headers initially contains
Accept-Encoding: gzip User-Agent: bayou.io
Calling this method with value=null
will remove the header from that map.
Example usage:
new HttpClientConf() .requestHeader(Headers.User_Agent, null) .requestHeader(Headers.Cache_Control, "no-cache")
public HttpClientConf userPassSupplier(Function<TcpAddress,Async<UserPass>> userPassSupplier)
default: null (none)
During httpClient.send(request)
,
if a 401 Unauthorized response
is received with Basic/Digest challenges,
the `userPassSupplier` will be invoked with the server address to get the username/password;
the request will then be retried with the authentication information.
The userPassSupplier should return (UserPass)null
if no username/password is available for the server.
See also userPass(host, username, password)
public HttpClientConf userPass(String host, String username, String password)
This is a convenience method equivalent to
userPassSupplier
(address->
{
if(address.host().equalsIgnoreCase(host))
return Async.success( new UserPass(username, password) );
else
return Async.success(null);
});
(This method cannot be used repeatedly to specify username/passwords for multiple servers.)
public HttpClientConf autoDecompress(boolean autoDecompress)
default: true
During httpClient.send(request)
,
if the response received from the server is compressed by "Content-Encoding: gzip",
it will be automatically decompressed; the application sees a response entity with
contentEncoding
=null.
Note that "ETag" is not transformed by this process, which can cause problems.
Applications may want to set `autoDecompress(false)`
if they prefer to receive compressed response bodies.
GunzipByteSource
can be used for manual decompression.
public HttpClientConf autoRedirectMax(int autoRedirectMax)
default: 10
During httpClient.send(request)
,
redirect responses will be automatically followed, up to the limitation of `autoRedirectMax`.
If autoRedirectMax<=0, redirect responses will not be automatically followed.
public HttpClientConf cookieStorage(CookieStorage cookieStorage)
default:
CookieStorage.newInMemoryStorage()
httpClient.send(request)
will use this storage to load cookies for requests
and save cookies from responses.
If cookieStorage is `null`, HttpClient does not handle cookies; application can manage cookies in requests/responses by itself.
public HttpClientConf keepAliveTimeout(Duration keepAliveTimeout)
default: 15 seconds
HttpClient will try to cache and reuse connections, `keepAliveTimeout` specifies how long connections could be cached.
If `keepAliveTimeout` is null, 0, or negative, no connections will be cached; each connection is used for one request-response trip.
public HttpClientConf await100Timeout(Duration await100Timeout)
default: 1 second
If a request contains header "Expect: 100-continue", HttpClient will delay sending the request body until a "100 Continue" response is received, or `await100Timeout` is reached.
If `await100Timeout` is null, 0, or negative, request body will always be sent immediately.
public HttpClientConf responseHeadFieldMaxLength(int responseHeadFieldMaxLength)
default: 8*1024 (8KB)
See also responseHeadTotalMaxLength
.
public HttpClientConf responseHeadTotalMaxLength(int responseHeadTotalMaxLength)
default: 32*1024 (32KB)
public HttpClientConf trafficDump(Consumer<CharSequence> trafficDump)
default: null
If non-null, request/response heads will be dumped to it. This feature is mostly enabled during development time.
Example Usage:
trafficDump( System.out::print );
public int[] get_selectorIds()
public ConsumerX<SocketChannel> get_socketConf()
public SSLContext get_sslContext()
public TcpTunnel[] get_tunnels()
public HttpProxy get_proxy()
public HeaderMap get_requestHeaders()
public Function<TcpAddress,Async<UserPass>> get_userPassSupplier()
public boolean get_autoDecompress()
public int get_autoRedirectMax()
public CookieStorage get_cookieStorage()
public Duration get_keepAliveTimeout()
public Duration get_await100Timeout()
public int get_responseHeadFieldMaxLength()
public int get_responseHeadTotalMaxLength()
public Consumer<CharSequence> get_trafficDump()