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 )) 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 );
1252 *url = build_wpad_url( name, ai );
1256 TRACE("returning %s\n", debugstr_w(*url));
1265 heap_free( domain );
1268 FIXME("getaddrinfo not found at build time\n");
1271 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1275 static const WCHAR Connections[] = {
1276 'S','o','f','t','w','a','r','e','\\',
1277 'M','i','c','r','o','s','o','f','t','\\',
1278 'W','i','n','d','o','w','s','\\',
1279 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1280 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1281 'C','o','n','n','e','c','t','i','o','n','s',0 };
1282 static const WCHAR WinHttpSettings[] = {
1283 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1284 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1285 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1286 static const DWORD PROXY_TYPE_DIRECT = 1;
1287 static const DWORD PROXY_TYPE_PROXY = 2;
1288 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1289 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1291 struct connection_settings_header
1294 DWORD unknown; /* always zero? */
1295 DWORD flags; /* one or more of PROXY_* */
1298 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1302 for (begin = src; src - begin < len; src++, dst++)
1307 /***********************************************************************
1308 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1310 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1314 BOOL got_from_reg = FALSE, direct = TRUE;
1317 TRACE("%p\n", info);
1319 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1322 DWORD type, size = 0;
1324 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1325 if (!l && type == REG_BINARY &&
1326 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1328 BYTE *buf = heap_alloc( size );
1332 struct connection_settings_header *hdr =
1333 (struct connection_settings_header *)buf;
1334 DWORD *len = (DWORD *)(hdr + 1);
1336 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1338 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1341 if (hdr->flags & PROXY_TYPE_PROXY)
1344 LPWSTR proxy = NULL;
1345 LPWSTR proxy_bypass = NULL;
1347 /* Sanity-check length of proxy string */
1348 if ((BYTE *)len - buf + *len <= size)
1351 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1353 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1354 len = (DWORD *)((BYTE *)(len + 1) + *len);
1358 /* Sanity-check length of proxy bypass string */
1359 if ((BYTE *)len - buf + *len <= size)
1361 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1363 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1368 GlobalFree( proxy );
1372 info->lpszProxy = proxy;
1373 info->lpszProxyBypass = proxy_bypass;
1376 got_from_reg = TRUE;
1378 info->dwAccessType =
1379 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1380 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1381 debugstr_w(info->lpszProxy),
1382 debugstr_w(info->lpszProxyBypass));
1391 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1393 char *colon, *http_proxy;
1395 if ((colon = strchr( envproxy, ':' )))
1397 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1399 static const char http[] = "http://";
1401 /* It's a scheme, check that it's http */
1402 if (!strncmp( envproxy, http, strlen( http ) ))
1403 http_proxy = envproxy + strlen( http );
1406 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1411 http_proxy = envproxy;
1414 http_proxy = envproxy;
1420 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1421 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1423 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1425 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1426 info->lpszProxy = http_proxyW;
1427 info->lpszProxyBypass = NULL;
1428 TRACE("http proxy (from environment) = %s\n",
1429 debugstr_w(info->lpszProxy));
1435 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1436 info->lpszProxy = NULL;
1437 info->lpszProxyBypass = NULL;
1442 /***********************************************************************
1443 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1445 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1447 static const WCHAR settingsW[] =
1448 {'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};
1450 struct connection_settings_header *hdr = NULL;
1451 DWORD type, offset, len, size = 0;
1454 TRACE("%p\n", config);
1458 set_last_error( ERROR_INVALID_PARAMETER );
1461 memset( config, 0, sizeof(*config) );
1462 config->fAutoDetect = TRUE;
1464 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1465 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1466 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1471 if (!(hdr = heap_alloc( size ))) goto done;
1472 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1473 hdr->magic != WININET_SETTINGS_MAGIC)
1479 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1480 offset = sizeof(*hdr);
1481 if (offset + sizeof(DWORD) > size) goto done;
1482 len = *(DWORD *)((char *)hdr + offset);
1483 offset += sizeof(DWORD);
1484 if (len && hdr->flags & PROXY_TYPE_PROXY)
1486 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1487 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1490 if (offset + sizeof(DWORD) > size) goto done;
1491 len = *(DWORD *)((char *)hdr + offset);
1492 offset += sizeof(DWORD);
1493 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1495 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1496 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1499 if (offset + sizeof(DWORD) > size) goto done;
1500 len = *(DWORD *)((char *)hdr + offset);
1501 offset += sizeof(DWORD);
1502 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1504 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1505 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1510 RegCloseKey( hkey );
1514 heap_free( config->lpszAutoConfigUrl );
1515 config->lpszAutoConfigUrl = NULL;
1516 heap_free( config->lpszProxy );
1517 config->lpszProxy = NULL;
1518 heap_free( config->lpszProxyBypass );
1519 config->lpszProxyBypass = NULL;
1524 static HRESULT WINAPI dispex_QueryInterface(
1525 IDispatchEx *iface, REFIID riid, void **ppv )
1529 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1530 IsEqualGUID( riid, &IID_IDispatch ) ||
1531 IsEqualGUID( riid, &IID_IDispatchEx ))
1534 return E_NOINTERFACE;
1539 static ULONG WINAPI dispex_AddRef(
1540 IDispatchEx *iface )
1545 static ULONG WINAPI dispex_Release(
1546 IDispatchEx *iface )
1551 static HRESULT WINAPI dispex_GetTypeInfoCount(
1552 IDispatchEx *iface, UINT *info )
1557 static HRESULT WINAPI dispex_GetTypeInfo(
1558 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1563 static HRESULT WINAPI dispex_GetIDsOfNames(
1564 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1569 static HRESULT WINAPI dispex_Invoke(
1570 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1571 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1576 static HRESULT WINAPI dispex_DeleteMemberByName(
1577 IDispatchEx *iface, BSTR name, DWORD flags )
1582 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1583 IDispatchEx *iface, DISPID id )
1588 static HRESULT WINAPI dispex_GetMemberProperties(
1589 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1594 static HRESULT WINAPI dispex_GetMemberName(
1595 IDispatchEx *iface, DISPID id, BSTR *name )
1600 static HRESULT WINAPI dispex_GetNextDispID(
1601 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1606 static HRESULT WINAPI dispex_GetNameSpaceParent(
1607 IDispatchEx *iface, IUnknown **unk )
1612 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1614 static HRESULT WINAPI dispex_GetDispID(
1615 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1617 if (!strcmpW( name, dns_resolveW ))
1619 *id = DISPID_GLOBAL_DNSRESOLVE;
1622 return DISP_E_UNKNOWNNAME;
1625 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1627 #ifdef HAVE_GETADDRINFO
1628 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1630 struct addrinfo *ai, *elem;
1635 hostnameA = strdupWA( hostname );
1637 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1639 if (!hostnameA) return E_OUTOFMEMORY;
1640 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1641 heap_free( hostnameA );
1642 if (res) return S_FALSE;
1645 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1651 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1653 V_VT( result ) = VT_BSTR;
1654 V_BSTR( result ) = SysAllocString( addr );
1657 FIXME("getaddrinfo not found at build time\n");
1662 static HRESULT WINAPI dispex_InvokeEx(
1663 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1664 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1666 if (id == DISPID_GLOBAL_DNSRESOLVE)
1668 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1669 if (V_VT(¶ms->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1670 return dns_resolve( V_BSTR(¶ms->rgvarg[0]), result );
1672 return DISP_E_MEMBERNOTFOUND;
1675 static const IDispatchExVtbl dispex_vtbl =
1677 dispex_QueryInterface,
1680 dispex_GetTypeInfoCount,
1682 dispex_GetIDsOfNames,
1686 dispex_DeleteMemberByName,
1687 dispex_DeleteMemberByDispID,
1688 dispex_GetMemberProperties,
1689 dispex_GetMemberName,
1690 dispex_GetNextDispID,
1691 dispex_GetNameSpaceParent
1694 static IDispatchEx global_dispex = { &dispex_vtbl };
1696 static HRESULT WINAPI site_QueryInterface(
1697 IActiveScriptSite *iface, REFIID riid, void **ppv )
1701 if (IsEqualGUID( &IID_IUnknown, riid ))
1703 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1706 return E_NOINTERFACE;
1708 IUnknown_AddRef( (IUnknown *)*ppv );
1712 static ULONG WINAPI site_AddRef(
1713 IActiveScriptSite *iface )
1718 static ULONG WINAPI site_Release(
1719 IActiveScriptSite *iface )
1724 static HRESULT WINAPI site_GetLCID(
1725 IActiveScriptSite *iface, LCID *lcid )
1730 static HRESULT WINAPI site_GetItemInfo(
1731 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1732 IUnknown **item, ITypeInfo **type_info )
1734 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1736 *item = (IUnknown *)&global_dispex;
1742 static HRESULT WINAPI site_GetDocVersionString(
1743 IActiveScriptSite *iface, BSTR *version )
1748 static HRESULT WINAPI site_OnScriptTerminate(
1749 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1754 static HRESULT WINAPI site_OnStateChange(
1755 IActiveScriptSite *iface, SCRIPTSTATE state )
1760 static HRESULT WINAPI site_OnScriptError(
1761 IActiveScriptSite *iface, IActiveScriptError *error )
1766 static HRESULT WINAPI site_OnEnterScript(
1767 IActiveScriptSite *iface )
1772 static HRESULT WINAPI site_OnLeaveScript(
1773 IActiveScriptSite *iface )
1778 static const IActiveScriptSiteVtbl site_vtbl =
1780 site_QueryInterface,
1785 site_GetDocVersionString,
1786 site_OnScriptTerminate,
1793 static IActiveScriptSite script_site = { &site_vtbl };
1795 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1797 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1802 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1803 info->lpszProxy = NULL;
1804 info->lpszProxyBypass = NULL;
1806 if (V_VT( &result ) != VT_BSTR) return TRUE;
1807 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1809 p = V_BSTR( &result );
1810 while (*p == ' ') p++;
1812 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1815 while (*p == ' ') p++;
1816 if (!*p || *p == ';') return TRUE;
1817 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1818 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1821 if (*q == ' ' || *q == ';')
1831 static BSTR include_pac_utils( BSTR script )
1833 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1834 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1841 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1842 size = SizeofResource( hmod, rsrc );
1843 data = LoadResource( hmod, rsrc );
1845 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1846 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1847 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1849 strcatW( ret, script );
1854 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1855 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1856 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1858 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1859 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1860 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1863 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1865 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1866 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1867 IActiveScriptParse *parser = NULL;
1868 IActiveScript *engine = NULL;
1869 IDispatch *dispatch = NULL;
1873 BSTR func = NULL, hostname = NULL, full_script = NULL;
1875 VARIANT args[2], result;
1879 memset( &uc, 0, sizeof(uc) );
1880 uc.dwStructSize = sizeof(uc);
1881 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1882 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1883 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1884 hostname[uc.dwHostNameLength] = 0;
1886 init = CoInitialize( NULL );
1887 hr = CLSIDFromProgID( jscriptW, &clsid );
1888 if (hr != S_OK) goto done;
1890 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1891 &IID_IActiveScript, (void **)&engine );
1892 if (hr != S_OK) goto done;
1894 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1895 if (hr != S_OK) goto done;
1897 hr = IActiveScriptParse_InitNew( parser );
1898 if (hr != S_OK) goto done;
1900 hr = IActiveScript_SetScriptSite( engine, &script_site );
1901 if (hr != S_OK) goto done;
1903 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1904 if (hr != S_OK) goto done;
1906 if (!(full_script = include_pac_utils( script ))) goto done;
1908 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1909 if (hr != S_OK) goto done;
1911 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1912 if (hr != S_OK) goto done;
1914 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1915 if (hr != S_OK) goto done;
1917 if (!(func = SysAllocString( findproxyW ))) goto done;
1918 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1919 if (hr != S_OK) goto done;
1921 V_VT( &args[0] ) = VT_BSTR;
1922 V_BSTR( &args[0] ) = hostname;
1923 V_VT( &args[1] ) = VT_BSTR;
1924 V_BSTR( &args[1] ) = SysAllocString( url );
1926 params.rgvarg = args;
1927 params.rgdispidNamedArgs = NULL;
1929 params.cNamedArgs = 0;
1930 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1931 ¶ms, &result, NULL, NULL );
1932 VariantClear( &args[1] );
1935 WARN("script failed 0x%08x\n", hr);
1938 ret = parse_script_result( result, info );
1941 SysFreeString( full_script );
1942 SysFreeString( hostname );
1943 SysFreeString( func );
1944 if (dispatch) IDispatch_Release( dispatch );
1945 if (parser) IActiveScriptParse_Release( parser );
1946 if (engine) IActiveScript_Release( engine );
1947 if (SUCCEEDED( init )) CoUninitialize();
1948 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1952 static BSTR download_script( const WCHAR *url )
1954 static const WCHAR typeW[] = {'*','/','*',0};
1955 static const WCHAR *acceptW[] = {typeW, NULL};
1956 HINTERNET ses, con = NULL, req = NULL;
1959 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1960 char *tmp, *buffer = NULL;
1964 memset( &uc, 0, sizeof(uc) );
1965 uc.dwStructSize = sizeof(uc);
1966 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1967 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1968 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1969 hostname[uc.dwHostNameLength] = 0;
1971 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1972 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1973 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1974 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1975 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1977 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1978 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1979 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1982 if (!(buffer = heap_alloc( size ))) goto done;
1987 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1988 if (!bytes_read) break;
1989 to_read -= bytes_read;
1990 offset += bytes_read;
1995 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1999 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
2000 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
2001 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
2005 WinHttpCloseHandle( req );
2006 WinHttpCloseHandle( con );
2007 WinHttpCloseHandle( ses );
2008 heap_free( buffer );
2009 heap_free( hostname );
2010 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
2014 /***********************************************************************
2015 * WinHttpGetProxyForUrl (winhttp.@)
2017 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
2018 WINHTTP_PROXY_INFO *info )
2020 WCHAR *detected_pac_url = NULL;
2021 const WCHAR *pac_url;
2026 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
2028 if (!(session = (session_t *)grab_object( hsession )))
2030 set_last_error( ERROR_INVALID_HANDLE );
2033 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
2035 release_object( &session->hdr );
2036 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2039 if (!url || !options || !info ||
2040 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
2041 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
2042 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
2043 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
2045 release_object( &session->hdr );
2046 set_last_error( ERROR_INVALID_PARAMETER );
2049 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2050 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2053 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2054 else pac_url = detected_pac_url;
2056 if (!(script = download_script( pac_url ))) goto done;
2057 ret = run_script( script, url, info );
2058 SysFreeString( script );
2061 GlobalFree( detected_pac_url );
2062 release_object( &session->hdr );
2066 /***********************************************************************
2067 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2069 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2076 TRACE("%p\n", info);
2080 set_last_error( ERROR_INVALID_PARAMETER );
2083 switch (info->dwAccessType)
2085 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2087 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2088 if (!info->lpszProxy)
2090 set_last_error( ERROR_INVALID_PARAMETER );
2093 /* Only ASCII characters are allowed */
2094 for (src = info->lpszProxy; *src; src++)
2097 set_last_error( ERROR_INVALID_PARAMETER );
2100 if (info->lpszProxyBypass)
2102 for (src = info->lpszProxyBypass; *src; src++)
2105 set_last_error( ERROR_INVALID_PARAMETER );
2111 set_last_error( ERROR_INVALID_PARAMETER );
2115 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2116 KEY_WRITE, NULL, &key, NULL );
2119 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2122 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2124 size += strlenW( info->lpszProxy );
2125 if (info->lpszProxyBypass)
2126 size += strlenW( info->lpszProxyBypass );
2128 buf = heap_alloc( size );
2131 struct connection_settings_header *hdr =
2132 (struct connection_settings_header *)buf;
2133 DWORD *len = (DWORD *)(hdr + 1);
2135 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2137 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2141 hdr->flags = PROXY_TYPE_PROXY;
2142 *len++ = strlenW( info->lpszProxy );
2143 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2147 if (info->lpszProxyBypass)
2149 *len++ = strlenW( info->lpszProxyBypass );
2150 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2159 hdr->flags = PROXY_TYPE_DIRECT;
2163 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2173 /***********************************************************************
2174 * WinHttpSetStatusCallback (winhttp.@)
2176 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2177 DWORD flags, DWORD_PTR reserved )
2179 object_header_t *hdr;
2180 WINHTTP_STATUS_CALLBACK ret;
2182 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2184 if (!(hdr = grab_object( handle )))
2186 set_last_error( ERROR_INVALID_HANDLE );
2187 return WINHTTP_INVALID_STATUS_CALLBACK;
2189 ret = hdr->callback;
2190 hdr->callback = callback;
2191 hdr->notify_mask = flags;
2193 release_object( hdr );
2197 /***********************************************************************
2198 * WinHttpSetTimeouts (winhttp.@)
2200 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2203 object_header_t *hdr;
2207 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2209 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2211 set_last_error( ERROR_INVALID_PARAMETER );
2215 if (!(hdr = grab_object( handle )))
2217 set_last_error( ERROR_INVALID_HANDLE );
2223 case WINHTTP_HANDLE_TYPE_REQUEST:
2224 request = (request_t *)hdr;
2225 request->connect_timeout = connect;
2227 if (resolve < 0) resolve = 0;
2228 request->resolve_timeout = resolve;
2230 if (send < 0) send = 0;
2231 request->send_timeout = send;
2233 if (receive < 0) receive = 0;
2234 request->recv_timeout = receive;
2236 if (netconn_connected( &request->netconn ))
2238 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2239 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2242 release_object( &request->hdr );
2245 case WINHTTP_HANDLE_TYPE_SESSION:
2246 session = (session_t *)hdr;
2247 session->connect_timeout = connect;
2249 if (resolve < 0) resolve = 0;
2250 session->resolve_timeout = resolve;
2252 if (send < 0) send = 0;
2253 session->send_timeout = send;
2255 if (receive < 0) receive = 0;
2256 session->recv_timeout = receive;
2260 release_object( hdr );
2261 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2267 static const WCHAR wkday[7][4] =
2268 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2269 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2270 static const WCHAR month[12][4] =
2271 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2272 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2273 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2275 /***********************************************************************
2276 * WinHttpTimeFromSystemTime (WININET.@)
2278 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2280 static const WCHAR format[] =
2281 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2282 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2284 TRACE("%p, %p\n", time, string);
2286 if (!time || !string) return FALSE;
2288 sprintfW( string, format,
2289 wkday[time->wDayOfWeek],
2291 month[time->wMonth - 1],
2300 /***********************************************************************
2301 * WinHttpTimeToSystemTime (WININET.@)
2303 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2306 const WCHAR *s = string;
2309 TRACE("%s, %p\n", debugstr_w(string), time);
2311 if (!string || !time) return FALSE;
2313 /* Windows does this too */
2314 GetSystemTime( time );
2316 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2317 * a SYSTEMTIME structure.
2320 while (*s && !isalphaW( *s )) s++;
2321 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2322 time->wDayOfWeek = 7;
2324 for (i = 0; i < 7; i++)
2326 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2327 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2328 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2330 time->wDayOfWeek = i;
2335 if (time->wDayOfWeek > 6) return TRUE;
2336 while (*s && !isdigitW( *s )) s++;
2337 time->wDay = strtolW( s, &end, 10 );
2340 while (*s && !isalphaW( *s )) s++;
2341 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2344 for (i = 0; i < 12; i++)
2346 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2347 toupperW( month[i][1]) == toupperW( s[1] ) &&
2348 toupperW( month[i][2]) == toupperW( s[2] ) )
2350 time->wMonth = i + 1;
2354 if (time->wMonth == 0) return TRUE;
2356 while (*s && !isdigitW( *s )) s++;
2357 if (*s == '\0') return TRUE;
2358 time->wYear = strtolW( s, &end, 10 );
2361 while (*s && !isdigitW( *s )) s++;
2362 if (*s == '\0') return TRUE;
2363 time->wHour = strtolW( s, &end, 10 );
2366 while (*s && !isdigitW( *s )) s++;
2367 if (*s == '\0') return TRUE;
2368 time->wMinute = strtolW( s, &end, 10 );
2371 while (*s && !isdigitW( *s )) s++;
2372 if (*s == '\0') return TRUE;
2373 time->wSecond = strtolW( s, &end, 10 );
2375 time->wMilliseconds = 0;