~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/platform/network/win/CookieJarWin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006, 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 "KURL.h"
 
28
#include "PlatformString.h"
 
29
#include "DeprecatedString.h"
 
30
#include "ResourceHandle.h"
 
31
#include <windows.h>
 
32
#if USE(CFNETWORK)
 
33
#include <CoreFoundation/CoreFoundation.h>
 
34
#include <CFNetwork/CFHTTPCookiesPriv.h>
 
35
#else
 
36
#include <Wininet.h>
 
37
#endif
 
38
 
 
39
namespace WebCore
 
40
{
 
41
 
 
42
#if USE(CFNETWORK)
 
43
    static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
 
44
    static const CFStringRef s_cookieCF = CFSTR("Cookie");
 
45
#endif
 
46
 
 
47
 
 
48
void setCookies(const KURL& url, const KURL& policyURL, const String& value)
 
49
{
 
50
#if USE(CFNETWORK)
 
51
    if (!ResourceHandle::cookieStorage())
 
52
        return;
 
53
 
 
54
    RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
 
55
    RetainPtr<CFURLRef> policyURLCF(AdoptCF, policyURL.createCFURL());
 
56
 
 
57
    // <http://bugzilla.opendarwin.org/show_bug.cgi?id=6531>, <rdar://4409034>
 
58
    // cookiesWithResponseHeaderFields doesn't parse cookies without a value
 
59
    String cookieString = value.contains('=') ? value : value + "=";
 
60
 
 
61
    RetainPtr<CFStringRef> cookieStringCF(AdoptCF, cookieString.createCFString());
 
62
    RetainPtr<CFDictionaryRef> headerFieldsCF(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, (const void**)&s_setCookieKeyCF, 
 
63
        (const void**)&cookieStringCF, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
 
64
 
 
65
    RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault,
 
66
        headerFieldsCF.get(), urlCF.get()));
 
67
 
 
68
    CFHTTPCookieStorageSetCookies(ResourceHandle::cookieStorage(), cookiesCF.get(), urlCF.get(), policyURLCF.get());
 
69
#else
 
70
    // FIXME: Deal with the policy URL.
 
71
    DeprecatedString str = url.url();
 
72
    str.append((UChar)'\0');
 
73
    DeprecatedString val = value.deprecatedString();
 
74
    val.append((UChar)'\0');
 
75
    InternetSetCookie((UChar*)str.unicode(), 0, (UChar*)val.unicode());
 
76
#endif
 
77
}
 
78
 
 
79
String cookies(const KURL& url)
 
80
{
 
81
#if USE(CFNETWORK)
 
82
    if (!ResourceHandle::cookieStorage())
 
83
        return String();
 
84
 
 
85
    String cookieString;
 
86
    RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
 
87
 
 
88
    bool secure = equalIgnoringCase(url.protocol(), "https");
 
89
 
 
90
    RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(ResourceHandle::cookieStorage(), urlCF.get(), secure));
 
91
    RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get()));
 
92
 
 
93
    return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
 
94
#else
 
95
    DeprecatedString str = url.url();
 
96
    str.append((UChar)'\0');
 
97
 
 
98
    DWORD count = str.length();
 
99
    InternetGetCookie((UChar*)str.unicode(), 0, 0, &count);
 
100
    if (count <= 1) // Null terminator counts as 1.
 
101
        return String();
 
102
 
 
103
    UChar* buffer = new UChar[count];
 
104
    InternetGetCookie((UChar*)str.unicode(), 0, buffer, &count);
 
105
    String& result = String(buffer, count-1); // Ignore the null terminator.
 
106
    delete[] buffer;
 
107
    return result;
 
108
#endif
 
109
}
 
110
 
 
111
bool cookiesEnabled()
 
112
{
 
113
    return ResourceHandle::cookieStorageAcceptPolicy() == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain ||
 
114
        ResourceHandle::cookieStorageAcceptPolicy() == CFHTTPCookieStorageAcceptPolicyAlways;
 
115
}
 
116
 
 
117
}