@@ -47,7 +47,18 @@ void CookieManager::Initialize(HWND window_handle) {
47
47
48
48
bool CookieManager::SetCookie (const std::string& url,
49
49
const std::string& cookie_data) {
50
+ return this ->SetCookie (url, cookie_data, false );
51
+ }
52
+
53
+ bool CookieManager::SetCookie (const std::string& url,
54
+ const std::string& cookie_data,
55
+ const bool is_httponly) {
50
56
std::string full_data = url + " |" + cookie_data;
57
+ WPARAM set_flags = 0 ;
58
+ if (is_httponly) {
59
+ set_flags = INTERNET_COOKIE_HTTPONLY;
60
+ }
61
+
51
62
HookSettings hook_settings;
52
63
hook_settings.hook_procedure_name = " CookieWndProc" ;
53
64
hook_settings.hook_procedure_type = WH_CALLWNDPROC;
@@ -57,7 +68,7 @@ bool CookieManager::SetCookie(const std::string& url,
57
68
HookProcessor hook;
58
69
hook.Initialize (hook_settings);
59
70
hook.PushData (StringUtilities::ToWString (full_data));
60
- ::SendMessage (this ->window_handle_, WD_SET_COOKIE, NULL , NULL );
71
+ ::SendMessage (this ->window_handle_, WD_SET_COOKIE, set_flags , NULL );
61
72
int status = HookProcessor::GetDataBufferSize ();
62
73
if (status != 0 ) {
63
74
return false ;
@@ -153,8 +164,7 @@ int CookieManager::GetCookies(const std::string& url,
153
164
}
154
165
155
166
bool CookieManager::DeleteCookie (const std::string& url,
156
- const std::string& cookie_name) {
157
-
167
+ const BrowserCookie& cookie) {
158
168
std::wstring wide_url = StringUtilities::ToWString (url);
159
169
CComPtr<IUri> uri_pointer;
160
170
::CreateUri (wide_url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &uri_pointer);
@@ -169,56 +179,98 @@ bool CookieManager::DeleteCookie(const std::string& url,
169
179
170
180
std::string domain = StringUtilities::ToString (wide_domain);
171
181
std::string path = StringUtilities::ToString (wide_path);
172
- return this ->RecursivelyDeleteCookie (url, cookie_name, domain, path);
182
+ return this ->RecursivelyDeleteCookie (url,
183
+ cookie.name (),
184
+ domain,
185
+ path,
186
+ cookie.is_httponly ());
173
187
}
174
188
189
+ // bool CookieManager::DeleteCookie(const std::string& url,
190
+ // const std::string& cookie_name) {
191
+ //
192
+ // std::wstring wide_url = StringUtilities::ToWString(url);
193
+ // CComPtr<IUri> uri_pointer;
194
+ // ::CreateUri(wide_url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &uri_pointer);
195
+ //
196
+ // CComBSTR host_bstr;
197
+ // uri_pointer->GetHost(&host_bstr);
198
+ // std::wstring wide_domain = host_bstr;
199
+ //
200
+ // CComBSTR path_bstr;
201
+ // uri_pointer->GetPath(&path_bstr);
202
+ // std::wstring wide_path = path_bstr;
203
+ //
204
+ // std::string domain = StringUtilities::ToString(wide_domain);
205
+ // std::string path = StringUtilities::ToString(wide_path);
206
+ // return this->RecursivelyDeleteCookie(url, cookie_name, domain, path, false);
207
+ // }
208
+
175
209
bool CookieManager::RecursivelyDeleteCookie (const std::string& url,
176
210
const std::string& name,
177
211
const std::string& domain,
178
- const std::string& path) {
212
+ const std::string& path,
213
+ const bool is_httponly) {
179
214
// TODO: Optimize this path from the recursive to only
180
215
// call setting the cookie as often as needed.
181
- return this ->RecurseCookiePath (url, name, " ." + domain, path);
216
+ return this ->RecurseCookiePath (url, name, " ." + domain, path, is_httponly );
182
217
}
183
218
184
219
bool CookieManager::RecurseCookiePath (const std::string& url,
185
220
const std::string& name,
186
221
const std::string& domain,
187
- const std::string& path) {
222
+ const std::string& path,
223
+ const bool is_httponly) {
188
224
size_t number_of_characters = 0 ;
189
225
size_t slash_index = path.find_last_of (' /' );
190
226
size_t final_index = path.size () - 1 ;
191
- if (slash_index == final_index && slash_index != 0 ) {
227
+ if (slash_index == final_index) {
192
228
number_of_characters = slash_index;
229
+ } else {
230
+ number_of_characters = slash_index + 1 ;
193
231
}
194
232
195
233
if (slash_index != std::string::npos) {
196
- bool deleted = this ->RecurseCookiePath (url, name, domain, path.substr (0 , number_of_characters));
234
+ bool deleted = this ->RecurseCookiePath (url,
235
+ name,
236
+ domain,
237
+ path.substr (0 , number_of_characters),
238
+ is_httponly);
197
239
}
198
- return this ->RecurseCookieDomain (url, name, domain, path);
240
+ return this ->RecurseCookieDomain (url, name, domain, path, is_httponly );
199
241
}
200
242
201
243
bool CookieManager::RecurseCookieDomain (const std::string& url,
202
244
const std::string& name,
203
245
const std::string& domain,
204
- const std::string& path) {
205
- bool deleted = this ->DeleteCookie (url, name, domain, path);
246
+ const std::string& path,
247
+ const bool is_httponly) {
248
+ bool deleted = this ->DeleteCookie (url, name, domain, path, is_httponly);
206
249
if (!deleted) {
207
250
size_t dot_index = domain.find_first_of (' .' );
208
251
if (dot_index == 0 ) {
209
- return this ->RecurseCookieDomain (url, name, domain.substr (1 ), path);
252
+ return this ->RecurseCookieDomain (url,
253
+ name,
254
+ domain.substr (1 ),
255
+ path,
256
+ is_httponly);
210
257
} else if (dot_index != std::string::npos) {
211
- return this ->RecurseCookieDomain (url, name, domain.substr (dot_index), path);
258
+ return this ->RecurseCookieDomain (url,
259
+ name,
260
+ domain.substr (dot_index),
261
+ path,
262
+ is_httponly);
212
263
}
213
- deleted = this ->DeleteCookie (url, name, " " , path);
264
+ deleted = this ->DeleteCookie (url, name, " " , path, is_httponly );
214
265
}
215
266
return deleted;
216
267
}
217
268
218
269
bool CookieManager::DeleteCookie (const std::string& url,
219
270
const std::string& name,
220
271
const std::string& domain,
221
- const std::string& path) {
272
+ const std::string& path,
273
+ const bool is_httponly) {
222
274
// N.B., We can hard-code the value and expiration time, since
223
275
// we are deleting the cookie.
224
276
std::string cookie_data = name;
@@ -232,7 +284,7 @@ bool CookieManager::DeleteCookie(const std::string& url,
232
284
cookie_data.append (path);
233
285
}
234
286
cookie_data.append (" ; expires=Thu 1 Jan 1970 00:00:01 GMT" );
235
- return this ->SetCookie (url, cookie_data);
287
+ return this ->SetCookie (url, cookie_data, is_httponly );
236
288
}
237
289
238
290
void CookieManager::ReadPersistentCookieFile (const std::wstring& file_name,
@@ -526,6 +578,7 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
526
578
webdriver::HookProcessor::CopyWStringToBuffer (file_list);
527
579
webdriver::HookProcessor::WriteBufferToPipe (driver_process_id);
528
580
} else if (WD_SET_COOKIE == call_window_proc_struct->message ) {
581
+ DWORD set_cookie_flags = static_cast <DWORD>(call_window_proc_struct->wParam );
529
582
std::wstring cookie_data = webdriver::HookProcessor::CopyWStringFromBuffer ();
530
583
size_t url_separator_pos = cookie_data.find_first_of (L" |" );
531
584
std::wstring url = cookie_data.substr (0 , url_separator_pos);
@@ -543,7 +596,11 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
543
596
544
597
// Leverage the shared data buffer size to return the error code
545
598
// back to the driver, if necessary.
546
- DWORD cookie_set = ::InternetSetCookieEx (parsed_uri.c_str (), NULL , cookie.c_str (), INTERNET_COOKIE_HTTPONLY, NULL );
599
+ DWORD cookie_set = ::InternetSetCookieEx (parsed_uri.c_str (),
600
+ NULL ,
601
+ cookie.c_str (),
602
+ set_cookie_flags,
603
+ NULL );
547
604
if (cookie_set) {
548
605
webdriver::HookProcessor::SetDataBufferSize (0 );
549
606
} else {
0 commit comments