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"
35 #include "winhttp_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
39 #define DEFAULT_RESOLVE_TIMEOUT 0
40 #define DEFAULT_CONNECT_TIMEOUT 20000
41 #define DEFAULT_SEND_TIMEOUT 30000
42 #define DEFAULT_RECEIVE_TIMEOUT 30000
44 void set_last_error( DWORD error )
47 SetLastError( error );
50 DWORD get_last_error( void )
53 return GetLastError();
56 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
58 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
60 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
63 /***********************************************************************
64 * WinHttpCheckPlatform (winhttp.@)
66 BOOL WINAPI WinHttpCheckPlatform( void )
72 /***********************************************************************
73 * session_destroy (internal)
75 static void session_destroy( object_header_t *hdr )
77 session_t *session = (session_t *)hdr;
78 struct list *item, *next;
81 TRACE("%p\n", session);
83 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
85 domain = LIST_ENTRY( item, domain_t, entry );
86 delete_domain( domain );
88 heap_free( session->agent );
89 heap_free( session->proxy_server );
90 heap_free( session->proxy_bypass );
91 heap_free( session->proxy_username );
92 heap_free( session->proxy_password );
96 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
98 session_t *session = (session_t *)hdr;
102 case WINHTTP_OPTION_REDIRECT_POLICY:
104 if (!buffer || *buflen < sizeof(DWORD))
106 *buflen = sizeof(DWORD);
107 set_last_error( ERROR_INSUFFICIENT_BUFFER );
111 *(DWORD *)buffer = hdr->redirect_policy;
112 *buflen = sizeof(DWORD);
115 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
116 *(DWORD *)buffer = session->resolve_timeout;
117 *buflen = sizeof(DWORD);
119 case WINHTTP_OPTION_CONNECT_TIMEOUT:
120 *(DWORD *)buffer = session->connect_timeout;
121 *buflen = sizeof(DWORD);
123 case WINHTTP_OPTION_SEND_TIMEOUT:
124 *(DWORD *)buffer = session->send_timeout;
125 *buflen = sizeof(DWORD);
127 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
128 *(DWORD *)buffer = session->recv_timeout;
129 *buflen = sizeof(DWORD);
132 FIXME("unimplemented option %u\n", option);
133 set_last_error( ERROR_INVALID_PARAMETER );
138 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
140 session_t *session = (session_t *)hdr;
144 case WINHTTP_OPTION_PROXY:
146 WINHTTP_PROXY_INFO *pi = buffer;
148 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
151 case WINHTTP_OPTION_REDIRECT_POLICY:
155 if (buflen != sizeof(policy))
157 set_last_error( ERROR_INSUFFICIENT_BUFFER );
161 policy = *(DWORD *)buffer;
162 TRACE("0x%x\n", policy);
163 hdr->redirect_policy = policy;
166 case WINHTTP_OPTION_DISABLE_FEATURE:
167 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
169 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
170 session->resolve_timeout = *(DWORD *)buffer;
172 case WINHTTP_OPTION_CONNECT_TIMEOUT:
173 session->connect_timeout = *(DWORD *)buffer;
175 case WINHTTP_OPTION_SEND_TIMEOUT:
176 session->send_timeout = *(DWORD *)buffer;
178 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
179 session->recv_timeout = *(DWORD *)buffer;
182 FIXME("unimplemented option %u\n", option);
183 set_last_error( ERROR_INVALID_PARAMETER );
188 static const object_vtbl_t session_vtbl =
191 session_query_option,
195 /***********************************************************************
196 * WinHttpOpen (winhttp.@)
198 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
201 HINTERNET handle = NULL;
203 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
205 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
207 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
208 session->hdr.vtbl = &session_vtbl;
209 session->hdr.flags = flags;
210 session->hdr.refs = 1;
211 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
212 list_init( &session->hdr.children );
213 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
214 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
215 session->send_timeout = DEFAULT_SEND_TIMEOUT;
216 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
217 list_init( &session->cookie_cache );
219 if (agent && !(session->agent = strdupW( agent ))) goto end;
220 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
222 WINHTTP_PROXY_INFO info;
224 WinHttpGetDefaultProxyConfiguration( &info );
225 session->access = info.dwAccessType;
226 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
228 GlobalFree( (LPWSTR)info.lpszProxy );
229 GlobalFree( (LPWSTR)info.lpszProxyBypass );
232 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
234 GlobalFree( (LPWSTR)info.lpszProxy );
235 GlobalFree( (LPWSTR)info.lpszProxyBypass );
239 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
241 session->access = access;
242 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
243 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
246 if (!(handle = alloc_handle( &session->hdr ))) goto end;
247 session->hdr.handle = handle;
250 release_object( &session->hdr );
251 TRACE("returning %p\n", handle);
255 /***********************************************************************
256 * connect_destroy (internal)
258 static void connect_destroy( object_header_t *hdr )
260 connect_t *connect = (connect_t *)hdr;
262 TRACE("%p\n", connect);
264 release_object( &connect->session->hdr );
266 heap_free( connect->hostname );
267 heap_free( connect->servername );
268 heap_free( connect->username );
269 heap_free( connect->password );
270 heap_free( connect );
273 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
275 connect_t *connect = (connect_t *)hdr;
279 case WINHTTP_OPTION_PARENT_HANDLE:
281 if (!buffer || *buflen < sizeof(HINTERNET))
283 *buflen = sizeof(HINTERNET);
284 set_last_error( ERROR_INSUFFICIENT_BUFFER );
288 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
289 *buflen = sizeof(HINTERNET);
292 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
293 *(DWORD *)buffer = connect->session->resolve_timeout;
294 *buflen = sizeof(DWORD);
296 case WINHTTP_OPTION_CONNECT_TIMEOUT:
297 *(DWORD *)buffer = connect->session->connect_timeout;
298 *buflen = sizeof(DWORD);
300 case WINHTTP_OPTION_SEND_TIMEOUT:
301 *(DWORD *)buffer = connect->session->send_timeout;
302 *buflen = sizeof(DWORD);
304 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
305 *(DWORD *)buffer = connect->session->recv_timeout;
306 *buflen = sizeof(DWORD);
309 FIXME("unimplemented option %u\n", option);
310 set_last_error( ERROR_INVALID_PARAMETER );
315 static const object_vtbl_t connect_vtbl =
318 connect_query_option,
322 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
324 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
327 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
329 else if (*domain == '*')
331 if (domain[1] == '.')
335 /* For a hostname to match a wildcard, the last domain must match
336 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
337 * hostname is www.foo.a.b, it matches, but a.b does not.
339 dot = strchrW( server, '.' );
342 int len = strlenW( dot + 1 );
344 if (len > strlenW( domain + 2 ))
348 /* The server's domain is longer than the wildcard, so it
349 * could be a subdomain. Compare the last portion of the
352 ptr = dot + len + 1 - strlenW( domain + 2 );
353 if (!strcmpiW( ptr, domain + 2 ))
355 /* This is only a match if the preceding character is
356 * a '.', i.e. that it is a matching domain. E.g.
357 * if domain is '*.b.c' and server is 'www.ab.c' they
360 ret = *(ptr - 1) == '.';
364 ret = !strcmpiW( dot + 1, domain + 2 );
369 ret = !strcmpiW( server, domain );
373 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
374 #define MAX_HOST_NAME_LENGTH 256
376 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
381 if (!session->proxy_bypass) return FALSE;
382 ptr = session->proxy_bypass;
386 ptr = strchrW( ptr, ';' );
388 ptr = strchrW( tmp, ' ' );
391 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
393 WCHAR domain[MAX_HOST_NAME_LENGTH];
395 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
396 domain[ptr - tmp] = 0;
397 ret = domain_matches( server, domain );
402 ret = domain_matches( server, tmp );
403 } while (ptr && !ret);
407 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
409 session_t *session = connect->session;
412 if (session->proxy_server && !should_bypass_proxy(session, server))
416 if ((colon = strchrW( session->proxy_server, ':' )))
418 if (!connect->servername || strncmpiW( connect->servername,
419 session->proxy_server, colon - session->proxy_server - 1 ))
421 heap_free( connect->servername );
422 if (!(connect->servername = heap_alloc(
423 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
428 memcpy( connect->servername, session->proxy_server,
429 (colon - session->proxy_server) * sizeof(WCHAR) );
430 connect->servername[colon - session->proxy_server] = 0;
432 connect->serverport = atoiW( colon + 1 );
434 connect->serverport = INTERNET_DEFAULT_PORT;
439 if (!connect->servername || strcmpiW( connect->servername,
440 session->proxy_server ))
442 heap_free( connect->servername );
443 if (!(connect->servername = strdupW( session->proxy_server )))
448 connect->serverport = INTERNET_DEFAULT_PORT;
454 heap_free( connect->servername );
455 if (!(connect->servername = strdupW( server )))
460 connect->serverport = port;
466 /***********************************************************************
467 * WinHttpConnect (winhttp.@)
469 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
473 HINTERNET hconnect = NULL;
475 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
479 set_last_error( ERROR_INVALID_PARAMETER );
482 if (!(session = (session_t *)grab_object( hsession )))
484 set_last_error( ERROR_INVALID_HANDLE );
487 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
489 release_object( &session->hdr );
490 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
493 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
495 release_object( &session->hdr );
498 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
499 connect->hdr.vtbl = &connect_vtbl;
500 connect->hdr.refs = 1;
501 connect->hdr.flags = session->hdr.flags;
502 connect->hdr.callback = session->hdr.callback;
503 connect->hdr.notify_mask = session->hdr.notify_mask;
504 connect->hdr.context = session->hdr.context;
505 list_init( &connect->hdr.children );
507 addref_object( &session->hdr );
508 connect->session = session;
509 list_add_head( &session->hdr.children, &connect->hdr.entry );
511 if (!(connect->hostname = strdupW( server ))) goto end;
512 connect->hostport = port;
513 if (!set_server_for_hostname( connect, server, port )) goto end;
515 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
516 connect->hdr.handle = hconnect;
518 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
521 release_object( &connect->hdr );
522 release_object( &session->hdr );
523 TRACE("returning %p\n", hconnect);
527 /***********************************************************************
528 * request_destroy (internal)
530 static void request_destroy( object_header_t *hdr )
532 request_t *request = (request_t *)hdr;
535 TRACE("%p\n", request);
537 release_object( &request->connect->hdr );
539 heap_free( request->verb );
540 heap_free( request->path );
541 heap_free( request->version );
542 heap_free( request->raw_headers );
543 heap_free( request->status_text );
544 for (i = 0; i < request->num_headers; i++)
546 heap_free( request->headers[i].field );
547 heap_free( request->headers[i].value );
549 heap_free( request->headers );
550 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
551 heap_free( request->accept_types );
552 heap_free( request );
555 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
558 if (str) len = strlenW( str );
559 if (buffer && *buflen > len)
561 memcpy( buffer, str, len * sizeof(WCHAR) );
564 *buflen = len * sizeof(WCHAR);
567 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
570 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
572 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
573 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
574 CertNameToStrW( encoding, blob, format, ret, size );
579 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
581 request_t *request = (request_t *)hdr;
585 case WINHTTP_OPTION_SECURITY_FLAGS:
590 if (!buffer || *buflen < sizeof(flags))
592 *buflen = sizeof(flags);
593 set_last_error( ERROR_INSUFFICIENT_BUFFER );
598 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
599 flags |= request->netconn.security_flags;
600 bits = netconn_get_cipher_strength( &request->netconn );
602 flags |= SECURITY_FLAG_STRENGTH_STRONG;
604 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
606 flags |= SECURITY_FLAG_STRENGTH_WEAK;
607 *(DWORD *)buffer = flags;
608 *buflen = sizeof(flags);
611 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
613 const CERT_CONTEXT *cert;
615 if (!buffer || *buflen < sizeof(cert))
617 *buflen = sizeof(cert);
618 set_last_error( ERROR_INSUFFICIENT_BUFFER );
622 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
623 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
624 *buflen = sizeof(cert);
627 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
629 const CERT_CONTEXT *cert;
630 const CRYPT_OID_INFO *oidInfo;
631 WINHTTP_CERTIFICATE_INFO *ci = buffer;
633 FIXME("partial stub\n");
635 if (!buffer || *buflen < sizeof(*ci))
637 *buflen = sizeof(*ci);
638 set_last_error( ERROR_INSUFFICIENT_BUFFER );
641 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
643 ci->ftExpiry = cert->pCertInfo->NotAfter;
644 ci->ftStart = cert->pCertInfo->NotBefore;
645 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
646 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
647 ci->lpszProtocolName = NULL;
648 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
649 cert->pCertInfo->SignatureAlgorithm.pszObjId,
652 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
654 ci->lpszSignatureAlgName = NULL;
655 ci->lpszEncryptionAlgName = NULL;
656 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
658 CertFreeCertificateContext( cert );
659 *buflen = sizeof(*ci);
662 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
664 if (!buffer || *buflen < sizeof(DWORD))
666 *buflen = sizeof(DWORD);
667 set_last_error( ERROR_INSUFFICIENT_BUFFER );
671 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
672 *buflen = sizeof(DWORD);
675 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
676 *(DWORD *)buffer = request->resolve_timeout;
677 *buflen = sizeof(DWORD);
679 case WINHTTP_OPTION_CONNECT_TIMEOUT:
680 *(DWORD *)buffer = request->connect_timeout;
681 *buflen = sizeof(DWORD);
683 case WINHTTP_OPTION_SEND_TIMEOUT:
684 *(DWORD *)buffer = request->send_timeout;
685 *buflen = sizeof(DWORD);
687 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
688 *(DWORD *)buffer = request->recv_timeout;
689 *buflen = sizeof(DWORD);
692 case WINHTTP_OPTION_USERNAME:
693 str_to_buffer( buffer, request->connect->username, buflen );
696 case WINHTTP_OPTION_PASSWORD:
697 str_to_buffer( buffer, request->connect->password, buflen );
700 case WINHTTP_OPTION_PROXY_USERNAME:
701 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
704 case WINHTTP_OPTION_PROXY_PASSWORD:
705 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
709 FIXME("unimplemented option %u\n", option);
710 set_last_error( ERROR_INVALID_PARAMETER );
715 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
718 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
720 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
724 set_last_error( ERROR_OUTOFMEMORY );
728 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
730 request_t *request = (request_t *)hdr;
734 case WINHTTP_OPTION_PROXY:
736 WINHTTP_PROXY_INFO *pi = buffer;
738 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
741 case WINHTTP_OPTION_DISABLE_FEATURE:
745 if (buflen != sizeof(DWORD))
747 set_last_error( ERROR_INSUFFICIENT_BUFFER );
751 disable = *(DWORD *)buffer;
752 TRACE("0x%x\n", disable);
753 hdr->disable_flags |= disable;
756 case WINHTTP_OPTION_AUTOLOGON_POLICY:
760 if (buflen != sizeof(DWORD))
762 set_last_error( ERROR_INSUFFICIENT_BUFFER );
766 policy = *(DWORD *)buffer;
767 TRACE("0x%x\n", policy);
768 hdr->logon_policy = policy;
771 case WINHTTP_OPTION_REDIRECT_POLICY:
775 if (buflen != sizeof(DWORD))
777 set_last_error( ERROR_INSUFFICIENT_BUFFER );
781 policy = *(DWORD *)buffer;
782 TRACE("0x%x\n", policy);
783 hdr->redirect_policy = policy;
786 case WINHTTP_OPTION_SECURITY_FLAGS:
790 if (buflen < sizeof(DWORD))
792 set_last_error( ERROR_INSUFFICIENT_BUFFER );
795 flags = *(DWORD *)buffer;
796 TRACE("0x%x\n", flags);
797 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
798 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
799 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
800 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
802 set_last_error( ERROR_INVALID_PARAMETER );
805 request->netconn.security_flags = flags;
808 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
809 request->resolve_timeout = *(DWORD *)buffer;
811 case WINHTTP_OPTION_CONNECT_TIMEOUT:
812 request->connect_timeout = *(DWORD *)buffer;
814 case WINHTTP_OPTION_SEND_TIMEOUT:
815 request->send_timeout = *(DWORD *)buffer;
817 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
818 request->recv_timeout = *(DWORD *)buffer;
821 case WINHTTP_OPTION_USERNAME:
823 connect_t *connect = request->connect;
825 heap_free( connect->username );
826 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
829 case WINHTTP_OPTION_PASSWORD:
831 connect_t *connect = request->connect;
833 heap_free( connect->password );
834 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
837 case WINHTTP_OPTION_PROXY_USERNAME:
839 session_t *session = request->connect->session;
841 heap_free( session->proxy_username );
842 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
845 case WINHTTP_OPTION_PROXY_PASSWORD:
847 session_t *session = request->connect->session;
849 heap_free( session->proxy_password );
850 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
854 FIXME("unimplemented option %u\n", option);
855 set_last_error( ERROR_INVALID_PARAMETER );
860 static const object_vtbl_t request_vtbl =
863 request_query_option,
867 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
869 const WCHAR **types = accept_types;
872 if (!types) return TRUE;
875 request->num_accept_types++;
878 if (!request->num_accept_types) return TRUE;
879 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
881 request->num_accept_types = 0;
884 types = accept_types;
885 for (i = 0; i < request->num_accept_types; i++)
887 if (!(request->accept_types[i] = strdupW( *types )))
889 for (; i >= 0; i--) heap_free( request->accept_types[i] );
890 heap_free( request->accept_types );
891 request->accept_types = NULL;
892 request->num_accept_types = 0;
900 /***********************************************************************
901 * WinHttpOpenRequest (winhttp.@)
903 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
904 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
908 HINTERNET hrequest = NULL;
910 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
911 debugstr_w(version), debugstr_w(referrer), types, flags);
913 if (!(connect = (connect_t *)grab_object( hconnect )))
915 set_last_error( ERROR_INVALID_HANDLE );
918 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
920 release_object( &connect->hdr );
921 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
924 if (!(request = heap_alloc_zero( sizeof(request_t) )))
926 release_object( &connect->hdr );
929 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
930 request->hdr.vtbl = &request_vtbl;
931 request->hdr.refs = 1;
932 request->hdr.flags = flags;
933 request->hdr.callback = connect->hdr.callback;
934 request->hdr.notify_mask = connect->hdr.notify_mask;
935 request->hdr.context = connect->hdr.context;
936 list_init( &request->hdr.children );
938 addref_object( &connect->hdr );
939 request->connect = connect;
940 list_add_head( &connect->hdr.children, &request->hdr.entry );
942 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
943 request->resolve_timeout = connect->session->resolve_timeout;
944 request->connect_timeout = connect->session->connect_timeout;
945 request->send_timeout = connect->session->send_timeout;
946 request->recv_timeout = connect->session->recv_timeout;
948 if (!verb || !verb[0]) verb = getW;
949 if (!(request->verb = strdupW( verb ))) goto end;
956 len = strlenW( object ) + 1;
957 if (object[0] != '/') len++;
958 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
960 if (object[0] != '/') *p++ = '/';
961 strcpyW( p, object );
962 request->path = path;
964 else if (!(request->path = strdupW( slashW ))) goto end;
966 if (!version || !version[0]) version = http1_1;
967 if (!(request->version = strdupW( version ))) goto end;
968 if (!(store_accept_types( request, types ))) goto end;
970 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
971 request->hdr.handle = hrequest;
973 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
976 release_object( &request->hdr );
977 release_object( &connect->hdr );
978 TRACE("returning %p\n", hrequest);
982 /***********************************************************************
983 * WinHttpCloseHandle (winhttp.@)
985 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
987 object_header_t *hdr;
989 TRACE("%p\n", handle);
991 if (!(hdr = grab_object( handle )))
993 set_last_error( ERROR_INVALID_HANDLE );
996 release_object( hdr );
997 free_handle( handle );
1001 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1007 set_last_error( ERROR_INVALID_PARAMETER );
1013 case WINHTTP_OPTION_CONTEXT_VALUE:
1015 if (!buffer || *buflen < sizeof(DWORD_PTR))
1017 *buflen = sizeof(DWORD_PTR);
1018 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1022 *(DWORD_PTR *)buffer = hdr->context;
1023 *buflen = sizeof(DWORD_PTR);
1027 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1030 FIXME("unimplemented option %u\n", option);
1031 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1039 /***********************************************************************
1040 * WinHttpQueryOption (winhttp.@)
1042 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1045 object_header_t *hdr;
1047 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1049 if (!(hdr = grab_object( handle )))
1051 set_last_error( ERROR_INVALID_HANDLE );
1055 ret = query_option( hdr, option, buffer, buflen );
1057 release_object( hdr );
1061 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1067 set_last_error( ERROR_INVALID_PARAMETER );
1073 case WINHTTP_OPTION_CONTEXT_VALUE:
1075 if (buflen != sizeof(DWORD_PTR))
1077 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1081 hdr->context = *(DWORD_PTR *)buffer;
1085 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1088 FIXME("unimplemented option %u\n", option);
1089 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1097 /***********************************************************************
1098 * WinHttpSetOption (winhttp.@)
1100 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1103 object_header_t *hdr;
1105 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1107 if (!(hdr = grab_object( handle )))
1109 set_last_error( ERROR_INVALID_HANDLE );
1113 ret = set_option( hdr, option, buffer, buflen );
1115 release_object( hdr );
1119 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1124 GetComputerNameExA( format, NULL, &size );
1125 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1126 if (!(ret = heap_alloc( size ))) return NULL;
1127 if (!GetComputerNameExA( format, ret, &size ))
1135 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1137 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1139 if (len_suffix > len_domain) return FALSE;
1140 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1144 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1146 static const WCHAR fmtW[] =
1147 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1148 '/','w','p','a','d','.','d','a','t',0};
1149 struct sockaddr_in *addr;
1152 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1153 if (!ai) return NULL;
1155 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1156 addr = (struct sockaddr_in *)ai->ai_addr;
1157 sprintfW( ret, fmtW,
1158 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1159 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1160 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1161 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1165 /***********************************************************************
1166 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1168 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1172 TRACE("0x%08x, %p\n", flags, url);
1176 set_last_error( ERROR_INVALID_PARAMETER );
1179 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP) FIXME("discovery via DHCP not supported\n");
1180 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1182 #ifdef HAVE_GETADDRINFO
1183 char *fqdn, *domain, *p;
1185 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1186 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1192 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1194 struct addrinfo *ai;
1198 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1201 heap_free( domain );
1204 strcpy( name, "wpad" );
1206 res = getaddrinfo( name, NULL, NULL, &ai );
1210 *url = build_wpad_url( ai );
1214 TRACE("returning %s\n", debugstr_w(*url));
1221 heap_free( domain );
1224 FIXME("getaddrinfo not found at build time\n");
1227 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1231 static const WCHAR Connections[] = {
1232 'S','o','f','t','w','a','r','e','\\',
1233 'M','i','c','r','o','s','o','f','t','\\',
1234 'W','i','n','d','o','w','s','\\',
1235 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1236 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1237 'C','o','n','n','e','c','t','i','o','n','s',0 };
1238 static const WCHAR WinHttpSettings[] = {
1239 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1240 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1241 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1242 static const DWORD PROXY_TYPE_DIRECT = 1;
1243 static const DWORD PROXY_TYPE_PROXY = 2;
1244 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1245 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1247 struct connection_settings_header
1250 DWORD unknown; /* always zero? */
1251 DWORD flags; /* one or more of PROXY_* */
1254 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1258 for (begin = src; src - begin < len; src++, dst++)
1263 /***********************************************************************
1264 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1266 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1270 BOOL got_from_reg = FALSE, direct = TRUE;
1273 TRACE("%p\n", info);
1275 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1278 DWORD type, size = 0;
1280 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1281 if (!l && type == REG_BINARY &&
1282 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1284 BYTE *buf = heap_alloc( size );
1288 struct connection_settings_header *hdr =
1289 (struct connection_settings_header *)buf;
1290 DWORD *len = (DWORD *)(hdr + 1);
1292 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1294 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1297 if (hdr->flags & PROXY_TYPE_PROXY)
1300 LPWSTR proxy = NULL;
1301 LPWSTR proxy_bypass = NULL;
1303 /* Sanity-check length of proxy string */
1304 if ((BYTE *)len - buf + *len <= size)
1307 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1309 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1310 len = (DWORD *)((BYTE *)(len + 1) + *len);
1314 /* Sanity-check length of proxy bypass string */
1315 if ((BYTE *)len - buf + *len <= size)
1317 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1319 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1324 GlobalFree( proxy );
1328 info->lpszProxy = proxy;
1329 info->lpszProxyBypass = proxy_bypass;
1332 got_from_reg = TRUE;
1334 info->dwAccessType =
1335 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1336 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1337 debugstr_w(info->lpszProxy),
1338 debugstr_w(info->lpszProxyBypass));
1347 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1349 char *colon, *http_proxy;
1351 if ((colon = strchr( envproxy, ':' )))
1353 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1355 static const char http[] = "http://";
1357 /* It's a scheme, check that it's http */
1358 if (!strncmp( envproxy, http, strlen( http ) ))
1359 http_proxy = envproxy + strlen( http );
1362 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1367 http_proxy = envproxy;
1370 http_proxy = envproxy;
1376 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1377 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1379 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1381 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1382 info->lpszProxy = http_proxyW;
1383 info->lpszProxyBypass = NULL;
1384 TRACE("http proxy (from environment) = %s\n",
1385 debugstr_w(info->lpszProxy));
1391 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1392 info->lpszProxy = NULL;
1393 info->lpszProxyBypass = NULL;
1398 /***********************************************************************
1399 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1401 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1403 static const WCHAR settingsW[] =
1404 {'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};
1406 struct connection_settings_header *hdr = NULL;
1407 DWORD type, offset, len, size = 0;
1410 TRACE("%p\n", config);
1414 set_last_error( ERROR_INVALID_PARAMETER );
1417 memset( config, 0, sizeof(*config) );
1418 config->fAutoDetect = TRUE;
1420 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1421 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1422 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1427 if (!(hdr = heap_alloc( size ))) goto done;
1428 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1429 hdr->magic != WININET_SETTINGS_MAGIC)
1435 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1436 offset = sizeof(*hdr);
1437 if (offset + sizeof(DWORD) > size) goto done;
1438 len = *(DWORD *)((char *)hdr + offset);
1439 offset += sizeof(DWORD);
1440 if (len && hdr->flags & PROXY_TYPE_PROXY)
1442 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1443 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1446 if (offset + sizeof(DWORD) > size) goto done;
1447 len = *(DWORD *)((char *)hdr + offset);
1448 offset += sizeof(DWORD);
1449 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1451 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1452 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1455 if (offset + sizeof(DWORD) > size) goto done;
1456 len = *(DWORD *)((char *)hdr + offset);
1457 offset += sizeof(DWORD);
1458 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1460 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1461 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1466 RegCloseKey( hkey );
1470 heap_free( config->lpszAutoConfigUrl );
1471 config->lpszAutoConfigUrl = NULL;
1472 heap_free( config->lpszProxy );
1473 config->lpszProxy = NULL;
1474 heap_free( config->lpszProxyBypass );
1475 config->lpszProxyBypass = NULL;
1480 static HRESULT WINAPI site_QueryInterface(
1481 IActiveScriptSite *iface, REFIID riid, void **ppv )
1485 if (IsEqualGUID( &IID_IUnknown, riid ))
1487 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1490 return E_NOINTERFACE;
1492 IUnknown_AddRef( (IUnknown *)*ppv );
1496 static ULONG WINAPI site_AddRef(
1497 IActiveScriptSite *iface )
1502 static ULONG WINAPI site_Release(
1503 IActiveScriptSite *iface )
1508 static HRESULT WINAPI site_GetLCID(
1509 IActiveScriptSite *iface, LCID *lcid )
1514 static HRESULT WINAPI site_GetItemInfo(
1515 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1516 IUnknown **item, ITypeInfo **type_info )
1521 static HRESULT WINAPI site_GetDocVersionString(
1522 IActiveScriptSite *iface, BSTR *version )
1527 static HRESULT WINAPI site_OnScriptTerminate(
1528 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1533 static HRESULT WINAPI site_OnStateChange(
1534 IActiveScriptSite *iface, SCRIPTSTATE state )
1539 static HRESULT WINAPI site_OnScriptError(
1540 IActiveScriptSite *iface, IActiveScriptError *error )
1545 static HRESULT WINAPI site_OnEnterScript(
1546 IActiveScriptSite *iface )
1551 static HRESULT WINAPI site_OnLeaveScript(
1552 IActiveScriptSite *iface )
1557 static const IActiveScriptSiteVtbl site_vtbl =
1559 site_QueryInterface,
1564 site_GetDocVersionString,
1565 site_OnScriptTerminate,
1572 static IActiveScriptSite script_site = { &site_vtbl };
1574 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1576 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1581 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1582 info->lpszProxy = NULL;
1583 info->lpszProxyBypass = NULL;
1585 if (V_VT( &result ) != VT_BSTR) return TRUE;
1586 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1588 p = V_BSTR( &result );
1589 while (*p == ' ') p++;
1591 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1594 while (*p == ' ') p++;
1595 if (!*p || *p == ';') return TRUE;
1596 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1597 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1600 if (*q == ' ' || *q == ';')
1610 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1612 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1613 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1614 IActiveScriptParse *parser = NULL;
1615 IActiveScript *engine = NULL;
1616 IDispatch *dispatch = NULL;
1620 BSTR func = NULL, hostname = NULL;
1622 VARIANT args[2], result;
1626 memset( &uc, 0, sizeof(uc) );
1627 uc.dwStructSize = sizeof(uc);
1628 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1629 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1630 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1631 hostname[uc.dwHostNameLength] = 0;
1633 init = CoInitialize( NULL );
1634 CLSIDFromProgID( jscriptW, &clsid );
1635 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1636 &IID_IActiveScript, (void **)&engine );
1637 if (hr != S_OK) goto done;
1639 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1640 if (hr != S_OK) goto done;
1642 hr = IActiveScriptParse64_InitNew( parser );
1643 if (hr != S_OK) goto done;
1645 hr = IActiveScript_SetScriptSite( engine, &script_site );
1646 if (hr != S_OK) goto done;
1648 /* FIXME: make standard functions available to script */
1650 hr = IActiveScriptParse64_ParseScriptText( parser, script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1651 if (hr != S_OK) goto done;
1653 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1654 if (hr != S_OK) goto done;
1656 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1657 if (hr != S_OK) goto done;
1659 if (!(func = SysAllocString( findproxyW ))) goto done;
1660 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1661 if (hr != S_OK) goto done;
1663 V_VT( &args[0] ) = VT_BSTR;
1664 V_BSTR( &args[0] ) = hostname;
1665 V_VT( &args[1] ) = VT_BSTR;
1666 V_BSTR( &args[1] ) = SysAllocString( url );
1668 params.rgvarg = args;
1669 params.rgdispidNamedArgs = NULL;
1671 params.cNamedArgs = 0;
1672 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1673 ¶ms, &result, NULL, NULL );
1674 VariantClear( &args[0] );
1675 VariantClear( &args[1] );
1676 if (hr != S_OK) goto done;
1678 ret = parse_script_result( result, info );
1681 SysFreeString( func );
1682 if (dispatch) IDispatch_Release( dispatch );
1683 if (parser) IUnknown_Release( parser );
1684 if (engine) IActiveScript_Release( engine );
1685 if (SUCCEEDED( init )) CoUninitialize();
1686 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1690 static BSTR download_script( HINTERNET ses, const WCHAR *url )
1692 static const WCHAR typeW[] = {'*','/','*',0};
1693 static const WCHAR *acceptW[] = {typeW, NULL};
1694 HINTERNET con, req = NULL;
1697 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1698 char *tmp, *buffer = NULL;
1702 memset( &uc, 0, sizeof(uc) );
1703 uc.dwStructSize = sizeof(uc);
1704 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1705 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1706 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1707 hostname[uc.dwHostNameLength] = 0;
1709 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1710 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1711 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1712 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1713 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1715 if (!(buffer = heap_alloc( size ))) goto done;
1720 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1721 if (!bytes_read) break;
1722 to_read -= bytes_read;
1723 offset += bytes_read;
1728 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1732 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1733 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1734 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1738 WinHttpCloseHandle( req );
1739 WinHttpCloseHandle( con );
1740 heap_free( buffer );
1741 heap_free( hostname );
1742 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1746 /***********************************************************************
1747 * WinHttpGetProxyForUrl (winhttp.@)
1749 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1750 WINHTTP_PROXY_INFO *info )
1752 WCHAR *detected_pac_url = NULL;
1753 const WCHAR *pac_url;
1758 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1760 if (!(session = (session_t *)grab_object( hsession )))
1762 set_last_error( ERROR_INVALID_HANDLE );
1765 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1767 release_object( &session->hdr );
1768 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1771 if (!url || !options || !info ||
1772 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1773 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1774 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1775 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1777 release_object( &session->hdr );
1778 set_last_error( ERROR_INVALID_PARAMETER );
1781 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1782 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1784 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1787 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1788 else pac_url = detected_pac_url;
1790 if (!(script = download_script( hsession, pac_url ))) goto done;
1791 ret = run_script( script, url, info );
1792 SysFreeString( script );
1795 GlobalFree( detected_pac_url );
1796 release_object( &session->hdr );
1800 /***********************************************************************
1801 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1803 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1810 TRACE("%p\n", info);
1814 set_last_error( ERROR_INVALID_PARAMETER );
1817 switch (info->dwAccessType)
1819 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1821 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1822 if (!info->lpszProxy)
1824 set_last_error( ERROR_INVALID_PARAMETER );
1827 /* Only ASCII characters are allowed */
1828 for (src = info->lpszProxy; *src; src++)
1831 set_last_error( ERROR_INVALID_PARAMETER );
1834 if (info->lpszProxyBypass)
1836 for (src = info->lpszProxyBypass; *src; src++)
1839 set_last_error( ERROR_INVALID_PARAMETER );
1845 set_last_error( ERROR_INVALID_PARAMETER );
1849 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1850 KEY_WRITE, NULL, &key, NULL );
1853 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1856 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1858 size += strlenW( info->lpszProxy );
1859 if (info->lpszProxyBypass)
1860 size += strlenW( info->lpszProxyBypass );
1862 buf = heap_alloc( size );
1865 struct connection_settings_header *hdr =
1866 (struct connection_settings_header *)buf;
1867 DWORD *len = (DWORD *)(hdr + 1);
1869 hdr->magic = WINHTTP_SETTINGS_MAGIC;
1871 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1875 hdr->flags = PROXY_TYPE_PROXY;
1876 *len++ = strlenW( info->lpszProxy );
1877 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1881 if (info->lpszProxyBypass)
1883 *len++ = strlenW( info->lpszProxyBypass );
1884 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1893 hdr->flags = PROXY_TYPE_DIRECT;
1897 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1907 /***********************************************************************
1908 * WinHttpSetStatusCallback (winhttp.@)
1910 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1911 DWORD flags, DWORD_PTR reserved )
1913 object_header_t *hdr;
1914 WINHTTP_STATUS_CALLBACK ret;
1916 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1918 if (!(hdr = grab_object( handle )))
1920 set_last_error( ERROR_INVALID_HANDLE );
1921 return WINHTTP_INVALID_STATUS_CALLBACK;
1923 ret = hdr->callback;
1924 hdr->callback = callback;
1925 hdr->notify_mask = flags;
1927 release_object( hdr );
1931 /***********************************************************************
1932 * WinHttpSetTimeouts (winhttp.@)
1934 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1937 object_header_t *hdr;
1941 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1943 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1945 set_last_error( ERROR_INVALID_PARAMETER );
1949 if (!(hdr = grab_object( handle )))
1951 set_last_error( ERROR_INVALID_HANDLE );
1957 case WINHTTP_HANDLE_TYPE_REQUEST:
1958 request = (request_t *)hdr;
1959 request->connect_timeout = connect;
1961 if (resolve < 0) resolve = 0;
1962 request->resolve_timeout = resolve;
1964 if (send < 0) send = 0;
1965 request->send_timeout = send;
1967 if (receive < 0) receive = 0;
1968 request->recv_timeout = receive;
1970 if (netconn_connected( &request->netconn ))
1972 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1973 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1976 release_object( &request->hdr );
1979 case WINHTTP_HANDLE_TYPE_SESSION:
1980 session = (session_t *)hdr;
1981 session->connect_timeout = connect;
1983 if (resolve < 0) resolve = 0;
1984 session->resolve_timeout = resolve;
1986 if (send < 0) send = 0;
1987 session->send_timeout = send;
1989 if (receive < 0) receive = 0;
1990 session->recv_timeout = receive;
1994 release_object( hdr );
1995 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2001 static const WCHAR wkday[7][4] =
2002 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2003 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2004 static const WCHAR month[12][4] =
2005 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2006 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2007 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2009 /***********************************************************************
2010 * WinHttpTimeFromSystemTime (WININET.@)
2012 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2014 static const WCHAR format[] =
2015 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2016 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2018 TRACE("%p, %p\n", time, string);
2020 if (!time || !string) return FALSE;
2022 sprintfW( string, format,
2023 wkday[time->wDayOfWeek],
2025 month[time->wMonth - 1],
2034 /***********************************************************************
2035 * WinHttpTimeToSystemTime (WININET.@)
2037 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2040 const WCHAR *s = string;
2043 TRACE("%s, %p\n", debugstr_w(string), time);
2045 if (!string || !time) return FALSE;
2047 /* Windows does this too */
2048 GetSystemTime( time );
2050 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2051 * a SYSTEMTIME structure.
2054 while (*s && !isalphaW( *s )) s++;
2055 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2056 time->wDayOfWeek = 7;
2058 for (i = 0; i < 7; i++)
2060 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2061 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2062 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2064 time->wDayOfWeek = i;
2069 if (time->wDayOfWeek > 6) return TRUE;
2070 while (*s && !isdigitW( *s )) s++;
2071 time->wDay = strtolW( s, &end, 10 );
2074 while (*s && !isalphaW( *s )) s++;
2075 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2078 for (i = 0; i < 12; i++)
2080 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2081 toupperW( month[i][1]) == toupperW( s[1] ) &&
2082 toupperW( month[i][2]) == toupperW( s[2] ) )
2084 time->wMonth = i + 1;
2088 if (time->wMonth == 0) return TRUE;
2090 while (*s && !isdigitW( *s )) s++;
2091 if (*s == '\0') return TRUE;
2092 time->wYear = strtolW( s, &end, 10 );
2095 while (*s && !isdigitW( *s )) s++;
2096 if (*s == '\0') return TRUE;
2097 time->wHour = strtolW( s, &end, 10 );
2100 while (*s && !isdigitW( *s )) s++;
2101 if (*s == '\0') return TRUE;
2102 time->wMinute = strtolW( s, &end, 10 );
2105 while (*s && !isdigitW( *s )) s++;
2106 if (*s == '\0') return TRUE;
2107 time->wSecond = strtolW( s, &end, 10 );
2109 time->wMilliseconds = 0;