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