winhttp: Manage a session global cookie cache.

This commit is contained in:
Hans Leidekker 2008-09-16 12:31:35 +02:00 committed by Alexandre Julliard
parent 4ae5741c4e
commit 4a706aae70
5 changed files with 324 additions and 7 deletions

View File

@ -8,6 +8,7 @@ IMPORTS = wininet kernel32
DELAYIMPORTS = crypt32
C_SRCS = \
cookie.c \
handle.c \
main.c \
net.c \

281
dlls/winhttp/cookie.c Normal file
View File

@ -0,0 +1,281 @@
/*
* 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"
#include <stdarg.h>
#include "wine/debug.h"
#include "wine/list.h"
#include "windef.h"
#include "winbase.h"
#include "winhttp.h"
#include "winhttp_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
static domain_t *add_domain( session_t *session, WCHAR *name )
{
domain_t *domain;
if (!(domain = heap_alloc_zero( sizeof(domain_t) ))) return NULL;
list_init( &domain->entry );
list_init( &domain->cookies );
domain->name = name;
list_add_tail( &session->cookie_cache, &domain->entry );
TRACE("%s\n", debugstr_w(domain->name));
return domain;
}
static cookie_t *find_cookie( domain_t *domain, const WCHAR *path, const WCHAR *name )
{
struct list *item;
cookie_t *cookie;
LIST_FOR_EACH( item, &domain->cookies )
{
cookie = LIST_ENTRY( item, cookie_t, entry );
if (!strcmpW( cookie->path, path ) && !strcmpiW( cookie->name, name ))
{
TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
return cookie;
}
}
return NULL;
}
static BOOL domain_match( const WCHAR *name, domain_t *domain, BOOL partial )
{
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;
}
static void free_cookie( cookie_t *cookie )
{
heap_free( cookie->name );
heap_free( cookie->value );
heap_free( cookie->path );
heap_free( cookie );
}
static void delete_cookie( cookie_t *cookie )
{
list_remove( &cookie->entry );
free_cookie( cookie );
}
void delete_domain( domain_t *domain )
{
cookie_t *cookie;
struct list *item, *next;
LIST_FOR_EACH_SAFE( item, next, &domain->cookies )
{
cookie = LIST_ENTRY( item, cookie_t, entry );
delete_cookie( cookie );
}
list_remove( &domain->entry );
heap_free( domain->name );
heap_free( domain );
}
static BOOL add_cookie( session_t *session, cookie_t *cookie, WCHAR *domain_name, WCHAR *path )
{
domain_t *domain = NULL;
cookie_t *old_cookie;
struct list *item;
LIST_FOR_EACH( item, &session->cookie_cache )
{
domain = LIST_ENTRY( item, domain_t, entry );
if (domain_match( domain_name, domain, FALSE )) break;
domain = NULL;
}
if (!domain)
{
if (!(domain = add_domain( session, domain_name ))) return FALSE;
}
else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
cookie->path = path;
list_add_tail( &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));
return TRUE;
}
static cookie_t *parse_cookie( const WCHAR *string )
{
cookie_t *cookie;
const WCHAR *p;
int len;
if (!(cookie = heap_alloc_zero( sizeof(cookie_t) ))) return NULL;
list_init( &cookie->entry );
if (!(p = strchrW( string, '=' )))
{
WARN("no '=' in %s\n", debugstr_w(string));
return NULL;
}
if (p == string)
{
WARN("empty cookie name in %s\n", debugstr_w(string));
return NULL;
}
len = p - string;
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;
p++; /* skip '=' */
while (*p == ' ') p++;
len = strlenW( p );
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;
return cookie;
}
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;
WCHAR *buffer, *p, *q, *r;
WCHAR *cookie_domain = NULL, *cookie_path = NULL;
session_t *session = request->connect->session;
cookie_t *cookie;
int len;
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;
}
if ((q = strstrW( p, domainW ))) /* FIXME: do real attribute parsing */
{
while (*q && *q != '=') q++;
if (!*q) goto end;
r = ++q;
while (*r && *r != ';') r++;
len = r - q;
if (!(cookie_domain = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
memcpy( cookie_domain, q, len * sizeof(WCHAR) );
cookie_domain[len] = 0;
}
if ((q = strstrW( p, pathW )))
{
while (*q && *q != '=') q++;
if (!*q) goto end;
r = ++q;
while (*r && *r != ';') r++;
len = r - q;
if (!(cookie_path = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
memcpy( cookie_path, q, len * sizeof(WCHAR) );
cookie_path[len] = 0;
}
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:
if (!ret)
{
free_cookie( cookie );
heap_free( cookie_domain );
heap_free( cookie_path );
}
heap_free( buffer );
return ret;
}
BOOL add_cookie_headers( request_t *request )
{
struct list *domain_cursor;
session_t *session = request->connect->session;
LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
{
domain_t *domain = LIST_ENTRY( domain_cursor, domain_t, entry );
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 )
{
cookie_t *cookie = LIST_ENTRY( cookie_cursor, cookie_t, entry );
TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
if (strstrW( request->path, cookie->path ) == request->path)
{
const WCHAR format[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
int len;
WCHAR *header;
len = strlenW( cookie->name ) + strlenW( format ) + strlenW( cookie->value );
if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
sprintfW( header, format, cookie->name, cookie->value );
TRACE("%s\n", debugstr_w(header));
add_request_headers( request, header, len, WINHTTP_ADDREQ_FLAG_ADD );
heap_free( header );
}
}
}
}
return TRUE;
}

View File

@ -404,7 +404,7 @@ static BOOL process_header( request_t *request, LPCWSTR field, LPCWSTR value, DW
return TRUE;
}
static BOOL add_request_headers( request_t *request, LPCWSTR headers, DWORD len, DWORD flags )
BOOL add_request_headers( request_t *request, LPCWSTR headers, DWORD len, DWORD flags )
{
BOOL ret = FALSE;
WCHAR *buffer, *p, *q;
@ -824,6 +824,11 @@ static BOOL send_request( request_t *request, LPCWSTR headers, DWORD headers_len
TRACE("failed to add request headers\n");
return FALSE;
}
if (!(request->hdr.disable_flags & WINHTTP_DISABLE_COOKIES) && !add_cookie_headers( request ))
{
WARN("failed to add cookie headers\n");
return FALSE;
}
if (!(ret = open_connection( request ))) goto end;
if (!(req = build_request_string( request ))) goto end;
@ -1273,16 +1278,16 @@ static void drain_content( request_t *request )
}
}
/* copy cookies from response headers to request headers */
static void add_cookies( request_t *request )
static void record_cookies( request_t *request )
{
unsigned int i;
for (i = 0; i < request->num_headers; i++)
{
if (!strcmpiW( request->headers[i].field, attr_set_cookie ) && !request->headers[i].is_request)
header_t *set_cookie = &request->headers[i];
if (!strcmpiW( set_cookie->field, attr_set_cookie ) && !set_cookie->is_request)
{
process_header( request, attr_cookie, request->headers[i].value, WINHTTP_ADDREQ_FLAG_ADD, TRUE );
set_cookies( request, set_cookie->value );
}
}
}
@ -1305,13 +1310,14 @@ static BOOL receive_response( request_t *request, BOOL async )
if (!query_headers( request, query, NULL, &request->content_length, &size, NULL ))
request->content_length = ~0UL;
if (!(request->hdr.disable_flags & WINHTTP_DISABLE_COOKIES)) record_cookies( request );
if (status == 301 || status == 302)
{
if (request->hdr.disable_flags & WINHTTP_DISABLE_REDIRECTS) break;
drain_content( request );
drain_content( request );
if (!(ret = handle_redirect( request ))) break;
if (!(request->hdr.disable_flags & WINHTTP_DISABLE_COOKIES)) add_cookies( request );
clear_response_headers( request );
ret = send_request( request, NULL, 0, NULL, 0, 0, 0, FALSE ); /* recurse synchronously */

View File

@ -65,9 +65,16 @@ BOOL WINAPI WinHttpCheckPlatform( void )
static void session_destroy( object_header_t *hdr )
{
session_t *session = (session_t *)hdr;
struct list *item, *next;
domain_t *domain;
TRACE("%p\n", session);
LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
{
domain = LIST_ENTRY( item, domain_t, entry );
delete_domain( domain );
}
heap_free( session->agent );
heap_free( session->proxy_server );
heap_free( session->proxy_bypass );
@ -173,6 +180,7 @@ HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWST
session->hdr.refs = 1;
session->access = access;
session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
list_init( &session->cookie_cache );
if (agent && !(session->agent = strdupW( agent ))) goto end;
if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;

View File

@ -68,6 +68,21 @@ struct _object_header_t
struct list children;
};
typedef struct
{
struct list entry;
WCHAR *name;
struct list cookies;
} domain_t;
typedef struct
{
struct list entry;
WCHAR *name;
WCHAR *value;
WCHAR *path;
} cookie_t;
typedef struct
{
object_header_t hdr;
@ -77,6 +92,7 @@ typedef struct
LPWSTR proxy_bypass;
LPWSTR proxy_username;
LPWSTR proxy_password;
struct list cookie_cache;
} session_t;
typedef struct
@ -195,6 +211,11 @@ BOOL netconn_secure_connect( netconn_t * );
BOOL netconn_send( netconn_t *, const void *, size_t, int, int * );
const void *netconn_get_certificate( netconn_t * );
BOOL set_cookies( request_t *, const WCHAR * );
BOOL add_cookie_headers( request_t * );
BOOL add_request_headers( request_t *, LPCWSTR, DWORD, DWORD );
void delete_domain( domain_t * );
static inline void *heap_alloc( SIZE_T size )
{
return HeapAlloc( GetProcessHeap(), 0, size );