ReactOS 0.4.16-dev-2491-g3dc6630
cookie.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19#include <stdarg.h>
20#include <wchar.h>
21
22#include "windef.h"
23#include "winbase.h"
24#include "ws2tcpip.h"
25#include "winhttp.h"
26
27#include "wine/debug.h"
28#include "wine/list.h"
29#include "winhttp_private.h"
30
32
33struct cookie
34{
35 struct list entry;
39};
40
41struct domain
42{
43 struct list entry;
45 struct list cookies;
46};
47
48static struct domain *add_domain( struct session *session, WCHAR *name )
49{
50 struct domain *domain;
51
52 if (!(domain = calloc( 1, sizeof(*domain) ))) return NULL;
53
56
57 domain->name = wcsdup( name );
59
60 TRACE("%s\n", debugstr_w(domain->name));
61 return domain;
62}
63
64static struct cookie *find_cookie( struct domain *domain, const WCHAR *path, const WCHAR *name )
65{
66 struct list *item;
67 struct cookie *cookie;
68
70 {
71 cookie = LIST_ENTRY( item, struct cookie, entry );
72 if (!wcscmp( cookie->path, path ) && !wcscmp( cookie->name, name ))
73 {
74 TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
75 return cookie;
76 }
77 }
78 return NULL;
79}
80
81static BOOL domain_match( const WCHAR *name, struct domain *domain, BOOL partial )
82{
83 TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));
84
85 if (partial && !wcsstr( name, domain->name )) return FALSE;
86 else if (!partial && wcscmp( name, domain->name )) return FALSE;
87 return TRUE;
88}
89
90static void free_cookie( struct cookie *cookie )
91{
92 free( cookie->name );
93 free( cookie->value );
94 free( cookie->path );
95 free( cookie );
96}
97
98static void delete_cookie( struct cookie *cookie )
99{
102}
103
104static void delete_domain( struct domain *domain )
105{
106 struct cookie *cookie;
107 struct list *item, *next;
108
110 {
111 cookie = LIST_ENTRY( item, struct cookie, entry );
113 }
114
116 free( domain->name );
117 free( domain );
118}
119
121{
122 struct list *item, *next;
123 struct domain *domain;
124
126 {
127 domain = LIST_ENTRY( item, struct domain, entry );
129 }
130}
131
132static BOOL add_cookie( struct session *session, struct cookie *cookie, WCHAR *domain_name, WCHAR *path )
133{
134 struct domain *domain = NULL;
135 struct cookie *old_cookie;
136 struct list *item;
137
138 if (!(cookie->path = wcsdup( path ))) return FALSE;
139
141
143 {
144 domain = LIST_ENTRY( item, struct domain, entry );
145 if (domain_match( domain_name, domain, FALSE )) break;
146 domain = NULL;
147 }
148 if (!domain) domain = add_domain( session, domain_name );
149 else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
150
151 if (domain)
152 {
154 TRACE("domain %s path %s <- %s=%s\n", debugstr_w(domain_name), debugstr_w(cookie->path),
156 }
157
159 return domain != NULL;
160}
161
162static struct cookie *parse_cookie( const WCHAR *string )
163{
164 struct cookie *cookie;
165 const WCHAR *p;
166 int len;
167
168 if (!(p = wcschr( string, '=' ))) p = string + lstrlenW( string );
169 len = p - string;
170 while (len && string[len - 1] == ' ') len--;
171 if (!len) return NULL;
172
173 if (!(cookie = calloc( 1, sizeof(*cookie) ))) return NULL;
175
176 if (!(cookie->name = malloc( (len + 1) * sizeof(WCHAR) )))
177 {
178 free( cookie );
179 return NULL;
180 }
181 memcpy( cookie->name, string, len * sizeof(WCHAR) );
182 cookie->name[len] = 0;
183
184 if (*p++ == '=')
185 {
186 while (*p == ' ') p++;
187 len = lstrlenW( p );
188 while (len && p[len - 1] == ' ') len--;
189
190 if (!(cookie->value = malloc( (len + 1) * sizeof(WCHAR) )))
191 {
193 return NULL;
194 }
195 memcpy( cookie->value, p, len * sizeof(WCHAR) );
196 cookie->value[len] = 0;
197 }
198 return cookie;
199}
200
201struct attr
202{
205};
206
207static void free_attr( struct attr *attr )
208{
209 if (!attr) return;
210 free( attr->name );
211 free( attr->value );
212 free( attr );
213}
214
215static struct attr *parse_attr( const WCHAR *str, int *used )
216{
217 const WCHAR *p = str, *q;
218 struct attr *attr;
219 int len;
220
221 while (*p == ' ') p++;
222 q = p;
223 while (*q && *q != ' ' && *q != '=' && *q != ';') q++;
224 len = q - p;
225 if (!len) return NULL;
226
227 if (!(attr = malloc( sizeof(*attr) ))) return NULL;
228 if (!(attr->name = malloc( (len + 1) * sizeof(WCHAR) )))
229 {
230 free( attr );
231 return NULL;
232 }
233 memcpy( attr->name, p, len * sizeof(WCHAR) );
234 attr->name[len] = 0;
235 attr->value = NULL;
236
237 p = q;
238 while (*p == ' ') p++;
239 if (*p++ == '=')
240 {
241 while (*p == ' ') p++;
242 q = p;
243 while (*q && *q != ';') q++;
244 len = q - p;
245 while (len && p[len - 1] == ' ') len--;
246
247 if (!(attr->value = malloc( (len + 1) * sizeof(WCHAR) )))
248 {
249 free_attr( attr );
250 return NULL;
251 }
252 memcpy( attr->value, p, len * sizeof(WCHAR) );
253 attr->value[len] = 0;
254 }
255
256 while (*q == ' ') q++;
257 if (*q == ';') q++;
258 *used = q - str;
259
260 return attr;
261}
262
264{
265 BOOL ret = FALSE;
266 WCHAR *buffer, *p;
267 WCHAR *cookie_domain = NULL, *cookie_path = NULL;
268 struct attr *attr, *domain = NULL, *path = NULL;
269 struct session *session = request->connect->session;
270 struct cookie *cookie;
271 int len, used;
272
273 len = lstrlenW( cookies );
274 if (!(buffer = malloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
276
277 p = buffer;
278 while (*p && *p != ';') p++;
279 if (*p == ';') *p++ = 0;
280 if (!(cookie = parse_cookie( buffer )))
281 {
282 free( buffer );
283 return FALSE;
284 }
285 len = lstrlenW( p );
286 while (len && (attr = parse_attr( p, &used )))
287 {
288 if (!wcsicmp( attr->name, L"domain" ))
289 {
290 domain = attr;
291 cookie_domain = attr->value;
292 }
293 else if (!wcsicmp( attr->name, L"path" ))
294 {
295 path = attr;
296 cookie_path = attr->value;
297 }
298 else
299 {
300 FIXME( "unhandled attribute %s\n", debugstr_w(attr->name) );
301 free_attr( attr );
302 }
303 len -= used;
304 p += used;
305 }
306 if (!cookie_domain && !(cookie_domain = wcsdup( request->connect->servername ))) goto end;
307 if (!cookie_path && !(cookie_path = wcsdup( request->path ))) goto end;
308
309 if ((p = wcsrchr( cookie_path, '/' )) && p != cookie_path) *p = 0;
310 ret = add_cookie( session, cookie, cookie_domain, cookie_path );
311
312end:
313 if (!ret) free_cookie( cookie );
314 if (domain) free_attr( domain );
315 else free( cookie_domain );
316 if (path) free_attr( path );
317 else free( cookie_path );
318 free( buffer );
319 return ret;
320}
321
323{
324 struct list *domain_cursor;
325 struct session *session = request->connect->session;
327
329
330 LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
331 {
332 struct domain *domain = LIST_ENTRY( domain_cursor, struct domain, entry );
333 if (domain_match( request->connect->servername, domain, TRUE ))
334 {
335 struct list *cookie_cursor;
336 TRACE("found domain %s\n", debugstr_w(domain->name));
337
338 LIST_FOR_EACH( cookie_cursor, &domain->cookies )
339 {
340 struct cookie *cookie = LIST_ENTRY( cookie_cursor, struct cookie, entry );
341
342 TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
343
344 if (wcsstr( request->path, cookie->path ) == request->path)
345 {
346 static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
347 int len, len_cookie = ARRAY_SIZE( cookieW ), len_name = lstrlenW( cookie->name );
348 WCHAR *header;
349
350 len = len_cookie + len_name;
351 if (cookie->value) len += lstrlenW( cookie->value ) + 1;
352 if (!(header = malloc( (len + 1) * sizeof(WCHAR) )))
353 {
355 return ERROR_OUTOFMEMORY;
356 }
357
358 memcpy( header, cookieW, len_cookie * sizeof(WCHAR) );
359 lstrcpyW( header + len_cookie, cookie->name );
360 if (cookie->value)
361 {
362 header[len_cookie + len_name] = '=';
363 lstrcpyW( header + len_cookie + len_name + 1, cookie->value );
364 }
365
366 TRACE("%s\n", debugstr_w(header));
369 free( header );
370 }
371 }
372 }
373 }
374
376 return ret;
377}
static int used
Definition: adh-main.c:39
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ARRAY_SIZE(A)
Definition: main.h:20
static void list_remove(struct list_entry *entry)
Definition: list.h:90
static void list_add_tail(struct list_entry *head, struct list_entry *entry)
Definition: list.h:83
static void list_add_head(struct list_entry *head, struct list_entry *entry)
Definition: list.h:76
static void list_init(struct list_entry *head)
Definition: list.h:51
#define FIXME(fmt,...)
Definition: precomp.h:53
Definition: list.h:37
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define wcschr
Definition: compat.h:17
#define wcsrchr
Definition: compat.h:16
#define lstrcpyW
Definition: compat.h:749
#define wcsicmp
Definition: compat.h:15
#define lstrlenW
Definition: compat.h:750
_ACRTIMP int __cdecl wcscmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:1972
_ACRTIMP wchar_t *__cdecl wcsstr(const wchar_t *, const wchar_t *)
Definition: wcs.c:2993
static wchar_t * wcsdup(const wchar_t *str)
Definition: string.h:94
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint end
Definition: gl.h:1545
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLuint buffer
Definition: glext.h:5915
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
uint32_t entry
Definition: isohybrid.c:63
#define debugstr_w
Definition: kernel32.h:32
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
char string[160]
Definition: util.h:11
static CookieInternal * cookies
Definition: msctf.cpp:37
static unsigned __int64 next
Definition: rand_nt.c:6
DWORD add_request_headers(struct request *request, const WCHAR *headers, DWORD len, DWORD flags)
Definition: request.c:485
#define calloc
Definition: rosglue.h:14
const WCHAR * str
#define LIST_FOR_EACH(cursor, list)
Definition: list.h:188
#define LIST_FOR_EACH_SAFE(cursor, cursor2, list)
Definition: list.h:192
#define TRACE(s)
Definition: solgame.cpp:4
Definition: cookie.c:202
WCHAR * value
Definition: cookie.c:204
WCHAR * name
Definition: cookie.c:203
Definition: cookie.c:34
WCHAR * name
Definition: cookie.c:36
WCHAR * value
Definition: cookie.c:37
struct list entry
Definition: cookie.c:35
WCHAR * path
Definition: cookie.c:38
Definition: cookie.c:42
WCHAR * name
Definition: cookie.c:44
struct list entry
Definition: cookie.c:43
struct list cookies
Definition: cookie.c:45
Definition: name.c:39
Definition: tftpd.h:86
char path[256]
Definition: tftpd.h:94
struct connect * connect
CRITICAL_SECTION cs
struct list cookie_cache
#define LIST_ENTRY(type)
Definition: queue.h:175
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
static void delete_cookie(struct cookie *cookie)
Definition: cookie.c:98
void destroy_cookies(struct session *session)
Definition: cookie.c:120
static struct cookie * find_cookie(struct domain *domain, const WCHAR *path, const WCHAR *name)
Definition: cookie.c:64
static void delete_domain(struct domain *domain)
Definition: cookie.c:104
static struct attr * parse_attr(const WCHAR *str, int *used)
Definition: cookie.c:215
static struct cookie * parse_cookie(const WCHAR *string)
Definition: cookie.c:162
static BOOL add_cookie(struct session *session, struct cookie *cookie, WCHAR *domain_name, WCHAR *path)
Definition: cookie.c:132
BOOL set_cookies(struct request *request, const WCHAR *cookies)
Definition: cookie.c:263
static BOOL domain_match(const WCHAR *name, struct domain *domain, BOOL partial)
Definition: cookie.c:81
static void free_attr(struct attr *attr)
Definition: cookie.c:207
static struct domain * add_domain(struct session *session, WCHAR *name)
Definition: cookie.c:48
DWORD add_cookie_headers(struct request *request)
Definition: cookie.c:322
#define WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
Definition: winhttp.h:94
#define WINHTTP_ADDREQ_FLAG_ADD
Definition: winhttp.h:92
void free_cookie(void)
Definition: cookie.c:1269
__wchar_t WCHAR
Definition: xmlstorage.h:180