1 /* 2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.net.http; 27 28 import java.io.IOException; 29 import java.net.CookieHandler; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Optional; 33 import java.net.http.HttpHeaders; 34 import jdk.internal.net.http.common.HttpHeadersBuilder; 35 import jdk.internal.net.http.common.Log; 36 import jdk.internal.net.http.common.Utils; 37 38 class CookieFilter implements HeaderFilter { 39 CookieFilter()40 public CookieFilter() { 41 } 42 43 @Override request(HttpRequestImpl r, MultiExchange<?> e)44 public void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException { 45 HttpClientImpl client = e.client(); 46 Optional<CookieHandler> cookieHandlerOpt = client.cookieHandler(); 47 if (cookieHandlerOpt.isPresent()) { 48 CookieHandler cookieHandler = cookieHandlerOpt.get(); 49 Map<String,List<String>> userheaders = r.getUserHeaders().map(); 50 Map<String,List<String>> cookies = cookieHandler.get(r.uri(), userheaders); 51 52 // add the returned cookies 53 HttpHeadersBuilder systemHeadersBuilder = r.getSystemHeadersBuilder(); 54 if (cookies.isEmpty()) { 55 Log.logTrace("Request: no cookie to add for {0}", r.uri()); 56 } else { 57 Log.logTrace("Request: adding cookies for {0}", r.uri()); 58 } 59 for (Map.Entry<String,List<String>> entry : cookies.entrySet()) { 60 final String hdrname = entry.getKey(); 61 if (!hdrname.equalsIgnoreCase("Cookie") 62 && !hdrname.equalsIgnoreCase("Cookie2")) 63 continue; 64 List<String> values = entry.getValue(); 65 if (values == null || values.isEmpty()) continue; 66 for (String val : values) { 67 if (Utils.isValidValue(val)) { 68 systemHeadersBuilder.addHeader(hdrname, val); 69 } 70 } 71 } 72 } else { 73 Log.logTrace("Request: No cookie manager found for {0}", r.uri()); 74 } 75 } 76 77 @Override response(Response r)78 public HttpRequestImpl response(Response r) throws IOException { 79 HttpHeaders hdrs = r.headers(); 80 HttpRequestImpl request = r.request(); 81 Exchange<?> e = r.exchange; 82 Log.logTrace("Response: processing cookies for {0}", request.uri()); 83 Optional<CookieHandler> cookieHandlerOpt = e.client().cookieHandler(); 84 if (cookieHandlerOpt.isPresent()) { 85 CookieHandler cookieHandler = cookieHandlerOpt.get(); 86 Log.logTrace("Response: parsing cookies from {0}", hdrs.map()); 87 cookieHandler.put(request.uri(), hdrs.map()); 88 } else { 89 Log.logTrace("Response: No cookie manager found for {0}", 90 request.uri()); 91 } 92 return null; 93 } 94 } 95