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:
parent
8ced17a930
commit
4c27395609
@ -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 {
|
||||
@ -154,7 +155,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
try {
|
||||
int status = executeMethod(head);
|
||||
Log_OC.d(TAG, "HEAD to " + path + " finished with HTTP status " + status +
|
||||
((status != HttpStatus.SC_OK)?"(FAIL)":""));
|
||||
((status != HttpStatus.SC_OK) ? "(FAIL)" : ""));
|
||||
exhaustResponse(head.getResponseBodyAsStream());
|
||||
return (status == HttpStatus.SC_OK);
|
||||
|
||||
@ -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,24 +200,25 @@ 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());
|
||||
|
||||
// logCookiesAtRequest(method.getRequestHeaders(), "before");
|
||||
// logCookiesAtState("before");
|
||||
//logCookiesAtRequest(method.getRequestHeaders(), "before");
|
||||
//logCookiesAtState("before");
|
||||
method.setFollowRedirects(false);
|
||||
|
||||
int status = super.executeMethod(method);
|
||||
@ -225,15 +227,37 @@ public class OwnCloudClient extends HttpClient {
|
||||
status = followRedirection(method).getLastStatus();
|
||||
}
|
||||
|
||||
// logCookiesAtRequest(method.getRequestHeaders(), "after");
|
||||
// logCookiesAtState("after");
|
||||
// logSetCookiesAtResponse(method.getResponseHeaders());
|
||||
//logCookiesAtRequest(method.getRequestHeaders(), "after");
|
||||
//logCookiesAtState("after");
|
||||
//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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,7 +267,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
int status = method.getStatusCode();
|
||||
RedirectionPath result = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT);
|
||||
while (redirectionsCount < MAX_REDIRECTIONS_COUNT &&
|
||||
( status == HttpStatus.SC_MOVED_PERMANENTLY ||
|
||||
(status == HttpStatus.SC_MOVED_PERMANENTLY ||
|
||||
status == HttpStatus.SC_MOVED_TEMPORARILY ||
|
||||
status == HttpStatus.SC_TEMPORARY_REDIRECT)
|
||||
) {
|
||||
@ -304,7 +328,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
public void exhaustResponse(InputStream responseBodyAsStream) {
|
||||
if (responseBodyAsStream != null) {
|
||||
try {
|
||||
while (responseBodyAsStream.read(sExhaustBuffer) >= 0);
|
||||
while (responseBodyAsStream.read(sExhaustBuffer) >= 0) ;
|
||||
responseBodyAsStream.close();
|
||||
|
||||
} catch (IOException io) {
|
||||
@ -337,7 +361,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
/**
|
||||
* Sets the root URI to the ownCloud server.
|
||||
*
|
||||
* <p>
|
||||
* Use with care.
|
||||
*
|
||||
* @param uri
|
||||
@ -367,7 +391,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
private void logCookiesAtRequest(Header[] headers, String when) {
|
||||
int counter = 0;
|
||||
for (int i=0; i<headers.length; i++) {
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
if (headers[i].getName().toLowerCase().equals("cookie")) {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber,
|
||||
"Cookies at request (" + when + ") (" + counter++ + "): " +
|
||||
@ -385,7 +409,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber, "No cookie at STATE before");
|
||||
} else {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber, "Cookies at STATE (before)");
|
||||
for (int i=0; i<cookies.length; i++) {
|
||||
for (int i = 0; i < cookies.length; i++) {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber, " (" + i + "):" +
|
||||
"\n name: " + cookies[i].getName() +
|
||||
"\n value: " + cookies[i].getValue() +
|
||||
@ -398,7 +422,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
private void logSetCookiesAtResponse(Header[] headers) {
|
||||
int counter = 0;
|
||||
for (int i=0; i<headers.length; i++) {
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
if (headers[i].getName().toLowerCase().equals("set-cookie")) {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber,
|
||||
"Set-Cookie (" + counter++ + "): " + headers[i].getValue());
|
||||
@ -407,7 +431,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
if (counter == 0) {
|
||||
Log_OC.d(TAG + " #" + mInstanceNumber, "No set-cookie");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getCookiesString() {
|
||||
@ -432,23 +455,23 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
|
||||
private void logCookie(Cookie cookie) {
|
||||
Log_OC.d(TAG, "Cookie name: "+ cookie.getName() );
|
||||
Log_OC.d(TAG, " value: "+ cookie.getValue() );
|
||||
Log_OC.d(TAG, " domain: "+ cookie.getDomain());
|
||||
Log_OC.d(TAG, " path: "+ cookie.getPath() );
|
||||
Log_OC.d(TAG, " version: "+ cookie.getVersion() );
|
||||
Log_OC.d(TAG, "Cookie name: " + cookie.getName());
|
||||
Log_OC.d(TAG, " value: " + cookie.getValue());
|
||||
Log_OC.d(TAG, " domain: " + cookie.getDomain());
|
||||
Log_OC.d(TAG, " path: " + cookie.getPath());
|
||||
Log_OC.d(TAG, " version: " + cookie.getVersion());
|
||||
Log_OC.d(TAG, " expiryDate: " +
|
||||
(cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
|
||||
Log_OC.d(TAG, " comment: "+ cookie.getComment() );
|
||||
Log_OC.d(TAG, " secure: "+ cookie.getSecure() );
|
||||
Log_OC.d(TAG, " comment: " + cookie.getComment());
|
||||
Log_OC.d(TAG, " secure: " + cookie.getSecure());
|
||||
}
|
||||
|
||||
|
||||
public void setOwnCloudVersion(OwnCloudVersion version){
|
||||
public void setOwnCloudVersion(OwnCloudVersion version) {
|
||||
mVersion = version;
|
||||
}
|
||||
|
||||
public OwnCloudVersion getOwnCloudVersion(){
|
||||
public OwnCloudVersion getOwnCloudVersion() {
|
||||
return mVersion;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user