Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
14 |
* implied. |
15 |
* |
16 |
* See the License for the specific language governing permissions and |
17 |
* limitations under the License. |
18 |
*/ |
19 |
|
20 |
package org.apache.jmeter.protocol.http.control; |
21 |
|
22 |
import java.net.URL; |
23 |
import java.util.ArrayList; |
24 |
import java.util.Date; |
25 |
import java.util.List; |
26 |
|
27 |
import org.apache.http.Header; |
28 |
import org.apache.http.client.params.CookiePolicy; |
29 |
import org.apache.http.cookie.CookieOrigin; |
30 |
import org.apache.http.cookie.CookieSpec; |
31 |
import org.apache.http.cookie.CookieSpecRegistry; |
32 |
import org.apache.http.cookie.MalformedCookieException; |
33 |
import org.apache.http.impl.cookie.BasicClientCookie; |
34 |
import org.apache.http.impl.cookie.BestMatchSpecFactory; |
35 |
import org.apache.http.impl.cookie.BrowserCompatSpecFactory; |
36 |
import org.apache.http.impl.cookie.IgnoreSpecFactory; |
37 |
import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; |
38 |
import org.apache.http.impl.cookie.RFC2109SpecFactory; |
39 |
import org.apache.http.impl.cookie.RFC2965SpecFactory; |
40 |
import org.apache.http.message.BasicHeader; |
41 |
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; |
42 |
import org.apache.jmeter.protocol.http.util.HTTPConstants; |
43 |
import org.apache.jmeter.testelement.property.CollectionProperty; |
44 |
import org.apache.jmeter.testelement.property.PropertyIterator; |
45 |
import org.apache.jorphan.logging.LoggingManager; |
46 |
import org.apache.log.Logger; |
47 |
|
48 |
public class HC4CookieHandler implements CookieHandler { |
49 |
private static final Logger log = LoggingManager.getLoggerForClass(); |
50 |
|
51 |
private transient CookieSpec cookieSpec; |
52 |
|
53 |
private static CookieSpecRegistry registry = new CookieSpecRegistry(); |
54 |
|
55 |
static { |
56 |
registry.register(CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); |
57 |
registry.register(CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); |
58 |
registry.register(CookiePolicy.RFC_2109, new RFC2109SpecFactory()); |
59 |
registry.register(CookiePolicy.RFC_2965, new RFC2965SpecFactory()); |
60 |
registry.register(CookiePolicy.IGNORE_COOKIES, new IgnoreSpecFactory()); |
61 |
registry.register(CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); |
62 |
} |
63 |
|
64 |
public HC4CookieHandler(String policy) { |
65 |
super(); |
66 |
if (policy.equals(org.apache.commons.httpclient.cookie.CookiePolicy.DEFAULT)) { // tweak diff HC3 vs HC4 |
67 |
policy = CookiePolicy.BEST_MATCH; |
68 |
} |
69 |
this.cookieSpec = registry.getCookieSpec(policy); |
70 |
} |
71 |
|
72 |
public void addCookieFromHeader(CookieManager cookieManager, |
73 |
boolean checkCookies, String cookieHeader, URL url) { |
74 |
boolean debugEnabled = log.isDebugEnabled(); |
75 |
if (debugEnabled) { |
76 |
log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm()); |
77 |
} |
78 |
String protocol = url.getProtocol(); |
79 |
String host = url.getHost(); |
80 |
int port= HTTPSamplerBase.getDefaultPort(protocol,url.getPort()); |
81 |
String path = url.getPath(); |
82 |
boolean isSecure=HTTPSamplerBase.isSecure(protocol); |
83 |
|
84 |
List<org.apache.http.cookie.Cookie> cookies = null; |
85 |
|
86 |
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure); |
87 |
BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader); |
88 |
|
89 |
try { |
90 |
cookies = cookieSpec.parse(basicHeader, cookieOrigin); |
91 |
} catch (MalformedCookieException e) { |
92 |
log.error("Unable to add the cookie", e); |
93 |
} |
94 |
if (cookies == null) { |
95 |
return; |
96 |
} |
97 |
for (org.apache.http.cookie.Cookie cookie : cookies) { |
98 |
try { |
99 |
if (checkCookies) { |
100 |
cookieSpec.validate(cookie, cookieOrigin); |
101 |
} |
102 |
Date expiryDate = cookie.getExpiryDate(); |
103 |
long exp = 0; |
104 |
if (expiryDate!= null) { |
105 |
exp=expiryDate.getTime(); |
106 |
} |
107 |
Cookie newCookie = new Cookie( |
108 |
cookie.getName(), |
109 |
cookie.getValue(), |
110 |
cookie.getDomain(), |
111 |
cookie.getPath(), |
112 |
cookie.isSecure(), |
113 |
exp / 1000 |
114 |
); |
115 |
|
116 |
// Store session cookies as well as unexpired ones |
117 |
if (exp == 0 || exp >= System.currentTimeMillis()) { |
118 |
newCookie.setVersion(cookie.getVersion()); |
119 |
cookieManager.add(newCookie); // Has its own debug log; removes matching cookies |
120 |
} else { |
121 |
cookieManager.removeMatchingCookies(newCookie); |
122 |
if (debugEnabled){ |
123 |
log.info("Dropping expired Cookie: "+newCookie.toString()); |
124 |
} |
125 |
} |
126 |
} catch (MalformedCookieException e) { // This means the cookie was wrong for the URL |
127 |
log.warn("Not storing invalid cookie: <"+cookieHeader+"> for URL "+url+" ("+e.getLocalizedMessage()+")"); |
128 |
} catch (IllegalArgumentException e) { |
129 |
log.warn(cookieHeader+e.getLocalizedMessage()); |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
public String getCookieHeaderForURL(CollectionProperty cookiesCP, URL url, |
135 |
boolean allowVariableCookie) { |
136 |
List<org.apache.http.cookie.Cookie> c = |
137 |
getCookiesForUrl(cookiesCP, url, allowVariableCookie); |
138 |
|
139 |
boolean debugEnabled = log.isDebugEnabled(); |
140 |
if (debugEnabled){ |
141 |
log.debug("Found "+c.size()+" cookies for "+url.toExternalForm()); |
142 |
} |
143 |
if (c.size() <= 0) { |
144 |
return null; |
145 |
} |
146 |
List<Header> lstHdr = cookieSpec.formatCookies(c); |
147 |
|
148 |
StringBuilder sbHdr = new StringBuilder(); |
149 |
for (Header header : lstHdr) { |
150 |
sbHdr.append(header.getValue()); |
151 |
} |
152 |
|
153 |
return sbHdr.toString(); |
154 |
} |
155 |
|
156 |
/** |
157 |
* Get array of valid HttpClient cookies for the URL |
158 |
* |
159 |
* @param url the target URL |
160 |
* @return array of HttpClient cookies |
161 |
* |
162 |
*/ |
163 |
List<org.apache.http.cookie.Cookie> getCookiesForUrl( |
164 |
CollectionProperty cookiesCP, URL url, boolean allowVariableCookie) { |
165 |
List<org.apache.http.cookie.Cookie> cookies = new ArrayList<org.apache.http.cookie.Cookie>(); |
166 |
|
167 |
for (PropertyIterator iter = cookiesCP.iterator(); iter.hasNext();) { |
168 |
Cookie jmcookie = (Cookie) iter.next().getObjectValue(); |
169 |
// Set to running version, to allow function evaluation for the cookie values (bug 28715) |
170 |
if (allowVariableCookie) { |
171 |
jmcookie.setRunningVersion(true); |
172 |
} |
173 |
cookies.add(makeCookie(jmcookie)); |
174 |
if (allowVariableCookie) { |
175 |
jmcookie.setRunningVersion(false); |
176 |
} |
177 |
} |
178 |
String host = url.getHost(); |
179 |
String protocol = url.getProtocol(); |
180 |
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort()); |
181 |
String path = url.getPath(); |
182 |
boolean secure = HTTPSamplerBase.isSecure(protocol); |
183 |
|
184 |
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, secure); |
185 |
|
186 |
List<org.apache.http.cookie.Cookie> cookiesValid = new ArrayList<org.apache.http.cookie.Cookie>(); |
187 |
for (org.apache.http.cookie.Cookie cookie : cookies) { |
188 |
if (cookieSpec.match(cookie, cookieOrigin)) { |
189 |
cookiesValid.add(cookie); |
190 |
} |
191 |
} |
192 |
|
193 |
return cookiesValid; |
194 |
} |
195 |
|
196 |
/** |
197 |
* Create an HttpClient cookie from a JMeter cookie |
198 |
*/ |
199 |
private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) { |
200 |
long exp = jmc.getExpiresMillis(); |
201 |
BasicClientCookie ret = new BasicClientCookie(jmc.getName(), |
202 |
jmc.getValue()); |
203 |
|
204 |
ret.setDomain(jmc.getDomain()); |
205 |
ret.setPath(jmc.getPath()); |
206 |
ret.setExpiryDate(exp > 0 ? new Date(exp) : null); // use null for no expiry |
207 |
ret.setSecure(jmc.getSecure()); |
208 |
ret.setVersion(jmc.getVersion()); |
209 |
return ret; |
210 |
} |
211 |
} |