1
+ package com .loopj .android .http ;
2
+
3
+ import org .apache .http .Header ;
4
+ import org .apache .http .HttpHost ;
5
+ import org .apache .http .HttpRequest ;
6
+ import org .apache .http .HttpResponse ;
7
+ import org .apache .http .HttpStatus ;
8
+ import org .apache .http .ProtocolException ;
9
+ import org .apache .http .client .CircularRedirectException ;
10
+ import org .apache .http .client .params .ClientPNames ;
11
+ import org .apache .http .client .utils .URIUtils ;
12
+ import org .apache .http .impl .client .DefaultRedirectHandler ;
13
+ import org .apache .http .impl .client .RedirectLocations ;
14
+ import org .apache .http .params .HttpParams ;
15
+ import org .apache .http .protocol .ExecutionContext ;
16
+ import org .apache .http .protocol .HttpContext ;
17
+
18
+ import java .net .URI ;
19
+ import java .net .URISyntaxException ;
20
+
21
+ // taken from: https://stackoverflow.com/questions/3420767/httpclient-redirecting-to-url-with-spaces-throwing-exception
22
+ class MyRedirectHandler extends DefaultRedirectHandler {
23
+
24
+ private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations" ;
25
+ private final boolean enableRedirects ;
26
+
27
+ public MyRedirectHandler (final boolean allowRedirects ) {
28
+ super ();
29
+ this .enableRedirects = allowRedirects ;
30
+ }
31
+
32
+ public boolean isRedirectRequested (
33
+ final HttpResponse response ,
34
+ final HttpContext context ) {
35
+ if (!enableRedirects ) {
36
+ return false ;
37
+ }
38
+ if (response == null ) {
39
+ throw new IllegalArgumentException ("HTTP response may not be null" );
40
+ }
41
+ int statusCode = response .getStatusLine ().getStatusCode ();
42
+ switch (statusCode ) {
43
+ case HttpStatus .SC_MOVED_TEMPORARILY :
44
+ case HttpStatus .SC_MOVED_PERMANENTLY :
45
+ case HttpStatus .SC_SEE_OTHER :
46
+ case HttpStatus .SC_TEMPORARY_REDIRECT :
47
+ return true ;
48
+ default :
49
+ return false ;
50
+ } //end of switch
51
+ }
52
+
53
+ public URI getLocationURI (
54
+ final HttpResponse response ,
55
+ final HttpContext context ) throws ProtocolException {
56
+ if (response == null ) {
57
+ throw new IllegalArgumentException ("HTTP response may not be null" );
58
+ }
59
+ //get the location header to find out where to redirect to
60
+ Header locationHeader = response .getFirstHeader ("location" );
61
+ if (locationHeader == null ) {
62
+ // got a redirect response, but no location header
63
+ throw new ProtocolException (
64
+ "Received redirect response " + response .getStatusLine ()
65
+ + " but no location header"
66
+ );
67
+ }
68
+ //HERE IS THE MODIFIED LINE OF CODE
69
+ String location = locationHeader .getValue ().replaceAll (" " , "%20" );
70
+
71
+ URI uri ;
72
+ try {
73
+ uri = new URI (location );
74
+ } catch (URISyntaxException ex ) {
75
+ throw new ProtocolException ("Invalid redirect URI: " + location , ex );
76
+ }
77
+
78
+ HttpParams params = response .getParams ();
79
+ // rfc2616 demands the location value be a complete URI
80
+ // Location = "Location" ":" absoluteURI
81
+ if (!uri .isAbsolute ()) {
82
+ if (params .isParameterTrue (ClientPNames .REJECT_RELATIVE_REDIRECT )) {
83
+ throw new ProtocolException ("Relative redirect location '"
84
+ + uri + "' not allowed" );
85
+ }
86
+ // Adjust location URI
87
+ HttpHost target = (HttpHost ) context .getAttribute (
88
+ ExecutionContext .HTTP_TARGET_HOST );
89
+ if (target == null ) {
90
+ throw new IllegalStateException ("Target host not available " +
91
+ "in the HTTP context" );
92
+ }
93
+
94
+ HttpRequest request = (HttpRequest ) context .getAttribute (
95
+ ExecutionContext .HTTP_REQUEST );
96
+
97
+ try {
98
+ URI requestURI = new URI (request .getRequestLine ().getUri ());
99
+ URI absoluteRequestURI = URIUtils .rewriteURI (requestURI , target , true );
100
+ uri = URIUtils .resolve (absoluteRequestURI , uri );
101
+ } catch (URISyntaxException ex ) {
102
+ throw new ProtocolException (ex .getMessage (), ex );
103
+ }
104
+ }
105
+
106
+ if (params .isParameterFalse (ClientPNames .ALLOW_CIRCULAR_REDIRECTS )) {
107
+
108
+ RedirectLocations redirectLocations = (RedirectLocations ) context .getAttribute (
109
+ REDIRECT_LOCATIONS );
110
+
111
+ if (redirectLocations == null ) {
112
+ redirectLocations = new RedirectLocations ();
113
+ context .setAttribute (REDIRECT_LOCATIONS , redirectLocations );
114
+ }
115
+
116
+ URI redirectURI ;
117
+ if (uri .getFragment () != null ) {
118
+ try {
119
+ HttpHost target = new HttpHost (
120
+ uri .getHost (),
121
+ uri .getPort (),
122
+ uri .getScheme ());
123
+ redirectURI = URIUtils .rewriteURI (uri , target , true );
124
+ } catch (URISyntaxException ex ) {
125
+ throw new ProtocolException (ex .getMessage (), ex );
126
+ }
127
+ } else {
128
+ redirectURI = uri ;
129
+ }
130
+
131
+ if (redirectLocations .contains (redirectURI )) {
132
+ throw new CircularRedirectException ("Circular redirect to '" +
133
+ redirectURI + "'" );
134
+ } else {
135
+ redirectLocations .add (redirectURI );
136
+ }
137
+ }
138
+
139
+ return uri ;
140
+ }
141
+ }
0 commit comments