user32: Don't flush window surfaces while waiting for a sent message reply.
[wine/multimedia.git] / dlls / winhttp / session.c
blob8ffcda137137a99aa622f804452660b7380d7866
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winhttp.h"
29 #include "wincrypt.h"
30 #include "winreg.h"
31 #define COBJMACROS
32 #include "ole2.h"
33 #include "dispex.h"
34 #include "activscp.h"
36 #include "winhttp_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
40 #define DEFAULT_RESOLVE_TIMEOUT 0
41 #define DEFAULT_CONNECT_TIMEOUT 20000
42 #define DEFAULT_SEND_TIMEOUT 30000
43 #define DEFAULT_RECEIVE_TIMEOUT 30000
45 static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
46 static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
48 void set_last_error( DWORD error )
50 /* FIXME */
51 SetLastError( error );
54 DWORD get_last_error( void )
56 /* FIXME */
57 return GetLastError();
60 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
62 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
64 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
67 /***********************************************************************
68 * WinHttpCheckPlatform (winhttp.@)
70 BOOL WINAPI WinHttpCheckPlatform( void )
72 TRACE("\n");
73 return TRUE;
76 /***********************************************************************
77 * session_destroy (internal)
79 static void session_destroy( object_header_t *hdr )
81 session_t *session = (session_t *)hdr;
82 struct list *item, *next;
83 domain_t *domain;
85 TRACE("%p\n", session);
87 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
89 domain = LIST_ENTRY( item, domain_t, entry );
90 delete_domain( domain );
92 heap_free( session->agent );
93 heap_free( session->proxy_server );
94 heap_free( session->proxy_bypass );
95 heap_free( session->proxy_username );
96 heap_free( session->proxy_password );
97 heap_free( session );
100 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
102 session_t *session = (session_t *)hdr;
104 switch (option)
106 case WINHTTP_OPTION_REDIRECT_POLICY:
108 if (!buffer || *buflen < sizeof(DWORD))
110 *buflen = sizeof(DWORD);
111 set_last_error( ERROR_INSUFFICIENT_BUFFER );
112 return FALSE;
115 *(DWORD *)buffer = hdr->redirect_policy;
116 *buflen = sizeof(DWORD);
117 return TRUE;
119 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
120 *(DWORD *)buffer = session->resolve_timeout;
121 *buflen = sizeof(DWORD);
122 return TRUE;
123 case WINHTTP_OPTION_CONNECT_TIMEOUT:
124 *(DWORD *)buffer = session->connect_timeout;
125 *buflen = sizeof(DWORD);
126 return TRUE;
127 case WINHTTP_OPTION_SEND_TIMEOUT:
128 *(DWORD *)buffer = session->send_timeout;
129 *buflen = sizeof(DWORD);
130 return TRUE;
131 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
132 *(DWORD *)buffer = session->recv_timeout;
133 *buflen = sizeof(DWORD);
134 return TRUE;
135 default:
136 FIXME("unimplemented option %u\n", option);
137 set_last_error( ERROR_INVALID_PARAMETER );
138 return FALSE;
142 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
144 session_t *session = (session_t *)hdr;
146 switch (option)
148 case WINHTTP_OPTION_PROXY:
150 WINHTTP_PROXY_INFO *pi = buffer;
152 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
153 return TRUE;
155 case WINHTTP_OPTION_REDIRECT_POLICY:
157 DWORD policy;
159 if (buflen != sizeof(policy))
161 set_last_error( ERROR_INSUFFICIENT_BUFFER );
162 return FALSE;
165 policy = *(DWORD *)buffer;
166 TRACE("0x%x\n", policy);
167 hdr->redirect_policy = policy;
168 return TRUE;
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 default:
186 FIXME("unimplemented option %u\n", option);
187 set_last_error( ERROR_INVALID_PARAMETER );
188 return FALSE;
192 static const object_vtbl_t session_vtbl =
194 session_destroy,
195 session_query_option,
196 session_set_option
199 /***********************************************************************
200 * WinHttpOpen (winhttp.@)
202 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
204 session_t *session;
205 HINTERNET handle = NULL;
207 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
209 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
211 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
212 session->hdr.vtbl = &session_vtbl;
213 session->hdr.flags = flags;
214 session->hdr.refs = 1;
215 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
216 list_init( &session->hdr.children );
217 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
218 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
219 session->send_timeout = DEFAULT_SEND_TIMEOUT;
220 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
221 list_init( &session->cookie_cache );
223 if (agent && !(session->agent = strdupW( agent ))) goto end;
224 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
226 WINHTTP_PROXY_INFO info;
228 WinHttpGetDefaultProxyConfiguration( &info );
229 session->access = info.dwAccessType;
230 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
232 GlobalFree( info.lpszProxy );
233 GlobalFree( info.lpszProxyBypass );
234 goto end;
236 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
238 GlobalFree( info.lpszProxy );
239 GlobalFree( info.lpszProxyBypass );
240 goto end;
243 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
245 session->access = access;
246 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
247 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
250 if (!(handle = alloc_handle( &session->hdr ))) goto end;
251 session->hdr.handle = handle;
253 end:
254 release_object( &session->hdr );
255 TRACE("returning %p\n", handle);
256 return handle;
259 /***********************************************************************
260 * connect_destroy (internal)
262 static void connect_destroy( object_header_t *hdr )
264 connect_t *connect = (connect_t *)hdr;
266 TRACE("%p\n", connect);
268 release_object( &connect->session->hdr );
270 heap_free( connect->hostname );
271 heap_free( connect->servername );
272 heap_free( connect->username );
273 heap_free( connect->password );
274 heap_free( connect );
277 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
279 connect_t *connect = (connect_t *)hdr;
281 switch (option)
283 case WINHTTP_OPTION_PARENT_HANDLE:
285 if (!buffer || *buflen < sizeof(HINTERNET))
287 *buflen = sizeof(HINTERNET);
288 set_last_error( ERROR_INSUFFICIENT_BUFFER );
289 return FALSE;
292 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
293 *buflen = sizeof(HINTERNET);
294 return TRUE;
296 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
297 *(DWORD *)buffer = connect->session->resolve_timeout;
298 *buflen = sizeof(DWORD);
299 return TRUE;
300 case WINHTTP_OPTION_CONNECT_TIMEOUT:
301 *(DWORD *)buffer = connect->session->connect_timeout;
302 *buflen = sizeof(DWORD);
303 return TRUE;
304 case WINHTTP_OPTION_SEND_TIMEOUT:
305 *(DWORD *)buffer = connect->session->send_timeout;
306 *buflen = sizeof(DWORD);
307 return TRUE;
308 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
309 *(DWORD *)buffer = connect->session->recv_timeout;
310 *buflen = sizeof(DWORD);
311 return TRUE;
312 default:
313 FIXME("unimplemented option %u\n", option);
314 set_last_error( ERROR_INVALID_PARAMETER );
315 return FALSE;
319 static const object_vtbl_t connect_vtbl =
321 connect_destroy,
322 connect_query_option,
323 NULL
326 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
328 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
329 BOOL ret = FALSE;
331 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
332 ret = TRUE;
333 else if (*domain == '*')
335 if (domain[1] == '.')
337 LPCWSTR dot;
339 /* For a hostname to match a wildcard, the last domain must match
340 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
341 * hostname is www.foo.a.b, it matches, but a.b does not.
343 dot = strchrW( server, '.' );
344 if (dot)
346 int len = strlenW( dot + 1 );
348 if (len > strlenW( domain + 2 ))
350 LPCWSTR ptr;
352 /* The server's domain is longer than the wildcard, so it
353 * could be a subdomain. Compare the last portion of the
354 * server's domain.
356 ptr = dot + len + 1 - strlenW( domain + 2 );
357 if (!strcmpiW( ptr, domain + 2 ))
359 /* This is only a match if the preceding character is
360 * a '.', i.e. that it is a matching domain. E.g.
361 * if domain is '*.b.c' and server is 'www.ab.c' they
362 * do not match.
364 ret = *(ptr - 1) == '.';
367 else
368 ret = !strcmpiW( dot + 1, domain + 2 );
372 else
373 ret = !strcmpiW( server, domain );
374 return ret;
377 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
378 #define MAX_HOST_NAME_LENGTH 256
380 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
382 LPCWSTR ptr;
383 BOOL ret = FALSE;
385 if (!session->proxy_bypass) return FALSE;
386 ptr = session->proxy_bypass;
387 do {
388 LPCWSTR tmp = ptr;
390 ptr = strchrW( ptr, ';' );
391 if (!ptr)
392 ptr = strchrW( tmp, ' ' );
393 if (ptr)
395 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
397 WCHAR domain[MAX_HOST_NAME_LENGTH];
399 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
400 domain[ptr - tmp] = 0;
401 ret = domain_matches( server, domain );
403 ptr += 1;
405 else if (*tmp)
406 ret = domain_matches( server, tmp );
407 } while (ptr && !ret);
408 return ret;
411 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
413 session_t *session = connect->session;
414 BOOL ret = TRUE;
416 if (session->proxy_server && !should_bypass_proxy(session, server))
418 LPCWSTR colon;
420 if ((colon = strchrW( session->proxy_server, ':' )))
422 if (!connect->servername || strncmpiW( connect->servername,
423 session->proxy_server, colon - session->proxy_server - 1 ))
425 heap_free( connect->servername );
426 connect->resolved = FALSE;
427 if (!(connect->servername = heap_alloc(
428 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
430 ret = FALSE;
431 goto end;
433 memcpy( connect->servername, session->proxy_server,
434 (colon - session->proxy_server) * sizeof(WCHAR) );
435 connect->servername[colon - session->proxy_server] = 0;
436 if (*(colon + 1))
437 connect->serverport = atoiW( colon + 1 );
438 else
439 connect->serverport = INTERNET_DEFAULT_PORT;
442 else
444 if (!connect->servername || strcmpiW( connect->servername,
445 session->proxy_server ))
447 heap_free( connect->servername );
448 connect->resolved = FALSE;
449 if (!(connect->servername = strdupW( session->proxy_server )))
451 ret = FALSE;
452 goto end;
454 connect->serverport = INTERNET_DEFAULT_PORT;
458 else if (server)
460 heap_free( connect->servername );
461 connect->resolved = FALSE;
462 if (!(connect->servername = strdupW( server )))
464 ret = FALSE;
465 goto end;
467 connect->serverport = port;
469 end:
470 return ret;
473 /***********************************************************************
474 * WinHttpConnect (winhttp.@)
476 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
478 connect_t *connect;
479 session_t *session;
480 HINTERNET hconnect = NULL;
482 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
484 if (!server)
486 set_last_error( ERROR_INVALID_PARAMETER );
487 return NULL;
489 if (!(session = (session_t *)grab_object( hsession )))
491 set_last_error( ERROR_INVALID_HANDLE );
492 return NULL;
494 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
496 release_object( &session->hdr );
497 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
498 return NULL;
500 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
502 release_object( &session->hdr );
503 return NULL;
505 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
506 connect->hdr.vtbl = &connect_vtbl;
507 connect->hdr.refs = 1;
508 connect->hdr.flags = session->hdr.flags;
509 connect->hdr.callback = session->hdr.callback;
510 connect->hdr.notify_mask = session->hdr.notify_mask;
511 connect->hdr.context = session->hdr.context;
512 connect->hdr.redirect_policy = session->hdr.redirect_policy;
513 list_init( &connect->hdr.children );
515 addref_object( &session->hdr );
516 connect->session = session;
517 list_add_head( &session->hdr.children, &connect->hdr.entry );
519 if (!(connect->hostname = strdupW( server ))) goto end;
520 connect->hostport = port;
521 if (!set_server_for_hostname( connect, server, port )) goto end;
523 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
524 connect->hdr.handle = hconnect;
526 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
528 end:
529 release_object( &connect->hdr );
530 release_object( &session->hdr );
531 TRACE("returning %p\n", hconnect);
532 return hconnect;
535 /***********************************************************************
536 * request_destroy (internal)
538 static void request_destroy( object_header_t *hdr )
540 request_t *request = (request_t *)hdr;
541 unsigned int i;
543 TRACE("%p\n", request);
545 release_object( &request->connect->hdr );
547 heap_free( request->verb );
548 heap_free( request->path );
549 heap_free( request->version );
550 heap_free( request->raw_headers );
551 heap_free( request->status_text );
552 for (i = 0; i < request->num_headers; i++)
554 heap_free( request->headers[i].field );
555 heap_free( request->headers[i].value );
557 heap_free( request->headers );
558 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
559 heap_free( request->accept_types );
560 heap_free( request );
563 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
565 int len = 0;
566 if (str) len = strlenW( str );
567 if (buffer && *buflen > len)
569 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
570 buffer[len] = 0;
572 *buflen = len * sizeof(WCHAR);
575 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
577 WCHAR *ret;
578 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
580 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
581 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
582 CertNameToStrW( encoding, blob, format, ret, size );
584 return ret;
587 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
589 request_t *request = (request_t *)hdr;
591 switch (option)
593 case WINHTTP_OPTION_SECURITY_FLAGS:
595 DWORD flags;
596 int bits;
598 if (!buffer || *buflen < sizeof(flags))
600 *buflen = sizeof(flags);
601 set_last_error( ERROR_INSUFFICIENT_BUFFER );
602 return FALSE;
605 flags = 0;
606 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
607 flags |= request->netconn.security_flags;
608 bits = netconn_get_cipher_strength( &request->netconn );
609 if (bits >= 128)
610 flags |= SECURITY_FLAG_STRENGTH_STRONG;
611 else if (bits >= 56)
612 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
613 else
614 flags |= SECURITY_FLAG_STRENGTH_WEAK;
615 *(DWORD *)buffer = flags;
616 *buflen = sizeof(flags);
617 return TRUE;
619 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
621 const CERT_CONTEXT *cert;
623 if (!buffer || *buflen < sizeof(cert))
625 *buflen = sizeof(cert);
626 set_last_error( ERROR_INSUFFICIENT_BUFFER );
627 return FALSE;
630 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
631 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
632 *buflen = sizeof(cert);
633 return TRUE;
635 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
637 const CERT_CONTEXT *cert;
638 const CRYPT_OID_INFO *oidInfo;
639 WINHTTP_CERTIFICATE_INFO *ci = buffer;
641 FIXME("partial stub\n");
643 if (!buffer || *buflen < sizeof(*ci))
645 *buflen = sizeof(*ci);
646 set_last_error( ERROR_INSUFFICIENT_BUFFER );
647 return FALSE;
649 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
651 ci->ftExpiry = cert->pCertInfo->NotAfter;
652 ci->ftStart = cert->pCertInfo->NotBefore;
653 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
654 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
655 ci->lpszProtocolName = NULL;
656 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
657 cert->pCertInfo->SignatureAlgorithm.pszObjId,
658 0 );
659 if (oidInfo)
660 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
661 else
662 ci->lpszSignatureAlgName = NULL;
663 ci->lpszEncryptionAlgName = NULL;
664 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
666 CertFreeCertificateContext( cert );
667 *buflen = sizeof(*ci);
668 return TRUE;
670 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
672 if (!buffer || *buflen < sizeof(DWORD))
674 *buflen = sizeof(DWORD);
675 set_last_error( ERROR_INSUFFICIENT_BUFFER );
676 return FALSE;
679 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
680 *buflen = sizeof(DWORD);
681 return TRUE;
683 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
684 *(DWORD *)buffer = request->resolve_timeout;
685 *buflen = sizeof(DWORD);
686 return TRUE;
687 case WINHTTP_OPTION_CONNECT_TIMEOUT:
688 *(DWORD *)buffer = request->connect_timeout;
689 *buflen = sizeof(DWORD);
690 return TRUE;
691 case WINHTTP_OPTION_SEND_TIMEOUT:
692 *(DWORD *)buffer = request->send_timeout;
693 *buflen = sizeof(DWORD);
694 return TRUE;
695 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
696 *(DWORD *)buffer = request->recv_timeout;
697 *buflen = sizeof(DWORD);
698 return TRUE;
700 case WINHTTP_OPTION_USERNAME:
701 str_to_buffer( buffer, request->connect->username, buflen );
702 return TRUE;
704 case WINHTTP_OPTION_PASSWORD:
705 str_to_buffer( buffer, request->connect->password, buflen );
706 return TRUE;
708 case WINHTTP_OPTION_PROXY_USERNAME:
709 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
710 return TRUE;
712 case WINHTTP_OPTION_PROXY_PASSWORD:
713 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
714 return TRUE;
716 default:
717 FIXME("unimplemented option %u\n", option);
718 set_last_error( ERROR_INVALID_PARAMETER );
719 return FALSE;
723 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
725 WCHAR *ret;
726 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
728 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
729 ret[buflen] = 0;
730 return ret;
732 set_last_error( ERROR_OUTOFMEMORY );
733 return NULL;
736 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
738 request_t *request = (request_t *)hdr;
740 switch (option)
742 case WINHTTP_OPTION_PROXY:
744 WINHTTP_PROXY_INFO *pi = buffer;
746 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
747 return TRUE;
749 case WINHTTP_OPTION_DISABLE_FEATURE:
751 DWORD disable;
753 if (buflen != sizeof(DWORD))
755 set_last_error( ERROR_INSUFFICIENT_BUFFER );
756 return FALSE;
759 disable = *(DWORD *)buffer;
760 TRACE("0x%x\n", disable);
761 hdr->disable_flags |= disable;
762 return TRUE;
764 case WINHTTP_OPTION_AUTOLOGON_POLICY:
766 DWORD policy;
768 if (buflen != sizeof(DWORD))
770 set_last_error( ERROR_INSUFFICIENT_BUFFER );
771 return FALSE;
774 policy = *(DWORD *)buffer;
775 TRACE("0x%x\n", policy);
776 hdr->logon_policy = policy;
777 return TRUE;
779 case WINHTTP_OPTION_REDIRECT_POLICY:
781 DWORD policy;
783 if (buflen != sizeof(DWORD))
785 set_last_error( ERROR_INSUFFICIENT_BUFFER );
786 return FALSE;
789 policy = *(DWORD *)buffer;
790 TRACE("0x%x\n", policy);
791 hdr->redirect_policy = policy;
792 return TRUE;
794 case WINHTTP_OPTION_SECURITY_FLAGS:
796 DWORD flags;
798 if (buflen < sizeof(DWORD))
800 set_last_error( ERROR_INSUFFICIENT_BUFFER );
801 return FALSE;
803 flags = *(DWORD *)buffer;
804 TRACE("0x%x\n", flags);
805 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
806 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
807 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
808 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
810 set_last_error( ERROR_INVALID_PARAMETER );
811 return FALSE;
813 request->netconn.security_flags = flags;
814 return TRUE;
816 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
817 request->resolve_timeout = *(DWORD *)buffer;
818 return TRUE;
819 case WINHTTP_OPTION_CONNECT_TIMEOUT:
820 request->connect_timeout = *(DWORD *)buffer;
821 return TRUE;
822 case WINHTTP_OPTION_SEND_TIMEOUT:
823 request->send_timeout = *(DWORD *)buffer;
824 return TRUE;
825 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
826 request->recv_timeout = *(DWORD *)buffer;
827 return TRUE;
829 case WINHTTP_OPTION_USERNAME:
831 connect_t *connect = request->connect;
833 heap_free( connect->username );
834 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
835 return TRUE;
837 case WINHTTP_OPTION_PASSWORD:
839 connect_t *connect = request->connect;
841 heap_free( connect->password );
842 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
843 return TRUE;
845 case WINHTTP_OPTION_PROXY_USERNAME:
847 session_t *session = request->connect->session;
849 heap_free( session->proxy_username );
850 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
851 return TRUE;
853 case WINHTTP_OPTION_PROXY_PASSWORD:
855 session_t *session = request->connect->session;
857 heap_free( session->proxy_password );
858 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
859 return TRUE;
861 default:
862 FIXME("unimplemented option %u\n", option);
863 set_last_error( ERROR_INVALID_PARAMETER );
864 return TRUE;
868 static const object_vtbl_t request_vtbl =
870 request_destroy,
871 request_query_option,
872 request_set_option
875 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
877 const WCHAR **types = accept_types;
878 int i;
880 if (!types) return TRUE;
881 while (*types)
883 request->num_accept_types++;
884 types++;
886 if (!request->num_accept_types) return TRUE;
887 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
889 request->num_accept_types = 0;
890 return FALSE;
892 types = accept_types;
893 for (i = 0; i < request->num_accept_types; i++)
895 if (!(request->accept_types[i] = strdupW( *types )))
897 for (; i >= 0; i--) heap_free( request->accept_types[i] );
898 heap_free( request->accept_types );
899 request->accept_types = NULL;
900 request->num_accept_types = 0;
901 return FALSE;
903 types++;
905 return TRUE;
908 /***********************************************************************
909 * WinHttpOpenRequest (winhttp.@)
911 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
912 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
914 request_t *request;
915 connect_t *connect;
916 HINTERNET hrequest = NULL;
918 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
919 debugstr_w(version), debugstr_w(referrer), types, flags);
921 if(types && TRACE_ON(winhttp)) {
922 const WCHAR **iter;
924 TRACE("accept types:\n");
925 for(iter = types; *iter; iter++)
926 TRACE(" %s\n", debugstr_w(*iter));
929 if (!(connect = (connect_t *)grab_object( hconnect )))
931 set_last_error( ERROR_INVALID_HANDLE );
932 return NULL;
934 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
936 release_object( &connect->hdr );
937 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
938 return NULL;
940 if (!(request = heap_alloc_zero( sizeof(request_t) )))
942 release_object( &connect->hdr );
943 return NULL;
945 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
946 request->hdr.vtbl = &request_vtbl;
947 request->hdr.refs = 1;
948 request->hdr.flags = flags;
949 request->hdr.callback = connect->hdr.callback;
950 request->hdr.notify_mask = connect->hdr.notify_mask;
951 request->hdr.context = connect->hdr.context;
952 request->hdr.redirect_policy = connect->hdr.redirect_policy;
953 list_init( &request->hdr.children );
955 addref_object( &connect->hdr );
956 request->connect = connect;
957 list_add_head( &connect->hdr.children, &request->hdr.entry );
959 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
960 request->resolve_timeout = connect->session->resolve_timeout;
961 request->connect_timeout = connect->session->connect_timeout;
962 request->send_timeout = connect->session->send_timeout;
963 request->recv_timeout = connect->session->recv_timeout;
965 if (!verb || !verb[0]) verb = getW;
966 if (!(request->verb = strdupW( verb ))) goto end;
968 if (object)
970 WCHAR *path, *p;
971 unsigned int len;
973 len = strlenW( object ) + 1;
974 if (object[0] != '/') len++;
975 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
977 if (object[0] != '/') *p++ = '/';
978 strcpyW( p, object );
979 request->path = path;
981 else if (!(request->path = strdupW( slashW ))) goto end;
983 if (!version || !version[0]) version = http1_1;
984 if (!(request->version = strdupW( version ))) goto end;
985 if (!(store_accept_types( request, types ))) goto end;
987 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
988 request->hdr.handle = hrequest;
990 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
992 end:
993 release_object( &request->hdr );
994 release_object( &connect->hdr );
995 TRACE("returning %p\n", hrequest);
996 return hrequest;
999 /***********************************************************************
1000 * WinHttpCloseHandle (winhttp.@)
1002 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1004 object_header_t *hdr;
1006 TRACE("%p\n", handle);
1008 if (!(hdr = grab_object( handle )))
1010 set_last_error( ERROR_INVALID_HANDLE );
1011 return FALSE;
1013 release_object( hdr );
1014 free_handle( handle );
1015 return TRUE;
1018 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1020 BOOL ret = FALSE;
1022 if (!buflen)
1024 set_last_error( ERROR_INVALID_PARAMETER );
1025 return FALSE;
1028 switch (option)
1030 case WINHTTP_OPTION_CONTEXT_VALUE:
1032 if (!buffer || *buflen < sizeof(DWORD_PTR))
1034 *buflen = sizeof(DWORD_PTR);
1035 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1036 return FALSE;
1039 *(DWORD_PTR *)buffer = hdr->context;
1040 *buflen = sizeof(DWORD_PTR);
1041 return TRUE;
1043 default:
1044 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1045 else
1047 FIXME("unimplemented option %u\n", option);
1048 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1049 return FALSE;
1051 break;
1053 return ret;
1056 /***********************************************************************
1057 * WinHttpQueryOption (winhttp.@)
1059 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1061 BOOL ret = FALSE;
1062 object_header_t *hdr;
1064 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1066 if (!(hdr = grab_object( handle )))
1068 set_last_error( ERROR_INVALID_HANDLE );
1069 return FALSE;
1072 ret = query_option( hdr, option, buffer, buflen );
1074 release_object( hdr );
1075 return ret;
1078 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1080 BOOL ret = TRUE;
1082 if (!buffer)
1084 set_last_error( ERROR_INVALID_PARAMETER );
1085 return FALSE;
1088 switch (option)
1090 case WINHTTP_OPTION_CONTEXT_VALUE:
1092 if (buflen != sizeof(DWORD_PTR))
1094 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1095 return FALSE;
1098 hdr->context = *(DWORD_PTR *)buffer;
1099 return TRUE;
1101 default:
1102 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1103 else
1105 FIXME("unimplemented option %u\n", option);
1106 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1107 return FALSE;
1109 break;
1111 return ret;
1114 /***********************************************************************
1115 * WinHttpSetOption (winhttp.@)
1117 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1119 BOOL ret = FALSE;
1120 object_header_t *hdr;
1122 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1124 if (!(hdr = grab_object( handle )))
1126 set_last_error( ERROR_INVALID_HANDLE );
1127 return FALSE;
1130 ret = set_option( hdr, option, buffer, buflen );
1132 release_object( hdr );
1133 return ret;
1136 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1138 char *ret;
1139 DWORD size = 0;
1141 GetComputerNameExA( format, NULL, &size );
1142 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1143 if (!(ret = heap_alloc( size ))) return NULL;
1144 if (!GetComputerNameExA( format, ret, &size ))
1146 heap_free( ret );
1147 return NULL;
1149 return ret;
1152 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1154 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1156 if (len_suffix > len_domain) return FALSE;
1157 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1158 return FALSE;
1161 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1163 sprintfW( buf, fmt,
1164 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1165 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1166 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1167 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1170 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1172 static const WCHAR fmtW[] =
1173 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1174 '/','w','p','a','d','.','d','a','t',0};
1175 WCHAR *ret;
1177 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1178 if (!ai) return NULL;
1180 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1181 printf_addr( fmtW, ret, (struct sockaddr_in *)ai->ai_addr );
1182 return ret;
1185 /***********************************************************************
1186 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1188 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1190 BOOL ret = FALSE;
1192 TRACE("0x%08x, %p\n", flags, url);
1194 if (!flags || !url)
1196 set_last_error( ERROR_INVALID_PARAMETER );
1197 return FALSE;
1199 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1201 static int fixme_shown;
1202 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1204 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1206 #ifdef HAVE_GETADDRINFO
1207 char *fqdn, *domain, *p;
1209 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1210 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1212 heap_free( fqdn );
1213 return FALSE;
1215 p = fqdn;
1216 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1218 struct addrinfo *ai;
1219 char *name;
1220 int res;
1222 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1224 heap_free( fqdn );
1225 heap_free( domain );
1226 return FALSE;
1228 strcpy( name, "wpad" );
1229 strcat( name, p );
1230 res = getaddrinfo( name, NULL, NULL, &ai );
1231 heap_free( name );
1232 if (!res)
1234 *url = build_wpad_url( ai );
1235 freeaddrinfo( ai );
1236 if (*url)
1238 TRACE("returning %s\n", debugstr_w(*url));
1239 ret = TRUE;
1240 break;
1243 p++;
1245 heap_free( domain );
1246 heap_free( fqdn );
1247 #else
1248 FIXME("getaddrinfo not found at build time\n");
1249 #endif
1251 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1252 return ret;
1255 static const WCHAR Connections[] = {
1256 'S','o','f','t','w','a','r','e','\\',
1257 'M','i','c','r','o','s','o','f','t','\\',
1258 'W','i','n','d','o','w','s','\\',
1259 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1260 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1261 'C','o','n','n','e','c','t','i','o','n','s',0 };
1262 static const WCHAR WinHttpSettings[] = {
1263 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1264 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1265 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1266 static const DWORD PROXY_TYPE_DIRECT = 1;
1267 static const DWORD PROXY_TYPE_PROXY = 2;
1268 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1269 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1271 struct connection_settings_header
1273 DWORD magic;
1274 DWORD unknown; /* always zero? */
1275 DWORD flags; /* one or more of PROXY_* */
1278 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1280 const BYTE *begin;
1282 for (begin = src; src - begin < len; src++, dst++)
1283 *dst = *src;
1284 *dst = 0;
1287 /***********************************************************************
1288 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1290 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1292 LONG l;
1293 HKEY key;
1294 BOOL got_from_reg = FALSE, direct = TRUE;
1295 char *envproxy;
1297 TRACE("%p\n", info);
1299 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1300 if (!l)
1302 DWORD type, size = 0;
1304 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1305 if (!l && type == REG_BINARY &&
1306 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1308 BYTE *buf = heap_alloc( size );
1310 if (buf)
1312 struct connection_settings_header *hdr =
1313 (struct connection_settings_header *)buf;
1314 DWORD *len = (DWORD *)(hdr + 1);
1316 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1317 &size );
1318 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1319 hdr->unknown == 0)
1321 if (hdr->flags & PROXY_TYPE_PROXY)
1323 BOOL sane = FALSE;
1324 LPWSTR proxy = NULL;
1325 LPWSTR proxy_bypass = NULL;
1327 /* Sanity-check length of proxy string */
1328 if ((BYTE *)len - buf + *len <= size)
1330 sane = TRUE;
1331 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1332 if (proxy)
1333 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1334 len = (DWORD *)((BYTE *)(len + 1) + *len);
1336 if (sane)
1338 /* Sanity-check length of proxy bypass string */
1339 if ((BYTE *)len - buf + *len <= size)
1341 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1342 if (proxy_bypass)
1343 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1345 else
1347 sane = FALSE;
1348 GlobalFree( proxy );
1349 proxy = NULL;
1352 info->lpszProxy = proxy;
1353 info->lpszProxyBypass = proxy_bypass;
1354 if (sane)
1356 got_from_reg = TRUE;
1357 direct = FALSE;
1358 info->dwAccessType =
1359 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1360 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1361 debugstr_w(info->lpszProxy),
1362 debugstr_w(info->lpszProxyBypass));
1366 heap_free( buf );
1369 RegCloseKey( key );
1371 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1373 char *colon, *http_proxy;
1375 if ((colon = strchr( envproxy, ':' )))
1377 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1379 static const char http[] = "http://";
1381 /* It's a scheme, check that it's http */
1382 if (!strncmp( envproxy, http, strlen( http ) ))
1383 http_proxy = envproxy + strlen( http );
1384 else
1386 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1387 http_proxy = NULL;
1390 else
1391 http_proxy = envproxy;
1393 else
1394 http_proxy = envproxy;
1395 if (http_proxy)
1397 WCHAR *http_proxyW;
1398 int len;
1400 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1401 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1403 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1404 direct = FALSE;
1405 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1406 info->lpszProxy = http_proxyW;
1407 info->lpszProxyBypass = NULL;
1408 TRACE("http proxy (from environment) = %s\n",
1409 debugstr_w(info->lpszProxy));
1413 if (direct)
1415 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1416 info->lpszProxy = NULL;
1417 info->lpszProxyBypass = NULL;
1419 return TRUE;
1422 /***********************************************************************
1423 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1425 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1427 static const WCHAR settingsW[] =
1428 {'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};
1429 HKEY hkey = NULL;
1430 struct connection_settings_header *hdr = NULL;
1431 DWORD type, offset, len, size = 0;
1432 BOOL ret = FALSE;
1434 TRACE("%p\n", config);
1436 if (!config)
1438 set_last_error( ERROR_INVALID_PARAMETER );
1439 return FALSE;
1441 memset( config, 0, sizeof(*config) );
1442 config->fAutoDetect = TRUE;
1444 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1445 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1446 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1448 ret = TRUE;
1449 goto done;
1451 if (!(hdr = heap_alloc( size ))) goto done;
1452 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1453 hdr->magic != WININET_SETTINGS_MAGIC)
1455 ret = TRUE;
1456 goto done;
1459 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1460 offset = sizeof(*hdr);
1461 if (offset + sizeof(DWORD) > size) goto done;
1462 len = *(DWORD *)((char *)hdr + offset);
1463 offset += sizeof(DWORD);
1464 if (len && hdr->flags & PROXY_TYPE_PROXY)
1466 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1467 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1469 offset += len;
1470 if (offset + sizeof(DWORD) > size) goto done;
1471 len = *(DWORD *)((char *)hdr + offset);
1472 offset += sizeof(DWORD);
1473 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1475 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1476 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1478 offset += len;
1479 if (offset + sizeof(DWORD) > size) goto done;
1480 len = *(DWORD *)((char *)hdr + offset);
1481 offset += sizeof(DWORD);
1482 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1484 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1485 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1487 ret = TRUE;
1489 done:
1490 RegCloseKey( hkey );
1491 heap_free( hdr );
1492 if (!ret)
1494 heap_free( config->lpszAutoConfigUrl );
1495 config->lpszAutoConfigUrl = NULL;
1496 heap_free( config->lpszProxy );
1497 config->lpszProxy = NULL;
1498 heap_free( config->lpszProxyBypass );
1499 config->lpszProxyBypass = NULL;
1501 return ret;
1504 static HRESULT WINAPI dispex_QueryInterface(
1505 IDispatchEx *iface, REFIID riid, void **ppv )
1507 *ppv = NULL;
1509 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1510 IsEqualGUID( riid, &IID_IDispatch ) ||
1511 IsEqualGUID( riid, &IID_IDispatchEx ))
1512 *ppv = iface;
1513 else
1514 return E_NOINTERFACE;
1516 return S_OK;
1519 static ULONG WINAPI dispex_AddRef(
1520 IDispatchEx *iface )
1522 return 2;
1525 static ULONG WINAPI dispex_Release(
1526 IDispatchEx *iface )
1528 return 1;
1531 static HRESULT WINAPI dispex_GetTypeInfoCount(
1532 IDispatchEx *iface, UINT *info )
1534 return E_NOTIMPL;
1537 static HRESULT WINAPI dispex_GetTypeInfo(
1538 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1540 return E_NOTIMPL;
1543 static HRESULT WINAPI dispex_GetIDsOfNames(
1544 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1546 return E_NOTIMPL;
1549 static HRESULT WINAPI dispex_Invoke(
1550 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1551 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1553 return E_NOTIMPL;
1556 static HRESULT WINAPI dispex_DeleteMemberByName(
1557 IDispatchEx *iface, BSTR name, DWORD flags )
1559 return E_NOTIMPL;
1562 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1563 IDispatchEx *iface, DISPID id )
1565 return E_NOTIMPL;
1568 static HRESULT WINAPI dispex_GetMemberProperties(
1569 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1571 return E_NOTIMPL;
1574 static HRESULT WINAPI dispex_GetMemberName(
1575 IDispatchEx *iface, DISPID id, BSTR *name )
1577 return E_NOTIMPL;
1580 static HRESULT WINAPI dispex_GetNextDispID(
1581 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1583 return E_NOTIMPL;
1586 static HRESULT WINAPI dispex_GetNameSpaceParent(
1587 IDispatchEx *iface, IUnknown **unk )
1589 return E_NOTIMPL;
1592 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1594 static HRESULT WINAPI dispex_GetDispID(
1595 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1597 if (!strcmpW( name, dns_resolveW ))
1599 *id = DISPID_GLOBAL_DNSRESOLVE;
1600 return S_OK;
1602 return DISP_E_UNKNOWNNAME;
1605 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1607 #ifdef HAVE_GETADDRINFO
1608 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1609 WCHAR addr[16];
1610 struct addrinfo *ai, *elem;
1611 char *hostnameA;
1612 int res;
1614 if (hostname[0])
1615 hostnameA = strdupWA( hostname );
1616 else
1617 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1619 if (!hostnameA) return E_OUTOFMEMORY;
1620 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1621 heap_free( hostnameA );
1622 if (res) return S_FALSE;
1624 elem = ai;
1625 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1626 if (!elem)
1628 freeaddrinfo( ai );
1629 return S_FALSE;
1631 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1632 freeaddrinfo( ai );
1633 V_VT( result ) = VT_BSTR;
1634 V_BSTR( result ) = SysAllocString( addr );
1635 return S_OK;
1636 #else
1637 FIXME("getaddrinfo not found at build time\n");
1638 return S_FALSE;
1639 #endif
1642 static HRESULT WINAPI dispex_InvokeEx(
1643 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1644 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1646 if (id == DISPID_GLOBAL_DNSRESOLVE)
1648 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1649 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1650 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
1652 return DISP_E_MEMBERNOTFOUND;
1655 static const IDispatchExVtbl dispex_vtbl =
1657 dispex_QueryInterface,
1658 dispex_AddRef,
1659 dispex_Release,
1660 dispex_GetTypeInfoCount,
1661 dispex_GetTypeInfo,
1662 dispex_GetIDsOfNames,
1663 dispex_Invoke,
1664 dispex_GetDispID,
1665 dispex_InvokeEx,
1666 dispex_DeleteMemberByName,
1667 dispex_DeleteMemberByDispID,
1668 dispex_GetMemberProperties,
1669 dispex_GetMemberName,
1670 dispex_GetNextDispID,
1671 dispex_GetNameSpaceParent
1674 static IDispatchEx global_dispex = { &dispex_vtbl };
1676 static HRESULT WINAPI site_QueryInterface(
1677 IActiveScriptSite *iface, REFIID riid, void **ppv )
1679 *ppv = NULL;
1681 if (IsEqualGUID( &IID_IUnknown, riid ))
1682 *ppv = iface;
1683 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1684 *ppv = iface;
1685 else
1686 return E_NOINTERFACE;
1688 IUnknown_AddRef( (IUnknown *)*ppv );
1689 return S_OK;
1692 static ULONG WINAPI site_AddRef(
1693 IActiveScriptSite *iface )
1695 return 2;
1698 static ULONG WINAPI site_Release(
1699 IActiveScriptSite *iface )
1701 return 1;
1704 static HRESULT WINAPI site_GetLCID(
1705 IActiveScriptSite *iface, LCID *lcid )
1707 return E_NOTIMPL;
1710 static HRESULT WINAPI site_GetItemInfo(
1711 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1712 IUnknown **item, ITypeInfo **type_info )
1714 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1716 *item = (IUnknown *)&global_dispex;
1717 return S_OK;
1719 return E_NOTIMPL;
1722 static HRESULT WINAPI site_GetDocVersionString(
1723 IActiveScriptSite *iface, BSTR *version )
1725 return E_NOTIMPL;
1728 static HRESULT WINAPI site_OnScriptTerminate(
1729 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1731 return E_NOTIMPL;
1734 static HRESULT WINAPI site_OnStateChange(
1735 IActiveScriptSite *iface, SCRIPTSTATE state )
1737 return E_NOTIMPL;
1740 static HRESULT WINAPI site_OnScriptError(
1741 IActiveScriptSite *iface, IActiveScriptError *error )
1743 return E_NOTIMPL;
1746 static HRESULT WINAPI site_OnEnterScript(
1747 IActiveScriptSite *iface )
1749 return E_NOTIMPL;
1752 static HRESULT WINAPI site_OnLeaveScript(
1753 IActiveScriptSite *iface )
1755 return E_NOTIMPL;
1758 static const IActiveScriptSiteVtbl site_vtbl =
1760 site_QueryInterface,
1761 site_AddRef,
1762 site_Release,
1763 site_GetLCID,
1764 site_GetItemInfo,
1765 site_GetDocVersionString,
1766 site_OnScriptTerminate,
1767 site_OnStateChange,
1768 site_OnScriptError,
1769 site_OnEnterScript,
1770 site_OnLeaveScript
1773 static IActiveScriptSite script_site = { &site_vtbl };
1775 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1777 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1778 const WCHAR *p;
1779 WCHAR *q;
1780 int len;
1782 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1783 info->lpszProxy = NULL;
1784 info->lpszProxyBypass = NULL;
1786 if (V_VT( &result ) != VT_BSTR) return TRUE;
1787 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1789 p = V_BSTR( &result );
1790 while (*p == ' ') p++;
1791 len = strlenW( p );
1792 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1794 p += 5;
1795 while (*p == ' ') p++;
1796 if (!*p || *p == ';') return TRUE;
1797 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1798 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1799 for (; *q; q++)
1801 if (*q == ' ' || *q == ';')
1803 *q = 0;
1804 break;
1808 return TRUE;
1811 static BSTR include_pac_utils( BSTR script )
1813 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1814 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1815 HRSRC rsrc;
1816 DWORD size;
1817 const char *data;
1818 BSTR ret;
1819 int len;
1821 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1822 size = SizeofResource( hmod, rsrc );
1823 data = LoadResource( hmod, rsrc );
1825 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1826 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1827 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1828 ret[len] = 0;
1829 strcatW( ret, script );
1830 return ret;
1833 #ifdef _WIN64
1834 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1835 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1836 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1837 #else
1838 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1839 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1840 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1841 #endif
1843 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1845 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1846 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1847 IActiveScriptParse *parser = NULL;
1848 IActiveScript *engine = NULL;
1849 IDispatch *dispatch = NULL;
1850 BOOL ret = FALSE;
1851 CLSID clsid;
1852 DISPID dispid;
1853 BSTR func = NULL, hostname = NULL, full_script = NULL;
1854 URL_COMPONENTSW uc;
1855 VARIANT args[2], result;
1856 DISPPARAMS params;
1857 HRESULT hr, init;
1859 memset( &uc, 0, sizeof(uc) );
1860 uc.dwStructSize = sizeof(uc);
1861 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1862 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1863 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1864 hostname[uc.dwHostNameLength] = 0;
1866 init = CoInitialize( NULL );
1867 hr = CLSIDFromProgID( jscriptW, &clsid );
1868 if (hr != S_OK) goto done;
1870 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1871 &IID_IActiveScript, (void **)&engine );
1872 if (hr != S_OK) goto done;
1874 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1875 if (hr != S_OK) goto done;
1877 hr = IActiveScriptParse_InitNew( parser );
1878 if (hr != S_OK) goto done;
1880 hr = IActiveScript_SetScriptSite( engine, &script_site );
1881 if (hr != S_OK) goto done;
1883 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1884 if (hr != S_OK) goto done;
1886 if (!(full_script = include_pac_utils( script ))) goto done;
1888 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1889 if (hr != S_OK) goto done;
1891 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1892 if (hr != S_OK) goto done;
1894 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1895 if (hr != S_OK) goto done;
1897 if (!(func = SysAllocString( findproxyW ))) goto done;
1898 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1899 if (hr != S_OK) goto done;
1901 V_VT( &args[0] ) = VT_BSTR;
1902 V_BSTR( &args[0] ) = hostname;
1903 V_VT( &args[1] ) = VT_BSTR;
1904 V_BSTR( &args[1] ) = SysAllocString( url );
1906 params.rgvarg = args;
1907 params.rgdispidNamedArgs = NULL;
1908 params.cArgs = 2;
1909 params.cNamedArgs = 0;
1910 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1911 &params, &result, NULL, NULL );
1912 VariantClear( &args[1] );
1913 if (hr != S_OK)
1915 WARN("script failed 0x%08x\n", hr);
1916 goto done;
1918 ret = parse_script_result( result, info );
1920 done:
1921 SysFreeString( full_script );
1922 SysFreeString( hostname );
1923 SysFreeString( func );
1924 if (dispatch) IDispatch_Release( dispatch );
1925 if (parser) IActiveScriptParse_Release( parser );
1926 if (engine) IActiveScript_Release( engine );
1927 if (SUCCEEDED( init )) CoUninitialize();
1928 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1929 return ret;
1932 static BSTR download_script( const WCHAR *url )
1934 static const WCHAR typeW[] = {'*','/','*',0};
1935 static const WCHAR *acceptW[] = {typeW, NULL};
1936 HINTERNET ses, con = NULL, req = NULL;
1937 WCHAR *hostname;
1938 URL_COMPONENTSW uc;
1939 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1940 char *tmp, *buffer = NULL;
1941 BSTR script = NULL;
1942 int len;
1944 memset( &uc, 0, sizeof(uc) );
1945 uc.dwStructSize = sizeof(uc);
1946 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1947 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1948 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1949 hostname[uc.dwHostNameLength] = 0;
1951 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1952 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1953 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1954 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1955 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1956 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1958 if (!(buffer = heap_alloc( size ))) goto done;
1959 to_read = size;
1960 offset = 0;
1961 for (;;)
1963 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1964 if (!bytes_read) break;
1965 to_read -= bytes_read;
1966 offset += bytes_read;
1967 if (!to_read)
1969 to_read = size;
1970 size *= 2;
1971 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1972 buffer = tmp;
1975 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1976 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1977 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1978 script[len] = 0;
1980 done:
1981 WinHttpCloseHandle( req );
1982 WinHttpCloseHandle( con );
1983 WinHttpCloseHandle( ses );
1984 heap_free( buffer );
1985 heap_free( hostname );
1986 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1987 return script;
1990 /***********************************************************************
1991 * WinHttpGetProxyForUrl (winhttp.@)
1993 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1994 WINHTTP_PROXY_INFO *info )
1996 WCHAR *detected_pac_url = NULL;
1997 const WCHAR *pac_url;
1998 session_t *session;
1999 BSTR script;
2000 BOOL ret = FALSE;
2002 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
2004 if (!(session = (session_t *)grab_object( hsession )))
2006 set_last_error( ERROR_INVALID_HANDLE );
2007 return FALSE;
2009 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
2011 release_object( &session->hdr );
2012 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2013 return FALSE;
2015 if (!url || !options || !info ||
2016 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
2017 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
2018 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
2019 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
2021 release_object( &session->hdr );
2022 set_last_error( ERROR_INVALID_PARAMETER );
2023 return FALSE;
2025 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2026 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2028 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
2029 goto done;
2031 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2032 else pac_url = detected_pac_url;
2034 if (!(script = download_script( pac_url ))) goto done;
2035 ret = run_script( script, url, info );
2036 SysFreeString( script );
2038 done:
2039 GlobalFree( detected_pac_url );
2040 release_object( &session->hdr );
2041 return ret;
2044 /***********************************************************************
2045 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2047 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2049 LONG l;
2050 HKEY key;
2051 BOOL ret = FALSE;
2052 const WCHAR *src;
2054 TRACE("%p\n", info);
2056 if (!info)
2058 set_last_error( ERROR_INVALID_PARAMETER );
2059 return FALSE;
2061 switch (info->dwAccessType)
2063 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2064 break;
2065 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2066 if (!info->lpszProxy)
2068 set_last_error( ERROR_INVALID_PARAMETER );
2069 return FALSE;
2071 /* Only ASCII characters are allowed */
2072 for (src = info->lpszProxy; *src; src++)
2073 if (*src > 0x7f)
2075 set_last_error( ERROR_INVALID_PARAMETER );
2076 return FALSE;
2078 if (info->lpszProxyBypass)
2080 for (src = info->lpszProxyBypass; *src; src++)
2081 if (*src > 0x7f)
2083 set_last_error( ERROR_INVALID_PARAMETER );
2084 return FALSE;
2087 break;
2088 default:
2089 set_last_error( ERROR_INVALID_PARAMETER );
2090 return FALSE;
2093 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2094 KEY_WRITE, NULL, &key, NULL );
2095 if (!l)
2097 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2098 BYTE *buf;
2100 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2102 size += strlenW( info->lpszProxy );
2103 if (info->lpszProxyBypass)
2104 size += strlenW( info->lpszProxyBypass );
2106 buf = heap_alloc( size );
2107 if (buf)
2109 struct connection_settings_header *hdr =
2110 (struct connection_settings_header *)buf;
2111 DWORD *len = (DWORD *)(hdr + 1);
2113 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2114 hdr->unknown = 0;
2115 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2117 BYTE *dst;
2119 hdr->flags = PROXY_TYPE_PROXY;
2120 *len++ = strlenW( info->lpszProxy );
2121 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2122 src++, dst++)
2123 *dst = *src;
2124 len = (DWORD *)dst;
2125 if (info->lpszProxyBypass)
2127 *len++ = strlenW( info->lpszProxyBypass );
2128 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2129 src++, dst++)
2130 *dst = *src;
2132 else
2133 *len++ = 0;
2135 else
2137 hdr->flags = PROXY_TYPE_DIRECT;
2138 *len++ = 0;
2139 *len++ = 0;
2141 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2142 if (!l)
2143 ret = TRUE;
2144 heap_free( buf );
2146 RegCloseKey( key );
2148 return ret;
2151 /***********************************************************************
2152 * WinHttpSetStatusCallback (winhttp.@)
2154 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2155 DWORD flags, DWORD_PTR reserved )
2157 object_header_t *hdr;
2158 WINHTTP_STATUS_CALLBACK ret;
2160 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2162 if (!(hdr = grab_object( handle )))
2164 set_last_error( ERROR_INVALID_HANDLE );
2165 return WINHTTP_INVALID_STATUS_CALLBACK;
2167 ret = hdr->callback;
2168 hdr->callback = callback;
2169 hdr->notify_mask = flags;
2171 release_object( hdr );
2172 return ret;
2175 /***********************************************************************
2176 * WinHttpSetTimeouts (winhttp.@)
2178 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2180 BOOL ret = TRUE;
2181 object_header_t *hdr;
2182 request_t *request;
2183 session_t *session;
2185 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2187 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2189 set_last_error( ERROR_INVALID_PARAMETER );
2190 return FALSE;
2193 if (!(hdr = grab_object( handle )))
2195 set_last_error( ERROR_INVALID_HANDLE );
2196 return FALSE;
2199 switch(hdr->type)
2201 case WINHTTP_HANDLE_TYPE_REQUEST:
2202 request = (request_t *)hdr;
2203 request->connect_timeout = connect;
2205 if (resolve < 0) resolve = 0;
2206 request->resolve_timeout = resolve;
2208 if (send < 0) send = 0;
2209 request->send_timeout = send;
2211 if (receive < 0) receive = 0;
2212 request->recv_timeout = receive;
2214 if (netconn_connected( &request->netconn ))
2216 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2217 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2220 release_object( &request->hdr );
2221 break;
2223 case WINHTTP_HANDLE_TYPE_SESSION:
2224 session = (session_t *)hdr;
2225 session->connect_timeout = connect;
2227 if (resolve < 0) resolve = 0;
2228 session->resolve_timeout = resolve;
2230 if (send < 0) send = 0;
2231 session->send_timeout = send;
2233 if (receive < 0) receive = 0;
2234 session->recv_timeout = receive;
2235 break;
2237 default:
2238 release_object( hdr );
2239 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2240 return FALSE;
2242 return ret;
2245 static const WCHAR wkday[7][4] =
2246 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2247 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2248 static const WCHAR month[12][4] =
2249 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2250 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2251 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2253 /***********************************************************************
2254 * WinHttpTimeFromSystemTime (WININET.@)
2256 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2258 static const WCHAR format[] =
2259 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2260 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2262 TRACE("%p, %p\n", time, string);
2264 if (!time || !string) return FALSE;
2266 sprintfW( string, format,
2267 wkday[time->wDayOfWeek],
2268 time->wDay,
2269 month[time->wMonth - 1],
2270 time->wYear,
2271 time->wHour,
2272 time->wMinute,
2273 time->wSecond );
2275 return TRUE;
2278 /***********************************************************************
2279 * WinHttpTimeToSystemTime (WININET.@)
2281 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2283 unsigned int i;
2284 const WCHAR *s = string;
2285 WCHAR *end;
2287 TRACE("%s, %p\n", debugstr_w(string), time);
2289 if (!string || !time) return FALSE;
2291 /* Windows does this too */
2292 GetSystemTime( time );
2294 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2295 * a SYSTEMTIME structure.
2298 while (*s && !isalphaW( *s )) s++;
2299 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2300 time->wDayOfWeek = 7;
2302 for (i = 0; i < 7; i++)
2304 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2305 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2306 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2308 time->wDayOfWeek = i;
2309 break;
2313 if (time->wDayOfWeek > 6) return TRUE;
2314 while (*s && !isdigitW( *s )) s++;
2315 time->wDay = strtolW( s, &end, 10 );
2316 s = end;
2318 while (*s && !isalphaW( *s )) s++;
2319 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2320 time->wMonth = 0;
2322 for (i = 0; i < 12; i++)
2324 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2325 toupperW( month[i][1]) == toupperW( s[1] ) &&
2326 toupperW( month[i][2]) == toupperW( s[2] ) )
2328 time->wMonth = i + 1;
2329 break;
2332 if (time->wMonth == 0) return TRUE;
2334 while (*s && !isdigitW( *s )) s++;
2335 if (*s == '\0') return TRUE;
2336 time->wYear = strtolW( s, &end, 10 );
2337 s = end;
2339 while (*s && !isdigitW( *s )) s++;
2340 if (*s == '\0') return TRUE;
2341 time->wHour = strtolW( s, &end, 10 );
2342 s = end;
2344 while (*s && !isdigitW( *s )) s++;
2345 if (*s == '\0') return TRUE;
2346 time->wMinute = strtolW( s, &end, 10 );
2347 s = end;
2349 while (*s && !isdigitW( *s )) s++;
2350 if (*s == '\0') return TRUE;
2351 time->wSecond = strtolW( s, &end, 10 );
2353 time->wMilliseconds = 0;
2354 return TRUE;