2525
2626package com .owncloud .android .lib .common ;
2727
28- import android .accounts .Account ;
2928import android .accounts .AccountManager ;
3029import android .accounts .AccountsException ;
3130import android .content .Context ;
3736import com .owncloud .android .lib .common .http .HttpClient ;
3837import com .owncloud .android .lib .common .http .HttpConstants ;
3938import com .owncloud .android .lib .common .http .methods .HttpBaseMethod ;
40- import com .owncloud .android .lib .common .http .methods .nonwebdav .HttpMethod ;
4139import com .owncloud .android .lib .common .network .RedirectionPath ;
4240import com .owncloud .android .lib .common .utils .Log_OC ;
4341import com .owncloud .android .lib .resources .status .OwnCloudVersion ;
4442
4543
4644import java .io .IOException ;
4745import java .io .InputStream ;
48- import java .util .ArrayList ;
4946import java .util .List ;
5047
48+ import at .bitfire .dav4android .exception .HttpException ;
5149import okhttp3 .Cookie ;
5250import okhttp3 .Headers ;
5351import okhttp3 .HttpUrl ;
@@ -136,9 +134,16 @@ public int executeHttpMethod (HttpBaseMethod method) throws Exception {
136134 int status ;
137135
138136 do {
139- //TODO Dav4Android doesn't allow follow redirections right now
140- // method.setFollowRedirects(mFollowRedirects);
141- status = method .execute ();
137+ try {
138+ status = method .execute ();
139+ } catch (HttpException e ) {
140+ if (e .getMessage ().contains (Integer .toString (HttpConstants .HTTP_MOVED_TEMPORARILY ))) {
141+ status = followRedirection (method ).getLastStatus ();
142+ } else {
143+ throw e ;
144+ }
145+ }
146+
142147 repeatWithFreshCredentials = checkUnauthorizedAccess (status , repeatCounter );
143148 if (repeatWithFreshCredentials ) {
144149 repeatCounter ++;
@@ -148,41 +153,26 @@ public int executeHttpMethod (HttpBaseMethod method) throws Exception {
148153 return status ;
149154 }
150155
151- private void checkFirstRedirection (HttpMethod method ) {
152- final String location = method .getResponseHeaders ()
153- .get ("location" );
156+ private int executeRedirectedHttpMethod (HttpBaseMethod method ) throws Exception {
157+ boolean repeatWithFreshCredentials ;
158+ int repeatCounter = 0 ;
159+ int status ;
154160
155- if (location != null && !location .isEmpty ()) {
156- mRedirectedLocation = location ;
157- }
158- }
161+ do {
162+ status = method .execute ();
159163
160- /**
161- * Fix for https://github.com/owncloud/android/issues/1847#issuecomment-267558274
162- *
163- * The problem: default SocketFactory in HTTPClient 3.x for HTTP connections creates a separate thread
164- * to create the socket. When a port out of TCP bounds is passed, an exception is thrown in that
165- * separate thread, and our original thread is not able to catch it. This is not happenning with HTTPS
166- * connections because we had to define our own socket factory,
167- * {@link com.owncloud.android.lib.common.network.AdvancedSslSocketFactory}, and it does not mess with
168- * threads.
169- *
170- * The solution: validate the input (the port number) ourselves before let the work to HTTPClient 3.x.
171- *
172- * @param method HTTP method to run.
173- * @throws IllegalArgumentException If 'method' targets an invalid port in an HTTP URI.
174- */
175- private void preventCrashDueToInvalidPort (HttpMethod method ) {
176- final int port = method .getUrl ().port ();
177- String scheme = method .getUrl ().scheme ().toLowerCase ();
178- if ("http" .equals (scheme ) && port > 0xFFFF ) {
179- // < 0 is not tested because -1 is used when no port number is specified in the URL;
180- // no problem, the network library will convert that in the default HTTP port
181- throw new IllegalArgumentException ("Invalid port number " + port );
182- }
164+ repeatWithFreshCredentials = checkUnauthorizedAccess (status , repeatCounter );
165+ if (repeatWithFreshCredentials ) {
166+ repeatCounter ++;
167+ }
168+ } while (repeatWithFreshCredentials );
169+
170+ return status ;
183171 }
184172
185- public RedirectionPath followRedirection (HttpMethod method ) throws Exception {
173+
174+
175+ public RedirectionPath followRedirection (HttpBaseMethod method ) throws Exception {
186176 int redirectionsCount = 0 ;
187177 int status = method .getStatusCode ();
188178 RedirectionPath result = new RedirectionPath (status , MAX_REDIRECTIONS_COUNT );
@@ -215,13 +205,19 @@ public RedirectionPath followRedirection(HttpMethod method) throws Exception {
215205 if (destination != null ) {
216206 final int suffixIndex = location .lastIndexOf (WEBDAV_PATH_4_0 );
217207 final String redirectionBase = location .substring (0 , suffixIndex );
218-
219208 final String destinationPath = destination .substring (mBaseUri .toString ().length ());
220- final String redirectedDestination = redirectionBase + destinationPath ;
221209
222- method .setRequestHeader ("destination" , destination );
210+ method .setRequestHeader ("destination" , redirectionBase + destinationPath );
211+ }
212+ try {
213+ status = executeRedirectedHttpMethod (method );
214+ } catch (HttpException e ) {
215+ if (e .getMessage ().contains (Integer .toString (HttpConstants .HTTP_MOVED_TEMPORARILY ))) {
216+ status = HttpConstants .HTTP_MOVED_TEMPORARILY ;
217+ } else {
218+ throw e ;
219+ }
223220 }
224- status = executeHttpMethod (method );
225221 result .addStatus (status );
226222 redirectionsCount ++;
227223
0 commit comments