Skip to content

Commit

Permalink
Add missing configuration to streaming connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 4, 2023
1 parent 2aafd25 commit 281ff08
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ public HttpRequest request(URI uri, HttpMethod requestMethod, String body) {

@Override
public HttpStreamRequest streamRequest(URI uri) {
return new HttpStreamRequestImpl(uri, mStreamingHeaders, mSslSocketFactory);
return new HttpStreamRequestImpl(uri,
mStreamingHeaders,
mProxy,
mProxyAuthenticator,
mConnectionTimeout,
mDevelopmentSslConfig,
mSslSocketFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private HttpURLConnection setUpConnection(HttpMethod method) throws IOException
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory);
} else {
Logger.e("Failed to set SSL socket factory in stream request. Connection is not SSL");
Logger.e("Failed to set SSL socket factory.");
}
}

Expand All @@ -151,7 +151,7 @@ private HttpURLConnection setUpConnection(HttpMethod method) throws IOException
((HttpsURLConnection) connection).setSSLSocketFactory(mDevelopmentSslConfig.getSslSocketFactory());
((HttpsURLConnection) connection).setHostnameVerifier(mDevelopmentSslConfig.getHostnameVerifier());
} else {
Logger.e("Failed to set SSL socket factory in stream request. Connection is not SSL");
Logger.e("Failed to set SSL socket factory in stream request.");
}
} catch (Exception ex) {
Logger.e("Could not set development SSL config: " + ex.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
Expand All @@ -27,16 +28,30 @@ public class HttpStreamRequestImpl implements HttpStreamRequest {
private static final int STREAMING_READ_TIMEOUT_IN_MILLISECONDS = 80000;
private final URI mUri;
private final Map<String, String> mHeaders;
private final SSLSocketFactory mSslSocketFactory;
private HttpURLConnection mConnection;
private BufferedReader mResponseBufferedReader;
private InputStream mResponseInputStream;
@Nullable
private final Proxy mProxy;
@Nullable
private final SplitUrlConnectionAuthenticator mProxyAuthenticator;
private final long mConnectionTimeout;
private final DevelopmentSslConfig mDevelopmentSslConfig;
private final SSLSocketFactory mSslSocketFactory;

HttpStreamRequestImpl(@NonNull URI uri,
@NonNull Map<String, String> headers,
@Nullable SSLSocketFactory sslSocketFactory) {
@NonNull Map<String, String> headers,
@Nullable Proxy proxy,
@Nullable SplitUrlConnectionAuthenticator proxyAuthenticator,
long connectionTimeout,
@Nullable DevelopmentSslConfig developmentSslConfig,
@Nullable SSLSocketFactory sslSocketFactory) {
mUri = checkNotNull(uri);
mHeaders = new HashMap<>(checkNotNull(headers));
mProxy = proxy;
mProxyAuthenticator = proxyAuthenticator;
mConnectionTimeout = connectionTimeout;
mDevelopmentSslConfig = developmentSslConfig;
mSslSocketFactory = sslSocketFactory;
}

Expand Down Expand Up @@ -80,21 +95,48 @@ private HttpStreamResponse getRequest() throws HttpException {
HttpStreamResponse response;
try {
url = mUri.toURL();
mConnection = (HttpURLConnection) url.openConnection();
if (mProxy != null) {
mConnection = (HttpURLConnection) url.openConnection(mProxy);
if (mProxyAuthenticator != null) {
mConnection = mProxyAuthenticator.authenticate(mConnection);
}
} else {
mConnection = (HttpURLConnection) url.openConnection();
}

mConnection.setReadTimeout(STREAMING_READ_TIMEOUT_IN_MILLISECONDS);

addHeaders(mConnection);
if (mConnectionTimeout > 0) {
if (mConnectionTimeout > Integer.MAX_VALUE) {
mConnection.setReadTimeout(Integer.MAX_VALUE);
} else {
mConnection.setConnectTimeout((int) mConnectionTimeout);
}
}

if (mSslSocketFactory != null) {
Logger.d("Setting SSL socket factory in stream request");
if (mConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) mConnection).setSSLSocketFactory(mSslSocketFactory);
} else {
Logger.d("Failed to set SSL socket factory in stream request. Connection is not SSL");
Logger.e("Failed to set SSL socket factory.");
}
}

if (mDevelopmentSslConfig != null) {
try {
if (mConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) mConnection).setSSLSocketFactory(mDevelopmentSslConfig.getSslSocketFactory());
((HttpsURLConnection) mConnection).setHostnameVerifier(mDevelopmentSslConfig.getHostnameVerifier());
} else {
Logger.e("Failed to set SSL socket factory in stream request.");
}
} catch (Exception ex) {
Logger.e("Could not set development SSL config: " + ex.getLocalizedMessage());
}
}

addHeaders(mConnection, mHeaders);
response = buildResponse(mConnection);

} catch (MalformedURLException e) {
Expand All @@ -107,13 +149,13 @@ private HttpStreamResponse getRequest() throws HttpException {
return response;
}

private void addHeaders(HttpURLConnection request) {
if (mHeaders == null) {
return;
}
private static void addHeaders(HttpURLConnection request, Map<String, String> headers) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
if (entry == null) {
continue;
}

for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
request.setRequestProperty(entry.getKey(), entry.getValue());
request.addRequestProperty(entry.getKey(), entry.getValue());
}
}

Expand Down

0 comments on commit 281ff08

Please sign in to comment.