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