2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CredentialStorage.h"
29 #include "Credential.h"
31 #include "ProtectionSpaceHash.h"
32 #include <wtf/text/WTFString.h>
33 #include <wtf/text/StringHash.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/HashSet.h>
36 #include <wtf/StdLibExtras.h>
40 typedef HashMap<ProtectionSpace, Credential> ProtectionSpaceToCredentialMap;
41 static ProtectionSpaceToCredentialMap& protectionSpaceToCredentialMap()
43 DEFINE_STATIC_LOCAL(ProtectionSpaceToCredentialMap, map, ());
47 static HashSet<String>& originsWithCredentials()
49 DEFINE_STATIC_LOCAL(HashSet<String>, set, ());
53 typedef HashMap<String, ProtectionSpace> PathToDefaultProtectionSpaceMap;
54 static PathToDefaultProtectionSpaceMap& pathToDefaultProtectionSpaceMap()
56 DEFINE_STATIC_LOCAL(PathToDefaultProtectionSpaceMap, map, ());
60 static String originStringFromURL(const KURL& url)
63 return url.protocol() + "://" + url.host() + ':' + String::number(url.port()) + '/';
65 return url.protocol() + "://" + url.host() + '/';
68 static String protectionSpaceMapKeyFromURL(const KURL& url)
70 ASSERT(url.isValid());
72 // Remove the last path component that is not a directory to determine the subtree for which credentials will apply.
73 // We keep a leading slash, but remove a trailing one.
74 String directoryURL = url.string().substring(0, url.pathEnd());
75 unsigned directoryURLPathStart = url.pathStart();
76 ASSERT(directoryURL[directoryURLPathStart] == '/');
77 if (directoryURL.length() > directoryURLPathStart + 1) {
78 size_t index = directoryURL.reverseFind('/');
79 ASSERT(index != notFound);
80 directoryURL = directoryURL.substring(0, (index != directoryURLPathStart) ? index : directoryURLPathStart + 1);
86 void CredentialStorage::set(const Credential& credential, const ProtectionSpace& protectionSpace, const KURL& url)
88 ASSERT(protectionSpace.isProxy() || url.protocolInHTTPFamily());
89 ASSERT(protectionSpace.isProxy() || url.isValid());
91 protectionSpaceToCredentialMap().set(protectionSpace, credential);
92 if (!protectionSpace.isProxy()) {
93 originsWithCredentials().add(originStringFromURL(url));
95 ProtectionSpaceAuthenticationScheme scheme = protectionSpace.authenticationScheme();
96 if (scheme == ProtectionSpaceAuthenticationSchemeHTTPBasic || scheme == ProtectionSpaceAuthenticationSchemeDefault) {
97 // The map can contain both a path and its subpath - while redundant, this makes lookups faster.
98 pathToDefaultProtectionSpaceMap().set(protectionSpaceMapKeyFromURL(url), protectionSpace);
103 Credential CredentialStorage::get(const ProtectionSpace& protectionSpace)
105 return protectionSpaceToCredentialMap().get(protectionSpace);
108 void CredentialStorage::remove(const ProtectionSpace& protectionSpace)
110 protectionSpaceToCredentialMap().remove(protectionSpace);
113 static PathToDefaultProtectionSpaceMap::iterator findDefaultProtectionSpaceForURL(const KURL& url)
115 ASSERT(url.protocolInHTTPFamily());
116 ASSERT(url.isValid());
118 PathToDefaultProtectionSpaceMap& map = pathToDefaultProtectionSpaceMap();
120 // Don't spend time iterating the path for origins that don't have any credentials.
121 if (!originsWithCredentials().contains(originStringFromURL(url)))
124 String directoryURL = protectionSpaceMapKeyFromURL(url);
125 unsigned directoryURLPathStart = url.pathStart();
127 PathToDefaultProtectionSpaceMap::iterator iter = map.find(directoryURL);
128 if (iter != map.end())
131 if (directoryURL.length() == directoryURLPathStart + 1) // path is "/" already, cannot shorten it any more
134 size_t index = directoryURL.reverseFind('/', directoryURL.length() - 2);
135 ASSERT(index != notFound);
136 directoryURL = directoryURL.substring(0, (index == directoryURLPathStart) ? index + 1 : index);
137 ASSERT(directoryURL.length() > directoryURLPathStart);
138 ASSERT(directoryURL.length() == directoryURLPathStart + 1 || directoryURL[directoryURL.length() - 1] != '/');
142 bool CredentialStorage::set(const Credential& credential, const KURL& url)
144 ASSERT(url.protocolInHTTPFamily());
145 ASSERT(url.isValid());
146 PathToDefaultProtectionSpaceMap::iterator iter = findDefaultProtectionSpaceForURL(url);
147 if (iter == pathToDefaultProtectionSpaceMap().end())
149 ASSERT(originsWithCredentials().contains(originStringFromURL(url)));
150 protectionSpaceToCredentialMap().set(iter->second, credential);
154 Credential CredentialStorage::get(const KURL& url)
156 PathToDefaultProtectionSpaceMap::iterator iter = findDefaultProtectionSpaceForURL(url);
157 if (iter == pathToDefaultProtectionSpaceMap().end())
159 return protectionSpaceToCredentialMap().get(iter->second);
162 } // namespace WebCore