Remove Cocoa CFURLConnection loading code
[WebKit-https.git] / Source / WebCore / platform / network / cf / AuthenticationCF.cpp
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "AuthenticationCF.h"
28
29 #if USE(CFURLCONNECTION)
30
31 #include "AuthenticationChallenge.h"
32 #include "AuthenticationClient.h"
33 #include "Credential.h"
34 #include "ProtectionSpace.h"
35 #include <CFNetwork/CFURLAuthChallengePriv.h>
36 #include <CFNetwork/CFURLCredentialPriv.h>
37 #include <CFNetwork/CFURLProtectionSpacePriv.h>
38
39 namespace WebCore {
40
41 AuthenticationChallenge::AuthenticationChallenge(const ProtectionSpace& protectionSpace, const Credential& proposedCredential, unsigned previousFailureCount, const ResourceResponse& response, const ResourceError& error)
42     : AuthenticationChallengeBase(protectionSpace, proposedCredential, previousFailureCount, response, error)
43 {
44 }
45
46 AuthenticationChallenge::AuthenticationChallenge(CFURLAuthChallengeRef cfChallenge, AuthenticationClient* authenticationClient)
47     : AuthenticationChallengeBase(core(CFURLAuthChallengeGetProtectionSpace(cfChallenge)), core(CFURLAuthChallengeGetProposedCredential(cfChallenge)),
48         CFURLAuthChallengeGetPreviousFailureCount(cfChallenge), (CFURLResponseRef)CFURLAuthChallengeGetFailureResponse(cfChallenge), CFURLAuthChallengeGetError(cfChallenge))
49     , m_authenticationClient(authenticationClient)
50     , m_cfChallenge(cfChallenge)
51 {
52 }
53
54 void AuthenticationChallenge::setAuthenticationClient(AuthenticationClient* client)
55 {
56     m_authenticationClient = client;
57 }
58
59 AuthenticationClient* AuthenticationChallenge::authenticationClient() const
60 {
61     return m_authenticationClient.get();
62 }
63
64 bool AuthenticationChallenge::platformCompare(const AuthenticationChallenge& a, const AuthenticationChallenge& b)
65 {
66     if (a.authenticationClient() != b.authenticationClient())
67         return false;
68
69     if (a.cfURLAuthChallengeRef() != b.cfURLAuthChallengeRef())
70         return false;
71         
72     return true;
73 }
74
75 CFURLAuthChallengeRef createCF(const AuthenticationChallenge& coreChallenge)
76 {
77     // FIXME: Why not cache CFURLAuthChallengeRef in m_cfChallenge? Foundation counterpart does that.
78
79     RetainPtr<CFURLCredentialRef> credential = adoptCF(createCF(coreChallenge.proposedCredential()));
80     RetainPtr<CFURLProtectionSpaceRef> protectionSpace = adoptCF(createCF(coreChallenge.protectionSpace()));
81     CFURLAuthChallengeRef result = CFURLAuthChallengeCreate(0, protectionSpace.get(), credential.get(), coreChallenge.previousFailureCount(), coreChallenge.failureResponse().cfURLResponse(), coreChallenge.error());
82     return result;
83 }
84
85 CFURLCredentialRef createCF(const Credential& coreCredential)
86 {
87     CFURLCredentialPersistence persistence = kCFURLCredentialPersistenceNone;
88     switch (coreCredential.persistence()) {
89     case CredentialPersistenceNone:
90         break;
91     case CredentialPersistenceForSession:
92         persistence = kCFURLCredentialPersistenceForSession;
93         break;
94     case CredentialPersistencePermanent:
95         persistence = kCFURLCredentialPersistencePermanent;
96         break;
97     default:
98         ASSERT_NOT_REACHED();
99     }
100     
101     return CFURLCredentialCreate(0, coreCredential.user().createCFString().get(), coreCredential.password().createCFString().get(), 0, persistence);
102 }
103
104 CFURLProtectionSpaceRef createCF(const ProtectionSpace& coreSpace)
105 {
106     CFURLProtectionSpaceServerType serverType = kCFURLProtectionSpaceServerHTTP;
107     switch (coreSpace.serverType()) {
108     case ProtectionSpaceServerHTTP:
109         serverType = kCFURLProtectionSpaceServerHTTP;
110         break;
111     case ProtectionSpaceServerHTTPS:
112         serverType = kCFURLProtectionSpaceServerHTTPS;
113         break;
114     case ProtectionSpaceServerFTP:
115         serverType = kCFURLProtectionSpaceServerFTP;
116         break;
117     case ProtectionSpaceServerFTPS:
118         serverType = kCFURLProtectionSpaceServerFTPS;
119         break;
120     case ProtectionSpaceProxyHTTP:
121         serverType = kCFURLProtectionSpaceProxyHTTP;
122         break;
123     case ProtectionSpaceProxyHTTPS:
124         serverType = kCFURLProtectionSpaceProxyHTTPS;
125         break;
126     case ProtectionSpaceProxyFTP:
127         serverType = kCFURLProtectionSpaceProxyFTP;
128         break;
129     case ProtectionSpaceProxySOCKS:
130         serverType = kCFURLProtectionSpaceProxySOCKS;
131         break;
132     default:
133         ASSERT_NOT_REACHED();
134     }
135
136     CFURLProtectionSpaceAuthenticationScheme scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
137     switch (coreSpace.authenticationScheme()) {
138     case ProtectionSpaceAuthenticationSchemeDefault:
139         scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
140         break;
141     case ProtectionSpaceAuthenticationSchemeHTTPBasic:
142         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic;
143         break;
144     case ProtectionSpaceAuthenticationSchemeHTTPDigest:
145         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest;
146         break;
147     case ProtectionSpaceAuthenticationSchemeHTMLForm:
148         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTMLForm;
149         break;
150     case ProtectionSpaceAuthenticationSchemeNTLM:
151         scheme = kCFURLProtectionSpaceAuthenticationSchemeNTLM;
152         break;
153     case ProtectionSpaceAuthenticationSchemeNegotiate:
154         scheme = kCFURLProtectionSpaceAuthenticationSchemeNegotiate;
155         break;
156 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
157     case ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
158         scheme = kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
159         break;
160     case ProtectionSpaceAuthenticationSchemeClientCertificateRequested:
161         scheme = kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested;
162         break;
163 #endif
164     default:
165         ASSERT_NOT_REACHED();
166     }
167
168     return CFURLProtectionSpaceCreate(0, coreSpace.host().createCFString().get(), coreSpace.port(), serverType, coreSpace.realm().createCFString().get(), scheme);
169 }
170
171 Credential core(CFURLCredentialRef cfCredential)
172 {
173     if (!cfCredential)
174         return Credential();
175
176     CredentialPersistence persistence = CredentialPersistenceNone;
177     switch (CFURLCredentialGetPersistence(cfCredential)) {
178     case kCFURLCredentialPersistenceNone:
179         break;
180     case kCFURLCredentialPersistenceForSession:
181         persistence = CredentialPersistenceForSession;
182         break;
183     case kCFURLCredentialPersistencePermanent:
184         persistence = CredentialPersistencePermanent;
185         break;
186     default:
187         ASSERT_NOT_REACHED();
188     }
189
190     RetainPtr<CFStringRef> password = adoptCF(CFURLCredentialCopyPassword(cfCredential));
191     return Credential(CFURLCredentialGetUsername(cfCredential), password.get(), persistence);
192 }
193
194 ProtectionSpace core(CFURLProtectionSpaceRef cfSpace)
195 {
196     ProtectionSpaceServerType serverType = ProtectionSpaceServerHTTP;
197     
198     switch (CFURLProtectionSpaceGetServerType(cfSpace)) {
199     case kCFURLProtectionSpaceServerHTTP:
200         break;
201     case kCFURLProtectionSpaceServerHTTPS:
202         serverType = ProtectionSpaceServerHTTPS;
203         break;
204     case kCFURLProtectionSpaceServerFTP:
205         serverType = ProtectionSpaceServerFTP;
206         break;
207     case kCFURLProtectionSpaceServerFTPS:
208         serverType = ProtectionSpaceServerFTPS;
209         break;
210     case kCFURLProtectionSpaceProxyHTTP:
211         serverType = ProtectionSpaceProxyHTTP;
212         break;
213     case kCFURLProtectionSpaceProxyHTTPS:
214         serverType = ProtectionSpaceProxyHTTPS;
215         break;
216     case kCFURLProtectionSpaceProxyFTP:
217         serverType = ProtectionSpaceProxyFTP;
218         break;
219     case kCFURLProtectionSpaceProxySOCKS:
220         serverType = ProtectionSpaceProxySOCKS;
221         break;
222     default:
223         ASSERT_NOT_REACHED();
224     }
225
226     ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault;
227     
228     switch (CFURLProtectionSpaceGetAuthenticationScheme(cfSpace)) {
229     case kCFURLProtectionSpaceAuthenticationSchemeDefault:
230         scheme = ProtectionSpaceAuthenticationSchemeDefault;
231         break;
232     case kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic:
233         scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic;
234         break;
235     case kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest:
236         scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest;
237         break;
238     case kCFURLProtectionSpaceAuthenticationSchemeHTMLForm:
239         scheme = ProtectionSpaceAuthenticationSchemeHTMLForm;
240         break;
241     case kCFURLProtectionSpaceAuthenticationSchemeNTLM:
242         scheme = ProtectionSpaceAuthenticationSchemeNTLM;
243         break;
244     case kCFURLProtectionSpaceAuthenticationSchemeNegotiate:
245         scheme = ProtectionSpaceAuthenticationSchemeNegotiate;
246         break;
247 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
248     case kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested:
249         scheme = ProtectionSpaceAuthenticationSchemeClientCertificateRequested;
250         break;
251     case kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
252         scheme = ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
253         break;
254 #endif
255     default:
256         scheme = ProtectionSpaceAuthenticationSchemeUnknown;
257         ASSERT_NOT_REACHED();
258     }
259         
260     return ProtectionSpace(CFURLProtectionSpaceGetHost(cfSpace), 
261                            CFURLProtectionSpaceGetPort(cfSpace),
262                            serverType,
263                            CFURLProtectionSpaceGetRealm(cfSpace),
264                            scheme);
265 }
266
267 }
268
269 #endif // USE(CFURLCONNECTION)