cookie.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2008 Hans Leidekker for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
20
#include "ws2tcpip.h"
21 22 23 24 25 26
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winhttp.h"

27 28
#include "wine/debug.h"
#include "wine/list.h"
29 30 31 32
#include "winhttp_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(winhttp);

33
struct cookie
34 35 36 37 38
{
    struct list entry;
    WCHAR *name;
    WCHAR *value;
    WCHAR *path;
39
};
40

41
struct domain
42
{
43 44 45 46
    struct list entry;
    WCHAR *name;
    struct list cookies;
};
47

48 49 50 51 52
static struct domain *add_domain( session_t *session, WCHAR *name )
{
    struct domain *domain;

    if (!(domain = heap_alloc_zero( sizeof(struct domain) ))) return NULL;
53 54 55 56

    list_init( &domain->entry );
    list_init( &domain->cookies );

57
    domain->name = strdupW( name );
58 59 60 61 62 63
    list_add_tail( &session->cookie_cache, &domain->entry );

    TRACE("%s\n", debugstr_w(domain->name));
    return domain;
}

64
static struct cookie *find_cookie( struct domain *domain, const WCHAR *path, const WCHAR *name )
65 66
{
    struct list *item;
67
    struct cookie *cookie;
68 69 70

    LIST_FOR_EACH( item, &domain->cookies )
    {
71
        cookie = LIST_ENTRY( item, struct cookie, entry );
72
        if (!strcmpW( cookie->path, path ) && !strcmpW( cookie->name, name ))
73 74 75 76 77 78 79 80
        {
            TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
            return cookie;
         }
    }
    return NULL;
}

81
static BOOL domain_match( const WCHAR *name, struct domain *domain, BOOL partial )
82 83 84 85 86 87 88 89
{
    TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));

    if (partial && !strstrW( name, domain->name )) return FALSE;
    else if (!partial && strcmpW( name, domain->name )) return FALSE;
    return TRUE;
}

90
static void free_cookie( struct cookie *cookie )
91 92 93 94 95 96 97
{
    heap_free( cookie->name );
    heap_free( cookie->value );
    heap_free( cookie->path );
    heap_free( cookie );
}

98
static void delete_cookie( struct cookie *cookie )
99 100 101 102 103
{
    list_remove( &cookie->entry );
    free_cookie( cookie );
}

104
static void delete_domain( struct domain *domain )
105
{
106
    struct cookie *cookie;
107 108 109 110
    struct list *item, *next;

    LIST_FOR_EACH_SAFE( item, next, &domain->cookies )
    {
111
        cookie = LIST_ENTRY( item, struct cookie, entry );
112 113 114 115 116 117 118 119
        delete_cookie( cookie );
    }

    list_remove( &domain->entry );
    heap_free( domain->name );
    heap_free( domain );
}

120 121 122 123 124 125 126 127 128 129 130 131
void destroy_cookies( session_t *session )
{
    struct list *item, *next;
    struct domain *domain;

    LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
    {
        domain = LIST_ENTRY( item, struct domain, entry );
        delete_domain( domain );
    }
}

132
static BOOL add_cookie( session_t *session, struct cookie *cookie, WCHAR *domain_name, WCHAR *path )
133
{
134
    struct domain *domain = NULL;
135
    struct cookie *old_cookie;
136 137
    struct list *item;

138 139 140 141
    if (!(cookie->path = strdupW( path ))) return FALSE;

    EnterCriticalSection( &session->cs );

142 143
    LIST_FOR_EACH( item, &session->cookie_cache )
    {
144
        domain = LIST_ENTRY( item, struct domain, entry );
145 146 147
        if (domain_match( domain_name, domain, FALSE )) break;
        domain = NULL;
    }
148
    if (!domain) domain = add_domain( session, domain_name );
149 150
    else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );

151 152 153 154 155 156
    if (domain)
    {
        list_add_head( &domain->cookies, &cookie->entry );
        TRACE("domain %s path %s <- %s=%s\n", debugstr_w(domain_name), debugstr_w(cookie->path),
              debugstr_w(cookie->name), debugstr_w(cookie->value));
    }
157

158 159
    LeaveCriticalSection( &session->cs );
    return domain != NULL;
160 161
}

162
static struct cookie *parse_cookie( const WCHAR *string )
163
{
164
    struct cookie *cookie;
165 166 167
    const WCHAR *p;
    int len;

168 169 170 171
    if (!(p = strchrW( string, '=' ))) p = string + strlenW( string );
    len = p - string;
    while (len && string[len - 1] == ' ') len--;
    if (!len) return NULL;
172

173
    if (!(cookie = heap_alloc_zero( sizeof(struct cookie) ))) return NULL;
174 175
    list_init( &cookie->entry );

176 177 178 179 180 181 182 183
    if (!(cookie->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
    {
        heap_free( cookie );
        return NULL;
    }
    memcpy( cookie->name, string, len * sizeof(WCHAR) );
    cookie->name[len] = 0;

184
    if (*p++ == '=')
185
    {
186
        while (*p == ' ') p++;
187 188
        len = strlenW( p );
        while (len && p[len - 1] == ' ') len--;
189

190 191 192 193 194 195 196 197
        if (!(cookie->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
        {
            free_cookie( cookie );
            return NULL;
        }
        memcpy( cookie->value, p, len * sizeof(WCHAR) );
        cookie->value[len] = 0;
    }
198 199 200
    return cookie;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
struct attr
{
    WCHAR *name;
    WCHAR *value;
};

static void free_attr( struct attr *attr )
{
    if (!attr) return;
    heap_free( attr->name );
    heap_free( attr->value );
    heap_free( attr );
}

static struct attr *parse_attr( const WCHAR *str, int *used )
{
    const WCHAR *p = str, *q;
    struct attr *attr;
    int len;

    while (*p == ' ') p++;
    q = p;
    while (*q && *q != ' ' && *q != '=' && *q != ';') q++;
    len = q - p;
    if (!len) return NULL;

    if (!(attr = heap_alloc( sizeof(struct attr) ))) return NULL;
    if (!(attr->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
    {
        heap_free( attr );
        return NULL;
    }
    memcpy( attr->name, p, len * sizeof(WCHAR) );
    attr->name[len] = 0;
    attr->value = NULL;

    p = q;
    while (*p == ' ') p++;
    if (*p++ == '=')
    {
        while (*p == ' ') p++;
        q = p;
        while (*q && *q != ';') q++;
        len = q - p;
        while (len && p[len - 1] == ' ') len--;

        if (!(attr->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
        {
            free_attr( attr );
            return NULL;
        }
        memcpy( attr->value, p, len * sizeof(WCHAR) );
        attr->value[len] = 0;
    }

    while (*q == ' ') q++;
    if (*q == ';') q++;
    *used = q - str;

    return attr;
}

263 264 265 266 267
BOOL set_cookies( request_t *request, const WCHAR *cookies )
{
    static const WCHAR pathW[] = {'p','a','t','h',0};
    static const WCHAR domainW[] = {'d','o','m','a','i','n',0};
    BOOL ret = FALSE;
268
    WCHAR *buffer, *p;
269
    WCHAR *cookie_domain = NULL, *cookie_path = NULL;
270
    struct attr *attr, *domain = NULL, *path = NULL;
271
    session_t *session = request->connect->session;
272
    struct cookie *cookie;
273
    int len, used;
274 275 276 277 278 279 280 281 282 283 284 285 286

    len = strlenW( cookies );
    if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
    strcpyW( buffer, cookies );

    p = buffer;
    while (*p && *p != ';') p++;
    if (*p == ';') *p++ = 0;
    if (!(cookie = parse_cookie( buffer )))
    {
        heap_free( buffer );
        return FALSE;
    }
287 288
    len = strlenW( p );
    while (len && (attr = parse_attr( p, &used )))
289
    {
290
        if (!strcmpiW( attr->name, domainW ))
291 292 293 294
        {
            domain = attr;
            cookie_domain = attr->value;
        }
295
        else if (!strcmpiW( attr->name, pathW ))
296 297 298 299 300 301 302 303 304 305 306
        {
            path = attr;
            cookie_path = attr->value;
        }
        else
        {
            FIXME( "unhandled attribute %s\n", debugstr_w(attr->name) );
            free_attr( attr );
        }
        len -= used;
        p += used;
307 308 309 310 311 312 313 314
    }
    if (!cookie_domain && !(cookie_domain = strdupW( request->connect->servername ))) goto end;
    if (!cookie_path && !(cookie_path = strdupW( request->path ))) goto end;

    if ((p = strrchrW( cookie_path, '/' )) && p != cookie_path) *p = 0;
    ret = add_cookie( session, cookie, cookie_domain, cookie_path );

end:
315
    if (!ret) free_cookie( cookie );
316 317 318 319
    if (domain) free_attr( domain );
    else heap_free( cookie_domain );
    if (path) free_attr( path );
    else heap_free( cookie_path );
320 321 322 323 324 325 326 327 328
    heap_free( buffer );
    return ret;
}

BOOL add_cookie_headers( request_t *request )
{
    struct list *domain_cursor;
    session_t *session = request->connect->session;

329 330
    EnterCriticalSection( &session->cs );

331 332
    LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
    {
333
        struct domain *domain = LIST_ENTRY( domain_cursor, struct domain, entry );
334 335 336 337 338 339 340
        if (domain_match( request->connect->servername, domain, TRUE ))
        {
            struct list *cookie_cursor;
            TRACE("found domain %s\n", debugstr_w(domain->name));

            LIST_FOR_EACH( cookie_cursor, &domain->cookies )
            {
341
                struct cookie *cookie = LIST_ENTRY( cookie_cursor, struct cookie, entry );
342 343 344 345 346

                TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));

                if (strstrW( request->path, cookie->path ) == request->path)
                {
347
                    static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
348
                    int len, len_cookie = ARRAY_SIZE( cookieW ), len_name = strlenW( cookie->name );
349 350
                    WCHAR *header;

351 352
                    len = len_cookie + len_name;
                    if (cookie->value) len += strlenW( cookie->value ) + 1;
353 354 355 356 357
                    if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) )))
                    {
                        LeaveCriticalSection( &session->cs );
                        return FALSE;
                    }
358

359 360 361 362 363 364 365
                    memcpy( header, cookieW, len_cookie * sizeof(WCHAR) );
                    strcpyW( header + len_cookie, cookie->name );
                    if (cookie->value)
                    {
                        header[len_cookie + len_name] = '=';
                        strcpyW( header + len_cookie + len_name + 1, cookie->value );
                    }
366 367

                    TRACE("%s\n", debugstr_w(header));
368 369
                    add_request_headers( request, header, len,
                                         WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON );
370 371 372 373 374
                    heap_free( header );
                }
            }
        }
    }
375 376

    LeaveCriticalSection( &session->cs );
377 378
    return TRUE;
}