1 /*
2 * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3 * Copyright (C) 2010 Mickael Guessant
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 package davmail.http;
20
21 import org.apache.commons.httpclient.Cookie;
22 import org.apache.commons.httpclient.cookie.MalformedCookieException;
23 import org.apache.commons.httpclient.cookie.RFC2109Spec;
24
25 /**
26 * Custom CookieSpec to allow extended domain names.
27 */
28 public class DavMailCookieSpec extends RFC2109Spec {
29 @Override
30 public void validate(String host, int port, String path,
31 boolean secure, final Cookie cookie) throws MalformedCookieException {
32 // workaround for space in cookie name
33 String cookieName = cookie.getName();
34 if (cookieName != null && cookieName.indexOf(' ') >= 0) {
35 cookie.setName(cookieName.replaceAll(" ", ""));
36 } else {
37 cookieName = null;
38 }
39 // workaround for invalid cookie path
40 String cookiePath = cookie.getPath();
41 if (cookiePath != null && !path.startsWith(cookiePath)) {
42 cookie.setPath(path);
43 } else {
44 cookiePath = null;
45 }
46 // workaround for invalid cookie domain
47 int dotIndex = -1;
48 if (host.endsWith(cookie.getDomain())) {
49 String hostWithoutDomain = host.substring(0, host.length()
50 - cookie.getDomain().length());
51 dotIndex = hostWithoutDomain.indexOf('.');
52 }
53 if (".login.microsoftonline.com".equals(cookie.getDomain())) {
54 cookie.setDomain(host);
55 }
56 if (dotIndex != -1) {
57 // discard additional host name part
58 super.validate(host.substring(dotIndex + 1), port, path, secure, cookie);
59 } else {
60 super.validate(host, port, path, secure, cookie);
61 }
62 if (cookieName != null) {
63 cookie.setName(cookieName);
64 }
65 if (cookiePath != null) {
66 cookie.setPath(cookiePath);
67 }
68 }
69 }