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"
41 #include "winhttp_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
45 #define DEFAULT_RESOLVE_TIMEOUT 0
46 #define DEFAULT_CONNECT_TIMEOUT 20000
47 #define DEFAULT_SEND_TIMEOUT 30000
48 #define DEFAULT_RECEIVE_TIMEOUT 30000
50 static const WCHAR global_funcsW
[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
51 static const WCHAR dns_resolveW
[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
53 void set_last_error( DWORD error
)
56 SetLastError( error
);
59 DWORD
get_last_error( void )
62 return GetLastError();
65 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
67 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
69 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
72 /***********************************************************************
73 * WinHttpCheckPlatform (winhttp.@)
75 BOOL WINAPI
WinHttpCheckPlatform( void )
81 /***********************************************************************
82 * session_destroy (internal)
84 static void session_destroy( object_header_t
*hdr
)
86 session_t
*session
= (session_t
*)hdr
;
87 struct list
*item
, *next
;
90 TRACE("%p\n", session
);
92 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
94 domain
= LIST_ENTRY( item
, domain_t
, entry
);
95 delete_domain( domain
);
97 heap_free( session
->agent
);
98 heap_free( session
->proxy_server
);
99 heap_free( session
->proxy_bypass
);
100 heap_free( session
->proxy_username
);
101 heap_free( session
->proxy_password
);
102 heap_free( session
);
105 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
107 session_t
*session
= (session_t
*)hdr
;
111 case WINHTTP_OPTION_REDIRECT_POLICY
:
113 if (!buffer
|| *buflen
< sizeof(DWORD
))
115 *buflen
= sizeof(DWORD
);
116 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
120 *(DWORD
*)buffer
= hdr
->redirect_policy
;
121 *buflen
= sizeof(DWORD
);
124 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
125 *(DWORD
*)buffer
= session
->resolve_timeout
;
126 *buflen
= sizeof(DWORD
);
128 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
129 *(DWORD
*)buffer
= session
->connect_timeout
;
130 *buflen
= sizeof(DWORD
);
132 case WINHTTP_OPTION_SEND_TIMEOUT
:
133 *(DWORD
*)buffer
= session
->send_timeout
;
134 *buflen
= sizeof(DWORD
);
136 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
137 *(DWORD
*)buffer
= session
->recv_timeout
;
138 *buflen
= sizeof(DWORD
);
141 FIXME("unimplemented option %u\n", option
);
142 set_last_error( ERROR_INVALID_PARAMETER
);
147 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
149 session_t
*session
= (session_t
*)hdr
;
153 case WINHTTP_OPTION_PROXY
:
155 WINHTTP_PROXY_INFO
*pi
= buffer
;
157 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
160 case WINHTTP_OPTION_REDIRECT_POLICY
:
164 if (buflen
!= sizeof(policy
))
166 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
170 policy
= *(DWORD
*)buffer
;
171 TRACE("0x%x\n", policy
);
172 hdr
->redirect_policy
= policy
;
175 case WINHTTP_OPTION_DISABLE_FEATURE
:
176 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
178 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
179 session
->resolve_timeout
= *(DWORD
*)buffer
;
181 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
182 session
->connect_timeout
= *(DWORD
*)buffer
;
184 case WINHTTP_OPTION_SEND_TIMEOUT
:
185 session
->send_timeout
= *(DWORD
*)buffer
;
187 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
188 session
->recv_timeout
= *(DWORD
*)buffer
;
190 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
191 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
194 FIXME("unimplemented option %u\n", option
);
195 set_last_error( ERROR_INVALID_PARAMETER
);
200 static const object_vtbl_t session_vtbl
=
203 session_query_option
,
207 /***********************************************************************
208 * WinHttpOpen (winhttp.@)
210 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
213 HINTERNET handle
= NULL
;
215 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
217 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
219 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
220 session
->hdr
.vtbl
= &session_vtbl
;
221 session
->hdr
.flags
= flags
;
222 session
->hdr
.refs
= 1;
223 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
224 list_init( &session
->hdr
.children
);
225 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
226 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
227 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
228 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
229 list_init( &session
->cookie_cache
);
231 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
232 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
234 WINHTTP_PROXY_INFO info
;
236 WinHttpGetDefaultProxyConfiguration( &info
);
237 session
->access
= info
.dwAccessType
;
238 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
240 GlobalFree( info
.lpszProxy
);
241 GlobalFree( info
.lpszProxyBypass
);
244 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
246 GlobalFree( info
.lpszProxy
);
247 GlobalFree( info
.lpszProxyBypass
);
251 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
253 session
->access
= access
;
254 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
255 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
258 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
259 session
->hdr
.handle
= handle
;
262 release_object( &session
->hdr
);
263 TRACE("returning %p\n", handle
);
267 /***********************************************************************
268 * connect_destroy (internal)
270 static void connect_destroy( object_header_t
*hdr
)
272 connect_t
*connect
= (connect_t
*)hdr
;
274 TRACE("%p\n", connect
);
276 release_object( &connect
->session
->hdr
);
278 heap_free( connect
->hostname
);
279 heap_free( connect
->servername
);
280 heap_free( connect
->username
);
281 heap_free( connect
->password
);
282 heap_free( connect
);
285 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
287 connect_t
*connect
= (connect_t
*)hdr
;
291 case WINHTTP_OPTION_PARENT_HANDLE
:
293 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
295 *buflen
= sizeof(HINTERNET
);
296 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
300 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
301 *buflen
= sizeof(HINTERNET
);
304 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
305 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
306 *buflen
= sizeof(DWORD
);
308 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
309 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
310 *buflen
= sizeof(DWORD
);
312 case WINHTTP_OPTION_SEND_TIMEOUT
:
313 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
314 *buflen
= sizeof(DWORD
);
316 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
317 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
318 *buflen
= sizeof(DWORD
);
321 FIXME("unimplemented option %u\n", option
);
322 set_last_error( ERROR_INVALID_PARAMETER
);
327 static const object_vtbl_t connect_vtbl
=
330 connect_query_option
,
334 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
336 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
339 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
341 else if (*domain
== '*')
343 if (domain
[1] == '.')
347 /* For a hostname to match a wildcard, the last domain must match
348 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
349 * hostname is www.foo.a.b, it matches, but a.b does not.
351 dot
= strchrW( server
, '.' );
354 int len
= strlenW( dot
+ 1 );
356 if (len
> strlenW( domain
+ 2 ))
360 /* The server's domain is longer than the wildcard, so it
361 * could be a subdomain. Compare the last portion of the
364 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
365 if (!strcmpiW( ptr
, domain
+ 2 ))
367 /* This is only a match if the preceding character is
368 * a '.', i.e. that it is a matching domain. E.g.
369 * if domain is '*.b.c' and server is 'www.ab.c' they
372 ret
= *(ptr
- 1) == '.';
376 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
381 ret
= !strcmpiW( server
, domain
);
385 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
386 #define MAX_HOST_NAME_LENGTH 256
388 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
393 if (!session
->proxy_bypass
) return FALSE
;
394 ptr
= session
->proxy_bypass
;
398 ptr
= strchrW( ptr
, ';' );
400 ptr
= strchrW( tmp
, ' ' );
403 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
405 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
407 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
408 domain
[ptr
- tmp
] = 0;
409 ret
= domain_matches( server
, domain
);
414 ret
= domain_matches( server
, tmp
);
415 } while (ptr
&& !ret
);
419 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
421 session_t
*session
= connect
->session
;
424 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
428 if ((colon
= strchrW( session
->proxy_server
, ':' )))
430 if (!connect
->servername
|| strncmpiW( connect
->servername
,
431 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
433 heap_free( connect
->servername
);
434 connect
->resolved
= FALSE
;
435 if (!(connect
->servername
= heap_alloc(
436 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
441 memcpy( connect
->servername
, session
->proxy_server
,
442 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
443 connect
->servername
[colon
- session
->proxy_server
] = 0;
445 connect
->serverport
= atoiW( colon
+ 1 );
447 connect
->serverport
= INTERNET_DEFAULT_PORT
;
452 if (!connect
->servername
|| strcmpiW( connect
->servername
,
453 session
->proxy_server
))
455 heap_free( connect
->servername
);
456 connect
->resolved
= FALSE
;
457 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
462 connect
->serverport
= INTERNET_DEFAULT_PORT
;
468 heap_free( connect
->servername
);
469 connect
->resolved
= FALSE
;
470 if (!(connect
->servername
= strdupW( server
)))
475 connect
->serverport
= port
;
481 /***********************************************************************
482 * WinHttpConnect (winhttp.@)
484 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
488 HINTERNET hconnect
= NULL
;
490 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
494 set_last_error( ERROR_INVALID_PARAMETER
);
497 if (!(session
= (session_t
*)grab_object( hsession
)))
499 set_last_error( ERROR_INVALID_HANDLE
);
502 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
504 release_object( &session
->hdr
);
505 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
508 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
510 release_object( &session
->hdr
);
513 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
514 connect
->hdr
.vtbl
= &connect_vtbl
;
515 connect
->hdr
.refs
= 1;
516 connect
->hdr
.flags
= session
->hdr
.flags
;
517 connect
->hdr
.callback
= session
->hdr
.callback
;
518 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
519 connect
->hdr
.context
= session
->hdr
.context
;
520 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
521 list_init( &connect
->hdr
.children
);
523 addref_object( &session
->hdr
);
524 connect
->session
= session
;
525 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
527 if (!(connect
->hostname
= strdupW( server
))) goto end
;
528 connect
->hostport
= port
;
529 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
531 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
532 connect
->hdr
.handle
= hconnect
;
534 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
537 release_object( &connect
->hdr
);
538 release_object( &session
->hdr
);
539 TRACE("returning %p\n", hconnect
);
543 /***********************************************************************
544 * request_destroy (internal)
546 static void request_destroy( object_header_t
*hdr
)
548 request_t
*request
= (request_t
*)hdr
;
551 TRACE("%p\n", request
);
553 release_object( &request
->connect
->hdr
);
555 destroy_authinfo( request
->authinfo
);
556 destroy_authinfo( request
->proxy_authinfo
);
558 heap_free( request
->verb
);
559 heap_free( request
->path
);
560 heap_free( request
->version
);
561 heap_free( request
->raw_headers
);
562 heap_free( request
->status_text
);
563 for (i
= 0; i
< request
->num_headers
; i
++)
565 heap_free( request
->headers
[i
].field
);
566 heap_free( request
->headers
[i
].value
);
568 heap_free( request
->headers
);
569 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
570 heap_free( request
->accept_types
);
571 heap_free( request
);
574 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
577 if (str
) len
= strlenW( str
);
578 if (buffer
&& *buflen
> len
)
580 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
583 *buflen
= len
* sizeof(WCHAR
);
586 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
589 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
591 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
592 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
593 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
598 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
601 switch (addr
->sa_family
)
605 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
606 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
609 addr_win
->sin_family
= WS_AF_INET
;
610 addr_win
->sin_port
= addr_unix
->sin_port
;
611 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
612 p
= (char *)&addr_win
->sin_addr
+ 4;
613 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
618 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
619 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
621 addr_win
->sin6_family
= WS_AF_INET6
;
622 addr_win
->sin6_port
= addr_unix
->sin6_port
;
623 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
624 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
625 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
626 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
628 addr_win
->sin6_scope_id
= 0;
630 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
634 ERR("unhandled family %u\n", addr
->sa_family
);
638 switch (addr
->sa_family
)
642 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
644 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
645 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
650 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
652 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
653 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
657 ERR("unhandled family %u\n", addr
->sa_family
);
663 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
665 request_t
*request
= (request_t
*)hdr
;
669 case WINHTTP_OPTION_SECURITY_FLAGS
:
674 if (!buffer
|| *buflen
< sizeof(flags
))
676 *buflen
= sizeof(flags
);
677 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
682 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
683 flags
|= request
->netconn
.security_flags
;
684 bits
= netconn_get_cipher_strength( &request
->netconn
);
686 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
688 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
690 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
691 *(DWORD
*)buffer
= flags
;
692 *buflen
= sizeof(flags
);
695 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
697 const CERT_CONTEXT
*cert
;
699 if (!buffer
|| *buflen
< sizeof(cert
))
701 *buflen
= sizeof(cert
);
702 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
706 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
707 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
708 *buflen
= sizeof(cert
);
711 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
713 const CERT_CONTEXT
*cert
;
714 const CRYPT_OID_INFO
*oidInfo
;
715 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
717 FIXME("partial stub\n");
719 if (!buffer
|| *buflen
< sizeof(*ci
))
721 *buflen
= sizeof(*ci
);
722 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
725 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
727 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
728 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
729 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
730 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
731 ci
->lpszProtocolName
= NULL
;
732 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
733 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
736 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
738 ci
->lpszSignatureAlgName
= NULL
;
739 ci
->lpszEncryptionAlgName
= NULL
;
740 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
742 CertFreeCertificateContext( cert
);
743 *buflen
= sizeof(*ci
);
746 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
748 if (!buffer
|| *buflen
< sizeof(DWORD
))
750 *buflen
= sizeof(DWORD
);
751 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
755 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
756 *buflen
= sizeof(DWORD
);
759 case WINHTTP_OPTION_CONNECTION_INFO
:
761 WINHTTP_CONNECTION_INFO
*info
= buffer
;
762 struct sockaddr local
;
763 socklen_t len
= sizeof(local
);
764 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
766 if (!buffer
|| *buflen
< sizeof(*info
))
768 *buflen
= sizeof(*info
);
769 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
772 if (!netconn_connected( &request
->netconn
))
774 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
777 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
778 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
779 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
780 info
->cbSize
= sizeof(*info
);
783 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
784 *(DWORD
*)buffer
= request
->resolve_timeout
;
785 *buflen
= sizeof(DWORD
);
787 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
788 *(DWORD
*)buffer
= request
->connect_timeout
;
789 *buflen
= sizeof(DWORD
);
791 case WINHTTP_OPTION_SEND_TIMEOUT
:
792 *(DWORD
*)buffer
= request
->send_timeout
;
793 *buflen
= sizeof(DWORD
);
795 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
796 *(DWORD
*)buffer
= request
->recv_timeout
;
797 *buflen
= sizeof(DWORD
);
800 case WINHTTP_OPTION_USERNAME
:
801 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
804 case WINHTTP_OPTION_PASSWORD
:
805 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
808 case WINHTTP_OPTION_PROXY_USERNAME
:
809 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
812 case WINHTTP_OPTION_PROXY_PASSWORD
:
813 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
817 FIXME("unimplemented option %u\n", option
);
818 set_last_error( ERROR_INVALID_PARAMETER
);
823 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
826 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
828 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
832 set_last_error( ERROR_OUTOFMEMORY
);
836 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
838 request_t
*request
= (request_t
*)hdr
;
842 case WINHTTP_OPTION_PROXY
:
844 WINHTTP_PROXY_INFO
*pi
= buffer
;
846 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
849 case WINHTTP_OPTION_DISABLE_FEATURE
:
853 if (buflen
!= sizeof(DWORD
))
855 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
859 disable
= *(DWORD
*)buffer
;
860 TRACE("0x%x\n", disable
);
861 hdr
->disable_flags
|= disable
;
864 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
868 if (buflen
!= sizeof(DWORD
))
870 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
874 policy
= *(DWORD
*)buffer
;
875 TRACE("0x%x\n", policy
);
876 hdr
->logon_policy
= policy
;
879 case WINHTTP_OPTION_REDIRECT_POLICY
:
883 if (buflen
!= sizeof(DWORD
))
885 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
889 policy
= *(DWORD
*)buffer
;
890 TRACE("0x%x\n", policy
);
891 hdr
->redirect_policy
= policy
;
894 case WINHTTP_OPTION_SECURITY_FLAGS
:
898 if (buflen
< sizeof(DWORD
))
900 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
903 flags
= *(DWORD
*)buffer
;
904 TRACE("0x%x\n", flags
);
905 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
906 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
907 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
908 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
910 set_last_error( ERROR_INVALID_PARAMETER
);
913 request
->netconn
.security_flags
= flags
;
916 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
917 request
->resolve_timeout
= *(DWORD
*)buffer
;
919 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
920 request
->connect_timeout
= *(DWORD
*)buffer
;
922 case WINHTTP_OPTION_SEND_TIMEOUT
:
923 request
->send_timeout
= *(DWORD
*)buffer
;
925 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
926 request
->recv_timeout
= *(DWORD
*)buffer
;
929 case WINHTTP_OPTION_USERNAME
:
931 connect_t
*connect
= request
->connect
;
933 heap_free( connect
->username
);
934 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
937 case WINHTTP_OPTION_PASSWORD
:
939 connect_t
*connect
= request
->connect
;
941 heap_free( connect
->password
);
942 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
945 case WINHTTP_OPTION_PROXY_USERNAME
:
947 session_t
*session
= request
->connect
->session
;
949 heap_free( session
->proxy_username
);
950 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
953 case WINHTTP_OPTION_PROXY_PASSWORD
:
955 session_t
*session
= request
->connect
->session
;
957 heap_free( session
->proxy_password
);
958 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
962 FIXME("unimplemented option %u\n", option
);
963 set_last_error( ERROR_INVALID_PARAMETER
);
968 static const object_vtbl_t request_vtbl
=
971 request_query_option
,
975 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
977 const WCHAR
**types
= accept_types
;
980 if (!types
) return TRUE
;
983 request
->num_accept_types
++;
986 if (!request
->num_accept_types
) return TRUE
;
987 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
989 request
->num_accept_types
= 0;
992 types
= accept_types
;
993 for (i
= 0; i
< request
->num_accept_types
; i
++)
995 if (!(request
->accept_types
[i
] = strdupW( *types
)))
997 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
998 heap_free( request
->accept_types
);
999 request
->accept_types
= NULL
;
1000 request
->num_accept_types
= 0;
1008 /***********************************************************************
1009 * WinHttpOpenRequest (winhttp.@)
1011 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1012 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1016 HINTERNET hrequest
= NULL
;
1018 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1019 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1021 if(types
&& TRACE_ON(winhttp
)) {
1024 TRACE("accept types:\n");
1025 for(iter
= types
; *iter
; iter
++)
1026 TRACE(" %s\n", debugstr_w(*iter
));
1029 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1031 set_last_error( ERROR_INVALID_HANDLE
);
1034 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1036 release_object( &connect
->hdr
);
1037 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1040 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1042 release_object( &connect
->hdr
);
1045 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1046 request
->hdr
.vtbl
= &request_vtbl
;
1047 request
->hdr
.refs
= 1;
1048 request
->hdr
.flags
= flags
;
1049 request
->hdr
.callback
= connect
->hdr
.callback
;
1050 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1051 request
->hdr
.context
= connect
->hdr
.context
;
1052 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1053 list_init( &request
->hdr
.children
);
1055 addref_object( &connect
->hdr
);
1056 request
->connect
= connect
;
1057 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1059 if (!netconn_init( &request
->netconn
)) goto end
;
1060 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1061 request
->connect_timeout
= connect
->session
->connect_timeout
;
1062 request
->send_timeout
= connect
->session
->send_timeout
;
1063 request
->recv_timeout
= connect
->session
->recv_timeout
;
1065 if (!verb
|| !verb
[0]) verb
= getW
;
1066 if (!(request
->verb
= strdupW( verb
))) goto end
;
1073 len
= strlenW( object
) + 1;
1074 if (object
[0] != '/') len
++;
1075 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1077 if (object
[0] != '/') *p
++ = '/';
1078 strcpyW( p
, object
);
1079 request
->path
= path
;
1081 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1083 if (!version
|| !version
[0]) version
= http1_1
;
1084 if (!(request
->version
= strdupW( version
))) goto end
;
1085 if (!(store_accept_types( request
, types
))) goto end
;
1087 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1088 request
->hdr
.handle
= hrequest
;
1090 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1093 release_object( &request
->hdr
);
1094 release_object( &connect
->hdr
);
1095 TRACE("returning %p\n", hrequest
);
1099 /***********************************************************************
1100 * WinHttpCloseHandle (winhttp.@)
1102 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1104 object_header_t
*hdr
;
1106 TRACE("%p\n", handle
);
1108 if (!(hdr
= grab_object( handle
)))
1110 set_last_error( ERROR_INVALID_HANDLE
);
1113 release_object( hdr
);
1114 free_handle( handle
);
1118 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1124 set_last_error( ERROR_INVALID_PARAMETER
);
1130 case WINHTTP_OPTION_CONTEXT_VALUE
:
1132 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1134 *buflen
= sizeof(DWORD_PTR
);
1135 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1139 *(DWORD_PTR
*)buffer
= hdr
->context
;
1140 *buflen
= sizeof(DWORD_PTR
);
1144 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1147 FIXME("unimplemented option %u\n", option
);
1148 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1156 /***********************************************************************
1157 * WinHttpQueryOption (winhttp.@)
1159 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1162 object_header_t
*hdr
;
1164 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1166 if (!(hdr
= grab_object( handle
)))
1168 set_last_error( ERROR_INVALID_HANDLE
);
1172 ret
= query_option( hdr
, option
, buffer
, buflen
);
1174 release_object( hdr
);
1178 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1184 set_last_error( ERROR_INVALID_PARAMETER
);
1190 case WINHTTP_OPTION_CONTEXT_VALUE
:
1192 if (buflen
!= sizeof(DWORD_PTR
))
1194 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1198 hdr
->context
= *(DWORD_PTR
*)buffer
;
1202 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1205 FIXME("unimplemented option %u\n", option
);
1206 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1214 /***********************************************************************
1215 * WinHttpSetOption (winhttp.@)
1217 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1220 object_header_t
*hdr
;
1222 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1224 if (!(hdr
= grab_object( handle
)))
1226 set_last_error( ERROR_INVALID_HANDLE
);
1230 ret
= set_option( hdr
, option
, buffer
, buflen
);
1232 release_object( hdr
);
1236 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1241 GetComputerNameExA( format
, NULL
, &size
);
1242 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1243 if (!(ret
= heap_alloc( size
))) return NULL
;
1244 if (!GetComputerNameExA( format
, ret
, &size
))
1252 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1254 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1256 if (len_suffix
> len_domain
) return FALSE
;
1257 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1261 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
1264 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
1265 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
1266 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
1267 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
1270 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1273 #ifdef HAVE_GETNAMEINFO
1274 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1279 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1281 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1282 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1283 char name
[NI_MAXHOST
];
1287 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1288 if (!ai
) return NULL
;
1290 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1292 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1293 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1294 strcpyW( p
, httpW
);
1295 p
+= strlenW( httpW
);
1296 while (*hostname
) { *p
++ = *hostname
++; }
1297 strcpyW( p
, wpadW
);
1301 /***********************************************************************
1302 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1304 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1308 TRACE("0x%08x, %p\n", flags
, url
);
1312 set_last_error( ERROR_INVALID_PARAMETER
);
1315 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1317 static int fixme_shown
;
1318 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1320 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1322 #ifdef HAVE_GETADDRINFO
1323 char *fqdn
, *domain
, *p
;
1325 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1326 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1332 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1334 struct addrinfo
*ai
;
1338 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1341 heap_free( domain
);
1344 strcpy( name
, "wpad" );
1346 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1349 *url
= build_wpad_url( name
, ai
);
1353 TRACE("returning %s\n", debugstr_w(*url
));
1362 heap_free( domain
);
1365 FIXME("getaddrinfo not found at build time\n");
1368 if (!ret
) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1372 static const WCHAR Connections
[] = {
1373 'S','o','f','t','w','a','r','e','\\',
1374 'M','i','c','r','o','s','o','f','t','\\',
1375 'W','i','n','d','o','w','s','\\',
1376 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1377 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1378 'C','o','n','n','e','c','t','i','o','n','s',0 };
1379 static const WCHAR WinHttpSettings
[] = {
1380 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1381 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1382 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1383 static const DWORD PROXY_TYPE_DIRECT
= 1;
1384 static const DWORD PROXY_TYPE_PROXY
= 2;
1385 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1386 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1388 struct connection_settings_header
1391 DWORD unknown
; /* always zero? */
1392 DWORD flags
; /* one or more of PROXY_* */
1395 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1399 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1404 /***********************************************************************
1405 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1407 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1411 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1414 TRACE("%p\n", info
);
1416 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1419 DWORD type
, size
= 0;
1421 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1422 if (!l
&& type
== REG_BINARY
&&
1423 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1425 BYTE
*buf
= heap_alloc( size
);
1429 struct connection_settings_header
*hdr
=
1430 (struct connection_settings_header
*)buf
;
1431 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1433 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1435 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1438 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1441 LPWSTR proxy
= NULL
;
1442 LPWSTR proxy_bypass
= NULL
;
1444 /* Sanity-check length of proxy string */
1445 if ((BYTE
*)len
- buf
+ *len
<= size
)
1448 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1450 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1451 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1455 /* Sanity-check length of proxy bypass string */
1456 if ((BYTE
*)len
- buf
+ *len
<= size
)
1458 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1460 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1465 GlobalFree( proxy
);
1469 info
->lpszProxy
= proxy
;
1470 info
->lpszProxyBypass
= proxy_bypass
;
1473 got_from_reg
= TRUE
;
1475 info
->dwAccessType
=
1476 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1477 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1478 debugstr_w(info
->lpszProxy
),
1479 debugstr_w(info
->lpszProxyBypass
));
1488 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1490 char *colon
, *http_proxy
;
1492 if ((colon
= strchr( envproxy
, ':' )))
1494 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1496 static const char http
[] = "http://";
1498 /* It's a scheme, check that it's http */
1499 if (!strncmp( envproxy
, http
, strlen( http
) ))
1500 http_proxy
= envproxy
+ strlen( http
);
1503 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1508 http_proxy
= envproxy
;
1511 http_proxy
= envproxy
;
1517 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1518 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1520 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1522 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1523 info
->lpszProxy
= http_proxyW
;
1524 info
->lpszProxyBypass
= NULL
;
1525 TRACE("http proxy (from environment) = %s\n",
1526 debugstr_w(info
->lpszProxy
));
1532 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1533 info
->lpszProxy
= NULL
;
1534 info
->lpszProxyBypass
= NULL
;
1539 /***********************************************************************
1540 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1542 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1544 static const WCHAR settingsW
[] =
1545 {'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};
1547 struct connection_settings_header
*hdr
= NULL
;
1548 DWORD type
, offset
, len
, size
= 0;
1551 TRACE("%p\n", config
);
1555 set_last_error( ERROR_INVALID_PARAMETER
);
1558 memset( config
, 0, sizeof(*config
) );
1559 config
->fAutoDetect
= TRUE
;
1561 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1562 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1563 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1568 if (!(hdr
= heap_alloc( size
))) goto done
;
1569 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1570 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1576 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1577 offset
= sizeof(*hdr
);
1578 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1579 len
= *(DWORD
*)((char *)hdr
+ offset
);
1580 offset
+= sizeof(DWORD
);
1581 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1583 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1584 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1587 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1588 len
= *(DWORD
*)((char *)hdr
+ offset
);
1589 offset
+= sizeof(DWORD
);
1590 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1592 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1593 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1596 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1597 len
= *(DWORD
*)((char *)hdr
+ offset
);
1598 offset
+= sizeof(DWORD
);
1599 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1601 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1602 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1607 RegCloseKey( hkey
);
1611 heap_free( config
->lpszAutoConfigUrl
);
1612 config
->lpszAutoConfigUrl
= NULL
;
1613 heap_free( config
->lpszProxy
);
1614 config
->lpszProxy
= NULL
;
1615 heap_free( config
->lpszProxyBypass
);
1616 config
->lpszProxyBypass
= NULL
;
1621 static HRESULT WINAPI
dispex_QueryInterface(
1622 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1626 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
1627 IsEqualGUID( riid
, &IID_IDispatch
) ||
1628 IsEqualGUID( riid
, &IID_IDispatchEx
))
1631 return E_NOINTERFACE
;
1636 static ULONG WINAPI
dispex_AddRef(
1637 IDispatchEx
*iface
)
1642 static ULONG WINAPI
dispex_Release(
1643 IDispatchEx
*iface
)
1648 static HRESULT WINAPI
dispex_GetTypeInfoCount(
1649 IDispatchEx
*iface
, UINT
*info
)
1654 static HRESULT WINAPI
dispex_GetTypeInfo(
1655 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
1660 static HRESULT WINAPI
dispex_GetIDsOfNames(
1661 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
1666 static HRESULT WINAPI
dispex_Invoke(
1667 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
1668 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
1673 static HRESULT WINAPI
dispex_DeleteMemberByName(
1674 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
1679 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
1680 IDispatchEx
*iface
, DISPID id
)
1685 static HRESULT WINAPI
dispex_GetMemberProperties(
1686 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
1691 static HRESULT WINAPI
dispex_GetMemberName(
1692 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
1697 static HRESULT WINAPI
dispex_GetNextDispID(
1698 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
1703 static HRESULT WINAPI
dispex_GetNameSpaceParent(
1704 IDispatchEx
*iface
, IUnknown
**unk
)
1709 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1711 static HRESULT WINAPI
dispex_GetDispID(
1712 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
1714 if (!strcmpW( name
, dns_resolveW
))
1716 *id
= DISPID_GLOBAL_DNSRESOLVE
;
1719 return DISP_E_UNKNOWNNAME
;
1722 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
1724 #ifdef HAVE_GETADDRINFO
1725 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1727 struct addrinfo
*ai
, *elem
;
1732 hostnameA
= strdupWA( hostname
);
1734 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
1736 if (!hostnameA
) return E_OUTOFMEMORY
;
1737 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
1738 heap_free( hostnameA
);
1739 if (res
) return S_FALSE
;
1742 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
1748 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
1750 V_VT( result
) = VT_BSTR
;
1751 V_BSTR( result
) = SysAllocString( addr
);
1754 FIXME("getaddrinfo not found at build time\n");
1759 static HRESULT WINAPI
dispex_InvokeEx(
1760 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
1761 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
1763 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
1765 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
1766 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
1767 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
1769 return DISP_E_MEMBERNOTFOUND
;
1772 static const IDispatchExVtbl dispex_vtbl
=
1774 dispex_QueryInterface
,
1777 dispex_GetTypeInfoCount
,
1779 dispex_GetIDsOfNames
,
1783 dispex_DeleteMemberByName
,
1784 dispex_DeleteMemberByDispID
,
1785 dispex_GetMemberProperties
,
1786 dispex_GetMemberName
,
1787 dispex_GetNextDispID
,
1788 dispex_GetNameSpaceParent
1791 static IDispatchEx global_dispex
= { &dispex_vtbl
};
1793 static HRESULT WINAPI
site_QueryInterface(
1794 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
1798 if (IsEqualGUID( &IID_IUnknown
, riid
))
1800 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
1803 return E_NOINTERFACE
;
1805 IUnknown_AddRef( (IUnknown
*)*ppv
);
1809 static ULONG WINAPI
site_AddRef(
1810 IActiveScriptSite
*iface
)
1815 static ULONG WINAPI
site_Release(
1816 IActiveScriptSite
*iface
)
1821 static HRESULT WINAPI
site_GetLCID(
1822 IActiveScriptSite
*iface
, LCID
*lcid
)
1827 static HRESULT WINAPI
site_GetItemInfo(
1828 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
1829 IUnknown
**item
, ITypeInfo
**type_info
)
1831 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
1833 *item
= (IUnknown
*)&global_dispex
;
1839 static HRESULT WINAPI
site_GetDocVersionString(
1840 IActiveScriptSite
*iface
, BSTR
*version
)
1845 static HRESULT WINAPI
site_OnScriptTerminate(
1846 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
1851 static HRESULT WINAPI
site_OnStateChange(
1852 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
1857 static HRESULT WINAPI
site_OnScriptError(
1858 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
1863 static HRESULT WINAPI
site_OnEnterScript(
1864 IActiveScriptSite
*iface
)
1869 static HRESULT WINAPI
site_OnLeaveScript(
1870 IActiveScriptSite
*iface
)
1875 static const IActiveScriptSiteVtbl site_vtbl
=
1877 site_QueryInterface
,
1882 site_GetDocVersionString
,
1883 site_OnScriptTerminate
,
1890 static IActiveScriptSite script_site
= { &site_vtbl
};
1892 static BOOL
parse_script_result( VARIANT result
, WINHTTP_PROXY_INFO
*info
)
1894 static const WCHAR proxyW
[] = {'P','R','O','X','Y'};
1899 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1900 info
->lpszProxy
= NULL
;
1901 info
->lpszProxyBypass
= NULL
;
1903 if (V_VT( &result
) != VT_BSTR
) return TRUE
;
1904 TRACE("%s\n", debugstr_w( V_BSTR( &result
) ));
1906 p
= V_BSTR( &result
);
1907 while (*p
== ' ') p
++;
1909 if (len
>= 5 && !memicmpW( p
, proxyW
, sizeof(proxyW
)/sizeof(WCHAR
) ))
1912 while (*p
== ' ') p
++;
1913 if (!*p
|| *p
== ';') return TRUE
;
1914 if (!(info
->lpszProxy
= q
= strdupW( p
))) return FALSE
;
1915 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1918 if (*q
== ' ' || *q
== ';')
1928 static BSTR
include_pac_utils( BSTR script
)
1930 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
1931 HMODULE hmod
= GetModuleHandleA( "winhttp.dll" );
1938 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
1939 size
= SizeofResource( hmod
, rsrc
);
1940 data
= LoadResource( hmod
, rsrc
);
1942 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
1943 if (!(ret
= SysAllocStringLen( NULL
, len
+ SysStringLen( script
) + 1 ))) return NULL
;
1944 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
1946 strcatW( ret
, script
);
1951 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1952 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1953 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1955 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1956 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1957 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1960 static BOOL
run_script( const BSTR script
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1962 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
1963 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1964 IActiveScriptParse
*parser
= NULL
;
1965 IActiveScript
*engine
= NULL
;
1966 IDispatch
*dispatch
= NULL
;
1970 BSTR func
= NULL
, hostname
= NULL
, full_script
= NULL
;
1972 VARIANT args
[2], result
;
1976 memset( &uc
, 0, sizeof(uc
) );
1977 uc
.dwStructSize
= sizeof(uc
);
1978 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return FALSE
;
1979 if (!(hostname
= SysAllocStringLen( NULL
, uc
.dwHostNameLength
+ 1 ))) return FALSE
;
1980 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1981 hostname
[uc
.dwHostNameLength
] = 0;
1983 init
= CoInitialize( NULL
);
1984 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
1985 if (hr
!= S_OK
) goto done
;
1987 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
1988 &IID_IActiveScript
, (void **)&engine
);
1989 if (hr
!= S_OK
) goto done
;
1991 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
1992 if (hr
!= S_OK
) goto done
;
1994 hr
= IActiveScriptParse_InitNew( parser
);
1995 if (hr
!= S_OK
) goto done
;
1997 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
1998 if (hr
!= S_OK
) goto done
;
2000 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
2001 if (hr
!= S_OK
) goto done
;
2003 if (!(full_script
= include_pac_utils( script
))) goto done
;
2005 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
2006 if (hr
!= S_OK
) goto done
;
2008 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
2009 if (hr
!= S_OK
) goto done
;
2011 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
2012 if (hr
!= S_OK
) goto done
;
2014 if (!(func
= SysAllocString( findproxyW
))) goto done
;
2015 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
2016 if (hr
!= S_OK
) goto done
;
2018 V_VT( &args
[0] ) = VT_BSTR
;
2019 V_BSTR( &args
[0] ) = hostname
;
2020 V_VT( &args
[1] ) = VT_BSTR
;
2021 V_BSTR( &args
[1] ) = SysAllocString( url
);
2023 params
.rgvarg
= args
;
2024 params
.rgdispidNamedArgs
= NULL
;
2026 params
.cNamedArgs
= 0;
2027 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
2028 ¶ms
, &result
, NULL
, NULL
);
2029 VariantClear( &args
[1] );
2032 WARN("script failed 0x%08x\n", hr
);
2035 ret
= parse_script_result( result
, info
);
2038 SysFreeString( full_script
);
2039 SysFreeString( hostname
);
2040 SysFreeString( func
);
2041 if (dispatch
) IDispatch_Release( dispatch
);
2042 if (parser
) IActiveScriptParse_Release( parser
);
2043 if (engine
) IActiveScript_Release( engine
);
2044 if (SUCCEEDED( init
)) CoUninitialize();
2045 if (!ret
) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT
);
2049 static BSTR
download_script( const WCHAR
*url
)
2051 static const WCHAR typeW
[] = {'*','/','*',0};
2052 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
2053 HINTERNET ses
, con
= NULL
, req
= NULL
;
2056 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
2057 char *tmp
, *buffer
= NULL
;
2061 memset( &uc
, 0, sizeof(uc
) );
2062 uc
.dwStructSize
= sizeof(uc
);
2063 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
2064 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
2065 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
2066 hostname
[uc
.dwHostNameLength
] = 0;
2068 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
2069 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
2070 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
2071 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
2072 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
2074 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
2075 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
2076 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
2079 if (!(buffer
= heap_alloc( size
))) goto done
;
2084 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
2085 if (!bytes_read
) break;
2086 to_read
-= bytes_read
;
2087 offset
+= bytes_read
;
2092 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
2096 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, NULL
, 0 );
2097 if (!(script
= SysAllocStringLen( NULL
, len
))) goto done
;
2098 MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, script
, len
);
2102 WinHttpCloseHandle( req
);
2103 WinHttpCloseHandle( con
);
2104 WinHttpCloseHandle( ses
);
2105 heap_free( buffer
);
2106 heap_free( hostname
);
2107 if (!script
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
2111 /***********************************************************************
2112 * WinHttpGetProxyForUrl (winhttp.@)
2114 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2115 WINHTTP_PROXY_INFO
*info
)
2117 WCHAR
*detected_pac_url
= NULL
;
2118 const WCHAR
*pac_url
;
2123 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
2125 if (!(session
= (session_t
*)grab_object( hsession
)))
2127 set_last_error( ERROR_INVALID_HANDLE
);
2130 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
2132 release_object( &session
->hdr
);
2133 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2136 if (!url
|| !options
|| !info
||
2137 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2138 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
2139 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
2140 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
2142 release_object( &session
->hdr
);
2143 set_last_error( ERROR_INVALID_PARAMETER
);
2146 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
2147 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
2150 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
2151 else pac_url
= detected_pac_url
;
2153 if (!(script
= download_script( pac_url
))) goto done
;
2154 ret
= run_script( script
, url
, info
);
2155 SysFreeString( script
);
2158 GlobalFree( detected_pac_url
);
2159 release_object( &session
->hdr
);
2163 /***********************************************************************
2164 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2166 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
2173 TRACE("%p\n", info
);
2177 set_last_error( ERROR_INVALID_PARAMETER
);
2180 switch (info
->dwAccessType
)
2182 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
2184 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
2185 if (!info
->lpszProxy
)
2187 set_last_error( ERROR_INVALID_PARAMETER
);
2190 /* Only ASCII characters are allowed */
2191 for (src
= info
->lpszProxy
; *src
; src
++)
2194 set_last_error( ERROR_INVALID_PARAMETER
);
2197 if (info
->lpszProxyBypass
)
2199 for (src
= info
->lpszProxyBypass
; *src
; src
++)
2202 set_last_error( ERROR_INVALID_PARAMETER
);
2208 set_last_error( ERROR_INVALID_PARAMETER
);
2212 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
2213 KEY_WRITE
, NULL
, &key
, NULL
);
2216 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
2219 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2221 size
+= strlenW( info
->lpszProxy
);
2222 if (info
->lpszProxyBypass
)
2223 size
+= strlenW( info
->lpszProxyBypass
);
2225 buf
= heap_alloc( size
);
2228 struct connection_settings_header
*hdr
=
2229 (struct connection_settings_header
*)buf
;
2230 DWORD
*len
= (DWORD
*)(hdr
+ 1);
2232 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
2234 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2238 hdr
->flags
= PROXY_TYPE_PROXY
;
2239 *len
++ = strlenW( info
->lpszProxy
);
2240 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
2244 if (info
->lpszProxyBypass
)
2246 *len
++ = strlenW( info
->lpszProxyBypass
);
2247 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
2256 hdr
->flags
= PROXY_TYPE_DIRECT
;
2260 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
2270 /***********************************************************************
2271 * WinHttpSetStatusCallback (winhttp.@)
2273 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
2274 DWORD flags
, DWORD_PTR reserved
)
2276 object_header_t
*hdr
;
2277 WINHTTP_STATUS_CALLBACK ret
;
2279 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
2281 if (!(hdr
= grab_object( handle
)))
2283 set_last_error( ERROR_INVALID_HANDLE
);
2284 return WINHTTP_INVALID_STATUS_CALLBACK
;
2286 ret
= hdr
->callback
;
2287 hdr
->callback
= callback
;
2288 hdr
->notify_mask
= flags
;
2290 release_object( hdr
);
2294 /***********************************************************************
2295 * WinHttpSetTimeouts (winhttp.@)
2297 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2300 object_header_t
*hdr
;
2304 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2306 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2308 set_last_error( ERROR_INVALID_PARAMETER
);
2312 if (!(hdr
= grab_object( handle
)))
2314 set_last_error( ERROR_INVALID_HANDLE
);
2320 case WINHTTP_HANDLE_TYPE_REQUEST
:
2321 request
= (request_t
*)hdr
;
2322 request
->connect_timeout
= connect
;
2324 if (resolve
< 0) resolve
= 0;
2325 request
->resolve_timeout
= resolve
;
2327 if (send
< 0) send
= 0;
2328 request
->send_timeout
= send
;
2330 if (receive
< 0) receive
= 0;
2331 request
->recv_timeout
= receive
;
2333 if (netconn_connected( &request
->netconn
))
2335 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2336 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2339 release_object( &request
->hdr
);
2342 case WINHTTP_HANDLE_TYPE_SESSION
:
2343 session
= (session_t
*)hdr
;
2344 session
->connect_timeout
= connect
;
2346 if (resolve
< 0) resolve
= 0;
2347 session
->resolve_timeout
= resolve
;
2349 if (send
< 0) send
= 0;
2350 session
->send_timeout
= send
;
2352 if (receive
< 0) receive
= 0;
2353 session
->recv_timeout
= receive
;
2357 release_object( hdr
);
2358 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2364 static const WCHAR wkday
[7][4] =
2365 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2366 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2367 static const WCHAR month
[12][4] =
2368 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2369 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2370 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2372 /***********************************************************************
2373 * WinHttpTimeFromSystemTime (WININET.@)
2375 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2377 static const WCHAR format
[] =
2378 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2379 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2381 TRACE("%p, %p\n", time
, string
);
2383 if (!time
|| !string
) return FALSE
;
2385 sprintfW( string
, format
,
2386 wkday
[time
->wDayOfWeek
],
2388 month
[time
->wMonth
- 1],
2397 /***********************************************************************
2398 * WinHttpTimeToSystemTime (WININET.@)
2400 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2403 const WCHAR
*s
= string
;
2406 TRACE("%s, %p\n", debugstr_w(string
), time
);
2408 if (!string
|| !time
) return FALSE
;
2410 /* Windows does this too */
2411 GetSystemTime( time
);
2413 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2414 * a SYSTEMTIME structure.
2417 while (*s
&& !isalphaW( *s
)) s
++;
2418 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2419 time
->wDayOfWeek
= 7;
2421 for (i
= 0; i
< 7; i
++)
2423 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2424 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2425 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2427 time
->wDayOfWeek
= i
;
2432 if (time
->wDayOfWeek
> 6) return TRUE
;
2433 while (*s
&& !isdigitW( *s
)) s
++;
2434 time
->wDay
= strtolW( s
, &end
, 10 );
2437 while (*s
&& !isalphaW( *s
)) s
++;
2438 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2441 for (i
= 0; i
< 12; i
++)
2443 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2444 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2445 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2447 time
->wMonth
= i
+ 1;
2451 if (time
->wMonth
== 0) return TRUE
;
2453 while (*s
&& !isdigitW( *s
)) s
++;
2454 if (*s
== '\0') return TRUE
;
2455 time
->wYear
= strtolW( s
, &end
, 10 );
2458 while (*s
&& !isdigitW( *s
)) s
++;
2459 if (*s
== '\0') return TRUE
;
2460 time
->wHour
= strtolW( s
, &end
, 10 );
2463 while (*s
&& !isdigitW( *s
)) s
++;
2464 if (*s
== '\0') return TRUE
;
2465 time
->wMinute
= strtolW( s
, &end
, 10 );
2468 while (*s
&& !isdigitW( *s
)) s
++;
2469 if (*s
== '\0') return TRUE
;
2470 time
->wSecond
= strtolW( s
, &end
, 10 );
2472 time
->wMilliseconds
= 0;