Skip to content

Commit

Permalink
HttpClientBuilder to initialize public suffix matcher for cookie spec…
Browse files Browse the repository at this point in the history
…s and the default hostname verifier

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1623735 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ok2c committed Sep 9, 2014
1 parent a4c0c6e commit abe50fa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
package org.apache.http.conn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -39,18 +41,34 @@
import org.apache.http.util.Args;

/**
* {@link org.apache.http.conn.util.PublicSuffixMatcher} loader.
*
* @since 4.4
*/
@ThreadSafe
public final class PublicSuffixMatcherLoader {

private static PublicSuffixMatcher load(final InputStream in) throws IOException {
final PublicSuffixList list = new PublicSuffixListParser().parse(
new InputStreamReader(in, Consts.UTF_8));
return new PublicSuffixMatcher(list.getRules(), list.getExceptions());
}

public static PublicSuffixMatcher load(final URL url) throws IOException {
Args.notNull(url, "URL");
final InputStream in = url.openStream();
try {
final PublicSuffixList list = new PublicSuffixListParser().parse(
new InputStreamReader(in, Consts.UTF_8));
return new PublicSuffixMatcher(list.getRules(), list.getExceptions());
return load(in);
} finally {
in.close();
}
}

public static PublicSuffixMatcher load(final File file) throws IOException {
Args.notNull(file, "File");
final InputStream in = new FileInputStream(file);
try {
return load(in);
} finally {
in.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.NoConnectionReuseStrategy;
Expand Down Expand Up @@ -211,6 +214,8 @@ public class HttpClientBuilder {

private List<Closeable> closeables;

private PublicSuffixMatcher publicSuffixMatcher;

public static HttpClientBuilder create() {
return new HttpClientBuilder();
}
Expand Down Expand Up @@ -258,6 +263,20 @@ public final HttpClientBuilder setSSLHostnameVerifier(final HostnameVerifier hos
return this;
}

/**
* Assigns file containing public suffix matcher. Instances of this class can be created
* with {@link org.apache.http.conn.util.PublicSuffixMatcherLoader}.
*
* @see org.apache.http.conn.util.PublicSuffixMatcher
* @see org.apache.http.conn.util.PublicSuffixMatcherLoader
*
* @since 4.4
*/
public final HttpClientBuilder setPublicSuffixMatcher(final PublicSuffixMatcher publicSuffixMatcher) {
this.publicSuffixMatcher = publicSuffixMatcher;
return this;
}

/**
* Assigns {@link SSLContext} instance.
* <p>
Expand Down Expand Up @@ -803,6 +822,11 @@ private static String[] split(final String s) {
public CloseableHttpClient build() {
// Create main request executor
// We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
PublicSuffixMatcher publicSuffixMatcherCopy = this.publicSuffixMatcher;
if (publicSuffixMatcherCopy == null) {
publicSuffixMatcherCopy = PublicSuffixMatcherLoader.getDefault();
}

HttpRequestExecutor requestExecCopy = this.requestExec;
if (requestExecCopy == null) {
requestExecCopy = new HttpRequestExecutor();
Expand All @@ -817,7 +841,7 @@ public CloseableHttpClient build() {
System.getProperty("https.cipherSuites")) : null;
HostnameVerifier hostnameVerifierCopy = this.hostnameVerifier;
if (hostnameVerifierCopy == null) {
hostnameVerifierCopy = SSLConnectionSocketFactory.getDefaultHostnameVerifier();
hostnameVerifierCopy = new DefaultHostnameVerifier(publicSuffixMatcherCopy);
}
if (sslcontext != null) {
sslSocketFactoryCopy = new SSLConnectionSocketFactory(
Expand Down Expand Up @@ -1047,8 +1071,8 @@ public CloseableHttpClient build() {
Lookup<CookieSpecProvider> cookieSpecRegistryCopy = this.cookieSpecRegistry;
if (cookieSpecRegistryCopy == null) {
cookieSpecRegistryCopy = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider())
.register(CookieSpecs.STANDARD, new RFC2965SpecProvider())
.register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcherCopy))
.register(CookieSpecs.STANDARD, new RFC2965SpecProvider(publicSuffixMatcherCopy))
.register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider())
.register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public DefaultCookieSpecProvider(
this(compatibilityLevel, publicSuffixMatcher, null, false);
}

public DefaultCookieSpecProvider(final PublicSuffixMatcher publicSuffixMatcher) {
this(CompatibilityLevel.DEFAULT, publicSuffixMatcher, null, false);
}

public DefaultCookieSpecProvider() {
this(CompatibilityLevel.DEFAULT, null, null, false);
}
Expand Down

0 comments on commit abe50fa

Please sign in to comment.