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.hc.client5.http.impl;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.Locale;
33
34 import org.apache.hc.client5.http.protocol.RedirectStrategy;
35 import org.apache.hc.client5.http.utils.URIUtils;
36 import org.apache.hc.core5.annotation.Contract;
37 import org.apache.hc.core5.annotation.ThreadingBehavior;
38 import org.apache.hc.core5.http.Header;
39 import org.apache.hc.core5.http.HttpException;
40 import org.apache.hc.core5.http.HttpHeaders;
41 import org.apache.hc.core5.http.HttpRequest;
42 import org.apache.hc.core5.http.HttpResponse;
43 import org.apache.hc.core5.http.HttpStatus;
44 import org.apache.hc.core5.http.ProtocolException;
45 import org.apache.hc.core5.http.protocol.HttpContext;
46 import org.apache.hc.core5.net.URIBuilder;
47 import org.apache.hc.core5.util.Args;
48 import org.apache.hc.core5.util.TextUtils;
49
50
51
52
53
54
55 @Contract(threading = ThreadingBehavior.STATELESS)
56 public class DefaultRedirectStrategy implements RedirectStrategy {
57
58 public static final DefaultRedirectStrategydirectStrategy.html#DefaultRedirectStrategy">DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
59
60 @Override
61 public boolean isRedirected(
62 final HttpRequest request,
63 final HttpResponse response,
64 final HttpContext context) throws ProtocolException {
65 Args.notNull(request, "HTTP request");
66 Args.notNull(response, "HTTP response");
67
68 if (!response.containsHeader(HttpHeaders.LOCATION)) {
69 return false;
70 }
71 final int statusCode = response.getCode();
72 switch (statusCode) {
73 case HttpStatus.SC_MOVED_PERMANENTLY:
74 case HttpStatus.SC_MOVED_TEMPORARILY:
75 case HttpStatus.SC_SEE_OTHER:
76 case HttpStatus.SC_TEMPORARY_REDIRECT:
77 case HttpStatus.SC_PERMANENT_REDIRECT:
78 return true;
79 default:
80 return false;
81 }
82 }
83
84 @Override
85 public URI getLocationURI(
86 final HttpRequest request,
87 final HttpResponse response,
88 final HttpContext context) throws HttpException {
89 Args.notNull(request, "HTTP request");
90 Args.notNull(response, "HTTP response");
91 Args.notNull(context, "HTTP context");
92
93
94 final Header locationHeader = response.getFirstHeader("location");
95 if (locationHeader == null) {
96 throw new HttpException("Redirect location is missing");
97 }
98 final String location = locationHeader.getValue();
99 URI uri = createLocationURI(location);
100 try {
101 if (!uri.isAbsolute()) {
102
103 uri = URIUtils.resolve(request.getUri(), uri);
104 }
105 } catch (final URISyntaxException ex) {
106 throw new ProtocolException(ex.getMessage(), ex);
107 }
108
109 return uri;
110 }
111
112
113
114
115 protected URI createLocationURI(final String location) throws ProtocolException {
116 try {
117 final URIBuilder b = new URIBuilder(new URI(location).normalize());
118 final String host = b.getHost();
119 if (host != null) {
120 b.setHost(host.toLowerCase(Locale.ROOT));
121 }
122 final String path = b.getPath();
123 if (TextUtils.isEmpty(path)) {
124 b.setPath("/");
125 }
126 return b.build();
127 } catch (final URISyntaxException ex) {
128 throw new ProtocolException("Invalid redirect URI: " + location, ex);
129 }
130 }
131
132 }