[WININET] initialize winsock with proper version. Based on wine tests CORE-12104
[reactos.git] / reactos / dll / win32 / winhttp / session.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 #include <wincrypt.h>
22 #include <winreg.h>
23 #include <dispex.h>
24 #include <activscp.h>
25
26 #define DEFAULT_RESOLVE_TIMEOUT 0
27 #define DEFAULT_CONNECT_TIMEOUT 20000
28 #define DEFAULT_SEND_TIMEOUT 30000
29 #define DEFAULT_RECEIVE_TIMEOUT 30000
30
31 void set_last_error( DWORD error )
32 {
33 /* FIXME */
34 SetLastError( error );
35 }
36
37 DWORD get_last_error( void )
38 {
39 /* FIXME */
40 return GetLastError();
41 }
42
43 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
44 {
45 if (hdr->callback && (hdr->notify_mask & status))
46 {
47 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
48 hdr->callback( hdr->handle, hdr->context, status, info, buflen );
49 TRACE("returning from 0x%08x callback\n", status);
50 }
51 }
52
53 /***********************************************************************
54 * WinHttpCheckPlatform (winhttp.@)
55 */
56 BOOL WINAPI WinHttpCheckPlatform( void )
57 {
58 TRACE("\n");
59 return TRUE;
60 }
61
62 /***********************************************************************
63 * session_destroy (internal)
64 */
65 static void session_destroy( object_header_t *hdr )
66 {
67 session_t *session = (session_t *)hdr;
68 struct list *item, *next;
69 domain_t *domain;
70
71 TRACE("%p\n", session);
72
73 if (session->unload_event) SetEvent( session->unload_event );
74
75 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
76 {
77 domain = LIST_ENTRY( item, domain_t, entry );
78 delete_domain( domain );
79 }
80 heap_free( session->agent );
81 heap_free( session->proxy_server );
82 heap_free( session->proxy_bypass );
83 heap_free( session->proxy_username );
84 heap_free( session->proxy_password );
85 heap_free( session );
86 #ifdef __REACTOS__
87 WSACleanup();
88 #endif
89 }
90
91 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
92 {
93 session_t *session = (session_t *)hdr;
94
95 switch (option)
96 {
97 case WINHTTP_OPTION_REDIRECT_POLICY:
98 {
99 if (!buffer || *buflen < sizeof(DWORD))
100 {
101 *buflen = sizeof(DWORD);
102 set_last_error( ERROR_INSUFFICIENT_BUFFER );
103 return FALSE;
104 }
105
106 *(DWORD *)buffer = hdr->redirect_policy;
107 *buflen = sizeof(DWORD);
108 return TRUE;
109 }
110 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
111 *(DWORD *)buffer = session->resolve_timeout;
112 *buflen = sizeof(DWORD);
113 return TRUE;
114 case WINHTTP_OPTION_CONNECT_TIMEOUT:
115 *(DWORD *)buffer = session->connect_timeout;
116 *buflen = sizeof(DWORD);
117 return TRUE;
118 case WINHTTP_OPTION_SEND_TIMEOUT:
119 *(DWORD *)buffer = session->send_timeout;
120 *buflen = sizeof(DWORD);
121 return TRUE;
122 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
123 *(DWORD *)buffer = session->recv_timeout;
124 *buflen = sizeof(DWORD);
125 return TRUE;
126 default:
127 FIXME("unimplemented option %u\n", option);
128 set_last_error( ERROR_INVALID_PARAMETER );
129 return FALSE;
130 }
131 }
132
133 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
134 {
135 session_t *session = (session_t *)hdr;
136
137 switch (option)
138 {
139 case WINHTTP_OPTION_PROXY:
140 {
141 WINHTTP_PROXY_INFO *pi = buffer;
142
143 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
144 return TRUE;
145 }
146 case WINHTTP_OPTION_REDIRECT_POLICY:
147 {
148 DWORD policy;
149
150 if (buflen != sizeof(policy))
151 {
152 set_last_error( ERROR_INSUFFICIENT_BUFFER );
153 return FALSE;
154 }
155
156 policy = *(DWORD *)buffer;
157 TRACE("0x%x\n", policy);
158 hdr->redirect_policy = policy;
159 return TRUE;
160 }
161 case WINHTTP_OPTION_DISABLE_FEATURE:
162 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
163 return FALSE;
164 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
165 session->resolve_timeout = *(DWORD *)buffer;
166 return TRUE;
167 case WINHTTP_OPTION_CONNECT_TIMEOUT:
168 session->connect_timeout = *(DWORD *)buffer;
169 return TRUE;
170 case WINHTTP_OPTION_SEND_TIMEOUT:
171 session->send_timeout = *(DWORD *)buffer;
172 return TRUE;
173 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
174 session->recv_timeout = *(DWORD *)buffer;
175 return TRUE;
176 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
177 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD *)buffer);
178 return TRUE;
179 case WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT:
180 TRACE("WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT: %p\n", *(HANDLE *)buffer);
181 session->unload_event = *(HANDLE *)buffer;
182 return TRUE;
183 default:
184 FIXME("unimplemented option %u\n", option);
185 set_last_error( ERROR_INVALID_PARAMETER );
186 return FALSE;
187 }
188 }
189
190 static const object_vtbl_t session_vtbl =
191 {
192 session_destroy,
193 session_query_option,
194 session_set_option
195 };
196
197 /***********************************************************************
198 * WinHttpOpen (winhttp.@)
199 */
200 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
201 {
202 session_t *session;
203 HINTERNET handle = NULL;
204 #ifdef __REACTOS__
205 WSADATA wsaData;
206 int error = WSAStartup(MAKEWORD(2, 2), &wsaData);
207 if (error) ERR("WSAStartup failed: %d\n", error);
208 #endif
209
210 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
211
212 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
213
214 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
215 session->hdr.vtbl = &session_vtbl;
216 session->hdr.flags = flags;
217 session->hdr.refs = 1;
218 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
219 list_init( &session->hdr.children );
220 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
221 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
222 session->send_timeout = DEFAULT_SEND_TIMEOUT;
223 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
224 list_init( &session->cookie_cache );
225
226 if (agent && !(session->agent = strdupW( agent ))) goto end;
227 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
228 {
229 WINHTTP_PROXY_INFO info;
230
231 WinHttpGetDefaultProxyConfiguration( &info );
232 session->access = info.dwAccessType;
233 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
234 {
235 GlobalFree( (LPWSTR)info.lpszProxy );
236 GlobalFree( (LPWSTR)info.lpszProxyBypass );
237 goto end;
238 }
239 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
240 {
241 GlobalFree( (LPWSTR)info.lpszProxy );
242 GlobalFree( (LPWSTR)info.lpszProxyBypass );
243 goto end;
244 }
245 }
246 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
247 {
248 session->access = access;
249 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
250 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
251 }
252
253 if (!(handle = alloc_handle( &session->hdr ))) goto end;
254 session->hdr.handle = handle;
255
256 end:
257 release_object( &session->hdr );
258 TRACE("returning %p\n", handle);
259 if (handle) set_last_error( ERROR_SUCCESS );
260 return handle;
261 }
262
263 /***********************************************************************
264 * connect_destroy (internal)
265 */
266 static void connect_destroy( object_header_t *hdr )
267 {
268 connect_t *connect = (connect_t *)hdr;
269
270 TRACE("%p\n", connect);
271
272 release_object( &connect->session->hdr );
273
274 heap_free( connect->hostname );
275 heap_free( connect->servername );
276 heap_free( connect->username );
277 heap_free( connect->password );
278 heap_free( connect );
279 }
280
281 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
282 {
283 connect_t *connect = (connect_t *)hdr;
284
285 switch (option)
286 {
287 case WINHTTP_OPTION_PARENT_HANDLE:
288 {
289 if (!buffer || *buflen < sizeof(HINTERNET))
290 {
291 *buflen = sizeof(HINTERNET);
292 set_last_error( ERROR_INSUFFICIENT_BUFFER );
293 return FALSE;
294 }
295
296 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
297 *buflen = sizeof(HINTERNET);
298 return TRUE;
299 }
300 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
301 *(DWORD *)buffer = connect->session->resolve_timeout;
302 *buflen = sizeof(DWORD);
303 return TRUE;
304 case WINHTTP_OPTION_CONNECT_TIMEOUT:
305 *(DWORD *)buffer = connect->session->connect_timeout;
306 *buflen = sizeof(DWORD);
307 return TRUE;
308 case WINHTTP_OPTION_SEND_TIMEOUT:
309 *(DWORD *)buffer = connect->session->send_timeout;
310 *buflen = sizeof(DWORD);
311 return TRUE;
312 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
313 *(DWORD *)buffer = connect->session->recv_timeout;
314 *buflen = sizeof(DWORD);
315 return TRUE;
316 default:
317 FIXME("unimplemented option %u\n", option);
318 set_last_error( ERROR_INVALID_PARAMETER );
319 return FALSE;
320 }
321 }
322
323 static const object_vtbl_t connect_vtbl =
324 {
325 connect_destroy,
326 connect_query_option,
327 NULL
328 };
329
330 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
331 {
332 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
333 BOOL ret = FALSE;
334
335 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
336 ret = TRUE;
337 else if (*domain == '*')
338 {
339 if (domain[1] == '.')
340 {
341 LPCWSTR dot;
342
343 /* For a hostname to match a wildcard, the last domain must match
344 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
345 * hostname is www.foo.a.b, it matches, but a.b does not.
346 */
347 dot = strchrW( server, '.' );
348 if (dot)
349 {
350 int len = strlenW( dot + 1 );
351
352 if (len > strlenW( domain + 2 ))
353 {
354 LPCWSTR ptr;
355
356 /* The server's domain is longer than the wildcard, so it
357 * could be a subdomain. Compare the last portion of the
358 * server's domain.
359 */
360 ptr = dot + len + 1 - strlenW( domain + 2 );
361 if (!strcmpiW( ptr, domain + 2 ))
362 {
363 /* This is only a match if the preceding character is
364 * a '.', i.e. that it is a matching domain. E.g.
365 * if domain is '*.b.c' and server is 'www.ab.c' they
366 * do not match.
367 */
368 ret = *(ptr - 1) == '.';
369 }
370 }
371 else
372 ret = !strcmpiW( dot + 1, domain + 2 );
373 }
374 }
375 }
376 else
377 ret = !strcmpiW( server, domain );
378 return ret;
379 }
380
381 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
382 #define MAX_HOST_NAME_LENGTH 256
383
384 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
385 {
386 LPCWSTR ptr;
387 BOOL ret = FALSE;
388
389 if (!session->proxy_bypass) return FALSE;
390 ptr = session->proxy_bypass;
391 do {
392 LPCWSTR tmp = ptr;
393
394 ptr = strchrW( ptr, ';' );
395 if (!ptr)
396 ptr = strchrW( tmp, ' ' );
397 if (ptr)
398 {
399 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
400 {
401 WCHAR domain[MAX_HOST_NAME_LENGTH];
402
403 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
404 domain[ptr - tmp] = 0;
405 ret = domain_matches( server, domain );
406 }
407 ptr += 1;
408 }
409 else if (*tmp)
410 ret = domain_matches( server, tmp );
411 } while (ptr && !ret);
412 return ret;
413 }
414
415 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
416 {
417 session_t *session = connect->session;
418 BOOL ret = TRUE;
419
420 if (session->proxy_server && !should_bypass_proxy(session, server))
421 {
422 LPCWSTR colon;
423
424 if ((colon = strchrW( session->proxy_server, ':' )))
425 {
426 if (!connect->servername || strncmpiW( connect->servername,
427 session->proxy_server, colon - session->proxy_server - 1 ))
428 {
429 heap_free( connect->servername );
430 connect->resolved = FALSE;
431 if (!(connect->servername = heap_alloc(
432 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
433 {
434 ret = FALSE;
435 goto end;
436 }
437 memcpy( connect->servername, session->proxy_server,
438 (colon - session->proxy_server) * sizeof(WCHAR) );
439 connect->servername[colon - session->proxy_server] = 0;
440 if (*(colon + 1))
441 connect->serverport = atoiW( colon + 1 );
442 else
443 connect->serverport = INTERNET_DEFAULT_PORT;
444 }
445 }
446 else
447 {
448 if (!connect->servername || strcmpiW( connect->servername,
449 session->proxy_server ))
450 {
451 heap_free( connect->servername );
452 connect->resolved = FALSE;
453 if (!(connect->servername = strdupW( session->proxy_server )))
454 {
455 ret = FALSE;
456 goto end;
457 }
458 connect->serverport = INTERNET_DEFAULT_PORT;
459 }
460 }
461 }
462 else if (server)
463 {
464 heap_free( connect->servername );
465 connect->resolved = FALSE;
466 if (!(connect->servername = strdupW( server )))
467 {
468 ret = FALSE;
469 goto end;
470 }
471 connect->serverport = port;
472 }
473 end:
474 return ret;
475 }
476
477 /***********************************************************************
478 * WinHttpConnect (winhttp.@)
479 */
480 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
481 {
482 connect_t *connect;
483 session_t *session;
484 HINTERNET hconnect = NULL;
485
486 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
487
488 if (!server)
489 {
490 set_last_error( ERROR_INVALID_PARAMETER );
491 return NULL;
492 }
493 if (!(session = (session_t *)grab_object( hsession )))
494 {
495 set_last_error( ERROR_INVALID_HANDLE );
496 return NULL;
497 }
498 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
499 {
500 release_object( &session->hdr );
501 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
502 return NULL;
503 }
504 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
505 {
506 release_object( &session->hdr );
507 return NULL;
508 }
509 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
510 connect->hdr.vtbl = &connect_vtbl;
511 connect->hdr.refs = 1;
512 connect->hdr.flags = session->hdr.flags;
513 connect->hdr.callback = session->hdr.callback;
514 connect->hdr.notify_mask = session->hdr.notify_mask;
515 connect->hdr.context = session->hdr.context;
516 connect->hdr.redirect_policy = session->hdr.redirect_policy;
517 list_init( &connect->hdr.children );
518
519 addref_object( &session->hdr );
520 connect->session = session;
521 list_add_head( &session->hdr.children, &connect->hdr.entry );
522
523 if (!(connect->hostname = strdupW( server ))) goto end;
524 connect->hostport = port;
525 if (!set_server_for_hostname( connect, server, port )) goto end;
526
527 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
528 connect->hdr.handle = hconnect;
529
530 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
531
532 end:
533 release_object( &connect->hdr );
534 release_object( &session->hdr );
535 TRACE("returning %p\n", hconnect);
536 if (hconnect) set_last_error( ERROR_SUCCESS );
537 return hconnect;
538 }
539
540 /***********************************************************************
541 * request_destroy (internal)
542 */
543 static void request_destroy( object_header_t *hdr )
544 {
545 request_t *request = (request_t *)hdr;
546 unsigned int i, j;
547
548 TRACE("%p\n", request);
549
550 if (request->task_thread)
551 {
552 /* Signal to the task proc to quit. It will call
553 this again when it does. */
554 HANDLE thread = request->task_thread;
555 request->task_thread = 0;
556 SetEvent( request->task_cancel );
557 CloseHandle( thread );
558 return;
559 }
560 release_object( &request->connect->hdr );
561
562 destroy_authinfo( request->authinfo );
563 destroy_authinfo( request->proxy_authinfo );
564
565 heap_free( request->verb );
566 heap_free( request->path );
567 heap_free( request->version );
568 heap_free( request->raw_headers );
569 heap_free( request->status_text );
570 for (i = 0; i < request->num_headers; i++)
571 {
572 heap_free( request->headers[i].field );
573 heap_free( request->headers[i].value );
574 }
575 heap_free( request->headers );
576 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
577 heap_free( request->accept_types );
578 for (i = 0; i < TARGET_MAX; i++)
579 {
580 for (j = 0; j < SCHEME_MAX; j++)
581 {
582 heap_free( request->creds[i][j].username );
583 heap_free( request->creds[i][j].password );
584 }
585 }
586 heap_free( request );
587 }
588
589 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
590 {
591 int len = 0;
592 if (str) len = strlenW( str );
593 if (buffer && *buflen > len)
594 {
595 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
596 buffer[len] = 0;
597 }
598 *buflen = len * sizeof(WCHAR);
599 }
600
601 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
602 {
603 WCHAR *ret;
604 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
605
606 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
607 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
608 CertNameToStrW( encoding, blob, format, ret, size );
609
610 return ret;
611 }
612
613 static BOOL convert_sockaddr( const struct sockaddr *addr, SOCKADDR_STORAGE *addr_storage )
614 {
615 #if !defined(__MINGW32__) && !defined(_MSC_VER)
616 switch (addr->sa_family)
617 {
618 case AF_INET:
619 {
620 const struct sockaddr_in *addr_unix = (const struct sockaddr_in *)addr;
621 struct WS_sockaddr_in *addr_win = (struct WS_sockaddr_in *)addr_storage;
622 char *p;
623
624 addr_win->sin_family = WS_AF_INET;
625 addr_win->sin_port = addr_unix->sin_port;
626 memcpy( &addr_win->sin_addr, &addr_unix->sin_addr, 4 );
627 p = (char *)&addr_win->sin_addr + 4;
628 memset( p, 0, sizeof(*addr_storage) - (p - (char *)addr_win) );
629 return TRUE;
630 }
631 case AF_INET6:
632 {
633 const struct sockaddr_in6 *addr_unix = (const struct sockaddr_in6 *)addr;
634 struct WS_sockaddr_in6 *addr_win = (struct WS_sockaddr_in6 *)addr_storage;
635
636 addr_win->sin6_family = WS_AF_INET6;
637 addr_win->sin6_port = addr_unix->sin6_port;
638 addr_win->sin6_flowinfo = addr_unix->sin6_flowinfo;
639 memcpy( &addr_win->sin6_addr, &addr_unix->sin6_addr, 16 );
640 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
641 addr_win->sin6_scope_id = addr_unix->sin6_scope_id;
642 #else
643 addr_win->sin6_scope_id = 0;
644 #endif
645 memset( addr_win + 1, 0, sizeof(*addr_storage) - sizeof(*addr_win) );
646 return TRUE;
647 }
648 default:
649 ERR("unhandled family %u\n", addr->sa_family);
650 return FALSE;
651 }
652 #else
653 switch (addr->sa_family)
654 {
655 case AF_INET:
656 {
657 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_storage;
658
659 memcpy( addr_in, addr, sizeof(*addr_in) );
660 memset( addr_in + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in) );
661 return TRUE;
662 }
663 case AF_INET6:
664 {
665 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr_storage;
666
667 memcpy( addr_in6, addr, sizeof(*addr_in6) );
668 memset( addr_in6 + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in6) );
669 return TRUE;
670 }
671 default:
672 ERR("unhandled family %u\n", addr->sa_family);
673 return FALSE;
674 }
675 #endif
676 }
677
678 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
679 {
680 request_t *request = (request_t *)hdr;
681
682 switch (option)
683 {
684 case WINHTTP_OPTION_SECURITY_FLAGS:
685 {
686 DWORD flags;
687 int bits;
688
689 if (!buffer || *buflen < sizeof(flags))
690 {
691 *buflen = sizeof(flags);
692 set_last_error( ERROR_INSUFFICIENT_BUFFER );
693 return FALSE;
694 }
695
696 flags = 0;
697 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
698 flags |= request->netconn.security_flags;
699 bits = netconn_get_cipher_strength( &request->netconn );
700 if (bits >= 128)
701 flags |= SECURITY_FLAG_STRENGTH_STRONG;
702 else if (bits >= 56)
703 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
704 else
705 flags |= SECURITY_FLAG_STRENGTH_WEAK;
706 *(DWORD *)buffer = flags;
707 *buflen = sizeof(flags);
708 return TRUE;
709 }
710 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
711 {
712 const CERT_CONTEXT *cert;
713
714 if (!buffer || *buflen < sizeof(cert))
715 {
716 *buflen = sizeof(cert);
717 set_last_error( ERROR_INSUFFICIENT_BUFFER );
718 return FALSE;
719 }
720
721 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
722 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
723 *buflen = sizeof(cert);
724 return TRUE;
725 }
726 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
727 {
728 const CERT_CONTEXT *cert;
729 const CRYPT_OID_INFO *oidInfo;
730 WINHTTP_CERTIFICATE_INFO *ci = buffer;
731
732 FIXME("partial stub\n");
733
734 if (!buffer || *buflen < sizeof(*ci))
735 {
736 *buflen = sizeof(*ci);
737 set_last_error( ERROR_INSUFFICIENT_BUFFER );
738 return FALSE;
739 }
740 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
741
742 ci->ftExpiry = cert->pCertInfo->NotAfter;
743 ci->ftStart = cert->pCertInfo->NotBefore;
744 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
745 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
746 ci->lpszProtocolName = NULL;
747 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
748 cert->pCertInfo->SignatureAlgorithm.pszObjId,
749 0 );
750 if (oidInfo)
751 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
752 else
753 ci->lpszSignatureAlgName = NULL;
754 ci->lpszEncryptionAlgName = NULL;
755 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
756
757 CertFreeCertificateContext( cert );
758 *buflen = sizeof(*ci);
759 return TRUE;
760 }
761 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
762 {
763 if (!buffer || *buflen < sizeof(DWORD))
764 {
765 *buflen = sizeof(DWORD);
766 set_last_error( ERROR_INSUFFICIENT_BUFFER );
767 return FALSE;
768 }
769
770 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
771 *buflen = sizeof(DWORD);
772 return TRUE;
773 }
774 case WINHTTP_OPTION_CONNECTION_INFO:
775 {
776 WINHTTP_CONNECTION_INFO *info = buffer;
777 struct sockaddr local;
778 socklen_t len = sizeof(local);
779 const struct sockaddr *remote = (const struct sockaddr *)&request->connect->sockaddr;
780
781 if (!buffer || *buflen < sizeof(*info))
782 {
783 *buflen = sizeof(*info);
784 set_last_error( ERROR_INSUFFICIENT_BUFFER );
785 return FALSE;
786 }
787 if (!netconn_connected( &request->netconn ))
788 {
789 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
790 return FALSE;
791 }
792 if (getsockname( request->netconn.socket, &local, &len )) return FALSE;
793 if (!convert_sockaddr( &local, &info->LocalAddress )) return FALSE;
794 if (!convert_sockaddr( remote, &info->RemoteAddress )) return FALSE;
795 info->cbSize = sizeof(*info);
796 return TRUE;
797 }
798 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
799 *(DWORD *)buffer = request->resolve_timeout;
800 *buflen = sizeof(DWORD);
801 return TRUE;
802 case WINHTTP_OPTION_CONNECT_TIMEOUT:
803 *(DWORD *)buffer = request->connect_timeout;
804 *buflen = sizeof(DWORD);
805 return TRUE;
806 case WINHTTP_OPTION_SEND_TIMEOUT:
807 *(DWORD *)buffer = request->send_timeout;
808 *buflen = sizeof(DWORD);
809 return TRUE;
810 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
811 *(DWORD *)buffer = request->recv_timeout;
812 *buflen = sizeof(DWORD);
813 return TRUE;
814
815 case WINHTTP_OPTION_USERNAME:
816 str_to_buffer( buffer, request->connect->username, buflen );
817 return TRUE;
818
819 case WINHTTP_OPTION_PASSWORD:
820 str_to_buffer( buffer, request->connect->password, buflen );
821 return TRUE;
822
823 case WINHTTP_OPTION_PROXY_USERNAME:
824 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
825 return TRUE;
826
827 case WINHTTP_OPTION_PROXY_PASSWORD:
828 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
829 return TRUE;
830
831 default:
832 FIXME("unimplemented option %u\n", option);
833 set_last_error( ERROR_INVALID_PARAMETER );
834 return FALSE;
835 }
836 }
837
838 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
839 {
840 WCHAR *ret;
841 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
842 {
843 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
844 ret[buflen] = 0;
845 return ret;
846 }
847 set_last_error( ERROR_OUTOFMEMORY );
848 return NULL;
849 }
850
851 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
852 {
853 request_t *request = (request_t *)hdr;
854
855 switch (option)
856 {
857 case WINHTTP_OPTION_PROXY:
858 {
859 WINHTTP_PROXY_INFO *pi = buffer;
860
861 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
862 return TRUE;
863 }
864 case WINHTTP_OPTION_DISABLE_FEATURE:
865 {
866 DWORD disable;
867
868 if (buflen != sizeof(DWORD))
869 {
870 set_last_error( ERROR_INSUFFICIENT_BUFFER );
871 return FALSE;
872 }
873
874 disable = *(DWORD *)buffer;
875 TRACE("0x%x\n", disable);
876 hdr->disable_flags |= disable;
877 return TRUE;
878 }
879 case WINHTTP_OPTION_AUTOLOGON_POLICY:
880 {
881 DWORD policy;
882
883 if (buflen != sizeof(DWORD))
884 {
885 set_last_error( ERROR_INSUFFICIENT_BUFFER );
886 return FALSE;
887 }
888
889 policy = *(DWORD *)buffer;
890 TRACE("0x%x\n", policy);
891 hdr->logon_policy = policy;
892 return TRUE;
893 }
894 case WINHTTP_OPTION_REDIRECT_POLICY:
895 {
896 DWORD policy;
897
898 if (buflen != sizeof(DWORD))
899 {
900 set_last_error( ERROR_INSUFFICIENT_BUFFER );
901 return FALSE;
902 }
903
904 policy = *(DWORD *)buffer;
905 TRACE("0x%x\n", policy);
906 hdr->redirect_policy = policy;
907 return TRUE;
908 }
909 case WINHTTP_OPTION_SECURITY_FLAGS:
910 {
911 DWORD flags;
912
913 if (buflen < sizeof(DWORD))
914 {
915 set_last_error( ERROR_INSUFFICIENT_BUFFER );
916 return FALSE;
917 }
918 flags = *(DWORD *)buffer;
919 TRACE("0x%x\n", flags);
920 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
921 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
922 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
923 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
924 {
925 set_last_error( ERROR_INVALID_PARAMETER );
926 return FALSE;
927 }
928 request->netconn.security_flags = flags;
929 return TRUE;
930 }
931 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
932 request->resolve_timeout = *(DWORD *)buffer;
933 return TRUE;
934 case WINHTTP_OPTION_CONNECT_TIMEOUT:
935 request->connect_timeout = *(DWORD *)buffer;
936 return TRUE;
937 case WINHTTP_OPTION_SEND_TIMEOUT:
938 request->send_timeout = *(DWORD *)buffer;
939 return TRUE;
940 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
941 request->recv_timeout = *(DWORD *)buffer;
942 return TRUE;
943
944 case WINHTTP_OPTION_USERNAME:
945 {
946 connect_t *connect = request->connect;
947
948 heap_free( connect->username );
949 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
950 return TRUE;
951 }
952 case WINHTTP_OPTION_PASSWORD:
953 {
954 connect_t *connect = request->connect;
955
956 heap_free( connect->password );
957 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
958 return TRUE;
959 }
960 case WINHTTP_OPTION_PROXY_USERNAME:
961 {
962 session_t *session = request->connect->session;
963
964 heap_free( session->proxy_username );
965 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
966 return TRUE;
967 }
968 case WINHTTP_OPTION_PROXY_PASSWORD:
969 {
970 session_t *session = request->connect->session;
971
972 heap_free( session->proxy_password );
973 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
974 return TRUE;
975 }
976 default:
977 FIXME("unimplemented option %u\n", option);
978 set_last_error( ERROR_INVALID_PARAMETER );
979 return TRUE;
980 }
981 }
982
983 static const object_vtbl_t request_vtbl =
984 {
985 request_destroy,
986 request_query_option,
987 request_set_option
988 };
989
990 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
991 {
992 const WCHAR **types = accept_types;
993 DWORD i;
994
995 if (!types) return TRUE;
996 while (*types)
997 {
998 request->num_accept_types++;
999 types++;
1000 }
1001 if (!request->num_accept_types) return TRUE;
1002 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
1003 {
1004 request->num_accept_types = 0;
1005 return FALSE;
1006 }
1007 types = accept_types;
1008 for (i = 0; i < request->num_accept_types; i++)
1009 {
1010 if (!(request->accept_types[i] = strdupW( *types )))
1011 {
1012 for ( ; i > 0; --i) heap_free( request->accept_types[i - 1] );
1013 heap_free( request->accept_types );
1014 request->accept_types = NULL;
1015 request->num_accept_types = 0;
1016 return FALSE;
1017 }
1018 types++;
1019 }
1020 return TRUE;
1021 }
1022
1023 /***********************************************************************
1024 * WinHttpOpenRequest (winhttp.@)
1025 */
1026 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
1027 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
1028 {
1029 request_t *request;
1030 connect_t *connect;
1031 HINTERNET hrequest = NULL;
1032
1033 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
1034 debugstr_w(version), debugstr_w(referrer), types, flags);
1035
1036 if(types && TRACE_ON(winhttp)) {
1037 const WCHAR **iter;
1038
1039 TRACE("accept types:\n");
1040 for(iter = types; *iter; iter++)
1041 TRACE(" %s\n", debugstr_w(*iter));
1042 }
1043
1044 if (!(connect = (connect_t *)grab_object( hconnect )))
1045 {
1046 set_last_error( ERROR_INVALID_HANDLE );
1047 return NULL;
1048 }
1049 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
1050 {
1051 release_object( &connect->hdr );
1052 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1053 return NULL;
1054 }
1055 if (!(request = heap_alloc_zero( sizeof(request_t) )))
1056 {
1057 release_object( &connect->hdr );
1058 return NULL;
1059 }
1060 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
1061 request->hdr.vtbl = &request_vtbl;
1062 request->hdr.refs = 1;
1063 request->hdr.flags = flags;
1064 request->hdr.callback = connect->hdr.callback;
1065 request->hdr.notify_mask = connect->hdr.notify_mask;
1066 request->hdr.context = connect->hdr.context;
1067 request->hdr.redirect_policy = connect->hdr.redirect_policy;
1068 list_init( &request->hdr.children );
1069 list_init( &request->task_queue );
1070
1071 addref_object( &connect->hdr );
1072 request->connect = connect;
1073 list_add_head( &connect->hdr.children, &request->hdr.entry );
1074
1075 if (!netconn_init( &request->netconn )) goto end;
1076 request->resolve_timeout = connect->session->resolve_timeout;
1077 request->connect_timeout = connect->session->connect_timeout;
1078 request->send_timeout = connect->session->send_timeout;
1079 request->recv_timeout = connect->session->recv_timeout;
1080
1081 if (!verb || !verb[0]) verb = getW;
1082 if (!(request->verb = strdupW( verb ))) goto end;
1083
1084 if (object)
1085 {
1086 WCHAR *path, *p;
1087 unsigned int len;
1088
1089 len = strlenW( object ) + 1;
1090 if (object[0] != '/') len++;
1091 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
1092
1093 if (object[0] != '/') *p++ = '/';
1094 strcpyW( p, object );
1095 request->path = path;
1096 }
1097 else if (!(request->path = strdupW( slashW ))) goto end;
1098
1099 if (!version || !version[0]) version = http1_1;
1100 if (!(request->version = strdupW( version ))) goto end;
1101 if (!(store_accept_types( request, types ))) goto end;
1102
1103 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
1104 request->hdr.handle = hrequest;
1105
1106 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
1107
1108 end:
1109 release_object( &request->hdr );
1110 release_object( &connect->hdr );
1111 TRACE("returning %p\n", hrequest);
1112 if (hrequest) set_last_error( ERROR_SUCCESS );
1113 return hrequest;
1114 }
1115
1116 /***********************************************************************
1117 * WinHttpCloseHandle (winhttp.@)
1118 */
1119 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1120 {
1121 object_header_t *hdr;
1122
1123 TRACE("%p\n", handle);
1124
1125 if (!(hdr = grab_object( handle )))
1126 {
1127 set_last_error( ERROR_INVALID_HANDLE );
1128 return FALSE;
1129 }
1130 release_object( hdr );
1131 free_handle( handle );
1132 set_last_error( ERROR_SUCCESS );
1133 return TRUE;
1134 }
1135
1136 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1137 {
1138 BOOL ret = FALSE;
1139
1140 if (!buflen)
1141 {
1142 set_last_error( ERROR_INVALID_PARAMETER );
1143 return FALSE;
1144 }
1145
1146 switch (option)
1147 {
1148 case WINHTTP_OPTION_CONTEXT_VALUE:
1149 {
1150 if (!buffer || *buflen < sizeof(DWORD_PTR))
1151 {
1152 *buflen = sizeof(DWORD_PTR);
1153 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1154 return FALSE;
1155 }
1156
1157 *(DWORD_PTR *)buffer = hdr->context;
1158 *buflen = sizeof(DWORD_PTR);
1159 return TRUE;
1160 }
1161 default:
1162 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1163 else
1164 {
1165 FIXME("unimplemented option %u\n", option);
1166 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1167 return FALSE;
1168 }
1169 break;
1170 }
1171 return ret;
1172 }
1173
1174 /***********************************************************************
1175 * WinHttpQueryOption (winhttp.@)
1176 */
1177 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1178 {
1179 BOOL ret = FALSE;
1180 object_header_t *hdr;
1181
1182 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1183
1184 if (!(hdr = grab_object( handle )))
1185 {
1186 set_last_error( ERROR_INVALID_HANDLE );
1187 return FALSE;
1188 }
1189
1190 ret = query_option( hdr, option, buffer, buflen );
1191
1192 release_object( hdr );
1193 if (ret) set_last_error( ERROR_SUCCESS );
1194 return ret;
1195 }
1196
1197 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1198 {
1199 BOOL ret = TRUE;
1200
1201 if (!buffer)
1202 {
1203 set_last_error( ERROR_INVALID_PARAMETER );
1204 return FALSE;
1205 }
1206
1207 switch (option)
1208 {
1209 case WINHTTP_OPTION_CONTEXT_VALUE:
1210 {
1211 if (buflen != sizeof(DWORD_PTR))
1212 {
1213 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1214 return FALSE;
1215 }
1216
1217 hdr->context = *(DWORD_PTR *)buffer;
1218 return TRUE;
1219 }
1220 default:
1221 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1222 else
1223 {
1224 FIXME("unimplemented option %u\n", option);
1225 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1226 return FALSE;
1227 }
1228 break;
1229 }
1230 return ret;
1231 }
1232
1233 /***********************************************************************
1234 * WinHttpSetOption (winhttp.@)
1235 */
1236 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1237 {
1238 BOOL ret = FALSE;
1239 object_header_t *hdr;
1240
1241 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1242
1243 if (!(hdr = grab_object( handle )))
1244 {
1245 set_last_error( ERROR_INVALID_HANDLE );
1246 return FALSE;
1247 }
1248
1249 ret = set_option( hdr, option, buffer, buflen );
1250
1251 release_object( hdr );
1252 if (ret) set_last_error( ERROR_SUCCESS );
1253 return ret;
1254 }
1255
1256 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1257 {
1258 char *ret;
1259 DWORD size = 0;
1260
1261 GetComputerNameExA( format, NULL, &size );
1262 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1263 if (!(ret = heap_alloc( size ))) return NULL;
1264 if (!GetComputerNameExA( format, ret, &size ))
1265 {
1266 heap_free( ret );
1267 return NULL;
1268 }
1269 return ret;
1270 }
1271
1272 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1273 {
1274 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1275
1276 if (len_suffix > len_domain) return FALSE;
1277 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1278 return FALSE;
1279 }
1280
1281 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1282 {
1283 int ret = -1;
1284 #ifdef HAVE_GETNAMEINFO
1285 ret = getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1286 #endif
1287 return ret;
1288 }
1289
1290 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1291 {
1292 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1293 static const WCHAR wpadW[] = {'/','w','p','a','d','.','d','a','t',0};
1294 char name[NI_MAXHOST];
1295 WCHAR *ret, *p;
1296 int len;
1297
1298 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1299 if (!ai) return NULL;
1300
1301 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1302
1303 len = strlenW( httpW ) + strlen( hostname ) + strlenW( wpadW );
1304 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1305 strcpyW( p, httpW );
1306 p += strlenW( httpW );
1307 while (*hostname) { *p++ = *hostname++; }
1308 strcpyW( p, wpadW );
1309 return ret;
1310 }
1311
1312 static BOOL get_system_proxy_autoconfig_url( char *buf, DWORD buflen )
1313 {
1314 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1315 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
1316 const void *ref;
1317 BOOL ret = FALSE;
1318
1319 if (!settings) return FALSE;
1320
1321 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
1322 {
1323 CFRelease( settings );
1324 return FALSE;
1325 }
1326 if (CFStringGetCString( ref, buf, buflen, kCFStringEncodingASCII ))
1327 {
1328 TRACE( "returning %s\n", debugstr_a(buf) );
1329 ret = TRUE;
1330 }
1331 CFRelease( settings );
1332 return ret;
1333 #else
1334 static int once;
1335 if (!once++) FIXME( "no support on this platform\n" );
1336 return FALSE;
1337 #endif
1338 }
1339
1340 #define INTERNET_MAX_URL_LENGTH 2084
1341
1342 /***********************************************************************
1343 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1344 */
1345 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1346 {
1347 BOOL ret = FALSE;
1348 char system_url[INTERNET_MAX_URL_LENGTH + 1];
1349
1350 TRACE("0x%08x, %p\n", flags, url);
1351
1352 if (!flags || !url)
1353 {
1354 set_last_error( ERROR_INVALID_PARAMETER );
1355 return FALSE;
1356 }
1357 if (get_system_proxy_autoconfig_url( system_url, sizeof(system_url) ))
1358 {
1359 WCHAR *urlW;
1360
1361 if (!(urlW = strdupAW( system_url ))) return FALSE;
1362 *url = urlW;
1363 set_last_error( ERROR_SUCCESS );
1364 return TRUE;
1365 }
1366 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1367 {
1368 static int fixme_shown;
1369 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1370 }
1371 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1372 {
1373 #ifdef HAVE_GETADDRINFO
1374 char *fqdn, *domain, *p;
1375
1376 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1377 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1378 {
1379 heap_free( fqdn );
1380 return FALSE;
1381 }
1382 p = fqdn;
1383 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1384 {
1385 struct addrinfo *ai;
1386 char *name;
1387 int res;
1388
1389 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1390 {
1391 heap_free( fqdn );
1392 heap_free( domain );
1393 return FALSE;
1394 }
1395 strcpy( name, "wpad" );
1396 strcat( name, p );
1397 res = getaddrinfo( name, NULL, NULL, &ai );
1398 if (!res)
1399 {
1400 *url = build_wpad_url( name, ai );
1401 freeaddrinfo( ai );
1402 if (*url)
1403 {
1404 TRACE("returning %s\n", debugstr_w(*url));
1405 heap_free( name );
1406 ret = TRUE;
1407 break;
1408 }
1409 }
1410 heap_free( name );
1411 p++;
1412 }
1413 heap_free( domain );
1414 heap_free( fqdn );
1415 #else
1416 FIXME("getaddrinfo not found at build time\n");
1417 #endif
1418 }
1419 if (!ret)
1420 {
1421 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1422 *url = NULL;
1423 }
1424 else set_last_error( ERROR_SUCCESS );
1425 return ret;
1426 }
1427
1428 static const WCHAR Connections[] = {
1429 'S','o','f','t','w','a','r','e','\\',
1430 'M','i','c','r','o','s','o','f','t','\\',
1431 'W','i','n','d','o','w','s','\\',
1432 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1433 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1434 'C','o','n','n','e','c','t','i','o','n','s',0 };
1435 static const WCHAR WinHttpSettings[] = {
1436 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1437 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1438 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1439 static const DWORD PROXY_TYPE_DIRECT = 1;
1440 static const DWORD PROXY_TYPE_PROXY = 2;
1441 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1442 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1443
1444 struct connection_settings_header
1445 {
1446 DWORD magic;
1447 DWORD unknown; /* always zero? */
1448 DWORD flags; /* one or more of PROXY_* */
1449 };
1450
1451 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1452 {
1453 const BYTE *begin;
1454
1455 for (begin = src; src - begin < len; src++, dst++)
1456 *dst = *src;
1457 *dst = 0;
1458 }
1459
1460 /***********************************************************************
1461 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1462 */
1463 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1464 {
1465 LONG l;
1466 HKEY key;
1467 BOOL got_from_reg = FALSE, direct = TRUE;
1468 char *envproxy;
1469
1470 TRACE("%p\n", info);
1471
1472 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1473 if (!l)
1474 {
1475 DWORD type, size = 0;
1476
1477 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1478 if (!l && type == REG_BINARY &&
1479 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1480 {
1481 BYTE *buf = heap_alloc( size );
1482
1483 if (buf)
1484 {
1485 struct connection_settings_header *hdr =
1486 (struct connection_settings_header *)buf;
1487 DWORD *len = (DWORD *)(hdr + 1);
1488
1489 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1490 &size );
1491 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1492 hdr->unknown == 0)
1493 {
1494 if (hdr->flags & PROXY_TYPE_PROXY)
1495 {
1496 BOOL sane = FALSE;
1497 LPWSTR proxy = NULL;
1498 LPWSTR proxy_bypass = NULL;
1499
1500 /* Sanity-check length of proxy string */
1501 if ((BYTE *)len - buf + *len <= size)
1502 {
1503 sane = TRUE;
1504 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1505 if (proxy)
1506 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1507 len = (DWORD *)((BYTE *)(len + 1) + *len);
1508 }
1509 if (sane)
1510 {
1511 /* Sanity-check length of proxy bypass string */
1512 if ((BYTE *)len - buf + *len <= size)
1513 {
1514 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1515 if (proxy_bypass)
1516 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1517 }
1518 else
1519 {
1520 sane = FALSE;
1521 GlobalFree( proxy );
1522 proxy = NULL;
1523 }
1524 }
1525 info->lpszProxy = proxy;
1526 info->lpszProxyBypass = proxy_bypass;
1527 if (sane)
1528 {
1529 got_from_reg = TRUE;
1530 direct = FALSE;
1531 info->dwAccessType =
1532 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1533 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1534 debugstr_w(info->lpszProxy),
1535 debugstr_w(info->lpszProxyBypass));
1536 }
1537 }
1538 }
1539 heap_free( buf );
1540 }
1541 }
1542 RegCloseKey( key );
1543 }
1544 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1545 {
1546 char *colon, *http_proxy;
1547
1548 if ((colon = strchr( envproxy, ':' )))
1549 {
1550 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1551 {
1552 static const char http[] = "http://";
1553
1554 /* It's a scheme, check that it's http */
1555 if (!strncmp( envproxy, http, strlen( http ) ))
1556 http_proxy = envproxy + strlen( http );
1557 else
1558 {
1559 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1560 http_proxy = NULL;
1561 }
1562 }
1563 else
1564 http_proxy = envproxy;
1565 }
1566 else
1567 http_proxy = envproxy;
1568 if (http_proxy)
1569 {
1570 WCHAR *http_proxyW;
1571 int len;
1572
1573 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1574 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1575 {
1576 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1577 direct = FALSE;
1578 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1579 info->lpszProxy = http_proxyW;
1580 info->lpszProxyBypass = NULL;
1581 TRACE("http proxy (from environment) = %s\n",
1582 debugstr_w(info->lpszProxy));
1583 }
1584 }
1585 }
1586 if (direct)
1587 {
1588 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1589 info->lpszProxy = NULL;
1590 info->lpszProxyBypass = NULL;
1591 }
1592 set_last_error( ERROR_SUCCESS );
1593 return TRUE;
1594 }
1595
1596 /***********************************************************************
1597 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1598 */
1599 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1600 {
1601 static const WCHAR settingsW[] =
1602 {'D','e','f','a','u','l','t','C','o','n','n','e','c','t','i','o','n','S','e','t','t','i','n','g','s',0};
1603 HKEY hkey = NULL;
1604 struct connection_settings_header *hdr = NULL;
1605 DWORD type, offset, len, size = 0;
1606 BOOL ret = FALSE;
1607
1608 TRACE("%p\n", config);
1609
1610 if (!config)
1611 {
1612 set_last_error( ERROR_INVALID_PARAMETER );
1613 return FALSE;
1614 }
1615 memset( config, 0, sizeof(*config) );
1616 config->fAutoDetect = TRUE;
1617
1618 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1619 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1620 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1621 {
1622 ret = TRUE;
1623 goto done;
1624 }
1625 if (!(hdr = heap_alloc( size ))) goto done;
1626 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1627 hdr->magic != WININET_SETTINGS_MAGIC)
1628 {
1629 ret = TRUE;
1630 goto done;
1631 }
1632
1633 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1634 offset = sizeof(*hdr);
1635 if (offset + sizeof(DWORD) > size) goto done;
1636 len = *(DWORD *)((char *)hdr + offset);
1637 offset += sizeof(DWORD);
1638 if (len && hdr->flags & PROXY_TYPE_PROXY)
1639 {
1640 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1641 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1642 }
1643 offset += len;
1644 if (offset + sizeof(DWORD) > size) goto done;
1645 len = *(DWORD *)((char *)hdr + offset);
1646 offset += sizeof(DWORD);
1647 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1648 {
1649 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1650 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1651 }
1652 offset += len;
1653 if (offset + sizeof(DWORD) > size) goto done;
1654 len = *(DWORD *)((char *)hdr + offset);
1655 offset += sizeof(DWORD);
1656 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1657 {
1658 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1659 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1660 }
1661 ret = TRUE;
1662
1663 done:
1664 RegCloseKey( hkey );
1665 heap_free( hdr );
1666 if (!ret)
1667 {
1668 GlobalFree( config->lpszAutoConfigUrl );
1669 config->lpszAutoConfigUrl = NULL;
1670 GlobalFree( config->lpszProxy );
1671 config->lpszProxy = NULL;
1672 GlobalFree( config->lpszProxyBypass );
1673 config->lpszProxyBypass = NULL;
1674 }
1675 else set_last_error( ERROR_SUCCESS );
1676 return ret;
1677 }
1678
1679 static BOOL parse_script_result( const char *result, WINHTTP_PROXY_INFO *info )
1680 {
1681 const char *p;
1682 WCHAR *q;
1683 int len;
1684
1685 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1686 info->lpszProxy = NULL;
1687 info->lpszProxyBypass = NULL;
1688
1689 TRACE("%s\n", debugstr_a( result ));
1690
1691 p = result;
1692 while (*p == ' ') p++;
1693 len = strlen( p );
1694 if (len >= 5 && !strncasecmp( p, "PROXY", sizeof("PROXY") - 1 ))
1695 {
1696 p += 5;
1697 while (*p == ' ') p++;
1698 if (!*p || *p == ';') return TRUE;
1699 if (!(info->lpszProxy = q = strdupAW( p ))) return FALSE;
1700 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1701 for (; *q; q++)
1702 {
1703 if (*q == ' ' || *q == ';')
1704 {
1705 *q = 0;
1706 break;
1707 }
1708 }
1709 }
1710 return TRUE;
1711 }
1712
1713 static char *download_script( const WCHAR *url, DWORD *out_size )
1714 {
1715 static const WCHAR typeW[] = {'*','/','*',0};
1716 static const WCHAR *acceptW[] = {typeW, NULL};
1717 HINTERNET ses, con = NULL, req = NULL;
1718 WCHAR *hostname;
1719 URL_COMPONENTSW uc;
1720 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1721 char *tmp, *buffer = NULL;
1722
1723 *out_size = 0;
1724
1725 memset( &uc, 0, sizeof(uc) );
1726 uc.dwStructSize = sizeof(uc);
1727 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1728 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1729 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1730 hostname[uc.dwHostNameLength] = 0;
1731
1732 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1733 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1734 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1735 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1736 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1737
1738 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1739 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1740 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1741
1742 size = 4096;
1743 if (!(buffer = heap_alloc( size ))) goto done;
1744 to_read = size;
1745 offset = 0;
1746 for (;;)
1747 {
1748 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1749 if (!bytes_read) break;
1750 to_read -= bytes_read;
1751 offset += bytes_read;
1752 *out_size += bytes_read;
1753 if (!to_read)
1754 {
1755 to_read = size;
1756 size *= 2;
1757 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1758 buffer = tmp;
1759 }
1760 }
1761
1762 done:
1763 WinHttpCloseHandle( req );
1764 WinHttpCloseHandle( con );
1765 WinHttpCloseHandle( ses );
1766 heap_free( hostname );
1767 if (!buffer) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1768 return buffer;
1769 }
1770
1771 struct AUTO_PROXY_SCRIPT_BUFFER
1772 {
1773 DWORD dwStructSize;
1774 LPSTR lpszScriptBuffer;
1775 DWORD dwScriptBufferSize;
1776 };
1777
1778 BOOL WINAPI InternetDeInitializeAutoProxyDll(LPSTR, DWORD);
1779 BOOL WINAPI InternetGetProxyInfo(LPCSTR, DWORD, LPSTR, DWORD, LPSTR *, LPDWORD);
1780 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD, LPSTR, LPSTR, void *, struct AUTO_PROXY_SCRIPT_BUFFER *);
1781
1782 static BOOL run_script( char *script, DWORD size, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1783 {
1784 BOOL ret;
1785 char *result, *urlA;
1786 DWORD len_result;
1787 struct AUTO_PROXY_SCRIPT_BUFFER buffer;
1788
1789 buffer.dwStructSize = sizeof(buffer);
1790 buffer.lpszScriptBuffer = script;
1791 buffer.dwScriptBufferSize = size;
1792
1793 if (!(urlA = strdupWA( url ))) return FALSE;
1794 if (!(ret = InternetInitializeAutoProxyDll( 0, NULL, NULL, NULL, &buffer )))
1795 {
1796 heap_free( urlA );
1797 return FALSE;
1798 }
1799 if ((ret = InternetGetProxyInfo( urlA, strlen(urlA), NULL, 0, &result, &len_result )))
1800 {
1801 ret = parse_script_result( result, info );
1802 heap_free( result );
1803 }
1804 heap_free( urlA );
1805 return InternetDeInitializeAutoProxyDll( NULL, 0 );
1806 }
1807
1808 /***********************************************************************
1809 * WinHttpGetProxyForUrl (winhttp.@)
1810 */
1811 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1812 WINHTTP_PROXY_INFO *info )
1813 {
1814 WCHAR *detected_pac_url = NULL;
1815 const WCHAR *pac_url;
1816 session_t *session;
1817 char *script;
1818 DWORD size;
1819 BOOL ret = FALSE;
1820
1821 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1822
1823 if (!(session = (session_t *)grab_object( hsession )))
1824 {
1825 set_last_error( ERROR_INVALID_HANDLE );
1826 return FALSE;
1827 }
1828 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1829 {
1830 release_object( &session->hdr );
1831 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1832 return FALSE;
1833 }
1834 if (!url || !options || !info ||
1835 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1836 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1837 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1838 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1839 {
1840 release_object( &session->hdr );
1841 set_last_error( ERROR_INVALID_PARAMETER );
1842 return FALSE;
1843 }
1844 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1845 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1846 goto done;
1847
1848 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1849 else pac_url = detected_pac_url;
1850
1851 if ((script = download_script( pac_url, &size )))
1852 {
1853 ret = run_script( script, size, url, info );
1854 heap_free( script );
1855 }
1856
1857 done:
1858 GlobalFree( detected_pac_url );
1859 release_object( &session->hdr );
1860 if (ret) set_last_error( ERROR_SUCCESS );
1861 return ret;
1862 }
1863
1864 /***********************************************************************
1865 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1866 */
1867 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1868 {
1869 LONG l;
1870 HKEY key;
1871 BOOL ret = FALSE;
1872 const WCHAR *src;
1873
1874 TRACE("%p\n", info);
1875
1876 if (!info)
1877 {
1878 set_last_error( ERROR_INVALID_PARAMETER );
1879 return FALSE;
1880 }
1881 switch (info->dwAccessType)
1882 {
1883 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1884 break;
1885 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1886 if (!info->lpszProxy)
1887 {
1888 set_last_error( ERROR_INVALID_PARAMETER );
1889 return FALSE;
1890 }
1891 /* Only ASCII characters are allowed */
1892 for (src = info->lpszProxy; *src; src++)
1893 if (*src > 0x7f)
1894 {
1895 set_last_error( ERROR_INVALID_PARAMETER );
1896 return FALSE;
1897 }
1898 if (info->lpszProxyBypass)
1899 {
1900 for (src = info->lpszProxyBypass; *src; src++)
1901 if (*src > 0x7f)
1902 {
1903 set_last_error( ERROR_INVALID_PARAMETER );
1904 return FALSE;
1905 }
1906 }
1907 break;
1908 default:
1909 set_last_error( ERROR_INVALID_PARAMETER );
1910 return FALSE;
1911 }
1912
1913 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1914 KEY_WRITE, NULL, &key, NULL );
1915 if (!l)
1916 {
1917 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1918 BYTE *buf;
1919
1920 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1921 {
1922 size += strlenW( info->lpszProxy );
1923 if (info->lpszProxyBypass)
1924 size += strlenW( info->lpszProxyBypass );
1925 }
1926 buf = heap_alloc( size );
1927 if (buf)
1928 {
1929 struct connection_settings_header *hdr =
1930 (struct connection_settings_header *)buf;
1931 DWORD *len = (DWORD *)(hdr + 1);
1932
1933 hdr->magic = WINHTTP_SETTINGS_MAGIC;
1934 hdr->unknown = 0;
1935 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1936 {
1937 BYTE *dst;
1938
1939 hdr->flags = PROXY_TYPE_PROXY;
1940 *len++ = strlenW( info->lpszProxy );
1941 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1942 src++, dst++)
1943 *dst = *src;
1944 len = (DWORD *)dst;
1945 if (info->lpszProxyBypass)
1946 {
1947 *len++ = strlenW( info->lpszProxyBypass );
1948 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1949 src++, dst++)
1950 *dst = *src;
1951 }
1952 else
1953 *len++ = 0;
1954 }
1955 else
1956 {
1957 hdr->flags = PROXY_TYPE_DIRECT;
1958 *len++ = 0;
1959 *len++ = 0;
1960 }
1961 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1962 if (!l)
1963 ret = TRUE;
1964 heap_free( buf );
1965 }
1966 RegCloseKey( key );
1967 }
1968 if (ret) set_last_error( ERROR_SUCCESS );
1969 return ret;
1970 }
1971
1972 /***********************************************************************
1973 * WinHttpSetStatusCallback (winhttp.@)
1974 */
1975 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1976 DWORD flags, DWORD_PTR reserved )
1977 {
1978 object_header_t *hdr;
1979 WINHTTP_STATUS_CALLBACK ret;
1980
1981 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1982
1983 if (!(hdr = grab_object( handle )))
1984 {
1985 set_last_error( ERROR_INVALID_HANDLE );
1986 return WINHTTP_INVALID_STATUS_CALLBACK;
1987 }
1988 ret = hdr->callback;
1989 hdr->callback = callback;
1990 hdr->notify_mask = flags;
1991
1992 release_object( hdr );
1993 set_last_error( ERROR_SUCCESS );
1994 return ret;
1995 }
1996
1997 /***********************************************************************
1998 * WinHttpSetTimeouts (winhttp.@)
1999 */
2000 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2001 {
2002 BOOL ret = TRUE;
2003 object_header_t *hdr;
2004 request_t *request;
2005 session_t *session;
2006
2007 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2008
2009 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2010 {
2011 set_last_error( ERROR_INVALID_PARAMETER );
2012 return FALSE;
2013 }
2014
2015 if (!(hdr = grab_object( handle )))
2016 {
2017 set_last_error( ERROR_INVALID_HANDLE );
2018 return FALSE;
2019 }
2020
2021 switch(hdr->type)
2022 {
2023 case WINHTTP_HANDLE_TYPE_REQUEST:
2024 request = (request_t *)hdr;
2025 request->connect_timeout = connect;
2026
2027 if (resolve < 0) resolve = 0;
2028 request->resolve_timeout = resolve;
2029
2030 if (send < 0) send = 0;
2031 request->send_timeout = send;
2032
2033 if (receive < 0) receive = 0;
2034 request->recv_timeout = receive;
2035
2036 if (netconn_connected( &request->netconn ))
2037 {
2038 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2039 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2040 }
2041 break;
2042
2043 case WINHTTP_HANDLE_TYPE_SESSION:
2044 session = (session_t *)hdr;
2045 session->connect_timeout = connect;
2046
2047 if (resolve < 0) resolve = 0;
2048 session->resolve_timeout = resolve;
2049
2050 if (send < 0) send = 0;
2051 session->send_timeout = send;
2052
2053 if (receive < 0) receive = 0;
2054 session->recv_timeout = receive;
2055 break;
2056
2057 default:
2058 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2059 ret = FALSE;
2060 }
2061 release_object( hdr );
2062 if (ret) set_last_error( ERROR_SUCCESS );
2063 return ret;
2064 }
2065
2066 static const WCHAR wkday[7][4] =
2067 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2068 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2069 static const WCHAR month[12][4] =
2070 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2071 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2072 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2073
2074 /***********************************************************************
2075 * WinHttpTimeFromSystemTime (WININET.@)
2076 */
2077 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2078 {
2079 static const WCHAR format[] =
2080 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2081 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2082
2083 TRACE("%p, %p\n", time, string);
2084
2085 if (!time || !string)
2086 {
2087 set_last_error( ERROR_INVALID_PARAMETER );
2088 return FALSE;
2089 }
2090
2091 sprintfW( string, format,
2092 wkday[time->wDayOfWeek],
2093 time->wDay,
2094 month[time->wMonth - 1],
2095 time->wYear,
2096 time->wHour,
2097 time->wMinute,
2098 time->wSecond );
2099
2100 set_last_error( ERROR_SUCCESS );
2101 return TRUE;
2102 }
2103
2104 /***********************************************************************
2105 * WinHttpTimeToSystemTime (WININET.@)
2106 */
2107 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2108 {
2109 unsigned int i;
2110 const WCHAR *s = string;
2111 WCHAR *end;
2112
2113 TRACE("%s, %p\n", debugstr_w(string), time);
2114
2115 if (!string || !time)
2116 {
2117 set_last_error( ERROR_INVALID_PARAMETER );
2118 return FALSE;
2119 }
2120
2121 /* Windows does this too */
2122 GetSystemTime( time );
2123
2124 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2125 * a SYSTEMTIME structure.
2126 */
2127
2128 set_last_error( ERROR_SUCCESS );
2129
2130 while (*s && !isalphaW( *s )) s++;
2131 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2132 time->wDayOfWeek = 7;
2133
2134 for (i = 0; i < 7; i++)
2135 {
2136 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2137 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2138 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2139 {
2140 time->wDayOfWeek = i;
2141 break;
2142 }
2143 }
2144
2145 if (time->wDayOfWeek > 6) return TRUE;
2146 while (*s && !isdigitW( *s )) s++;
2147 time->wDay = strtolW( s, &end, 10 );
2148 s = end;
2149
2150 while (*s && !isalphaW( *s )) s++;
2151 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2152 time->wMonth = 0;
2153
2154 for (i = 0; i < 12; i++)
2155 {
2156 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2157 toupperW( month[i][1]) == toupperW( s[1] ) &&
2158 toupperW( month[i][2]) == toupperW( s[2] ) )
2159 {
2160 time->wMonth = i + 1;
2161 break;
2162 }
2163 }
2164 if (time->wMonth == 0) return TRUE;
2165
2166 while (*s && !isdigitW( *s )) s++;
2167 if (*s == '\0') return TRUE;
2168 time->wYear = strtolW( s, &end, 10 );
2169 s = end;
2170
2171 while (*s && !isdigitW( *s )) s++;
2172 if (*s == '\0') return TRUE;
2173 time->wHour = strtolW( s, &end, 10 );
2174 s = end;
2175
2176 while (*s && !isdigitW( *s )) s++;
2177 if (*s == '\0') return TRUE;
2178 time->wMinute = strtolW( s, &end, 10 );
2179 s = end;
2180
2181 while (*s && !isdigitW( *s )) s++;
2182 if (*s == '\0') return TRUE;
2183 time->wSecond = strtolW( s, &end, 10 );
2184
2185 time->wMilliseconds = 0;
2186 return TRUE;
2187 }