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 COMPUTER, 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 COMPUTER, 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 "WebKit.h"
28#include "WebKitDLL.h"
29#include "WebURLProtectionSpace.h"
30
31#include <WebCore/BString.h>
32
33using namespace WebCore;
34
35// WebURLProtectionSpace ----------------------------------------------------------------
36
37WebURLProtectionSpace::WebURLProtectionSpace(const ProtectionSpace& protectionSpace)
38    : m_refCount(0)
39    , m_protectionSpace(protectionSpace)
40{
41    gClassCount++;
42    gClassNameCount.add("WebURLProtectionSpace");
43}
44
45WebURLProtectionSpace::~WebURLProtectionSpace()
46{
47    gClassCount--;
48    gClassNameCount.remove("WebURLProtectionSpace");
49}
50
51WebURLProtectionSpace* WebURLProtectionSpace::createInstance()
52{
53    WebURLProtectionSpace* instance = new WebURLProtectionSpace(ProtectionSpace());
54    instance->AddRef();
55    return instance;
56}
57
58WebURLProtectionSpace* WebURLProtectionSpace::createInstance(const ProtectionSpace& protectionSpace)
59{
60    WebURLProtectionSpace* instance = new WebURLProtectionSpace(protectionSpace);
61    instance->AddRef();
62    return instance;
63}
64
65// IUnknown -------------------------------------------------------------------
66
67HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::QueryInterface(REFIID riid, void** ppvObject)
68{
69    *ppvObject = 0;
70    if (IsEqualGUID(riid, IID_IUnknown))
71        *ppvObject = static_cast<IUnknown*>(this);
72    else if (IsEqualGUID(riid, CLSID_WebURLProtectionSpace))
73        *ppvObject = static_cast<WebURLProtectionSpace*>(this);
74    else if (IsEqualGUID(riid, IID_IWebURLProtectionSpace))
75        *ppvObject = static_cast<IWebURLProtectionSpace*>(this);
76    else
77        return E_NOINTERFACE;
78
79    AddRef();
80    return S_OK;
81}
82
83ULONG STDMETHODCALLTYPE WebURLProtectionSpace::AddRef(void)
84{
85    return ++m_refCount;
86}
87
88ULONG STDMETHODCALLTYPE WebURLProtectionSpace::Release(void)
89{
90    ULONG newRef = --m_refCount;
91    if (!newRef)
92        delete(this);
93
94    return newRef;
95}
96
97// IWebURLProtectionSpace -------------------------------------------------------------------
98
99HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::authenticationMethod(
100    /* [out, retval] */ BSTR* result)
101{
102    switch (m_protectionSpace.authenticationScheme()) {
103    case ProtectionSpaceAuthenticationSchemeDefault:
104        *result = SysAllocString(WebURLAuthenticationMethodDefault);
105        break;
106    case ProtectionSpaceAuthenticationSchemeHTTPBasic:
107        *result = SysAllocString(WebURLAuthenticationMethodHTTPBasic);
108        break;
109    case ProtectionSpaceAuthenticationSchemeHTTPDigest:
110        *result = SysAllocString(WebURLAuthenticationMethodHTTPDigest);
111        break;
112    case ProtectionSpaceAuthenticationSchemeHTMLForm:
113        *result = SysAllocString(WebURLAuthenticationMethodHTMLForm);
114        break;
115    default:
116        ASSERT_NOT_REACHED();
117        return E_FAIL;
118    }
119    return S_OK;
120}
121
122HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::host(
123    /* [out, retval] */ BSTR* result)
124{
125    BString str = m_protectionSpace.host();
126    *result = str.release();
127    return S_OK;
128}
129
130static ProtectionSpaceAuthenticationScheme coreScheme(BSTR authenticationMethod)
131{
132    ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault;
133    if (BString(authenticationMethod) == BString(WebURLAuthenticationMethodDefault))
134        scheme = ProtectionSpaceAuthenticationSchemeDefault;
135    else if (BString(authenticationMethod) == BString(WebURLAuthenticationMethodHTTPBasic))
136        scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic;
137    else if (BString(authenticationMethod) == BString(WebURLAuthenticationMethodHTTPDigest))
138        scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest;
139    else if (BString(authenticationMethod) == BString(WebURLAuthenticationMethodHTMLForm))
140        scheme = ProtectionSpaceAuthenticationSchemeHTMLForm;
141    else
142        ASSERT_NOT_REACHED();
143    return scheme;
144}
145
146HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::initWithHost(
147    /* [in] */ BSTR host,
148    /* [in] */ int port,
149    /* [in] */ BSTR protocol,
150    /* [in] */ BSTR realm,
151    /* [in] */ BSTR authenticationMethod)
152{
153    static BString& webURLProtectionSpaceHTTPBString = *new BString(WebURLProtectionSpaceHTTP);
154    static BString& webURLProtectionSpaceHTTPSBString = *new BString(WebURLProtectionSpaceHTTPS);
155    static BString& webURLProtectionSpaceFTPBString = *new BString(WebURLProtectionSpaceFTP);
156    static BString& webURLProtectionSpaceFTPSBString = *new BString(WebURLProtectionSpaceFTPS);
157
158    ProtectionSpaceServerType serverType = ProtectionSpaceServerHTTP;
159    if (BString(protocol) == webURLProtectionSpaceHTTPBString)
160        serverType = ProtectionSpaceServerHTTP;
161    else if (BString(protocol) == webURLProtectionSpaceHTTPSBString)
162        serverType = ProtectionSpaceServerHTTPS;
163    else if (BString(protocol) == webURLProtectionSpaceFTPBString)
164        serverType = ProtectionSpaceServerFTP;
165    else if (BString(protocol) == webURLProtectionSpaceFTPSBString)
166        serverType = ProtectionSpaceServerFTPS;
167
168    m_protectionSpace = ProtectionSpace(String(host, SysStringLen(host)), port, serverType,
169        String(realm, SysStringLen(realm)), coreScheme(authenticationMethod));
170
171    return S_OK;
172}
173
174HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::initWithProxyHost(
175    /* [in] */ BSTR host,
176    /* [in] */ int port,
177    /* [in] */ BSTR proxyType,
178    /* [in] */ BSTR realm,
179    /* [in] */ BSTR authenticationMethod)
180{
181    static BString& webURLProtectionSpaceHTTPProxyBString = *new BString(WebURLProtectionSpaceHTTPProxy);
182    static BString& webURLProtectionSpaceHTTPSProxyBString = *new BString(WebURLProtectionSpaceHTTPSProxy);
183    static BString& webURLProtectionSpaceFTPProxyBString = *new BString(WebURLProtectionSpaceFTPProxy);
184    static BString& webURLProtectionSpaceSOCKSProxyBString = *new BString(WebURLProtectionSpaceSOCKSProxy);
185
186    ProtectionSpaceServerType serverType = ProtectionSpaceProxyHTTP;
187    if (BString(proxyType) == webURLProtectionSpaceHTTPProxyBString)
188        serverType = ProtectionSpaceProxyHTTP;
189    else if (BString(proxyType) == webURLProtectionSpaceHTTPSProxyBString)
190        serverType = ProtectionSpaceProxyHTTPS;
191    else if (BString(proxyType) == webURLProtectionSpaceFTPProxyBString)
192        serverType = ProtectionSpaceProxyFTP;
193    else if (BString(proxyType) == webURLProtectionSpaceSOCKSProxyBString)
194        serverType = ProtectionSpaceProxySOCKS;
195    else
196        ASSERT_NOT_REACHED();
197
198    m_protectionSpace = ProtectionSpace(String(host, SysStringLen(host)), port, serverType,
199        String(realm, SysStringLen(realm)), coreScheme(authenticationMethod));
200
201    return S_OK;
202}
203
204HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::isProxy(
205    /* [out, retval] */ BOOL* result)
206{
207    *result = m_protectionSpace.isProxy();
208    return S_OK;
209}
210
211HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::port(
212    /* [out, retval] */ int* result)
213{
214    *result = m_protectionSpace.port();
215    return S_OK;
216}
217
218HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::protocol(
219    /* [out, retval] */ BSTR* result)
220{
221    switch (m_protectionSpace.serverType()) {
222    case ProtectionSpaceServerHTTP:
223        *result = SysAllocString(WebURLProtectionSpaceHTTP);
224        break;
225    case ProtectionSpaceServerHTTPS:
226        *result = SysAllocString(WebURLProtectionSpaceHTTPS);
227        break;
228    case ProtectionSpaceServerFTP:
229        *result = SysAllocString(WebURLProtectionSpaceFTP);
230        break;
231    case ProtectionSpaceServerFTPS:
232        *result = SysAllocString(WebURLProtectionSpaceFTPS);
233        break;
234    default:
235        ASSERT_NOT_REACHED();
236        return E_FAIL;
237    }
238    return S_OK;
239}
240
241HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::proxyType(
242    /* [out, retval] */ BSTR* result)
243{
244    switch (m_protectionSpace.serverType()) {
245    case ProtectionSpaceProxyHTTP:
246        *result = SysAllocString(WebURLProtectionSpaceHTTPProxy);
247        break;
248    case ProtectionSpaceProxyHTTPS:
249        *result = SysAllocString(WebURLProtectionSpaceHTTPSProxy);
250        break;
251    case ProtectionSpaceProxyFTP:
252        *result = SysAllocString(WebURLProtectionSpaceFTPProxy);
253        break;
254    case ProtectionSpaceProxySOCKS:
255        *result = SysAllocString(WebURLProtectionSpaceSOCKSProxy);
256        break;
257    default:
258        ASSERT_NOT_REACHED();
259        return E_FAIL;
260    }
261    return S_OK;
262}
263
264HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::realm(
265    /* [out, retval] */ BSTR* result)
266{
267    BString bstring = m_protectionSpace.realm();
268    *result = bstring.release();
269    return S_OK;
270}
271
272HRESULT STDMETHODCALLTYPE WebURLProtectionSpace::receivesCredentialSecurely(
273    /* [out, retval] */ BOOL* result)
274{
275    *result = m_protectionSpace.receivesCredentialSecurely();
276    return S_OK;
277}
278
279// WebURLProtectionSpace -------------------------------------------------------------------
280const ProtectionSpace& WebURLProtectionSpace::protectionSpace() const
281{
282    return m_protectionSpace;
283}
284