2008-09-16 12:31:35 +02:00
|
|
|
/*
|
|
|
|
* 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 <stdarg.h>
|
2019-11-20 14:29:08 +01:00
|
|
|
#include <wchar.h>
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2019-11-20 14:29:08 +01:00
|
|
|
#include "ws2tcpip.h"
|
2008-09-16 12:31:35 +02:00
|
|
|
#include "winhttp.h"
|
|
|
|
|
2018-08-27 14:36:38 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/list.h"
|
2008-09-16 12:31:35 +02:00
|
|
|
#include "winhttp_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
|
|
|
|
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie
|
2018-10-05 14:58:45 +02:00
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
WCHAR *name;
|
|
|
|
WCHAR *value;
|
|
|
|
WCHAR *path;
|
2018-11-06 16:08:58 +01:00
|
|
|
};
|
2018-10-05 14:58:45 +02:00
|
|
|
|
2018-11-06 16:08:59 +01:00
|
|
|
struct domain
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2018-11-06 16:08:59 +01:00
|
|
|
struct list entry;
|
|
|
|
WCHAR *name;
|
|
|
|
struct list cookies;
|
|
|
|
};
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2018-11-22 21:15:51 +01:00
|
|
|
static struct domain *add_domain( struct session *session, WCHAR *name )
|
2018-11-06 16:08:59 +01:00
|
|
|
{
|
|
|
|
struct domain *domain;
|
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(domain = calloc( 1, sizeof(*domain) ))) return NULL;
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
list_init( &domain->entry );
|
|
|
|
list_init( &domain->cookies );
|
|
|
|
|
2009-10-21 13:19:42 +02:00
|
|
|
domain->name = strdupW( name );
|
2008-09-16 12:31:35 +02:00
|
|
|
list_add_tail( &session->cookie_cache, &domain->entry );
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(domain->name));
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:59 +01:00
|
|
|
static struct cookie *find_cookie( struct domain *domain, const WCHAR *path, const WCHAR *name )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
struct list *item;
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *cookie;
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
LIST_FOR_EACH( item, &domain->cookies )
|
|
|
|
{
|
2018-11-06 16:08:58 +01:00
|
|
|
cookie = LIST_ENTRY( item, struct cookie, entry );
|
2019-11-20 14:29:08 +01:00
|
|
|
if (!wcscmp( cookie->path, path ) && !wcscmp( cookie->name, name ))
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
|
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:59 +01:00
|
|
|
static BOOL domain_match( const WCHAR *name, struct domain *domain, BOOL partial )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if (partial && !wcsstr( name, domain->name )) return FALSE;
|
|
|
|
else if (!partial && wcscmp( name, domain->name )) return FALSE;
|
2008-09-16 12:31:35 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:58 +01:00
|
|
|
static void free_cookie( struct cookie *cookie )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2021-06-08 16:27:44 +02:00
|
|
|
free( cookie->name );
|
|
|
|
free( cookie->value );
|
|
|
|
free( cookie->path );
|
|
|
|
free( cookie );
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:58 +01:00
|
|
|
static void delete_cookie( struct cookie *cookie )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
list_remove( &cookie->entry );
|
|
|
|
free_cookie( cookie );
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:59 +01:00
|
|
|
static void delete_domain( struct domain *domain )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *cookie;
|
2008-09-16 12:31:35 +02:00
|
|
|
struct list *item, *next;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE( item, next, &domain->cookies )
|
|
|
|
{
|
2018-11-06 16:08:58 +01:00
|
|
|
cookie = LIST_ENTRY( item, struct cookie, entry );
|
2008-09-16 12:31:35 +02:00
|
|
|
delete_cookie( cookie );
|
|
|
|
}
|
|
|
|
|
|
|
|
list_remove( &domain->entry );
|
2021-06-08 16:27:44 +02:00
|
|
|
free( domain->name );
|
|
|
|
free( domain );
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 21:15:51 +01:00
|
|
|
void destroy_cookies( struct session *session )
|
2018-11-06 16:08:59 +01:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 21:15:51 +01:00
|
|
|
static BOOL add_cookie( struct session *session, struct cookie *cookie, WCHAR *domain_name, WCHAR *path )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2018-11-06 16:08:59 +01:00
|
|
|
struct domain *domain = NULL;
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *old_cookie;
|
2008-09-16 12:31:35 +02:00
|
|
|
struct list *item;
|
|
|
|
|
2018-10-05 14:59:10 +02:00
|
|
|
if (!(cookie->path = strdupW( path ))) return FALSE;
|
|
|
|
|
|
|
|
EnterCriticalSection( &session->cs );
|
|
|
|
|
2008-09-16 12:31:35 +02:00
|
|
|
LIST_FOR_EACH( item, &session->cookie_cache )
|
|
|
|
{
|
2018-11-06 16:08:59 +01:00
|
|
|
domain = LIST_ENTRY( item, struct domain, entry );
|
2008-09-16 12:31:35 +02:00
|
|
|
if (domain_match( domain_name, domain, FALSE )) break;
|
|
|
|
domain = NULL;
|
|
|
|
}
|
2018-10-05 14:59:10 +02:00
|
|
|
if (!domain) domain = add_domain( session, domain_name );
|
2008-09-16 12:31:35 +02:00
|
|
|
else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
|
|
|
|
|
2018-10-05 14:59:10 +02:00
|
|
|
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));
|
|
|
|
}
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2018-10-05 14:59:10 +02:00
|
|
|
LeaveCriticalSection( &session->cs );
|
|
|
|
return domain != NULL;
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:08:58 +01:00
|
|
|
static struct cookie *parse_cookie( const WCHAR *string )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *cookie;
|
2008-09-16 12:31:35 +02:00
|
|
|
const WCHAR *p;
|
|
|
|
int len;
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if (!(p = wcschr( string, '=' ))) p = string + lstrlenW( string );
|
2015-06-15 17:32:25 +02:00
|
|
|
len = p - string;
|
|
|
|
while (len && string[len - 1] == ' ') len--;
|
|
|
|
if (!len) return NULL;
|
2014-03-25 05:04:45 +01:00
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(cookie = calloc( 1, sizeof(*cookie) ))) return NULL;
|
2014-03-25 05:04:45 +01:00
|
|
|
list_init( &cookie->entry );
|
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(cookie->name = malloc( (len + 1) * sizeof(WCHAR) )))
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2021-06-08 16:27:44 +02:00
|
|
|
free( cookie );
|
2008-09-16 12:31:35 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( cookie->name, string, len * sizeof(WCHAR) );
|
|
|
|
cookie->name[len] = 0;
|
|
|
|
|
2015-06-15 17:32:25 +02:00
|
|
|
if (*p++ == '=')
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2017-10-05 10:00:46 +02:00
|
|
|
while (*p == ' ') p++;
|
2019-11-20 14:29:08 +01:00
|
|
|
len = lstrlenW( p );
|
2015-06-15 17:32:25 +02:00
|
|
|
while (len && p[len - 1] == ' ') len--;
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(cookie->value = malloc( (len + 1) * sizeof(WCHAR) )))
|
2015-06-15 17:32:25 +02:00
|
|
|
{
|
|
|
|
free_cookie( cookie );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( cookie->value, p, len * sizeof(WCHAR) );
|
|
|
|
cookie->value[len] = 0;
|
|
|
|
}
|
2008-09-16 12:31:35 +02:00
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:24:36 +01:00
|
|
|
struct attr
|
|
|
|
{
|
|
|
|
WCHAR *name;
|
|
|
|
WCHAR *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void free_attr( struct attr *attr )
|
|
|
|
{
|
|
|
|
if (!attr) return;
|
2021-06-08 16:27:44 +02:00
|
|
|
free( attr->name );
|
|
|
|
free( attr->value );
|
|
|
|
free( attr );
|
2017-02-21 09:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(attr = malloc( sizeof(*attr) ))) return NULL;
|
|
|
|
if (!(attr->name = malloc( (len + 1) * sizeof(WCHAR) )))
|
2017-02-21 09:24:36 +01:00
|
|
|
{
|
2021-06-08 16:27:44 +02:00
|
|
|
free( attr );
|
2017-02-21 09:24:36 +01:00
|
|
|
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--;
|
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(attr->value = malloc( (len + 1) * sizeof(WCHAR) )))
|
2017-02-21 09:24:36 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-22 21:15:54 +01:00
|
|
|
BOOL set_cookies( struct request *request, const WCHAR *cookies )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
2017-02-21 09:24:36 +01:00
|
|
|
WCHAR *buffer, *p;
|
2008-09-16 12:31:35 +02:00
|
|
|
WCHAR *cookie_domain = NULL, *cookie_path = NULL;
|
2017-02-21 09:24:36 +01:00
|
|
|
struct attr *attr, *domain = NULL, *path = NULL;
|
2018-11-22 21:15:51 +01:00
|
|
|
struct session *session = request->connect->session;
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *cookie;
|
2017-02-21 09:24:36 +01:00
|
|
|
int len, used;
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
len = lstrlenW( cookies );
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(buffer = malloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
|
2019-11-20 14:29:08 +01:00
|
|
|
lstrcpyW( buffer, cookies );
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (*p && *p != ';') p++;
|
|
|
|
if (*p == ';') *p++ = 0;
|
|
|
|
if (!(cookie = parse_cookie( buffer )))
|
|
|
|
{
|
2021-06-08 16:27:44 +02:00
|
|
|
free( buffer );
|
2008-09-16 12:31:35 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-11-20 14:29:08 +01:00
|
|
|
len = lstrlenW( p );
|
2017-02-21 09:24:36 +01:00
|
|
|
while (len && (attr = parse_attr( p, &used )))
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2019-11-22 16:32:24 +01:00
|
|
|
if (!wcsicmp( attr->name, L"domain" ))
|
2017-02-21 09:24:36 +01:00
|
|
|
{
|
|
|
|
domain = attr;
|
|
|
|
cookie_domain = attr->value;
|
|
|
|
}
|
2019-11-22 16:32:24 +01:00
|
|
|
else if (!wcsicmp( attr->name, L"path" ))
|
2017-02-21 09:24:36 +01:00
|
|
|
{
|
|
|
|
path = attr;
|
|
|
|
cookie_path = attr->value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME( "unhandled attribute %s\n", debugstr_w(attr->name) );
|
|
|
|
free_attr( attr );
|
|
|
|
}
|
|
|
|
len -= used;
|
|
|
|
p += used;
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|
|
|
|
if (!cookie_domain && !(cookie_domain = strdupW( request->connect->servername ))) goto end;
|
|
|
|
if (!cookie_path && !(cookie_path = strdupW( request->path ))) goto end;
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((p = wcsrchr( cookie_path, '/' )) && p != cookie_path) *p = 0;
|
2008-09-16 12:31:35 +02:00
|
|
|
ret = add_cookie( session, cookie, cookie_domain, cookie_path );
|
|
|
|
|
|
|
|
end:
|
2009-10-21 13:19:42 +02:00
|
|
|
if (!ret) free_cookie( cookie );
|
2017-02-21 09:24:36 +01:00
|
|
|
if (domain) free_attr( domain );
|
2021-06-08 16:27:44 +02:00
|
|
|
else free( cookie_domain );
|
2017-02-21 09:24:36 +01:00
|
|
|
if (path) free_attr( path );
|
2021-06-08 16:27:44 +02:00
|
|
|
else free( cookie_path );
|
|
|
|
free( buffer );
|
2008-09-16 12:31:35 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:22:04 +02:00
|
|
|
DWORD add_cookie_headers( struct request *request )
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
|
|
|
struct list *domain_cursor;
|
2018-11-22 21:15:51 +01:00
|
|
|
struct session *session = request->connect->session;
|
2020-04-07 10:22:04 +02:00
|
|
|
DWORD ret = ERROR_SUCCESS;
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2018-10-05 14:59:10 +02:00
|
|
|
EnterCriticalSection( &session->cs );
|
|
|
|
|
2008-09-16 12:31:35 +02:00
|
|
|
LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
|
|
|
|
{
|
2018-11-06 16:08:59 +01:00
|
|
|
struct domain *domain = LIST_ENTRY( domain_cursor, struct domain, entry );
|
2008-09-16 12:31:35 +02:00
|
|
|
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 )
|
|
|
|
{
|
2018-11-06 16:08:58 +01:00
|
|
|
struct cookie *cookie = LIST_ENTRY( cookie_cursor, struct cookie, entry );
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if (wcsstr( request->path, cookie->path ) == request->path)
|
2008-09-16 12:31:35 +02:00
|
|
|
{
|
2018-11-12 04:52:13 +01:00
|
|
|
static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
|
2019-11-20 14:29:08 +01:00
|
|
|
int len, len_cookie = ARRAY_SIZE( cookieW ), len_name = lstrlenW( cookie->name );
|
2008-09-16 12:31:35 +02:00
|
|
|
WCHAR *header;
|
|
|
|
|
2015-06-15 17:32:25 +02:00
|
|
|
len = len_cookie + len_name;
|
2019-11-20 14:29:08 +01:00
|
|
|
if (cookie->value) len += lstrlenW( cookie->value ) + 1;
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(header = malloc( (len + 1) * sizeof(WCHAR) )))
|
2018-10-05 14:59:10 +02:00
|
|
|
{
|
|
|
|
LeaveCriticalSection( &session->cs );
|
2020-04-07 10:22:04 +02:00
|
|
|
return ERROR_OUTOFMEMORY;
|
2018-10-05 14:59:10 +02:00
|
|
|
}
|
2008-09-16 12:31:35 +02:00
|
|
|
|
2015-06-15 17:32:25 +02:00
|
|
|
memcpy( header, cookieW, len_cookie * sizeof(WCHAR) );
|
2019-11-20 14:29:08 +01:00
|
|
|
lstrcpyW( header + len_cookie, cookie->name );
|
2015-06-15 17:32:25 +02:00
|
|
|
if (cookie->value)
|
|
|
|
{
|
|
|
|
header[len_cookie + len_name] = '=';
|
2019-11-20 14:29:08 +01:00
|
|
|
lstrcpyW( header + len_cookie + len_name + 1, cookie->value );
|
2015-06-15 17:32:25 +02:00
|
|
|
}
|
2008-09-16 12:31:35 +02:00
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(header));
|
2020-04-07 10:22:04 +02:00
|
|
|
ret = add_request_headers( request, header, len,
|
|
|
|
WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON );
|
2021-06-08 16:27:44 +02:00
|
|
|
free( header );
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 14:59:10 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection( &session->cs );
|
2020-04-07 10:22:04 +02:00
|
|
|
return ret;
|
2008-09-16 12:31:35 +02:00
|
|
|
}
|