Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6_1_X][TIMOB-24748] Android: HTTPClient - can't use streaming mode with authentication #9119

Merged
merged 2 commits into from Jun 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,7 +18,6 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -50,6 +49,7 @@
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;

import android.util.Base64;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
Expand Down Expand Up @@ -103,7 +103,7 @@ public class TiHTTPClient

private static AtomicInteger httpClientThreadCounter;
private HttpURLConnection client;
private KrollProxy proxy;
private HTTPClientProxy proxy;
private int readyState;
private String responseText;
private DocumentProxy responseXml;
Expand Down Expand Up @@ -151,6 +151,9 @@ public class TiHTTPClient
public static final int REDIRECTS = 5;

private TiFile responseFile;
private boolean hasAuthentication = false;
private String username;
private String password;

private void handleResponse(HttpURLConnection connection) throws IOException {
connected = true;
Expand Down Expand Up @@ -397,7 +400,7 @@ public void write(int b) throws IOException
}
}

public TiHTTPClient(KrollProxy proxy)
public TiHTTPClient(HTTPClientProxy proxy)
{
this.proxy = proxy;
if (httpClientThreadCounter == null) {
Expand Down Expand Up @@ -443,7 +446,7 @@ public void addAuthFactory(String scheme, AuthSchemeFactory theFactory)

public void setReadyState(int readyState)
{
Log.d(TAG, "Setting ready state to " + readyState, Log.DEBUG_MODE);
Log.d(TAG, "Setting ready state to " + readyState);
this.readyState = readyState;
KrollDict data = new KrollDict();
data.put("readyState", Integer.valueOf(readyState));
Expand Down Expand Up @@ -678,9 +681,9 @@ public void setRequestHeader(String header, String value)
if (requestHeaders.containsKey(header)){
// Appends a value to a header
// If it is a cookie, use ';'. If not, use ','.
String seperator = ("Cookie".equalsIgnoreCase(header))? "; " : ", ";
String separator = ("Cookie".equalsIgnoreCase(header))? "; " : ", ";
StringBuffer val = new StringBuffer(requestHeaders.get(header));
val.append(seperator+value);
val.append(separator+value);
requestHeaders.put(header, val.toString());
} else {
// Set header for the first time
Expand Down Expand Up @@ -800,12 +803,11 @@ public void open(String method, String url)
"Instantiating host with hostString='" + hostString + "', port='" + port + "', scheme='" + uri.getScheme() + "'",
Log.DEBUG_MODE);

final String username = ((HTTPClientProxy)proxy).getUsername();
final String password = ((HTTPClientProxy)proxy).getPassword();
final String domain = ((HTTPClientProxy)proxy).getDomain();
username = proxy.getUsername();
password = proxy.getPassword();

if ((username != null) && (password != null)) {
Authenticator.setDefault(new TiAuthenticator(domain, username, password));
hasAuthentication = true;
}

setReadyState(READY_STATE_OPENED);
Expand Down Expand Up @@ -915,8 +917,8 @@ private void setUpSSL(boolean validating, HttpsURLConnection securedConnection)

if (this.securityManager != null) {
if (this.securityManager.willHandleURL(this.uri)) {
TrustManager[] trustManagerArray = this.securityManager.getTrustManagers((HTTPClientProxy)this.proxy);
KeyManager[] keyManagerArray = this.securityManager.getKeyManagers((HTTPClientProxy)this.proxy);
TrustManager[] trustManagerArray = this.securityManager.getTrustManagers(this.proxy);
KeyManager[] keyManagerArray = this.securityManager.getKeyManagers(this.proxy);

try {
sslSocketFactory = new TiSocketFactory(keyManagerArray, trustManagerArray, tlsVersion);
Expand Down Expand Up @@ -1327,6 +1329,15 @@ protected void setUpClient(HttpURLConnection client, Boolean isPostOrPutOrPatch)
client.setDoOutput(true);
}
client.setUseCaches(false);
//Set Authorization value for Basic authentication
if (hasAuthentication) {
String domain = proxy.getDomain();
if (domain != null) {
username = domain + "\\" + username;
}
String encodedCredentials = Base64.encodeToString((username + ":" + password).getBytes(),Base64.DEFAULT);
client.setRequestProperty("Authorization", "Basic " + encodedCredentials);
}
// This is to set gzip default to disable
// https://code.google.com/p/android/issues/detail?id=174949
client.setRequestProperty("Accept-Encoding", "identity");
Expand Down