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 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
24 //#include "wine/port.h"
25 #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
);
108 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
110 session_t
*session
= (session_t
*)hdr
;
114 case WINHTTP_OPTION_REDIRECT_POLICY
:
116 if (!buffer
|| *buflen
< sizeof(DWORD
))
118 *buflen
= sizeof(DWORD
);
119 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
123 *(DWORD
*)buffer
= hdr
->redirect_policy
;
124 *buflen
= sizeof(DWORD
);
127 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
128 *(DWORD
*)buffer
= session
->resolve_timeout
;
129 *buflen
= sizeof(DWORD
);
131 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
132 *(DWORD
*)buffer
= session
->connect_timeout
;
133 *buflen
= sizeof(DWORD
);
135 case WINHTTP_OPTION_SEND_TIMEOUT
:
136 *(DWORD
*)buffer
= session
->send_timeout
;
137 *buflen
= sizeof(DWORD
);
139 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
140 *(DWORD
*)buffer
= session
->recv_timeout
;
141 *buflen
= sizeof(DWORD
);
144 FIXME("unimplemented option %u\n", option
);
145 set_last_error( ERROR_INVALID_PARAMETER
);
150 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
152 session_t
*session
= (session_t
*)hdr
;
156 case WINHTTP_OPTION_PROXY
:
158 WINHTTP_PROXY_INFO
*pi
= buffer
;
160 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
163 case WINHTTP_OPTION_REDIRECT_POLICY
:
167 if (buflen
!= sizeof(policy
))
169 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
173 policy
= *(DWORD
*)buffer
;
174 TRACE("0x%x\n", policy
);
175 hdr
->redirect_policy
= policy
;
178 case WINHTTP_OPTION_DISABLE_FEATURE
:
179 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
181 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
182 session
->resolve_timeout
= *(DWORD
*)buffer
;
184 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
185 session
->connect_timeout
= *(DWORD
*)buffer
;
187 case WINHTTP_OPTION_SEND_TIMEOUT
:
188 session
->send_timeout
= *(DWORD
*)buffer
;
190 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
191 session
->recv_timeout
= *(DWORD
*)buffer
;
193 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
194 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
197 FIXME("unimplemented option %u\n", option
);
198 set_last_error( ERROR_INVALID_PARAMETER
);
203 static const object_vtbl_t session_vtbl
=
206 session_query_option
,
210 /***********************************************************************
211 * WinHttpOpen (winhttp.@)
213 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
216 HINTERNET handle
= NULL
;
219 int error
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
220 if (error
) ERR("WSAStartup failed: %d\n", error
);
223 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
225 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
227 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
228 session
->hdr
.vtbl
= &session_vtbl
;
229 session
->hdr
.flags
= flags
;
230 session
->hdr
.refs
= 1;
231 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
232 list_init( &session
->hdr
.children
);
233 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
234 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
235 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
236 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
237 list_init( &session
->cookie_cache
);
239 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
240 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
242 WINHTTP_PROXY_INFO info
;
244 WinHttpGetDefaultProxyConfiguration( &info
);
245 session
->access
= info
.dwAccessType
;
246 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
248 GlobalFree( (LPWSTR
)info
.lpszProxy
);
249 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
252 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
254 GlobalFree( (LPWSTR
)info
.lpszProxy
);
255 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
259 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
261 session
->access
= access
;
262 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
263 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
266 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
267 session
->hdr
.handle
= handle
;
270 release_object( &session
->hdr
);
271 TRACE("returning %p\n", handle
);
275 /***********************************************************************
276 * connect_destroy (internal)
278 static void connect_destroy( object_header_t
*hdr
)
280 connect_t
*connect
= (connect_t
*)hdr
;
282 TRACE("%p\n", connect
);
284 release_object( &connect
->session
->hdr
);
286 heap_free( connect
->hostname
);
287 heap_free( connect
->servername
);
288 heap_free( connect
->username
);
289 heap_free( connect
->password
);
290 heap_free( connect
);
293 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
295 connect_t
*connect
= (connect_t
*)hdr
;
299 case WINHTTP_OPTION_PARENT_HANDLE
:
301 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
303 *buflen
= sizeof(HINTERNET
);
304 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
308 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
309 *buflen
= sizeof(HINTERNET
);
312 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
313 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
314 *buflen
= sizeof(DWORD
);
316 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
317 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
318 *buflen
= sizeof(DWORD
);
320 case WINHTTP_OPTION_SEND_TIMEOUT
:
321 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
322 *buflen
= sizeof(DWORD
);
324 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
325 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
326 *buflen
= sizeof(DWORD
);
329 FIXME("unimplemented option %u\n", option
);
330 set_last_error( ERROR_INVALID_PARAMETER
);
335 static const object_vtbl_t connect_vtbl
=
338 connect_query_option
,
342 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
344 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
347 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
349 else if (*domain
== '*')
351 if (domain
[1] == '.')
355 /* For a hostname to match a wildcard, the last domain must match
356 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
357 * hostname is www.foo.a.b, it matches, but a.b does not.
359 dot
= strchrW( server
, '.' );
362 int len
= strlenW( dot
+ 1 );
364 if (len
> strlenW( domain
+ 2 ))
368 /* The server's domain is longer than the wildcard, so it
369 * could be a subdomain. Compare the last portion of the
372 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
373 if (!strcmpiW( ptr
, domain
+ 2 ))
375 /* This is only a match if the preceding character is
376 * a '.', i.e. that it is a matching domain. E.g.
377 * if domain is '*.b.c' and server is 'www.ab.c' they
380 ret
= *(ptr
- 1) == '.';
384 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
389 ret
= !strcmpiW( server
, domain
);
393 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
394 #define MAX_HOST_NAME_LENGTH 256
396 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
401 if (!session
->proxy_bypass
) return FALSE
;
402 ptr
= session
->proxy_bypass
;
406 ptr
= strchrW( ptr
, ';' );
408 ptr
= strchrW( tmp
, ' ' );
411 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
413 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
415 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
416 domain
[ptr
- tmp
] = 0;
417 ret
= domain_matches( server
, domain
);
422 ret
= domain_matches( server
, tmp
);
423 } while (ptr
&& !ret
);
427 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
429 session_t
*session
= connect
->session
;
432 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
436 if ((colon
= strchrW( session
->proxy_server
, ':' )))
438 if (!connect
->servername
|| strncmpiW( connect
->servername
,
439 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
441 heap_free( connect
->servername
);
442 connect
->resolved
= FALSE
;
443 if (!(connect
->servername
= heap_alloc(
444 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
449 memcpy( connect
->servername
, session
->proxy_server
,
450 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
451 connect
->servername
[colon
- session
->proxy_server
] = 0;
453 connect
->serverport
= atoiW( colon
+ 1 );
455 connect
->serverport
= INTERNET_DEFAULT_PORT
;
460 if (!connect
->servername
|| strcmpiW( connect
->servername
,
461 session
->proxy_server
))
463 heap_free( connect
->servername
);
464 connect
->resolved
= FALSE
;
465 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
470 connect
->serverport
= INTERNET_DEFAULT_PORT
;
476 heap_free( connect
->servername
);
477 connect
->resolved
= FALSE
;
478 if (!(connect
->servername
= strdupW( server
)))
483 connect
->serverport
= port
;
489 /***********************************************************************
490 * WinHttpConnect (winhttp.@)
492 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
496 HINTERNET hconnect
= NULL
;
498 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
502 set_last_error( ERROR_INVALID_PARAMETER
);
505 if (!(session
= (session_t
*)grab_object( hsession
)))
507 set_last_error( ERROR_INVALID_HANDLE
);
510 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
512 release_object( &session
->hdr
);
513 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
516 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
518 release_object( &session
->hdr
);
521 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
522 connect
->hdr
.vtbl
= &connect_vtbl
;
523 connect
->hdr
.refs
= 1;
524 connect
->hdr
.flags
= session
->hdr
.flags
;
525 connect
->hdr
.callback
= session
->hdr
.callback
;
526 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
527 connect
->hdr
.context
= session
->hdr
.context
;
528 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
529 list_init( &connect
->hdr
.children
);
531 addref_object( &session
->hdr
);
532 connect
->session
= session
;
533 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
535 if (!(connect
->hostname
= strdupW( server
))) goto end
;
536 connect
->hostport
= port
;
537 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
539 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
540 connect
->hdr
.handle
= hconnect
;
542 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
545 release_object( &connect
->hdr
);
546 release_object( &session
->hdr
);
547 TRACE("returning %p\n", hconnect
);
551 /***********************************************************************
552 * request_destroy (internal)
554 static void request_destroy( object_header_t
*hdr
)
556 request_t
*request
= (request_t
*)hdr
;
559 TRACE("%p\n", request
);
561 release_object( &request
->connect
->hdr
);
563 destroy_authinfo( request
->authinfo
);
564 destroy_authinfo( request
->proxy_authinfo
);
566 heap_free( request
->verb
);
567 heap_free( request
->path
);
568 heap_free( request
->version
);
569 heap_free( request
->raw_headers
);
570 heap_free( request
->status_text
);
571 for (i
= 0; i
< request
->num_headers
; i
++)
573 heap_free( request
->headers
[i
].field
);
574 heap_free( request
->headers
[i
].value
);
576 heap_free( request
->headers
);
577 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
578 heap_free( request
->accept_types
);
579 heap_free( request
);
582 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
585 if (str
) len
= strlenW( str
);
586 if (buffer
&& *buflen
> len
)
588 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
591 *buflen
= len
* sizeof(WCHAR
);
594 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
597 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
599 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
600 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
601 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
606 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
608 #if !defined(__MINGW32__) && !defined(_MSC_VER)
609 switch (addr
->sa_family
)
613 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
614 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
617 addr_win
->sin_family
= WS_AF_INET
;
618 addr_win
->sin_port
= addr_unix
->sin_port
;
619 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
620 p
= (char *)&addr_win
->sin_addr
+ 4;
621 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
626 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
627 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
629 addr_win
->sin6_family
= WS_AF_INET6
;
630 addr_win
->sin6_port
= addr_unix
->sin6_port
;
631 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
632 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
633 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
634 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
636 addr_win
->sin6_scope_id
= 0;
638 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
642 ERR("unhandled family %u\n", addr
->sa_family
);
646 switch (addr
->sa_family
)
650 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
652 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
653 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
658 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
660 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
661 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
665 ERR("unhandled family %u\n", addr
->sa_family
);
671 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
673 request_t
*request
= (request_t
*)hdr
;
677 case WINHTTP_OPTION_SECURITY_FLAGS
:
682 if (!buffer
|| *buflen
< sizeof(flags
))
684 *buflen
= sizeof(flags
);
685 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
690 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
691 flags
|= request
->netconn
.security_flags
;
692 bits
= netconn_get_cipher_strength( &request
->netconn
);
694 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
696 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
698 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
699 *(DWORD
*)buffer
= flags
;
700 *buflen
= sizeof(flags
);
703 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
705 const CERT_CONTEXT
*cert
;
707 if (!buffer
|| *buflen
< sizeof(cert
))
709 *buflen
= sizeof(cert
);
710 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
714 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
715 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
716 *buflen
= sizeof(cert
);
719 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
721 const CERT_CONTEXT
*cert
;
722 const CRYPT_OID_INFO
*oidInfo
;
723 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
725 FIXME("partial stub\n");
727 if (!buffer
|| *buflen
< sizeof(*ci
))
729 *buflen
= sizeof(*ci
);
730 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
733 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
735 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
736 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
737 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
738 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
739 ci
->lpszProtocolName
= NULL
;
740 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
741 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
744 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
746 ci
->lpszSignatureAlgName
= NULL
;
747 ci
->lpszEncryptionAlgName
= NULL
;
748 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
750 CertFreeCertificateContext( cert
);
751 *buflen
= sizeof(*ci
);
754 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
756 if (!buffer
|| *buflen
< sizeof(DWORD
))
758 *buflen
= sizeof(DWORD
);
759 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
763 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
764 *buflen
= sizeof(DWORD
);
767 case WINHTTP_OPTION_CONNECTION_INFO
:
769 WINHTTP_CONNECTION_INFO
*info
= buffer
;
770 struct sockaddr local
;
771 socklen_t len
= sizeof(local
);
772 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
774 if (!buffer
|| *buflen
< sizeof(*info
))
776 *buflen
= sizeof(*info
);
777 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
780 if (!netconn_connected( &request
->netconn
))
782 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
785 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
786 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
787 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
788 info
->cbSize
= sizeof(*info
);
791 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
792 *(DWORD
*)buffer
= request
->resolve_timeout
;
793 *buflen
= sizeof(DWORD
);
795 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
796 *(DWORD
*)buffer
= request
->connect_timeout
;
797 *buflen
= sizeof(DWORD
);
799 case WINHTTP_OPTION_SEND_TIMEOUT
:
800 *(DWORD
*)buffer
= request
->send_timeout
;
801 *buflen
= sizeof(DWORD
);
803 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
804 *(DWORD
*)buffer
= request
->recv_timeout
;
805 *buflen
= sizeof(DWORD
);
808 case WINHTTP_OPTION_USERNAME
:
809 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
812 case WINHTTP_OPTION_PASSWORD
:
813 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
816 case WINHTTP_OPTION_PROXY_USERNAME
:
817 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
820 case WINHTTP_OPTION_PROXY_PASSWORD
:
821 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
825 FIXME("unimplemented option %u\n", option
);
826 set_last_error( ERROR_INVALID_PARAMETER
);
831 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
834 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
836 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
840 set_last_error( ERROR_OUTOFMEMORY
);
844 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
846 request_t
*request
= (request_t
*)hdr
;
850 case WINHTTP_OPTION_PROXY
:
852 WINHTTP_PROXY_INFO
*pi
= buffer
;
854 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
857 case WINHTTP_OPTION_DISABLE_FEATURE
:
861 if (buflen
!= sizeof(DWORD
))
863 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
867 disable
= *(DWORD
*)buffer
;
868 TRACE("0x%x\n", disable
);
869 hdr
->disable_flags
|= disable
;
872 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
876 if (buflen
!= sizeof(DWORD
))
878 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
882 policy
= *(DWORD
*)buffer
;
883 TRACE("0x%x\n", policy
);
884 hdr
->logon_policy
= policy
;
887 case WINHTTP_OPTION_REDIRECT_POLICY
:
891 if (buflen
!= sizeof(DWORD
))
893 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
897 policy
= *(DWORD
*)buffer
;
898 TRACE("0x%x\n", policy
);
899 hdr
->redirect_policy
= policy
;
902 case WINHTTP_OPTION_SECURITY_FLAGS
:
906 if (buflen
< sizeof(DWORD
))
908 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
911 flags
= *(DWORD
*)buffer
;
912 TRACE("0x%x\n", flags
);
913 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
914 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
915 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
916 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
918 set_last_error( ERROR_INVALID_PARAMETER
);
921 request
->netconn
.security_flags
= flags
;
924 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
925 request
->resolve_timeout
= *(DWORD
*)buffer
;
927 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
928 request
->connect_timeout
= *(DWORD
*)buffer
;
930 case WINHTTP_OPTION_SEND_TIMEOUT
:
931 request
->send_timeout
= *(DWORD
*)buffer
;
933 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
934 request
->recv_timeout
= *(DWORD
*)buffer
;
937 case WINHTTP_OPTION_USERNAME
:
939 connect_t
*connect
= request
->connect
;
941 heap_free( connect
->username
);
942 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
945 case WINHTTP_OPTION_PASSWORD
:
947 connect_t
*connect
= request
->connect
;
949 heap_free( connect
->password
);
950 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
953 case WINHTTP_OPTION_PROXY_USERNAME
:
955 session_t
*session
= request
->connect
->session
;
957 heap_free( session
->proxy_username
);
958 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
961 case WINHTTP_OPTION_PROXY_PASSWORD
:
963 session_t
*session
= request
->connect
->session
;
965 heap_free( session
->proxy_password
);
966 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
970 FIXME("unimplemented option %u\n", option
);
971 set_last_error( ERROR_INVALID_PARAMETER
);
976 static const object_vtbl_t request_vtbl
=
979 request_query_option
,
983 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
985 const WCHAR
**types
= accept_types
;
988 if (!types
) return TRUE
;
991 request
->num_accept_types
++;
994 if (!request
->num_accept_types
) return TRUE
;
995 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
997 request
->num_accept_types
= 0;
1000 types
= accept_types
;
1001 for (i
= 0; i
< request
->num_accept_types
; i
++)
1003 if (!(request
->accept_types
[i
] = strdupW( *types
)))
1005 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
1006 heap_free( request
->accept_types
);
1007 request
->accept_types
= NULL
;
1008 request
->num_accept_types
= 0;
1016 /***********************************************************************
1017 * WinHttpOpenRequest (winhttp.@)
1019 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1020 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1024 HINTERNET hrequest
= NULL
;
1026 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1027 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1029 if(types
&& TRACE_ON(winhttp
)) {
1032 TRACE("accept types:\n");
1033 for(iter
= types
; *iter
; iter
++)
1034 TRACE(" %s\n", debugstr_w(*iter
));
1037 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1039 set_last_error( ERROR_INVALID_HANDLE
);
1042 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1044 release_object( &connect
->hdr
);
1045 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1048 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1050 release_object( &connect
->hdr
);
1053 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1054 request
->hdr
.vtbl
= &request_vtbl
;
1055 request
->hdr
.refs
= 1;
1056 request
->hdr
.flags
= flags
;
1057 request
->hdr
.callback
= connect
->hdr
.callback
;
1058 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1059 request
->hdr
.context
= connect
->hdr
.context
;
1060 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1061 list_init( &request
->hdr
.children
);
1063 addref_object( &connect
->hdr
);
1064 request
->connect
= connect
;
1065 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1067 if (!netconn_init( &request
->netconn
)) goto end
;
1068 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1069 request
->connect_timeout
= connect
->session
->connect_timeout
;
1070 request
->send_timeout
= connect
->session
->send_timeout
;
1071 request
->recv_timeout
= connect
->session
->recv_timeout
;
1073 if (!verb
|| !verb
[0]) verb
= getW
;
1074 if (!(request
->verb
= strdupW( verb
))) goto end
;
1081 len
= strlenW( object
) + 1;
1082 if (object
[0] != '/') len
++;
1083 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1085 if (object
[0] != '/') *p
++ = '/';
1086 strcpyW( p
, object
);
1087 request
->path
= path
;
1089 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1091 if (!version
|| !version
[0]) version
= http1_1
;
1092 if (!(request
->version
= strdupW( version
))) goto end
;
1093 if (!(store_accept_types( request
, types
))) goto end
;
1095 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1096 request
->hdr
.handle
= hrequest
;
1098 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1101 release_object( &request
->hdr
);
1102 release_object( &connect
->hdr
);
1103 TRACE("returning %p\n", hrequest
);
1107 /***********************************************************************
1108 * WinHttpCloseHandle (winhttp.@)
1110 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1112 object_header_t
*hdr
;
1114 TRACE("%p\n", handle
);
1116 if (!(hdr
= grab_object( handle
)))
1118 set_last_error( ERROR_INVALID_HANDLE
);
1121 release_object( hdr
);
1122 free_handle( handle
);
1126 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1132 set_last_error( ERROR_INVALID_PARAMETER
);
1138 case WINHTTP_OPTION_CONTEXT_VALUE
:
1140 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1142 *buflen
= sizeof(DWORD_PTR
);
1143 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1147 *(DWORD_PTR
*)buffer
= hdr
->context
;
1148 *buflen
= sizeof(DWORD_PTR
);
1152 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1155 FIXME("unimplemented option %u\n", option
);
1156 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1164 /***********************************************************************
1165 * WinHttpQueryOption (winhttp.@)
1167 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1170 object_header_t
*hdr
;
1172 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1174 if (!(hdr
= grab_object( handle
)))
1176 set_last_error( ERROR_INVALID_HANDLE
);
1180 ret
= query_option( hdr
, option
, buffer
, buflen
);
1182 release_object( hdr
);
1186 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1192 set_last_error( ERROR_INVALID_PARAMETER
);
1198 case WINHTTP_OPTION_CONTEXT_VALUE
:
1200 if (buflen
!= sizeof(DWORD_PTR
))
1202 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1206 hdr
->context
= *(DWORD_PTR
*)buffer
;
1210 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1213 FIXME("unimplemented option %u\n", option
);
1214 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1222 /***********************************************************************
1223 * WinHttpSetOption (winhttp.@)
1225 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1228 object_header_t
*hdr
;
1230 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1232 if (!(hdr
= grab_object( handle
)))
1234 set_last_error( ERROR_INVALID_HANDLE
);
1238 ret
= set_option( hdr
, option
, buffer
, buflen
);
1240 release_object( hdr
);
1244 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1249 GetComputerNameExA( format
, NULL
, &size
);
1250 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1251 if (!(ret
= heap_alloc( size
))) return NULL
;
1252 if (!GetComputerNameExA( format
, ret
, &size
))
1260 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1262 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1264 if (len_suffix
> len_domain
) return FALSE
;
1265 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1269 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
1272 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
1273 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
1274 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
1275 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
1278 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1281 #ifdef HAVE_GETNAMEINFO
1282 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1287 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1289 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1290 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1291 char name
[NI_MAXHOST
];
1295 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1296 if (!ai
) return NULL
;
1298 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1300 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1301 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1302 strcpyW( p
, httpW
);
1303 p
+= strlenW( httpW
);
1304 while (*hostname
) { *p
++ = *hostname
++; }
1305 strcpyW( p
, wpadW
);
1309 /***********************************************************************
1310 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1312 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1316 TRACE("0x%08x, %p\n", flags
, url
);
1320 set_last_error( ERROR_INVALID_PARAMETER
);
1323 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1325 static int fixme_shown
;
1326 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1328 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1330 #ifdef HAVE_GETADDRINFO
1331 char *fqdn
, *domain
, *p
;
1333 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1334 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1340 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1342 struct addrinfo
*ai
;
1346 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1349 heap_free( domain
);
1352 strcpy( name
, "wpad" );
1354 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1357 *url
= build_wpad_url( name
, ai
);
1361 TRACE("returning %s\n", debugstr_w(*url
));
1370 heap_free( domain
);
1373 FIXME("getaddrinfo not found at build time\n");
1376 if (!ret
) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1380 static const WCHAR Connections
[] = {
1381 'S','o','f','t','w','a','r','e','\\',
1382 'M','i','c','r','o','s','o','f','t','\\',
1383 'W','i','n','d','o','w','s','\\',
1384 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1385 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1386 'C','o','n','n','e','c','t','i','o','n','s',0 };
1387 static const WCHAR WinHttpSettings
[] = {
1388 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1389 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1390 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1391 static const DWORD PROXY_TYPE_DIRECT
= 1;
1392 static const DWORD PROXY_TYPE_PROXY
= 2;
1393 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1394 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1396 struct connection_settings_header
1399 DWORD unknown
; /* always zero? */
1400 DWORD flags
; /* one or more of PROXY_* */
1403 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1407 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1412 /***********************************************************************
1413 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1415 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1419 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1422 TRACE("%p\n", info
);
1424 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1427 DWORD type
, size
= 0;
1429 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1430 if (!l
&& type
== REG_BINARY
&&
1431 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1433 BYTE
*buf
= heap_alloc( size
);
1437 struct connection_settings_header
*hdr
=
1438 (struct connection_settings_header
*)buf
;
1439 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1441 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1443 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1446 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1449 LPWSTR proxy
= NULL
;
1450 LPWSTR proxy_bypass
= NULL
;
1452 /* Sanity-check length of proxy string */
1453 if ((BYTE
*)len
- buf
+ *len
<= size
)
1456 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1458 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1459 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1463 /* Sanity-check length of proxy bypass string */
1464 if ((BYTE
*)len
- buf
+ *len
<= size
)
1466 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1468 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1473 GlobalFree( proxy
);
1477 info
->lpszProxy
= proxy
;
1478 info
->lpszProxyBypass
= proxy_bypass
;
1481 got_from_reg
= TRUE
;
1483 info
->dwAccessType
=
1484 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1485 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1486 debugstr_w(info
->lpszProxy
),
1487 debugstr_w(info
->lpszProxyBypass
));
1496 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1498 char *colon
, *http_proxy
;
1500 if ((colon
= strchr( envproxy
, ':' )))
1502 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1504 static const char http
[] = "http://";
1506 /* It's a scheme, check that it's http */
1507 if (!strncmp( envproxy
, http
, strlen( http
) ))
1508 http_proxy
= envproxy
+ strlen( http
);
1511 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1516 http_proxy
= envproxy
;
1519 http_proxy
= envproxy
;
1525 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1526 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1528 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1530 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1531 info
->lpszProxy
= http_proxyW
;
1532 info
->lpszProxyBypass
= NULL
;
1533 TRACE("http proxy (from environment) = %s\n",
1534 debugstr_w(info
->lpszProxy
));
1540 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1541 info
->lpszProxy
= NULL
;
1542 info
->lpszProxyBypass
= NULL
;
1547 /***********************************************************************
1548 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1550 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1552 static const WCHAR settingsW
[] =
1553 {'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};
1555 struct connection_settings_header
*hdr
= NULL
;
1556 DWORD type
, offset
, len
, size
= 0;
1559 TRACE("%p\n", config
);
1563 set_last_error( ERROR_INVALID_PARAMETER
);
1566 memset( config
, 0, sizeof(*config
) );
1567 config
->fAutoDetect
= TRUE
;
1569 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1570 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1571 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1576 if (!(hdr
= heap_alloc( size
))) goto done
;
1577 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1578 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1584 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1585 offset
= sizeof(*hdr
);
1586 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1587 len
= *(DWORD
*)((char *)hdr
+ offset
);
1588 offset
+= sizeof(DWORD
);
1589 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1591 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1592 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1595 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1596 len
= *(DWORD
*)((char *)hdr
+ offset
);
1597 offset
+= sizeof(DWORD
);
1598 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1600 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1601 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1604 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1605 len
= *(DWORD
*)((char *)hdr
+ offset
);
1606 offset
+= sizeof(DWORD
);
1607 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1609 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1610 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1615 RegCloseKey( hkey
);
1619 heap_free( config
->lpszAutoConfigUrl
);
1620 config
->lpszAutoConfigUrl
= NULL
;
1621 heap_free( config
->lpszProxy
);
1622 config
->lpszProxy
= NULL
;
1623 heap_free( config
->lpszProxyBypass
);
1624 config
->lpszProxyBypass
= NULL
;
1629 static HRESULT WINAPI
dispex_QueryInterface(
1630 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1634 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
1635 IsEqualGUID( riid
, &IID_IDispatch
) ||
1636 IsEqualGUID( riid
, &IID_IDispatchEx
))
1639 return E_NOINTERFACE
;
1644 static ULONG WINAPI
dispex_AddRef(
1645 IDispatchEx
*iface
)
1650 static ULONG WINAPI
dispex_Release(
1651 IDispatchEx
*iface
)
1656 static HRESULT WINAPI
dispex_GetTypeInfoCount(
1657 IDispatchEx
*iface
, UINT
*info
)
1662 static HRESULT WINAPI
dispex_GetTypeInfo(
1663 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
1668 static HRESULT WINAPI
dispex_GetIDsOfNames(
1669 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
1674 static HRESULT WINAPI
dispex_Invoke(
1675 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
1676 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
1681 static HRESULT WINAPI
dispex_DeleteMemberByName(
1682 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
1687 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
1688 IDispatchEx
*iface
, DISPID id
)
1693 static HRESULT WINAPI
dispex_GetMemberProperties(
1694 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
1699 static HRESULT WINAPI
dispex_GetMemberName(
1700 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
1705 static HRESULT WINAPI
dispex_GetNextDispID(
1706 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
1711 static HRESULT WINAPI
dispex_GetNameSpaceParent(
1712 IDispatchEx
*iface
, IUnknown
**unk
)
1717 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1719 static HRESULT WINAPI
dispex_GetDispID(
1720 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
1722 if (!strcmpW( name
, dns_resolveW
))
1724 *id
= DISPID_GLOBAL_DNSRESOLVE
;
1727 return DISP_E_UNKNOWNNAME
;
1730 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
1732 #ifdef HAVE_GETADDRINFO
1733 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1735 struct addrinfo
*ai
, *elem
;
1740 hostnameA
= strdupWA( hostname
);
1742 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
1744 if (!hostnameA
) return E_OUTOFMEMORY
;
1745 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
1746 heap_free( hostnameA
);
1747 if (res
) return S_FALSE
;
1750 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
1756 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
1758 V_VT( result
) = VT_BSTR
;
1759 V_BSTR( result
) = SysAllocString( addr
);
1762 FIXME("getaddrinfo not found at build time\n");
1767 static HRESULT WINAPI
dispex_InvokeEx(
1768 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
1769 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
1771 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
1773 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
1774 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
1775 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
1777 return DISP_E_MEMBERNOTFOUND
;
1780 static const IDispatchExVtbl dispex_vtbl
=
1782 dispex_QueryInterface
,
1785 dispex_GetTypeInfoCount
,
1787 dispex_GetIDsOfNames
,
1791 dispex_DeleteMemberByName
,
1792 dispex_DeleteMemberByDispID
,
1793 dispex_GetMemberProperties
,
1794 dispex_GetMemberName
,
1795 dispex_GetNextDispID
,
1796 dispex_GetNameSpaceParent
1799 static IDispatchEx global_dispex
= { &dispex_vtbl
};
1801 static HRESULT WINAPI
site_QueryInterface(
1802 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
1806 if (IsEqualGUID( &IID_IUnknown
, riid
))
1808 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
1811 return E_NOINTERFACE
;
1813 IUnknown_AddRef( (IUnknown
*)*ppv
);
1817 static ULONG WINAPI
site_AddRef(
1818 IActiveScriptSite
*iface
)
1823 static ULONG WINAPI
site_Release(
1824 IActiveScriptSite
*iface
)
1829 static HRESULT WINAPI
site_GetLCID(
1830 IActiveScriptSite
*iface
, LCID
*lcid
)
1835 static HRESULT WINAPI
site_GetItemInfo(
1836 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
1837 IUnknown
**item
, ITypeInfo
**type_info
)
1839 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
1841 *item
= (IUnknown
*)&global_dispex
;
1847 static HRESULT WINAPI
site_GetDocVersionString(
1848 IActiveScriptSite
*iface
, BSTR
*version
)
1853 static HRESULT WINAPI
site_OnScriptTerminate(
1854 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
1859 static HRESULT WINAPI
site_OnStateChange(
1860 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
1865 static HRESULT WINAPI
site_OnScriptError(
1866 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
1871 static HRESULT WINAPI
site_OnEnterScript(
1872 IActiveScriptSite
*iface
)
1877 static HRESULT WINAPI
site_OnLeaveScript(
1878 IActiveScriptSite
*iface
)
1883 static const IActiveScriptSiteVtbl site_vtbl
=
1885 site_QueryInterface
,
1890 site_GetDocVersionString
,
1891 site_OnScriptTerminate
,
1898 static IActiveScriptSite script_site
= { &site_vtbl
};
1900 static BOOL
parse_script_result( VARIANT result
, WINHTTP_PROXY_INFO
*info
)
1902 static const WCHAR proxyW
[] = {'P','R','O','X','Y'};
1907 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1908 info
->lpszProxy
= NULL
;
1909 info
->lpszProxyBypass
= NULL
;
1911 if (V_VT( &result
) != VT_BSTR
) return TRUE
;
1912 TRACE("%s\n", debugstr_w( V_BSTR( &result
) ));
1914 p
= V_BSTR( &result
);
1915 while (*p
== ' ') p
++;
1917 if (len
>= 5 && !memicmpW( p
, proxyW
, sizeof(proxyW
)/sizeof(WCHAR
) ))
1920 while (*p
== ' ') p
++;
1921 if (!*p
|| *p
== ';') return TRUE
;
1922 if (!(info
->lpszProxy
= q
= strdupW( p
))) return FALSE
;
1923 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1926 if (*q
== ' ' || *q
== ';')
1936 static BSTR
include_pac_utils( BSTR script
)
1938 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
1939 HMODULE hmod
= GetModuleHandleA( "winhttp.dll" );
1946 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
1947 size
= SizeofResource( hmod
, rsrc
);
1948 data
= LoadResource( hmod
, rsrc
);
1950 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
1951 if (!(ret
= SysAllocStringLen( NULL
, len
+ SysStringLen( script
) + 1 ))) return NULL
;
1952 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
1954 strcatW( ret
, script
);
1959 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1960 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1961 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1963 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1964 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1965 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1968 static BOOL
run_script( const BSTR script
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1970 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
1971 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1972 IActiveScriptParse
*parser
= NULL
;
1973 IActiveScript
*engine
= NULL
;
1974 IDispatch
*dispatch
= NULL
;
1978 BSTR func
= NULL
, hostname
= NULL
, full_script
= NULL
;
1980 VARIANT args
[2], result
;
1984 memset( &uc
, 0, sizeof(uc
) );
1985 uc
.dwStructSize
= sizeof(uc
);
1986 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return FALSE
;
1987 if (!(hostname
= SysAllocStringLen( NULL
, uc
.dwHostNameLength
+ 1 ))) return FALSE
;
1988 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1989 hostname
[uc
.dwHostNameLength
] = 0;
1991 init
= CoInitialize( NULL
);
1992 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
1993 if (hr
!= S_OK
) goto done
;
1995 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
1996 &IID_IActiveScript
, (void **)&engine
);
1997 if (hr
!= S_OK
) goto done
;
1999 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
2000 if (hr
!= S_OK
) goto done
;
2002 hr
= IActiveScriptParse_InitNew( parser
);
2003 if (hr
!= S_OK
) goto done
;
2005 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
2006 if (hr
!= S_OK
) goto done
;
2008 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
2009 if (hr
!= S_OK
) goto done
;
2011 if (!(full_script
= include_pac_utils( script
))) goto done
;
2013 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
2014 if (hr
!= S_OK
) goto done
;
2016 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
2017 if (hr
!= S_OK
) goto done
;
2019 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
2020 if (hr
!= S_OK
) goto done
;
2022 if (!(func
= SysAllocString( findproxyW
))) goto done
;
2023 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
2024 if (hr
!= S_OK
) goto done
;
2026 V_VT( &args
[0] ) = VT_BSTR
;
2027 V_BSTR( &args
[0] ) = hostname
;
2028 V_VT( &args
[1] ) = VT_BSTR
;
2029 V_BSTR( &args
[1] ) = SysAllocString( url
);
2031 params
.rgvarg
= args
;
2032 params
.rgdispidNamedArgs
= NULL
;
2034 params
.cNamedArgs
= 0;
2035 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
2036 ¶ms
, &result
, NULL
, NULL
);
2037 VariantClear( &args
[1] );
2040 WARN("script failed 0x%08x\n", hr
);
2043 ret
= parse_script_result( result
, info
);
2046 SysFreeString( full_script
);
2047 SysFreeString( hostname
);
2048 SysFreeString( func
);
2049 if (dispatch
) IDispatch_Release( dispatch
);
2050 if (parser
) IActiveScriptParse_Release( parser
);
2051 if (engine
) IActiveScript_Release( engine
);
2052 if (SUCCEEDED( init
)) CoUninitialize();
2053 if (!ret
) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT
);
2057 static BSTR
download_script( const WCHAR
*url
)
2059 static const WCHAR typeW
[] = {'*','/','*',0};
2060 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
2061 HINTERNET ses
, con
= NULL
, req
= NULL
;
2064 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
2065 char *tmp
, *buffer
= NULL
;
2069 memset( &uc
, 0, sizeof(uc
) );
2070 uc
.dwStructSize
= sizeof(uc
);
2071 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
2072 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
2073 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
2074 hostname
[uc
.dwHostNameLength
] = 0;
2076 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
2077 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
2078 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
2079 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
2080 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
2082 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
2083 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
2084 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
2087 if (!(buffer
= heap_alloc( size
))) goto done
;
2092 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
2093 if (!bytes_read
) break;
2094 to_read
-= bytes_read
;
2095 offset
+= bytes_read
;
2100 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
2104 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, NULL
, 0 );
2105 if (!(script
= SysAllocStringLen( NULL
, len
))) goto done
;
2106 MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, script
, len
);
2110 WinHttpCloseHandle( req
);
2111 WinHttpCloseHandle( con
);
2112 WinHttpCloseHandle( ses
);
2113 heap_free( buffer
);
2114 heap_free( hostname
);
2115 if (!script
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
2119 /***********************************************************************
2120 * WinHttpGetProxyForUrl (winhttp.@)
2122 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2123 WINHTTP_PROXY_INFO
*info
)
2125 WCHAR
*detected_pac_url
= NULL
;
2126 const WCHAR
*pac_url
;
2131 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
2133 if (!(session
= (session_t
*)grab_object( hsession
)))
2135 set_last_error( ERROR_INVALID_HANDLE
);
2138 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
2140 release_object( &session
->hdr
);
2141 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2144 if (!url
|| !options
|| !info
||
2145 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2146 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
2147 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
2148 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
2150 release_object( &session
->hdr
);
2151 set_last_error( ERROR_INVALID_PARAMETER
);
2154 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
2155 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
2158 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
2159 else pac_url
= detected_pac_url
;
2161 if (!(script
= download_script( pac_url
))) goto done
;
2162 ret
= run_script( script
, url
, info
);
2163 SysFreeString( script
);
2166 GlobalFree( detected_pac_url
);
2167 release_object( &session
->hdr
);
2171 /***********************************************************************
2172 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2174 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
2181 TRACE("%p\n", info
);
2185 set_last_error( ERROR_INVALID_PARAMETER
);
2188 switch (info
->dwAccessType
)
2190 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
2192 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
2193 if (!info
->lpszProxy
)
2195 set_last_error( ERROR_INVALID_PARAMETER
);
2198 /* Only ASCII characters are allowed */
2199 for (src
= info
->lpszProxy
; *src
; src
++)
2202 set_last_error( ERROR_INVALID_PARAMETER
);
2205 if (info
->lpszProxyBypass
)
2207 for (src
= info
->lpszProxyBypass
; *src
; src
++)
2210 set_last_error( ERROR_INVALID_PARAMETER
);
2216 set_last_error( ERROR_INVALID_PARAMETER
);
2220 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
2221 KEY_WRITE
, NULL
, &key
, NULL
);
2224 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
2227 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2229 size
+= strlenW( info
->lpszProxy
);
2230 if (info
->lpszProxyBypass
)
2231 size
+= strlenW( info
->lpszProxyBypass
);
2233 buf
= heap_alloc( size
);
2236 struct connection_settings_header
*hdr
=
2237 (struct connection_settings_header
*)buf
;
2238 DWORD
*len
= (DWORD
*)(hdr
+ 1);
2240 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
2242 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2246 hdr
->flags
= PROXY_TYPE_PROXY
;
2247 *len
++ = strlenW( info
->lpszProxy
);
2248 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
2252 if (info
->lpszProxyBypass
)
2254 *len
++ = strlenW( info
->lpszProxyBypass
);
2255 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
2264 hdr
->flags
= PROXY_TYPE_DIRECT
;
2268 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
2278 /***********************************************************************
2279 * WinHttpSetStatusCallback (winhttp.@)
2281 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
2282 DWORD flags
, DWORD_PTR reserved
)
2284 object_header_t
*hdr
;
2285 WINHTTP_STATUS_CALLBACK ret
;
2287 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
2289 if (!(hdr
= grab_object( handle
)))
2291 set_last_error( ERROR_INVALID_HANDLE
);
2292 return WINHTTP_INVALID_STATUS_CALLBACK
;
2294 ret
= hdr
->callback
;
2295 hdr
->callback
= callback
;
2296 hdr
->notify_mask
= flags
;
2298 release_object( hdr
);
2302 /***********************************************************************
2303 * WinHttpSetTimeouts (winhttp.@)
2305 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2308 object_header_t
*hdr
;
2312 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2314 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2316 set_last_error( ERROR_INVALID_PARAMETER
);
2320 if (!(hdr
= grab_object( handle
)))
2322 set_last_error( ERROR_INVALID_HANDLE
);
2328 case WINHTTP_HANDLE_TYPE_REQUEST
:
2329 request
= (request_t
*)hdr
;
2330 request
->connect_timeout
= connect
;
2332 if (resolve
< 0) resolve
= 0;
2333 request
->resolve_timeout
= resolve
;
2335 if (send
< 0) send
= 0;
2336 request
->send_timeout
= send
;
2338 if (receive
< 0) receive
= 0;
2339 request
->recv_timeout
= receive
;
2341 if (netconn_connected( &request
->netconn
))
2343 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2344 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2347 release_object( &request
->hdr
);
2350 case WINHTTP_HANDLE_TYPE_SESSION
:
2351 session
= (session_t
*)hdr
;
2352 session
->connect_timeout
= connect
;
2354 if (resolve
< 0) resolve
= 0;
2355 session
->resolve_timeout
= resolve
;
2357 if (send
< 0) send
= 0;
2358 session
->send_timeout
= send
;
2360 if (receive
< 0) receive
= 0;
2361 session
->recv_timeout
= receive
;
2365 release_object( hdr
);
2366 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2372 static const WCHAR wkday
[7][4] =
2373 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2374 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2375 static const WCHAR month
[12][4] =
2376 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2377 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2378 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2380 /***********************************************************************
2381 * WinHttpTimeFromSystemTime (WININET.@)
2383 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2385 static const WCHAR format
[] =
2386 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2387 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2389 TRACE("%p, %p\n", time
, string
);
2391 if (!time
|| !string
) return FALSE
;
2393 sprintfW( string
, format
,
2394 wkday
[time
->wDayOfWeek
],
2396 month
[time
->wMonth
- 1],
2405 /***********************************************************************
2406 * WinHttpTimeToSystemTime (WININET.@)
2408 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2411 const WCHAR
*s
= string
;
2414 TRACE("%s, %p\n", debugstr_w(string
), time
);
2416 if (!string
|| !time
) return FALSE
;
2418 /* Windows does this too */
2419 GetSystemTime( time
);
2421 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2422 * a SYSTEMTIME structure.
2425 while (*s
&& !isalphaW( *s
)) s
++;
2426 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2427 time
->wDayOfWeek
= 7;
2429 for (i
= 0; i
< 7; i
++)
2431 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2432 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2433 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2435 time
->wDayOfWeek
= i
;
2440 if (time
->wDayOfWeek
> 6) return TRUE
;
2441 while (*s
&& !isdigitW( *s
)) s
++;
2442 time
->wDay
= strtolW( s
, &end
, 10 );
2445 while (*s
&& !isalphaW( *s
)) s
++;
2446 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2449 for (i
= 0; i
< 12; i
++)
2451 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2452 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2453 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2455 time
->wMonth
= i
+ 1;
2459 if (time
->wMonth
== 0) return TRUE
;
2461 while (*s
&& !isdigitW( *s
)) s
++;
2462 if (*s
== '\0') return TRUE
;
2463 time
->wYear
= strtolW( s
, &end
, 10 );
2466 while (*s
&& !isdigitW( *s
)) s
++;
2467 if (*s
== '\0') return TRUE
;
2468 time
->wHour
= strtolW( s
, &end
, 10 );
2471 while (*s
&& !isdigitW( *s
)) s
++;
2472 if (*s
== '\0') return TRUE
;
2473 time
->wMinute
= strtolW( s
, &end
, 10 );
2476 while (*s
&& !isdigitW( *s
)) s
++;
2477 if (*s
== '\0') return TRUE
;
2478 time
->wSecond
= strtolW( s
, &end
, 10 );
2480 time
->wMilliseconds
= 0;