| 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 | |
package wjhk.jupload2.upload.helper; |
| 27 | |
|
| 28 | |
import java.net.URL; |
| 29 | |
import java.text.ParseException; |
| 30 | |
import java.text.SimpleDateFormat; |
| 31 | |
import java.util.HashMap; |
| 32 | |
import java.util.StringTokenizer; |
| 33 | |
import java.util.regex.Matcher; |
| 34 | |
import java.util.regex.Pattern; |
| 35 | |
|
| 36 | |
import wjhk.jupload2.policies.UploadPolicy; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class CookieJar { |
| 44 | |
|
| 45 | 0 | final static Pattern pNvPair = Pattern.compile( |
| 46 | |
"^\\s*([^=\\s]+)(\\s*=\\s*(.+))*$", Pattern.CASE_INSENSITIVE); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | private UploadPolicy uploadPolicy = null; |
| 52 | |
|
| 53 | 0 | private HashMap<String, Cookie> jar = new HashMap<String, Cookie>(); |
| 54 | |
|
| 55 | 0 | private String domain = null; |
| 56 | |
|
| 57 | 0 | private static class Cookie implements Cloneable { |
| 58 | |
|
| 59 | 0 | String domain = null; |
| 60 | |
|
| 61 | 0 | String name = null; |
| 62 | |
|
| 63 | 0 | String value = null; |
| 64 | |
|
| 65 | 0 | String path = null; |
| 66 | |
|
| 67 | 0 | long max_age = 0; |
| 68 | |
|
| 69 | 0 | int version = 0; |
| 70 | |
|
| 71 | 0 | int secure = 0; |
| 72 | |
|
| 73 | 0 | Cookie() { |
| 74 | |
|
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public Cookie clone() throws CloneNotSupportedException { |
| 83 | 0 | Cookie ret = (Cookie) super.clone(); |
| 84 | 0 | ret.domain = this.domain; |
| 85 | 0 | ret.name = this.name; |
| 86 | 0 | ret.value = this.value; |
| 87 | 0 | ret.path = this.path; |
| 88 | 0 | ret.max_age = this.max_age; |
| 89 | 0 | ret.version = this.version; |
| 90 | 0 | ret.secure = this.secure; |
| 91 | 0 | return ret; |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public String getKey() { |
| 101 | 0 | String ret = this.name; |
| 102 | 0 | if (null != this.path) |
| 103 | 0 | ret += this.path; |
| 104 | 0 | return ret; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public String getHeader(String path, int secure) { |
| 116 | 0 | StringBuffer sb = new StringBuffer(); |
| 117 | 0 | if ((null == this.path || this.path.equals("/") || this.path |
| 118 | 0 | .startsWith(path)) |
| 119 | |
&& (this.secure <= secure) |
| 120 | 0 | && (this.max_age > System.currentTimeMillis())) { |
| 121 | 0 | if (this.version > 0) { |
| 122 | 0 | sb.append("$Version=").append(this.version).append("; "); |
| 123 | 0 | sb.append(this.name).append("=").append(this.value).append( |
| 124 | |
";"); |
| 125 | 0 | if (null != this.path) |
| 126 | 0 | sb.append(" $Path=").append(this.path).append(";"); |
| 127 | 0 | if (null != this.domain) |
| 128 | 0 | sb.append(" $Domain=").append(this.domain).append(";"); |
| 129 | |
} else { |
| 130 | 0 | sb.append(this.name).append("=").append(this.value).append( |
| 131 | |
";"); |
| 132 | |
} |
| 133 | |
} |
| 134 | 0 | return sb.toString(); |
| 135 | |
} |
| 136 | |
|
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | 0 | public CookieJar(UploadPolicy uploadPolicy) { |
| 153 | 0 | this.uploadPolicy = uploadPolicy; |
| 154 | 0 | } |
| 155 | |
|
| 156 | |
private String stripQuotes(String s) { |
| 157 | 0 | if (s.startsWith("\"") && s.endsWith("\"")) |
| 158 | 0 | return s.substring(1, s.length() - 1); |
| 159 | 0 | return s; |
| 160 | |
} |
| 161 | |
|
| 162 | |
private boolean domainMatch(String cd) { |
| 163 | 0 | if (cd == null) { |
| 164 | 0 | return false; |
| 165 | |
} else { |
| 166 | 0 | if (!cd.startsWith(".")) { |
| 167 | 0 | int dot = cd.indexOf('.'); |
| 168 | 0 | if (dot >= 0) |
| 169 | 0 | cd = cd.substring(dot); |
| 170 | |
|
| 171 | |
} |
| 172 | 0 | return cd.equals(this.domain); |
| 173 | |
} |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public void setDomain(String domain) { |
| 183 | 0 | if (!domain.startsWith(".")) { |
| 184 | 0 | int dot = domain.indexOf('.'); |
| 185 | 0 | if (dot >= 0) |
| 186 | 0 | domain = domain.substring(dot); |
| 187 | |
|
| 188 | |
} |
| 189 | 0 | this.domain = domain; |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public String buildCookieHeader(URL url) { |
| 200 | 0 | String domain = url.getHost(); |
| 201 | 0 | int dot = domain.indexOf('.'); |
| 202 | 0 | if (dot >= 0) |
| 203 | 0 | domain = domain.substring(dot); |
| 204 | 0 | if (domain.equals(this.domain)) { |
| 205 | 0 | String path = url.getPath(); |
| 206 | 0 | int secure = url.getProtocol().equalsIgnoreCase("https") ? 1 : 0; |
| 207 | 0 | StringBuffer sb = new StringBuffer(); |
| 208 | 0 | for (String key : this.jar.keySet()) { |
| 209 | 0 | Cookie c = this.jar.get(key); |
| 210 | 0 | if (null != c) |
| 211 | 0 | sb.append(c.getHeader(path, secure)); |
| 212 | 0 | } |
| 213 | 0 | if (sb.length() > 0) { |
| 214 | 0 | sb.append("\r\n"); |
| 215 | 0 | return "Cookie: " + sb.toString(); |
| 216 | |
} |
| 217 | |
} |
| 218 | 0 | return null; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public void parseCookieHeader(String s) { |
| 230 | 0 | StringTokenizer t = new StringTokenizer(s, ";"); |
| 231 | 0 | Cookie cookie = new Cookie(); |
| 232 | 0 | while (t.hasMoreTokens()) { |
| 233 | 0 | Matcher m = pNvPair.matcher(t.nextToken()); |
| 234 | 0 | if (m.matches()) { |
| 235 | 0 | String n = m.group(1); |
| 236 | 0 | String v = (m.groupCount() > 2 && m.group(3) != null) ? m |
| 237 | 0 | .group(3).trim() : ""; |
| 238 | 0 | if (n.compareToIgnoreCase("version") == 0) { |
| 239 | 0 | cookie.version = Integer.parseInt(v); |
| 240 | 0 | continue; |
| 241 | |
} |
| 242 | 0 | if (n.compareToIgnoreCase("domain") == 0) { |
| 243 | 0 | cookie.domain = v; |
| 244 | 0 | continue; |
| 245 | |
} |
| 246 | 0 | if (n.compareToIgnoreCase("path") == 0) { |
| 247 | 0 | cookie.path = v; |
| 248 | 0 | continue; |
| 249 | |
} |
| 250 | 0 | if (n.compareToIgnoreCase("max-age") == 0) { |
| 251 | 0 | cookie.max_age = Integer.parseInt(v); |
| 252 | 0 | if (cookie.max_age < 0) |
| 253 | 0 | cookie.max_age = 0; |
| 254 | 0 | cookie.max_age *= 1000; |
| 255 | 0 | cookie.max_age += System.currentTimeMillis(); |
| 256 | 0 | continue; |
| 257 | |
} |
| 258 | 0 | if (n.compareToIgnoreCase("expires") == 0) { |
| 259 | 0 | SimpleDateFormat df = new SimpleDateFormat( |
| 260 | |
"EEE, dd-MMM-yy HH:mm:ss zzz"); |
| 261 | |
try { |
| 262 | 0 | cookie.max_age = System.currentTimeMillis() |
| 263 | 0 | - df.parse(v).getTime(); |
| 264 | 0 | if (cookie.max_age < 0) |
| 265 | 0 | cookie.max_age = 0; |
| 266 | 0 | } catch (ParseException e) { |
| 267 | 0 | cookie.max_age = 0; |
| 268 | 0 | } |
| 269 | 0 | continue; |
| 270 | |
} |
| 271 | 0 | if (n.compareToIgnoreCase("comment") == 0) { |
| 272 | |
|
| 273 | 0 | continue; |
| 274 | |
} |
| 275 | 0 | if (n.compareToIgnoreCase("secure") == 0) { |
| 276 | 0 | cookie.secure = 1; |
| 277 | 0 | continue; |
| 278 | |
} |
| 279 | 0 | if (!n.startsWith("$")) { |
| 280 | 0 | if (null != cookie.name) { |
| 281 | 0 | if (cookie.version > 0) { |
| 282 | |
|
| 283 | 0 | cookie.domain = stripQuotes(cookie.domain); |
| 284 | 0 | cookie.path = stripQuotes(cookie.path); |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
} |
| 289 | 0 | if (domainMatch(cookie.domain)) { |
| 290 | 0 | if (cookie.max_age > 0) { |
| 291 | 0 | this.jar.put(cookie.getKey(), cookie); |
| 292 | 0 | this.uploadPolicy.displayDebug( |
| 293 | |
"[CookieJar] Adding cookie: " |
| 294 | 0 | + cookie.getKey() + ": " |
| 295 | |
+ cookie, 50); |
| 296 | |
|
| 297 | |
} else { |
| 298 | 0 | this.jar.put(cookie.getKey(), null); |
| 299 | 0 | this.uploadPolicy.displayDebug( |
| 300 | |
"[CookieJar] Ignoring cookie: " |
| 301 | 0 | + cookie.getKey() + ": " |
| 302 | |
+ cookie, 50); |
| 303 | |
} |
| 304 | |
} |
| 305 | |
try { |
| 306 | 0 | cookie = cookie.clone(); |
| 307 | 0 | } catch (CloneNotSupportedException e) { |
| 308 | 0 | cookie = new Cookie(); |
| 309 | 0 | } |
| 310 | |
} |
| 311 | 0 | cookie.name = n; |
| 312 | 0 | cookie.value = v; |
| 313 | |
} |
| 314 | |
} |
| 315 | 0 | } |
| 316 | 0 | } |
| 317 | |
} |