e340dbedc5d9e6ceee941c259415877808cf491f
[reactos.git] / reactos / 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 (!(cookie = heap_alloc_zero( sizeof(cookie_t) ))) return NULL;
126
127 list_init( &cookie->entry );
128
129 if (!(p = strchrW( string, '=' )))
130 {
131 WARN("no '=' in %s\n", debugstr_w(string));
132 return NULL;
133 }
134 if (p == string)
135 {
136 WARN("empty cookie name in %s\n", debugstr_w(string));
137 return NULL;
138 }
139 len = p - string;
140 if (!(cookie->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
141 {
142 heap_free( cookie );
143 return NULL;
144 }
145 memcpy( cookie->name, string, len * sizeof(WCHAR) );
146 cookie->name[len] = 0;
147
148 p++; /* skip '=' */
149 while (*p == ' ') p++;
150
151 len = strlenW( p );
152 if (!(cookie->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
153 {
154 free_cookie( cookie );
155 return NULL;
156 }
157 memcpy( cookie->value, p, len * sizeof(WCHAR) );
158 cookie->value[len] = 0;
159
160 return cookie;
161 }
162
163 BOOL set_cookies( request_t *request, const WCHAR *cookies )
164 {
165 static const WCHAR pathW[] = {'p','a','t','h',0};
166 static const WCHAR domainW[] = {'d','o','m','a','i','n',0};
167
168 BOOL ret = FALSE;
169 WCHAR *buffer, *p, *q, *r;
170 WCHAR *cookie_domain = NULL, *cookie_path = NULL;
171 session_t *session = request->connect->session;
172 cookie_t *cookie;
173 int len;
174
175 len = strlenW( cookies );
176 if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
177 strcpyW( buffer, cookies );
178
179 p = buffer;
180 while (*p && *p != ';') p++;
181 if (*p == ';') *p++ = 0;
182 if (!(cookie = parse_cookie( buffer )))
183 {
184 heap_free( buffer );
185 return FALSE;
186 }
187 if ((q = strstrW( p, domainW ))) /* FIXME: do real attribute parsing */
188 {
189 while (*q && *q != '=') q++;
190 if (!*q) goto end;
191
192 r = ++q;
193 while (*r && *r != ';') r++;
194 len = r - q;
195
196 if (!(cookie_domain = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
197 memcpy( cookie_domain, q, len * sizeof(WCHAR) );
198 cookie_domain[len] = 0;
199
200 }
201 if ((q = strstrW( p, pathW )))
202 {
203 while (*q && *q != '=') q++;
204 if (!*q) goto end;
205
206 r = ++q;
207 while (*r && *r != ';') r++;
208 len = r - q;
209
210 if (!(cookie_path = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto end;
211 memcpy( cookie_path, q, len * sizeof(WCHAR) );
212 cookie_path[len] = 0;
213 }
214 if (!cookie_domain && !(cookie_domain = strdupW( request->connect->servername ))) goto end;
215 if (!cookie_path && !(cookie_path = strdupW( request->path ))) goto end;
216
217 if ((p = strrchrW( cookie_path, '/' )) && p != cookie_path) *p = 0;
218 ret = add_cookie( session, cookie, cookie_domain, cookie_path );
219
220 end:
221 if (!ret) free_cookie( cookie );
222 heap_free( cookie_domain );
223 heap_free( cookie_path );
224 heap_free( buffer );
225 return ret;
226 }
227
228 BOOL add_cookie_headers( request_t *request )
229 {
230 struct list *domain_cursor;
231 session_t *session = request->connect->session;
232
233 LIST_FOR_EACH( domain_cursor, &session->cookie_cache )
234 {
235 domain_t *domain = LIST_ENTRY( domain_cursor, domain_t, entry );
236 if (domain_match( request->connect->servername, domain, TRUE ))
237 {
238 struct list *cookie_cursor;
239 TRACE("found domain %s\n", debugstr_w(domain->name));
240
241 LIST_FOR_EACH( cookie_cursor, &domain->cookies )
242 {
243 cookie_t *cookie = LIST_ENTRY( cookie_cursor, cookie_t, entry );
244
245 TRACE("comparing path %s with %s\n", debugstr_w(request->path), debugstr_w(cookie->path));
246
247 if (strstrW( request->path, cookie->path ) == request->path)
248 {
249 const WCHAR format[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
250 int len;
251 WCHAR *header;
252
253 len = strlenW( cookie->name ) + strlenW( format ) + strlenW( cookie->value );
254 if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
255
256 sprintfW( header, format, cookie->name, cookie->value );
257
258 TRACE("%s\n", debugstr_w(header));
259 add_request_headers( request, header, len, WINHTTP_ADDREQ_FLAG_ADD );
260 heap_free( header );
261 }
262 }
263 }
264 }
265 return TRUE;
266 }