diff options
author | Dan Morrill <morrildl@sententio.us> | 2013-03-31 19:35:17 -0700 |
---|---|---|
committer | Dan Morrill <morrildl@sententio.us> | 2013-03-31 20:02:48 -0700 |
commit | 3f081a83d5ed92d4e6ec4abf87137cb246d92d0c (patch) | |
tree | 36a023ec36c611640ad579288b4270f907183d13 /src | |
parent | 33e2590d2b88b85d422cdd39a24a39c71e6f2f6e (diff) | |
download | volley-3f081a83d5ed92d4e6ec4abf87137cb246d92d0c.tar.gz |
Fix a bug in POSTs & support SslSocketFactories.
For POSTs and PUTs, HttpURLConnection requires that you set the method before
you write any body data. i.e. writing body data causes the socket to be
opened, at which point you can't set the method anymore. Volley was calling
these two methods in the wrong order; this fixes that.
Separately, this also adds a constructor and appropriate support for a
caller-provided SslSocketFactory. The use case for this is self-signed certs,
where a developer's app is is preloaded with the public key for a specific
server cert not signed by one of the usual CAs. The caller is responsible for
setting up the socket factory with an appropriate keystore, etc.
Change-Id: I2738ce956202b8a8508a733fc7f8b6600cd19bf5
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/volley/toolbox/HurlStack.java | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/com/android/volley/toolbox/HurlStack.java b/src/com/android/volley/toolbox/HurlStack.java index d669166..bbfb12a 100644 --- a/src/com/android/volley/toolbox/HurlStack.java +++ b/src/com/android/volley/toolbox/HurlStack.java @@ -40,6 +40,9 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSocketFactory; + /** * An {@link HttpStack} based on {@link HttpURLConnection}. */ @@ -59,6 +62,7 @@ public class HurlStack implements HttpStack { } private final UrlRewriter mUrlRewriter; + private final SSLSocketFactory mSslSocketFactory; public HurlStack() { this(null); @@ -68,7 +72,16 @@ public class HurlStack implements HttpStack { * @param urlRewriter Rewriter to use for request URLs */ public HurlStack(UrlRewriter urlRewriter) { + this(urlRewriter, null); + } + + /** + * @param urlRewriter Rewriter to use for request URLs + * @param sslSocketFactory SSL factory to use for HTTPS connections + */ + public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) { mUrlRewriter = urlRewriter; + mSslSocketFactory = sslSocketFactory; } @Override @@ -146,6 +159,12 @@ public class HurlStack implements HttpStack { connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); + + // use caller-provided custom SslSocketFactory, if any, for HTTPS + if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { + ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory); + } + return connection; } @@ -180,12 +199,12 @@ public class HurlStack implements HttpStack { connection.setRequestMethod("DELETE"); break; case Method.POST: - addBodyIfExists(connection, request); connection.setRequestMethod("POST"); + addBodyIfExists(connection, request); break; case Method.PUT: - addBodyIfExists(connection, request); connection.setRequestMethod("PUT"); + addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); |