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