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"
26 #ifdef HAVE_CORESERVICES_CORESERVICES_H
27 #define GetCurrentThread MacGetCurrentThread
28 #define LoadResource MacLoadResource
29 #include <CoreServices/CoreServices.h>
30 #undef GetCurrentThread
50 #include "winhttp_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
54 #define DEFAULT_RESOLVE_TIMEOUT 0
55 #define DEFAULT_CONNECT_TIMEOUT 20000
56 #define DEFAULT_SEND_TIMEOUT 30000
57 #define DEFAULT_RECEIVE_TIMEOUT 30000
59 void set_last_error( DWORD error
)
62 SetLastError( error
);
65 DWORD
get_last_error( void )
68 return GetLastError();
71 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
73 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
75 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
78 /***********************************************************************
79 * WinHttpCheckPlatform (winhttp.@)
81 BOOL WINAPI
WinHttpCheckPlatform( void )
87 /***********************************************************************
88 * session_destroy (internal)
90 static void session_destroy( object_header_t
*hdr
)
92 session_t
*session
= (session_t
*)hdr
;
93 struct list
*item
, *next
;
96 TRACE("%p\n", session
);
98 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
100 domain
= LIST_ENTRY( item
, domain_t
, entry
);
101 delete_domain( domain
);
103 heap_free( session
->agent
);
104 heap_free( session
->proxy_server
);
105 heap_free( session
->proxy_bypass
);
106 heap_free( session
->proxy_username
);
107 heap_free( session
->proxy_password
);
108 heap_free( session
);
111 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
113 session_t
*session
= (session_t
*)hdr
;
117 case WINHTTP_OPTION_REDIRECT_POLICY
:
119 if (!buffer
|| *buflen
< sizeof(DWORD
))
121 *buflen
= sizeof(DWORD
);
122 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
126 *(DWORD
*)buffer
= hdr
->redirect_policy
;
127 *buflen
= sizeof(DWORD
);
130 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
131 *(DWORD
*)buffer
= session
->resolve_timeout
;
132 *buflen
= sizeof(DWORD
);
134 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
135 *(DWORD
*)buffer
= session
->connect_timeout
;
136 *buflen
= sizeof(DWORD
);
138 case WINHTTP_OPTION_SEND_TIMEOUT
:
139 *(DWORD
*)buffer
= session
->send_timeout
;
140 *buflen
= sizeof(DWORD
);
142 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
143 *(DWORD
*)buffer
= session
->recv_timeout
;
144 *buflen
= sizeof(DWORD
);
147 FIXME("unimplemented option %u\n", option
);
148 set_last_error( ERROR_INVALID_PARAMETER
);
153 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
155 session_t
*session
= (session_t
*)hdr
;
159 case WINHTTP_OPTION_PROXY
:
161 WINHTTP_PROXY_INFO
*pi
= buffer
;
163 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
166 case WINHTTP_OPTION_REDIRECT_POLICY
:
170 if (buflen
!= sizeof(policy
))
172 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
176 policy
= *(DWORD
*)buffer
;
177 TRACE("0x%x\n", policy
);
178 hdr
->redirect_policy
= policy
;
181 case WINHTTP_OPTION_DISABLE_FEATURE
:
182 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
184 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
185 session
->resolve_timeout
= *(DWORD
*)buffer
;
187 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
188 session
->connect_timeout
= *(DWORD
*)buffer
;
190 case WINHTTP_OPTION_SEND_TIMEOUT
:
191 session
->send_timeout
= *(DWORD
*)buffer
;
193 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
194 session
->recv_timeout
= *(DWORD
*)buffer
;
196 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
197 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
200 FIXME("unimplemented option %u\n", option
);
201 set_last_error( ERROR_INVALID_PARAMETER
);
206 static const object_vtbl_t session_vtbl
=
209 session_query_option
,
213 /***********************************************************************
214 * WinHttpOpen (winhttp.@)
216 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
219 HINTERNET handle
= NULL
;
221 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
223 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
225 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
226 session
->hdr
.vtbl
= &session_vtbl
;
227 session
->hdr
.flags
= flags
;
228 session
->hdr
.refs
= 1;
229 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
230 list_init( &session
->hdr
.children
);
231 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
232 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
233 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
234 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
235 list_init( &session
->cookie_cache
);
237 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
238 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
240 WINHTTP_PROXY_INFO info
;
242 WinHttpGetDefaultProxyConfiguration( &info
);
243 session
->access
= info
.dwAccessType
;
244 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
246 GlobalFree( info
.lpszProxy
);
247 GlobalFree( info
.lpszProxyBypass
);
250 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
252 GlobalFree( info
.lpszProxy
);
253 GlobalFree( info
.lpszProxyBypass
);
257 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
259 session
->access
= access
;
260 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
261 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
264 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
265 session
->hdr
.handle
= handle
;
268 release_object( &session
->hdr
);
269 TRACE("returning %p\n", handle
);
273 /***********************************************************************
274 * connect_destroy (internal)
276 static void connect_destroy( object_header_t
*hdr
)
278 connect_t
*connect
= (connect_t
*)hdr
;
280 TRACE("%p\n", connect
);
282 release_object( &connect
->session
->hdr
);
284 heap_free( connect
->hostname
);
285 heap_free( connect
->servername
);
286 heap_free( connect
->username
);
287 heap_free( connect
->password
);
288 heap_free( connect
);
291 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
293 connect_t
*connect
= (connect_t
*)hdr
;
297 case WINHTTP_OPTION_PARENT_HANDLE
:
299 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
301 *buflen
= sizeof(HINTERNET
);
302 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
306 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
307 *buflen
= sizeof(HINTERNET
);
310 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
311 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
312 *buflen
= sizeof(DWORD
);
314 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
315 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
316 *buflen
= sizeof(DWORD
);
318 case WINHTTP_OPTION_SEND_TIMEOUT
:
319 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
320 *buflen
= sizeof(DWORD
);
322 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
323 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
324 *buflen
= sizeof(DWORD
);
327 FIXME("unimplemented option %u\n", option
);
328 set_last_error( ERROR_INVALID_PARAMETER
);
333 static const object_vtbl_t connect_vtbl
=
336 connect_query_option
,
340 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
342 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
345 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
347 else if (*domain
== '*')
349 if (domain
[1] == '.')
353 /* For a hostname to match a wildcard, the last domain must match
354 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
355 * hostname is www.foo.a.b, it matches, but a.b does not.
357 dot
= strchrW( server
, '.' );
360 int len
= strlenW( dot
+ 1 );
362 if (len
> strlenW( domain
+ 2 ))
366 /* The server's domain is longer than the wildcard, so it
367 * could be a subdomain. Compare the last portion of the
370 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
371 if (!strcmpiW( ptr
, domain
+ 2 ))
373 /* This is only a match if the preceding character is
374 * a '.', i.e. that it is a matching domain. E.g.
375 * if domain is '*.b.c' and server is 'www.ab.c' they
378 ret
= *(ptr
- 1) == '.';
382 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
387 ret
= !strcmpiW( server
, domain
);
391 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
392 #define MAX_HOST_NAME_LENGTH 256
394 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
399 if (!session
->proxy_bypass
) return FALSE
;
400 ptr
= session
->proxy_bypass
;
404 ptr
= strchrW( ptr
, ';' );
406 ptr
= strchrW( tmp
, ' ' );
409 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
411 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
413 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
414 domain
[ptr
- tmp
] = 0;
415 ret
= domain_matches( server
, domain
);
420 ret
= domain_matches( server
, tmp
);
421 } while (ptr
&& !ret
);
425 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
427 session_t
*session
= connect
->session
;
430 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
434 if ((colon
= strchrW( session
->proxy_server
, ':' )))
436 if (!connect
->servername
|| strncmpiW( connect
->servername
,
437 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
439 heap_free( connect
->servername
);
440 connect
->resolved
= FALSE
;
441 if (!(connect
->servername
= heap_alloc(
442 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
447 memcpy( connect
->servername
, session
->proxy_server
,
448 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
449 connect
->servername
[colon
- session
->proxy_server
] = 0;
451 connect
->serverport
= atoiW( colon
+ 1 );
453 connect
->serverport
= INTERNET_DEFAULT_PORT
;
458 if (!connect
->servername
|| strcmpiW( connect
->servername
,
459 session
->proxy_server
))
461 heap_free( connect
->servername
);
462 connect
->resolved
= FALSE
;
463 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
468 connect
->serverport
= INTERNET_DEFAULT_PORT
;
474 heap_free( connect
->servername
);
475 connect
->resolved
= FALSE
;
476 if (!(connect
->servername
= strdupW( server
)))
481 connect
->serverport
= port
;
487 /***********************************************************************
488 * WinHttpConnect (winhttp.@)
490 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
494 HINTERNET hconnect
= NULL
;
496 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
500 set_last_error( ERROR_INVALID_PARAMETER
);
503 if (!(session
= (session_t
*)grab_object( hsession
)))
505 set_last_error( ERROR_INVALID_HANDLE
);
508 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
510 release_object( &session
->hdr
);
511 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
514 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
516 release_object( &session
->hdr
);
519 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
520 connect
->hdr
.vtbl
= &connect_vtbl
;
521 connect
->hdr
.refs
= 1;
522 connect
->hdr
.flags
= session
->hdr
.flags
;
523 connect
->hdr
.callback
= session
->hdr
.callback
;
524 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
525 connect
->hdr
.context
= session
->hdr
.context
;
526 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
527 list_init( &connect
->hdr
.children
);
529 addref_object( &session
->hdr
);
530 connect
->session
= session
;
531 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
533 if (!(connect
->hostname
= strdupW( server
))) goto end
;
534 connect
->hostport
= port
;
535 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
537 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
538 connect
->hdr
.handle
= hconnect
;
540 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
543 release_object( &connect
->hdr
);
544 release_object( &session
->hdr
);
545 TRACE("returning %p\n", hconnect
);
549 /***********************************************************************
550 * request_destroy (internal)
552 static void request_destroy( object_header_t
*hdr
)
554 request_t
*request
= (request_t
*)hdr
;
557 TRACE("%p\n", request
);
559 release_object( &request
->connect
->hdr
);
561 destroy_authinfo( request
->authinfo
);
562 destroy_authinfo( request
->proxy_authinfo
);
564 heap_free( request
->verb
);
565 heap_free( request
->path
);
566 heap_free( request
->version
);
567 heap_free( request
->raw_headers
);
568 heap_free( request
->status_text
);
569 for (i
= 0; i
< request
->num_headers
; i
++)
571 heap_free( request
->headers
[i
].field
);
572 heap_free( request
->headers
[i
].value
);
574 heap_free( request
->headers
);
575 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
576 heap_free( request
->accept_types
);
577 heap_free( request
);
580 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
583 if (str
) len
= strlenW( str
);
584 if (buffer
&& *buflen
> len
)
586 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
589 *buflen
= len
* sizeof(WCHAR
);
592 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
595 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
597 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
598 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
599 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
604 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
607 switch (addr
->sa_family
)
611 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
612 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
615 addr_win
->sin_family
= WS_AF_INET
;
616 addr_win
->sin_port
= addr_unix
->sin_port
;
617 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
618 p
= (char *)&addr_win
->sin_addr
+ 4;
619 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
624 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
625 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
627 addr_win
->sin6_family
= WS_AF_INET6
;
628 addr_win
->sin6_port
= addr_unix
->sin6_port
;
629 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
630 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
631 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
632 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
634 addr_win
->sin6_scope_id
= 0;
636 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
640 ERR("unhandled family %u\n", addr
->sa_family
);
644 switch (addr
->sa_family
)
648 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
650 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
651 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
656 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
658 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
659 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
663 ERR("unhandled family %u\n", addr
->sa_family
);
669 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
671 request_t
*request
= (request_t
*)hdr
;
675 case WINHTTP_OPTION_SECURITY_FLAGS
:
680 if (!buffer
|| *buflen
< sizeof(flags
))
682 *buflen
= sizeof(flags
);
683 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
688 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
689 flags
|= request
->netconn
.security_flags
;
690 bits
= netconn_get_cipher_strength( &request
->netconn
);
692 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
694 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
696 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
697 *(DWORD
*)buffer
= flags
;
698 *buflen
= sizeof(flags
);
701 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
703 const CERT_CONTEXT
*cert
;
705 if (!buffer
|| *buflen
< sizeof(cert
))
707 *buflen
= sizeof(cert
);
708 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
712 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
713 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
714 *buflen
= sizeof(cert
);
717 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
719 const CERT_CONTEXT
*cert
;
720 const CRYPT_OID_INFO
*oidInfo
;
721 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
723 FIXME("partial stub\n");
725 if (!buffer
|| *buflen
< sizeof(*ci
))
727 *buflen
= sizeof(*ci
);
728 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
731 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
733 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
734 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
735 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
736 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
737 ci
->lpszProtocolName
= NULL
;
738 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
739 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
742 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
744 ci
->lpszSignatureAlgName
= NULL
;
745 ci
->lpszEncryptionAlgName
= NULL
;
746 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
748 CertFreeCertificateContext( cert
);
749 *buflen
= sizeof(*ci
);
752 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
754 if (!buffer
|| *buflen
< sizeof(DWORD
))
756 *buflen
= sizeof(DWORD
);
757 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
761 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
762 *buflen
= sizeof(DWORD
);
765 case WINHTTP_OPTION_CONNECTION_INFO
:
767 WINHTTP_CONNECTION_INFO
*info
= buffer
;
768 struct sockaddr local
;
769 socklen_t len
= sizeof(local
);
770 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
772 if (!buffer
|| *buflen
< sizeof(*info
))
774 *buflen
= sizeof(*info
);
775 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
778 if (!netconn_connected( &request
->netconn
))
780 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
783 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
784 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
785 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
786 info
->cbSize
= sizeof(*info
);
789 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
790 *(DWORD
*)buffer
= request
->resolve_timeout
;
791 *buflen
= sizeof(DWORD
);
793 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
794 *(DWORD
*)buffer
= request
->connect_timeout
;
795 *buflen
= sizeof(DWORD
);
797 case WINHTTP_OPTION_SEND_TIMEOUT
:
798 *(DWORD
*)buffer
= request
->send_timeout
;
799 *buflen
= sizeof(DWORD
);
801 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
802 *(DWORD
*)buffer
= request
->recv_timeout
;
803 *buflen
= sizeof(DWORD
);
806 case WINHTTP_OPTION_USERNAME
:
807 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
810 case WINHTTP_OPTION_PASSWORD
:
811 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
814 case WINHTTP_OPTION_PROXY_USERNAME
:
815 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
818 case WINHTTP_OPTION_PROXY_PASSWORD
:
819 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
823 FIXME("unimplemented option %u\n", option
);
824 set_last_error( ERROR_INVALID_PARAMETER
);
829 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
832 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
834 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
838 set_last_error( ERROR_OUTOFMEMORY
);
842 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
844 request_t
*request
= (request_t
*)hdr
;
848 case WINHTTP_OPTION_PROXY
:
850 WINHTTP_PROXY_INFO
*pi
= buffer
;
852 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
855 case WINHTTP_OPTION_DISABLE_FEATURE
:
859 if (buflen
!= sizeof(DWORD
))
861 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
865 disable
= *(DWORD
*)buffer
;
866 TRACE("0x%x\n", disable
);
867 hdr
->disable_flags
|= disable
;
870 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
874 if (buflen
!= sizeof(DWORD
))
876 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
880 policy
= *(DWORD
*)buffer
;
881 TRACE("0x%x\n", policy
);
882 hdr
->logon_policy
= policy
;
885 case WINHTTP_OPTION_REDIRECT_POLICY
:
889 if (buflen
!= sizeof(DWORD
))
891 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
895 policy
= *(DWORD
*)buffer
;
896 TRACE("0x%x\n", policy
);
897 hdr
->redirect_policy
= policy
;
900 case WINHTTP_OPTION_SECURITY_FLAGS
:
904 if (buflen
< sizeof(DWORD
))
906 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
909 flags
= *(DWORD
*)buffer
;
910 TRACE("0x%x\n", flags
);
911 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
912 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
913 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
914 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
916 set_last_error( ERROR_INVALID_PARAMETER
);
919 request
->netconn
.security_flags
= flags
;
922 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
923 request
->resolve_timeout
= *(DWORD
*)buffer
;
925 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
926 request
->connect_timeout
= *(DWORD
*)buffer
;
928 case WINHTTP_OPTION_SEND_TIMEOUT
:
929 request
->send_timeout
= *(DWORD
*)buffer
;
931 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
932 request
->recv_timeout
= *(DWORD
*)buffer
;
935 case WINHTTP_OPTION_USERNAME
:
937 connect_t
*connect
= request
->connect
;
939 heap_free( connect
->username
);
940 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
943 case WINHTTP_OPTION_PASSWORD
:
945 connect_t
*connect
= request
->connect
;
947 heap_free( connect
->password
);
948 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
951 case WINHTTP_OPTION_PROXY_USERNAME
:
953 session_t
*session
= request
->connect
->session
;
955 heap_free( session
->proxy_username
);
956 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
959 case WINHTTP_OPTION_PROXY_PASSWORD
:
961 session_t
*session
= request
->connect
->session
;
963 heap_free( session
->proxy_password
);
964 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
968 FIXME("unimplemented option %u\n", option
);
969 set_last_error( ERROR_INVALID_PARAMETER
);
974 static const object_vtbl_t request_vtbl
=
977 request_query_option
,
981 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
983 const WCHAR
**types
= accept_types
;
986 if (!types
) return TRUE
;
989 request
->num_accept_types
++;
992 if (!request
->num_accept_types
) return TRUE
;
993 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
995 request
->num_accept_types
= 0;
998 types
= accept_types
;
999 for (i
= 0; i
< request
->num_accept_types
; i
++)
1001 if (!(request
->accept_types
[i
] = strdupW( *types
)))
1003 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
1004 heap_free( request
->accept_types
);
1005 request
->accept_types
= NULL
;
1006 request
->num_accept_types
= 0;
1014 /***********************************************************************
1015 * WinHttpOpenRequest (winhttp.@)
1017 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1018 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1022 HINTERNET hrequest
= NULL
;
1024 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1025 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1027 if(types
&& TRACE_ON(winhttp
)) {
1030 TRACE("accept types:\n");
1031 for(iter
= types
; *iter
; iter
++)
1032 TRACE(" %s\n", debugstr_w(*iter
));
1035 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1037 set_last_error( ERROR_INVALID_HANDLE
);
1040 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1042 release_object( &connect
->hdr
);
1043 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1046 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1048 release_object( &connect
->hdr
);
1051 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1052 request
->hdr
.vtbl
= &request_vtbl
;
1053 request
->hdr
.refs
= 1;
1054 request
->hdr
.flags
= flags
;
1055 request
->hdr
.callback
= connect
->hdr
.callback
;
1056 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1057 request
->hdr
.context
= connect
->hdr
.context
;
1058 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1059 list_init( &request
->hdr
.children
);
1061 addref_object( &connect
->hdr
);
1062 request
->connect
= connect
;
1063 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1065 if (!netconn_init( &request
->netconn
)) goto end
;
1066 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1067 request
->connect_timeout
= connect
->session
->connect_timeout
;
1068 request
->send_timeout
= connect
->session
->send_timeout
;
1069 request
->recv_timeout
= connect
->session
->recv_timeout
;
1071 if (!verb
|| !verb
[0]) verb
= getW
;
1072 if (!(request
->verb
= strdupW( verb
))) goto end
;
1079 len
= strlenW( object
) + 1;
1080 if (object
[0] != '/') len
++;
1081 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1083 if (object
[0] != '/') *p
++ = '/';
1084 strcpyW( p
, object
);
1085 request
->path
= path
;
1087 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1089 if (!version
|| !version
[0]) version
= http1_1
;
1090 if (!(request
->version
= strdupW( version
))) goto end
;
1091 if (!(store_accept_types( request
, types
))) goto end
;
1093 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1094 request
->hdr
.handle
= hrequest
;
1096 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1099 release_object( &request
->hdr
);
1100 release_object( &connect
->hdr
);
1101 TRACE("returning %p\n", hrequest
);
1105 /***********************************************************************
1106 * WinHttpCloseHandle (winhttp.@)
1108 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1110 object_header_t
*hdr
;
1112 TRACE("%p\n", handle
);
1114 if (!(hdr
= grab_object( handle
)))
1116 set_last_error( ERROR_INVALID_HANDLE
);
1119 release_object( hdr
);
1120 free_handle( handle
);
1124 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1130 set_last_error( ERROR_INVALID_PARAMETER
);
1136 case WINHTTP_OPTION_CONTEXT_VALUE
:
1138 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1140 *buflen
= sizeof(DWORD_PTR
);
1141 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1145 *(DWORD_PTR
*)buffer
= hdr
->context
;
1146 *buflen
= sizeof(DWORD_PTR
);
1150 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1153 FIXME("unimplemented option %u\n", option
);
1154 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1162 /***********************************************************************
1163 * WinHttpQueryOption (winhttp.@)
1165 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1168 object_header_t
*hdr
;
1170 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1172 if (!(hdr
= grab_object( handle
)))
1174 set_last_error( ERROR_INVALID_HANDLE
);
1178 ret
= query_option( hdr
, option
, buffer
, buflen
);
1180 release_object( hdr
);
1184 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1190 set_last_error( ERROR_INVALID_PARAMETER
);
1196 case WINHTTP_OPTION_CONTEXT_VALUE
:
1198 if (buflen
!= sizeof(DWORD_PTR
))
1200 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1204 hdr
->context
= *(DWORD_PTR
*)buffer
;
1208 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1211 FIXME("unimplemented option %u\n", option
);
1212 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1220 /***********************************************************************
1221 * WinHttpSetOption (winhttp.@)
1223 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1226 object_header_t
*hdr
;
1228 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1230 if (!(hdr
= grab_object( handle
)))
1232 set_last_error( ERROR_INVALID_HANDLE
);
1236 ret
= set_option( hdr
, option
, buffer
, buflen
);
1238 release_object( hdr
);
1242 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1247 GetComputerNameExA( format
, NULL
, &size
);
1248 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1249 if (!(ret
= heap_alloc( size
))) return NULL
;
1250 if (!GetComputerNameExA( format
, ret
, &size
))
1258 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1260 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1262 if (len_suffix
> len_domain
) return FALSE
;
1263 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1267 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1270 #ifdef HAVE_GETNAMEINFO
1271 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1276 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1278 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1279 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1280 char name
[NI_MAXHOST
];
1284 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1285 if (!ai
) return NULL
;
1287 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1289 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1290 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1291 strcpyW( p
, httpW
);
1292 p
+= strlenW( httpW
);
1293 while (*hostname
) { *p
++ = *hostname
++; }
1294 strcpyW( p
, wpadW
);
1298 static BOOL
get_system_proxy_autoconfig_url( char *buf
, DWORD buflen
)
1300 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1301 CFDictionaryRef settings
= CFNetworkCopySystemProxySettings();
1305 if (!settings
) return FALSE
;
1307 if (!(ref
= CFDictionaryGetValue( settings
, kCFNetworkProxiesProxyAutoConfigURLString
)))
1309 CFRelease( settings
);
1312 if (CFStringGetCString( ref
, buf
, buflen
, kCFStringEncodingASCII
))
1314 TRACE( "returning %s\n", debugstr_a(buf
) );
1317 CFRelease( settings
);
1320 FIXME( "no support on this platform\n" );
1325 #define INTERNET_MAX_URL_LENGTH 2084
1327 /***********************************************************************
1328 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1330 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1333 char system_url
[INTERNET_MAX_URL_LENGTH
+ 1];
1335 TRACE("0x%08x, %p\n", flags
, url
);
1339 set_last_error( ERROR_INVALID_PARAMETER
);
1342 if (get_system_proxy_autoconfig_url( system_url
, sizeof(system_url
) ))
1346 if (!(urlW
= strdupAW( system_url
))) return FALSE
;
1350 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1352 static int fixme_shown
;
1353 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1355 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1357 #ifdef HAVE_GETADDRINFO
1358 char *fqdn
, *domain
, *p
;
1360 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1361 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1367 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1369 struct addrinfo
*ai
;
1373 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1376 heap_free( domain
);
1379 strcpy( name
, "wpad" );
1381 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1384 *url
= build_wpad_url( name
, ai
);
1388 TRACE("returning %s\n", debugstr_w(*url
));
1397 heap_free( domain
);
1400 FIXME("getaddrinfo not found at build time\n");
1405 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1411 static const WCHAR Connections
[] = {
1412 'S','o','f','t','w','a','r','e','\\',
1413 'M','i','c','r','o','s','o','f','t','\\',
1414 'W','i','n','d','o','w','s','\\',
1415 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1416 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1417 'C','o','n','n','e','c','t','i','o','n','s',0 };
1418 static const WCHAR WinHttpSettings
[] = {
1419 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1420 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1421 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1422 static const DWORD PROXY_TYPE_DIRECT
= 1;
1423 static const DWORD PROXY_TYPE_PROXY
= 2;
1424 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1425 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1427 struct connection_settings_header
1430 DWORD unknown
; /* always zero? */
1431 DWORD flags
; /* one or more of PROXY_* */
1434 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1438 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1443 /***********************************************************************
1444 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1446 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1450 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1453 TRACE("%p\n", info
);
1455 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1458 DWORD type
, size
= 0;
1460 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1461 if (!l
&& type
== REG_BINARY
&&
1462 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1464 BYTE
*buf
= heap_alloc( size
);
1468 struct connection_settings_header
*hdr
=
1469 (struct connection_settings_header
*)buf
;
1470 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1472 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1474 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1477 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1480 LPWSTR proxy
= NULL
;
1481 LPWSTR proxy_bypass
= NULL
;
1483 /* Sanity-check length of proxy string */
1484 if ((BYTE
*)len
- buf
+ *len
<= size
)
1487 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1489 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1490 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1494 /* Sanity-check length of proxy bypass string */
1495 if ((BYTE
*)len
- buf
+ *len
<= size
)
1497 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1499 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1504 GlobalFree( proxy
);
1508 info
->lpszProxy
= proxy
;
1509 info
->lpszProxyBypass
= proxy_bypass
;
1512 got_from_reg
= TRUE
;
1514 info
->dwAccessType
=
1515 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1516 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1517 debugstr_w(info
->lpszProxy
),
1518 debugstr_w(info
->lpszProxyBypass
));
1527 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1529 char *colon
, *http_proxy
;
1531 if ((colon
= strchr( envproxy
, ':' )))
1533 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1535 static const char http
[] = "http://";
1537 /* It's a scheme, check that it's http */
1538 if (!strncmp( envproxy
, http
, strlen( http
) ))
1539 http_proxy
= envproxy
+ strlen( http
);
1542 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1547 http_proxy
= envproxy
;
1550 http_proxy
= envproxy
;
1556 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1557 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1559 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1561 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1562 info
->lpszProxy
= http_proxyW
;
1563 info
->lpszProxyBypass
= NULL
;
1564 TRACE("http proxy (from environment) = %s\n",
1565 debugstr_w(info
->lpszProxy
));
1571 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1572 info
->lpszProxy
= NULL
;
1573 info
->lpszProxyBypass
= NULL
;
1578 /***********************************************************************
1579 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1581 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1583 static const WCHAR settingsW
[] =
1584 {'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};
1586 struct connection_settings_header
*hdr
= NULL
;
1587 DWORD type
, offset
, len
, size
= 0;
1590 TRACE("%p\n", config
);
1594 set_last_error( ERROR_INVALID_PARAMETER
);
1597 memset( config
, 0, sizeof(*config
) );
1598 config
->fAutoDetect
= TRUE
;
1600 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1601 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1602 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1607 if (!(hdr
= heap_alloc( size
))) goto done
;
1608 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1609 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1615 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1616 offset
= sizeof(*hdr
);
1617 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1618 len
= *(DWORD
*)((char *)hdr
+ offset
);
1619 offset
+= sizeof(DWORD
);
1620 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1622 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1623 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1626 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1627 len
= *(DWORD
*)((char *)hdr
+ offset
);
1628 offset
+= sizeof(DWORD
);
1629 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1631 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1632 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1635 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1636 len
= *(DWORD
*)((char *)hdr
+ offset
);
1637 offset
+= sizeof(DWORD
);
1638 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1640 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1641 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1646 RegCloseKey( hkey
);
1650 GlobalFree( config
->lpszAutoConfigUrl
);
1651 config
->lpszAutoConfigUrl
= NULL
;
1652 GlobalFree( config
->lpszProxy
);
1653 config
->lpszProxy
= NULL
;
1654 GlobalFree( config
->lpszProxyBypass
);
1655 config
->lpszProxyBypass
= NULL
;
1660 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1666 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1667 info
->lpszProxy
= NULL
;
1668 info
->lpszProxyBypass
= NULL
;
1670 TRACE("%s\n", debugstr_a( result
));
1673 while (*p
== ' ') p
++;
1675 if (len
>= 5 && !strncasecmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1678 while (*p
== ' ') p
++;
1679 if (!*p
|| *p
== ';') return TRUE
;
1680 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1681 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1684 if (*q
== ' ' || *q
== ';')
1694 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1696 static const WCHAR typeW
[] = {'*','/','*',0};
1697 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
1698 HINTERNET ses
, con
= NULL
, req
= NULL
;
1701 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1702 char *tmp
, *buffer
= NULL
;
1706 memset( &uc
, 0, sizeof(uc
) );
1707 uc
.dwStructSize
= sizeof(uc
);
1708 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1709 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1710 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1711 hostname
[uc
.dwHostNameLength
] = 0;
1713 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1714 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1715 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1716 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1717 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1719 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
1720 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1721 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1724 if (!(buffer
= heap_alloc( size
))) goto done
;
1729 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
1730 if (!bytes_read
) break;
1731 to_read
-= bytes_read
;
1732 offset
+= bytes_read
;
1733 *out_size
+= bytes_read
;
1738 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
1744 WinHttpCloseHandle( req
);
1745 WinHttpCloseHandle( con
);
1746 WinHttpCloseHandle( ses
);
1747 heap_free( hostname
);
1748 if (!buffer
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1752 struct AUTO_PROXY_SCRIPT_BUFFER
1755 LPSTR lpszScriptBuffer
;
1756 DWORD dwScriptBufferSize
;
1759 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
1760 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
1761 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
1763 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1766 char *result
, *urlA
;
1768 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
1770 buffer
.dwStructSize
= sizeof(buffer
);
1771 buffer
.lpszScriptBuffer
= script
;
1772 buffer
.dwScriptBufferSize
= size
;
1774 if (!(urlA
= strdupWA( url
))) return FALSE
;
1775 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
1780 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
), NULL
, 0, &result
, &len_result
)))
1782 ret
= parse_script_result( result
, info
);
1783 heap_free( result
);
1786 return InternetDeInitializeAutoProxyDll( NULL
, 0 );
1789 /***********************************************************************
1790 * WinHttpGetProxyForUrl (winhttp.@)
1792 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1793 WINHTTP_PROXY_INFO
*info
)
1795 WCHAR
*detected_pac_url
= NULL
;
1796 const WCHAR
*pac_url
;
1802 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1804 if (!(session
= (session_t
*)grab_object( hsession
)))
1806 set_last_error( ERROR_INVALID_HANDLE
);
1809 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
1811 release_object( &session
->hdr
);
1812 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1815 if (!url
|| !options
|| !info
||
1816 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
1817 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
1818 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
1819 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
1821 release_object( &session
->hdr
);
1822 set_last_error( ERROR_INVALID_PARAMETER
);
1825 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
1826 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
1829 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
1830 else pac_url
= detected_pac_url
;
1832 if ((script
= download_script( pac_url
, &size
)))
1834 ret
= run_script( script
, size
, url
, info
);
1835 heap_free( script
);
1839 GlobalFree( detected_pac_url
);
1840 release_object( &session
->hdr
);
1844 /***********************************************************************
1845 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1847 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1854 TRACE("%p\n", info
);
1858 set_last_error( ERROR_INVALID_PARAMETER
);
1861 switch (info
->dwAccessType
)
1863 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1865 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1866 if (!info
->lpszProxy
)
1868 set_last_error( ERROR_INVALID_PARAMETER
);
1871 /* Only ASCII characters are allowed */
1872 for (src
= info
->lpszProxy
; *src
; src
++)
1875 set_last_error( ERROR_INVALID_PARAMETER
);
1878 if (info
->lpszProxyBypass
)
1880 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1883 set_last_error( ERROR_INVALID_PARAMETER
);
1889 set_last_error( ERROR_INVALID_PARAMETER
);
1893 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1894 KEY_WRITE
, NULL
, &key
, NULL
);
1897 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
1900 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1902 size
+= strlenW( info
->lpszProxy
);
1903 if (info
->lpszProxyBypass
)
1904 size
+= strlenW( info
->lpszProxyBypass
);
1906 buf
= heap_alloc( size
);
1909 struct connection_settings_header
*hdr
=
1910 (struct connection_settings_header
*)buf
;
1911 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1913 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
1915 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1919 hdr
->flags
= PROXY_TYPE_PROXY
;
1920 *len
++ = strlenW( info
->lpszProxy
);
1921 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1925 if (info
->lpszProxyBypass
)
1927 *len
++ = strlenW( info
->lpszProxyBypass
);
1928 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1937 hdr
->flags
= PROXY_TYPE_DIRECT
;
1941 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1951 /***********************************************************************
1952 * WinHttpSetStatusCallback (winhttp.@)
1954 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1955 DWORD flags
, DWORD_PTR reserved
)
1957 object_header_t
*hdr
;
1958 WINHTTP_STATUS_CALLBACK ret
;
1960 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1962 if (!(hdr
= grab_object( handle
)))
1964 set_last_error( ERROR_INVALID_HANDLE
);
1965 return WINHTTP_INVALID_STATUS_CALLBACK
;
1967 ret
= hdr
->callback
;
1968 hdr
->callback
= callback
;
1969 hdr
->notify_mask
= flags
;
1971 release_object( hdr
);
1975 /***********************************************************************
1976 * WinHttpSetTimeouts (winhttp.@)
1978 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
1981 object_header_t
*hdr
;
1985 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
1987 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
1989 set_last_error( ERROR_INVALID_PARAMETER
);
1993 if (!(hdr
= grab_object( handle
)))
1995 set_last_error( ERROR_INVALID_HANDLE
);
2001 case WINHTTP_HANDLE_TYPE_REQUEST
:
2002 request
= (request_t
*)hdr
;
2003 request
->connect_timeout
= connect
;
2005 if (resolve
< 0) resolve
= 0;
2006 request
->resolve_timeout
= resolve
;
2008 if (send
< 0) send
= 0;
2009 request
->send_timeout
= send
;
2011 if (receive
< 0) receive
= 0;
2012 request
->recv_timeout
= receive
;
2014 if (netconn_connected( &request
->netconn
))
2016 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2017 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2020 release_object( &request
->hdr
);
2023 case WINHTTP_HANDLE_TYPE_SESSION
:
2024 session
= (session_t
*)hdr
;
2025 session
->connect_timeout
= connect
;
2027 if (resolve
< 0) resolve
= 0;
2028 session
->resolve_timeout
= resolve
;
2030 if (send
< 0) send
= 0;
2031 session
->send_timeout
= send
;
2033 if (receive
< 0) receive
= 0;
2034 session
->recv_timeout
= receive
;
2038 release_object( hdr
);
2039 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2045 static const WCHAR wkday
[7][4] =
2046 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2047 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2048 static const WCHAR month
[12][4] =
2049 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2050 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2051 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2053 /***********************************************************************
2054 * WinHttpTimeFromSystemTime (WININET.@)
2056 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2058 static const WCHAR format
[] =
2059 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2060 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2062 TRACE("%p, %p\n", time
, string
);
2064 if (!time
|| !string
) return FALSE
;
2066 sprintfW( string
, format
,
2067 wkday
[time
->wDayOfWeek
],
2069 month
[time
->wMonth
- 1],
2078 /***********************************************************************
2079 * WinHttpTimeToSystemTime (WININET.@)
2081 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2084 const WCHAR
*s
= string
;
2087 TRACE("%s, %p\n", debugstr_w(string
), time
);
2089 if (!string
|| !time
) return FALSE
;
2091 /* Windows does this too */
2092 GetSystemTime( time
);
2094 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2095 * a SYSTEMTIME structure.
2098 while (*s
&& !isalphaW( *s
)) s
++;
2099 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2100 time
->wDayOfWeek
= 7;
2102 for (i
= 0; i
< 7; i
++)
2104 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2105 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2106 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2108 time
->wDayOfWeek
= i
;
2113 if (time
->wDayOfWeek
> 6) return TRUE
;
2114 while (*s
&& !isdigitW( *s
)) s
++;
2115 time
->wDay
= strtolW( s
, &end
, 10 );
2118 while (*s
&& !isalphaW( *s
)) s
++;
2119 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2122 for (i
= 0; i
< 12; i
++)
2124 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2125 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2126 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2128 time
->wMonth
= i
+ 1;
2132 if (time
->wMonth
== 0) return TRUE
;
2134 while (*s
&& !isdigitW( *s
)) s
++;
2135 if (*s
== '\0') return TRUE
;
2136 time
->wYear
= strtolW( s
, &end
, 10 );
2139 while (*s
&& !isdigitW( *s
)) s
++;
2140 if (*s
== '\0') return TRUE
;
2141 time
->wHour
= strtolW( s
, &end
, 10 );
2144 while (*s
&& !isdigitW( *s
)) s
++;
2145 if (*s
== '\0') return TRUE
;
2146 time
->wMinute
= strtolW( s
, &end
, 10 );
2149 while (*s
&& !isdigitW( *s
)) s
++;
2150 if (*s
== '\0') return TRUE
;
2151 time
->wSecond
= strtolW( s
, &end
, 10 );
2153 time
->wMilliseconds
= 0;