1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Fix crash with invalid TCP port

This commit is contained in:
David A. Velasco 2016-12-20 11:05:03 +01:00
parent 8ced17a930
commit 4c27395609

View File

@ -39,6 +39,7 @@ import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
@ -90,13 +91,14 @@ public class OwnCloudClient extends HttpClient {
getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
getParams().setParameter(
PARAM_PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpVersion.HTTP_1_1
);
getParams().setCookiePolicy(
CookiePolicy.IGNORE_COOKIES);
getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
getParams().setParameter(
PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
PARAM_SINGLE_COOKIE_HEADER_VALUE);
PARAM_SINGLE_COOKIE_HEADER_VALUE
);
applyProxySettings();
@ -143,10 +145,9 @@ public class OwnCloudClient extends HttpClient {
/**
* Check if a file exists in the OC server
*
* @deprecated Use ExistenceCheckOperation instead
*
* @return 'true' if the file exists; 'false' it doesn't exist
* @throws Exception When the existence could not be determined
* @deprecated Use ExistenceCheckOperation instead
*/
@Deprecated
public boolean existsFile(String path) throws IOException, HttpException {
@ -165,11 +166,11 @@ public class OwnCloudClient extends HttpClient {
/**
* Requests the received method with the received timeout (milliseconds).
*
* <p>
* Executes the method through the inherited HttpClient.executedMethod(method).
*
* <p>
* Sets the socket and connection timeouts only for the method received.
*
* <p>
* The timeouts are both in milliseconds; 0 means 'infinite';
* < 0 means 'do not change the default'
*
@ -199,19 +200,20 @@ public class OwnCloudClient extends HttpClient {
/**
* Requests the received method.
*
* <p>
* Executes the method through the inherited HttpClient.executedMethod(method).
*
* @param method HTTP method request.
*/
@Override
public int executeMethod(HttpMethod method) throws IOException {
try {
// Update User Agent
HttpParams params = method.getParams();
String userAgent = OwnCloudClientManagerFactory.getUserAgent();
params.setParameter(HttpMethodParams.USER_AGENT, userAgent);
preventCrashDueToInvalidPort(method);
Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " +
method.getName() + " " + method.getPath());
@ -230,10 +232,32 @@ public class OwnCloudClient extends HttpClient {
//logSetCookiesAtResponse(method.getResponseHeaders());
return status;
}
} catch (IOException e) {
//Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e);
throw e;
/**
* Fix for https://github.com/owncloud/android/issues/1847#issuecomment-267558274
*
* The problem: default SocketFactory in HTTPClient 3.x for HTTP connections creates a separate thread
* to create the socket. When a port out of TCP bounds is passed, an exception is thrown in that
* separate thread, and our original thread is not able to catch it. This is not happenning with HTTPS
* connections because we had to define our own socket factory,
* {@link com.owncloud.android.lib.common.network.AdvancedSslSocketFactory}, and it does not mess with
* threads.
*
* The solution: validate the input (the port number) ourselves before let the work to HTTPClient 3.x.
*
* @param method HTTP method to run.
* @throws IllegalArgumentException If 'method' targets an invalid port in an HTTP URI.
* @throws URIException If the URI to the target server cannot be built.
*/
private void preventCrashDueToInvalidPort(HttpMethod method) throws URIException {
int port = method.getURI().getPort();
String scheme = method.getURI().getScheme().toLowerCase();
if ("http".equals(scheme) && port > 0xFFFF) {
// < 0 is not tested because -1 is used when no port number is specified in the URL;
// no problem, the network library will convert that in the default HTTP port
throw new IllegalArgumentException("Invalid port number " + port);
}
}
@ -337,7 +361,7 @@ public class OwnCloudClient extends HttpClient {
/**
* Sets the root URI to the ownCloud server.
*
* <p>
* Use with care.
*
* @param uri
@ -407,7 +431,6 @@ public class OwnCloudClient extends HttpClient {
if (counter == 0) {
Log_OC.d(TAG + " #" + mInstanceNumber, "No set-cookie");
}
}
public String getCookiesString() {