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
19 #include "winhttp_private.h"
26 #define DEFAULT_RESOLVE_TIMEOUT 0
27 #define DEFAULT_CONNECT_TIMEOUT 20000
28 #define DEFAULT_SEND_TIMEOUT 30000
29 #define DEFAULT_RECEIVE_TIMEOUT 30000
31 static const WCHAR global_funcsW
[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
32 static const WCHAR dns_resolveW
[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
34 void set_last_error( DWORD error
)
37 SetLastError( error
);
40 DWORD
get_last_error( void )
43 return GetLastError();
46 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
48 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
50 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
53 /***********************************************************************
54 * WinHttpCheckPlatform (winhttp.@)
56 BOOL WINAPI
WinHttpCheckPlatform( void )
62 /***********************************************************************
63 * session_destroy (internal)
65 static void session_destroy( object_header_t
*hdr
)
67 session_t
*session
= (session_t
*)hdr
;
68 struct list
*item
, *next
;
71 TRACE("%p\n", session
);
73 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
75 domain
= LIST_ENTRY( item
, domain_t
, entry
);
76 delete_domain( domain
);
78 heap_free( session
->agent
);
79 heap_free( session
->proxy_server
);
80 heap_free( session
->proxy_bypass
);
81 heap_free( session
->proxy_username
);
82 heap_free( session
->proxy_password
);
89 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
91 session_t
*session
= (session_t
*)hdr
;
95 case WINHTTP_OPTION_REDIRECT_POLICY
:
97 if (!buffer
|| *buflen
< sizeof(DWORD
))
99 *buflen
= sizeof(DWORD
);
100 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
104 *(DWORD
*)buffer
= hdr
->redirect_policy
;
105 *buflen
= sizeof(DWORD
);
108 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
109 *(DWORD
*)buffer
= session
->resolve_timeout
;
110 *buflen
= sizeof(DWORD
);
112 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
113 *(DWORD
*)buffer
= session
->connect_timeout
;
114 *buflen
= sizeof(DWORD
);
116 case WINHTTP_OPTION_SEND_TIMEOUT
:
117 *(DWORD
*)buffer
= session
->send_timeout
;
118 *buflen
= sizeof(DWORD
);
120 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
121 *(DWORD
*)buffer
= session
->recv_timeout
;
122 *buflen
= sizeof(DWORD
);
125 FIXME("unimplemented option %u\n", option
);
126 set_last_error( ERROR_INVALID_PARAMETER
);
131 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
133 session_t
*session
= (session_t
*)hdr
;
137 case WINHTTP_OPTION_PROXY
:
139 WINHTTP_PROXY_INFO
*pi
= buffer
;
141 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
144 case WINHTTP_OPTION_REDIRECT_POLICY
:
148 if (buflen
!= sizeof(policy
))
150 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
154 policy
= *(DWORD
*)buffer
;
155 TRACE("0x%x\n", policy
);
156 hdr
->redirect_policy
= policy
;
159 case WINHTTP_OPTION_DISABLE_FEATURE
:
160 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
162 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
163 session
->resolve_timeout
= *(DWORD
*)buffer
;
165 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
166 session
->connect_timeout
= *(DWORD
*)buffer
;
168 case WINHTTP_OPTION_SEND_TIMEOUT
:
169 session
->send_timeout
= *(DWORD
*)buffer
;
171 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
172 session
->recv_timeout
= *(DWORD
*)buffer
;
174 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
175 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
178 FIXME("unimplemented option %u\n", option
);
179 set_last_error( ERROR_INVALID_PARAMETER
);
184 static const object_vtbl_t session_vtbl
=
187 session_query_option
,
191 /***********************************************************************
192 * WinHttpOpen (winhttp.@)
194 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
197 HINTERNET handle
= NULL
;
200 int error
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
201 if (error
) ERR("WSAStartup failed: %d\n", error
);
204 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
206 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
208 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
209 session
->hdr
.vtbl
= &session_vtbl
;
210 session
->hdr
.flags
= flags
;
211 session
->hdr
.refs
= 1;
212 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
213 list_init( &session
->hdr
.children
);
214 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
215 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
216 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
217 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
218 list_init( &session
->cookie_cache
);
220 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
221 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
223 WINHTTP_PROXY_INFO info
;
225 WinHttpGetDefaultProxyConfiguration( &info
);
226 session
->access
= info
.dwAccessType
;
227 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
229 GlobalFree( (LPWSTR
)info
.lpszProxy
);
230 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
233 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
235 GlobalFree( (LPWSTR
)info
.lpszProxy
);
236 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
240 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
242 session
->access
= access
;
243 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
244 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
247 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
248 session
->hdr
.handle
= handle
;
251 release_object( &session
->hdr
);
252 TRACE("returning %p\n", handle
);
256 /***********************************************************************
257 * connect_destroy (internal)
259 static void connect_destroy( object_header_t
*hdr
)
261 connect_t
*connect
= (connect_t
*)hdr
;
263 TRACE("%p\n", connect
);
265 release_object( &connect
->session
->hdr
);
267 heap_free( connect
->hostname
);
268 heap_free( connect
->servername
);
269 heap_free( connect
->username
);
270 heap_free( connect
->password
);
271 heap_free( connect
);
274 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
276 connect_t
*connect
= (connect_t
*)hdr
;
280 case WINHTTP_OPTION_PARENT_HANDLE
:
282 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
284 *buflen
= sizeof(HINTERNET
);
285 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
289 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
290 *buflen
= sizeof(HINTERNET
);
293 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
294 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
295 *buflen
= sizeof(DWORD
);
297 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
298 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
299 *buflen
= sizeof(DWORD
);
301 case WINHTTP_OPTION_SEND_TIMEOUT
:
302 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
303 *buflen
= sizeof(DWORD
);
305 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
306 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
307 *buflen
= sizeof(DWORD
);
310 FIXME("unimplemented option %u\n", option
);
311 set_last_error( ERROR_INVALID_PARAMETER
);
316 static const object_vtbl_t connect_vtbl
=
319 connect_query_option
,
323 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
325 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
328 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
330 else if (*domain
== '*')
332 if (domain
[1] == '.')
336 /* For a hostname to match a wildcard, the last domain must match
337 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
338 * hostname is www.foo.a.b, it matches, but a.b does not.
340 dot
= strchrW( server
, '.' );
343 int len
= strlenW( dot
+ 1 );
345 if (len
> strlenW( domain
+ 2 ))
349 /* The server's domain is longer than the wildcard, so it
350 * could be a subdomain. Compare the last portion of the
353 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
354 if (!strcmpiW( ptr
, domain
+ 2 ))
356 /* This is only a match if the preceding character is
357 * a '.', i.e. that it is a matching domain. E.g.
358 * if domain is '*.b.c' and server is 'www.ab.c' they
361 ret
= *(ptr
- 1) == '.';
365 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
370 ret
= !strcmpiW( server
, domain
);
374 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
375 #define MAX_HOST_NAME_LENGTH 256
377 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
382 if (!session
->proxy_bypass
) return FALSE
;
383 ptr
= session
->proxy_bypass
;
387 ptr
= strchrW( ptr
, ';' );
389 ptr
= strchrW( tmp
, ' ' );
392 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
394 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
396 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
397 domain
[ptr
- tmp
] = 0;
398 ret
= domain_matches( server
, domain
);
403 ret
= domain_matches( server
, tmp
);
404 } while (ptr
&& !ret
);
408 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
410 session_t
*session
= connect
->session
;
413 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
417 if ((colon
= strchrW( session
->proxy_server
, ':' )))
419 if (!connect
->servername
|| strncmpiW( connect
->servername
,
420 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
422 heap_free( connect
->servername
);
423 connect
->resolved
= FALSE
;
424 if (!(connect
->servername
= heap_alloc(
425 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
430 memcpy( connect
->servername
, session
->proxy_server
,
431 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
432 connect
->servername
[colon
- session
->proxy_server
] = 0;
434 connect
->serverport
= atoiW( colon
+ 1 );
436 connect
->serverport
= INTERNET_DEFAULT_PORT
;
441 if (!connect
->servername
|| strcmpiW( connect
->servername
,
442 session
->proxy_server
))
444 heap_free( connect
->servername
);
445 connect
->resolved
= FALSE
;
446 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
451 connect
->serverport
= INTERNET_DEFAULT_PORT
;
457 heap_free( connect
->servername
);
458 connect
->resolved
= FALSE
;
459 if (!(connect
->servername
= strdupW( server
)))
464 connect
->serverport
= port
;
470 /***********************************************************************
471 * WinHttpConnect (winhttp.@)
473 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
477 HINTERNET hconnect
= NULL
;
479 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
483 set_last_error( ERROR_INVALID_PARAMETER
);
486 if (!(session
= (session_t
*)grab_object( hsession
)))
488 set_last_error( ERROR_INVALID_HANDLE
);
491 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
493 release_object( &session
->hdr
);
494 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
497 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
499 release_object( &session
->hdr
);
502 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
503 connect
->hdr
.vtbl
= &connect_vtbl
;
504 connect
->hdr
.refs
= 1;
505 connect
->hdr
.flags
= session
->hdr
.flags
;
506 connect
->hdr
.callback
= session
->hdr
.callback
;
507 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
508 connect
->hdr
.context
= session
->hdr
.context
;
509 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
510 list_init( &connect
->hdr
.children
);
512 addref_object( &session
->hdr
);
513 connect
->session
= session
;
514 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
516 if (!(connect
->hostname
= strdupW( server
))) goto end
;
517 connect
->hostport
= port
;
518 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
520 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
521 connect
->hdr
.handle
= hconnect
;
523 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
526 release_object( &connect
->hdr
);
527 release_object( &session
->hdr
);
528 TRACE("returning %p\n", hconnect
);
532 /***********************************************************************
533 * request_destroy (internal)
535 static void request_destroy( object_header_t
*hdr
)
537 request_t
*request
= (request_t
*)hdr
;
540 TRACE("%p\n", request
);
542 release_object( &request
->connect
->hdr
);
544 destroy_authinfo( request
->authinfo
);
545 destroy_authinfo( request
->proxy_authinfo
);
547 heap_free( request
->verb
);
548 heap_free( request
->path
);
549 heap_free( request
->version
);
550 heap_free( request
->raw_headers
);
551 heap_free( request
->status_text
);
552 for (i
= 0; i
< request
->num_headers
; i
++)
554 heap_free( request
->headers
[i
].field
);
555 heap_free( request
->headers
[i
].value
);
557 heap_free( request
->headers
);
558 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
559 heap_free( request
->accept_types
);
560 heap_free( request
);
563 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
566 if (str
) len
= strlenW( str
);
567 if (buffer
&& *buflen
> len
)
569 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
572 *buflen
= len
* sizeof(WCHAR
);
575 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
578 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
580 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
581 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
582 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
587 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
589 #if !defined(__MINGW32__) && !defined(_MSC_VER)
590 switch (addr
->sa_family
)
594 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
595 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
598 addr_win
->sin_family
= WS_AF_INET
;
599 addr_win
->sin_port
= addr_unix
->sin_port
;
600 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
601 p
= (char *)&addr_win
->sin_addr
+ 4;
602 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
607 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
608 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
610 addr_win
->sin6_family
= WS_AF_INET6
;
611 addr_win
->sin6_port
= addr_unix
->sin6_port
;
612 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
613 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
614 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
615 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
617 addr_win
->sin6_scope_id
= 0;
619 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
623 ERR("unhandled family %u\n", addr
->sa_family
);
627 switch (addr
->sa_family
)
631 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
633 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
634 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
639 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
641 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
642 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
646 ERR("unhandled family %u\n", addr
->sa_family
);
652 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
654 request_t
*request
= (request_t
*)hdr
;
658 case WINHTTP_OPTION_SECURITY_FLAGS
:
663 if (!buffer
|| *buflen
< sizeof(flags
))
665 *buflen
= sizeof(flags
);
666 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
671 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
672 flags
|= request
->netconn
.security_flags
;
673 bits
= netconn_get_cipher_strength( &request
->netconn
);
675 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
677 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
679 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
680 *(DWORD
*)buffer
= flags
;
681 *buflen
= sizeof(flags
);
684 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
686 const CERT_CONTEXT
*cert
;
688 if (!buffer
|| *buflen
< sizeof(cert
))
690 *buflen
= sizeof(cert
);
691 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
695 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
696 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
697 *buflen
= sizeof(cert
);
700 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
702 const CERT_CONTEXT
*cert
;
703 const CRYPT_OID_INFO
*oidInfo
;
704 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
706 FIXME("partial stub\n");
708 if (!buffer
|| *buflen
< sizeof(*ci
))
710 *buflen
= sizeof(*ci
);
711 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
714 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
716 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
717 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
718 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
719 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
720 ci
->lpszProtocolName
= NULL
;
721 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
722 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
725 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
727 ci
->lpszSignatureAlgName
= NULL
;
728 ci
->lpszEncryptionAlgName
= NULL
;
729 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
731 CertFreeCertificateContext( cert
);
732 *buflen
= sizeof(*ci
);
735 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
737 if (!buffer
|| *buflen
< sizeof(DWORD
))
739 *buflen
= sizeof(DWORD
);
740 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
744 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
745 *buflen
= sizeof(DWORD
);
748 case WINHTTP_OPTION_CONNECTION_INFO
:
750 WINHTTP_CONNECTION_INFO
*info
= buffer
;
751 struct sockaddr local
;
752 socklen_t len
= sizeof(local
);
753 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
755 if (!buffer
|| *buflen
< sizeof(*info
))
757 *buflen
= sizeof(*info
);
758 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
761 if (!netconn_connected( &request
->netconn
))
763 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
766 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
767 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
768 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
769 info
->cbSize
= sizeof(*info
);
772 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
773 *(DWORD
*)buffer
= request
->resolve_timeout
;
774 *buflen
= sizeof(DWORD
);
776 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
777 *(DWORD
*)buffer
= request
->connect_timeout
;
778 *buflen
= sizeof(DWORD
);
780 case WINHTTP_OPTION_SEND_TIMEOUT
:
781 *(DWORD
*)buffer
= request
->send_timeout
;
782 *buflen
= sizeof(DWORD
);
784 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
785 *(DWORD
*)buffer
= request
->recv_timeout
;
786 *buflen
= sizeof(DWORD
);
789 case WINHTTP_OPTION_USERNAME
:
790 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
793 case WINHTTP_OPTION_PASSWORD
:
794 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
797 case WINHTTP_OPTION_PROXY_USERNAME
:
798 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
801 case WINHTTP_OPTION_PROXY_PASSWORD
:
802 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
806 FIXME("unimplemented option %u\n", option
);
807 set_last_error( ERROR_INVALID_PARAMETER
);
812 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
815 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
817 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
821 set_last_error( ERROR_OUTOFMEMORY
);
825 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
827 request_t
*request
= (request_t
*)hdr
;
831 case WINHTTP_OPTION_PROXY
:
833 WINHTTP_PROXY_INFO
*pi
= buffer
;
835 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
838 case WINHTTP_OPTION_DISABLE_FEATURE
:
842 if (buflen
!= sizeof(DWORD
))
844 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
848 disable
= *(DWORD
*)buffer
;
849 TRACE("0x%x\n", disable
);
850 hdr
->disable_flags
|= disable
;
853 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
857 if (buflen
!= sizeof(DWORD
))
859 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
863 policy
= *(DWORD
*)buffer
;
864 TRACE("0x%x\n", policy
);
865 hdr
->logon_policy
= policy
;
868 case WINHTTP_OPTION_REDIRECT_POLICY
:
872 if (buflen
!= sizeof(DWORD
))
874 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
878 policy
= *(DWORD
*)buffer
;
879 TRACE("0x%x\n", policy
);
880 hdr
->redirect_policy
= policy
;
883 case WINHTTP_OPTION_SECURITY_FLAGS
:
887 if (buflen
< sizeof(DWORD
))
889 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
892 flags
= *(DWORD
*)buffer
;
893 TRACE("0x%x\n", flags
);
894 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
895 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
896 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
897 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
899 set_last_error( ERROR_INVALID_PARAMETER
);
902 request
->netconn
.security_flags
= flags
;
905 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
906 request
->resolve_timeout
= *(DWORD
*)buffer
;
908 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
909 request
->connect_timeout
= *(DWORD
*)buffer
;
911 case WINHTTP_OPTION_SEND_TIMEOUT
:
912 request
->send_timeout
= *(DWORD
*)buffer
;
914 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
915 request
->recv_timeout
= *(DWORD
*)buffer
;
918 case WINHTTP_OPTION_USERNAME
:
920 connect_t
*connect
= request
->connect
;
922 heap_free( connect
->username
);
923 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
926 case WINHTTP_OPTION_PASSWORD
:
928 connect_t
*connect
= request
->connect
;
930 heap_free( connect
->password
);
931 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
934 case WINHTTP_OPTION_PROXY_USERNAME
:
936 session_t
*session
= request
->connect
->session
;
938 heap_free( session
->proxy_username
);
939 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
942 case WINHTTP_OPTION_PROXY_PASSWORD
:
944 session_t
*session
= request
->connect
->session
;
946 heap_free( session
->proxy_password
);
947 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
951 FIXME("unimplemented option %u\n", option
);
952 set_last_error( ERROR_INVALID_PARAMETER
);
957 static const object_vtbl_t request_vtbl
=
960 request_query_option
,
964 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
966 const WCHAR
**types
= accept_types
;
969 if (!types
) return TRUE
;
972 request
->num_accept_types
++;
975 if (!request
->num_accept_types
) return TRUE
;
976 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
978 request
->num_accept_types
= 0;
981 types
= accept_types
;
982 for (i
= 0; i
< request
->num_accept_types
; i
++)
984 if (!(request
->accept_types
[i
] = strdupW( *types
)))
986 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
987 heap_free( request
->accept_types
);
988 request
->accept_types
= NULL
;
989 request
->num_accept_types
= 0;
997 /***********************************************************************
998 * WinHttpOpenRequest (winhttp.@)
1000 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1001 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1005 HINTERNET hrequest
= NULL
;
1007 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1008 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1010 if(types
&& TRACE_ON(winhttp
)) {
1013 TRACE("accept types:\n");
1014 for(iter
= types
; *iter
; iter
++)
1015 TRACE(" %s\n", debugstr_w(*iter
));
1018 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1020 set_last_error( ERROR_INVALID_HANDLE
);
1023 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1025 release_object( &connect
->hdr
);
1026 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1029 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1031 release_object( &connect
->hdr
);
1034 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1035 request
->hdr
.vtbl
= &request_vtbl
;
1036 request
->hdr
.refs
= 1;
1037 request
->hdr
.flags
= flags
;
1038 request
->hdr
.callback
= connect
->hdr
.callback
;
1039 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1040 request
->hdr
.context
= connect
->hdr
.context
;
1041 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1042 list_init( &request
->hdr
.children
);
1044 addref_object( &connect
->hdr
);
1045 request
->connect
= connect
;
1046 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1048 if (!netconn_init( &request
->netconn
)) goto end
;
1049 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1050 request
->connect_timeout
= connect
->session
->connect_timeout
;
1051 request
->send_timeout
= connect
->session
->send_timeout
;
1052 request
->recv_timeout
= connect
->session
->recv_timeout
;
1054 if (!verb
|| !verb
[0]) verb
= getW
;
1055 if (!(request
->verb
= strdupW( verb
))) goto end
;
1062 len
= strlenW( object
) + 1;
1063 if (object
[0] != '/') len
++;
1064 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1066 if (object
[0] != '/') *p
++ = '/';
1067 strcpyW( p
, object
);
1068 request
->path
= path
;
1070 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1072 if (!version
|| !version
[0]) version
= http1_1
;
1073 if (!(request
->version
= strdupW( version
))) goto end
;
1074 if (!(store_accept_types( request
, types
))) goto end
;
1076 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1077 request
->hdr
.handle
= hrequest
;
1079 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1082 release_object( &request
->hdr
);
1083 release_object( &connect
->hdr
);
1084 TRACE("returning %p\n", hrequest
);
1088 /***********************************************************************
1089 * WinHttpCloseHandle (winhttp.@)
1091 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1093 object_header_t
*hdr
;
1095 TRACE("%p\n", handle
);
1097 if (!(hdr
= grab_object( handle
)))
1099 set_last_error( ERROR_INVALID_HANDLE
);
1102 release_object( hdr
);
1103 free_handle( handle
);
1107 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1113 set_last_error( ERROR_INVALID_PARAMETER
);
1119 case WINHTTP_OPTION_CONTEXT_VALUE
:
1121 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1123 *buflen
= sizeof(DWORD_PTR
);
1124 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1128 *(DWORD_PTR
*)buffer
= hdr
->context
;
1129 *buflen
= sizeof(DWORD_PTR
);
1133 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1136 FIXME("unimplemented option %u\n", option
);
1137 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1145 /***********************************************************************
1146 * WinHttpQueryOption (winhttp.@)
1148 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1151 object_header_t
*hdr
;
1153 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1155 if (!(hdr
= grab_object( handle
)))
1157 set_last_error( ERROR_INVALID_HANDLE
);
1161 ret
= query_option( hdr
, option
, buffer
, buflen
);
1163 release_object( hdr
);
1167 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1173 set_last_error( ERROR_INVALID_PARAMETER
);
1179 case WINHTTP_OPTION_CONTEXT_VALUE
:
1181 if (buflen
!= sizeof(DWORD_PTR
))
1183 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1187 hdr
->context
= *(DWORD_PTR
*)buffer
;
1191 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1194 FIXME("unimplemented option %u\n", option
);
1195 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1203 /***********************************************************************
1204 * WinHttpSetOption (winhttp.@)
1206 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1209 object_header_t
*hdr
;
1211 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1213 if (!(hdr
= grab_object( handle
)))
1215 set_last_error( ERROR_INVALID_HANDLE
);
1219 ret
= set_option( hdr
, option
, buffer
, buflen
);
1221 release_object( hdr
);
1225 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1230 GetComputerNameExA( format
, NULL
, &size
);
1231 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1232 if (!(ret
= heap_alloc( size
))) return NULL
;
1233 if (!GetComputerNameExA( format
, ret
, &size
))
1241 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1243 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1245 if (len_suffix
> len_domain
) return FALSE
;
1246 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1250 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
1253 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
1254 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
1255 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
1256 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
1259 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1262 #ifdef HAVE_GETNAMEINFO
1263 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1268 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1270 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1271 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1272 char name
[NI_MAXHOST
];
1276 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1277 if (!ai
) return NULL
;
1279 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1281 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1282 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1283 strcpyW( p
, httpW
);
1284 p
+= strlenW( httpW
);
1285 while (*hostname
) { *p
++ = *hostname
++; }
1286 strcpyW( p
, wpadW
);
1290 /***********************************************************************
1291 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1293 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1297 TRACE("0x%08x, %p\n", flags
, url
);
1301 set_last_error( ERROR_INVALID_PARAMETER
);
1304 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1306 static int fixme_shown
;
1307 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1309 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1311 #ifdef HAVE_GETADDRINFO
1312 char *fqdn
, *domain
, *p
;
1314 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1315 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1321 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1323 struct addrinfo
*ai
;
1327 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1330 heap_free( domain
);
1333 strcpy( name
, "wpad" );
1335 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1338 *url
= build_wpad_url( name
, ai
);
1342 TRACE("returning %s\n", debugstr_w(*url
));
1351 heap_free( domain
);
1354 FIXME("getaddrinfo not found at build time\n");
1359 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1365 static const WCHAR Connections
[] = {
1366 'S','o','f','t','w','a','r','e','\\',
1367 'M','i','c','r','o','s','o','f','t','\\',
1368 'W','i','n','d','o','w','s','\\',
1369 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1370 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1371 'C','o','n','n','e','c','t','i','o','n','s',0 };
1372 static const WCHAR WinHttpSettings
[] = {
1373 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1374 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1375 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1376 static const DWORD PROXY_TYPE_DIRECT
= 1;
1377 static const DWORD PROXY_TYPE_PROXY
= 2;
1378 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1379 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1381 struct connection_settings_header
1384 DWORD unknown
; /* always zero? */
1385 DWORD flags
; /* one or more of PROXY_* */
1388 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1392 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1397 /***********************************************************************
1398 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1400 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1404 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1407 TRACE("%p\n", info
);
1409 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1412 DWORD type
, size
= 0;
1414 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1415 if (!l
&& type
== REG_BINARY
&&
1416 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1418 BYTE
*buf
= heap_alloc( size
);
1422 struct connection_settings_header
*hdr
=
1423 (struct connection_settings_header
*)buf
;
1424 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1426 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1428 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1431 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1434 LPWSTR proxy
= NULL
;
1435 LPWSTR proxy_bypass
= NULL
;
1437 /* Sanity-check length of proxy string */
1438 if ((BYTE
*)len
- buf
+ *len
<= size
)
1441 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1443 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1444 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1448 /* Sanity-check length of proxy bypass string */
1449 if ((BYTE
*)len
- buf
+ *len
<= size
)
1451 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1453 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1458 GlobalFree( proxy
);
1462 info
->lpszProxy
= proxy
;
1463 info
->lpszProxyBypass
= proxy_bypass
;
1466 got_from_reg
= TRUE
;
1468 info
->dwAccessType
=
1469 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1470 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1471 debugstr_w(info
->lpszProxy
),
1472 debugstr_w(info
->lpszProxyBypass
));
1481 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1483 char *colon
, *http_proxy
;
1485 if ((colon
= strchr( envproxy
, ':' )))
1487 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1489 static const char http
[] = "http://";
1491 /* It's a scheme, check that it's http */
1492 if (!strncmp( envproxy
, http
, strlen( http
) ))
1493 http_proxy
= envproxy
+ strlen( http
);
1496 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1501 http_proxy
= envproxy
;
1504 http_proxy
= envproxy
;
1510 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1511 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1513 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1515 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1516 info
->lpszProxy
= http_proxyW
;
1517 info
->lpszProxyBypass
= NULL
;
1518 TRACE("http proxy (from environment) = %s\n",
1519 debugstr_w(info
->lpszProxy
));
1525 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1526 info
->lpszProxy
= NULL
;
1527 info
->lpszProxyBypass
= NULL
;
1532 /***********************************************************************
1533 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1535 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1537 static const WCHAR settingsW
[] =
1538 {'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};
1540 struct connection_settings_header
*hdr
= NULL
;
1541 DWORD type
, offset
, len
, size
= 0;
1544 TRACE("%p\n", config
);
1548 set_last_error( ERROR_INVALID_PARAMETER
);
1551 memset( config
, 0, sizeof(*config
) );
1552 config
->fAutoDetect
= TRUE
;
1554 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1555 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1556 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1561 if (!(hdr
= heap_alloc( size
))) goto done
;
1562 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1563 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1569 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1570 offset
= sizeof(*hdr
);
1571 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1572 len
= *(DWORD
*)((char *)hdr
+ offset
);
1573 offset
+= sizeof(DWORD
);
1574 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1576 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1577 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1580 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1581 len
= *(DWORD
*)((char *)hdr
+ offset
);
1582 offset
+= sizeof(DWORD
);
1583 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1585 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1586 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1589 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1590 len
= *(DWORD
*)((char *)hdr
+ offset
);
1591 offset
+= sizeof(DWORD
);
1592 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1594 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1595 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1600 RegCloseKey( hkey
);
1604 GlobalFree( config
->lpszAutoConfigUrl
);
1605 config
->lpszAutoConfigUrl
= NULL
;
1606 GlobalFree( config
->lpszProxy
);
1607 config
->lpszProxy
= NULL
;
1608 GlobalFree( config
->lpszProxyBypass
);
1609 config
->lpszProxyBypass
= NULL
;
1614 static HRESULT WINAPI
dispex_QueryInterface(
1615 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1619 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
1620 IsEqualGUID( riid
, &IID_IDispatch
) ||
1621 IsEqualGUID( riid
, &IID_IDispatchEx
))
1624 return E_NOINTERFACE
;
1629 static ULONG WINAPI
dispex_AddRef(
1630 IDispatchEx
*iface
)
1635 static ULONG WINAPI
dispex_Release(
1636 IDispatchEx
*iface
)
1641 static HRESULT WINAPI
dispex_GetTypeInfoCount(
1642 IDispatchEx
*iface
, UINT
*info
)
1647 static HRESULT WINAPI
dispex_GetTypeInfo(
1648 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
1653 static HRESULT WINAPI
dispex_GetIDsOfNames(
1654 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
1659 static HRESULT WINAPI
dispex_Invoke(
1660 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
1661 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
1666 static HRESULT WINAPI
dispex_DeleteMemberByName(
1667 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
1672 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
1673 IDispatchEx
*iface
, DISPID id
)
1678 static HRESULT WINAPI
dispex_GetMemberProperties(
1679 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
1684 static HRESULT WINAPI
dispex_GetMemberName(
1685 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
1690 static HRESULT WINAPI
dispex_GetNextDispID(
1691 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
1696 static HRESULT WINAPI
dispex_GetNameSpaceParent(
1697 IDispatchEx
*iface
, IUnknown
**unk
)
1702 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1704 static HRESULT WINAPI
dispex_GetDispID(
1705 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
1707 if (!strcmpW( name
, dns_resolveW
))
1709 *id
= DISPID_GLOBAL_DNSRESOLVE
;
1712 return DISP_E_UNKNOWNNAME
;
1715 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
1717 #ifdef HAVE_GETADDRINFO
1718 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1720 struct addrinfo
*ai
, *elem
;
1725 hostnameA
= strdupWA( hostname
);
1727 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
1729 if (!hostnameA
) return E_OUTOFMEMORY
;
1730 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
1731 heap_free( hostnameA
);
1732 if (res
) return S_FALSE
;
1735 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
1741 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
1743 V_VT( result
) = VT_BSTR
;
1744 V_BSTR( result
) = SysAllocString( addr
);
1747 FIXME("getaddrinfo not found at build time\n");
1752 static HRESULT WINAPI
dispex_InvokeEx(
1753 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
1754 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
1756 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
1758 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
1759 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
1760 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
1762 return DISP_E_MEMBERNOTFOUND
;
1765 static const IDispatchExVtbl dispex_vtbl
=
1767 dispex_QueryInterface
,
1770 dispex_GetTypeInfoCount
,
1772 dispex_GetIDsOfNames
,
1776 dispex_DeleteMemberByName
,
1777 dispex_DeleteMemberByDispID
,
1778 dispex_GetMemberProperties
,
1779 dispex_GetMemberName
,
1780 dispex_GetNextDispID
,
1781 dispex_GetNameSpaceParent
1784 static IDispatchEx global_dispex
= { &dispex_vtbl
};
1786 static HRESULT WINAPI
site_QueryInterface(
1787 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
1791 if (IsEqualGUID( &IID_IUnknown
, riid
))
1793 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
1796 return E_NOINTERFACE
;
1798 IUnknown_AddRef( (IUnknown
*)*ppv
);
1802 static ULONG WINAPI
site_AddRef(
1803 IActiveScriptSite
*iface
)
1808 static ULONG WINAPI
site_Release(
1809 IActiveScriptSite
*iface
)
1814 static HRESULT WINAPI
site_GetLCID(
1815 IActiveScriptSite
*iface
, LCID
*lcid
)
1820 static HRESULT WINAPI
site_GetItemInfo(
1821 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
1822 IUnknown
**item
, ITypeInfo
**type_info
)
1824 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
1826 *item
= (IUnknown
*)&global_dispex
;
1832 static HRESULT WINAPI
site_GetDocVersionString(
1833 IActiveScriptSite
*iface
, BSTR
*version
)
1838 static HRESULT WINAPI
site_OnScriptTerminate(
1839 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
1844 static HRESULT WINAPI
site_OnStateChange(
1845 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
1850 static HRESULT WINAPI
site_OnScriptError(
1851 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
1856 static HRESULT WINAPI
site_OnEnterScript(
1857 IActiveScriptSite
*iface
)
1862 static HRESULT WINAPI
site_OnLeaveScript(
1863 IActiveScriptSite
*iface
)
1868 static const IActiveScriptSiteVtbl site_vtbl
=
1870 site_QueryInterface
,
1875 site_GetDocVersionString
,
1876 site_OnScriptTerminate
,
1883 static IActiveScriptSite script_site
= { &site_vtbl
};
1885 static BOOL
parse_script_result( VARIANT result
, WINHTTP_PROXY_INFO
*info
)
1887 static const WCHAR proxyW
[] = {'P','R','O','X','Y'};
1892 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1893 info
->lpszProxy
= NULL
;
1894 info
->lpszProxyBypass
= NULL
;
1896 if (V_VT( &result
) != VT_BSTR
) return TRUE
;
1897 TRACE("%s\n", debugstr_w( V_BSTR( &result
) ));
1899 p
= V_BSTR( &result
);
1900 while (*p
== ' ') p
++;
1902 if (len
>= 5 && !memicmpW( p
, proxyW
, sizeof(proxyW
)/sizeof(WCHAR
) ))
1905 while (*p
== ' ') p
++;
1906 if (!*p
|| *p
== ';') return TRUE
;
1907 if (!(info
->lpszProxy
= q
= strdupW( p
))) return FALSE
;
1908 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1911 if (*q
== ' ' || *q
== ';')
1921 static BSTR
include_pac_utils( BSTR script
)
1923 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
1924 HMODULE hmod
= GetModuleHandleA( "winhttp.dll" );
1931 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
1932 size
= SizeofResource( hmod
, rsrc
);
1933 data
= LoadResource( hmod
, rsrc
);
1935 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
1936 if (!(ret
= SysAllocStringLen( NULL
, len
+ SysStringLen( script
) + 1 ))) return NULL
;
1937 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
1939 strcatW( ret
, script
);
1944 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1945 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1946 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1948 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1949 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1950 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1953 static BOOL
run_script( const BSTR script
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1955 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
1956 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1957 IActiveScriptParse
*parser
= NULL
;
1958 IActiveScript
*engine
= NULL
;
1959 IDispatch
*dispatch
= NULL
;
1963 BSTR func
= NULL
, hostname
= NULL
, full_script
= NULL
;
1965 VARIANT args
[2], result
;
1969 memset( &uc
, 0, sizeof(uc
) );
1970 uc
.dwStructSize
= sizeof(uc
);
1971 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return FALSE
;
1972 if (!(hostname
= SysAllocStringLen( NULL
, uc
.dwHostNameLength
+ 1 ))) return FALSE
;
1973 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1974 hostname
[uc
.dwHostNameLength
] = 0;
1976 init
= CoInitialize( NULL
);
1977 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
1978 if (hr
!= S_OK
) goto done
;
1980 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
1981 &IID_IActiveScript
, (void **)&engine
);
1982 if (hr
!= S_OK
) goto done
;
1984 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
1985 if (hr
!= S_OK
) goto done
;
1987 hr
= IActiveScriptParse_InitNew( parser
);
1988 if (hr
!= S_OK
) goto done
;
1990 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
1991 if (hr
!= S_OK
) goto done
;
1993 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
1994 if (hr
!= S_OK
) goto done
;
1996 if (!(full_script
= include_pac_utils( script
))) goto done
;
1998 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
1999 if (hr
!= S_OK
) goto done
;
2001 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
2002 if (hr
!= S_OK
) goto done
;
2004 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
2005 if (hr
!= S_OK
) goto done
;
2007 if (!(func
= SysAllocString( findproxyW
))) goto done
;
2008 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
2009 if (hr
!= S_OK
) goto done
;
2011 V_VT( &args
[0] ) = VT_BSTR
;
2012 V_BSTR( &args
[0] ) = hostname
;
2013 V_VT( &args
[1] ) = VT_BSTR
;
2014 V_BSTR( &args
[1] ) = SysAllocString( url
);
2016 params
.rgvarg
= args
;
2017 params
.rgdispidNamedArgs
= NULL
;
2019 params
.cNamedArgs
= 0;
2020 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
2021 ¶ms
, &result
, NULL
, NULL
);
2022 VariantClear( &args
[1] );
2025 WARN("script failed 0x%08x\n", hr
);
2028 ret
= parse_script_result( result
, info
);
2031 SysFreeString( full_script
);
2032 SysFreeString( hostname
);
2033 SysFreeString( func
);
2034 if (dispatch
) IDispatch_Release( dispatch
);
2035 if (parser
) IActiveScriptParse_Release( parser
);
2036 if (engine
) IActiveScript_Release( engine
);
2037 if (SUCCEEDED( init
)) CoUninitialize();
2038 if (!ret
) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT
);
2042 static BSTR
download_script( const WCHAR
*url
)
2044 static const WCHAR typeW
[] = {'*','/','*',0};
2045 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
2046 HINTERNET ses
, con
= NULL
, req
= NULL
;
2049 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
2050 char *tmp
, *buffer
= NULL
;
2054 memset( &uc
, 0, sizeof(uc
) );
2055 uc
.dwStructSize
= sizeof(uc
);
2056 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
2057 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
2058 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
2059 hostname
[uc
.dwHostNameLength
] = 0;
2061 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
2062 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
2063 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
2064 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
2065 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
2067 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
2068 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
2069 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
2072 if (!(buffer
= heap_alloc( size
))) goto done
;
2077 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
2078 if (!bytes_read
) break;
2079 to_read
-= bytes_read
;
2080 offset
+= bytes_read
;
2085 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
2089 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, NULL
, 0 );
2090 if (!(script
= SysAllocStringLen( NULL
, len
))) goto done
;
2091 MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, script
, len
);
2095 WinHttpCloseHandle( req
);
2096 WinHttpCloseHandle( con
);
2097 WinHttpCloseHandle( ses
);
2098 heap_free( buffer
);
2099 heap_free( hostname
);
2100 if (!script
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
2104 /***********************************************************************
2105 * WinHttpGetProxyForUrl (winhttp.@)
2107 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2108 WINHTTP_PROXY_INFO
*info
)
2110 WCHAR
*detected_pac_url
= NULL
;
2111 const WCHAR
*pac_url
;
2116 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
2118 if (!(session
= (session_t
*)grab_object( hsession
)))
2120 set_last_error( ERROR_INVALID_HANDLE
);
2123 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
2125 release_object( &session
->hdr
);
2126 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2129 if (!url
|| !options
|| !info
||
2130 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2131 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
2132 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
2133 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
2135 release_object( &session
->hdr
);
2136 set_last_error( ERROR_INVALID_PARAMETER
);
2139 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
2140 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
2143 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
2144 else pac_url
= detected_pac_url
;
2146 if (!(script
= download_script( pac_url
))) goto done
;
2147 ret
= run_script( script
, url
, info
);
2148 SysFreeString( script
);
2151 GlobalFree( detected_pac_url
);
2152 release_object( &session
->hdr
);
2156 /***********************************************************************
2157 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2159 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
2166 TRACE("%p\n", info
);
2170 set_last_error( ERROR_INVALID_PARAMETER
);
2173 switch (info
->dwAccessType
)
2175 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
2177 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
2178 if (!info
->lpszProxy
)
2180 set_last_error( ERROR_INVALID_PARAMETER
);
2183 /* Only ASCII characters are allowed */
2184 for (src
= info
->lpszProxy
; *src
; src
++)
2187 set_last_error( ERROR_INVALID_PARAMETER
);
2190 if (info
->lpszProxyBypass
)
2192 for (src
= info
->lpszProxyBypass
; *src
; src
++)
2195 set_last_error( ERROR_INVALID_PARAMETER
);
2201 set_last_error( ERROR_INVALID_PARAMETER
);
2205 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
2206 KEY_WRITE
, NULL
, &key
, NULL
);
2209 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
2212 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2214 size
+= strlenW( info
->lpszProxy
);
2215 if (info
->lpszProxyBypass
)
2216 size
+= strlenW( info
->lpszProxyBypass
);
2218 buf
= heap_alloc( size
);
2221 struct connection_settings_header
*hdr
=
2222 (struct connection_settings_header
*)buf
;
2223 DWORD
*len
= (DWORD
*)(hdr
+ 1);
2225 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
2227 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2231 hdr
->flags
= PROXY_TYPE_PROXY
;
2232 *len
++ = strlenW( info
->lpszProxy
);
2233 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
2237 if (info
->lpszProxyBypass
)
2239 *len
++ = strlenW( info
->lpszProxyBypass
);
2240 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
2249 hdr
->flags
= PROXY_TYPE_DIRECT
;
2253 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
2263 /***********************************************************************
2264 * WinHttpSetStatusCallback (winhttp.@)
2266 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
2267 DWORD flags
, DWORD_PTR reserved
)
2269 object_header_t
*hdr
;
2270 WINHTTP_STATUS_CALLBACK ret
;
2272 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
2274 if (!(hdr
= grab_object( handle
)))
2276 set_last_error( ERROR_INVALID_HANDLE
);
2277 return WINHTTP_INVALID_STATUS_CALLBACK
;
2279 ret
= hdr
->callback
;
2280 hdr
->callback
= callback
;
2281 hdr
->notify_mask
= flags
;
2283 release_object( hdr
);
2287 /***********************************************************************
2288 * WinHttpSetTimeouts (winhttp.@)
2290 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2293 object_header_t
*hdr
;
2297 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2299 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2301 set_last_error( ERROR_INVALID_PARAMETER
);
2305 if (!(hdr
= grab_object( handle
)))
2307 set_last_error( ERROR_INVALID_HANDLE
);
2313 case WINHTTP_HANDLE_TYPE_REQUEST
:
2314 request
= (request_t
*)hdr
;
2315 request
->connect_timeout
= connect
;
2317 if (resolve
< 0) resolve
= 0;
2318 request
->resolve_timeout
= resolve
;
2320 if (send
< 0) send
= 0;
2321 request
->send_timeout
= send
;
2323 if (receive
< 0) receive
= 0;
2324 request
->recv_timeout
= receive
;
2326 if (netconn_connected( &request
->netconn
))
2328 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2329 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2332 release_object( &request
->hdr
);
2335 case WINHTTP_HANDLE_TYPE_SESSION
:
2336 session
= (session_t
*)hdr
;
2337 session
->connect_timeout
= connect
;
2339 if (resolve
< 0) resolve
= 0;
2340 session
->resolve_timeout
= resolve
;
2342 if (send
< 0) send
= 0;
2343 session
->send_timeout
= send
;
2345 if (receive
< 0) receive
= 0;
2346 session
->recv_timeout
= receive
;
2350 release_object( hdr
);
2351 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2357 static const WCHAR wkday
[7][4] =
2358 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2359 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2360 static const WCHAR month
[12][4] =
2361 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2362 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2363 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2365 /***********************************************************************
2366 * WinHttpTimeFromSystemTime (WININET.@)
2368 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2370 static const WCHAR format
[] =
2371 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2372 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2374 TRACE("%p, %p\n", time
, string
);
2376 if (!time
|| !string
) return FALSE
;
2378 sprintfW( string
, format
,
2379 wkday
[time
->wDayOfWeek
],
2381 month
[time
->wMonth
- 1],
2390 /***********************************************************************
2391 * WinHttpTimeToSystemTime (WININET.@)
2393 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2396 const WCHAR
*s
= string
;
2399 TRACE("%s, %p\n", debugstr_w(string
), time
);
2401 if (!string
|| !time
) return FALSE
;
2403 /* Windows does this too */
2404 GetSystemTime( time
);
2406 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2407 * a SYSTEMTIME structure.
2410 while (*s
&& !isalphaW( *s
)) s
++;
2411 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2412 time
->wDayOfWeek
= 7;
2414 for (i
= 0; i
< 7; i
++)
2416 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2417 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2418 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2420 time
->wDayOfWeek
= i
;
2425 if (time
->wDayOfWeek
> 6) return TRUE
;
2426 while (*s
&& !isdigitW( *s
)) s
++;
2427 time
->wDay
= strtolW( s
, &end
, 10 );
2430 while (*s
&& !isalphaW( *s
)) s
++;
2431 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2434 for (i
= 0; i
< 12; i
++)
2436 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2437 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2438 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2440 time
->wMonth
= i
+ 1;
2444 if (time
->wMonth
== 0) return TRUE
;
2446 while (*s
&& !isdigitW( *s
)) s
++;
2447 if (*s
== '\0') return TRUE
;
2448 time
->wYear
= strtolW( s
, &end
, 10 );
2451 while (*s
&& !isdigitW( *s
)) s
++;
2452 if (*s
== '\0') return TRUE
;
2453 time
->wHour
= strtolW( s
, &end
, 10 );
2456 while (*s
&& !isdigitW( *s
)) s
++;
2457 if (*s
== '\0') return TRUE
;
2458 time
->wMinute
= strtolW( s
, &end
, 10 );
2461 while (*s
&& !isdigitW( *s
)) s
++;
2462 if (*s
== '\0') return TRUE
;
2463 time
->wSecond
= strtolW( s
, &end
, 10 );
2465 time
->wMilliseconds
= 0;