View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.client;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  import java.util.Locale;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.http.Header;
37  import org.apache.http.HttpHost;
38  import org.apache.http.HttpRequest;
39  import org.apache.http.HttpResponse;
40  import org.apache.http.HttpStatus;
41  import org.apache.http.ProtocolException;
42  import org.apache.http.annotation.Immutable;
43  import org.apache.http.client.CircularRedirectException;
44  import org.apache.http.client.RedirectStrategy;
45  import org.apache.http.client.config.RequestConfig;
46  import org.apache.http.client.methods.HttpGet;
47  import org.apache.http.client.methods.HttpHead;
48  import org.apache.http.client.methods.HttpUriRequest;
49  import org.apache.http.client.methods.RequestBuilder;
50  import org.apache.http.client.protocol.HttpClientContext;
51  import org.apache.http.client.utils.URIBuilder;
52  import org.apache.http.client.utils.URIUtils;
53  import org.apache.http.protocol.HttpContext;
54  import org.apache.http.util.Args;
55  import org.apache.http.util.Asserts;
56  import org.apache.http.util.TextUtils;
57  
58  /**
59   * Default implementation of {@link RedirectStrategy}. This strategy honors the restrictions
60   * on automatic redirection of entity enclosing methods such as POST and PUT imposed by the
61   * HTTP specification. {@code 302 Moved Temporarily}, {@code 301 Moved Permanently} and
62   * {@code 307 Temporary Redirect} status codes will result in an automatic redirect of
63   * HEAD and GET methods only. POST and PUT methods will not be automatically redirected
64   * as requiring user confirmation.
65   * <p>
66   * The restriction on automatic redirection of POST methods can be relaxed by using
67   * {@link LaxRedirectStrategy} instead of {@link DefaultRedirectStrategy}.
68   * </p>
69   *
70   * @see LaxRedirectStrategy
71   * @since 4.1
72   */
73  @Immutable
74  public class DefaultRedirectStrategy implements RedirectStrategy {
75  
76      private final Log log = LogFactory.getLog(getClass());
77  
78      /**
79       * @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
80       */
81      @Deprecated
82      public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
83  
84      public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
85  
86      /**
87       * Redirectable methods.
88       */
89      private static final String[] REDIRECT_METHODS = new String[] {
90          HttpGet.METHOD_NAME,
91          HttpHead.METHOD_NAME
92      };
93  
94      public DefaultRedirectStrategy() {
95          super();
96      }
97  
98      @Override
99      public boolean isRedirected(
100             final HttpRequest request,
101             final HttpResponse response,
102             final HttpContext context) throws ProtocolException {
103         Args.notNull(request, "HTTP request");
104         Args.notNull(response, "HTTP response");
105 
106         final int statusCode = response.getStatusLine().getStatusCode();
107         final String method = request.getRequestLine().getMethod();
108         final Header locationHeader = response.getFirstHeader("location");
109         switch (statusCode) {
110         case HttpStatus.SC_MOVED_TEMPORARILY:
111             return isRedirectable(method) && locationHeader != null;
112         case HttpStatus.SC_MOVED_PERMANENTLY:
113         case HttpStatus.SC_TEMPORARY_REDIRECT:
114             return isRedirectable(method);
115         case HttpStatus.SC_SEE_OTHER:
116             return true;
117         default:
118             return false;
119         } //end of switch
120     }
121 
122     public URI getLocationURI(
123             final HttpRequest request,
124             final HttpResponse response,
125             final HttpContext context) throws ProtocolException {
126         Args.notNull(request, "HTTP request");
127         Args.notNull(response, "HTTP response");
128         Args.notNull(context, "HTTP context");
129 
130         final HttpClientContext clientContext = HttpClientContext.adapt(context);
131 
132         //get the location header to find out where to redirect to
133         final Header locationHeader = response.getFirstHeader("location");
134         if (locationHeader == null) {
135             // got a redirect response, but no location header
136             throw new ProtocolException(
137                     "Received redirect response " + response.getStatusLine()
138                     + " but no location header");
139         }
140         final String location = locationHeader.getValue();
141         if (this.log.isDebugEnabled()) {
142             this.log.debug("Redirect requested to location '" + location + "'");
143         }
144 
145         final RequestConfig config = clientContext.getRequestConfig();
146 
147         URI uri = createLocationURI(location);
148 
149         // rfc2616 demands the location value be a complete URI
150         // Location       = "Location" ":" absoluteURI
151         try {
152             if (!uri.isAbsolute()) {
153                 if (!config.isRelativeRedirectsAllowed()) {
154                     throw new ProtocolException("Relative redirect location '"
155                             + uri + "' not allowed");
156                 }
157                 // Adjust location URI
158                 final HttpHost target = clientContext.getTargetHost();
159                 Asserts.notNull(target, "Target host");
160                 final URI requestURI = new URI(request.getRequestLine().getUri());
161                 final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
162                 uri = URIUtils.resolve(absoluteRequestURI, uri);
163             }
164         } catch (final URISyntaxException ex) {
165             throw new ProtocolException(ex.getMessage(), ex);
166         }
167 
168         RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
169                 HttpClientContext.REDIRECT_LOCATIONS);
170         if (redirectLocations == null) {
171             redirectLocations = new RedirectLocations();
172             context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
173         }
174         if (!config.isCircularRedirectsAllowed()) {
175             if (redirectLocations.contains(uri)) {
176                 throw new CircularRedirectException("Circular redirect to '" + uri + "'");
177             }
178         }
179         redirectLocations.add(uri);
180         return uri;
181     }
182 
183     /**
184      * @since 4.1
185      */
186     protected URI createLocationURI(final String location) throws ProtocolException {
187         try {
188             final URIBuilder b = new URIBuilder(new URI(location).normalize());
189             final String host = b.getHost();
190             if (host != null) {
191                 b.setHost(host.toLowerCase(Locale.ROOT));
192             }
193             final String path = b.getPath();
194             if (TextUtils.isEmpty(path)) {
195                 b.setPath("/");
196             }
197             return b.build();
198         } catch (final URISyntaxException ex) {
199             throw new ProtocolException("Invalid redirect URI: " + location, ex);
200         }
201     }
202 
203     /**
204      * @since 4.2
205      */
206     protected boolean isRedirectable(final String method) {
207         for (final String m: REDIRECT_METHODS) {
208             if (m.equalsIgnoreCase(method)) {
209                 return true;
210             }
211         }
212         return false;
213     }
214 
215     @Override
216     public HttpUriRequest getRedirect(
217             final HttpRequest request,
218             final HttpResponse response,
219             final HttpContext context) throws ProtocolException {
220         final URI uri = getLocationURI(request, response, context);
221         final String method = request.getRequestLine().getMethod();
222         if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
223             return new HttpHead(uri);
224         } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
225             return new HttpGet(uri);
226         } else {
227             final int status = response.getStatusLine().getStatusCode();
228             if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
229                 return RequestBuilder.copy(request).setUri(uri).build();
230             } else {
231                 return new HttpGet(uri);
232             }
233         }
234     }
235 
236 }