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 "winhttp_private.h"
21 static domain_t
*add_domain( session_t
*session
, WCHAR
*name
)
25 if (!(domain
= heap_alloc_zero( sizeof(domain_t
) ))) return NULL
;
27 list_init( &domain
->entry
);
28 list_init( &domain
->cookies
);
30 domain
->name
= strdupW( name
);
31 list_add_tail( &session
->cookie_cache
, &domain
->entry
);
33 TRACE("%s\n", debugstr_w(domain
->name
));
37 static cookie_t
*find_cookie( domain_t
*domain
, const WCHAR
*path
, const WCHAR
*name
)
42 LIST_FOR_EACH( item
, &domain
->cookies
)
44 cookie
= LIST_ENTRY( item
, cookie_t
, entry
);
45 if (!strcmpW( cookie
->path
, path
) && !strcmpiW( cookie
->name
, name
))
47 TRACE("found %s=%s\n", debugstr_w(cookie
->name
), debugstr_w(cookie
->value
));
54 static BOOL
domain_match( const WCHAR
*name
, domain_t
*domain
, BOOL partial
)
56 TRACE("comparing %s with %s\n", debugstr_w(name
), debugstr_w(domain
->name
));
58 if (partial
&& !strstrW( name
, domain
->name
)) return FALSE
;
59 else if (!partial
&& strcmpW( name
, domain
->name
)) return FALSE
;
63 static void free_cookie( cookie_t
*cookie
)
65 heap_free( cookie
->name
);
66 heap_free( cookie
->value
);
67 heap_free( cookie
->path
);
71 static void delete_cookie( cookie_t
*cookie
)
73 list_remove( &cookie
->entry
);
74 free_cookie( cookie
);
77 void delete_domain( domain_t
*domain
)
80 struct list
*item
, *next
;
82 LIST_FOR_EACH_SAFE( item
, next
, &domain
->cookies
)
84 cookie
= LIST_ENTRY( item
, cookie_t
, entry
);
85 delete_cookie( cookie
);
88 list_remove( &domain
->entry
);
89 heap_free( domain
->name
);
93 static BOOL
add_cookie( session_t
*session
, cookie_t
*cookie
, WCHAR
*domain_name
, WCHAR
*path
)
95 domain_t
*domain
= NULL
;
99 LIST_FOR_EACH( item
, &session
->cookie_cache
)
101 domain
= LIST_ENTRY( item
, domain_t
, entry
);
102 if (domain_match( domain_name
, domain
, FALSE
)) break;
107 if (!(domain
= add_domain( session
, domain_name
))) return FALSE
;
109 else if ((old_cookie
= find_cookie( domain
, path
, cookie
->name
))) delete_cookie( old_cookie
);
111 cookie
->path
= strdupW( path
);
112 list_add_tail( &domain
->cookies
, &cookie
->entry
);
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
));
119 static cookie_t
*parse_cookie( const WCHAR
*string
)
125 if (!(p
= strchrW( string
, '=' )))
127 WARN("no '=' in %s\n", debugstr_w(string
));
132 WARN("empty cookie name in %s\n", debugstr_w(string
));
136 if (!(cookie
= heap_alloc_zero( sizeof(cookie_t
) ))) return NULL
;
138 list_init( &cookie
->entry
);
141 if (!(cookie
->name
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
146 memcpy( cookie
->name
, string
, len
* sizeof(WCHAR
) );
147 cookie
->name
[len
] = 0;
150 while (*p
== ' ') p
++;
153 if (!(cookie
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
155 free_cookie( cookie
);
158 memcpy( cookie
->value
, p
, len
* sizeof(WCHAR
) );
159 cookie
->value
[len
] = 0;
164 BOOL
set_cookies( request_t
*request
, const WCHAR
*cookies
)
166 static const WCHAR pathW
[] = {'p','a','t','h',0};
167 static const WCHAR domainW
[] = {'d','o','m','a','i','n',0};
170 WCHAR
*buffer
, *p
, *q
, *r
;
171 WCHAR
*cookie_domain
= NULL
, *cookie_path
= NULL
;
172 session_t
*session
= request
->connect
->session
;
176 len
= strlenW( cookies
);
177 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
178 strcpyW( buffer
, cookies
);
181 while (*p
&& *p
!= ';') p
++;
182 if (*p
== ';') *p
++ = 0;
183 if (!(cookie
= parse_cookie( buffer
)))
188 if ((q
= strstrW( p
, domainW
))) /* FIXME: do real attribute parsing */
190 while (*q
&& *q
!= '=') q
++;
194 while (*r
&& *r
!= ';') r
++;
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;
202 if ((q
= strstrW( p
, pathW
)))
204 while (*q
&& *q
!= '=') q
++;
208 while (*r
&& *r
!= ';') r
++;
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;
215 if (!cookie_domain
&& !(cookie_domain
= strdupW( request
->connect
->servername
))) goto end
;
216 if (!cookie_path
&& !(cookie_path
= strdupW( request
->path
))) goto end
;
218 if ((p
= strrchrW( cookie_path
, '/' )) && p
!= cookie_path
) *p
= 0;
219 ret
= add_cookie( session
, cookie
, cookie_domain
, cookie_path
);
222 if (!ret
) free_cookie( cookie
);
223 heap_free( cookie_domain
);
224 heap_free( cookie_path
);
229 BOOL
add_cookie_headers( request_t
*request
)
231 struct list
*domain_cursor
;
232 session_t
*session
= request
->connect
->session
;
234 LIST_FOR_EACH( domain_cursor
, &session
->cookie_cache
)
236 domain_t
*domain
= LIST_ENTRY( domain_cursor
, domain_t
, entry
);
237 if (domain_match( request
->connect
->servername
, domain
, TRUE
))
239 struct list
*cookie_cursor
;
240 TRACE("found domain %s\n", debugstr_w(domain
->name
));
242 LIST_FOR_EACH( cookie_cursor
, &domain
->cookies
)
244 cookie_t
*cookie
= LIST_ENTRY( cookie_cursor
, cookie_t
, entry
);
246 TRACE("comparing path %s with %s\n", debugstr_w(request
->path
), debugstr_w(cookie
->path
));
248 if (strstrW( request
->path
, cookie
->path
) == request
->path
)
250 const WCHAR format
[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
254 len
= strlenW( cookie
->name
) + strlenW( format
) + strlenW( cookie
->value
);
255 if (!(header
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
257 sprintfW( header
, format
, cookie
->name
, cookie
->value
);
259 TRACE("%s\n", debugstr_w(header
));
260 add_request_headers( request
, header
, len
, WINHTTP_ADDREQ_FLAG_ADD
);