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.http.annotation.Immutable;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.http.Header;
42 import org.apache.http.HttpException;
43 import org.apache.http.HttpHost;
44 import org.apache.http.HttpRequest;
45 import org.apache.http.HttpRequestInterceptor;
46 import org.apache.http.ProtocolException;
47 import org.apache.http.client.CookieStore;
48 import org.apache.http.client.methods.HttpUriRequest;
49 import org.apache.http.client.params.HttpClientParams;
50 import org.apache.http.conn.HttpRoutedConnection;
51 import org.apache.http.conn.routing.HttpRoute;
52 import org.apache.http.cookie.Cookie;
53 import org.apache.http.cookie.CookieOrigin;
54 import org.apache.http.cookie.CookieSpec;
55 import org.apache.http.cookie.CookieSpecRegistry;
56 import org.apache.http.cookie.SetCookie2;
57 import org.apache.http.protocol.HttpContext;
58 import org.apache.http.protocol.ExecutionContext;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 @Immutable
76 public class RequestAddCookies implements HttpRequestInterceptor {
77
78 private final Log log = LogFactory.getLog(getClass());
79
80 public RequestAddCookies() {
81 super();
82 }
83
84 public void process(final HttpRequest request, final HttpContext context)
85 throws HttpException, IOException {
86 if (request == null) {
87 throw new IllegalArgumentException("HTTP request may not be null");
88 }
89 if (context == null) {
90 throw new IllegalArgumentException("HTTP context may not be null");
91 }
92
93 String method = request.getRequestLine().getMethod();
94 if (method.equalsIgnoreCase("CONNECT")) {
95 return;
96 }
97
98
99 CookieStore cookieStore = (CookieStore) context.getAttribute(
100 ClientContext.COOKIE_STORE);
101 if (cookieStore == null) {
102 this.log.debug("Cookie store not specified in HTTP context");
103 return;
104 }
105
106
107 CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute(
108 ClientContext.COOKIESPEC_REGISTRY);
109 if (registry == null) {
110 this.log.debug("CookieSpec registry not specified in HTTP context");
111 return;
112 }
113
114
115 HttpHost targetHost = (HttpHost) context.getAttribute(
116 ExecutionContext.HTTP_TARGET_HOST);
117 if (targetHost == null) {
118 this.log.debug("Target host not set in the context");
119 return;
120 }
121
122
123 HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
124 ExecutionContext.HTTP_CONNECTION);
125 if (conn == null) {
126 this.log.debug("HTTP connection not set in the context");
127 return;
128 }
129
130 String policy = HttpClientParams.getCookiePolicy(request.getParams());
131 if (this.log.isDebugEnabled()) {
132 this.log.debug("CookieSpec selected: " + policy);
133 }
134
135 URI requestURI;
136 if (request instanceof HttpUriRequest) {
137 requestURI = ((HttpUriRequest) request).getURI();
138 } else {
139 try {
140 requestURI = new URI(request.getRequestLine().getUri());
141 } catch (URISyntaxException ex) {
142 throw new ProtocolException("Invalid request URI: " +
143 request.getRequestLine().getUri(), ex);
144 }
145 }
146
147 String hostName = targetHost.getHostName();
148 int port = targetHost.getPort();
149 if (port < 0) {
150 HttpRoute route = conn.getRoute();
151 if (route.getHopCount() == 1) {
152 port = conn.getRemotePort();
153 } else {
154
155
156 String scheme = targetHost.getSchemeName();
157 if (scheme.equalsIgnoreCase("http")) {
158 port = 80;
159 } else if (scheme.equalsIgnoreCase("https")) {
160 port = 443;
161 } else {
162 port = 0;
163 }
164 }
165 }
166
167 CookieOrigin cookieOrigin = new CookieOrigin(
168 hostName,
169 port,
170 requestURI.getPath(),
171 conn.isSecure());
172
173
174 CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
175
176 List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies());
177
178 List<Cookie> matchedCookies = new ArrayList<Cookie>();
179 Date now = new Date();
180 for (Cookie cookie : cookies) {
181 if (!cookie.isExpired(now)) {
182 if (cookieSpec.match(cookie, cookieOrigin)) {
183 if (this.log.isDebugEnabled()) {
184 this.log.debug("Cookie " + cookie + " match " + cookieOrigin);
185 }
186 matchedCookies.add(cookie);
187 }
188 } else {
189 if (this.log.isDebugEnabled()) {
190 this.log.debug("Cookie " + cookie + " expired");
191 }
192 }
193 }
194
195 if (!matchedCookies.isEmpty()) {
196 List<Header> headers = cookieSpec.formatCookies(matchedCookies);
197 for (Header header : headers) {
198 request.addHeader(header);
199 }
200 }
201
202 int ver = cookieSpec.getVersion();
203 if (ver > 0) {
204 boolean needVersionHeader = false;
205 for (Cookie cookie : matchedCookies) {
206 if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
207 needVersionHeader = true;
208 }
209 }
210
211 if (needVersionHeader) {
212 Header header = cookieSpec.getVersionHeader();
213 if (header != null) {
214
215 request.addHeader(header);
216 }
217 }
218 }
219
220
221
222 context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec);
223 context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin);
224 }
225
226 }