1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.impl.client;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32
33 import org.apache.http.annotation.Immutable;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.http.Header;
38 import org.apache.http.HttpHost;
39 import org.apache.http.HttpRequest;
40 import org.apache.http.HttpResponse;
41 import org.apache.http.HttpStatus;
42 import org.apache.http.ProtocolException;
43 import org.apache.http.client.CircularRedirectException;
44 import org.apache.http.client.RedirectHandler;
45 import org.apache.http.client.methods.HttpGet;
46 import org.apache.http.client.methods.HttpHead;
47 import org.apache.http.client.params.ClientPNames;
48 import org.apache.http.client.utils.URIUtils;
49 import org.apache.http.params.HttpParams;
50 import org.apache.http.protocol.HttpContext;
51 import org.apache.http.protocol.ExecutionContext;
52
53
54
55
56
57
58
59
60 @Immutable
61 @Deprecated
62 public class DefaultRedirectHandler implements RedirectHandler {
63
64 private final Log log = LogFactory.getLog(getClass());
65
66 private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
67
68 public DefaultRedirectHandler() {
69 super();
70 }
71
72 public boolean isRedirectRequested(
73 final HttpResponse response,
74 final HttpContext context) {
75 if (response == null) {
76 throw new IllegalArgumentException("HTTP response may not be null");
77 }
78
79 int statusCode = response.getStatusLine().getStatusCode();
80 switch (statusCode) {
81 case HttpStatus.SC_MOVED_TEMPORARILY:
82 case HttpStatus.SC_MOVED_PERMANENTLY:
83 case HttpStatus.SC_TEMPORARY_REDIRECT:
84 HttpRequest request = (HttpRequest) context.getAttribute(
85 ExecutionContext.HTTP_REQUEST);
86 String method = request.getRequestLine().getMethod();
87 return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
88 || method.equalsIgnoreCase(HttpHead.METHOD_NAME);
89 case HttpStatus.SC_SEE_OTHER:
90 return true;
91 default:
92 return false;
93 }
94 }
95
96 public URI getLocationURI(
97 final HttpResponse response,
98 final HttpContext context) throws ProtocolException {
99 if (response == null) {
100 throw new IllegalArgumentException("HTTP response may not be null");
101 }
102
103 Header locationHeader = response.getFirstHeader("location");
104 if (locationHeader == null) {
105
106 throw new ProtocolException(
107 "Received redirect response " + response.getStatusLine()
108 + " but no location header");
109 }
110 String location = locationHeader.getValue();
111 if (this.log.isDebugEnabled()) {
112 this.log.debug("Redirect requested to location '" + location + "'");
113 }
114
115 URI uri;
116 try {
117 uri = new URI(location);
118 } catch (URISyntaxException ex) {
119 throw new ProtocolException("Invalid redirect URI: " + location, ex);
120 }
121
122 HttpParams params = response.getParams();
123
124
125 if (!uri.isAbsolute()) {
126 if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
127 throw new ProtocolException("Relative redirect location '"
128 + uri + "' not allowed");
129 }
130
131 HttpHost target = (HttpHost) context.getAttribute(
132 ExecutionContext.HTTP_TARGET_HOST);
133 if (target == null) {
134 throw new IllegalStateException("Target host not available " +
135 "in the HTTP context");
136 }
137
138 HttpRequest request = (HttpRequest) context.getAttribute(
139 ExecutionContext.HTTP_REQUEST);
140
141 try {
142 URI requestURI = new URI(request.getRequestLine().getUri());
143 URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
144 uri = URIUtils.resolve(absoluteRequestURI, uri);
145 } catch (URISyntaxException ex) {
146 throw new ProtocolException(ex.getMessage(), ex);
147 }
148 }
149
150 if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
151
152 RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
153 REDIRECT_LOCATIONS);
154
155 if (redirectLocations == null) {
156 redirectLocations = new RedirectLocations();
157 context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
158 }
159
160 URI redirectURI;
161 if (uri.getFragment() != null) {
162 try {
163 HttpHost target = new HttpHost(
164 uri.getHost(),
165 uri.getPort(),
166 uri.getScheme());
167 redirectURI = URIUtils.rewriteURI(uri, target, true);
168 } catch (URISyntaxException ex) {
169 throw new ProtocolException(ex.getMessage(), ex);
170 }
171 } else {
172 redirectURI = uri;
173 }
174
175 if (redirectLocations.contains(redirectURI)) {
176 throw new CircularRedirectException("Circular redirect to '" +
177 redirectURI + "'");
178 } else {
179 redirectLocations.add(redirectURI);
180 }
181 }
182
183 return uri;
184 }
185
186 }