ntdll: Fix race in NtRead/WriteFile.
[wine/testsucceed.git] / dlls / winhttp / cookie.c
blobaf881ea29153a6f0d9f1a602b2c900a7fc3d2ec4
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include <stdarg.h>
22 #include "wine/debug.h"
23 #include "wine/list.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winhttp.h"
29 #include "winhttp_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
33 static domain_t *add_domain( session_t *session, WCHAR *name )
35 domain_t *domain;
37 if (!(domain = heap_alloc_zero( sizeof(domain_t) ))) return NULL;
39 list_init( &domain->entry );
40 list_init( &domain->cookies );
42 domain->name = strdupW( name );
43 list_add_tail( &session->cookie_cache, &domain->entry );
45 TRACE("%s\n", debugstr_w(domain->name));
46 return domain;
49 static cookie_t *find_cookie( domain_t *domain, const WCHAR *path, const WCHAR *name )
51 struct list *item;
52 cookie_t *cookie;
54 LIST_FOR_EACH( item, &domain->cookies )
56 cookie = LIST_ENTRY( item, cookie_t, entry );
57 if (!strcmpW( cookie->path, path ) && !strcmpiW( cookie->name, name ))
59 TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
60 return cookie;
63 return NULL;
66 static BOOL domain_match( const WCHAR *name, domain_t *domain, BOOL partial )
68 TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));
70 if (partial && !strstrW( name, domain->name )) return FALSE;
71 else if (!partial && strcmpW( name, domain->name )) return FALSE;
72 return TRUE;
75 static void free_cookie( cookie_t *cookie )
77 heap_free( cookie->name );
78 heap_free( cookie->value );
79 heap_free( cookie->path );
80 heap_free( cookie );
83 static void delete_cookie( cookie_t *cookie )
85 list_remove( &cookie->entry );
86 free_cookie( cookie );
89 void delete_domain( domain_t *domain )
91 cookie_t *cookie;
92 struct list *item, *next;
94 LIST_FOR_EACH_SAFE( item, next, &domain->cookies )
96 cookie = LIST_ENTRY( item, cookie_t, entry );
97 delete_cookie( cookie );
100 list_remove( &domain->entry );
101 heap_free( domain->name );
102 heap_free( domain );
105 static BOOL add_cookie( session_t *session, cookie_t *cookie, WCHAR *domain_name, WCHAR *path )
107 domain_t *domain = NULL;
108 cookie_t *old_cookie;
109 struct list *item;
111 LIST_FOR_EACH( item, &session->cookie_cache )
113 domain = LIST_ENTRY( item, domain_t, entry );
114 if (domain_match( domain_name, domain, FALSE )) break;
115 domain = NULL;
117 if (!domain)
119 if (!(domain = add_domain( session, domain_name ))) return FALSE;
121 else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
123 cookie->path = strdupW( path );
124 list_add_tail( &domain->cookies, &cookie->entry );
126 TRACE("domain %s path %s <- %s=%s\n", debugstr_w(domain_name), debugstr_w(cookie->path),
127 debugstr_w(cookie->name), debugstr_w(cookie->value));
128 return TRUE;
131 static cookie_t *parse_cookie( const WCHAR *string )
133 cookie_t *cookie;
134 const WCHAR *p;
135 int len;
137 if (!(cookie = heap_alloc_zero( sizeof(cookie_t) ))) return NULL;
139 list_init( &cookie->entry );
141 if (!(p = strchrW( string, '=' )))
143 WARN("no '=' in %s\n", debugstr_w(string));
144 return NULL;
146 if (p == string)
148 WARN("empty cookie name in %s\n", debugstr_w(string));
149 return NULL;
151 len = p - string;
152 if (!(cookie->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
154 heap_free( cookie );
155 return NULL;
157 memcpy( cookie->name, string, len * sizeof(WCHAR) );
158 cookie->name[len] = 0;
160 p++; /* skip '=' */
161 while (*p == ' ') p++;
163 len = strlenW( p );
164 if (!(cookie->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
166 free_cookie( cookie );
167 return NULL;
169 memcpy( cookie->value, p, len * sizeof(WCHAR) );
170 cookie->value[len] = 0;
172 return cookie;
175 BOOL set_cookies( request_t *request, const WCHAR *cookies )
177 static const WCHAR pathW[] = {'p','a','t','h',0};
178 static const WCHAR domainW[] = {'d','o','m','a','i','n',0};
180 BOOL ret = FALSE;
181 WCHAR *buffer, *p, *q, *r;
182 WCHAR *cookie_domain = NULL, *cookie_path = NULL;
183 session_t *session = request->connect->session;
184 cookie_t *cookie;
185 int len;
187 len = strlenW( cookies );
188 if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
189 strcpyW( buffer, cookies );
191 p = buffer;
192 while (*p && *p != ';') p++;
193 if (*p == ';') *p++ = 0;
194 if (!(cookie = parse_cookie( buffer )))
196 heap_free( buffer );
197 return FALSE;
199 if ((q = strstrW( p, domainW ))) /* FIXME: do real attribute parsing */
201 while (*q && *q != '=') q++;
202 if (!*q) goto end;
204 r = ++q;
205 while (*r && *r != ';') r++;
206 len = r - q;
208 if (!(cookie_domain = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
209 memcpy( cookie_domain, q, len * sizeof(WCHAR) );
210 cookie_domain[len] = 0;
213 if ((q = strstrW( p, pathW )))
215 while (*q && *q != '=') q++;
216 if (!*q) goto end;
218 r = ++q;
219 while (*r && *r != ';') r++;
220 len = r - q;
222 if (!(cookie_path = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
223 memcpy( cookie_path, q, len * sizeof(WCHAR) );
224 cookie_path[len] = 0;
226 if (!cookie_domain && !(cookie_domain = strdupW( request->connect->servername ))) goto end;
227 if (!cookie_path && !(cookie_path = strdupW( request->path ))) goto end;
229 if ((p = strrchrW( cookie_path, '/' )) && p != cookie_path) *p = 0;
230 ret = add_cookie( session, cookie, cookie_domain, cookie_path );
232 end:
233 if (!ret) free_cookie( cookie );
234 heap_free( cookie_domain );
235 heap_free( cookie_path );
236 heap_free( buffer );
237 return ret;
240 BOOL add_cookie_headers( request_t *request )
242 struct list *domain_cursor;
243 session_t *session = request->connect->session;
245 LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
247 domain_t *domain = LIST_ENTRY( domain_cursor, domain_t, entry );
248 if (domain_match( request->connect->servername, domain, TRUE ))
250 struct list *cookie_cursor;
251 TRACE("found domain %s\n", debugstr_w(domain->name));
253 LIST_FOR_EACH( cookie_cursor, &domain->cookies )
255 cookie_t *cookie = LIST_ENTRY( cookie_cursor, cookie_t, entry );
257 TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
259 if (strstrW( request->path, cookie->path ) == request->path)
261 const WCHAR format[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
262 int len;
263 WCHAR *header;
265 len = strlenW( cookie->name ) + strlenW( format ) + strlenW( cookie->value );
266 if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
268 sprintfW( header, format, cookie->name, cookie->value );
270 TRACE("%s\n", debugstr_w(header));
271 add_request_headers( request, header, len, WINHTTP_ADDREQ_FLAG_ADD );
272 heap_free( header );
277 return TRUE;