cookie.c 26.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Wininet - cookie handling stuff
 *
 * Copyright 2002 TransGaming Technologies Inc.
 *
 * David Hammerton
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23
 */

#include "config.h"
24
#include "wine/port.h"
25

26 27 28 29
#if defined(__MINGW32__) || defined (_MSC_VER)
#include <ws2tcpip.h>
#endif

30
#include <stdarg.h>
31 32 33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
34 35 36
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include "windef.h"
#include "winbase.h"
#include "wininet.h"
#include "winerror.h"

#include "wine/debug.h"
#include "internet.h"

#define RESPONSE_TIMEOUT        30            /* FROM internet.c */


WINE_DEFAULT_DEBUG_CHANNEL(wininet);

/* FIXME
 *     Cookies are currently memory only.
 *     Cookies are NOT THREAD SAFE
54
 *     Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
55 56 57 58 59 60 61
 */

typedef struct _cookie_domain cookie_domain;
typedef struct _cookie cookie;

struct _cookie
{
62
    struct list entry;
63 64 65

    struct _cookie_domain *parent;

66 67
    LPWSTR lpCookieName;
    LPWSTR lpCookieData;
68
    FILETIME expiry;
69 70 71 72
};

struct _cookie_domain
{
73
    struct list entry;
74

75 76
    LPWSTR lpCookieDomain;
    LPWSTR lpCookiePath;
77
    struct list cookie_list;
78 79
};

80
static struct list domain_list = LIST_INIT(domain_list);
81

82
static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data, FILETIME expiry);
83
static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
84
static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
85
static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
86 87 88 89
static void COOKIE_deleteDomain(cookie_domain *deadDomain);


/* adds a cookie to the domain */
90
static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data, FILETIME expiry)
91 92 93
{
    cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));

94
    list_init(&newCookie->entry);
95 96
    newCookie->lpCookieName = NULL;
    newCookie->lpCookieData = NULL;
97
    newCookie->expiry = expiry;
98 99 100

    if (name)
    {
101 102
	newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
        lstrcpyW(newCookie->lpCookieName, name);
103 104 105
    }
    if (data)
    {
106 107
	newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
        lstrcpyW(newCookie->lpCookieData, data);
108 109
    }

110
    TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
111

112
    list_add_tail(&domain->cookie_list, &newCookie->entry);
113 114 115 116 117 118
    newCookie->parent = domain;
    return newCookie;
}


/* finds a cookie in the domain matching the cookie name */
119
static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
120
{
121
    struct list * cursor;
122
    TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
123

124
    LIST_FOR_EACH(cursor, &domain->cookie_list)
125
    {
126
        cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
127 128 129 130 131
	BOOL candidate = TRUE;
	if (candidate && lpszCookieName)
	{
	    if (candidate && !searchCookie->lpCookieName)
		candidate = FALSE;
132
	    if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
133 134 135 136 137 138 139 140 141 142 143
                candidate = FALSE;
	}
	if (candidate)
	    return searchCookie;
    }
    return NULL;
}

/* removes a cookie from the list, if its the last cookie we also remove the domain */
static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
{
144 145
    HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
    HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
146
    list_remove(&deadCookie->entry);
147

148 149 150 151
    /* special case: last cookie, lets remove the domain to save memory */
    if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
        COOKIE_deleteDomain(deadCookie->parent);
    HeapFree(GetProcessHeap(), 0, deadCookie);
152 153 154
}

/* allocates a domain and adds it to the end */
155
static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
156 157 158
{
    cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));

159 160
    list_init(&newDomain->entry);
    list_init(&newDomain->cookie_list);
161 162 163 164 165
    newDomain->lpCookieDomain = NULL;
    newDomain->lpCookiePath = NULL;

    if (domain)
    {
166 167
	newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
        strcpyW(newDomain->lpCookieDomain, domain);
168 169 170
    }
    if (path)
    {
171 172
	newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
        lstrcpyW(newDomain->lpCookiePath, path);
173 174
    }

175 176
    list_add_tail(&domain_list, &newDomain->entry);

177 178 179 180
    TRACE("Adding domain: %p\n", newDomain);
    return newDomain;
}

181
static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
182
{
183
    URL_COMPONENTSW UrlComponents;
184
    BOOL rc;
185 186 187 188 189 190 191

    UrlComponents.lpszExtraInfo = NULL;
    UrlComponents.lpszPassword = NULL;
    UrlComponents.lpszScheme = NULL;
    UrlComponents.lpszUrlPath = path;
    UrlComponents.lpszUserName = NULL;
    UrlComponents.lpszHostName = hostName;
192 193 194 195
    UrlComponents.dwExtraInfoLength = 0;
    UrlComponents.dwPasswordLength = 0;
    UrlComponents.dwSchemeLength = 0;
    UrlComponents.dwUserNameLength = 0;
196 197
    UrlComponents.dwHostNameLength = hostNameLen;
    UrlComponents.dwUrlPathLength = pathLen;
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    rc = InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);

    /* discard the webpage off the end of the path */
    if (pathLen > 0 && path[pathLen-1] != '/')
    {
        LPWSTR ptr;
        ptr = strrchrW(path,'/');
        if (ptr)
            *(++ptr) = 0;
        else
        {
            path[0] = '/';
            path[1] = 0;
        }
    }
    return rc;
215 216
}

217 218 219
/* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
                               cookie_domain *searchDomain, BOOL allow_partial)
220
{
221 222
    TRACE("searching on domain %p\n", searchDomain);
	if (lpszCookieDomain)
223
	{
224 225
	    if (!searchDomain->lpCookieDomain)
            return FALSE;
226

227
	    TRACE("comparing domain %s with %s\n", 
228 229 230 231 232 233 234
            debugstr_w(lpszCookieDomain), 
            debugstr_w(searchDomain->lpCookieDomain));

        if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
            return FALSE;
        else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
            return FALSE;
235
 	}
236 237
    if (lpszCookiePath)
    {
238
        INT len;
239
        TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
240 241 242
        /* paths match at the beginning.  so a path of  /foo would match
         * /foobar and /foo/bar
         */
243 244
        if (!searchDomain->lpCookiePath)
            return FALSE;
245 246 247 248 249 250 251
        if (allow_partial)
        {
            len = lstrlenW(searchDomain->lpCookiePath);
            if (strncmpiW(searchDomain->lpCookiePath, lpszCookiePath, len)!=0)
                return FALSE;
        }
        else if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
252
            return FALSE;
253

254
	}
255
	return TRUE;
256 257 258 259 260
}

/* remove a domain from the list and delete it */
static void COOKIE_deleteDomain(cookie_domain *deadDomain)
{
261 262 263 264 265 266 267
    struct list * cursor;
    while ((cursor = list_tail(&deadDomain->cookie_list)))
    {
        COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
        list_remove(cursor);
    }

268 269
    HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
    HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
270

271 272
    list_remove(&deadDomain->entry);

273 274 275 276
    HeapFree(GetProcessHeap(), 0, deadDomain);
}

/***********************************************************************
277
 *           InternetGetCookieW (WININET.@)
278 279 280 281 282 283 284 285 286 287 288
 *
 * Retrieve cookie from the specified url
 *
 *  It should be noted that on windows the lpszCookieName parameter is "not implemented".
 *    So it won't be implemented here.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
289 290
BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
    LPWSTR lpCookieData, LPDWORD lpdwSize)
291
{
292
    BOOL ret;
293
    struct list * cursor;
294
    unsigned int cnt = 0, domain_count = 0, cookie_count = 0;
295
    WCHAR hostName[2048], path[2048];
296
    FILETIME tm;
297

298
    TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
299
          lpCookieData, lpdwSize);
300

301 302
    if (!lpszUrl)
    {
303
        SetLastError(ERROR_INVALID_PARAMETER);
304 305 306
        return FALSE;
    }

307 308 309
    hostName[0] = 0;
    ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
    if (!ret || !hostName[0]) return FALSE;
310

311 312
    GetSystemTimeAsFileTime(&tm);

313
    LIST_FOR_EACH(cursor, &domain_list)
314
    {
315
        cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
316
        if (COOKIE_matchDomain(hostName, path, cookiesDomain, TRUE))
317 318 319 320 321 322 323 324
        {
            struct list * cursor;
            domain_count++;
            TRACE("found domain %p\n", cookiesDomain);
    
            LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
            {
                cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
325 326 327 328 329 330 331 332
                /* check for expiry */
                if ((thisCookie->expiry.dwLowDateTime != 0 || thisCookie->expiry.dwHighDateTime != 0) && CompareFileTime(&tm,&thisCookie->expiry)  > 0)
                {
                    TRACE("Found expired cookie. deleting\n");
                    COOKIE_deleteCookie(thisCookie, FALSE);
                    continue;
                }

333 334
                if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
                {
335 336 337
                    unsigned int len;

                    if (cookie_count) cnt += 2; /* '; ' */
338
                    cnt += strlenW(thisCookie->lpCookieName);
339 340 341 342 343
                    if ((len = strlenW(thisCookie->lpCookieData)))
                    {
                        cnt += 1; /* = */
                        cnt += len;
                    }
344 345 346 347
                }
                else
                {
                    static const WCHAR szsc[] = { ';',' ',0 };
348 349 350 351 352 353 354 355 356 357
                    static const WCHAR szname[] = { '%','s',0 };
                    static const WCHAR szdata[] = { '=','%','s',0 };

                    if (cookie_count) cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
                    cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szname, thisCookie->lpCookieName);

                    if (thisCookie->lpCookieData[0])
                        cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szdata, thisCookie->lpCookieData);

                    TRACE("Cookie: %s\n", debugstr_w(lpCookieData));
358 359 360 361
                }
                cookie_count++;
            }
        }
362
    }
363 364 365 366 367 368 369 370

    if (!domain_count)
    {
        TRACE("no cookies found for %s\n", debugstr_w(hostName));
        SetLastError(ERROR_NO_MORE_ITEMS);
        return FALSE;
    }

371 372
    if (lpCookieData == NULL)
    {
373 374 375
        *lpdwSize = (cnt + 1) * sizeof(WCHAR);
        TRACE("returning %u\n", *lpdwSize);
        return TRUE;
376 377
    }

378
    *lpdwSize = cnt + 1;
379

380
    TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count,
381
           debugstr_w(lpCookieData));
382 383 384 385 386 387

    return (cnt ? TRUE : FALSE);
}


/***********************************************************************
388
 *           InternetGetCookieA (WININET.@)
389 390 391 392 393 394 395 396
 *
 * Retrieve cookie from the specified url
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
397 398
BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
    LPSTR lpCookieData, LPDWORD lpdwSize)
399
{
400 401 402 403 404
    DWORD len;
    LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
    BOOL r;

    TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
405
        lpCookieData);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

    if( lpszUrl )
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
        szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
    }

    if( lpszCookieName )
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
        szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
    }

    r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
    if( r )
    {
        szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        if( !szCookieData )
426 427 428 429 430 431
        {
            r = FALSE;
        }
        else
        {
            r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
432

433 434 435
            *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
                                    lpCookieData, *lpdwSize, NULL, NULL );
        }
436 437
    }

438 439 440
    HeapFree( GetProcessHeap(), 0, szCookieData );
    HeapFree( GetProcessHeap(), 0, szCookieName );
    HeapFree( GetProcessHeap(), 0, szUrl );
441 442

    return r;
443 444
}

445 446 447 448 449
static BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cookie_data)
{
    cookie_domain *thisCookieDomain = NULL;
    cookie *thisCookie;
    struct list *cursor;
450
    LPWSTR data, value;
451
    WCHAR *ptr;
452 453
    FILETIME expiry;
    BOOL expired = FALSE;
454

455
    value = data = HeapAlloc(GetProcessHeap(), 0, (strlenW(cookie_data) + 1) * sizeof(WCHAR));
456
    strcpyW(data,cookie_data);
457
    memset(&expiry,0,sizeof(expiry));
458

459
    /* lots of information can be parsed out of the cookie value */
460 461 462 463 464 465 466 467 468 469 470 471

    ptr = data;
    for (;;)
    {
        static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
        static const WCHAR szPath[] = {'p','a','t','h','=',0};
        static const WCHAR szExpires[] = {'e','x','p','i','r','e','s','=',0};
        static const WCHAR szSecure[] = {'s','e','c','u','r','e',0};
        static const WCHAR szHttpOnly[] = {'h','t','t','p','o','n','l','y',0};

        if (!(ptr = strchrW(ptr,';'))) break;
        *ptr++ = 0;
472 473 474 475

        value = HeapAlloc(GetProcessHeap(), 0, (ptr - data) * sizeof(WCHAR));
        strcpyW(value, data);

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
        while (*ptr == ' ') ptr++; /* whitespace */

        if (strncmpiW(ptr, szDomain, 7) == 0)
        {
            ptr+=strlenW(szDomain);
            domain = ptr;
            TRACE("Parsing new domain %s\n",debugstr_w(domain));
        }
        else if (strncmpiW(ptr, szPath, 5) == 0)
        {
            ptr+=strlenW(szPath);
            path = ptr;
            TRACE("Parsing new path %s\n",debugstr_w(path));
        }
        else if (strncmpiW(ptr, szExpires, 8) == 0)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
        {
            FILETIME ft;
            SYSTEMTIME st;
            FIXME("persistent cookies not handled (%s)\n",debugstr_w(ptr));
            ptr+=strlenW(szExpires);
            if (InternetTimeToSystemTimeW(ptr, &st, 0))
            {
                SystemTimeToFileTime(&st, &expiry);
                GetSystemTimeAsFileTime(&ft);

                if (CompareFileTime(&ft,&expiry) > 0)
                {
                    TRACE("Cookie already expired.\n");
                    expired = TRUE;
                }
            }
        }
508
        else if (strncmpiW(ptr, szSecure, 6) == 0)
509
        {
510
            FIXME("secure not handled (%s)\n",debugstr_w(ptr));
511 512
            ptr += strlenW(szSecure);
        }
513
        else if (strncmpiW(ptr, szHttpOnly, 8) == 0)
514
        {
515
            FIXME("httponly not handled (%s)\n",debugstr_w(ptr));
516 517 518 519
            ptr += strlenW(szHttpOnly);
        }
        else if (*ptr)
        {
520
            FIXME("Unknown additional option %s\n",debugstr_w(ptr));
521 522
            break;
        }
523
    }
524 525 526 527

    LIST_FOR_EACH(cursor, &domain_list)
    {
        thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
528
        if (COOKIE_matchDomain(domain, path, thisCookieDomain, FALSE))
529 530 531 532 533
            break;
        thisCookieDomain = NULL;
    }

    if (!thisCookieDomain)
534 535 536 537 538 539
    {
        if (!expired)
            thisCookieDomain = COOKIE_addDomain(domain, path);
        else
        {
            HeapFree(GetProcessHeap(),0,data);
540
            if (value != data) HeapFree(GetProcessHeap(), 0, value);
541 542 543
            return TRUE;
        }
    }
544 545 546 547

    if ((thisCookie = COOKIE_findCookie(thisCookieDomain, cookie_name)))
        COOKIE_deleteCookie(thisCookie, FALSE);

548
    TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name),
549
          debugstr_w(value), debugstr_w(thisCookieDomain->lpCookieDomain),debugstr_w(thisCookieDomain->lpCookiePath));
550

551
    if (!expired && !COOKIE_addCookie(thisCookieDomain, cookie_name, value, expiry))
552 553
    {
        HeapFree(GetProcessHeap(),0,data);
554
        if (value != data) HeapFree(GetProcessHeap(), 0, value);
555
        return FALSE;
556
    }
557

558
    HeapFree(GetProcessHeap(),0,data);
559
    if (value != data) HeapFree(GetProcessHeap(), 0, value);
560 561
    return TRUE;
}
562 563

/***********************************************************************
564
 *           InternetSetCookieW (WININET.@)
565 566 567 568 569 570 571 572
 *
 * Sets cookie for the specified url
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
573 574
BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
    LPCWSTR lpCookieData)
575
{
576
    BOOL ret;
577
    WCHAR hostName[2048], path[2048];
578

579 580
    TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
        debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
581

582
    if (!lpszUrl || !lpCookieData)
583
    {
584
        SetLastError(ERROR_INVALID_PARAMETER);
585
        return FALSE;
586
    }
587

588
    hostName[0] = path[0] = 0;
589 590 591
    ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
    if (!ret || !hostName[0]) return FALSE;

592 593
    if (!lpszCookieName)
    {
594 595
        unsigned int len;
        WCHAR *cookie, *data;
596

597 598 599 600 601 602 603
        len = strlenW(lpCookieData);
        if (!(cookie = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR))))
        {
            SetLastError(ERROR_OUTOFMEMORY);
            return FALSE;
        }
        strcpyW(cookie, lpCookieData);
604

605 606 607 608
        /* some apps (or is it us??) try to add a cookie with no cookie name, but
         * the cookie data in the form of name[=data].
         */
        if (!(data = strchrW(cookie, '='))) data = cookie + len;
609
        else *data++ = 0;
610

611
        ret = set_cookie(hostName, path, cookie, data);
612

613 614 615 616
        HeapFree(GetProcessHeap(), 0, cookie);
        return ret;
    }
    return set_cookie(hostName, path, lpszCookieName, lpCookieData);
617 618 619 620
}


/***********************************************************************
621
 *           InternetSetCookieA (WININET.@)
622 623 624 625 626 627 628 629
 *
 * Sets cookie for the specified url
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
630 631
BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
    LPCSTR lpCookieData)
632
{
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    DWORD len;
    LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
    BOOL r;

    TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
        debugstr_a(lpszCookieName), debugstr_a(lpCookieData));

    if( lpszUrl )
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
        szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
    }

    if( lpszCookieName )
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
        szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
    }

    if( lpCookieData )
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
        szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
        MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
    }

    r = InternetSetCookieW( szUrl, szCookieName, szCookieData );

663 664 665
    HeapFree( GetProcessHeap(), 0, szCookieData );
    HeapFree( GetProcessHeap(), 0, szCookieName );
    HeapFree( GetProcessHeap(), 0, szUrl );
666 667

    return r;
668
}
669 670 671 672 673 674 675 676 677

/***********************************************************************
 *           InternetSetCookieExA (WININET.@)
 *
 * See InternetSetCookieExW.
 */
DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
                                   DWORD dwFlags, DWORD_PTR dwReserved)
{
678
    TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
679 680
          debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
          dwFlags, dwReserved);
681 682 683

    if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
    return InternetSetCookieA(lpszURL, lpszCookieName, lpszCookieData);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
}

/***********************************************************************
 *           InternetSetCookieExW (WININET.@)
 *
 * Sets a cookie for the specified URL.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
                                   DWORD dwFlags, DWORD_PTR dwReserved)
{
699
    TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
700 701
          debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
          dwFlags, dwReserved);
702 703 704

    if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
    return InternetSetCookieW(lpszURL, lpszCookieName, lpszCookieData);
705 706 707 708 709 710 711 712 713 714
}

/***********************************************************************
 *           InternetGetCookieExA (WININET.@)
 *
 * See InternetGetCookieExW.
 */
BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
                                  LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
{
715
    TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
716 717
          debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
          pcchCookieData, dwFlags, lpReserved);
718 719 720

    if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
    return InternetGetCookieA(pchURL, pchCookieName, pchCookieData, pcchCookieData);
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
}

/***********************************************************************
 *           InternetGetCookieExW (WININET.@)
 *
 * Retrieve cookie for the specified URL.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
                                  LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
{
736
    TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
737 738
          debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
          pcchCookieData, dwFlags, lpReserved);
739 740 741

    if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
    return InternetGetCookieW(pchURL, pchCookieName, pchCookieData, pcchCookieData);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
}

/***********************************************************************
 *           InternetClearAllPerSiteCookieDecisions (WININET.@)
 *
 * Clears all per-site decisions about cookies.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
{
    FIXME("stub\n");
    return TRUE;
}

/***********************************************************************
 *           InternetEnumPerSiteCookieDecisionA (WININET.@)
 *
 * See InternetEnumPerSiteCookieDecisionW.
 */
765 766
BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
                                                ULONG *pdwDecision, ULONG dwIndex )
767
{
768
    FIXME("(%s, %p, %p, 0x%08x) stub\n",
769 770 771 772 773 774 775 776 777 778 779 780 781 782
          debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
    return FALSE;
}

/***********************************************************************
 *           InternetEnumPerSiteCookieDecisionW (WININET.@)
 *
 * Enumerates all per-site decisions about cookies.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
783 784
BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
                                                ULONG *pdwDecision, ULONG dwIndex )
785
{
786
    FIXME("(%s, %p, %p, 0x%08x) stub\n",
787 788 789 790 791 792 793
          debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
    return FALSE;
}

/***********************************************************************
 *           InternetGetPerSiteCookieDecisionA (WININET.@)
 */
794
BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
795 796 797 798 799 800 801 802
{
    FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
    return FALSE;
}

/***********************************************************************
 *           InternetGetPerSiteCookieDecisionW (WININET.@)
 */
803
BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
804 805 806 807 808 809 810 811 812 813
{
    FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
    return FALSE;
}

/***********************************************************************
 *           InternetSetPerSiteCookieDecisionA (WININET.@)
 */
BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
{
814
    FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
815 816 817 818 819 820 821 822
    return FALSE;
}

/***********************************************************************
 *           InternetSetPerSiteCookieDecisionW (WININET.@)
 */
BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
{
823
    FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
824 825
    return FALSE;
}
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

/***********************************************************************
 *           IsDomainLegalCookieDomainW (WININET.@)
 */
BOOL WINAPI IsDomainLegalCookieDomainW( LPCWSTR s1, LPCWSTR s2 )
{
    const WCHAR *p;

    FIXME("(%s, %s)\n", debugstr_w(s1), debugstr_w(s2));

    if (!s1 || !s2)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
    if (s1[0] == '.' || !s1[0] || s2[0] == '.' || !s2[0])
    {
        SetLastError(ERROR_INVALID_NAME);
        return FALSE;
    }
    if (!(p = strchrW(s2, '.'))) return FALSE;
    if (strchrW(p + 1, '.') && !strcmpW(p + 1, s1)) return TRUE;
    else if (!strcmpW(s1, s2)) return TRUE;
    return FALSE;
}