Sync to trunk revision 63922.
[reactos.git] / dll / win32 / winhttp / cookie.c
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 "winhttp_private.h"
20
21 static domain_t *add_domain( session_t *session, WCHAR *name )
22 {
23 domain_t *domain;
24
25 if (!(domain = heap_alloc_zero( sizeof(domain_t) ))) return NULL;
26
27 list_init( &domain->entry );
28 list_init( &domain->cookies );
29
30 domain->name = strdupW( name );
31 list_add_tail( &session->cookie_cache, &domain->entry );
32
33 TRACE("%s\n", debugstr_w(domain->name));
34 return domain;
35 }
36
37 static cookie_t *find_cookie( domain_t *domain, const WCHAR *path, const WCHAR *name )
38 {
39 struct list *item;
40 cookie_t *cookie;
41
42 LIST_FOR_EACH( item, &domain->cookies )
43 {
44 cookie = LIST_ENTRY( item, cookie_t, entry );
45 if (!strcmpW( cookie->path, path ) && !strcmpiW( cookie->name, name ))
46 {
47 TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value));
48 return cookie;
49 }
50 }
51 return NULL;
52 }
53
54 static BOOL domain_match( const WCHAR *name, domain_t *domain, BOOL partial )
55 {
56 TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));
57
58 if (partial && !strstrW( name, domain->name )) return FALSE;
59 else if (!partial && strcmpW( name, domain->name )) return FALSE;
60 return TRUE;
61 }
62
63 static void free_cookie( cookie_t *cookie )
64 {
65 heap_free( cookie->name );
66 heap_free( cookie->value );
67 heap_free( cookie->path );
68 heap_free( cookie );
69 }
70
71 static void delete_cookie( cookie_t *cookie )
72 {
73 list_remove( &cookie->entry );
74 free_cookie( cookie );
75 }
76
77 void delete_domain( domain_t *domain )
78 {
79 cookie_t *cookie;
80 struct list *item, *next;
81
82 LIST_FOR_EACH_SAFE( item, next, &domain->cookies )
83 {
84 cookie = LIST_ENTRY( item, cookie_t, entry );
85 delete_cookie( cookie );
86 }
87
88 list_remove( &domain->entry );
89 heap_free( domain->name );
90 heap_free( domain );
91 }
92
93 static BOOL add_cookie( session_t *session, cookie_t *cookie, WCHAR *domain_name, WCHAR *path )
94 {
95 domain_t *domain = NULL;
96 cookie_t *old_cookie;
97 struct list *item;
98
99 LIST_FOR_EACH( item, &session->cookie_cache )
100 {
101 domain = LIST_ENTRY( item, domain_t, entry );
102 if (domain_match( domain_name, domain, FALSE )) break;
103 domain = NULL;
104 }
105 if (!domain)
106 {
107 if (!(domain = add_domain( session, domain_name ))) return FALSE;
108 }
109 else if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
110
111 cookie->path = strdupW( path );
112 list_add_tail( &domain->cookies, &cookie->entry );
113
114 TRACE("domain %s path %s <- %s=%s\n", debugstr_w(domain_name), debugstr_w(cookie->path),
115 debugstr_w(cookie->name), debugstr_w(cookie->value));
116 return TRUE;
117 }
118
119 static cookie_t *parse_cookie( const WCHAR *string )
120 {
121 cookie_t *cookie;
122 const WCHAR *p;
123 int len;
124
125 if (!(p = strchrW( string, '=' )))
126 {
127 WARN("no '=' in %s\n", debugstr_w(string));
128 return NULL;
129 }
130 if (p == string)
131 {
132 WARN("empty cookie name in %s\n", debugstr_w(string));
133 return NULL;
134 }
135
136 if (!(cookie = heap_alloc_zero( sizeof(cookie_t) ))) return NULL;
137
138 list_init( &cookie->entry );
139
140 len = p - string;
141 if (!(cookie->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
142 {
143 heap_free( cookie );
144 return NULL;
145 }
146 memcpy( cookie->name, string, len * sizeof(WCHAR) );
147 cookie->name[len] = 0;
148
149 p++; /* skip '=' */
150 while (*p == ' ') p++;
151
152 len = strlenW( p );
153 if (!(cookie->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
154 {
155 free_cookie( cookie );
156 return NULL;
157 }
158 memcpy( cookie->value, p, len * sizeof(WCHAR) );
159 cookie->value[len] = 0;
160
161 return cookie;
162 }
163
164 BOOL set_cookies( request_t *request, const WCHAR *cookies )
165 {
166 static const WCHAR pathW[] = {'p','a','t','h',0};
167 static const WCHAR domainW[] = {'d','o','m','a','i','n',0};
168
169 BOOL ret = FALSE;
170 WCHAR *buffer, *p, *q, *r;
171 WCHAR *cookie_domain = NULL, *cookie_path = NULL;
172 session_t *session = request->connect->session;
173 cookie_t *cookie;
174 int len;
175
176 len = strlenW( cookies );
177 if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
178 strcpyW( buffer, cookies );
179
180 p = buffer;
181 while (*p && *p != ';') p++;
182 if (*p == ';') *p++ = 0;
183 if (!(cookie = parse_cookie( buffer )))
184 {
185 heap_free( buffer );
186 return FALSE;
187 }
188 if ((q = strstrW( p, domainW ))) /* FIXME: do real attribute parsing */
189 {
190 while (*q && *q != '=') q++;
191 if (!*q) goto end;
192
193 r = ++q;
194 while (*r && *r != ';') r++;
195 len = r - q;
196
197 if (!(cookie_domain = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
198 memcpy( cookie_domain, q, len * sizeof(WCHAR) );
199 cookie_domain[len] = 0;
200
201 }
202 if ((q = strstrW( p, pathW )))
203 {
204 while (*q && *q != '=') q++;
205 if (!*q) goto end;
206
207 r = ++q;
208 while (*r && *r != ';') r++;
209 len = r - q;
210
211 if (!(cookie_path = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
212 memcpy( cookie_path, q, len * sizeof(WCHAR) );
213 cookie_path[len] = 0;
214 }
215 if (!cookie_domain && !(cookie_domain = strdupW( request->connect->servername ))) goto end;
216 if (!cookie_path && !(cookie_path = strdupW( request->path ))) goto end;
217
218 if ((p = strrchrW( cookie_path, '/' )) && p != cookie_path) *p = 0;
219 ret = add_cookie( session, cookie, cookie_domain, cookie_path );
220
221 end:
222 if (!ret) free_cookie( cookie );
223 heap_free( cookie_domain );
224 heap_free( cookie_path );
225 heap_free( buffer );
226 return ret;
227 }
228
229 BOOL add_cookie_headers( request_t *request )
230 {
231 struct list *domain_cursor;
232 session_t *session = request->connect->session;
233
234 LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
235 {
236 domain_t *domain = LIST_ENTRY( domain_cursor, domain_t, entry );
237 if (domain_match( request->connect->servername, domain, TRUE ))
238 {
239 struct list *cookie_cursor;
240 TRACE("found domain %s\n", debugstr_w(domain->name));
241
242 LIST_FOR_EACH( cookie_cursor, &domain->cookies )
243 {
244 cookie_t *cookie = LIST_ENTRY( cookie_cursor, cookie_t, entry );
245
246 TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
247
248 if (strstrW( request->path, cookie->path ) == request->path)
249 {
250 const WCHAR format[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
251 int len;
252 WCHAR *header;
253
254 len = strlenW( cookie->name ) + strlenW( format ) + strlenW( cookie->value );
255 if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
256
257 sprintfW( header, format, cookie->name, cookie->value );
258
259 TRACE("%s\n", debugstr_w(header));
260 add_request_headers( request, header, len, WINHTTP_ADDREQ_FLAG_ADD );
261 heap_free( header );
262 }
263 }
264 }
265 }
266 return TRUE;
267 }