2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
36 #include "winhttp_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
40 #define DEFAULT_RESOLVE_TIMEOUT 0
41 #define DEFAULT_CONNECT_TIMEOUT 20000
42 #define DEFAULT_SEND_TIMEOUT 30000
43 #define DEFAULT_RECEIVE_TIMEOUT 30000
45 static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
46 static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
48 void set_last_error( DWORD error )
51 SetLastError( error );
54 DWORD get_last_error( void )
57 return GetLastError();
60 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
62 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
64 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
67 /***********************************************************************
68 * WinHttpCheckPlatform (winhttp.@)
70 BOOL WINAPI WinHttpCheckPlatform( void )
76 /***********************************************************************
77 * session_destroy (internal)
79 static void session_destroy( object_header_t *hdr )
81 session_t *session = (session_t *)hdr;
82 struct list *item, *next;
85 TRACE("%p\n", session);
87 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
89 domain = LIST_ENTRY( item, domain_t, entry );
90 delete_domain( domain );
92 heap_free( session->agent );
93 heap_free( session->proxy_server );
94 heap_free( session->proxy_bypass );
95 heap_free( session->proxy_username );
96 heap_free( session->proxy_password );
100 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
102 session_t *session = (session_t *)hdr;
106 case WINHTTP_OPTION_REDIRECT_POLICY:
108 if (!buffer || *buflen < sizeof(DWORD))
110 *buflen = sizeof(DWORD);
111 set_last_error( ERROR_INSUFFICIENT_BUFFER );
115 *(DWORD *)buffer = hdr->redirect_policy;
116 *buflen = sizeof(DWORD);
119 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
120 *(DWORD *)buffer = session->resolve_timeout;
121 *buflen = sizeof(DWORD);
123 case WINHTTP_OPTION_CONNECT_TIMEOUT:
124 *(DWORD *)buffer = session->connect_timeout;
125 *buflen = sizeof(DWORD);
127 case WINHTTP_OPTION_SEND_TIMEOUT:
128 *(DWORD *)buffer = session->send_timeout;
129 *buflen = sizeof(DWORD);
131 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
132 *(DWORD *)buffer = session->recv_timeout;
133 *buflen = sizeof(DWORD);
136 FIXME("unimplemented option %u\n", option);
137 set_last_error( ERROR_INVALID_PARAMETER );
142 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
144 session_t *session = (session_t *)hdr;
148 case WINHTTP_OPTION_PROXY:
150 WINHTTP_PROXY_INFO *pi = buffer;
152 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
155 case WINHTTP_OPTION_REDIRECT_POLICY:
159 if (buflen != sizeof(policy))
161 set_last_error( ERROR_INSUFFICIENT_BUFFER );
165 policy = *(DWORD *)buffer;
166 TRACE("0x%x\n", policy);
167 hdr->redirect_policy = policy;
170 case WINHTTP_OPTION_DISABLE_FEATURE:
171 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
173 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
174 session->resolve_timeout = *(DWORD *)buffer;
176 case WINHTTP_OPTION_CONNECT_TIMEOUT:
177 session->connect_timeout = *(DWORD *)buffer;
179 case WINHTTP_OPTION_SEND_TIMEOUT:
180 session->send_timeout = *(DWORD *)buffer;
182 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
183 session->recv_timeout = *(DWORD *)buffer;
186 FIXME("unimplemented option %u\n", option);
187 set_last_error( ERROR_INVALID_PARAMETER );
192 static const object_vtbl_t session_vtbl =
195 session_query_option,
199 /***********************************************************************
200 * WinHttpOpen (winhttp.@)
202 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
205 HINTERNET handle = NULL;
207 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
209 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
211 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
212 session->hdr.vtbl = &session_vtbl;
213 session->hdr.flags = flags;
214 session->hdr.refs = 1;
215 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
216 list_init( &session->hdr.children );
217 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
218 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
219 session->send_timeout = DEFAULT_SEND_TIMEOUT;
220 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
221 list_init( &session->cookie_cache );
223 if (agent && !(session->agent = strdupW( agent ))) goto end;
224 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
226 WINHTTP_PROXY_INFO info;
228 WinHttpGetDefaultProxyConfiguration( &info );
229 session->access = info.dwAccessType;
230 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
232 GlobalFree( info.lpszProxy );
233 GlobalFree( info.lpszProxyBypass );
236 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
238 GlobalFree( info.lpszProxy );
239 GlobalFree( info.lpszProxyBypass );
243 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
245 session->access = access;
246 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
247 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
250 if (!(handle = alloc_handle( &session->hdr ))) goto end;
251 session->hdr.handle = handle;
254 release_object( &session->hdr );
255 TRACE("returning %p\n", handle);
259 /***********************************************************************
260 * connect_destroy (internal)
262 static void connect_destroy( object_header_t *hdr )
264 connect_t *connect = (connect_t *)hdr;
266 TRACE("%p\n", connect);
268 release_object( &connect->session->hdr );
270 heap_free( connect->hostname );
271 heap_free( connect->servername );
272 heap_free( connect->username );
273 heap_free( connect->password );
274 heap_free( connect );
277 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
279 connect_t *connect = (connect_t *)hdr;
283 case WINHTTP_OPTION_PARENT_HANDLE:
285 if (!buffer || *buflen < sizeof(HINTERNET))
287 *buflen = sizeof(HINTERNET);
288 set_last_error( ERROR_INSUFFICIENT_BUFFER );
292 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
293 *buflen = sizeof(HINTERNET);
296 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
297 *(DWORD *)buffer = connect->session->resolve_timeout;
298 *buflen = sizeof(DWORD);
300 case WINHTTP_OPTION_CONNECT_TIMEOUT:
301 *(DWORD *)buffer = connect->session->connect_timeout;
302 *buflen = sizeof(DWORD);
304 case WINHTTP_OPTION_SEND_TIMEOUT:
305 *(DWORD *)buffer = connect->session->send_timeout;
306 *buflen = sizeof(DWORD);
308 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
309 *(DWORD *)buffer = connect->session->recv_timeout;
310 *buflen = sizeof(DWORD);
313 FIXME("unimplemented option %u\n", option);
314 set_last_error( ERROR_INVALID_PARAMETER );
319 static const object_vtbl_t connect_vtbl =
322 connect_query_option,
326 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
328 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
331 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
333 else if (*domain == '*')
335 if (domain[1] == '.')
339 /* For a hostname to match a wildcard, the last domain must match
340 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
341 * hostname is www.foo.a.b, it matches, but a.b does not.
343 dot = strchrW( server, '.' );
346 int len = strlenW( dot + 1 );
348 if (len > strlenW( domain + 2 ))
352 /* The server's domain is longer than the wildcard, so it
353 * could be a subdomain. Compare the last portion of the
356 ptr = dot + len + 1 - strlenW( domain + 2 );
357 if (!strcmpiW( ptr, domain + 2 ))
359 /* This is only a match if the preceding character is
360 * a '.', i.e. that it is a matching domain. E.g.
361 * if domain is '*.b.c' and server is 'www.ab.c' they
364 ret = *(ptr - 1) == '.';
368 ret = !strcmpiW( dot + 1, domain + 2 );
373 ret = !strcmpiW( server, domain );
377 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
378 #define MAX_HOST_NAME_LENGTH 256
380 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
385 if (!session->proxy_bypass) return FALSE;
386 ptr = session->proxy_bypass;
390 ptr = strchrW( ptr, ';' );
392 ptr = strchrW( tmp, ' ' );
395 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
397 WCHAR domain[MAX_HOST_NAME_LENGTH];
399 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
400 domain[ptr - tmp] = 0;
401 ret = domain_matches( server, domain );
406 ret = domain_matches( server, tmp );
407 } while (ptr && !ret);
411 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
413 session_t *session = connect->session;
416 if (session->proxy_server && !should_bypass_proxy(session, server))
420 if ((colon = strchrW( session->proxy_server, ':' )))
422 if (!connect->servername || strncmpiW( connect->servername,
423 session->proxy_server, colon - session->proxy_server - 1 ))
425 heap_free( connect->servername );
426 connect->resolved = FALSE;
427 if (!(connect->servername = heap_alloc(
428 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
433 memcpy( connect->servername, session->proxy_server,
434 (colon - session->proxy_server) * sizeof(WCHAR) );
435 connect->servername[colon - session->proxy_server] = 0;
437 connect->serverport = atoiW( colon + 1 );
439 connect->serverport = INTERNET_DEFAULT_PORT;
444 if (!connect->servername || strcmpiW( connect->servername,
445 session->proxy_server ))
447 heap_free( connect->servername );
448 connect->resolved = FALSE;
449 if (!(connect->servername = strdupW( session->proxy_server )))
454 connect->serverport = INTERNET_DEFAULT_PORT;
460 heap_free( connect->servername );
461 connect->resolved = FALSE;
462 if (!(connect->servername = strdupW( server )))
467 connect->serverport = port;
473 /***********************************************************************
474 * WinHttpConnect (winhttp.@)
476 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
480 HINTERNET hconnect = NULL;
482 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
486 set_last_error( ERROR_INVALID_PARAMETER );
489 if (!(session = (session_t *)grab_object( hsession )))
491 set_last_error( ERROR_INVALID_HANDLE );
494 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
496 release_object( &session->hdr );
497 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
500 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
502 release_object( &session->hdr );
505 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
506 connect->hdr.vtbl = &connect_vtbl;
507 connect->hdr.refs = 1;
508 connect->hdr.flags = session->hdr.flags;
509 connect->hdr.callback = session->hdr.callback;
510 connect->hdr.notify_mask = session->hdr.notify_mask;
511 connect->hdr.context = session->hdr.context;
512 connect->hdr.redirect_policy = session->hdr.redirect_policy;
513 list_init( &connect->hdr.children );
515 addref_object( &session->hdr );
516 connect->session = session;
517 list_add_head( &session->hdr.children, &connect->hdr.entry );
519 if (!(connect->hostname = strdupW( server ))) goto end;
520 connect->hostport = port;
521 if (!set_server_for_hostname( connect, server, port )) goto end;
523 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
524 connect->hdr.handle = hconnect;
526 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
529 release_object( &connect->hdr );
530 release_object( &session->hdr );
531 TRACE("returning %p\n", hconnect);
535 /***********************************************************************
536 * request_destroy (internal)
538 static void request_destroy( object_header_t *hdr )
540 request_t *request = (request_t *)hdr;
543 TRACE("%p\n", request);
545 release_object( &request->connect->hdr );
547 heap_free( request->verb );
548 heap_free( request->path );
549 heap_free( request->version );
550 heap_free( request->raw_headers );
551 heap_free( request->status_text );
552 for (i = 0; i < request->num_headers; i++)
554 heap_free( request->headers[i].field );
555 heap_free( request->headers[i].value );
557 heap_free( request->headers );
558 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
559 heap_free( request->accept_types );
560 heap_free( request );
563 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
566 if (str) len = strlenW( str );
567 if (buffer && *buflen > len)
569 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
572 *buflen = len * sizeof(WCHAR);
575 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
578 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
580 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
581 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
582 CertNameToStrW( encoding, blob, format, ret, size );
587 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
589 request_t *request = (request_t *)hdr;
593 case WINHTTP_OPTION_SECURITY_FLAGS:
598 if (!buffer || *buflen < sizeof(flags))
600 *buflen = sizeof(flags);
601 set_last_error( ERROR_INSUFFICIENT_BUFFER );
606 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
607 flags |= request->netconn.security_flags;
608 bits = netconn_get_cipher_strength( &request->netconn );
610 flags |= SECURITY_FLAG_STRENGTH_STRONG;
612 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
614 flags |= SECURITY_FLAG_STRENGTH_WEAK;
615 *(DWORD *)buffer = flags;
616 *buflen = sizeof(flags);
619 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
621 const CERT_CONTEXT *cert;
623 if (!buffer || *buflen < sizeof(cert))
625 *buflen = sizeof(cert);
626 set_last_error( ERROR_INSUFFICIENT_BUFFER );
630 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
631 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
632 *buflen = sizeof(cert);
635 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
637 const CERT_CONTEXT *cert;
638 const CRYPT_OID_INFO *oidInfo;
639 WINHTTP_CERTIFICATE_INFO *ci = buffer;
641 FIXME("partial stub\n");
643 if (!buffer || *buflen < sizeof(*ci))
645 *buflen = sizeof(*ci);
646 set_last_error( ERROR_INSUFFICIENT_BUFFER );
649 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
651 ci->ftExpiry = cert->pCertInfo->NotAfter;
652 ci->ftStart = cert->pCertInfo->NotBefore;
653 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
654 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
655 ci->lpszProtocolName = NULL;
656 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
657 cert->pCertInfo->SignatureAlgorithm.pszObjId,
660 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
662 ci->lpszSignatureAlgName = NULL;
663 ci->lpszEncryptionAlgName = NULL;
664 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
666 CertFreeCertificateContext( cert );
667 *buflen = sizeof(*ci);
670 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
672 if (!buffer || *buflen < sizeof(DWORD))
674 *buflen = sizeof(DWORD);
675 set_last_error( ERROR_INSUFFICIENT_BUFFER );
679 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
680 *buflen = sizeof(DWORD);
683 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
684 *(DWORD *)buffer = request->resolve_timeout;
685 *buflen = sizeof(DWORD);
687 case WINHTTP_OPTION_CONNECT_TIMEOUT:
688 *(DWORD *)buffer = request->connect_timeout;
689 *buflen = sizeof(DWORD);
691 case WINHTTP_OPTION_SEND_TIMEOUT:
692 *(DWORD *)buffer = request->send_timeout;
693 *buflen = sizeof(DWORD);
695 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
696 *(DWORD *)buffer = request->recv_timeout;
697 *buflen = sizeof(DWORD);
700 case WINHTTP_OPTION_USERNAME:
701 str_to_buffer( buffer, request->connect->username, buflen );
704 case WINHTTP_OPTION_PASSWORD:
705 str_to_buffer( buffer, request->connect->password, buflen );
708 case WINHTTP_OPTION_PROXY_USERNAME:
709 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
712 case WINHTTP_OPTION_PROXY_PASSWORD:
713 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
717 FIXME("unimplemented option %u\n", option);
718 set_last_error( ERROR_INVALID_PARAMETER );
723 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
726 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
728 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
732 set_last_error( ERROR_OUTOFMEMORY );
736 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
738 request_t *request = (request_t *)hdr;
742 case WINHTTP_OPTION_PROXY:
744 WINHTTP_PROXY_INFO *pi = buffer;
746 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
749 case WINHTTP_OPTION_DISABLE_FEATURE:
753 if (buflen != sizeof(DWORD))
755 set_last_error( ERROR_INSUFFICIENT_BUFFER );
759 disable = *(DWORD *)buffer;
760 TRACE("0x%x\n", disable);
761 hdr->disable_flags |= disable;
764 case WINHTTP_OPTION_AUTOLOGON_POLICY:
768 if (buflen != sizeof(DWORD))
770 set_last_error( ERROR_INSUFFICIENT_BUFFER );
774 policy = *(DWORD *)buffer;
775 TRACE("0x%x\n", policy);
776 hdr->logon_policy = policy;
779 case WINHTTP_OPTION_REDIRECT_POLICY:
783 if (buflen != sizeof(DWORD))
785 set_last_error( ERROR_INSUFFICIENT_BUFFER );
789 policy = *(DWORD *)buffer;
790 TRACE("0x%x\n", policy);
791 hdr->redirect_policy = policy;
794 case WINHTTP_OPTION_SECURITY_FLAGS:
798 if (buflen < sizeof(DWORD))
800 set_last_error( ERROR_INSUFFICIENT_BUFFER );
803 flags = *(DWORD *)buffer;
804 TRACE("0x%x\n", flags);
805 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
806 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
807 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
808 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
810 set_last_error( ERROR_INVALID_PARAMETER );
813 request->netconn.security_flags = flags;
816 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
817 request->resolve_timeout = *(DWORD *)buffer;
819 case WINHTTP_OPTION_CONNECT_TIMEOUT:
820 request->connect_timeout = *(DWORD *)buffer;
822 case WINHTTP_OPTION_SEND_TIMEOUT:
823 request->send_timeout = *(DWORD *)buffer;
825 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
826 request->recv_timeout = *(DWORD *)buffer;
829 case WINHTTP_OPTION_USERNAME:
831 connect_t *connect = request->connect;
833 heap_free( connect->username );
834 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
837 case WINHTTP_OPTION_PASSWORD:
839 connect_t *connect = request->connect;
841 heap_free( connect->password );
842 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
845 case WINHTTP_OPTION_PROXY_USERNAME:
847 session_t *session = request->connect->session;
849 heap_free( session->proxy_username );
850 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
853 case WINHTTP_OPTION_PROXY_PASSWORD:
855 session_t *session = request->connect->session;
857 heap_free( session->proxy_password );
858 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
862 FIXME("unimplemented option %u\n", option);
863 set_last_error( ERROR_INVALID_PARAMETER );
868 static const object_vtbl_t request_vtbl =
871 request_query_option,
875 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
877 const WCHAR **types = accept_types;
880 if (!types) return TRUE;
883 request->num_accept_types++;
886 if (!request->num_accept_types) return TRUE;
887 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
889 request->num_accept_types = 0;
892 types = accept_types;
893 for (i = 0; i < request->num_accept_types; i++)
895 if (!(request->accept_types[i] = strdupW( *types )))
897 for (; i >= 0; i--) heap_free( request->accept_types[i] );
898 heap_free( request->accept_types );
899 request->accept_types = NULL;
900 request->num_accept_types = 0;
908 /***********************************************************************
909 * WinHttpOpenRequest (winhttp.@)
911 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
912 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
916 HINTERNET hrequest = NULL;
918 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
919 debugstr_w(version), debugstr_w(referrer), types, flags);
921 if(types && TRACE_ON(winhttp)) {
924 TRACE("accept types:\n");
925 for(iter = types; *iter; iter++)
926 TRACE(" %s\n", debugstr_w(*iter));
929 if (!(connect = (connect_t *)grab_object( hconnect )))
931 set_last_error( ERROR_INVALID_HANDLE );
934 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
936 release_object( &connect->hdr );
937 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
940 if (!(request = heap_alloc_zero( sizeof(request_t) )))
942 release_object( &connect->hdr );
945 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
946 request->hdr.vtbl = &request_vtbl;
947 request->hdr.refs = 1;
948 request->hdr.flags = flags;
949 request->hdr.callback = connect->hdr.callback;
950 request->hdr.notify_mask = connect->hdr.notify_mask;
951 request->hdr.context = connect->hdr.context;
952 request->hdr.redirect_policy = connect->hdr.redirect_policy;
953 list_init( &request->hdr.children );
955 addref_object( &connect->hdr );
956 request->connect = connect;
957 list_add_head( &connect->hdr.children, &request->hdr.entry );
959 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
960 request->resolve_timeout = connect->session->resolve_timeout;
961 request->connect_timeout = connect->session->connect_timeout;
962 request->send_timeout = connect->session->send_timeout;
963 request->recv_timeout = connect->session->recv_timeout;
965 if (!verb || !verb[0]) verb = getW;
966 if (!(request->verb = strdupW( verb ))) goto end;
973 len = strlenW( object ) + 1;
974 if (object[0] != '/') len++;
975 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
977 if (object[0] != '/') *p++ = '/';
978 strcpyW( p, object );
979 request->path = path;
981 else if (!(request->path = strdupW( slashW ))) goto end;
983 if (!version || !version[0]) version = http1_1;
984 if (!(request->version = strdupW( version ))) goto end;
985 if (!(store_accept_types( request, types ))) goto end;
987 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
988 request->hdr.handle = hrequest;
990 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
993 release_object( &request->hdr );
994 release_object( &connect->hdr );
995 TRACE("returning %p\n", hrequest);
999 /***********************************************************************
1000 * WinHttpCloseHandle (winhttp.@)
1002 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1004 object_header_t *hdr;
1006 TRACE("%p\n", handle);
1008 if (!(hdr = grab_object( handle )))
1010 set_last_error( ERROR_INVALID_HANDLE );
1013 release_object( hdr );
1014 free_handle( handle );
1018 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1024 set_last_error( ERROR_INVALID_PARAMETER );
1030 case WINHTTP_OPTION_CONTEXT_VALUE:
1032 if (!buffer || *buflen < sizeof(DWORD_PTR))
1034 *buflen = sizeof(DWORD_PTR);
1035 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1039 *(DWORD_PTR *)buffer = hdr->context;
1040 *buflen = sizeof(DWORD_PTR);
1044 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1047 FIXME("unimplemented option %u\n", option);
1048 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1056 /***********************************************************************
1057 * WinHttpQueryOption (winhttp.@)
1059 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1062 object_header_t *hdr;
1064 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1066 if (!(hdr = grab_object( handle )))
1068 set_last_error( ERROR_INVALID_HANDLE );
1072 ret = query_option( hdr, option, buffer, buflen );
1074 release_object( hdr );
1078 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1084 set_last_error( ERROR_INVALID_PARAMETER );
1090 case WINHTTP_OPTION_CONTEXT_VALUE:
1092 if (buflen != sizeof(DWORD_PTR))
1094 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1098 hdr->context = *(DWORD_PTR *)buffer;
1102 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1105 FIXME("unimplemented option %u\n", option);
1106 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1114 /***********************************************************************
1115 * WinHttpSetOption (winhttp.@)
1117 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1120 object_header_t *hdr;
1122 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1124 if (!(hdr = grab_object( handle )))
1126 set_last_error( ERROR_INVALID_HANDLE );
1130 ret = set_option( hdr, option, buffer, buflen );
1132 release_object( hdr );
1136 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1141 GetComputerNameExA( format, NULL, &size );
1142 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1143 if (!(ret = heap_alloc( size ))) return NULL;
1144 if (!GetComputerNameExA( format, ret, &size ))
1152 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1154 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1156 if (len_suffix > len_domain) return FALSE;
1157 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1161 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1164 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1165 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1166 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1167 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1170 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1172 static const WCHAR fmtW[] =
1173 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1174 '/','w','p','a','d','.','d','a','t',0};
1177 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1178 if (!ai) return NULL;
1180 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1181 printf_addr( fmtW, ret, (struct sockaddr_in *)ai->ai_addr );
1185 /***********************************************************************
1186 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1188 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1192 TRACE("0x%08x, %p\n", flags, url);
1196 set_last_error( ERROR_INVALID_PARAMETER );
1199 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1201 static int fixme_shown;
1202 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1204 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1206 #ifdef HAVE_GETADDRINFO
1207 char *fqdn, *domain, *p;
1209 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1210 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1216 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1218 struct addrinfo *ai;
1222 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1225 heap_free( domain );
1228 strcpy( name, "wpad" );
1230 res = getaddrinfo( name, NULL, NULL, &ai );
1234 *url = build_wpad_url( ai );
1238 TRACE("returning %s\n", debugstr_w(*url));
1245 heap_free( domain );
1248 FIXME("getaddrinfo not found at build time\n");
1251 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1255 static const WCHAR Connections[] = {
1256 'S','o','f','t','w','a','r','e','\\',
1257 'M','i','c','r','o','s','o','f','t','\\',
1258 'W','i','n','d','o','w','s','\\',
1259 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1260 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1261 'C','o','n','n','e','c','t','i','o','n','s',0 };
1262 static const WCHAR WinHttpSettings[] = {
1263 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1264 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1265 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1266 static const DWORD PROXY_TYPE_DIRECT = 1;
1267 static const DWORD PROXY_TYPE_PROXY = 2;
1268 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1269 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1271 struct connection_settings_header
1274 DWORD unknown; /* always zero? */
1275 DWORD flags; /* one or more of PROXY_* */
1278 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1282 for (begin = src; src - begin < len; src++, dst++)
1287 /***********************************************************************
1288 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1290 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1294 BOOL got_from_reg = FALSE, direct = TRUE;
1297 TRACE("%p\n", info);
1299 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1302 DWORD type, size = 0;
1304 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1305 if (!l && type == REG_BINARY &&
1306 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1308 BYTE *buf = heap_alloc( size );
1312 struct connection_settings_header *hdr =
1313 (struct connection_settings_header *)buf;
1314 DWORD *len = (DWORD *)(hdr + 1);
1316 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1318 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1321 if (hdr->flags & PROXY_TYPE_PROXY)
1324 LPWSTR proxy = NULL;
1325 LPWSTR proxy_bypass = NULL;
1327 /* Sanity-check length of proxy string */
1328 if ((BYTE *)len - buf + *len <= size)
1331 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1333 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1334 len = (DWORD *)((BYTE *)(len + 1) + *len);
1338 /* Sanity-check length of proxy bypass string */
1339 if ((BYTE *)len - buf + *len <= size)
1341 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1343 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1348 GlobalFree( proxy );
1352 info->lpszProxy = proxy;
1353 info->lpszProxyBypass = proxy_bypass;
1356 got_from_reg = TRUE;
1358 info->dwAccessType =
1359 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1360 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1361 debugstr_w(info->lpszProxy),
1362 debugstr_w(info->lpszProxyBypass));
1371 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1373 char *colon, *http_proxy;
1375 if ((colon = strchr( envproxy, ':' )))
1377 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1379 static const char http[] = "http://";
1381 /* It's a scheme, check that it's http */
1382 if (!strncmp( envproxy, http, strlen( http ) ))
1383 http_proxy = envproxy + strlen( http );
1386 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1391 http_proxy = envproxy;
1394 http_proxy = envproxy;
1400 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1401 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1403 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1405 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1406 info->lpszProxy = http_proxyW;
1407 info->lpszProxyBypass = NULL;
1408 TRACE("http proxy (from environment) = %s\n",
1409 debugstr_w(info->lpszProxy));
1415 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1416 info->lpszProxy = NULL;
1417 info->lpszProxyBypass = NULL;
1422 /***********************************************************************
1423 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1425 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1427 static const WCHAR settingsW[] =
1428 {'D','e','f','a','u','l','t','C','o','n','n','e','c','t','i','o','n','S','e','t','t','i','n','g','s',0};
1430 struct connection_settings_header *hdr = NULL;
1431 DWORD type, offset, len, size = 0;
1434 TRACE("%p\n", config);
1438 set_last_error( ERROR_INVALID_PARAMETER );
1441 memset( config, 0, sizeof(*config) );
1442 config->fAutoDetect = TRUE;
1444 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1445 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1446 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1451 if (!(hdr = heap_alloc( size ))) goto done;
1452 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1453 hdr->magic != WININET_SETTINGS_MAGIC)
1459 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1460 offset = sizeof(*hdr);
1461 if (offset + sizeof(DWORD) > size) goto done;
1462 len = *(DWORD *)((char *)hdr + offset);
1463 offset += sizeof(DWORD);
1464 if (len && hdr->flags & PROXY_TYPE_PROXY)
1466 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1467 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1470 if (offset + sizeof(DWORD) > size) goto done;
1471 len = *(DWORD *)((char *)hdr + offset);
1472 offset += sizeof(DWORD);
1473 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1475 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1476 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1479 if (offset + sizeof(DWORD) > size) goto done;
1480 len = *(DWORD *)((char *)hdr + offset);
1481 offset += sizeof(DWORD);
1482 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1484 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1485 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1490 RegCloseKey( hkey );
1494 heap_free( config->lpszAutoConfigUrl );
1495 config->lpszAutoConfigUrl = NULL;
1496 heap_free( config->lpszProxy );
1497 config->lpszProxy = NULL;
1498 heap_free( config->lpszProxyBypass );
1499 config->lpszProxyBypass = NULL;
1504 static HRESULT WINAPI dispex_QueryInterface(
1505 IDispatchEx *iface, REFIID riid, void **ppv )
1509 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1510 IsEqualGUID( riid, &IID_IDispatch ) ||
1511 IsEqualGUID( riid, &IID_IDispatchEx ))
1514 return E_NOINTERFACE;
1519 static ULONG WINAPI dispex_AddRef(
1520 IDispatchEx *iface )
1525 static ULONG WINAPI dispex_Release(
1526 IDispatchEx *iface )
1531 static HRESULT WINAPI dispex_GetTypeInfoCount(
1532 IDispatchEx *iface, UINT *info )
1537 static HRESULT WINAPI dispex_GetTypeInfo(
1538 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1543 static HRESULT WINAPI dispex_GetIDsOfNames(
1544 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1549 static HRESULT WINAPI dispex_Invoke(
1550 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1551 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1556 static HRESULT WINAPI dispex_DeleteMemberByName(
1557 IDispatchEx *iface, BSTR name, DWORD flags )
1562 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1563 IDispatchEx *iface, DISPID id )
1568 static HRESULT WINAPI dispex_GetMemberProperties(
1569 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1574 static HRESULT WINAPI dispex_GetMemberName(
1575 IDispatchEx *iface, DISPID id, BSTR *name )
1580 static HRESULT WINAPI dispex_GetNextDispID(
1581 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1586 static HRESULT WINAPI dispex_GetNameSpaceParent(
1587 IDispatchEx *iface, IUnknown **unk )
1592 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1594 static HRESULT WINAPI dispex_GetDispID(
1595 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1597 if (!strcmpW( name, dns_resolveW ))
1599 *id = DISPID_GLOBAL_DNSRESOLVE;
1602 return DISP_E_UNKNOWNNAME;
1605 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1607 #ifdef HAVE_GETADDRINFO
1608 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1610 struct addrinfo *ai, *elem;
1615 hostnameA = strdupWA( hostname );
1617 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1619 if (!hostnameA) return E_OUTOFMEMORY;
1620 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1621 heap_free( hostnameA );
1622 if (res) return S_FALSE;
1625 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1631 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1633 V_VT( result ) = VT_BSTR;
1634 V_BSTR( result ) = SysAllocString( addr );
1637 FIXME("getaddrinfo not found at build time\n");
1642 static HRESULT WINAPI dispex_InvokeEx(
1643 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1644 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1646 if (id == DISPID_GLOBAL_DNSRESOLVE)
1648 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1649 if (V_VT(¶ms->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1650 return dns_resolve( V_BSTR(¶ms->rgvarg[0]), result );
1652 return DISP_E_MEMBERNOTFOUND;
1655 static const IDispatchExVtbl dispex_vtbl =
1657 dispex_QueryInterface,
1660 dispex_GetTypeInfoCount,
1662 dispex_GetIDsOfNames,
1666 dispex_DeleteMemberByName,
1667 dispex_DeleteMemberByDispID,
1668 dispex_GetMemberProperties,
1669 dispex_GetMemberName,
1670 dispex_GetNextDispID,
1671 dispex_GetNameSpaceParent
1674 static IDispatchEx global_dispex = { &dispex_vtbl };
1676 static HRESULT WINAPI site_QueryInterface(
1677 IActiveScriptSite *iface, REFIID riid, void **ppv )
1681 if (IsEqualGUID( &IID_IUnknown, riid ))
1683 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1686 return E_NOINTERFACE;
1688 IUnknown_AddRef( (IUnknown *)*ppv );
1692 static ULONG WINAPI site_AddRef(
1693 IActiveScriptSite *iface )
1698 static ULONG WINAPI site_Release(
1699 IActiveScriptSite *iface )
1704 static HRESULT WINAPI site_GetLCID(
1705 IActiveScriptSite *iface, LCID *lcid )
1710 static HRESULT WINAPI site_GetItemInfo(
1711 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1712 IUnknown **item, ITypeInfo **type_info )
1714 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1716 *item = (IUnknown *)&global_dispex;
1722 static HRESULT WINAPI site_GetDocVersionString(
1723 IActiveScriptSite *iface, BSTR *version )
1728 static HRESULT WINAPI site_OnScriptTerminate(
1729 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1734 static HRESULT WINAPI site_OnStateChange(
1735 IActiveScriptSite *iface, SCRIPTSTATE state )
1740 static HRESULT WINAPI site_OnScriptError(
1741 IActiveScriptSite *iface, IActiveScriptError *error )
1746 static HRESULT WINAPI site_OnEnterScript(
1747 IActiveScriptSite *iface )
1752 static HRESULT WINAPI site_OnLeaveScript(
1753 IActiveScriptSite *iface )
1758 static const IActiveScriptSiteVtbl site_vtbl =
1760 site_QueryInterface,
1765 site_GetDocVersionString,
1766 site_OnScriptTerminate,
1773 static IActiveScriptSite script_site = { &site_vtbl };
1775 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1777 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1782 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1783 info->lpszProxy = NULL;
1784 info->lpszProxyBypass = NULL;
1786 if (V_VT( &result ) != VT_BSTR) return TRUE;
1787 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1789 p = V_BSTR( &result );
1790 while (*p == ' ') p++;
1792 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1795 while (*p == ' ') p++;
1796 if (!*p || *p == ';') return TRUE;
1797 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1798 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1801 if (*q == ' ' || *q == ';')
1811 static BSTR include_pac_utils( BSTR script )
1813 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1814 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1821 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1822 size = SizeofResource( hmod, rsrc );
1823 data = LoadResource( hmod, rsrc );
1825 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1826 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1827 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1829 strcatW( ret, script );
1834 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1835 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1836 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1838 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1839 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1840 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1843 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1845 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1846 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1847 IActiveScriptParse *parser = NULL;
1848 IActiveScript *engine = NULL;
1849 IDispatch *dispatch = NULL;
1853 BSTR func = NULL, hostname = NULL, full_script = NULL;
1855 VARIANT args[2], result;
1859 memset( &uc, 0, sizeof(uc) );
1860 uc.dwStructSize = sizeof(uc);
1861 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1862 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1863 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1864 hostname[uc.dwHostNameLength] = 0;
1866 init = CoInitialize( NULL );
1867 hr = CLSIDFromProgID( jscriptW, &clsid );
1868 if (hr != S_OK) goto done;
1870 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1871 &IID_IActiveScript, (void **)&engine );
1872 if (hr != S_OK) goto done;
1874 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1875 if (hr != S_OK) goto done;
1877 hr = IActiveScriptParse_InitNew( parser );
1878 if (hr != S_OK) goto done;
1880 hr = IActiveScript_SetScriptSite( engine, &script_site );
1881 if (hr != S_OK) goto done;
1883 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1884 if (hr != S_OK) goto done;
1886 if (!(full_script = include_pac_utils( script ))) goto done;
1888 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1889 if (hr != S_OK) goto done;
1891 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1892 if (hr != S_OK) goto done;
1894 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1895 if (hr != S_OK) goto done;
1897 if (!(func = SysAllocString( findproxyW ))) goto done;
1898 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1899 if (hr != S_OK) goto done;
1901 V_VT( &args[0] ) = VT_BSTR;
1902 V_BSTR( &args[0] ) = hostname;
1903 V_VT( &args[1] ) = VT_BSTR;
1904 V_BSTR( &args[1] ) = SysAllocString( url );
1906 params.rgvarg = args;
1907 params.rgdispidNamedArgs = NULL;
1909 params.cNamedArgs = 0;
1910 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1911 ¶ms, &result, NULL, NULL );
1912 VariantClear( &args[1] );
1915 WARN("script failed 0x%08x\n", hr);
1918 ret = parse_script_result( result, info );
1921 SysFreeString( full_script );
1922 SysFreeString( hostname );
1923 SysFreeString( func );
1924 if (dispatch) IDispatch_Release( dispatch );
1925 if (parser) IActiveScriptParse_Release( parser );
1926 if (engine) IActiveScript_Release( engine );
1927 if (SUCCEEDED( init )) CoUninitialize();
1928 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1932 static BSTR download_script( const WCHAR *url )
1934 static const WCHAR typeW[] = {'*','/','*',0};
1935 static const WCHAR *acceptW[] = {typeW, NULL};
1936 HINTERNET ses, con = NULL, req = NULL;
1939 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1940 char *tmp, *buffer = NULL;
1944 memset( &uc, 0, sizeof(uc) );
1945 uc.dwStructSize = sizeof(uc);
1946 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1947 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1948 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1949 hostname[uc.dwHostNameLength] = 0;
1951 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1952 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1953 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1954 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1955 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1956 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1958 if (!(buffer = heap_alloc( size ))) goto done;
1963 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1964 if (!bytes_read) break;
1965 to_read -= bytes_read;
1966 offset += bytes_read;
1971 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1975 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1976 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1977 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1981 WinHttpCloseHandle( req );
1982 WinHttpCloseHandle( con );
1983 WinHttpCloseHandle( ses );
1984 heap_free( buffer );
1985 heap_free( hostname );
1986 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1990 /***********************************************************************
1991 * WinHttpGetProxyForUrl (winhttp.@)
1993 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1994 WINHTTP_PROXY_INFO *info )
1996 WCHAR *detected_pac_url = NULL;
1997 const WCHAR *pac_url;
2002 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
2004 if (!(session = (session_t *)grab_object( hsession )))
2006 set_last_error( ERROR_INVALID_HANDLE );
2009 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
2011 release_object( &session->hdr );
2012 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2015 if (!url || !options || !info ||
2016 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
2017 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
2018 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
2019 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
2021 release_object( &session->hdr );
2022 set_last_error( ERROR_INVALID_PARAMETER );
2025 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2026 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2028 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
2031 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2032 else pac_url = detected_pac_url;
2034 if (!(script = download_script( pac_url ))) goto done;
2035 ret = run_script( script, url, info );
2036 SysFreeString( script );
2039 GlobalFree( detected_pac_url );
2040 release_object( &session->hdr );
2044 /***********************************************************************
2045 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2047 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2054 TRACE("%p\n", info);
2058 set_last_error( ERROR_INVALID_PARAMETER );
2061 switch (info->dwAccessType)
2063 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2065 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2066 if (!info->lpszProxy)
2068 set_last_error( ERROR_INVALID_PARAMETER );
2071 /* Only ASCII characters are allowed */
2072 for (src = info->lpszProxy; *src; src++)
2075 set_last_error( ERROR_INVALID_PARAMETER );
2078 if (info->lpszProxyBypass)
2080 for (src = info->lpszProxyBypass; *src; src++)
2083 set_last_error( ERROR_INVALID_PARAMETER );
2089 set_last_error( ERROR_INVALID_PARAMETER );
2093 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2094 KEY_WRITE, NULL, &key, NULL );
2097 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2100 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2102 size += strlenW( info->lpszProxy );
2103 if (info->lpszProxyBypass)
2104 size += strlenW( info->lpszProxyBypass );
2106 buf = heap_alloc( size );
2109 struct connection_settings_header *hdr =
2110 (struct connection_settings_header *)buf;
2111 DWORD *len = (DWORD *)(hdr + 1);
2113 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2115 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2119 hdr->flags = PROXY_TYPE_PROXY;
2120 *len++ = strlenW( info->lpszProxy );
2121 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2125 if (info->lpszProxyBypass)
2127 *len++ = strlenW( info->lpszProxyBypass );
2128 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2137 hdr->flags = PROXY_TYPE_DIRECT;
2141 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2151 /***********************************************************************
2152 * WinHttpSetStatusCallback (winhttp.@)
2154 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2155 DWORD flags, DWORD_PTR reserved )
2157 object_header_t *hdr;
2158 WINHTTP_STATUS_CALLBACK ret;
2160 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2162 if (!(hdr = grab_object( handle )))
2164 set_last_error( ERROR_INVALID_HANDLE );
2165 return WINHTTP_INVALID_STATUS_CALLBACK;
2167 ret = hdr->callback;
2168 hdr->callback = callback;
2169 hdr->notify_mask = flags;
2171 release_object( hdr );
2175 /***********************************************************************
2176 * WinHttpSetTimeouts (winhttp.@)
2178 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2181 object_header_t *hdr;
2185 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2187 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2189 set_last_error( ERROR_INVALID_PARAMETER );
2193 if (!(hdr = grab_object( handle )))
2195 set_last_error( ERROR_INVALID_HANDLE );
2201 case WINHTTP_HANDLE_TYPE_REQUEST:
2202 request = (request_t *)hdr;
2203 request->connect_timeout = connect;
2205 if (resolve < 0) resolve = 0;
2206 request->resolve_timeout = resolve;
2208 if (send < 0) send = 0;
2209 request->send_timeout = send;
2211 if (receive < 0) receive = 0;
2212 request->recv_timeout = receive;
2214 if (netconn_connected( &request->netconn ))
2216 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2217 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2220 release_object( &request->hdr );
2223 case WINHTTP_HANDLE_TYPE_SESSION:
2224 session = (session_t *)hdr;
2225 session->connect_timeout = connect;
2227 if (resolve < 0) resolve = 0;
2228 session->resolve_timeout = resolve;
2230 if (send < 0) send = 0;
2231 session->send_timeout = send;
2233 if (receive < 0) receive = 0;
2234 session->recv_timeout = receive;
2238 release_object( hdr );
2239 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2245 static const WCHAR wkday[7][4] =
2246 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2247 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2248 static const WCHAR month[12][4] =
2249 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2250 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2251 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2253 /***********************************************************************
2254 * WinHttpTimeFromSystemTime (WININET.@)
2256 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2258 static const WCHAR format[] =
2259 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2260 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2262 TRACE("%p, %p\n", time, string);
2264 if (!time || !string) return FALSE;
2266 sprintfW( string, format,
2267 wkday[time->wDayOfWeek],
2269 month[time->wMonth - 1],
2278 /***********************************************************************
2279 * WinHttpTimeToSystemTime (WININET.@)
2281 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2284 const WCHAR *s = string;
2287 TRACE("%s, %p\n", debugstr_w(string), time);
2289 if (!string || !time) return FALSE;
2291 /* Windows does this too */
2292 GetSystemTime( time );
2294 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2295 * a SYSTEMTIME structure.
2298 while (*s && !isalphaW( *s )) s++;
2299 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2300 time->wDayOfWeek = 7;
2302 for (i = 0; i < 7; i++)
2304 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2305 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2306 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2308 time->wDayOfWeek = i;
2313 if (time->wDayOfWeek > 6) return TRUE;
2314 while (*s && !isdigitW( *s )) s++;
2315 time->wDay = strtolW( s, &end, 10 );
2318 while (*s && !isalphaW( *s )) s++;
2319 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2322 for (i = 0; i < 12; i++)
2324 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2325 toupperW( month[i][1]) == toupperW( s[1] ) &&
2326 toupperW( month[i][2]) == toupperW( s[2] ) )
2328 time->wMonth = i + 1;
2332 if (time->wMonth == 0) return TRUE;
2334 while (*s && !isdigitW( *s )) s++;
2335 if (*s == '\0') return TRUE;
2336 time->wYear = strtolW( s, &end, 10 );
2339 while (*s && !isdigitW( *s )) s++;
2340 if (*s == '\0') return TRUE;
2341 time->wHour = strtolW( s, &end, 10 );
2344 while (*s && !isdigitW( *s )) s++;
2345 if (*s == '\0') return TRUE;
2346 time->wMinute = strtolW( s, &end, 10 );
2349 while (*s && !isdigitW( *s )) s++;
2350 if (*s == '\0') return TRUE;
2351 time->wSecond = strtolW( s, &end, 10 );
2353 time->wMilliseconds = 0;