184
185 /**
186 * To retrieve current cookie store.
187 *
188 * @return the cookie store currently used by cookie manager.
189 */
190 public CookieStore getCookieStore() {
191 return cookieJar;
192 }
193
194
195 public Map<String, List<String>>
196 get(URI uri, Map<String, List<String>> requestHeaders)
197 throws IOException
198 {
199 // pre-condition check
200 if (uri == null || requestHeaders == null) {
201 throw new IllegalArgumentException("Argument is null");
202 }
203
204 Map<String, List<String>> cookieMap =
205 new java.util.HashMap<String, List<String>>();
206 // if there's no default CookieStore, no way for us to get any cookie
207 if (cookieJar == null)
208 return Collections.unmodifiableMap(cookieMap);
209
210 boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
211 List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
212 String path = uri.getPath();
213 if (path == null || path.isEmpty()) {
214 path = "/";
215 }
216 for (HttpCookie cookie : cookieJar.get(uri)) {
217 // apply path-matches rule (RFC 2965 sec. 3.3.4)
218 // and check for the possible "secure" tag (i.e. don't send
219 // 'secure' cookies over unsecure links)
220 if (pathMatches(path, cookie.getPath()) &&
221 (secureLink || !cookie.getSecure())) {
222 // Enforce httponly attribute
223 if (cookie.isHttpOnly()) {
224 String s = uri.getScheme();
225 if (!"http".equalsIgnoreCase(s) && !"https".equalsIgnoreCase(s)) {
226 continue;
227 }
228 }
229 // Let's check the authorize port list if it exists
230 String ports = cookie.getPortlist();
231 if (ports != null && !ports.isEmpty()) {
394 */
395 private boolean pathMatches(String path, String pathToMatchWith) {
396 if (path == pathToMatchWith)
397 return true;
398 if (path == null || pathToMatchWith == null)
399 return false;
400 if (path.startsWith(pathToMatchWith))
401 return true;
402
403 return false;
404 }
405
406
407 /*
408 * sort cookies with respect to their path: those with more specific Path attributes
409 * precede those with less specific, as defined in RFC 2965 sec. 3.3.4
410 */
411 private List<String> sortByPath(List<HttpCookie> cookies) {
412 Collections.sort(cookies, new CookiePathComparator());
413
414 List<String> cookieHeader = new java.util.ArrayList<String>();
415 for (HttpCookie cookie : cookies) {
416 // Netscape cookie spec and RFC 2965 have different format of Cookie
417 // header; RFC 2965 requires a leading $Version="1" string while Netscape
418 // does not.
419 // The workaround here is to add a $Version="1" string in advance
420 if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) {
421 cookieHeader.add("$Version=\"1\"");
422 }
423
424 cookieHeader.add(cookie.toString());
425 }
426 return cookieHeader;
427 }
428
429
430 static class CookiePathComparator implements Comparator<HttpCookie> {
431 public int compare(HttpCookie c1, HttpCookie c2) {
432 if (c1 == c2) return 0;
433 if (c1 == null) return -1;
434 if (c2 == null) return 1;
|
184
185 /**
186 * To retrieve current cookie store.
187 *
188 * @return the cookie store currently used by cookie manager.
189 */
190 public CookieStore getCookieStore() {
191 return cookieJar;
192 }
193
194
195 public Map<String, List<String>>
196 get(URI uri, Map<String, List<String>> requestHeaders)
197 throws IOException
198 {
199 // pre-condition check
200 if (uri == null || requestHeaders == null) {
201 throw new IllegalArgumentException("Argument is null");
202 }
203
204 Map<String, List<String>> cookieMap = new java.util.HashMap<>();
205 // if there's no default CookieStore, no way for us to get any cookie
206 if (cookieJar == null)
207 return Collections.unmodifiableMap(cookieMap);
208
209 boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
210 List<HttpCookie> cookies = new java.util.ArrayList<>();
211 String path = uri.getPath();
212 if (path == null || path.isEmpty()) {
213 path = "/";
214 }
215 for (HttpCookie cookie : cookieJar.get(uri)) {
216 // apply path-matches rule (RFC 2965 sec. 3.3.4)
217 // and check for the possible "secure" tag (i.e. don't send
218 // 'secure' cookies over unsecure links)
219 if (pathMatches(path, cookie.getPath()) &&
220 (secureLink || !cookie.getSecure())) {
221 // Enforce httponly attribute
222 if (cookie.isHttpOnly()) {
223 String s = uri.getScheme();
224 if (!"http".equalsIgnoreCase(s) && !"https".equalsIgnoreCase(s)) {
225 continue;
226 }
227 }
228 // Let's check the authorize port list if it exists
229 String ports = cookie.getPortlist();
230 if (ports != null && !ports.isEmpty()) {
393 */
394 private boolean pathMatches(String path, String pathToMatchWith) {
395 if (path == pathToMatchWith)
396 return true;
397 if (path == null || pathToMatchWith == null)
398 return false;
399 if (path.startsWith(pathToMatchWith))
400 return true;
401
402 return false;
403 }
404
405
406 /*
407 * sort cookies with respect to their path: those with more specific Path attributes
408 * precede those with less specific, as defined in RFC 2965 sec. 3.3.4
409 */
410 private List<String> sortByPath(List<HttpCookie> cookies) {
411 Collections.sort(cookies, new CookiePathComparator());
412
413 List<String> cookieHeader = new java.util.ArrayList<>();
414 for (HttpCookie cookie : cookies) {
415 // Netscape cookie spec and RFC 2965 have different format of Cookie
416 // header; RFC 2965 requires a leading $Version="1" string while Netscape
417 // does not.
418 // The workaround here is to add a $Version="1" string in advance
419 if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) {
420 cookieHeader.add("$Version=\"1\"");
421 }
422
423 cookieHeader.add(cookie.toString());
424 }
425 return cookieHeader;
426 }
427
428
429 static class CookiePathComparator implements Comparator<HttpCookie> {
430 public int compare(HttpCookie c1, HttpCookie c2) {
431 if (c1 == c2) return 0;
432 if (c1 == null) return -1;
433 if (c2 == null) return 1;
|