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
20 #include "wine/port.h"
21 #include "wine/debug.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 )
51 SetLastError( error );
54 DWORD get_last_error( void )
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 )
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;
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 );
100 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
102 session_t *session = (session_t *)hdr;
106 case WINHTTP_OPTION_REDIRECT_POLICY:
108 if (!buffer || *buflen < sizeof(DWORD))
110 *buflen = sizeof(DWORD);
111 set_last_error( ERROR_INSUFFICIENT_BUFFER );
115 *(DWORD *)buffer = hdr->redirect_policy;
116 *buflen = sizeof(DWORD);
119 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
120 *(DWORD *)buffer = session->resolve_timeout;
121 *buflen = sizeof(DWORD);
123 case WINHTTP_OPTION_CONNECT_TIMEOUT:
124 *(DWORD *)buffer = session->connect_timeout;
125 *buflen = sizeof(DWORD);
127 case WINHTTP_OPTION_SEND_TIMEOUT:
128 *(DWORD *)buffer = session->send_timeout;
129 *buflen = sizeof(DWORD);
131 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
132 *(DWORD *)buffer = session->recv_timeout;
133 *buflen = sizeof(DWORD);
136 FIXME("unimplemented option %u\n", option);
137 set_last_error( ERROR_INVALID_PARAMETER );
142 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
144 session_t *session = (session_t *)hdr;
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));
155 case WINHTTP_OPTION_REDIRECT_POLICY:
159 if (buflen != sizeof(policy))
161 set_last_error( ERROR_INSUFFICIENT_BUFFER );
165 policy = *(DWORD *)buffer;
166 TRACE("0x%x\n", policy);
167 hdr->redirect_policy = policy;
170 case WINHTTP_OPTION_DISABLE_FEATURE:
171 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
173 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
174 session->resolve_timeout = *(DWORD *)buffer;
176 case WINHTTP_OPTION_CONNECT_TIMEOUT:
177 session->connect_timeout = *(DWORD *)buffer;
179 case WINHTTP_OPTION_SEND_TIMEOUT:
180 session->send_timeout = *(DWORD *)buffer;
182 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
183 session->recv_timeout = *(DWORD *)buffer;
186 FIXME("unimplemented option %u\n", option);
187 set_last_error( ERROR_INVALID_PARAMETER );
192 static const object_vtbl_t session_vtbl =
195 session_query_option,
199 /***********************************************************************
200 * WinHttpOpen (winhttp.@)
202 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
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( (LPWSTR)info.lpszProxy );
233 GlobalFree( (LPWSTR)info.lpszProxyBypass );
236 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
238 GlobalFree( (LPWSTR)info.lpszProxy );
239 GlobalFree( (LPWSTR)info.lpszProxyBypass );
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;
254 release_object( &session->hdr );
255 TRACE("returning %p\n", 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;
283 case WINHTTP_OPTION_PARENT_HANDLE:
285 if (!buffer || *buflen < sizeof(HINTERNET))
287 *buflen = sizeof(HINTERNET);
288 set_last_error( ERROR_INSUFFICIENT_BUFFER );
292 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
293 *buflen = sizeof(HINTERNET);
296 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
297 *(DWORD *)buffer = connect->session->resolve_timeout;
298 *buflen = sizeof(DWORD);
300 case WINHTTP_OPTION_CONNECT_TIMEOUT:
301 *(DWORD *)buffer = connect->session->connect_timeout;
302 *buflen = sizeof(DWORD);
304 case WINHTTP_OPTION_SEND_TIMEOUT:
305 *(DWORD *)buffer = connect->session->send_timeout;
306 *buflen = sizeof(DWORD);
308 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
309 *(DWORD *)buffer = connect->session->recv_timeout;
310 *buflen = sizeof(DWORD);
313 FIXME("unimplemented option %u\n", option);
314 set_last_error( ERROR_INVALID_PARAMETER );
319 static const object_vtbl_t connect_vtbl =
322 connect_query_option,
326 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
328 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
331 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
333 else if (*domain == '*')
335 if (domain[1] == '.')
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, '.' );
346 int len = strlenW( dot + 1 );
348 if (len > strlenW( domain + 2 ))
352 /* The server's domain is longer than the wildcard, so it
353 * could be a subdomain. Compare the last portion of the
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
364 ret = *(ptr - 1) == '.';
368 ret = !strcmpiW( dot + 1, domain + 2 );
373 ret = !strcmpiW( server, domain );
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)
385 if (!session->proxy_bypass) return FALSE;
386 ptr = session->proxy_bypass;
390 ptr = strchrW( ptr, ';' );
392 ptr = strchrW( tmp, ' ' );
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 );
406 ret = domain_matches( server, tmp );
407 } while (ptr && !ret);
411 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
413 session_t *session = connect->session;
416 if (session->proxy_server && !should_bypass_proxy(session, server))
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) )))
433 memcpy( connect->servername, session->proxy_server,
434 (colon - session->proxy_server) * sizeof(WCHAR) );
435 connect->servername[colon - session->proxy_server] = 0;
437 connect->serverport = atoiW( colon + 1 );
439 connect->serverport = INTERNET_DEFAULT_PORT;
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 )))
454 connect->serverport = INTERNET_DEFAULT_PORT;
460 heap_free( connect->servername );
461 connect->resolved = FALSE;
462 if (!(connect->servername = strdupW( server )))
467 connect->serverport = port;
473 /***********************************************************************
474 * WinHttpConnect (winhttp.@)
476 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
480 HINTERNET hconnect = NULL;
482 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
486 set_last_error( ERROR_INVALID_PARAMETER );
489 if (!(session = (session_t *)grab_object( hsession )))
491 set_last_error( ERROR_INVALID_HANDLE );
494 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
496 release_object( &session->hdr );
497 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
500 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
502 release_object( &session->hdr );
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 list_init( &connect->hdr.children );
514 addref_object( &session->hdr );
515 connect->session = session;
516 list_add_head( &session->hdr.children, &connect->hdr.entry );
518 if (!(connect->hostname = strdupW( server ))) goto end;
519 connect->hostport = port;
520 if (!set_server_for_hostname( connect, server, port )) goto end;
522 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
523 connect->hdr.handle = hconnect;
525 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
528 release_object( &connect->hdr );
529 release_object( &session->hdr );
530 TRACE("returning %p\n", hconnect);
534 /***********************************************************************
535 * request_destroy (internal)
537 static void request_destroy( object_header_t *hdr )
539 request_t *request = (request_t *)hdr;
542 TRACE("%p\n", request);
544 release_object( &request->connect->hdr );
546 heap_free( request->verb );
547 heap_free( request->path );
548 heap_free( request->version );
549 heap_free( request->raw_headers );
550 heap_free( request->status_text );
551 for (i = 0; i < request->num_headers; i++)
553 heap_free( request->headers[i].field );
554 heap_free( request->headers[i].value );
556 heap_free( request->headers );
557 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
558 heap_free( request->accept_types );
559 heap_free( request );
562 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
565 if (str) len = strlenW( str );
566 if (buffer && *buflen > len)
568 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
571 *buflen = len * sizeof(WCHAR);
574 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
577 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
579 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
580 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
581 CertNameToStrW( encoding, blob, format, ret, size );
586 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
588 request_t *request = (request_t *)hdr;
592 case WINHTTP_OPTION_SECURITY_FLAGS:
597 if (!buffer || *buflen < sizeof(flags))
599 *buflen = sizeof(flags);
600 set_last_error( ERROR_INSUFFICIENT_BUFFER );
605 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
606 flags |= request->netconn.security_flags;
607 bits = netconn_get_cipher_strength( &request->netconn );
609 flags |= SECURITY_FLAG_STRENGTH_STRONG;
611 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
613 flags |= SECURITY_FLAG_STRENGTH_WEAK;
614 *(DWORD *)buffer = flags;
615 *buflen = sizeof(flags);
618 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
620 const CERT_CONTEXT *cert;
622 if (!buffer || *buflen < sizeof(cert))
624 *buflen = sizeof(cert);
625 set_last_error( ERROR_INSUFFICIENT_BUFFER );
629 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
630 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
631 *buflen = sizeof(cert);
634 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
636 const CERT_CONTEXT *cert;
637 const CRYPT_OID_INFO *oidInfo;
638 WINHTTP_CERTIFICATE_INFO *ci = buffer;
640 FIXME("partial stub\n");
642 if (!buffer || *buflen < sizeof(*ci))
644 *buflen = sizeof(*ci);
645 set_last_error( ERROR_INSUFFICIENT_BUFFER );
648 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
650 ci->ftExpiry = cert->pCertInfo->NotAfter;
651 ci->ftStart = cert->pCertInfo->NotBefore;
652 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
653 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
654 ci->lpszProtocolName = NULL;
655 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
656 cert->pCertInfo->SignatureAlgorithm.pszObjId,
659 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
661 ci->lpszSignatureAlgName = NULL;
662 ci->lpszEncryptionAlgName = NULL;
663 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
665 CertFreeCertificateContext( cert );
666 *buflen = sizeof(*ci);
669 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
671 if (!buffer || *buflen < sizeof(DWORD))
673 *buflen = sizeof(DWORD);
674 set_last_error( ERROR_INSUFFICIENT_BUFFER );
678 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
679 *buflen = sizeof(DWORD);
682 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
683 *(DWORD *)buffer = request->resolve_timeout;
684 *buflen = sizeof(DWORD);
686 case WINHTTP_OPTION_CONNECT_TIMEOUT:
687 *(DWORD *)buffer = request->connect_timeout;
688 *buflen = sizeof(DWORD);
690 case WINHTTP_OPTION_SEND_TIMEOUT:
691 *(DWORD *)buffer = request->send_timeout;
692 *buflen = sizeof(DWORD);
694 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
695 *(DWORD *)buffer = request->recv_timeout;
696 *buflen = sizeof(DWORD);
699 case WINHTTP_OPTION_USERNAME:
700 str_to_buffer( buffer, request->connect->username, buflen );
703 case WINHTTP_OPTION_PASSWORD:
704 str_to_buffer( buffer, request->connect->password, buflen );
707 case WINHTTP_OPTION_PROXY_USERNAME:
708 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
711 case WINHTTP_OPTION_PROXY_PASSWORD:
712 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
716 FIXME("unimplemented option %u\n", option);
717 set_last_error( ERROR_INVALID_PARAMETER );
722 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
725 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
727 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
731 set_last_error( ERROR_OUTOFMEMORY );
735 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
737 request_t *request = (request_t *)hdr;
741 case WINHTTP_OPTION_PROXY:
743 WINHTTP_PROXY_INFO *pi = buffer;
745 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
748 case WINHTTP_OPTION_DISABLE_FEATURE:
752 if (buflen != sizeof(DWORD))
754 set_last_error( ERROR_INSUFFICIENT_BUFFER );
758 disable = *(DWORD *)buffer;
759 TRACE("0x%x\n", disable);
760 hdr->disable_flags |= disable;
763 case WINHTTP_OPTION_AUTOLOGON_POLICY:
767 if (buflen != sizeof(DWORD))
769 set_last_error( ERROR_INSUFFICIENT_BUFFER );
773 policy = *(DWORD *)buffer;
774 TRACE("0x%x\n", policy);
775 hdr->logon_policy = policy;
778 case WINHTTP_OPTION_REDIRECT_POLICY:
782 if (buflen != sizeof(DWORD))
784 set_last_error( ERROR_INSUFFICIENT_BUFFER );
788 policy = *(DWORD *)buffer;
789 TRACE("0x%x\n", policy);
790 hdr->redirect_policy = policy;
793 case WINHTTP_OPTION_SECURITY_FLAGS:
797 if (buflen < sizeof(DWORD))
799 set_last_error( ERROR_INSUFFICIENT_BUFFER );
802 flags = *(DWORD *)buffer;
803 TRACE("0x%x\n", flags);
804 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
805 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
806 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
807 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
809 set_last_error( ERROR_INVALID_PARAMETER );
812 request->netconn.security_flags = flags;
815 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
816 request->resolve_timeout = *(DWORD *)buffer;
818 case WINHTTP_OPTION_CONNECT_TIMEOUT:
819 request->connect_timeout = *(DWORD *)buffer;
821 case WINHTTP_OPTION_SEND_TIMEOUT:
822 request->send_timeout = *(DWORD *)buffer;
824 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
825 request->recv_timeout = *(DWORD *)buffer;
828 case WINHTTP_OPTION_USERNAME:
830 connect_t *connect = request->connect;
832 heap_free( connect->username );
833 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
836 case WINHTTP_OPTION_PASSWORD:
838 connect_t *connect = request->connect;
840 heap_free( connect->password );
841 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
844 case WINHTTP_OPTION_PROXY_USERNAME:
846 session_t *session = request->connect->session;
848 heap_free( session->proxy_username );
849 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
852 case WINHTTP_OPTION_PROXY_PASSWORD:
854 session_t *session = request->connect->session;
856 heap_free( session->proxy_password );
857 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
861 FIXME("unimplemented option %u\n", option);
862 set_last_error( ERROR_INVALID_PARAMETER );
867 static const object_vtbl_t request_vtbl =
870 request_query_option,
874 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
876 const WCHAR **types = accept_types;
879 if (!types) return TRUE;
882 request->num_accept_types++;
885 if (!request->num_accept_types) return TRUE;
886 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
888 request->num_accept_types = 0;
891 types = accept_types;
892 for (i = 0; i < request->num_accept_types; i++)
894 if (!(request->accept_types[i] = strdupW( *types )))
896 for (; i >= 0; i--) heap_free( request->accept_types[i] );
897 heap_free( request->accept_types );
898 request->accept_types = NULL;
899 request->num_accept_types = 0;
907 /***********************************************************************
908 * WinHttpOpenRequest (winhttp.@)
910 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
911 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
915 HINTERNET hrequest = NULL;
917 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
918 debugstr_w(version), debugstr_w(referrer), types, flags);
920 if (!(connect = (connect_t *)grab_object( hconnect )))
922 set_last_error( ERROR_INVALID_HANDLE );
925 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
927 release_object( &connect->hdr );
928 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
931 if (!(request = heap_alloc_zero( sizeof(request_t) )))
933 release_object( &connect->hdr );
936 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
937 request->hdr.vtbl = &request_vtbl;
938 request->hdr.refs = 1;
939 request->hdr.flags = flags;
940 request->hdr.callback = connect->hdr.callback;
941 request->hdr.notify_mask = connect->hdr.notify_mask;
942 request->hdr.context = connect->hdr.context;
943 list_init( &request->hdr.children );
945 addref_object( &connect->hdr );
946 request->connect = connect;
947 list_add_head( &connect->hdr.children, &request->hdr.entry );
949 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
950 request->resolve_timeout = connect->session->resolve_timeout;
951 request->connect_timeout = connect->session->connect_timeout;
952 request->send_timeout = connect->session->send_timeout;
953 request->recv_timeout = connect->session->recv_timeout;
955 if (!verb || !verb[0]) verb = getW;
956 if (!(request->verb = strdupW( verb ))) goto end;
963 len = strlenW( object ) + 1;
964 if (object[0] != '/') len++;
965 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
967 if (object[0] != '/') *p++ = '/';
968 strcpyW( p, object );
969 request->path = path;
971 else if (!(request->path = strdupW( slashW ))) goto end;
973 if (!version || !version[0]) version = http1_1;
974 if (!(request->version = strdupW( version ))) goto end;
975 if (!(store_accept_types( request, types ))) goto end;
977 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
978 request->hdr.handle = hrequest;
980 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
983 release_object( &request->hdr );
984 release_object( &connect->hdr );
985 TRACE("returning %p\n", hrequest);
989 /***********************************************************************
990 * WinHttpCloseHandle (winhttp.@)
992 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
994 object_header_t *hdr;
996 TRACE("%p\n", handle);
998 if (!(hdr = grab_object( handle )))
1000 set_last_error( ERROR_INVALID_HANDLE );
1003 release_object( hdr );
1004 free_handle( handle );
1008 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1014 set_last_error( ERROR_INVALID_PARAMETER );
1020 case WINHTTP_OPTION_CONTEXT_VALUE:
1022 if (!buffer || *buflen < sizeof(DWORD_PTR))
1024 *buflen = sizeof(DWORD_PTR);
1025 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1029 *(DWORD_PTR *)buffer = hdr->context;
1030 *buflen = sizeof(DWORD_PTR);
1034 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1037 FIXME("unimplemented option %u\n", option);
1038 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1046 /***********************************************************************
1047 * WinHttpQueryOption (winhttp.@)
1049 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1052 object_header_t *hdr;
1054 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1056 if (!(hdr = grab_object( handle )))
1058 set_last_error( ERROR_INVALID_HANDLE );
1062 ret = query_option( hdr, option, buffer, buflen );
1064 release_object( hdr );
1068 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1074 set_last_error( ERROR_INVALID_PARAMETER );
1080 case WINHTTP_OPTION_CONTEXT_VALUE:
1082 if (buflen != sizeof(DWORD_PTR))
1084 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1088 hdr->context = *(DWORD_PTR *)buffer;
1092 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1095 FIXME("unimplemented option %u\n", option);
1096 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1104 /***********************************************************************
1105 * WinHttpSetOption (winhttp.@)
1107 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1110 object_header_t *hdr;
1112 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1114 if (!(hdr = grab_object( handle )))
1116 set_last_error( ERROR_INVALID_HANDLE );
1120 ret = set_option( hdr, option, buffer, buflen );
1122 release_object( hdr );
1126 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1131 GetComputerNameExA( format, NULL, &size );
1132 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1133 if (!(ret = heap_alloc( size ))) return NULL;
1134 if (!GetComputerNameExA( format, ret, &size ))
1142 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1144 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1146 if (len_suffix > len_domain) return FALSE;
1147 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1151 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1154 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1155 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1156 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1157 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1160 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1162 static const WCHAR fmtW[] =
1163 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1164 '/','w','p','a','d','.','d','a','t',0};
1167 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1168 if (!ai) return NULL;
1170 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1171 printf_addr( fmtW, ret, (struct sockaddr_in *)ai->ai_addr );
1175 /***********************************************************************
1176 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1178 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1182 TRACE("0x%08x, %p\n", flags, url);
1186 set_last_error( ERROR_INVALID_PARAMETER );
1189 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP) FIXME("discovery via DHCP not supported\n");
1190 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1192 #ifdef HAVE_GETADDRINFO
1193 char *fqdn, *domain, *p;
1195 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1196 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1202 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1204 struct addrinfo *ai;
1208 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1211 heap_free( domain );
1214 strcpy( name, "wpad" );
1216 res = getaddrinfo( name, NULL, NULL, &ai );
1220 *url = build_wpad_url( ai );
1224 TRACE("returning %s\n", debugstr_w(*url));
1231 heap_free( domain );
1234 FIXME("getaddrinfo not found at build time\n");
1237 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1241 static const WCHAR Connections[] = {
1242 'S','o','f','t','w','a','r','e','\\',
1243 'M','i','c','r','o','s','o','f','t','\\',
1244 'W','i','n','d','o','w','s','\\',
1245 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1246 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1247 'C','o','n','n','e','c','t','i','o','n','s',0 };
1248 static const WCHAR WinHttpSettings[] = {
1249 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1250 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1251 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1252 static const DWORD PROXY_TYPE_DIRECT = 1;
1253 static const DWORD PROXY_TYPE_PROXY = 2;
1254 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1255 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1257 struct connection_settings_header
1260 DWORD unknown; /* always zero? */
1261 DWORD flags; /* one or more of PROXY_* */
1264 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1268 for (begin = src; src - begin < len; src++, dst++)
1273 /***********************************************************************
1274 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1276 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1280 BOOL got_from_reg = FALSE, direct = TRUE;
1283 TRACE("%p\n", info);
1285 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1288 DWORD type, size = 0;
1290 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1291 if (!l && type == REG_BINARY &&
1292 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1294 BYTE *buf = heap_alloc( size );
1298 struct connection_settings_header *hdr =
1299 (struct connection_settings_header *)buf;
1300 DWORD *len = (DWORD *)(hdr + 1);
1302 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1304 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1307 if (hdr->flags & PROXY_TYPE_PROXY)
1310 LPWSTR proxy = NULL;
1311 LPWSTR proxy_bypass = NULL;
1313 /* Sanity-check length of proxy string */
1314 if ((BYTE *)len - buf + *len <= size)
1317 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1319 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1320 len = (DWORD *)((BYTE *)(len + 1) + *len);
1324 /* Sanity-check length of proxy bypass string */
1325 if ((BYTE *)len - buf + *len <= size)
1327 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1329 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1334 GlobalFree( proxy );
1338 info->lpszProxy = proxy;
1339 info->lpszProxyBypass = proxy_bypass;
1342 got_from_reg = TRUE;
1344 info->dwAccessType =
1345 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1346 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1347 debugstr_w(info->lpszProxy),
1348 debugstr_w(info->lpszProxyBypass));
1357 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1359 char *colon, *http_proxy;
1361 if ((colon = strchr( envproxy, ':' )))
1363 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1365 static const char http[] = "http://";
1367 /* It's a scheme, check that it's http */
1368 if (!strncmp( envproxy, http, strlen( http ) ))
1369 http_proxy = envproxy + strlen( http );
1372 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1377 http_proxy = envproxy;
1380 http_proxy = envproxy;
1386 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1387 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1389 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1391 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1392 info->lpszProxy = http_proxyW;
1393 info->lpszProxyBypass = NULL;
1394 TRACE("http proxy (from environment) = %s\n",
1395 debugstr_w(info->lpszProxy));
1401 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1402 info->lpszProxy = NULL;
1403 info->lpszProxyBypass = NULL;
1408 /***********************************************************************
1409 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1411 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1413 static const WCHAR settingsW[] =
1414 {'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};
1416 struct connection_settings_header *hdr = NULL;
1417 DWORD type, offset, len, size = 0;
1420 TRACE("%p\n", config);
1424 set_last_error( ERROR_INVALID_PARAMETER );
1427 memset( config, 0, sizeof(*config) );
1428 config->fAutoDetect = TRUE;
1430 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1431 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1432 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1437 if (!(hdr = heap_alloc( size ))) goto done;
1438 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1439 hdr->magic != WININET_SETTINGS_MAGIC)
1445 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1446 offset = sizeof(*hdr);
1447 if (offset + sizeof(DWORD) > size) goto done;
1448 len = *(DWORD *)((char *)hdr + offset);
1449 offset += sizeof(DWORD);
1450 if (len && hdr->flags & PROXY_TYPE_PROXY)
1452 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1453 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1456 if (offset + sizeof(DWORD) > size) goto done;
1457 len = *(DWORD *)((char *)hdr + offset);
1458 offset += sizeof(DWORD);
1459 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1461 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1462 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1465 if (offset + sizeof(DWORD) > size) goto done;
1466 len = *(DWORD *)((char *)hdr + offset);
1467 offset += sizeof(DWORD);
1468 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1470 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1471 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1476 RegCloseKey( hkey );
1480 heap_free( config->lpszAutoConfigUrl );
1481 config->lpszAutoConfigUrl = NULL;
1482 heap_free( config->lpszProxy );
1483 config->lpszProxy = NULL;
1484 heap_free( config->lpszProxyBypass );
1485 config->lpszProxyBypass = NULL;
1490 static HRESULT WINAPI dispex_QueryInterface(
1491 IDispatchEx *iface, REFIID riid, void **ppv )
1495 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1496 IsEqualGUID( riid, &IID_IDispatch ) ||
1497 IsEqualGUID( riid, &IID_IDispatchEx ))
1500 return E_NOINTERFACE;
1505 static ULONG WINAPI dispex_AddRef(
1506 IDispatchEx *iface )
1511 static ULONG WINAPI dispex_Release(
1512 IDispatchEx *iface )
1517 static HRESULT WINAPI dispex_GetTypeInfoCount(
1518 IDispatchEx *iface, UINT *info )
1523 static HRESULT WINAPI dispex_GetTypeInfo(
1524 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1529 static HRESULT WINAPI dispex_GetIDsOfNames(
1530 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1535 static HRESULT WINAPI dispex_Invoke(
1536 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1537 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1542 static HRESULT WINAPI dispex_DeleteMemberByName(
1543 IDispatchEx *iface, BSTR name, DWORD flags )
1548 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1549 IDispatchEx *iface, DISPID id )
1554 static HRESULT WINAPI dispex_GetMemberProperties(
1555 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1560 static HRESULT WINAPI dispex_GetMemberName(
1561 IDispatchEx *iface, DISPID id, BSTR *name )
1566 static HRESULT WINAPI dispex_GetNextDispID(
1567 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1572 static HRESULT WINAPI dispex_GetNameSpaceParent(
1573 IDispatchEx *iface, IUnknown **unk )
1578 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1580 static HRESULT WINAPI dispex_GetDispID(
1581 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1583 if (!strcmpW( name, dns_resolveW ))
1585 *id = DISPID_GLOBAL_DNSRESOLVE;
1588 return DISP_E_UNKNOWNNAME;
1591 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1593 #ifdef HAVE_GETADDRINFO
1594 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1596 struct addrinfo *ai, *elem;
1601 hostnameA = strdupWA( hostname );
1603 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1605 if (!hostnameA) return E_OUTOFMEMORY;
1606 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1607 heap_free( hostnameA );
1608 if (res) return S_FALSE;
1611 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1617 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1619 V_VT( result ) = VT_BSTR;
1620 V_BSTR( result ) = SysAllocString( addr );
1623 FIXME("getaddrinfo not found at build time\n");
1628 static HRESULT WINAPI dispex_InvokeEx(
1629 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1630 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1632 if (id == DISPID_GLOBAL_DNSRESOLVE)
1634 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1635 if (V_VT(¶ms->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1636 return dns_resolve( V_BSTR(¶ms->rgvarg[0]), result );
1638 return DISP_E_MEMBERNOTFOUND;
1641 static const IDispatchExVtbl dispex_vtbl =
1643 dispex_QueryInterface,
1646 dispex_GetTypeInfoCount,
1648 dispex_GetIDsOfNames,
1652 dispex_DeleteMemberByName,
1653 dispex_DeleteMemberByDispID,
1654 dispex_GetMemberProperties,
1655 dispex_GetMemberName,
1656 dispex_GetNextDispID,
1657 dispex_GetNameSpaceParent
1660 static IDispatchEx global_dispex = { &dispex_vtbl };
1662 static HRESULT WINAPI site_QueryInterface(
1663 IActiveScriptSite *iface, REFIID riid, void **ppv )
1667 if (IsEqualGUID( &IID_IUnknown, riid ))
1669 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1672 return E_NOINTERFACE;
1674 IUnknown_AddRef( (IUnknown *)*ppv );
1678 static ULONG WINAPI site_AddRef(
1679 IActiveScriptSite *iface )
1684 static ULONG WINAPI site_Release(
1685 IActiveScriptSite *iface )
1690 static HRESULT WINAPI site_GetLCID(
1691 IActiveScriptSite *iface, LCID *lcid )
1696 static HRESULT WINAPI site_GetItemInfo(
1697 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1698 IUnknown **item, ITypeInfo **type_info )
1700 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1702 *item = (IUnknown *)&global_dispex;
1708 static HRESULT WINAPI site_GetDocVersionString(
1709 IActiveScriptSite *iface, BSTR *version )
1714 static HRESULT WINAPI site_OnScriptTerminate(
1715 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1720 static HRESULT WINAPI site_OnStateChange(
1721 IActiveScriptSite *iface, SCRIPTSTATE state )
1726 static HRESULT WINAPI site_OnScriptError(
1727 IActiveScriptSite *iface, IActiveScriptError *error )
1732 static HRESULT WINAPI site_OnEnterScript(
1733 IActiveScriptSite *iface )
1738 static HRESULT WINAPI site_OnLeaveScript(
1739 IActiveScriptSite *iface )
1744 static const IActiveScriptSiteVtbl site_vtbl =
1746 site_QueryInterface,
1751 site_GetDocVersionString,
1752 site_OnScriptTerminate,
1759 static IActiveScriptSite script_site = { &site_vtbl };
1761 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1763 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1768 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1769 info->lpszProxy = NULL;
1770 info->lpszProxyBypass = NULL;
1772 if (V_VT( &result ) != VT_BSTR) return TRUE;
1773 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1775 p = V_BSTR( &result );
1776 while (*p == ' ') p++;
1778 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1781 while (*p == ' ') p++;
1782 if (!*p || *p == ';') return TRUE;
1783 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1784 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1787 if (*q == ' ' || *q == ';')
1797 static BSTR include_pac_utils( BSTR script )
1799 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1800 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1807 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1808 size = SizeofResource( hmod, rsrc );
1809 data = LoadResource( hmod, rsrc );
1811 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1812 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1813 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1815 strcatW( ret, script );
1819 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1821 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1822 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1823 IActiveScriptParse *parser = NULL;
1824 IActiveScript *engine = NULL;
1825 IDispatch *dispatch = NULL;
1829 BSTR func = NULL, hostname = NULL, full_script = NULL;
1831 VARIANT args[2], result;
1835 memset( &uc, 0, sizeof(uc) );
1836 uc.dwStructSize = sizeof(uc);
1837 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1838 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1839 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1840 hostname[uc.dwHostNameLength] = 0;
1842 init = CoInitialize( NULL );
1843 hr = CLSIDFromProgID( jscriptW, &clsid );
1844 if (hr != S_OK) goto done;
1846 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1847 &IID_IActiveScript, (void **)&engine );
1848 if (hr != S_OK) goto done;
1850 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1851 if (hr != S_OK) goto done;
1853 hr = IActiveScriptParse64_InitNew( parser );
1854 if (hr != S_OK) goto done;
1856 hr = IActiveScript_SetScriptSite( engine, &script_site );
1857 if (hr != S_OK) goto done;
1859 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1860 if (hr != S_OK) goto done;
1862 if (!(full_script = include_pac_utils( script ))) goto done;
1864 hr = IActiveScriptParse64_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1865 if (hr != S_OK) goto done;
1867 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1868 if (hr != S_OK) goto done;
1870 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1871 if (hr != S_OK) goto done;
1873 if (!(func = SysAllocString( findproxyW ))) goto done;
1874 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1875 if (hr != S_OK) goto done;
1877 V_VT( &args[0] ) = VT_BSTR;
1878 V_BSTR( &args[0] ) = hostname;
1879 V_VT( &args[1] ) = VT_BSTR;
1880 V_BSTR( &args[1] ) = SysAllocString( url );
1882 params.rgvarg = args;
1883 params.rgdispidNamedArgs = NULL;
1885 params.cNamedArgs = 0;
1886 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1887 ¶ms, &result, NULL, NULL );
1888 VariantClear( &args[1] );
1891 WARN("script failed 0x%08x\n", hr);
1894 ret = parse_script_result( result, info );
1897 SysFreeString( full_script );
1898 SysFreeString( hostname );
1899 SysFreeString( func );
1900 if (dispatch) IDispatch_Release( dispatch );
1901 if (parser) IUnknown_Release( parser );
1902 if (engine) IActiveScript_Release( engine );
1903 if (SUCCEEDED( init )) CoUninitialize();
1904 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1908 static BSTR download_script( const WCHAR *url )
1910 static const WCHAR typeW[] = {'*','/','*',0};
1911 static const WCHAR *acceptW[] = {typeW, NULL};
1912 HINTERNET ses, con = NULL, req = NULL;
1915 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1916 char *tmp, *buffer = NULL;
1920 memset( &uc, 0, sizeof(uc) );
1921 uc.dwStructSize = sizeof(uc);
1922 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1923 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1924 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1925 hostname[uc.dwHostNameLength] = 0;
1927 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1928 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1929 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1930 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1931 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1932 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1934 if (!(buffer = heap_alloc( size ))) goto done;
1939 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1940 if (!bytes_read) break;
1941 to_read -= bytes_read;
1942 offset += bytes_read;
1947 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1951 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1952 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1953 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1957 WinHttpCloseHandle( req );
1958 WinHttpCloseHandle( con );
1959 WinHttpCloseHandle( ses );
1960 heap_free( buffer );
1961 heap_free( hostname );
1962 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1966 /***********************************************************************
1967 * WinHttpGetProxyForUrl (winhttp.@)
1969 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1970 WINHTTP_PROXY_INFO *info )
1972 WCHAR *detected_pac_url = NULL;
1973 const WCHAR *pac_url;
1978 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1980 if (!(session = (session_t *)grab_object( hsession )))
1982 set_last_error( ERROR_INVALID_HANDLE );
1985 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1987 release_object( &session->hdr );
1988 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1991 if (!url || !options || !info ||
1992 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1993 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1994 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1995 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1997 release_object( &session->hdr );
1998 set_last_error( ERROR_INVALID_PARAMETER );
2001 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2002 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2004 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
2007 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2008 else pac_url = detected_pac_url;
2010 if (!(script = download_script( pac_url ))) goto done;
2011 ret = run_script( script, url, info );
2012 SysFreeString( script );
2015 GlobalFree( detected_pac_url );
2016 release_object( &session->hdr );
2020 /***********************************************************************
2021 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2023 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2030 TRACE("%p\n", info);
2034 set_last_error( ERROR_INVALID_PARAMETER );
2037 switch (info->dwAccessType)
2039 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2041 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2042 if (!info->lpszProxy)
2044 set_last_error( ERROR_INVALID_PARAMETER );
2047 /* Only ASCII characters are allowed */
2048 for (src = info->lpszProxy; *src; src++)
2051 set_last_error( ERROR_INVALID_PARAMETER );
2054 if (info->lpszProxyBypass)
2056 for (src = info->lpszProxyBypass; *src; src++)
2059 set_last_error( ERROR_INVALID_PARAMETER );
2065 set_last_error( ERROR_INVALID_PARAMETER );
2069 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2070 KEY_WRITE, NULL, &key, NULL );
2073 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2076 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2078 size += strlenW( info->lpszProxy );
2079 if (info->lpszProxyBypass)
2080 size += strlenW( info->lpszProxyBypass );
2082 buf = heap_alloc( size );
2085 struct connection_settings_header *hdr =
2086 (struct connection_settings_header *)buf;
2087 DWORD *len = (DWORD *)(hdr + 1);
2089 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2091 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2095 hdr->flags = PROXY_TYPE_PROXY;
2096 *len++ = strlenW( info->lpszProxy );
2097 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2101 if (info->lpszProxyBypass)
2103 *len++ = strlenW( info->lpszProxyBypass );
2104 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2113 hdr->flags = PROXY_TYPE_DIRECT;
2117 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2127 /***********************************************************************
2128 * WinHttpSetStatusCallback (winhttp.@)
2130 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2131 DWORD flags, DWORD_PTR reserved )
2133 object_header_t *hdr;
2134 WINHTTP_STATUS_CALLBACK ret;
2136 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2138 if (!(hdr = grab_object( handle )))
2140 set_last_error( ERROR_INVALID_HANDLE );
2141 return WINHTTP_INVALID_STATUS_CALLBACK;
2143 ret = hdr->callback;
2144 hdr->callback = callback;
2145 hdr->notify_mask = flags;
2147 release_object( hdr );
2151 /***********************************************************************
2152 * WinHttpSetTimeouts (winhttp.@)
2154 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2157 object_header_t *hdr;
2161 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2163 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2165 set_last_error( ERROR_INVALID_PARAMETER );
2169 if (!(hdr = grab_object( handle )))
2171 set_last_error( ERROR_INVALID_HANDLE );
2177 case WINHTTP_HANDLE_TYPE_REQUEST:
2178 request = (request_t *)hdr;
2179 request->connect_timeout = connect;
2181 if (resolve < 0) resolve = 0;
2182 request->resolve_timeout = resolve;
2184 if (send < 0) send = 0;
2185 request->send_timeout = send;
2187 if (receive < 0) receive = 0;
2188 request->recv_timeout = receive;
2190 if (netconn_connected( &request->netconn ))
2192 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2193 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2196 release_object( &request->hdr );
2199 case WINHTTP_HANDLE_TYPE_SESSION:
2200 session = (session_t *)hdr;
2201 session->connect_timeout = connect;
2203 if (resolve < 0) resolve = 0;
2204 session->resolve_timeout = resolve;
2206 if (send < 0) send = 0;
2207 session->send_timeout = send;
2209 if (receive < 0) receive = 0;
2210 session->recv_timeout = receive;
2214 release_object( hdr );
2215 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2221 static const WCHAR wkday[7][4] =
2222 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2223 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2224 static const WCHAR month[12][4] =
2225 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2226 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2227 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2229 /***********************************************************************
2230 * WinHttpTimeFromSystemTime (WININET.@)
2232 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2234 static const WCHAR format[] =
2235 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2236 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2238 TRACE("%p, %p\n", time, string);
2240 if (!time || !string) return FALSE;
2242 sprintfW( string, format,
2243 wkday[time->wDayOfWeek],
2245 month[time->wMonth - 1],
2254 /***********************************************************************
2255 * WinHttpTimeToSystemTime (WININET.@)
2257 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2260 const WCHAR *s = string;
2263 TRACE("%s, %p\n", debugstr_w(string), time);
2265 if (!string || !time) return FALSE;
2267 /* Windows does this too */
2268 GetSystemTime( time );
2270 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2271 * a SYSTEMTIME structure.
2274 while (*s && !isalphaW( *s )) s++;
2275 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2276 time->wDayOfWeek = 7;
2278 for (i = 0; i < 7; i++)
2280 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2281 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2282 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2284 time->wDayOfWeek = i;
2289 if (time->wDayOfWeek > 6) return TRUE;
2290 while (*s && !isdigitW( *s )) s++;
2291 time->wDay = strtolW( s, &end, 10 );
2294 while (*s && !isalphaW( *s )) s++;
2295 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2298 for (i = 0; i < 12; i++)
2300 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2301 toupperW( month[i][1]) == toupperW( s[1] ) &&
2302 toupperW( month[i][2]) == toupperW( s[2] ) )
2304 time->wMonth = i + 1;
2308 if (time->wMonth == 0) return TRUE;
2310 while (*s && !isdigitW( *s )) s++;
2311 if (*s == '\0') return TRUE;
2312 time->wYear = strtolW( s, &end, 10 );
2315 while (*s && !isdigitW( *s )) s++;
2316 if (*s == '\0') return TRUE;
2317 time->wHour = strtolW( s, &end, 10 );
2320 while (*s && !isdigitW( *s )) s++;
2321 if (*s == '\0') return TRUE;
2322 time->wMinute = strtolW( s, &end, 10 );
2325 while (*s && !isdigitW( *s )) s++;
2326 if (*s == '\0') return TRUE;
2327 time->wSecond = strtolW( s, &end, 10 );
2329 time->wMilliseconds = 0;