WebURLProtectionSpace.cpp 10 KB

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