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. <tt>302 Moved Temporarily</tt>, <tt>301 Moved Permanently</tt> and
62   * <tt>307 Temporary Redirect</tt> 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   *
69   * @see LaxRedirectStrategy
70   * @since 4.1
71   */
72  @Immutable
73  public class DefaultRedirectStrategy implements RedirectStrategy {
74  
75      private final Log log = LogFactory.getLog(getClass());
76  
77      /**
78       * @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
79       */
80      @Deprecated
81      public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
82  
83      public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
84  
85      /**
86       * Redirectable methods.
87       */
88      private static final String[] REDIRECT_METHODS = new String[] {
89          HttpGet.METHOD_NAME,
90          HttpHead.METHOD_NAME
91      };
92  
93      public DefaultRedirectStrategy() {
94          super();
95      }
96  
97      public boolean isRedirected(
98              final HttpRequest request,
99              final HttpResponse response,
100             final HttpContext context) throws ProtocolException {
101         Args.notNull(request, "HTTP request");
102         Args.notNull(response, "HTTP response");
103 
104         final int statusCode = response.getStatusLine().getStatusCode();
105         final String method = request.getRequestLine().getMethod();
106         final Header locationHeader = response.getFirstHeader("location");
107         switch (statusCode) {
108         case HttpStatus.SC_MOVED_TEMPORARILY:
109             return isRedirectable(method) && locationHeader != null;
110         case HttpStatus.SC_MOVED_PERMANENTLY:
111         case HttpStatus.SC_TEMPORARY_REDIRECT:
112             return isRedirectable(method);
113         case HttpStatus.SC_SEE_OTHER:
114             return true;
115         default:
116             return false;
117         } //end of switch
118     }
119 
120     public URI getLocationURI(
121             final HttpRequest request,
122             final HttpResponse response,
123             final HttpContext context) throws ProtocolException {
124         Args.notNull(request, "HTTP request");
125         Args.notNull(response, "HTTP response");
126         Args.notNull(context, "HTTP context");
127 
128         final HttpClientContext clientContext = HttpClientContext.adapt(context);
129 
130         //get the location header to find out where to redirect to
131         final Header locationHeader = response.getFirstHeader("location");
132         if (locationHeader == null) {
133             // got a redirect response, but no location header
134             throw new ProtocolException(
135                     "Received redirect response " + response.getStatusLine()
136                     + " but no location header");
137         }
138         final String location = locationHeader.getValue();
139         if (this.log.isDebugEnabled()) {
140             this.log.debug("Redirect requested to location '" + location + "'");
141         }
142 
143         final RequestConfig config = clientContext.getRequestConfig();
144 
145         URI uri = createLocationURI(location);
146 
147         // rfc2616 demands the location value be a complete URI
148         // Location       = "Location" ":" absoluteURI
149         try {
150             if (!uri.isAbsolute()) {
151                 if (!config.isRelativeRedirectsAllowed()) {
152                     throw new ProtocolException("Relative redirect location '"
153                             + uri + "' not allowed");
154                 }
155                 // Adjust location URI
156                 final HttpHost target = clientContext.getTargetHost();
157                 Asserts.notNull(target, "Target host");
158                 final URI requestURI = new URI(request.getRequestLine().getUri());
159                 final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
160                 uri = URIUtils.resolve(absoluteRequestURI, uri);
161             }
162         } catch (final URISyntaxException ex) {
163             throw new ProtocolException(ex.getMessage(), ex);
164         }
165 
166         RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
167                 HttpClientContext.REDIRECT_LOCATIONS);
168         if (redirectLocations == null) {
169             redirectLocations = new RedirectLocations();
170             context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
171         }
172         if (!config.isCircularRedirectsAllowed()) {
173             if (redirectLocations.contains(uri)) {
174                 throw new CircularRedirectException("Circular redirect to '" + uri + "'");
175             }
176         }
177         redirectLocations.add(uri);
178         return uri;
179     }
180 
181     /**
182      * @since 4.1
183      */
184     protected URI createLocationURI(final String location) throws ProtocolException {
185         try {
186             final URIBuilder b = new URIBuilder(new URI(location).normalize());
187             final String host = b.getHost();
188             if (host != null) {
189                 b.setHost(host.toLowerCase(Locale.US));
190             }
191             final String path = b.getPath();
192             if (TextUtils.isEmpty(path)) {
193                 b.setPath("/");
194             }
195             return b.build();
196         } catch (final URISyntaxException ex) {
197             throw new ProtocolException("Invalid redirect URI: " + location, ex);
198         }
199     }
200 
201     /**
202      * @since 4.2
203      */
204     protected boolean isRedirectable(final String method) {
205         for (final String m: REDIRECT_METHODS) {
206             if (m.equalsIgnoreCase(method)) {
207                 return true;
208             }
209         }
210         return false;
211     }
212 
213     public HttpUriRequest getRedirect(
214             final HttpRequest request,
215             final HttpResponse response,
216             final HttpContext context) throws ProtocolException {
217         final URI uri = getLocationURI(request, response, context);
218         final String method = request.getRequestLine().getMethod();
219         if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
220             return new HttpHead(uri);
221         } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
222             return new HttpGet(uri);
223         } else {
224             final int status = response.getStatusLine().getStatusCode();
225             if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
226                 return RequestBuilder.copy(request).setUri(uri).build();
227             } else {
228                 return new HttpGet(uri);
229             }
230         }
231     }
232 
233 }