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;
185 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
186 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD *)buffer);
189 FIXME("unimplemented option %u\n", option);
190 set_last_error( ERROR_INVALID_PARAMETER );
195 static const object_vtbl_t session_vtbl =
198 session_query_option,
202 /***********************************************************************
203 * WinHttpOpen (winhttp.@)
205 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
208 HINTERNET handle = NULL;
210 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
212 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
214 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
215 session->hdr.vtbl = &session_vtbl;
216 session->hdr.flags = flags;
217 session->hdr.refs = 1;
218 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
219 list_init( &session->hdr.children );
220 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
221 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
222 session->send_timeout = DEFAULT_SEND_TIMEOUT;
223 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
224 list_init( &session->cookie_cache );
226 if (agent && !(session->agent = strdupW( agent ))) goto end;
227 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
229 WINHTTP_PROXY_INFO info;
231 WinHttpGetDefaultProxyConfiguration( &info );
232 session->access = info.dwAccessType;
233 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
235 GlobalFree( info.lpszProxy );
236 GlobalFree( info.lpszProxyBypass );
239 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
241 GlobalFree( info.lpszProxy );
242 GlobalFree( info.lpszProxyBypass );
246 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
248 session->access = access;
249 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
250 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
253 if (!(handle = alloc_handle( &session->hdr ))) goto end;
254 session->hdr.handle = handle;
257 release_object( &session->hdr );
258 TRACE("returning %p\n", handle);
262 /***********************************************************************
263 * connect_destroy (internal)
265 static void connect_destroy( object_header_t *hdr )
267 connect_t *connect = (connect_t *)hdr;
269 TRACE("%p\n", connect);
271 release_object( &connect->session->hdr );
273 heap_free( connect->hostname );
274 heap_free( connect->servername );
275 heap_free( connect->username );
276 heap_free( connect->password );
277 heap_free( connect );
280 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
282 connect_t *connect = (connect_t *)hdr;
286 case WINHTTP_OPTION_PARENT_HANDLE:
288 if (!buffer || *buflen < sizeof(HINTERNET))
290 *buflen = sizeof(HINTERNET);
291 set_last_error( ERROR_INSUFFICIENT_BUFFER );
295 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
296 *buflen = sizeof(HINTERNET);
299 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
300 *(DWORD *)buffer = connect->session->resolve_timeout;
301 *buflen = sizeof(DWORD);
303 case WINHTTP_OPTION_CONNECT_TIMEOUT:
304 *(DWORD *)buffer = connect->session->connect_timeout;
305 *buflen = sizeof(DWORD);
307 case WINHTTP_OPTION_SEND_TIMEOUT:
308 *(DWORD *)buffer = connect->session->send_timeout;
309 *buflen = sizeof(DWORD);
311 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
312 *(DWORD *)buffer = connect->session->recv_timeout;
313 *buflen = sizeof(DWORD);
316 FIXME("unimplemented option %u\n", option);
317 set_last_error( ERROR_INVALID_PARAMETER );
322 static const object_vtbl_t connect_vtbl =
325 connect_query_option,
329 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
331 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
334 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
336 else if (*domain == '*')
338 if (domain[1] == '.')
342 /* For a hostname to match a wildcard, the last domain must match
343 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
344 * hostname is www.foo.a.b, it matches, but a.b does not.
346 dot = strchrW( server, '.' );
349 int len = strlenW( dot + 1 );
351 if (len > strlenW( domain + 2 ))
355 /* The server's domain is longer than the wildcard, so it
356 * could be a subdomain. Compare the last portion of the
359 ptr = dot + len + 1 - strlenW( domain + 2 );
360 if (!strcmpiW( ptr, domain + 2 ))
362 /* This is only a match if the preceding character is
363 * a '.', i.e. that it is a matching domain. E.g.
364 * if domain is '*.b.c' and server is 'www.ab.c' they
367 ret = *(ptr - 1) == '.';
371 ret = !strcmpiW( dot + 1, domain + 2 );
376 ret = !strcmpiW( server, domain );
380 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
381 #define MAX_HOST_NAME_LENGTH 256
383 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
388 if (!session->proxy_bypass) return FALSE;
389 ptr = session->proxy_bypass;
393 ptr = strchrW( ptr, ';' );
395 ptr = strchrW( tmp, ' ' );
398 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
400 WCHAR domain[MAX_HOST_NAME_LENGTH];
402 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
403 domain[ptr - tmp] = 0;
404 ret = domain_matches( server, domain );
409 ret = domain_matches( server, tmp );
410 } while (ptr && !ret);
414 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
416 session_t *session = connect->session;
419 if (session->proxy_server && !should_bypass_proxy(session, server))
423 if ((colon = strchrW( session->proxy_server, ':' )))
425 if (!connect->servername || strncmpiW( connect->servername,
426 session->proxy_server, colon - session->proxy_server - 1 ))
428 heap_free( connect->servername );
429 connect->resolved = FALSE;
430 if (!(connect->servername = heap_alloc(
431 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
436 memcpy( connect->servername, session->proxy_server,
437 (colon - session->proxy_server) * sizeof(WCHAR) );
438 connect->servername[colon - session->proxy_server] = 0;
440 connect->serverport = atoiW( colon + 1 );
442 connect->serverport = INTERNET_DEFAULT_PORT;
447 if (!connect->servername || strcmpiW( connect->servername,
448 session->proxy_server ))
450 heap_free( connect->servername );
451 connect->resolved = FALSE;
452 if (!(connect->servername = strdupW( session->proxy_server )))
457 connect->serverport = INTERNET_DEFAULT_PORT;
463 heap_free( connect->servername );
464 connect->resolved = FALSE;
465 if (!(connect->servername = strdupW( server )))
470 connect->serverport = port;
476 /***********************************************************************
477 * WinHttpConnect (winhttp.@)
479 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
483 HINTERNET hconnect = NULL;
485 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
489 set_last_error( ERROR_INVALID_PARAMETER );
492 if (!(session = (session_t *)grab_object( hsession )))
494 set_last_error( ERROR_INVALID_HANDLE );
497 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
499 release_object( &session->hdr );
500 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
503 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
505 release_object( &session->hdr );
508 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
509 connect->hdr.vtbl = &connect_vtbl;
510 connect->hdr.refs = 1;
511 connect->hdr.flags = session->hdr.flags;
512 connect->hdr.callback = session->hdr.callback;
513 connect->hdr.notify_mask = session->hdr.notify_mask;
514 connect->hdr.context = session->hdr.context;
515 connect->hdr.redirect_policy = session->hdr.redirect_policy;
516 list_init( &connect->hdr.children );
518 addref_object( &session->hdr );
519 connect->session = session;
520 list_add_head( &session->hdr.children, &connect->hdr.entry );
522 if (!(connect->hostname = strdupW( server ))) goto end;
523 connect->hostport = port;
524 if (!set_server_for_hostname( connect, server, port )) goto end;
526 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
527 connect->hdr.handle = hconnect;
529 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
532 release_object( &connect->hdr );
533 release_object( &session->hdr );
534 TRACE("returning %p\n", hconnect);
538 /***********************************************************************
539 * request_destroy (internal)
541 static void request_destroy( object_header_t *hdr )
543 request_t *request = (request_t *)hdr;
546 TRACE("%p\n", request);
548 release_object( &request->connect->hdr );
550 heap_free( request->verb );
551 heap_free( request->path );
552 heap_free( request->version );
553 heap_free( request->raw_headers );
554 heap_free( request->status_text );
555 for (i = 0; i < request->num_headers; i++)
557 heap_free( request->headers[i].field );
558 heap_free( request->headers[i].value );
560 heap_free( request->headers );
561 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
562 heap_free( request->accept_types );
563 heap_free( request );
566 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
569 if (str) len = strlenW( str );
570 if (buffer && *buflen > len)
572 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
575 *buflen = len * sizeof(WCHAR);
578 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
581 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
583 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
584 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
585 CertNameToStrW( encoding, blob, format, ret, size );
590 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
592 request_t *request = (request_t *)hdr;
596 case WINHTTP_OPTION_SECURITY_FLAGS:
601 if (!buffer || *buflen < sizeof(flags))
603 *buflen = sizeof(flags);
604 set_last_error( ERROR_INSUFFICIENT_BUFFER );
609 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
610 flags |= request->netconn.security_flags;
611 bits = netconn_get_cipher_strength( &request->netconn );
613 flags |= SECURITY_FLAG_STRENGTH_STRONG;
615 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
617 flags |= SECURITY_FLAG_STRENGTH_WEAK;
618 *(DWORD *)buffer = flags;
619 *buflen = sizeof(flags);
622 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
624 const CERT_CONTEXT *cert;
626 if (!buffer || *buflen < sizeof(cert))
628 *buflen = sizeof(cert);
629 set_last_error( ERROR_INSUFFICIENT_BUFFER );
633 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
634 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
635 *buflen = sizeof(cert);
638 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
640 const CERT_CONTEXT *cert;
641 const CRYPT_OID_INFO *oidInfo;
642 WINHTTP_CERTIFICATE_INFO *ci = buffer;
644 FIXME("partial stub\n");
646 if (!buffer || *buflen < sizeof(*ci))
648 *buflen = sizeof(*ci);
649 set_last_error( ERROR_INSUFFICIENT_BUFFER );
652 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
654 ci->ftExpiry = cert->pCertInfo->NotAfter;
655 ci->ftStart = cert->pCertInfo->NotBefore;
656 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
657 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
658 ci->lpszProtocolName = NULL;
659 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
660 cert->pCertInfo->SignatureAlgorithm.pszObjId,
663 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
665 ci->lpszSignatureAlgName = NULL;
666 ci->lpszEncryptionAlgName = NULL;
667 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
669 CertFreeCertificateContext( cert );
670 *buflen = sizeof(*ci);
673 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
675 if (!buffer || *buflen < sizeof(DWORD))
677 *buflen = sizeof(DWORD);
678 set_last_error( ERROR_INSUFFICIENT_BUFFER );
682 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
683 *buflen = sizeof(DWORD);
686 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
687 *(DWORD *)buffer = request->resolve_timeout;
688 *buflen = sizeof(DWORD);
690 case WINHTTP_OPTION_CONNECT_TIMEOUT:
691 *(DWORD *)buffer = request->connect_timeout;
692 *buflen = sizeof(DWORD);
694 case WINHTTP_OPTION_SEND_TIMEOUT:
695 *(DWORD *)buffer = request->send_timeout;
696 *buflen = sizeof(DWORD);
698 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
699 *(DWORD *)buffer = request->recv_timeout;
700 *buflen = sizeof(DWORD);
703 case WINHTTP_OPTION_USERNAME:
704 str_to_buffer( buffer, request->connect->username, buflen );
707 case WINHTTP_OPTION_PASSWORD:
708 str_to_buffer( buffer, request->connect->password, buflen );
711 case WINHTTP_OPTION_PROXY_USERNAME:
712 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
715 case WINHTTP_OPTION_PROXY_PASSWORD:
716 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
720 FIXME("unimplemented option %u\n", option);
721 set_last_error( ERROR_INVALID_PARAMETER );
726 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
729 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
731 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
735 set_last_error( ERROR_OUTOFMEMORY );
739 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
741 request_t *request = (request_t *)hdr;
745 case WINHTTP_OPTION_PROXY:
747 WINHTTP_PROXY_INFO *pi = buffer;
749 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
752 case WINHTTP_OPTION_DISABLE_FEATURE:
756 if (buflen != sizeof(DWORD))
758 set_last_error( ERROR_INSUFFICIENT_BUFFER );
762 disable = *(DWORD *)buffer;
763 TRACE("0x%x\n", disable);
764 hdr->disable_flags |= disable;
767 case WINHTTP_OPTION_AUTOLOGON_POLICY:
771 if (buflen != sizeof(DWORD))
773 set_last_error( ERROR_INSUFFICIENT_BUFFER );
777 policy = *(DWORD *)buffer;
778 TRACE("0x%x\n", policy);
779 hdr->logon_policy = policy;
782 case WINHTTP_OPTION_REDIRECT_POLICY:
786 if (buflen != sizeof(DWORD))
788 set_last_error( ERROR_INSUFFICIENT_BUFFER );
792 policy = *(DWORD *)buffer;
793 TRACE("0x%x\n", policy);
794 hdr->redirect_policy = policy;
797 case WINHTTP_OPTION_SECURITY_FLAGS:
801 if (buflen < sizeof(DWORD))
803 set_last_error( ERROR_INSUFFICIENT_BUFFER );
806 flags = *(DWORD *)buffer;
807 TRACE("0x%x\n", flags);
808 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
809 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
810 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
811 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
813 set_last_error( ERROR_INVALID_PARAMETER );
816 request->netconn.security_flags = flags;
819 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
820 request->resolve_timeout = *(DWORD *)buffer;
822 case WINHTTP_OPTION_CONNECT_TIMEOUT:
823 request->connect_timeout = *(DWORD *)buffer;
825 case WINHTTP_OPTION_SEND_TIMEOUT:
826 request->send_timeout = *(DWORD *)buffer;
828 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
829 request->recv_timeout = *(DWORD *)buffer;
832 case WINHTTP_OPTION_USERNAME:
834 connect_t *connect = request->connect;
836 heap_free( connect->username );
837 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
840 case WINHTTP_OPTION_PASSWORD:
842 connect_t *connect = request->connect;
844 heap_free( connect->password );
845 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
848 case WINHTTP_OPTION_PROXY_USERNAME:
850 session_t *session = request->connect->session;
852 heap_free( session->proxy_username );
853 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
856 case WINHTTP_OPTION_PROXY_PASSWORD:
858 session_t *session = request->connect->session;
860 heap_free( session->proxy_password );
861 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
865 FIXME("unimplemented option %u\n", option);
866 set_last_error( ERROR_INVALID_PARAMETER );
871 static const object_vtbl_t request_vtbl =
874 request_query_option,
878 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
880 const WCHAR **types = accept_types;
883 if (!types) return TRUE;
886 request->num_accept_types++;
889 if (!request->num_accept_types) return TRUE;
890 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
892 request->num_accept_types = 0;
895 types = accept_types;
896 for (i = 0; i < request->num_accept_types; i++)
898 if (!(request->accept_types[i] = strdupW( *types )))
900 for (; i >= 0; i--) heap_free( request->accept_types[i] );
901 heap_free( request->accept_types );
902 request->accept_types = NULL;
903 request->num_accept_types = 0;
911 /***********************************************************************
912 * WinHttpOpenRequest (winhttp.@)
914 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
915 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
919 HINTERNET hrequest = NULL;
921 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
922 debugstr_w(version), debugstr_w(referrer), types, flags);
924 if(types && TRACE_ON(winhttp)) {
927 TRACE("accept types:\n");
928 for(iter = types; *iter; iter++)
929 TRACE(" %s\n", debugstr_w(*iter));
932 if (!(connect = (connect_t *)grab_object( hconnect )))
934 set_last_error( ERROR_INVALID_HANDLE );
937 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
939 release_object( &connect->hdr );
940 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
943 if (!(request = heap_alloc_zero( sizeof(request_t) )))
945 release_object( &connect->hdr );
948 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
949 request->hdr.vtbl = &request_vtbl;
950 request->hdr.refs = 1;
951 request->hdr.flags = flags;
952 request->hdr.callback = connect->hdr.callback;
953 request->hdr.notify_mask = connect->hdr.notify_mask;
954 request->hdr.context = connect->hdr.context;
955 request->hdr.redirect_policy = connect->hdr.redirect_policy;
956 list_init( &request->hdr.children );
958 addref_object( &connect->hdr );
959 request->connect = connect;
960 list_add_head( &connect->hdr.children, &request->hdr.entry );
962 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
963 request->resolve_timeout = connect->session->resolve_timeout;
964 request->connect_timeout = connect->session->connect_timeout;
965 request->send_timeout = connect->session->send_timeout;
966 request->recv_timeout = connect->session->recv_timeout;
968 if (!verb || !verb[0]) verb = getW;
969 if (!(request->verb = strdupW( verb ))) goto end;
976 len = strlenW( object ) + 1;
977 if (object[0] != '/') len++;
978 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
980 if (object[0] != '/') *p++ = '/';
981 strcpyW( p, object );
982 request->path = path;
984 else if (!(request->path = strdupW( slashW ))) goto end;
986 if (!version || !version[0]) version = http1_1;
987 if (!(request->version = strdupW( version ))) goto end;
988 if (!(store_accept_types( request, types ))) goto end;
990 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
991 request->hdr.handle = hrequest;
993 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
996 release_object( &request->hdr );
997 release_object( &connect->hdr );
998 TRACE("returning %p\n", hrequest);
1002 /***********************************************************************
1003 * WinHttpCloseHandle (winhttp.@)
1005 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1007 object_header_t *hdr;
1009 TRACE("%p\n", handle);
1011 if (!(hdr = grab_object( handle )))
1013 set_last_error( ERROR_INVALID_HANDLE );
1016 release_object( hdr );
1017 free_handle( handle );
1021 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1027 set_last_error( ERROR_INVALID_PARAMETER );
1033 case WINHTTP_OPTION_CONTEXT_VALUE:
1035 if (!buffer || *buflen < sizeof(DWORD_PTR))
1037 *buflen = sizeof(DWORD_PTR);
1038 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1042 *(DWORD_PTR *)buffer = hdr->context;
1043 *buflen = sizeof(DWORD_PTR);
1047 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1050 FIXME("unimplemented option %u\n", option);
1051 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1059 /***********************************************************************
1060 * WinHttpQueryOption (winhttp.@)
1062 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1065 object_header_t *hdr;
1067 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1069 if (!(hdr = grab_object( handle )))
1071 set_last_error( ERROR_INVALID_HANDLE );
1075 ret = query_option( hdr, option, buffer, buflen );
1077 release_object( hdr );
1081 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1087 set_last_error( ERROR_INVALID_PARAMETER );
1093 case WINHTTP_OPTION_CONTEXT_VALUE:
1095 if (buflen != sizeof(DWORD_PTR))
1097 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1101 hdr->context = *(DWORD_PTR *)buffer;
1105 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1108 FIXME("unimplemented option %u\n", option);
1109 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1117 /***********************************************************************
1118 * WinHttpSetOption (winhttp.@)
1120 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1123 object_header_t *hdr;
1125 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1127 if (!(hdr = grab_object( handle )))
1129 set_last_error( ERROR_INVALID_HANDLE );
1133 ret = set_option( hdr, option, buffer, buflen );
1135 release_object( hdr );
1139 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1144 GetComputerNameExA( format, NULL, &size );
1145 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1146 if (!(ret = heap_alloc( size ))) return NULL;
1147 if (!GetComputerNameExA( format, ret, &size ))
1155 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1157 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1159 if (len_suffix > len_domain) return FALSE;
1160 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1164 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1167 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1168 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1169 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1170 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1173 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1176 #ifdef HAVE_GETNAMEINFO
1177 ret = getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1182 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1184 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1185 static const WCHAR wpadW[] = {'/','w','p','a','d','.','d','a','t',0};
1186 char name[NI_MAXHOST];
1190 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1191 if (!ai) return NULL;
1193 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1195 len = strlenW( httpW ) + strlen( hostname ) + strlenW( wpadW );
1196 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1197 strcpyW( p, httpW );
1198 p += strlenW( httpW );
1199 while (*hostname) { *p++ = *hostname++; }
1200 strcpyW( p, wpadW );
1204 /***********************************************************************
1205 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1207 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1211 TRACE("0x%08x, %p\n", flags, url);
1215 set_last_error( ERROR_INVALID_PARAMETER );
1218 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1220 static int fixme_shown;
1221 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1223 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1225 #ifdef HAVE_GETADDRINFO
1226 char *fqdn, *domain, *p;
1228 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1229 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1235 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1237 struct addrinfo *ai;
1241 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1244 heap_free( domain );
1247 strcpy( name, "wpad" );
1249 res = getaddrinfo( name, NULL, NULL, &ai );
1253 *url = build_wpad_url( name, ai );
1257 TRACE("returning %s\n", debugstr_w(*url));
1264 heap_free( domain );
1267 FIXME("getaddrinfo not found at build time\n");
1270 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1274 static const WCHAR Connections[] = {
1275 'S','o','f','t','w','a','r','e','\\',
1276 'M','i','c','r','o','s','o','f','t','\\',
1277 'W','i','n','d','o','w','s','\\',
1278 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1279 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1280 'C','o','n','n','e','c','t','i','o','n','s',0 };
1281 static const WCHAR WinHttpSettings[] = {
1282 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1283 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1284 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1285 static const DWORD PROXY_TYPE_DIRECT = 1;
1286 static const DWORD PROXY_TYPE_PROXY = 2;
1287 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1288 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1290 struct connection_settings_header
1293 DWORD unknown; /* always zero? */
1294 DWORD flags; /* one or more of PROXY_* */
1297 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1301 for (begin = src; src - begin < len; src++, dst++)
1306 /***********************************************************************
1307 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1309 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1313 BOOL got_from_reg = FALSE, direct = TRUE;
1316 TRACE("%p\n", info);
1318 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1321 DWORD type, size = 0;
1323 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1324 if (!l && type == REG_BINARY &&
1325 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1327 BYTE *buf = heap_alloc( size );
1331 struct connection_settings_header *hdr =
1332 (struct connection_settings_header *)buf;
1333 DWORD *len = (DWORD *)(hdr + 1);
1335 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1337 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1340 if (hdr->flags & PROXY_TYPE_PROXY)
1343 LPWSTR proxy = NULL;
1344 LPWSTR proxy_bypass = NULL;
1346 /* Sanity-check length of proxy string */
1347 if ((BYTE *)len - buf + *len <= size)
1350 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1352 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1353 len = (DWORD *)((BYTE *)(len + 1) + *len);
1357 /* Sanity-check length of proxy bypass string */
1358 if ((BYTE *)len - buf + *len <= size)
1360 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1362 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1367 GlobalFree( proxy );
1371 info->lpszProxy = proxy;
1372 info->lpszProxyBypass = proxy_bypass;
1375 got_from_reg = TRUE;
1377 info->dwAccessType =
1378 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1379 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1380 debugstr_w(info->lpszProxy),
1381 debugstr_w(info->lpszProxyBypass));
1390 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1392 char *colon, *http_proxy;
1394 if ((colon = strchr( envproxy, ':' )))
1396 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1398 static const char http[] = "http://";
1400 /* It's a scheme, check that it's http */
1401 if (!strncmp( envproxy, http, strlen( http ) ))
1402 http_proxy = envproxy + strlen( http );
1405 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1410 http_proxy = envproxy;
1413 http_proxy = envproxy;
1419 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1420 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1422 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1424 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1425 info->lpszProxy = http_proxyW;
1426 info->lpszProxyBypass = NULL;
1427 TRACE("http proxy (from environment) = %s\n",
1428 debugstr_w(info->lpszProxy));
1434 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1435 info->lpszProxy = NULL;
1436 info->lpszProxyBypass = NULL;
1441 /***********************************************************************
1442 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1444 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1446 static const WCHAR settingsW[] =
1447 {'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};
1449 struct connection_settings_header *hdr = NULL;
1450 DWORD type, offset, len, size = 0;
1453 TRACE("%p\n", config);
1457 set_last_error( ERROR_INVALID_PARAMETER );
1460 memset( config, 0, sizeof(*config) );
1461 config->fAutoDetect = TRUE;
1463 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1464 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1465 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1470 if (!(hdr = heap_alloc( size ))) goto done;
1471 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1472 hdr->magic != WININET_SETTINGS_MAGIC)
1478 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1479 offset = sizeof(*hdr);
1480 if (offset + sizeof(DWORD) > size) goto done;
1481 len = *(DWORD *)((char *)hdr + offset);
1482 offset += sizeof(DWORD);
1483 if (len && hdr->flags & PROXY_TYPE_PROXY)
1485 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1486 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1489 if (offset + sizeof(DWORD) > size) goto done;
1490 len = *(DWORD *)((char *)hdr + offset);
1491 offset += sizeof(DWORD);
1492 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1494 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1495 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1498 if (offset + sizeof(DWORD) > size) goto done;
1499 len = *(DWORD *)((char *)hdr + offset);
1500 offset += sizeof(DWORD);
1501 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1503 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1504 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1509 RegCloseKey( hkey );
1513 heap_free( config->lpszAutoConfigUrl );
1514 config->lpszAutoConfigUrl = NULL;
1515 heap_free( config->lpszProxy );
1516 config->lpszProxy = NULL;
1517 heap_free( config->lpszProxyBypass );
1518 config->lpszProxyBypass = NULL;
1523 static HRESULT WINAPI dispex_QueryInterface(
1524 IDispatchEx *iface, REFIID riid, void **ppv )
1528 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1529 IsEqualGUID( riid, &IID_IDispatch ) ||
1530 IsEqualGUID( riid, &IID_IDispatchEx ))
1533 return E_NOINTERFACE;
1538 static ULONG WINAPI dispex_AddRef(
1539 IDispatchEx *iface )
1544 static ULONG WINAPI dispex_Release(
1545 IDispatchEx *iface )
1550 static HRESULT WINAPI dispex_GetTypeInfoCount(
1551 IDispatchEx *iface, UINT *info )
1556 static HRESULT WINAPI dispex_GetTypeInfo(
1557 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1562 static HRESULT WINAPI dispex_GetIDsOfNames(
1563 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1568 static HRESULT WINAPI dispex_Invoke(
1569 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1570 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1575 static HRESULT WINAPI dispex_DeleteMemberByName(
1576 IDispatchEx *iface, BSTR name, DWORD flags )
1581 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1582 IDispatchEx *iface, DISPID id )
1587 static HRESULT WINAPI dispex_GetMemberProperties(
1588 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1593 static HRESULT WINAPI dispex_GetMemberName(
1594 IDispatchEx *iface, DISPID id, BSTR *name )
1599 static HRESULT WINAPI dispex_GetNextDispID(
1600 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1605 static HRESULT WINAPI dispex_GetNameSpaceParent(
1606 IDispatchEx *iface, IUnknown **unk )
1611 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1613 static HRESULT WINAPI dispex_GetDispID(
1614 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1616 if (!strcmpW( name, dns_resolveW ))
1618 *id = DISPID_GLOBAL_DNSRESOLVE;
1621 return DISP_E_UNKNOWNNAME;
1624 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1626 #ifdef HAVE_GETADDRINFO
1627 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1629 struct addrinfo *ai, *elem;
1634 hostnameA = strdupWA( hostname );
1636 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1638 if (!hostnameA) return E_OUTOFMEMORY;
1639 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1640 heap_free( hostnameA );
1641 if (res) return S_FALSE;
1644 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1650 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1652 V_VT( result ) = VT_BSTR;
1653 V_BSTR( result ) = SysAllocString( addr );
1656 FIXME("getaddrinfo not found at build time\n");
1661 static HRESULT WINAPI dispex_InvokeEx(
1662 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1663 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1665 if (id == DISPID_GLOBAL_DNSRESOLVE)
1667 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1668 if (V_VT(¶ms->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1669 return dns_resolve( V_BSTR(¶ms->rgvarg[0]), result );
1671 return DISP_E_MEMBERNOTFOUND;
1674 static const IDispatchExVtbl dispex_vtbl =
1676 dispex_QueryInterface,
1679 dispex_GetTypeInfoCount,
1681 dispex_GetIDsOfNames,
1685 dispex_DeleteMemberByName,
1686 dispex_DeleteMemberByDispID,
1687 dispex_GetMemberProperties,
1688 dispex_GetMemberName,
1689 dispex_GetNextDispID,
1690 dispex_GetNameSpaceParent
1693 static IDispatchEx global_dispex = { &dispex_vtbl };
1695 static HRESULT WINAPI site_QueryInterface(
1696 IActiveScriptSite *iface, REFIID riid, void **ppv )
1700 if (IsEqualGUID( &IID_IUnknown, riid ))
1702 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1705 return E_NOINTERFACE;
1707 IUnknown_AddRef( (IUnknown *)*ppv );
1711 static ULONG WINAPI site_AddRef(
1712 IActiveScriptSite *iface )
1717 static ULONG WINAPI site_Release(
1718 IActiveScriptSite *iface )
1723 static HRESULT WINAPI site_GetLCID(
1724 IActiveScriptSite *iface, LCID *lcid )
1729 static HRESULT WINAPI site_GetItemInfo(
1730 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1731 IUnknown **item, ITypeInfo **type_info )
1733 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1735 *item = (IUnknown *)&global_dispex;
1741 static HRESULT WINAPI site_GetDocVersionString(
1742 IActiveScriptSite *iface, BSTR *version )
1747 static HRESULT WINAPI site_OnScriptTerminate(
1748 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1753 static HRESULT WINAPI site_OnStateChange(
1754 IActiveScriptSite *iface, SCRIPTSTATE state )
1759 static HRESULT WINAPI site_OnScriptError(
1760 IActiveScriptSite *iface, IActiveScriptError *error )
1765 static HRESULT WINAPI site_OnEnterScript(
1766 IActiveScriptSite *iface )
1771 static HRESULT WINAPI site_OnLeaveScript(
1772 IActiveScriptSite *iface )
1777 static const IActiveScriptSiteVtbl site_vtbl =
1779 site_QueryInterface,
1784 site_GetDocVersionString,
1785 site_OnScriptTerminate,
1792 static IActiveScriptSite script_site = { &site_vtbl };
1794 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1796 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1801 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1802 info->lpszProxy = NULL;
1803 info->lpszProxyBypass = NULL;
1805 if (V_VT( &result ) != VT_BSTR) return TRUE;
1806 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1808 p = V_BSTR( &result );
1809 while (*p == ' ') p++;
1811 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1814 while (*p == ' ') p++;
1815 if (!*p || *p == ';') return TRUE;
1816 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1817 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1820 if (*q == ' ' || *q == ';')
1830 static BSTR include_pac_utils( BSTR script )
1832 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1833 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1840 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1841 size = SizeofResource( hmod, rsrc );
1842 data = LoadResource( hmod, rsrc );
1844 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1845 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1846 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1848 strcatW( ret, script );
1853 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1854 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1855 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1857 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1858 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1859 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1862 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1864 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1865 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1866 IActiveScriptParse *parser = NULL;
1867 IActiveScript *engine = NULL;
1868 IDispatch *dispatch = NULL;
1872 BSTR func = NULL, hostname = NULL, full_script = NULL;
1874 VARIANT args[2], result;
1878 memset( &uc, 0, sizeof(uc) );
1879 uc.dwStructSize = sizeof(uc);
1880 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1881 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1882 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1883 hostname[uc.dwHostNameLength] = 0;
1885 init = CoInitialize( NULL );
1886 hr = CLSIDFromProgID( jscriptW, &clsid );
1887 if (hr != S_OK) goto done;
1889 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1890 &IID_IActiveScript, (void **)&engine );
1891 if (hr != S_OK) goto done;
1893 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1894 if (hr != S_OK) goto done;
1896 hr = IActiveScriptParse_InitNew( parser );
1897 if (hr != S_OK) goto done;
1899 hr = IActiveScript_SetScriptSite( engine, &script_site );
1900 if (hr != S_OK) goto done;
1902 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1903 if (hr != S_OK) goto done;
1905 if (!(full_script = include_pac_utils( script ))) goto done;
1907 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1908 if (hr != S_OK) goto done;
1910 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1911 if (hr != S_OK) goto done;
1913 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1914 if (hr != S_OK) goto done;
1916 if (!(func = SysAllocString( findproxyW ))) goto done;
1917 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1918 if (hr != S_OK) goto done;
1920 V_VT( &args[0] ) = VT_BSTR;
1921 V_BSTR( &args[0] ) = hostname;
1922 V_VT( &args[1] ) = VT_BSTR;
1923 V_BSTR( &args[1] ) = SysAllocString( url );
1925 params.rgvarg = args;
1926 params.rgdispidNamedArgs = NULL;
1928 params.cNamedArgs = 0;
1929 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1930 ¶ms, &result, NULL, NULL );
1931 VariantClear( &args[1] );
1934 WARN("script failed 0x%08x\n", hr);
1937 ret = parse_script_result( result, info );
1940 SysFreeString( full_script );
1941 SysFreeString( hostname );
1942 SysFreeString( func );
1943 if (dispatch) IDispatch_Release( dispatch );
1944 if (parser) IActiveScriptParse_Release( parser );
1945 if (engine) IActiveScript_Release( engine );
1946 if (SUCCEEDED( init )) CoUninitialize();
1947 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1951 static BSTR download_script( const WCHAR *url )
1953 static const WCHAR typeW[] = {'*','/','*',0};
1954 static const WCHAR *acceptW[] = {typeW, NULL};
1955 HINTERNET ses, con = NULL, req = NULL;
1958 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1959 char *tmp, *buffer = NULL;
1963 memset( &uc, 0, sizeof(uc) );
1964 uc.dwStructSize = sizeof(uc);
1965 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1966 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1967 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1968 hostname[uc.dwHostNameLength] = 0;
1970 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1971 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1972 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1973 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1974 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1976 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1977 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1978 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1981 if (!(buffer = heap_alloc( size ))) goto done;
1986 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1987 if (!bytes_read) break;
1988 to_read -= bytes_read;
1989 offset += bytes_read;
1994 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1998 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1999 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
2000 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
2004 WinHttpCloseHandle( req );
2005 WinHttpCloseHandle( con );
2006 WinHttpCloseHandle( ses );
2007 heap_free( buffer );
2008 heap_free( hostname );
2009 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
2013 /***********************************************************************
2014 * WinHttpGetProxyForUrl (winhttp.@)
2016 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
2017 WINHTTP_PROXY_INFO *info )
2019 WCHAR *detected_pac_url = NULL;
2020 const WCHAR *pac_url;
2025 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
2027 if (!(session = (session_t *)grab_object( hsession )))
2029 set_last_error( ERROR_INVALID_HANDLE );
2032 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
2034 release_object( &session->hdr );
2035 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2038 if (!url || !options || !info ||
2039 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
2040 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
2041 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
2042 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
2044 release_object( &session->hdr );
2045 set_last_error( ERROR_INVALID_PARAMETER );
2048 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2049 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2052 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2053 else pac_url = detected_pac_url;
2055 if (!(script = download_script( pac_url ))) goto done;
2056 ret = run_script( script, url, info );
2057 SysFreeString( script );
2060 GlobalFree( detected_pac_url );
2061 release_object( &session->hdr );
2065 /***********************************************************************
2066 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2068 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2075 TRACE("%p\n", info);
2079 set_last_error( ERROR_INVALID_PARAMETER );
2082 switch (info->dwAccessType)
2084 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2086 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2087 if (!info->lpszProxy)
2089 set_last_error( ERROR_INVALID_PARAMETER );
2092 /* Only ASCII characters are allowed */
2093 for (src = info->lpszProxy; *src; src++)
2096 set_last_error( ERROR_INVALID_PARAMETER );
2099 if (info->lpszProxyBypass)
2101 for (src = info->lpszProxyBypass; *src; src++)
2104 set_last_error( ERROR_INVALID_PARAMETER );
2110 set_last_error( ERROR_INVALID_PARAMETER );
2114 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2115 KEY_WRITE, NULL, &key, NULL );
2118 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2121 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2123 size += strlenW( info->lpszProxy );
2124 if (info->lpszProxyBypass)
2125 size += strlenW( info->lpszProxyBypass );
2127 buf = heap_alloc( size );
2130 struct connection_settings_header *hdr =
2131 (struct connection_settings_header *)buf;
2132 DWORD *len = (DWORD *)(hdr + 1);
2134 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2136 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2140 hdr->flags = PROXY_TYPE_PROXY;
2141 *len++ = strlenW( info->lpszProxy );
2142 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2146 if (info->lpszProxyBypass)
2148 *len++ = strlenW( info->lpszProxyBypass );
2149 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2158 hdr->flags = PROXY_TYPE_DIRECT;
2162 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2172 /***********************************************************************
2173 * WinHttpSetStatusCallback (winhttp.@)
2175 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2176 DWORD flags, DWORD_PTR reserved )
2178 object_header_t *hdr;
2179 WINHTTP_STATUS_CALLBACK ret;
2181 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2183 if (!(hdr = grab_object( handle )))
2185 set_last_error( ERROR_INVALID_HANDLE );
2186 return WINHTTP_INVALID_STATUS_CALLBACK;
2188 ret = hdr->callback;
2189 hdr->callback = callback;
2190 hdr->notify_mask = flags;
2192 release_object( hdr );
2196 /***********************************************************************
2197 * WinHttpSetTimeouts (winhttp.@)
2199 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2202 object_header_t *hdr;
2206 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2208 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2210 set_last_error( ERROR_INVALID_PARAMETER );
2214 if (!(hdr = grab_object( handle )))
2216 set_last_error( ERROR_INVALID_HANDLE );
2222 case WINHTTP_HANDLE_TYPE_REQUEST:
2223 request = (request_t *)hdr;
2224 request->connect_timeout = connect;
2226 if (resolve < 0) resolve = 0;
2227 request->resolve_timeout = resolve;
2229 if (send < 0) send = 0;
2230 request->send_timeout = send;
2232 if (receive < 0) receive = 0;
2233 request->recv_timeout = receive;
2235 if (netconn_connected( &request->netconn ))
2237 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2238 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2241 release_object( &request->hdr );
2244 case WINHTTP_HANDLE_TYPE_SESSION:
2245 session = (session_t *)hdr;
2246 session->connect_timeout = connect;
2248 if (resolve < 0) resolve = 0;
2249 session->resolve_timeout = resolve;
2251 if (send < 0) send = 0;
2252 session->send_timeout = send;
2254 if (receive < 0) receive = 0;
2255 session->recv_timeout = receive;
2259 release_object( hdr );
2260 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2266 static const WCHAR wkday[7][4] =
2267 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2268 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2269 static const WCHAR month[12][4] =
2270 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2271 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2272 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2274 /***********************************************************************
2275 * WinHttpTimeFromSystemTime (WININET.@)
2277 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2279 static const WCHAR format[] =
2280 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2281 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2283 TRACE("%p, %p\n", time, string);
2285 if (!time || !string) return FALSE;
2287 sprintfW( string, format,
2288 wkday[time->wDayOfWeek],
2290 month[time->wMonth - 1],
2299 /***********************************************************************
2300 * WinHttpTimeToSystemTime (WININET.@)
2302 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2305 const WCHAR *s = string;
2308 TRACE("%s, %p\n", debugstr_w(string), time);
2310 if (!string || !time) return FALSE;
2312 /* Windows does this too */
2313 GetSystemTime( time );
2315 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2316 * a SYSTEMTIME structure.
2319 while (*s && !isalphaW( *s )) s++;
2320 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2321 time->wDayOfWeek = 7;
2323 for (i = 0; i < 7; i++)
2325 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2326 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2327 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2329 time->wDayOfWeek = i;
2334 if (time->wDayOfWeek > 6) return TRUE;
2335 while (*s && !isdigitW( *s )) s++;
2336 time->wDay = strtolW( s, &end, 10 );
2339 while (*s && !isalphaW( *s )) s++;
2340 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2343 for (i = 0; i < 12; i++)
2345 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2346 toupperW( month[i][1]) == toupperW( s[1] ) &&
2347 toupperW( month[i][2]) == toupperW( s[2] ) )
2349 time->wMonth = i + 1;
2353 if (time->wMonth == 0) return TRUE;
2355 while (*s && !isdigitW( *s )) s++;
2356 if (*s == '\0') return TRUE;
2357 time->wYear = strtolW( s, &end, 10 );
2360 while (*s && !isdigitW( *s )) s++;
2361 if (*s == '\0') return TRUE;
2362 time->wHour = strtolW( s, &end, 10 );
2365 while (*s && !isdigitW( *s )) s++;
2366 if (*s == '\0') return TRUE;
2367 time->wMinute = strtolW( s, &end, 10 );
2370 while (*s && !isdigitW( *s )) s++;
2371 if (*s == '\0') return TRUE;
2372 time->wSecond = strtolW( s, &end, 10 );
2374 time->wMilliseconds = 0;