2 * Copyright (C) 2006, 2007 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 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.
27 #include "WebURLResponse.h"
29 #include "WebKitDLL.h"
32 #include "COMPropertyBag.h"
33 #include "MarshallingHelpers.h"
36 #include <WebKitSystemInterface/WebKitSystemInterface.h>
39 #include <wtf/platform.h>
40 #include <WebCore/BString.h>
41 #include <WebCore/KURL.h>
42 #include <WebCore/LocalizedStrings.h>
43 #include <WebCore/ResourceHandle.h>
48 using namespace WebCore;
50 static String CFHTTPMessageCopyLocalizedShortDescriptionForStatusCode(CFIndex statusCode)
53 if (statusCode < 100 || statusCode >= 600)
54 result = WEB_UI_STRING("server error", "HTTP result code string");
55 else if (statusCode >= 100 && statusCode <= 199) {
58 result = WEB_UI_STRING("continue", "HTTP result code string");
61 result = WEB_UI_STRING("switching protocols", "HTTP result code string");
64 result = WEB_UI_STRING("informational", "HTTP result code string");
67 } else if (statusCode >= 200 && statusCode <= 299) {
70 result = WEB_UI_STRING("no error", "HTTP result code string");
73 result = WEB_UI_STRING("created", "HTTP result code string");
76 result = WEB_UI_STRING("accepted", "HTTP result code string");
79 result = WEB_UI_STRING("non-authoritative information", "HTTP result code string");
82 result = WEB_UI_STRING("no content", "HTTP result code string");
85 result = WEB_UI_STRING("reset content", "HTTP result code string");
88 result = WEB_UI_STRING("partial content", "HTTP result code string");
91 result = WEB_UI_STRING("success", "HTTP result code string");
94 } else if (statusCode >= 300 && statusCode <= 399) {
97 result = WEB_UI_STRING("multiple choices", "HTTP result code string");
100 result = WEB_UI_STRING("moved permanently", "HTTP result code string");
103 result = WEB_UI_STRING("found", "HTTP result code string");
106 result = WEB_UI_STRING("see other", "HTTP result code string");
109 result = WEB_UI_STRING("not modified", "HTTP result code string");
112 result = WEB_UI_STRING("needs proxy", "HTTP result code string");
115 result = WEB_UI_STRING("temporarily redirected", "HTTP result code string");
117 case 306: // 306 status code unused in HTTP
119 result = WEB_UI_STRING("redirected", "HTTP result code string");
122 } else if (statusCode >= 400 && statusCode <= 499) {
123 switch (statusCode) {
125 result = WEB_UI_STRING("bad request", "HTTP result code string");
128 result = WEB_UI_STRING("unauthorized", "HTTP result code string");
131 result = WEB_UI_STRING("payment required", "HTTP result code string");
134 result = WEB_UI_STRING("forbidden", "HTTP result code string");
137 result = WEB_UI_STRING("not found", "HTTP result code string");
140 result = WEB_UI_STRING("method not allowed", "HTTP result code string");
143 result = WEB_UI_STRING("unacceptable", "HTTP result code string");
146 result = WEB_UI_STRING("proxy authentication required", "HTTP result code string");
149 result = WEB_UI_STRING("request timed out", "HTTP result code string");
152 result = WEB_UI_STRING("conflict", "HTTP result code string");
155 result = WEB_UI_STRING("no longer exists", "HTTP result code string");
158 result = WEB_UI_STRING("length required", "HTTP result code string");
161 result = WEB_UI_STRING("precondition failed", "HTTP result code string");
164 result = WEB_UI_STRING("request too large", "HTTP result code string");
167 result = WEB_UI_STRING("requested URL too long", "HTTP result code string");
170 result = WEB_UI_STRING("unsupported media type", "HTTP result code string");
173 result = WEB_UI_STRING("requested range not satisfiable", "HTTP result code string");
176 result = WEB_UI_STRING("expectation failed", "HTTP result code string");
179 result = WEB_UI_STRING("client error", "HTTP result code string");
182 } else if (statusCode >= 500 && statusCode <= 599) {
183 switch (statusCode) {
185 result = WEB_UI_STRING("internal server error", "HTTP result code string");
188 result = WEB_UI_STRING("unimplemented", "HTTP result code string");
191 result = WEB_UI_STRING("bad gateway", "HTTP result code string");
194 result = WEB_UI_STRING("service unavailable", "HTTP result code string");
197 result = WEB_UI_STRING("gateway timed out", "HTTP result code string");
200 result = WEB_UI_STRING("unsupported version", "HTTP result code string");
203 result = WEB_UI_STRING("server error", "HTTP result code string");
210 // IWebURLResponse ----------------------------------------------------------------
212 WebURLResponse::WebURLResponse()
216 gClassNameCount.add("WebURLResponse");
219 WebURLResponse::~WebURLResponse()
222 gClassNameCount.remove("WebURLResponse");
225 WebURLResponse* WebURLResponse::createInstance()
227 WebURLResponse* instance = new WebURLResponse();
228 // fake an http response - so it has the IWebHTTPURLResponse interface
229 instance->m_response = ResourceResponse(KURL(ParsedURLString, "http://"), String(), 0, String(), String());
234 WebURLResponse* WebURLResponse::createInstance(const ResourceResponse& response)
236 if (response.isNull())
239 WebURLResponse* instance = new WebURLResponse();
241 instance->m_response = response;
246 // IUnknown -------------------------------------------------------------------
248 HRESULT STDMETHODCALLTYPE WebURLResponse::QueryInterface(REFIID riid, void** ppvObject)
251 if (IsEqualGUID(riid, IID_IUnknown))
252 *ppvObject = static_cast<IWebURLResponse*>(this);
253 else if (IsEqualGUID(riid, __uuidof(this)))
255 else if (IsEqualGUID(riid, IID_IWebURLResponse))
256 *ppvObject = static_cast<IWebURLResponse*>(this);
257 else if (IsEqualGUID(riid, IID_IWebURLResponsePrivate))
258 *ppvObject = static_cast<IWebURLResponsePrivate*>(this);
259 else if (m_response.isHTTP() && IsEqualGUID(riid, IID_IWebHTTPURLResponse))
260 *ppvObject = static_cast<IWebHTTPURLResponse*>(this);
262 return E_NOINTERFACE;
268 ULONG STDMETHODCALLTYPE WebURLResponse::AddRef(void)
273 ULONG STDMETHODCALLTYPE WebURLResponse::Release(void)
275 ULONG newRef = --m_refCount;
282 // IWebURLResponse --------------------------------------------------------------------
284 HRESULT STDMETHODCALLTYPE WebURLResponse::expectedContentLength(
285 /* [retval][out] */ long long* result)
287 *result = m_response.expectedContentLength();
291 HRESULT STDMETHODCALLTYPE WebURLResponse::initWithURL(
293 /* [in] */ BSTR mimeType,
294 /* [in] */ int expectedContentLength,
295 /* [in] */ BSTR textEncodingName)
297 m_response = ResourceResponse(MarshallingHelpers::BSTRToKURL(url), String(mimeType), expectedContentLength, String(textEncodingName), String());
301 HRESULT STDMETHODCALLTYPE WebURLResponse::MIMEType(
302 /* [retval][out] */ BSTR* result)
304 BString mimeType(m_response.mimeType());
305 *result = mimeType.release();
306 if (!m_response.mimeType().isNull() && !*result)
307 return E_OUTOFMEMORY;
312 HRESULT STDMETHODCALLTYPE WebURLResponse::suggestedFilename(
313 /* [retval][out] */ BSTR* result)
316 ASSERT_NOT_REACHED();
322 if (m_response.url().isEmpty())
325 *result = BString(m_response.suggestedFilename()).release();
329 HRESULT STDMETHODCALLTYPE WebURLResponse::textEncodingName(
330 /* [retval][out] */ BSTR* result)
335 BString textEncodingName(m_response.textEncodingName());
336 *result = textEncodingName.release();
337 if (!m_response.textEncodingName().isNull() && !*result)
338 return E_OUTOFMEMORY;
343 HRESULT STDMETHODCALLTYPE WebURLResponse::URL(
344 /* [retval][out] */ BSTR* result)
349 BString url(m_response.url().string());
350 *result = url.release();
351 if (!m_response.url().isEmpty() && !*result)
352 return E_OUTOFMEMORY;
357 // IWebHTTPURLResponse --------------------------------------------------------
359 HRESULT STDMETHODCALLTYPE WebURLResponse::allHeaderFields(
360 /* [retval][out] */ IPropertyBag** headerFields)
362 ASSERT(m_response.isHTTP());
364 *headerFields = COMPropertyBag<String, AtomicString, CaseFoldingHash>::createInstance(m_response.httpHeaderFields());
368 HRESULT STDMETHODCALLTYPE WebURLResponse::localizedStringForStatusCode(
369 /* [in] */ int statusCode,
370 /* [retval][out] */ BSTR* statusString)
372 ASSERT(m_response.isHTTP());
375 String statusText = CFHTTPMessageCopyLocalizedShortDescriptionForStatusCode(statusCode);
379 *statusString = BString(statusText).release();
383 HRESULT STDMETHODCALLTYPE WebURLResponse::statusCode(
384 /* [retval][out] */ int* statusCode)
386 ASSERT(m_response.isHTTP());
388 *statusCode = m_response.httpStatusCode();
392 HRESULT STDMETHODCALLTYPE WebURLResponse::isAttachment(
393 /* [retval][out] */ BOOL *attachment)
395 *attachment = m_response.isAttachment();
400 HRESULT STDMETHODCALLTYPE WebURLResponse::sslPeerCertificate(
401 /* [retval][out] */ OLE_HANDLE* result)
408 CFDictionaryRef dict = certificateDictionary();
411 void* data = wkGetSSLPeerCertificateDataBytePtr(dict);
414 *result = (OLE_HANDLE)(ULONG64)data;
417 return *result ? S_OK : E_FAIL;
420 // WebURLResponse -------------------------------------------------------------
422 HRESULT WebURLResponse::suggestedFileExtension(BSTR *result)
429 if (m_response.mimeType().isEmpty())
432 BString mimeType(m_response.mimeType());
434 LONG err = RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("MIME\\Database\\Content Type"), 0, KEY_QUERY_VALUE, &key);
437 err = RegOpenKeyEx(key, mimeType, 0, KEY_QUERY_VALUE, &subKey);
439 DWORD keyType = REG_SZ;
440 WCHAR extension[MAX_PATH];
441 DWORD keySize = sizeof(extension)/sizeof(extension[0]);
442 err = RegQueryValueEx(subKey, TEXT("Extension"), 0, &keyType, (LPBYTE)extension, &keySize);
443 if (!err && keyType != REG_SZ)
444 err = ERROR_INVALID_DATA;
447 if (!wcscmp(mimeType, L"text/html")) {
448 wcscpy(extension, L".html");
450 } else if (!wcscmp(mimeType, L"application/xhtml+xml")) {
451 wcscpy(extension, L".xhtml");
453 } else if (!wcscmp(mimeType, L"image/svg+xml")) {
454 wcscpy(extension, L".svg");
459 *result = SysAllocString(extension);
461 err = ERROR_OUTOFMEMORY;
468 return HRESULT_FROM_WIN32(err);
471 const ResourceResponse& WebURLResponse::resourceResponse() const
477 CFDictionaryRef WebURLResponse::certificateDictionary() const
479 if (m_SSLCertificateInfo)
480 return m_SSLCertificateInfo.get();
482 CFURLResponseRef cfResponse = m_response.cfURLResponse();
485 m_SSLCertificateInfo = wkGetSSLCertificateInfo(cfResponse);
486 return m_SSLCertificateInfo.get();