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.client.protocol;
29
30 import java.io.IOException;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.http.Header;
40 import org.apache.http.HttpException;
41 import org.apache.http.HttpHost;
42 import org.apache.http.HttpRequest;
43 import org.apache.http.HttpRequestInterceptor;
44 import org.apache.http.annotation.Immutable;
45 import org.apache.http.client.CookieStore;
46 import org.apache.http.client.config.CookieSpecs;
47 import org.apache.http.client.config.RequestConfig;
48 import org.apache.http.config.Lookup;
49 import org.apache.http.conn.routing.RouteInfo;
50 import org.apache.http.cookie.Cookie;
51 import org.apache.http.cookie.CookieOrigin;
52 import org.apache.http.cookie.CookieSpec;
53 import org.apache.http.cookie.CookieSpecProvider;
54 import org.apache.http.cookie.SetCookie2;
55 import org.apache.http.protocol.HttpContext;
56 import org.apache.http.util.Args;
57 import org.apache.http.util.TextUtils;
58
59
60
61
62
63
64
65
66 @Immutable
67 public class RequestAddCookies implements HttpRequestInterceptor {
68
69 private final Log log = LogFactory.getLog(getClass());
70
71 public RequestAddCookies() {
72 super();
73 }
74
75 public void process(final HttpRequest request, final HttpContext context)
76 throws HttpException, IOException {
77 Args.notNull(request, "HTTP request");
78 Args.notNull(context, "HTTP context");
79
80 final String method = request.getRequestLine().getMethod();
81 if (method.equalsIgnoreCase("CONNECT")) {
82 return;
83 }
84
85 final HttpClientContext clientContext = HttpClientContext.adapt(context);
86
87
88 final CookieStore cookieStore = clientContext.getCookieStore();
89 if (cookieStore == null) {
90 this.log.debug("Cookie store not specified in HTTP context");
91 return;
92 }
93
94
95 final Lookup<CookieSpecProvider> registry = clientContext.getCookieSpecRegistry();
96 if (registry == null) {
97 this.log.debug("CookieSpec registry not specified in HTTP context");
98 return;
99 }
100
101
102 final HttpHost targetHost = clientContext.getTargetHost();
103 if (targetHost == null) {
104 this.log.debug("Target host not set in the context");
105 return;
106 }
107
108
109 final RouteInfo route = clientContext.getHttpRoute();
110 if (route == null) {
111 this.log.debug("Connection route not set in the context");
112 return;
113 }
114
115 final RequestConfig config = clientContext.getRequestConfig();
116 String policy = config.getCookieSpec();
117 if (policy == null) {
118 policy = CookieSpecs.BEST_MATCH;
119 }
120 if (this.log.isDebugEnabled()) {
121 this.log.debug("CookieSpec selected: " + policy);
122 }
123
124 URI requestURI = null;
125 try {
126 requestURI = new URI(request.getRequestLine().getUri());
127 } catch (final URISyntaxException ignore) {
128 }
129 final String path = requestURI != null ? requestURI.getPath() : null;
130 final String hostName = targetHost.getHostName();
131 int port = targetHost.getPort();
132 if (port < 0) {
133 port = route.getTargetHost().getPort();
134 }
135
136 final CookieOrigin cookieOrigin = new CookieOrigin(
137 hostName,
138 port >= 0 ? port : 0,
139 !TextUtils.isEmpty(path) ? path : "/",
140 route.isSecure());
141
142
143 final CookieSpecProvider provider = registry.lookup(policy);
144 if (provider == null) {
145 throw new HttpException("Unsupported cookie policy: " + policy);
146 }
147 final CookieSpec cookieSpec = provider.create(clientContext);
148
149 final List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies());
150
151 final List<Cookie> matchedCookies = new ArrayList<Cookie>();
152 final Date now = new Date();
153 for (final Cookie cookie : cookies) {
154 if (!cookie.isExpired(now)) {
155 if (cookieSpec.match(cookie, cookieOrigin)) {
156 if (this.log.isDebugEnabled()) {
157 this.log.debug("Cookie " + cookie + " match " + cookieOrigin);
158 }
159 matchedCookies.add(cookie);
160 }
161 } else {
162 if (this.log.isDebugEnabled()) {
163 this.log.debug("Cookie " + cookie + " expired");
164 }
165 }
166 }
167
168 if (!matchedCookies.isEmpty()) {
169 final List<Header> headers = cookieSpec.formatCookies(matchedCookies);
170 for (final Header header : headers) {
171 request.addHeader(header);
172 }
173 }
174
175 final int ver = cookieSpec.getVersion();
176 if (ver > 0) {
177 boolean needVersionHeader = false;
178 for (final Cookie cookie : matchedCookies) {
179 if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
180 needVersionHeader = true;
181 }
182 }
183
184 if (needVersionHeader) {
185 final Header header = cookieSpec.getVersionHeader();
186 if (header != null) {
187
188 request.addHeader(header);
189 }
190 }
191 }
192
193
194
195 context.setAttribute(HttpClientContext.COOKIE_SPEC, cookieSpec);
196 context.setAttribute(HttpClientContext.COOKIE_ORIGIN, cookieOrigin);
197 }
198
199 }