2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
41 #include "winhttp_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
45 #define DEFAULT_RESOLVE_TIMEOUT 0
46 #define DEFAULT_CONNECT_TIMEOUT 20000
47 #define DEFAULT_SEND_TIMEOUT 30000
48 #define DEFAULT_RECEIVE_TIMEOUT 30000
50 void set_last_error( DWORD error
)
53 SetLastError( error
);
56 DWORD
get_last_error( void )
59 return GetLastError();
62 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
64 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
66 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
69 /***********************************************************************
70 * WinHttpCheckPlatform (winhttp.@)
72 BOOL WINAPI
WinHttpCheckPlatform( void )
78 /***********************************************************************
79 * session_destroy (internal)
81 static void session_destroy( object_header_t
*hdr
)
83 session_t
*session
= (session_t
*)hdr
;
84 struct list
*item
, *next
;
87 TRACE("%p\n", session
);
89 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
91 domain
= LIST_ENTRY( item
, domain_t
, entry
);
92 delete_domain( domain
);
94 heap_free( session
->agent
);
95 heap_free( session
->proxy_server
);
96 heap_free( session
->proxy_bypass
);
97 heap_free( session
->proxy_username
);
98 heap_free( session
->proxy_password
);
102 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
104 session_t
*session
= (session_t
*)hdr
;
108 case WINHTTP_OPTION_REDIRECT_POLICY
:
110 if (!buffer
|| *buflen
< sizeof(DWORD
))
112 *buflen
= sizeof(DWORD
);
113 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
117 *(DWORD
*)buffer
= hdr
->redirect_policy
;
118 *buflen
= sizeof(DWORD
);
121 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
122 *(DWORD
*)buffer
= session
->resolve_timeout
;
123 *buflen
= sizeof(DWORD
);
125 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
126 *(DWORD
*)buffer
= session
->connect_timeout
;
127 *buflen
= sizeof(DWORD
);
129 case WINHTTP_OPTION_SEND_TIMEOUT
:
130 *(DWORD
*)buffer
= session
->send_timeout
;
131 *buflen
= sizeof(DWORD
);
133 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
134 *(DWORD
*)buffer
= session
->recv_timeout
;
135 *buflen
= sizeof(DWORD
);
138 FIXME("unimplemented option %u\n", option
);
139 set_last_error( ERROR_INVALID_PARAMETER
);
144 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
146 session_t
*session
= (session_t
*)hdr
;
150 case WINHTTP_OPTION_PROXY
:
152 WINHTTP_PROXY_INFO
*pi
= buffer
;
154 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
157 case WINHTTP_OPTION_REDIRECT_POLICY
:
161 if (buflen
!= sizeof(policy
))
163 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
167 policy
= *(DWORD
*)buffer
;
168 TRACE("0x%x\n", policy
);
169 hdr
->redirect_policy
= policy
;
172 case WINHTTP_OPTION_DISABLE_FEATURE
:
173 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
175 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
176 session
->resolve_timeout
= *(DWORD
*)buffer
;
178 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
179 session
->connect_timeout
= *(DWORD
*)buffer
;
181 case WINHTTP_OPTION_SEND_TIMEOUT
:
182 session
->send_timeout
= *(DWORD
*)buffer
;
184 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
185 session
->recv_timeout
= *(DWORD
*)buffer
;
187 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
188 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
191 FIXME("unimplemented option %u\n", option
);
192 set_last_error( ERROR_INVALID_PARAMETER
);
197 static const object_vtbl_t session_vtbl
=
200 session_query_option
,
204 /***********************************************************************
205 * WinHttpOpen (winhttp.@)
207 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
210 HINTERNET handle
= NULL
;
212 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
214 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
216 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
217 session
->hdr
.vtbl
= &session_vtbl
;
218 session
->hdr
.flags
= flags
;
219 session
->hdr
.refs
= 1;
220 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
221 list_init( &session
->hdr
.children
);
222 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
223 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
224 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
225 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
226 list_init( &session
->cookie_cache
);
228 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
229 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
231 WINHTTP_PROXY_INFO info
;
233 WinHttpGetDefaultProxyConfiguration( &info
);
234 session
->access
= info
.dwAccessType
;
235 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
237 GlobalFree( info
.lpszProxy
);
238 GlobalFree( info
.lpszProxyBypass
);
241 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
243 GlobalFree( info
.lpszProxy
);
244 GlobalFree( info
.lpszProxyBypass
);
248 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
250 session
->access
= access
;
251 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
252 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
255 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
256 session
->hdr
.handle
= handle
;
259 release_object( &session
->hdr
);
260 TRACE("returning %p\n", handle
);
264 /***********************************************************************
265 * connect_destroy (internal)
267 static void connect_destroy( object_header_t
*hdr
)
269 connect_t
*connect
= (connect_t
*)hdr
;
271 TRACE("%p\n", connect
);
273 release_object( &connect
->session
->hdr
);
275 heap_free( connect
->hostname
);
276 heap_free( connect
->servername
);
277 heap_free( connect
->username
);
278 heap_free( connect
->password
);
279 heap_free( connect
);
282 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
284 connect_t
*connect
= (connect_t
*)hdr
;
288 case WINHTTP_OPTION_PARENT_HANDLE
:
290 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
292 *buflen
= sizeof(HINTERNET
);
293 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
297 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
298 *buflen
= sizeof(HINTERNET
);
301 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
302 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
303 *buflen
= sizeof(DWORD
);
305 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
306 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
307 *buflen
= sizeof(DWORD
);
309 case WINHTTP_OPTION_SEND_TIMEOUT
:
310 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
311 *buflen
= sizeof(DWORD
);
313 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
314 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
315 *buflen
= sizeof(DWORD
);
318 FIXME("unimplemented option %u\n", option
);
319 set_last_error( ERROR_INVALID_PARAMETER
);
324 static const object_vtbl_t connect_vtbl
=
327 connect_query_option
,
331 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
333 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
336 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
338 else if (*domain
== '*')
340 if (domain
[1] == '.')
344 /* For a hostname to match a wildcard, the last domain must match
345 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
346 * hostname is www.foo.a.b, it matches, but a.b does not.
348 dot
= strchrW( server
, '.' );
351 int len
= strlenW( dot
+ 1 );
353 if (len
> strlenW( domain
+ 2 ))
357 /* The server's domain is longer than the wildcard, so it
358 * could be a subdomain. Compare the last portion of the
361 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
362 if (!strcmpiW( ptr
, domain
+ 2 ))
364 /* This is only a match if the preceding character is
365 * a '.', i.e. that it is a matching domain. E.g.
366 * if domain is '*.b.c' and server is 'www.ab.c' they
369 ret
= *(ptr
- 1) == '.';
373 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
378 ret
= !strcmpiW( server
, domain
);
382 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
383 #define MAX_HOST_NAME_LENGTH 256
385 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
390 if (!session
->proxy_bypass
) return FALSE
;
391 ptr
= session
->proxy_bypass
;
395 ptr
= strchrW( ptr
, ';' );
397 ptr
= strchrW( tmp
, ' ' );
400 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
402 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
404 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
405 domain
[ptr
- tmp
] = 0;
406 ret
= domain_matches( server
, domain
);
411 ret
= domain_matches( server
, tmp
);
412 } while (ptr
&& !ret
);
416 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
418 session_t
*session
= connect
->session
;
421 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
425 if ((colon
= strchrW( session
->proxy_server
, ':' )))
427 if (!connect
->servername
|| strncmpiW( connect
->servername
,
428 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
430 heap_free( connect
->servername
);
431 connect
->resolved
= FALSE
;
432 if (!(connect
->servername
= heap_alloc(
433 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
438 memcpy( connect
->servername
, session
->proxy_server
,
439 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
440 connect
->servername
[colon
- session
->proxy_server
] = 0;
442 connect
->serverport
= atoiW( colon
+ 1 );
444 connect
->serverport
= INTERNET_DEFAULT_PORT
;
449 if (!connect
->servername
|| strcmpiW( connect
->servername
,
450 session
->proxy_server
))
452 heap_free( connect
->servername
);
453 connect
->resolved
= FALSE
;
454 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
459 connect
->serverport
= INTERNET_DEFAULT_PORT
;
465 heap_free( connect
->servername
);
466 connect
->resolved
= FALSE
;
467 if (!(connect
->servername
= strdupW( server
)))
472 connect
->serverport
= port
;
478 /***********************************************************************
479 * WinHttpConnect (winhttp.@)
481 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
485 HINTERNET hconnect
= NULL
;
487 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
491 set_last_error( ERROR_INVALID_PARAMETER
);
494 if (!(session
= (session_t
*)grab_object( hsession
)))
496 set_last_error( ERROR_INVALID_HANDLE
);
499 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
501 release_object( &session
->hdr
);
502 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
505 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
507 release_object( &session
->hdr
);
510 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
511 connect
->hdr
.vtbl
= &connect_vtbl
;
512 connect
->hdr
.refs
= 1;
513 connect
->hdr
.flags
= session
->hdr
.flags
;
514 connect
->hdr
.callback
= session
->hdr
.callback
;
515 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
516 connect
->hdr
.context
= session
->hdr
.context
;
517 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
518 list_init( &connect
->hdr
.children
);
520 addref_object( &session
->hdr
);
521 connect
->session
= session
;
522 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
524 if (!(connect
->hostname
= strdupW( server
))) goto end
;
525 connect
->hostport
= port
;
526 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
528 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
529 connect
->hdr
.handle
= hconnect
;
531 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
534 release_object( &connect
->hdr
);
535 release_object( &session
->hdr
);
536 TRACE("returning %p\n", hconnect
);
540 /***********************************************************************
541 * request_destroy (internal)
543 static void request_destroy( object_header_t
*hdr
)
545 request_t
*request
= (request_t
*)hdr
;
548 TRACE("%p\n", request
);
550 release_object( &request
->connect
->hdr
);
552 destroy_authinfo( request
->authinfo
);
553 destroy_authinfo( request
->proxy_authinfo
);
555 heap_free( request
->verb
);
556 heap_free( request
->path
);
557 heap_free( request
->version
);
558 heap_free( request
->raw_headers
);
559 heap_free( request
->status_text
);
560 for (i
= 0; i
< request
->num_headers
; i
++)
562 heap_free( request
->headers
[i
].field
);
563 heap_free( request
->headers
[i
].value
);
565 heap_free( request
->headers
);
566 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
567 heap_free( request
->accept_types
);
568 heap_free( request
);
571 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
574 if (str
) len
= strlenW( str
);
575 if (buffer
&& *buflen
> len
)
577 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
580 *buflen
= len
* sizeof(WCHAR
);
583 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
586 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
588 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
589 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
590 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
595 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
598 switch (addr
->sa_family
)
602 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
603 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
606 addr_win
->sin_family
= WS_AF_INET
;
607 addr_win
->sin_port
= addr_unix
->sin_port
;
608 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
609 p
= (char *)&addr_win
->sin_addr
+ 4;
610 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
615 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
616 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
618 addr_win
->sin6_family
= WS_AF_INET6
;
619 addr_win
->sin6_port
= addr_unix
->sin6_port
;
620 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
621 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
622 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
623 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
625 addr_win
->sin6_scope_id
= 0;
627 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
631 ERR("unhandled family %u\n", addr
->sa_family
);
635 switch (addr
->sa_family
)
639 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
641 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
642 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
647 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
649 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
650 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
654 ERR("unhandled family %u\n", addr
->sa_family
);
660 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
662 request_t
*request
= (request_t
*)hdr
;
666 case WINHTTP_OPTION_SECURITY_FLAGS
:
671 if (!buffer
|| *buflen
< sizeof(flags
))
673 *buflen
= sizeof(flags
);
674 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
679 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
680 flags
|= request
->netconn
.security_flags
;
681 bits
= netconn_get_cipher_strength( &request
->netconn
);
683 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
685 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
687 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
688 *(DWORD
*)buffer
= flags
;
689 *buflen
= sizeof(flags
);
692 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
694 const CERT_CONTEXT
*cert
;
696 if (!buffer
|| *buflen
< sizeof(cert
))
698 *buflen
= sizeof(cert
);
699 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
703 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
704 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
705 *buflen
= sizeof(cert
);
708 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
710 const CERT_CONTEXT
*cert
;
711 const CRYPT_OID_INFO
*oidInfo
;
712 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
714 FIXME("partial stub\n");
716 if (!buffer
|| *buflen
< sizeof(*ci
))
718 *buflen
= sizeof(*ci
);
719 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
722 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
724 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
725 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
726 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
727 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
728 ci
->lpszProtocolName
= NULL
;
729 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
730 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
733 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
735 ci
->lpszSignatureAlgName
= NULL
;
736 ci
->lpszEncryptionAlgName
= NULL
;
737 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
739 CertFreeCertificateContext( cert
);
740 *buflen
= sizeof(*ci
);
743 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
745 if (!buffer
|| *buflen
< sizeof(DWORD
))
747 *buflen
= sizeof(DWORD
);
748 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
752 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
753 *buflen
= sizeof(DWORD
);
756 case WINHTTP_OPTION_CONNECTION_INFO
:
758 WINHTTP_CONNECTION_INFO
*info
= buffer
;
759 struct sockaddr local
;
760 socklen_t len
= sizeof(local
);
761 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
763 if (!buffer
|| *buflen
< sizeof(*info
))
765 *buflen
= sizeof(*info
);
766 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
769 if (!netconn_connected( &request
->netconn
))
771 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
774 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
775 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
776 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
777 info
->cbSize
= sizeof(*info
);
780 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
781 *(DWORD
*)buffer
= request
->resolve_timeout
;
782 *buflen
= sizeof(DWORD
);
784 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
785 *(DWORD
*)buffer
= request
->connect_timeout
;
786 *buflen
= sizeof(DWORD
);
788 case WINHTTP_OPTION_SEND_TIMEOUT
:
789 *(DWORD
*)buffer
= request
->send_timeout
;
790 *buflen
= sizeof(DWORD
);
792 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
793 *(DWORD
*)buffer
= request
->recv_timeout
;
794 *buflen
= sizeof(DWORD
);
797 case WINHTTP_OPTION_USERNAME
:
798 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
801 case WINHTTP_OPTION_PASSWORD
:
802 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
805 case WINHTTP_OPTION_PROXY_USERNAME
:
806 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
809 case WINHTTP_OPTION_PROXY_PASSWORD
:
810 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
814 FIXME("unimplemented option %u\n", option
);
815 set_last_error( ERROR_INVALID_PARAMETER
);
820 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
823 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
825 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
829 set_last_error( ERROR_OUTOFMEMORY
);
833 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
835 request_t
*request
= (request_t
*)hdr
;
839 case WINHTTP_OPTION_PROXY
:
841 WINHTTP_PROXY_INFO
*pi
= buffer
;
843 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
846 case WINHTTP_OPTION_DISABLE_FEATURE
:
850 if (buflen
!= sizeof(DWORD
))
852 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
856 disable
= *(DWORD
*)buffer
;
857 TRACE("0x%x\n", disable
);
858 hdr
->disable_flags
|= disable
;
861 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
865 if (buflen
!= sizeof(DWORD
))
867 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
871 policy
= *(DWORD
*)buffer
;
872 TRACE("0x%x\n", policy
);
873 hdr
->logon_policy
= policy
;
876 case WINHTTP_OPTION_REDIRECT_POLICY
:
880 if (buflen
!= sizeof(DWORD
))
882 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
886 policy
= *(DWORD
*)buffer
;
887 TRACE("0x%x\n", policy
);
888 hdr
->redirect_policy
= policy
;
891 case WINHTTP_OPTION_SECURITY_FLAGS
:
895 if (buflen
< sizeof(DWORD
))
897 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
900 flags
= *(DWORD
*)buffer
;
901 TRACE("0x%x\n", flags
);
902 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
903 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
904 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
905 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
907 set_last_error( ERROR_INVALID_PARAMETER
);
910 request
->netconn
.security_flags
= flags
;
913 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
914 request
->resolve_timeout
= *(DWORD
*)buffer
;
916 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
917 request
->connect_timeout
= *(DWORD
*)buffer
;
919 case WINHTTP_OPTION_SEND_TIMEOUT
:
920 request
->send_timeout
= *(DWORD
*)buffer
;
922 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
923 request
->recv_timeout
= *(DWORD
*)buffer
;
926 case WINHTTP_OPTION_USERNAME
:
928 connect_t
*connect
= request
->connect
;
930 heap_free( connect
->username
);
931 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
934 case WINHTTP_OPTION_PASSWORD
:
936 connect_t
*connect
= request
->connect
;
938 heap_free( connect
->password
);
939 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
942 case WINHTTP_OPTION_PROXY_USERNAME
:
944 session_t
*session
= request
->connect
->session
;
946 heap_free( session
->proxy_username
);
947 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
950 case WINHTTP_OPTION_PROXY_PASSWORD
:
952 session_t
*session
= request
->connect
->session
;
954 heap_free( session
->proxy_password
);
955 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
959 FIXME("unimplemented option %u\n", option
);
960 set_last_error( ERROR_INVALID_PARAMETER
);
965 static const object_vtbl_t request_vtbl
=
968 request_query_option
,
972 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
974 const WCHAR
**types
= accept_types
;
977 if (!types
) return TRUE
;
980 request
->num_accept_types
++;
983 if (!request
->num_accept_types
) return TRUE
;
984 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
986 request
->num_accept_types
= 0;
989 types
= accept_types
;
990 for (i
= 0; i
< request
->num_accept_types
; i
++)
992 if (!(request
->accept_types
[i
] = strdupW( *types
)))
994 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
995 heap_free( request
->accept_types
);
996 request
->accept_types
= NULL
;
997 request
->num_accept_types
= 0;
1005 /***********************************************************************
1006 * WinHttpOpenRequest (winhttp.@)
1008 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1009 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1013 HINTERNET hrequest
= NULL
;
1015 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1016 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1018 if(types
&& TRACE_ON(winhttp
)) {
1021 TRACE("accept types:\n");
1022 for(iter
= types
; *iter
; iter
++)
1023 TRACE(" %s\n", debugstr_w(*iter
));
1026 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1028 set_last_error( ERROR_INVALID_HANDLE
);
1031 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1033 release_object( &connect
->hdr
);
1034 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1037 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1039 release_object( &connect
->hdr
);
1042 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1043 request
->hdr
.vtbl
= &request_vtbl
;
1044 request
->hdr
.refs
= 1;
1045 request
->hdr
.flags
= flags
;
1046 request
->hdr
.callback
= connect
->hdr
.callback
;
1047 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1048 request
->hdr
.context
= connect
->hdr
.context
;
1049 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1050 list_init( &request
->hdr
.children
);
1052 addref_object( &connect
->hdr
);
1053 request
->connect
= connect
;
1054 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1056 if (!netconn_init( &request
->netconn
)) goto end
;
1057 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1058 request
->connect_timeout
= connect
->session
->connect_timeout
;
1059 request
->send_timeout
= connect
->session
->send_timeout
;
1060 request
->recv_timeout
= connect
->session
->recv_timeout
;
1062 if (!verb
|| !verb
[0]) verb
= getW
;
1063 if (!(request
->verb
= strdupW( verb
))) goto end
;
1070 len
= strlenW( object
) + 1;
1071 if (object
[0] != '/') len
++;
1072 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1074 if (object
[0] != '/') *p
++ = '/';
1075 strcpyW( p
, object
);
1076 request
->path
= path
;
1078 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1080 if (!version
|| !version
[0]) version
= http1_1
;
1081 if (!(request
->version
= strdupW( version
))) goto end
;
1082 if (!(store_accept_types( request
, types
))) goto end
;
1084 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1085 request
->hdr
.handle
= hrequest
;
1087 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1090 release_object( &request
->hdr
);
1091 release_object( &connect
->hdr
);
1092 TRACE("returning %p\n", hrequest
);
1096 /***********************************************************************
1097 * WinHttpCloseHandle (winhttp.@)
1099 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1101 object_header_t
*hdr
;
1103 TRACE("%p\n", handle
);
1105 if (!(hdr
= grab_object( handle
)))
1107 set_last_error( ERROR_INVALID_HANDLE
);
1110 release_object( hdr
);
1111 free_handle( handle
);
1115 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1121 set_last_error( ERROR_INVALID_PARAMETER
);
1127 case WINHTTP_OPTION_CONTEXT_VALUE
:
1129 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1131 *buflen
= sizeof(DWORD_PTR
);
1132 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1136 *(DWORD_PTR
*)buffer
= hdr
->context
;
1137 *buflen
= sizeof(DWORD_PTR
);
1141 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1144 FIXME("unimplemented option %u\n", option
);
1145 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1153 /***********************************************************************
1154 * WinHttpQueryOption (winhttp.@)
1156 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1159 object_header_t
*hdr
;
1161 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1163 if (!(hdr
= grab_object( handle
)))
1165 set_last_error( ERROR_INVALID_HANDLE
);
1169 ret
= query_option( hdr
, option
, buffer
, buflen
);
1171 release_object( hdr
);
1175 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1181 set_last_error( ERROR_INVALID_PARAMETER
);
1187 case WINHTTP_OPTION_CONTEXT_VALUE
:
1189 if (buflen
!= sizeof(DWORD_PTR
))
1191 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1195 hdr
->context
= *(DWORD_PTR
*)buffer
;
1199 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1202 FIXME("unimplemented option %u\n", option
);
1203 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1211 /***********************************************************************
1212 * WinHttpSetOption (winhttp.@)
1214 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1217 object_header_t
*hdr
;
1219 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1221 if (!(hdr
= grab_object( handle
)))
1223 set_last_error( ERROR_INVALID_HANDLE
);
1227 ret
= set_option( hdr
, option
, buffer
, buflen
);
1229 release_object( hdr
);
1233 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1238 GetComputerNameExA( format
, NULL
, &size
);
1239 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1240 if (!(ret
= heap_alloc( size
))) return NULL
;
1241 if (!GetComputerNameExA( format
, ret
, &size
))
1249 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1251 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1253 if (len_suffix
> len_domain
) return FALSE
;
1254 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1258 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1261 #ifdef HAVE_GETNAMEINFO
1262 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1267 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1269 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1270 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1271 char name
[NI_MAXHOST
];
1275 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1276 if (!ai
) return NULL
;
1278 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1280 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1281 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1282 strcpyW( p
, httpW
);
1283 p
+= strlenW( httpW
);
1284 while (*hostname
) { *p
++ = *hostname
++; }
1285 strcpyW( p
, wpadW
);
1289 /***********************************************************************
1290 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1292 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1296 TRACE("0x%08x, %p\n", flags
, url
);
1300 set_last_error( ERROR_INVALID_PARAMETER
);
1303 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1305 static int fixme_shown
;
1306 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1308 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1310 #ifdef HAVE_GETADDRINFO
1311 char *fqdn
, *domain
, *p
;
1313 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1314 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1320 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1322 struct addrinfo
*ai
;
1326 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1329 heap_free( domain
);
1332 strcpy( name
, "wpad" );
1334 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1337 *url
= build_wpad_url( name
, ai
);
1341 TRACE("returning %s\n", debugstr_w(*url
));
1350 heap_free( domain
);
1353 FIXME("getaddrinfo not found at build time\n");
1358 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1364 static const WCHAR Connections
[] = {
1365 'S','o','f','t','w','a','r','e','\\',
1366 'M','i','c','r','o','s','o','f','t','\\',
1367 'W','i','n','d','o','w','s','\\',
1368 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1369 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1370 'C','o','n','n','e','c','t','i','o','n','s',0 };
1371 static const WCHAR WinHttpSettings
[] = {
1372 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1373 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1374 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1375 static const DWORD PROXY_TYPE_DIRECT
= 1;
1376 static const DWORD PROXY_TYPE_PROXY
= 2;
1377 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1378 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1380 struct connection_settings_header
1383 DWORD unknown
; /* always zero? */
1384 DWORD flags
; /* one or more of PROXY_* */
1387 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1391 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1396 /***********************************************************************
1397 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1399 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1403 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1406 TRACE("%p\n", info
);
1408 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1411 DWORD type
, size
= 0;
1413 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1414 if (!l
&& type
== REG_BINARY
&&
1415 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1417 BYTE
*buf
= heap_alloc( size
);
1421 struct connection_settings_header
*hdr
=
1422 (struct connection_settings_header
*)buf
;
1423 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1425 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1427 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1430 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1433 LPWSTR proxy
= NULL
;
1434 LPWSTR proxy_bypass
= NULL
;
1436 /* Sanity-check length of proxy string */
1437 if ((BYTE
*)len
- buf
+ *len
<= size
)
1440 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1442 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1443 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1447 /* Sanity-check length of proxy bypass string */
1448 if ((BYTE
*)len
- buf
+ *len
<= size
)
1450 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1452 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1457 GlobalFree( proxy
);
1461 info
->lpszProxy
= proxy
;
1462 info
->lpszProxyBypass
= proxy_bypass
;
1465 got_from_reg
= TRUE
;
1467 info
->dwAccessType
=
1468 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1469 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1470 debugstr_w(info
->lpszProxy
),
1471 debugstr_w(info
->lpszProxyBypass
));
1480 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1482 char *colon
, *http_proxy
;
1484 if ((colon
= strchr( envproxy
, ':' )))
1486 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1488 static const char http
[] = "http://";
1490 /* It's a scheme, check that it's http */
1491 if (!strncmp( envproxy
, http
, strlen( http
) ))
1492 http_proxy
= envproxy
+ strlen( http
);
1495 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1500 http_proxy
= envproxy
;
1503 http_proxy
= envproxy
;
1509 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1510 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1512 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1514 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1515 info
->lpszProxy
= http_proxyW
;
1516 info
->lpszProxyBypass
= NULL
;
1517 TRACE("http proxy (from environment) = %s\n",
1518 debugstr_w(info
->lpszProxy
));
1524 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1525 info
->lpszProxy
= NULL
;
1526 info
->lpszProxyBypass
= NULL
;
1531 /***********************************************************************
1532 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1534 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1536 static const WCHAR settingsW
[] =
1537 {'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};
1539 struct connection_settings_header
*hdr
= NULL
;
1540 DWORD type
, offset
, len
, size
= 0;
1543 TRACE("%p\n", config
);
1547 set_last_error( ERROR_INVALID_PARAMETER
);
1550 memset( config
, 0, sizeof(*config
) );
1551 config
->fAutoDetect
= TRUE
;
1553 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1554 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1555 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1560 if (!(hdr
= heap_alloc( size
))) goto done
;
1561 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1562 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1568 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1569 offset
= sizeof(*hdr
);
1570 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1571 len
= *(DWORD
*)((char *)hdr
+ offset
);
1572 offset
+= sizeof(DWORD
);
1573 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1575 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1576 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1579 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1580 len
= *(DWORD
*)((char *)hdr
+ offset
);
1581 offset
+= sizeof(DWORD
);
1582 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1584 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1585 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1588 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1589 len
= *(DWORD
*)((char *)hdr
+ offset
);
1590 offset
+= sizeof(DWORD
);
1591 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1593 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1594 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1599 RegCloseKey( hkey
);
1603 GlobalFree( config
->lpszAutoConfigUrl
);
1604 config
->lpszAutoConfigUrl
= NULL
;
1605 GlobalFree( config
->lpszProxy
);
1606 config
->lpszProxy
= NULL
;
1607 GlobalFree( config
->lpszProxyBypass
);
1608 config
->lpszProxyBypass
= NULL
;
1613 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1619 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1620 info
->lpszProxy
= NULL
;
1621 info
->lpszProxyBypass
= NULL
;
1623 TRACE("%s\n", debugstr_a( result
));
1626 while (*p
== ' ') p
++;
1628 if (len
>= 5 && !strncasecmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1631 while (*p
== ' ') p
++;
1632 if (!*p
|| *p
== ';') return TRUE
;
1633 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1634 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1637 if (*q
== ' ' || *q
== ';')
1647 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1649 static const WCHAR typeW
[] = {'*','/','*',0};
1650 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
1651 HINTERNET ses
, con
= NULL
, req
= NULL
;
1654 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1655 char *tmp
, *buffer
= NULL
;
1659 memset( &uc
, 0, sizeof(uc
) );
1660 uc
.dwStructSize
= sizeof(uc
);
1661 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1662 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1663 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1664 hostname
[uc
.dwHostNameLength
] = 0;
1666 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1667 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1668 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1669 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1670 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1672 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
1673 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1674 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1677 if (!(buffer
= heap_alloc( size
))) goto done
;
1682 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
1683 if (!bytes_read
) break;
1684 to_read
-= bytes_read
;
1685 offset
+= bytes_read
;
1686 *out_size
+= bytes_read
;
1691 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
1697 WinHttpCloseHandle( req
);
1698 WinHttpCloseHandle( con
);
1699 WinHttpCloseHandle( ses
);
1700 heap_free( hostname
);
1701 if (!buffer
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1705 struct AUTO_PROXY_SCRIPT_BUFFER
1708 LPSTR lpszScriptBuffer
;
1709 DWORD dwScriptBufferSize
;
1712 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
1713 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
1714 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
1716 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1719 char *result
, *urlA
;
1721 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
1723 buffer
.dwStructSize
= sizeof(buffer
);
1724 buffer
.lpszScriptBuffer
= script
;
1725 buffer
.dwScriptBufferSize
= size
;
1727 if (!(urlA
= strdupWA( url
))) return FALSE
;
1728 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
1733 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
), NULL
, 0, &result
, &len_result
)))
1735 ret
= parse_script_result( result
, info
);
1736 heap_free( result
);
1739 return InternetDeInitializeAutoProxyDll( NULL
, 0 );
1742 /***********************************************************************
1743 * WinHttpGetProxyForUrl (winhttp.@)
1745 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1746 WINHTTP_PROXY_INFO
*info
)
1748 WCHAR
*detected_pac_url
= NULL
;
1749 const WCHAR
*pac_url
;
1755 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1757 if (!(session
= (session_t
*)grab_object( hsession
)))
1759 set_last_error( ERROR_INVALID_HANDLE
);
1762 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
1764 release_object( &session
->hdr
);
1765 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1768 if (!url
|| !options
|| !info
||
1769 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
1770 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
1771 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
1772 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
1774 release_object( &session
->hdr
);
1775 set_last_error( ERROR_INVALID_PARAMETER
);
1778 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
1779 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
1782 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
1783 else pac_url
= detected_pac_url
;
1785 if ((script
= download_script( pac_url
, &size
)))
1787 ret
= run_script( script
, size
, url
, info
);
1788 heap_free( script
);
1792 GlobalFree( detected_pac_url
);
1793 release_object( &session
->hdr
);
1797 /***********************************************************************
1798 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1800 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1807 TRACE("%p\n", info
);
1811 set_last_error( ERROR_INVALID_PARAMETER
);
1814 switch (info
->dwAccessType
)
1816 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1818 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1819 if (!info
->lpszProxy
)
1821 set_last_error( ERROR_INVALID_PARAMETER
);
1824 /* Only ASCII characters are allowed */
1825 for (src
= info
->lpszProxy
; *src
; src
++)
1828 set_last_error( ERROR_INVALID_PARAMETER
);
1831 if (info
->lpszProxyBypass
)
1833 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1836 set_last_error( ERROR_INVALID_PARAMETER
);
1842 set_last_error( ERROR_INVALID_PARAMETER
);
1846 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1847 KEY_WRITE
, NULL
, &key
, NULL
);
1850 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
1853 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1855 size
+= strlenW( info
->lpszProxy
);
1856 if (info
->lpszProxyBypass
)
1857 size
+= strlenW( info
->lpszProxyBypass
);
1859 buf
= heap_alloc( size
);
1862 struct connection_settings_header
*hdr
=
1863 (struct connection_settings_header
*)buf
;
1864 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1866 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
1868 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1872 hdr
->flags
= PROXY_TYPE_PROXY
;
1873 *len
++ = strlenW( info
->lpszProxy
);
1874 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1878 if (info
->lpszProxyBypass
)
1880 *len
++ = strlenW( info
->lpszProxyBypass
);
1881 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1890 hdr
->flags
= PROXY_TYPE_DIRECT
;
1894 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1904 /***********************************************************************
1905 * WinHttpSetStatusCallback (winhttp.@)
1907 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1908 DWORD flags
, DWORD_PTR reserved
)
1910 object_header_t
*hdr
;
1911 WINHTTP_STATUS_CALLBACK ret
;
1913 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1915 if (!(hdr
= grab_object( handle
)))
1917 set_last_error( ERROR_INVALID_HANDLE
);
1918 return WINHTTP_INVALID_STATUS_CALLBACK
;
1920 ret
= hdr
->callback
;
1921 hdr
->callback
= callback
;
1922 hdr
->notify_mask
= flags
;
1924 release_object( hdr
);
1928 /***********************************************************************
1929 * WinHttpSetTimeouts (winhttp.@)
1931 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
1934 object_header_t
*hdr
;
1938 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
1940 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
1942 set_last_error( ERROR_INVALID_PARAMETER
);
1946 if (!(hdr
= grab_object( handle
)))
1948 set_last_error( ERROR_INVALID_HANDLE
);
1954 case WINHTTP_HANDLE_TYPE_REQUEST
:
1955 request
= (request_t
*)hdr
;
1956 request
->connect_timeout
= connect
;
1958 if (resolve
< 0) resolve
= 0;
1959 request
->resolve_timeout
= resolve
;
1961 if (send
< 0) send
= 0;
1962 request
->send_timeout
= send
;
1964 if (receive
< 0) receive
= 0;
1965 request
->recv_timeout
= receive
;
1967 if (netconn_connected( &request
->netconn
))
1969 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
1970 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
1973 release_object( &request
->hdr
);
1976 case WINHTTP_HANDLE_TYPE_SESSION
:
1977 session
= (session_t
*)hdr
;
1978 session
->connect_timeout
= connect
;
1980 if (resolve
< 0) resolve
= 0;
1981 session
->resolve_timeout
= resolve
;
1983 if (send
< 0) send
= 0;
1984 session
->send_timeout
= send
;
1986 if (receive
< 0) receive
= 0;
1987 session
->recv_timeout
= receive
;
1991 release_object( hdr
);
1992 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1998 static const WCHAR wkday
[7][4] =
1999 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2000 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2001 static const WCHAR month
[12][4] =
2002 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2003 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2004 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2006 /***********************************************************************
2007 * WinHttpTimeFromSystemTime (WININET.@)
2009 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2011 static const WCHAR format
[] =
2012 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2013 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2015 TRACE("%p, %p\n", time
, string
);
2017 if (!time
|| !string
) return FALSE
;
2019 sprintfW( string
, format
,
2020 wkday
[time
->wDayOfWeek
],
2022 month
[time
->wMonth
- 1],
2031 /***********************************************************************
2032 * WinHttpTimeToSystemTime (WININET.@)
2034 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2037 const WCHAR
*s
= string
;
2040 TRACE("%s, %p\n", debugstr_w(string
), time
);
2042 if (!string
|| !time
) return FALSE
;
2044 /* Windows does this too */
2045 GetSystemTime( time
);
2047 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2048 * a SYSTEMTIME structure.
2051 while (*s
&& !isalphaW( *s
)) s
++;
2052 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2053 time
->wDayOfWeek
= 7;
2055 for (i
= 0; i
< 7; i
++)
2057 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2058 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2059 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2061 time
->wDayOfWeek
= i
;
2066 if (time
->wDayOfWeek
> 6) return TRUE
;
2067 while (*s
&& !isdigitW( *s
)) s
++;
2068 time
->wDay
= strtolW( s
, &end
, 10 );
2071 while (*s
&& !isalphaW( *s
)) s
++;
2072 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2075 for (i
= 0; i
< 12; i
++)
2077 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2078 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2079 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2081 time
->wMonth
= i
+ 1;
2085 if (time
->wMonth
== 0) return TRUE
;
2087 while (*s
&& !isdigitW( *s
)) s
++;
2088 if (*s
== '\0') return TRUE
;
2089 time
->wYear
= strtolW( s
, &end
, 10 );
2092 while (*s
&& !isdigitW( *s
)) s
++;
2093 if (*s
== '\0') return TRUE
;
2094 time
->wHour
= strtolW( s
, &end
, 10 );
2097 while (*s
&& !isdigitW( *s
)) s
++;
2098 if (*s
== '\0') return TRUE
;
2099 time
->wMinute
= strtolW( s
, &end
, 10 );
2102 while (*s
&& !isdigitW( *s
)) s
++;
2103 if (*s
== '\0') return TRUE
;
2104 time
->wSecond
= strtolW( s
, &end
, 10 );
2106 time
->wMilliseconds
= 0;