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 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
26 #include <wine/debug.h>
27 //#include "wine/list.h"
33 #include "winhttp_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
37 static domain_t
*add_domain( session_t
*session
, WCHAR
*name
)
41 if (!(domain
= heap_alloc_zero( sizeof(domain_t
) ))) return NULL
;
43 list_init( &domain
->entry
);
44 list_init( &domain
->cookies
);
46 domain
->name
= strdupW( name
);
47 list_add_tail( &session
->cookie_cache
, &domain
->entry
);
49 TRACE("%s\n", debugstr_w(domain
->name
));
53 static cookie_t
*find_cookie( domain_t
*domain
, const WCHAR
*path
, const WCHAR
*name
)
58 LIST_FOR_EACH( item
, &domain
->cookies
)
60 cookie
= LIST_ENTRY( item
, cookie_t
, entry
);
61 if (!strcmpW( cookie
->path
, path
) && !strcmpiW( cookie
->name
, name
))
63 TRACE("found %s=%s\n", debugstr_w(cookie
->name
), debugstr_w(cookie
->value
));
70 static BOOL
domain_match( const WCHAR
*name
, domain_t
*domain
, BOOL partial
)
72 TRACE("comparing %s with %s\n", debugstr_w(name
), debugstr_w(domain
->name
));
74 if (partial
&& !strstrW( name
, domain
->name
)) return FALSE
;
75 else if (!partial
&& strcmpW( name
, domain
->name
)) return FALSE
;
79 static void free_cookie( cookie_t
*cookie
)
81 heap_free( cookie
->name
);
82 heap_free( cookie
->value
);
83 heap_free( cookie
->path
);
87 static void delete_cookie( cookie_t
*cookie
)
89 list_remove( &cookie
->entry
);
90 free_cookie( cookie
);
93 void delete_domain( domain_t
*domain
)
96 struct list
*item
, *next
;
98 LIST_FOR_EACH_SAFE( item
, next
, &domain
->cookies
)
100 cookie
= LIST_ENTRY( item
, cookie_t
, entry
);
101 delete_cookie( cookie
);
104 list_remove( &domain
->entry
);
105 heap_free( domain
->name
);
109 static BOOL
add_cookie( session_t
*session
, cookie_t
*cookie
, WCHAR
*domain_name
, WCHAR
*path
)
111 domain_t
*domain
= NULL
;
112 cookie_t
*old_cookie
;
115 LIST_FOR_EACH( item
, &session
->cookie_cache
)
117 domain
= LIST_ENTRY( item
, domain_t
, entry
);
118 if (domain_match( domain_name
, domain
, FALSE
)) break;
123 if (!(domain
= add_domain( session
, domain_name
))) return FALSE
;
125 else if ((old_cookie
= find_cookie( domain
, path
, cookie
->name
))) delete_cookie( old_cookie
);
127 cookie
->path
= strdupW( path
);
128 list_add_tail( &domain
->cookies
, &cookie
->entry
);
130 TRACE("domain %s path %s <- %s=%s\n", debugstr_w(domain_name
), debugstr_w(cookie
->path
),
131 debugstr_w(cookie
->name
), debugstr_w(cookie
->value
));
135 static cookie_t
*parse_cookie( const WCHAR
*string
)
141 if (!(cookie
= heap_alloc_zero( sizeof(cookie_t
) ))) return NULL
;
143 list_init( &cookie
->entry
);
145 if (!(p
= strchrW( string
, '=' )))
147 WARN("no '=' in %s\n", debugstr_w(string
));
152 WARN("empty cookie name in %s\n", debugstr_w(string
));
156 if (!(cookie
->name
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
161 memcpy( cookie
->name
, string
, len
* sizeof(WCHAR
) );
162 cookie
->name
[len
] = 0;
165 while (*p
== ' ') p
++;
168 if (!(cookie
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
170 free_cookie( cookie
);
173 memcpy( cookie
->value
, p
, len
* sizeof(WCHAR
) );
174 cookie
->value
[len
] = 0;
179 BOOL
set_cookies( request_t
*request
, const WCHAR
*cookies
)
181 static const WCHAR pathW
[] = {'p','a','t','h',0};
182 static const WCHAR domainW
[] = {'d','o','m','a','i','n',0};
185 WCHAR
*buffer
, *p
, *q
, *r
;
186 WCHAR
*cookie_domain
= NULL
, *cookie_path
= NULL
;
187 session_t
*session
= request
->connect
->session
;
191 len
= strlenW( cookies
);
192 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
193 strcpyW( buffer
, cookies
);
196 while (*p
&& *p
!= ';') p
++;
197 if (*p
== ';') *p
++ = 0;
198 if (!(cookie
= parse_cookie( buffer
)))
203 if ((q
= strstrW( p
, domainW
))) /* FIXME: do real attribute parsing */
205 while (*q
&& *q
!= '=') q
++;
209 while (*r
&& *r
!= ';') r
++;
212 if (!(cookie_domain
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
213 memcpy( cookie_domain
, q
, len
* sizeof(WCHAR
) );
214 cookie_domain
[len
] = 0;
217 if ((q
= strstrW( p
, pathW
)))
219 while (*q
&& *q
!= '=') q
++;
223 while (*r
&& *r
!= ';') r
++;
226 if (!(cookie_path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
227 memcpy( cookie_path
, q
, len
* sizeof(WCHAR
) );
228 cookie_path
[len
] = 0;
230 if (!cookie_domain
&& !(cookie_domain
= strdupW( request
->connect
->servername
))) goto end
;
231 if (!cookie_path
&& !(cookie_path
= strdupW( request
->path
))) goto end
;
233 if ((p
= strrchrW( cookie_path
, '/' )) && p
!= cookie_path
) *p
= 0;
234 ret
= add_cookie( session
, cookie
, cookie_domain
, cookie_path
);
237 if (!ret
) free_cookie( cookie
);
238 heap_free( cookie_domain
);
239 heap_free( cookie_path
);
244 BOOL
add_cookie_headers( request_t
*request
)
246 struct list
*domain_cursor
;
247 session_t
*session
= request
->connect
->session
;
249 LIST_FOR_EACH( domain_cursor
, &session
->cookie_cache
)
251 domain_t
*domain
= LIST_ENTRY( domain_cursor
, domain_t
, entry
);
252 if (domain_match( request
->connect
->servername
, domain
, TRUE
))
254 struct list
*cookie_cursor
;
255 TRACE("found domain %s\n", debugstr_w(domain
->name
));
257 LIST_FOR_EACH( cookie_cursor
, &domain
->cookies
)
259 cookie_t
*cookie
= LIST_ENTRY( cookie_cursor
, cookie_t
, entry
);
261 TRACE("comparing path %s with %s\n", debugstr_w(request
->path
), debugstr_w(cookie
->path
));
263 if (strstrW( request
->path
, cookie
->path
) == request
->path
)
265 const WCHAR format
[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
269 len
= strlenW( cookie
->name
) + strlenW( format
) + strlenW( cookie
->value
);
270 if (!(header
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
272 sprintfW( header
, format
, cookie
->name
, cookie
->value
);
274 TRACE("%s\n", debugstr_w(header
));
275 add_request_headers( request
, header
, len
, WINHTTP_ADDREQ_FLAG_ADD
);