1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.httpclient;
16
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.http.NoHttpResponseException;
25 import org.apache.http.cookie.ClientCookie;
26 import org.apache.http.cookie.Cookie;
27 import org.apache.http.cookie.CookieOrigin;
28 import org.apache.http.cookie.CookieSpec;
29 import org.apache.http.cookie.MalformedCookieException;
30 import org.apache.http.message.BasicNameValuePair;
31 import org.apache.http.message.BufferedHeader;
32 import org.apache.http.util.CharArrayBuffer;
33 import org.htmlunit.BrowserVersion;
34 import org.htmlunit.util.NameValuePair;
35 import org.htmlunit.util.UrlUtils;
36
37
38
39
40
41
42 public final class HttpClientConverter {
43
44 private HttpClientConverter() {
45
46 }
47
48
49
50
51
52
53 public static List<org.apache.http.NameValuePair> nameValuePairsToHttpClient(final List<NameValuePair> pairs) {
54 final List<org.apache.http.NameValuePair> resultingPairs = new ArrayList<>(pairs.size());
55 for (final NameValuePair pair : pairs) {
56 resultingPairs.add(new BasicNameValuePair(pair.getName(), pair.getValue()));
57 }
58 return resultingPairs;
59 }
60
61
62
63
64
65 public static boolean isNoHttpResponseException(final Exception e) {
66 return e instanceof NoHttpResponseException;
67 }
68
69
70
71
72
73
74 public static CookieOrigin buildCookieOrigin(final URL url) {
75 final URL normalizedUrl = replaceForCookieIfNecessary(url);
76
77 int port = normalizedUrl.getPort();
78 if (port == -1) {
79 port = normalizedUrl.getDefaultPort();
80 }
81
82 return new CookieOrigin(
83 normalizedUrl.getHost(),
84 port,
85 normalizedUrl.getPath(),
86 "https".equals(normalizedUrl.getProtocol()));
87 }
88
89
90
91
92
93
94
95
96
97 public static URL replaceForCookieIfNecessary(URL url) {
98 final String protocol = url.getProtocol();
99 final boolean file = "file".equals(protocol);
100 if (file) {
101 try {
102 url = UrlUtils.getUrlWithNewHostAndPort(url,
103 HtmlUnitBrowserCompatCookieSpec.LOCAL_FILESYSTEM_DOMAIN, 0);
104 }
105 catch (final MalformedURLException e) {
106 throw new RuntimeException(e);
107 }
108 }
109 return url;
110 }
111
112
113
114
115
116
117
118
119 public static List<org.htmlunit.util.Cookie> parseCookie(final String cookieString, final URL pageUrl,
120 final BrowserVersion browserVersion)
121 throws MalformedCookieException {
122 final CharArrayBuffer buffer = new CharArrayBuffer(cookieString.length() + 22);
123 buffer.append("Set-Cookie: ");
124 buffer.append(cookieString);
125
126 final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(browserVersion);
127 final List<Cookie> cookies = cookieSpec.parse(new BufferedHeader(buffer), buildCookieOrigin(pageUrl));
128
129 final List<org.htmlunit.util.Cookie> htmlUnitCookies = new ArrayList<>(cookies.size());
130 for (final Cookie cookie : cookies) {
131 final org.htmlunit.util.Cookie htmlUnitCookie = new org.htmlunit.util.Cookie((ClientCookie) cookie);
132 htmlUnitCookies.add(htmlUnitCookie);
133 }
134 return htmlUnitCookies;
135 }
136
137
138
139
140
141
142 public static List<Cookie> toHttpClient(final Collection<org.htmlunit.util.Cookie> cookies) {
143 final ArrayList<Cookie> array = new ArrayList<>(cookies.size());
144 for (final org.htmlunit.util.Cookie cookie : cookies) {
145 array.add(cookie.toHttpClient());
146 }
147 return array;
148 }
149
150
151
152
153
154
155 public static List<org.htmlunit.util.Cookie> fromHttpClient(final List<Cookie> cookies) {
156 final List<org.htmlunit.util.Cookie> list = new ArrayList<>(cookies.size());
157 for (final Cookie c : cookies) {
158 list.add(new org.htmlunit.util.Cookie((ClientCookie) c));
159 }
160 return list;
161 }
162
163
164
165
166
167
168
169
170 public static void addMatching(final Set<org.htmlunit.util.Cookie> cookies,
171 final URL normalizedUrl, final BrowserVersion browserVersion,
172 final Set<org.htmlunit.util.Cookie> matches) {
173 if (!cookies.isEmpty()) {
174 final CookieOrigin cookieOrigin = HttpClientConverter.buildCookieOrigin(normalizedUrl);
175 final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(browserVersion);
176 for (final org.htmlunit.util.Cookie cookie : cookies) {
177 if (cookieSpec.match(cookie.toHttpClient(), cookieOrigin)) {
178 matches.add(cookie);
179 }
180 }
181 }
182 }
183 }