2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "winhttp_private.h"
26 #define DEFAULT_RESOLVE_TIMEOUT 0
27 #define DEFAULT_CONNECT_TIMEOUT 20000
28 #define DEFAULT_SEND_TIMEOUT 30000
29 #define DEFAULT_RECEIVE_TIMEOUT 30000
31 void set_last_error( DWORD error
)
34 SetLastError( error
);
37 DWORD
get_last_error( void )
40 return GetLastError();
43 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
45 if (hdr
->callback
&& (hdr
->notify_mask
& status
))
47 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
48 hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
49 TRACE("returning from 0x%08x callback\n", status
);
53 /***********************************************************************
54 * WinHttpCheckPlatform (winhttp.@)
56 BOOL WINAPI
WinHttpCheckPlatform( void )
62 /***********************************************************************
63 * session_destroy (internal)
65 static void session_destroy( object_header_t
*hdr
)
67 session_t
*session
= (session_t
*)hdr
;
68 struct list
*item
, *next
;
71 TRACE("%p\n", session
);
73 if (session
->unload_event
) SetEvent( session
->unload_event
);
75 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
77 domain
= LIST_ENTRY( item
, domain_t
, entry
);
78 delete_domain( domain
);
80 heap_free( session
->agent
);
81 heap_free( session
->proxy_server
);
82 heap_free( session
->proxy_bypass
);
83 heap_free( session
->proxy_username
);
84 heap_free( session
->proxy_password
);
88 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
90 session_t
*session
= (session_t
*)hdr
;
94 case WINHTTP_OPTION_REDIRECT_POLICY
:
96 if (!buffer
|| *buflen
< sizeof(DWORD
))
98 *buflen
= sizeof(DWORD
);
99 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
103 *(DWORD
*)buffer
= hdr
->redirect_policy
;
104 *buflen
= sizeof(DWORD
);
107 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
108 *(DWORD
*)buffer
= session
->resolve_timeout
;
109 *buflen
= sizeof(DWORD
);
111 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
112 *(DWORD
*)buffer
= session
->connect_timeout
;
113 *buflen
= sizeof(DWORD
);
115 case WINHTTP_OPTION_SEND_TIMEOUT
:
116 *(DWORD
*)buffer
= session
->send_timeout
;
117 *buflen
= sizeof(DWORD
);
119 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
120 *(DWORD
*)buffer
= session
->recv_timeout
;
121 *buflen
= sizeof(DWORD
);
124 FIXME("unimplemented option %u\n", option
);
125 set_last_error( ERROR_INVALID_PARAMETER
);
130 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
132 session_t
*session
= (session_t
*)hdr
;
136 case WINHTTP_OPTION_PROXY
:
138 WINHTTP_PROXY_INFO
*pi
= buffer
;
140 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
143 case WINHTTP_OPTION_REDIRECT_POLICY
:
147 if (buflen
!= sizeof(policy
))
149 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
153 policy
= *(DWORD
*)buffer
;
154 TRACE("0x%x\n", policy
);
155 hdr
->redirect_policy
= policy
;
158 case WINHTTP_OPTION_DISABLE_FEATURE
:
159 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
161 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
162 session
->resolve_timeout
= *(DWORD
*)buffer
;
164 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
165 session
->connect_timeout
= *(DWORD
*)buffer
;
167 case WINHTTP_OPTION_SEND_TIMEOUT
:
168 session
->send_timeout
= *(DWORD
*)buffer
;
170 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
171 session
->recv_timeout
= *(DWORD
*)buffer
;
173 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
174 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
176 case WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT
:
177 TRACE("WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT: %p\n", *(HANDLE
*)buffer
);
178 session
->unload_event
= *(HANDLE
*)buffer
;
181 FIXME("unimplemented option %u\n", option
);
182 set_last_error( ERROR_INVALID_PARAMETER
);
187 static const object_vtbl_t session_vtbl
=
190 session_query_option
,
195 BOOL
netconn_init_winsock();
196 #endif /* __REACTOS__ */
197 /***********************************************************************
198 * WinHttpOpen (winhttp.@)
200 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
203 HINTERNET handle
= NULL
;
205 if (!netconn_init_winsock()) return NULL
;
208 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
210 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
212 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
213 session
->hdr
.vtbl
= &session_vtbl
;
214 session
->hdr
.flags
= flags
;
215 session
->hdr
.refs
= 1;
216 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
217 list_init( &session
->hdr
.children
);
218 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
219 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
220 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
221 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
222 list_init( &session
->cookie_cache
);
224 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
225 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
227 WINHTTP_PROXY_INFO info
;
229 WinHttpGetDefaultProxyConfiguration( &info
);
230 session
->access
= info
.dwAccessType
;
231 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
233 GlobalFree( (LPWSTR
)info
.lpszProxy
);
234 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
237 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
239 GlobalFree( (LPWSTR
)info
.lpszProxy
);
240 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
244 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
246 session
->access
= access
;
247 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
248 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
251 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
252 session
->hdr
.handle
= handle
;
255 release_object( &session
->hdr
);
256 TRACE("returning %p\n", handle
);
257 if (handle
) set_last_error( ERROR_SUCCESS
);
261 /***********************************************************************
262 * connect_destroy (internal)
264 static void connect_destroy( object_header_t
*hdr
)
266 connect_t
*connect
= (connect_t
*)hdr
;
268 TRACE("%p\n", connect
);
270 release_object( &connect
->session
->hdr
);
272 heap_free( connect
->hostname
);
273 heap_free( connect
->servername
);
274 heap_free( connect
->username
);
275 heap_free( connect
->password
);
276 heap_free( connect
);
279 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
281 connect_t
*connect
= (connect_t
*)hdr
;
285 case WINHTTP_OPTION_PARENT_HANDLE
:
287 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
289 *buflen
= sizeof(HINTERNET
);
290 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
294 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
295 *buflen
= sizeof(HINTERNET
);
298 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
299 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
300 *buflen
= sizeof(DWORD
);
302 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
303 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
304 *buflen
= sizeof(DWORD
);
306 case WINHTTP_OPTION_SEND_TIMEOUT
:
307 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
308 *buflen
= sizeof(DWORD
);
310 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
311 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
312 *buflen
= sizeof(DWORD
);
315 FIXME("unimplemented option %u\n", option
);
316 set_last_error( ERROR_INVALID_PARAMETER
);
321 static const object_vtbl_t connect_vtbl
=
324 connect_query_option
,
328 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
330 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
333 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
335 else if (*domain
== '*')
337 if (domain
[1] == '.')
341 /* For a hostname to match a wildcard, the last domain must match
342 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
343 * hostname is www.foo.a.b, it matches, but a.b does not.
345 dot
= strchrW( server
, '.' );
348 int len
= strlenW( dot
+ 1 );
350 if (len
> strlenW( domain
+ 2 ))
354 /* The server's domain is longer than the wildcard, so it
355 * could be a subdomain. Compare the last portion of the
358 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
359 if (!strcmpiW( ptr
, domain
+ 2 ))
361 /* This is only a match if the preceding character is
362 * a '.', i.e. that it is a matching domain. E.g.
363 * if domain is '*.b.c' and server is 'www.ab.c' they
366 ret
= *(ptr
- 1) == '.';
370 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
375 ret
= !strcmpiW( server
, domain
);
379 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
380 #define MAX_HOST_NAME_LENGTH 256
382 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
387 if (!session
->proxy_bypass
) return FALSE
;
388 ptr
= session
->proxy_bypass
;
392 ptr
= strchrW( ptr
, ';' );
394 ptr
= strchrW( tmp
, ' ' );
397 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
399 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
401 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
402 domain
[ptr
- tmp
] = 0;
403 ret
= domain_matches( server
, domain
);
408 ret
= domain_matches( server
, tmp
);
409 } while (ptr
&& !ret
);
413 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
415 session_t
*session
= connect
->session
;
418 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
422 if ((colon
= strchrW( session
->proxy_server
, ':' )))
424 if (!connect
->servername
|| strncmpiW( connect
->servername
,
425 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
427 heap_free( connect
->servername
);
428 connect
->resolved
= FALSE
;
429 if (!(connect
->servername
= heap_alloc(
430 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
435 memcpy( connect
->servername
, session
->proxy_server
,
436 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
437 connect
->servername
[colon
- session
->proxy_server
] = 0;
439 connect
->serverport
= atoiW( colon
+ 1 );
441 connect
->serverport
= INTERNET_DEFAULT_PORT
;
446 if (!connect
->servername
|| strcmpiW( connect
->servername
,
447 session
->proxy_server
))
449 heap_free( connect
->servername
);
450 connect
->resolved
= FALSE
;
451 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
456 connect
->serverport
= INTERNET_DEFAULT_PORT
;
462 heap_free( connect
->servername
);
463 connect
->resolved
= FALSE
;
464 if (!(connect
->servername
= strdupW( server
)))
469 connect
->serverport
= port
;
475 /***********************************************************************
476 * WinHttpConnect (winhttp.@)
478 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
482 HINTERNET hconnect
= NULL
;
484 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
488 set_last_error( ERROR_INVALID_PARAMETER
);
491 if (!(session
= (session_t
*)grab_object( hsession
)))
493 set_last_error( ERROR_INVALID_HANDLE
);
496 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
498 release_object( &session
->hdr
);
499 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
502 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
504 release_object( &session
->hdr
);
507 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
508 connect
->hdr
.vtbl
= &connect_vtbl
;
509 connect
->hdr
.refs
= 1;
510 connect
->hdr
.flags
= session
->hdr
.flags
;
511 connect
->hdr
.callback
= session
->hdr
.callback
;
512 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
513 connect
->hdr
.context
= session
->hdr
.context
;
514 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
515 list_init( &connect
->hdr
.children
);
517 addref_object( &session
->hdr
);
518 connect
->session
= session
;
519 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
521 if (!(connect
->hostname
= strdupW( server
))) goto end
;
522 connect
->hostport
= port
;
523 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
525 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
526 connect
->hdr
.handle
= hconnect
;
528 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
531 release_object( &connect
->hdr
);
532 release_object( &session
->hdr
);
533 TRACE("returning %p\n", hconnect
);
534 if (hconnect
) set_last_error( ERROR_SUCCESS
);
538 /***********************************************************************
539 * request_destroy (internal)
541 static void request_destroy( object_header_t
*hdr
)
543 request_t
*request
= (request_t
*)hdr
;
546 TRACE("%p\n", request
);
548 if (request
->task_thread
)
550 /* Signal to the task proc to quit. It will call
551 this again when it does. */
552 HANDLE thread
= request
->task_thread
;
553 request
->task_thread
= 0;
554 SetEvent( request
->task_cancel
);
555 CloseHandle( thread
);
558 release_object( &request
->connect
->hdr
);
560 destroy_authinfo( request
->authinfo
);
561 destroy_authinfo( request
->proxy_authinfo
);
563 heap_free( request
->verb
);
564 heap_free( request
->path
);
565 heap_free( request
->version
);
566 heap_free( request
->raw_headers
);
567 heap_free( request
->status_text
);
568 for (i
= 0; i
< request
->num_headers
; i
++)
570 heap_free( request
->headers
[i
].field
);
571 heap_free( request
->headers
[i
].value
);
573 heap_free( request
->headers
);
574 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
575 heap_free( request
->accept_types
);
576 for (i
= 0; i
< TARGET_MAX
; i
++)
578 for (j
= 0; j
< SCHEME_MAX
; j
++)
580 heap_free( request
->creds
[i
][j
].username
);
581 heap_free( request
->creds
[i
][j
].password
);
584 heap_free( request
);
587 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
590 if (str
) len
= strlenW( str
);
591 if (buffer
&& *buflen
> len
)
593 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
596 *buflen
= len
* sizeof(WCHAR
);
599 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
602 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
604 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
605 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
606 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
611 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
613 #if !defined(__MINGW32__) && !defined(_MSC_VER)
614 switch (addr
->sa_family
)
618 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
619 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
622 addr_win
->sin_family
= WS_AF_INET
;
623 addr_win
->sin_port
= addr_unix
->sin_port
;
624 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
625 p
= (char *)&addr_win
->sin_addr
+ 4;
626 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
631 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
632 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
634 addr_win
->sin6_family
= WS_AF_INET6
;
635 addr_win
->sin6_port
= addr_unix
->sin6_port
;
636 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
637 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
638 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
639 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
641 addr_win
->sin6_scope_id
= 0;
643 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
647 ERR("unhandled family %u\n", addr
->sa_family
);
651 switch (addr
->sa_family
)
655 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
657 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
658 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
663 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
665 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
666 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
670 ERR("unhandled family %u\n", addr
->sa_family
);
676 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
678 request_t
*request
= (request_t
*)hdr
;
682 case WINHTTP_OPTION_SECURITY_FLAGS
:
687 if (!buffer
|| *buflen
< sizeof(flags
))
689 *buflen
= sizeof(flags
);
690 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
695 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
696 flags
|= request
->netconn
.security_flags
;
697 bits
= netconn_get_cipher_strength( &request
->netconn
);
699 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
701 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
703 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
704 *(DWORD
*)buffer
= flags
;
705 *buflen
= sizeof(flags
);
708 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
710 const CERT_CONTEXT
*cert
;
712 if (!buffer
|| *buflen
< sizeof(cert
))
714 *buflen
= sizeof(cert
);
715 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
719 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
720 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
721 *buflen
= sizeof(cert
);
724 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
726 const CERT_CONTEXT
*cert
;
727 const CRYPT_OID_INFO
*oidInfo
;
728 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
730 FIXME("partial stub\n");
732 if (!buffer
|| *buflen
< sizeof(*ci
))
734 *buflen
= sizeof(*ci
);
735 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
738 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
740 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
741 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
742 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
743 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
744 ci
->lpszProtocolName
= NULL
;
745 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
746 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
749 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
751 ci
->lpszSignatureAlgName
= NULL
;
752 ci
->lpszEncryptionAlgName
= NULL
;
753 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
755 CertFreeCertificateContext( cert
);
756 *buflen
= sizeof(*ci
);
759 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
761 if (!buffer
|| *buflen
< sizeof(DWORD
))
763 *buflen
= sizeof(DWORD
);
764 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
768 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
769 *buflen
= sizeof(DWORD
);
772 case WINHTTP_OPTION_CONNECTION_INFO
:
774 WINHTTP_CONNECTION_INFO
*info
= buffer
;
775 struct sockaddr local
;
776 socklen_t len
= sizeof(local
);
777 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
779 if (!buffer
|| *buflen
< sizeof(*info
))
781 *buflen
= sizeof(*info
);
782 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
785 if (!netconn_connected( &request
->netconn
))
787 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
790 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
791 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
792 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
793 info
->cbSize
= sizeof(*info
);
796 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
797 *(DWORD
*)buffer
= request
->resolve_timeout
;
798 *buflen
= sizeof(DWORD
);
800 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
801 *(DWORD
*)buffer
= request
->connect_timeout
;
802 *buflen
= sizeof(DWORD
);
804 case WINHTTP_OPTION_SEND_TIMEOUT
:
805 *(DWORD
*)buffer
= request
->send_timeout
;
806 *buflen
= sizeof(DWORD
);
808 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
809 *(DWORD
*)buffer
= request
->recv_timeout
;
810 *buflen
= sizeof(DWORD
);
813 case WINHTTP_OPTION_USERNAME
:
814 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
817 case WINHTTP_OPTION_PASSWORD
:
818 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
821 case WINHTTP_OPTION_PROXY_USERNAME
:
822 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
825 case WINHTTP_OPTION_PROXY_PASSWORD
:
826 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
830 FIXME("unimplemented option %u\n", option
);
831 set_last_error( ERROR_INVALID_PARAMETER
);
836 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
839 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
841 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
845 set_last_error( ERROR_OUTOFMEMORY
);
849 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
851 request_t
*request
= (request_t
*)hdr
;
855 case WINHTTP_OPTION_PROXY
:
857 WINHTTP_PROXY_INFO
*pi
= buffer
;
859 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
862 case WINHTTP_OPTION_DISABLE_FEATURE
:
866 if (buflen
!= sizeof(DWORD
))
868 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
872 disable
= *(DWORD
*)buffer
;
873 TRACE("0x%x\n", disable
);
874 hdr
->disable_flags
|= disable
;
877 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
881 if (buflen
!= sizeof(DWORD
))
883 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
887 policy
= *(DWORD
*)buffer
;
888 TRACE("0x%x\n", policy
);
889 hdr
->logon_policy
= policy
;
892 case WINHTTP_OPTION_REDIRECT_POLICY
:
896 if (buflen
!= sizeof(DWORD
))
898 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
902 policy
= *(DWORD
*)buffer
;
903 TRACE("0x%x\n", policy
);
904 hdr
->redirect_policy
= policy
;
907 case WINHTTP_OPTION_SECURITY_FLAGS
:
911 if (buflen
< sizeof(DWORD
))
913 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
916 flags
= *(DWORD
*)buffer
;
917 TRACE("0x%x\n", flags
);
918 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
919 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
920 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
921 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
923 set_last_error( ERROR_INVALID_PARAMETER
);
926 request
->netconn
.security_flags
= flags
;
929 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
930 request
->resolve_timeout
= *(DWORD
*)buffer
;
932 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
933 request
->connect_timeout
= *(DWORD
*)buffer
;
935 case WINHTTP_OPTION_SEND_TIMEOUT
:
936 request
->send_timeout
= *(DWORD
*)buffer
;
938 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
939 request
->recv_timeout
= *(DWORD
*)buffer
;
942 case WINHTTP_OPTION_USERNAME
:
944 connect_t
*connect
= request
->connect
;
946 heap_free( connect
->username
);
947 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
950 case WINHTTP_OPTION_PASSWORD
:
952 connect_t
*connect
= request
->connect
;
954 heap_free( connect
->password
);
955 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
958 case WINHTTP_OPTION_PROXY_USERNAME
:
960 session_t
*session
= request
->connect
->session
;
962 heap_free( session
->proxy_username
);
963 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
966 case WINHTTP_OPTION_PROXY_PASSWORD
:
968 session_t
*session
= request
->connect
->session
;
970 heap_free( session
->proxy_password
);
971 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
975 FIXME("unimplemented option %u\n", option
);
976 set_last_error( ERROR_INVALID_PARAMETER
);
981 static const object_vtbl_t request_vtbl
=
984 request_query_option
,
988 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
990 const WCHAR
**types
= accept_types
;
993 if (!types
) return TRUE
;
996 request
->num_accept_types
++;
999 if (!request
->num_accept_types
) return TRUE
;
1000 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
1002 request
->num_accept_types
= 0;
1005 types
= accept_types
;
1006 for (i
= 0; i
< request
->num_accept_types
; i
++)
1008 if (!(request
->accept_types
[i
] = strdupW( *types
)))
1010 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
1011 heap_free( request
->accept_types
);
1012 request
->accept_types
= NULL
;
1013 request
->num_accept_types
= 0;
1021 /***********************************************************************
1022 * WinHttpOpenRequest (winhttp.@)
1024 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1025 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1029 HINTERNET hrequest
= NULL
;
1031 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1032 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1034 if(types
&& TRACE_ON(winhttp
)) {
1037 TRACE("accept types:\n");
1038 for(iter
= types
; *iter
; iter
++)
1039 TRACE(" %s\n", debugstr_w(*iter
));
1042 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1044 set_last_error( ERROR_INVALID_HANDLE
);
1047 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1049 release_object( &connect
->hdr
);
1050 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1053 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1055 release_object( &connect
->hdr
);
1058 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1059 request
->hdr
.vtbl
= &request_vtbl
;
1060 request
->hdr
.refs
= 1;
1061 request
->hdr
.flags
= flags
;
1062 request
->hdr
.callback
= connect
->hdr
.callback
;
1063 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1064 request
->hdr
.context
= connect
->hdr
.context
;
1065 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1066 list_init( &request
->hdr
.children
);
1067 list_init( &request
->task_queue
);
1069 addref_object( &connect
->hdr
);
1070 request
->connect
= connect
;
1071 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1073 if (!netconn_init( &request
->netconn
)) goto end
;
1074 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1075 request
->connect_timeout
= connect
->session
->connect_timeout
;
1076 request
->send_timeout
= connect
->session
->send_timeout
;
1077 request
->recv_timeout
= connect
->session
->recv_timeout
;
1079 if (!verb
|| !verb
[0]) verb
= getW
;
1080 if (!(request
->verb
= strdupW( verb
))) goto end
;
1087 len
= strlenW( object
) + 1;
1088 if (object
[0] != '/') len
++;
1089 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1091 if (object
[0] != '/') *p
++ = '/';
1092 strcpyW( p
, object
);
1093 request
->path
= path
;
1095 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1097 if (!version
|| !version
[0]) version
= http1_1
;
1098 if (!(request
->version
= strdupW( version
))) goto end
;
1099 if (!(store_accept_types( request
, types
))) goto end
;
1101 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1102 request
->hdr
.handle
= hrequest
;
1104 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1107 release_object( &request
->hdr
);
1108 release_object( &connect
->hdr
);
1109 TRACE("returning %p\n", hrequest
);
1110 if (hrequest
) set_last_error( ERROR_SUCCESS
);
1114 /***********************************************************************
1115 * WinHttpCloseHandle (winhttp.@)
1117 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1119 object_header_t
*hdr
;
1121 TRACE("%p\n", handle
);
1123 if (!(hdr
= grab_object( handle
)))
1125 set_last_error( ERROR_INVALID_HANDLE
);
1128 release_object( hdr
);
1129 free_handle( handle
);
1130 set_last_error( ERROR_SUCCESS
);
1134 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1140 set_last_error( ERROR_INVALID_PARAMETER
);
1146 case WINHTTP_OPTION_CONTEXT_VALUE
:
1148 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1150 *buflen
= sizeof(DWORD_PTR
);
1151 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1155 *(DWORD_PTR
*)buffer
= hdr
->context
;
1156 *buflen
= sizeof(DWORD_PTR
);
1160 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1163 FIXME("unimplemented option %u\n", option
);
1164 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1172 /***********************************************************************
1173 * WinHttpQueryOption (winhttp.@)
1175 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1178 object_header_t
*hdr
;
1180 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1182 if (!(hdr
= grab_object( handle
)))
1184 set_last_error( ERROR_INVALID_HANDLE
);
1188 ret
= query_option( hdr
, option
, buffer
, buflen
);
1190 release_object( hdr
);
1191 if (ret
) set_last_error( ERROR_SUCCESS
);
1195 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1201 set_last_error( ERROR_INVALID_PARAMETER
);
1207 case WINHTTP_OPTION_CONTEXT_VALUE
:
1209 if (buflen
!= sizeof(DWORD_PTR
))
1211 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1215 hdr
->context
= *(DWORD_PTR
*)buffer
;
1219 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1222 FIXME("unimplemented option %u\n", option
);
1223 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1231 /***********************************************************************
1232 * WinHttpSetOption (winhttp.@)
1234 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1237 object_header_t
*hdr
;
1239 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1241 if (!(hdr
= grab_object( handle
)))
1243 set_last_error( ERROR_INVALID_HANDLE
);
1247 ret
= set_option( hdr
, option
, buffer
, buflen
);
1249 release_object( hdr
);
1250 if (ret
) set_last_error( ERROR_SUCCESS
);
1254 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1259 GetComputerNameExA( format
, NULL
, &size
);
1260 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1261 if (!(ret
= heap_alloc( size
))) return NULL
;
1262 if (!GetComputerNameExA( format
, ret
, &size
))
1270 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1272 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1274 if (len_suffix
> len_domain
) return FALSE
;
1275 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1279 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1282 #ifdef HAVE_GETNAMEINFO
1283 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1288 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1290 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1291 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1292 char name
[NI_MAXHOST
];
1296 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1297 if (!ai
) return NULL
;
1299 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1301 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1302 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1303 strcpyW( p
, httpW
);
1304 p
+= strlenW( httpW
);
1305 while (*hostname
) { *p
++ = *hostname
++; }
1306 strcpyW( p
, wpadW
);
1310 static BOOL
get_system_proxy_autoconfig_url( char *buf
, DWORD buflen
)
1312 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1313 CFDictionaryRef settings
= CFNetworkCopySystemProxySettings();
1317 if (!settings
) return FALSE
;
1319 if (!(ref
= CFDictionaryGetValue( settings
, kCFNetworkProxiesProxyAutoConfigURLString
)))
1321 CFRelease( settings
);
1324 if (CFStringGetCString( ref
, buf
, buflen
, kCFStringEncodingASCII
))
1326 TRACE( "returning %s\n", debugstr_a(buf
) );
1329 CFRelease( settings
);
1333 if (!once
++) FIXME( "no support on this platform\n" );
1338 #define INTERNET_MAX_URL_LENGTH 2084
1340 /***********************************************************************
1341 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1343 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1346 char system_url
[INTERNET_MAX_URL_LENGTH
+ 1];
1348 TRACE("0x%08x, %p\n", flags
, url
);
1352 set_last_error( ERROR_INVALID_PARAMETER
);
1355 if (get_system_proxy_autoconfig_url( system_url
, sizeof(system_url
) ))
1359 if (!(urlW
= strdupAW( system_url
))) return FALSE
;
1361 set_last_error( ERROR_SUCCESS
);
1364 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1366 static int fixme_shown
;
1367 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1369 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1371 #ifdef HAVE_GETADDRINFO
1372 char *fqdn
, *domain
, *p
;
1374 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1375 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1381 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1383 struct addrinfo
*ai
;
1387 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1390 heap_free( domain
);
1393 strcpy( name
, "wpad" );
1395 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1398 *url
= build_wpad_url( name
, ai
);
1402 TRACE("returning %s\n", debugstr_w(*url
));
1411 heap_free( domain
);
1414 FIXME("getaddrinfo not found at build time\n");
1419 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1422 else set_last_error( ERROR_SUCCESS
);
1426 static const WCHAR Connections
[] = {
1427 'S','o','f','t','w','a','r','e','\\',
1428 'M','i','c','r','o','s','o','f','t','\\',
1429 'W','i','n','d','o','w','s','\\',
1430 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1431 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1432 'C','o','n','n','e','c','t','i','o','n','s',0 };
1433 static const WCHAR WinHttpSettings
[] = {
1434 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1435 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1436 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1437 static const DWORD PROXY_TYPE_DIRECT
= 1;
1438 static const DWORD PROXY_TYPE_PROXY
= 2;
1439 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1440 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1442 struct connection_settings_header
1445 DWORD unknown
; /* always zero? */
1446 DWORD flags
; /* one or more of PROXY_* */
1449 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1453 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1458 /***********************************************************************
1459 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1461 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1465 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1468 TRACE("%p\n", info
);
1470 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1473 DWORD type
, size
= 0;
1475 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1476 if (!l
&& type
== REG_BINARY
&&
1477 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1479 BYTE
*buf
= heap_alloc( size
);
1483 struct connection_settings_header
*hdr
=
1484 (struct connection_settings_header
*)buf
;
1485 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1487 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1489 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1492 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1495 LPWSTR proxy
= NULL
;
1496 LPWSTR proxy_bypass
= NULL
;
1498 /* Sanity-check length of proxy string */
1499 if ((BYTE
*)len
- buf
+ *len
<= size
)
1502 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1504 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1505 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1509 /* Sanity-check length of proxy bypass string */
1510 if ((BYTE
*)len
- buf
+ *len
<= size
)
1512 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1514 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1519 GlobalFree( proxy
);
1523 info
->lpszProxy
= proxy
;
1524 info
->lpszProxyBypass
= proxy_bypass
;
1527 got_from_reg
= TRUE
;
1529 info
->dwAccessType
=
1530 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1531 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1532 debugstr_w(info
->lpszProxy
),
1533 debugstr_w(info
->lpszProxyBypass
));
1542 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1544 char *colon
, *http_proxy
;
1546 if ((colon
= strchr( envproxy
, ':' )))
1548 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1550 static const char http
[] = "http://";
1552 /* It's a scheme, check that it's http */
1553 if (!strncmp( envproxy
, http
, strlen( http
) ))
1554 http_proxy
= envproxy
+ strlen( http
);
1557 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1562 http_proxy
= envproxy
;
1565 http_proxy
= envproxy
;
1571 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1572 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1574 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1576 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1577 info
->lpszProxy
= http_proxyW
;
1578 info
->lpszProxyBypass
= NULL
;
1579 TRACE("http proxy (from environment) = %s\n",
1580 debugstr_w(info
->lpszProxy
));
1586 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1587 info
->lpszProxy
= NULL
;
1588 info
->lpszProxyBypass
= NULL
;
1590 set_last_error( ERROR_SUCCESS
);
1594 /***********************************************************************
1595 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1597 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1599 static const WCHAR settingsW
[] =
1600 {'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};
1602 struct connection_settings_header
*hdr
= NULL
;
1603 DWORD type
, offset
, len
, size
= 0;
1606 TRACE("%p\n", config
);
1610 set_last_error( ERROR_INVALID_PARAMETER
);
1613 memset( config
, 0, sizeof(*config
) );
1614 config
->fAutoDetect
= TRUE
;
1616 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1617 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1618 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1623 if (!(hdr
= heap_alloc( size
))) goto done
;
1624 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1625 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1631 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1632 offset
= sizeof(*hdr
);
1633 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1634 len
= *(DWORD
*)((char *)hdr
+ offset
);
1635 offset
+= sizeof(DWORD
);
1636 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1638 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1639 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1642 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1643 len
= *(DWORD
*)((char *)hdr
+ offset
);
1644 offset
+= sizeof(DWORD
);
1645 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1647 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1648 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1651 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1652 len
= *(DWORD
*)((char *)hdr
+ offset
);
1653 offset
+= sizeof(DWORD
);
1654 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1656 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1657 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1662 RegCloseKey( hkey
);
1666 GlobalFree( config
->lpszAutoConfigUrl
);
1667 config
->lpszAutoConfigUrl
= NULL
;
1668 GlobalFree( config
->lpszProxy
);
1669 config
->lpszProxy
= NULL
;
1670 GlobalFree( config
->lpszProxyBypass
);
1671 config
->lpszProxyBypass
= NULL
;
1673 else set_last_error( ERROR_SUCCESS
);
1677 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1683 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1684 info
->lpszProxy
= NULL
;
1685 info
->lpszProxyBypass
= NULL
;
1687 TRACE("%s\n", debugstr_a( result
));
1690 while (*p
== ' ') p
++;
1692 if (len
>= 5 && !strncasecmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1695 while (*p
== ' ') p
++;
1696 if (!*p
|| *p
== ';') return TRUE
;
1697 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1698 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1701 if (*q
== ' ' || *q
== ';')
1711 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1713 static const WCHAR typeW
[] = {'*','/','*',0};
1714 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
1715 HINTERNET ses
, con
= NULL
, req
= NULL
;
1718 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1719 char *tmp
, *buffer
= NULL
;
1723 memset( &uc
, 0, sizeof(uc
) );
1724 uc
.dwStructSize
= sizeof(uc
);
1725 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1726 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1727 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1728 hostname
[uc
.dwHostNameLength
] = 0;
1730 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1731 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1732 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1733 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1734 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1736 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
1737 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1738 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1741 if (!(buffer
= heap_alloc( size
))) goto done
;
1746 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
1747 if (!bytes_read
) break;
1748 to_read
-= bytes_read
;
1749 offset
+= bytes_read
;
1750 *out_size
+= bytes_read
;
1755 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
1761 WinHttpCloseHandle( req
);
1762 WinHttpCloseHandle( con
);
1763 WinHttpCloseHandle( ses
);
1764 heap_free( hostname
);
1765 if (!buffer
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1769 struct AUTO_PROXY_SCRIPT_BUFFER
1772 LPSTR lpszScriptBuffer
;
1773 DWORD dwScriptBufferSize
;
1776 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
1777 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
1778 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
1780 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1783 char *result
, *urlA
;
1785 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
1787 buffer
.dwStructSize
= sizeof(buffer
);
1788 buffer
.lpszScriptBuffer
= script
;
1789 buffer
.dwScriptBufferSize
= size
;
1791 if (!(urlA
= strdupWA( url
))) return FALSE
;
1792 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
1797 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
), NULL
, 0, &result
, &len_result
)))
1799 ret
= parse_script_result( result
, info
);
1800 heap_free( result
);
1803 return InternetDeInitializeAutoProxyDll( NULL
, 0 );
1806 /***********************************************************************
1807 * WinHttpGetProxyForUrl (winhttp.@)
1809 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1810 WINHTTP_PROXY_INFO
*info
)
1812 WCHAR
*detected_pac_url
= NULL
;
1813 const WCHAR
*pac_url
;
1819 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1821 if (!(session
= (session_t
*)grab_object( hsession
)))
1823 set_last_error( ERROR_INVALID_HANDLE
);
1826 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
1828 release_object( &session
->hdr
);
1829 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1832 if (!url
|| !options
|| !info
||
1833 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
1834 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
1835 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
1836 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
1838 release_object( &session
->hdr
);
1839 set_last_error( ERROR_INVALID_PARAMETER
);
1842 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
1843 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
1846 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
1847 else pac_url
= detected_pac_url
;
1849 if ((script
= download_script( pac_url
, &size
)))
1851 ret
= run_script( script
, size
, url
, info
);
1852 heap_free( script
);
1856 GlobalFree( detected_pac_url
);
1857 release_object( &session
->hdr
);
1858 if (ret
) set_last_error( ERROR_SUCCESS
);
1862 /***********************************************************************
1863 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1865 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1872 TRACE("%p\n", info
);
1876 set_last_error( ERROR_INVALID_PARAMETER
);
1879 switch (info
->dwAccessType
)
1881 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1883 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1884 if (!info
->lpszProxy
)
1886 set_last_error( ERROR_INVALID_PARAMETER
);
1889 /* Only ASCII characters are allowed */
1890 for (src
= info
->lpszProxy
; *src
; src
++)
1893 set_last_error( ERROR_INVALID_PARAMETER
);
1896 if (info
->lpszProxyBypass
)
1898 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1901 set_last_error( ERROR_INVALID_PARAMETER
);
1907 set_last_error( ERROR_INVALID_PARAMETER
);
1911 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1912 KEY_WRITE
, NULL
, &key
, NULL
);
1915 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
1918 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1920 size
+= strlenW( info
->lpszProxy
);
1921 if (info
->lpszProxyBypass
)
1922 size
+= strlenW( info
->lpszProxyBypass
);
1924 buf
= heap_alloc( size
);
1927 struct connection_settings_header
*hdr
=
1928 (struct connection_settings_header
*)buf
;
1929 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1931 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
1933 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1937 hdr
->flags
= PROXY_TYPE_PROXY
;
1938 *len
++ = strlenW( info
->lpszProxy
);
1939 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1943 if (info
->lpszProxyBypass
)
1945 *len
++ = strlenW( info
->lpszProxyBypass
);
1946 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1955 hdr
->flags
= PROXY_TYPE_DIRECT
;
1959 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1966 if (ret
) set_last_error( ERROR_SUCCESS
);
1970 /***********************************************************************
1971 * WinHttpSetStatusCallback (winhttp.@)
1973 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1974 DWORD flags
, DWORD_PTR reserved
)
1976 object_header_t
*hdr
;
1977 WINHTTP_STATUS_CALLBACK ret
;
1979 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1981 if (!(hdr
= grab_object( handle
)))
1983 set_last_error( ERROR_INVALID_HANDLE
);
1984 return WINHTTP_INVALID_STATUS_CALLBACK
;
1986 ret
= hdr
->callback
;
1987 hdr
->callback
= callback
;
1988 hdr
->notify_mask
= flags
;
1990 release_object( hdr
);
1991 set_last_error( ERROR_SUCCESS
);
1995 /***********************************************************************
1996 * WinHttpSetTimeouts (winhttp.@)
1998 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2001 object_header_t
*hdr
;
2005 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2007 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2009 set_last_error( ERROR_INVALID_PARAMETER
);
2013 if (!(hdr
= grab_object( handle
)))
2015 set_last_error( ERROR_INVALID_HANDLE
);
2021 case WINHTTP_HANDLE_TYPE_REQUEST
:
2022 request
= (request_t
*)hdr
;
2023 request
->connect_timeout
= connect
;
2025 if (resolve
< 0) resolve
= 0;
2026 request
->resolve_timeout
= resolve
;
2028 if (send
< 0) send
= 0;
2029 request
->send_timeout
= send
;
2031 if (receive
< 0) receive
= 0;
2032 request
->recv_timeout
= receive
;
2034 if (netconn_connected( &request
->netconn
))
2036 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2037 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2041 case WINHTTP_HANDLE_TYPE_SESSION
:
2042 session
= (session_t
*)hdr
;
2043 session
->connect_timeout
= connect
;
2045 if (resolve
< 0) resolve
= 0;
2046 session
->resolve_timeout
= resolve
;
2048 if (send
< 0) send
= 0;
2049 session
->send_timeout
= send
;
2051 if (receive
< 0) receive
= 0;
2052 session
->recv_timeout
= receive
;
2056 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2059 release_object( hdr
);
2060 if (ret
) set_last_error( ERROR_SUCCESS
);
2064 static const WCHAR wkday
[7][4] =
2065 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2066 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2067 static const WCHAR month
[12][4] =
2068 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2069 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2070 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2072 /***********************************************************************
2073 * WinHttpTimeFromSystemTime (WININET.@)
2075 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2077 static const WCHAR format
[] =
2078 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2079 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2081 TRACE("%p, %p\n", time
, string
);
2083 if (!time
|| !string
)
2085 set_last_error( ERROR_INVALID_PARAMETER
);
2089 sprintfW( string
, format
,
2090 wkday
[time
->wDayOfWeek
],
2092 month
[time
->wMonth
- 1],
2098 set_last_error( ERROR_SUCCESS
);
2102 /***********************************************************************
2103 * WinHttpTimeToSystemTime (WININET.@)
2105 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2108 const WCHAR
*s
= string
;
2111 TRACE("%s, %p\n", debugstr_w(string
), time
);
2113 if (!string
|| !time
)
2115 set_last_error( ERROR_INVALID_PARAMETER
);
2119 /* Windows does this too */
2120 GetSystemTime( time
);
2122 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2123 * a SYSTEMTIME structure.
2126 set_last_error( ERROR_SUCCESS
);
2128 while (*s
&& !isalphaW( *s
)) s
++;
2129 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2130 time
->wDayOfWeek
= 7;
2132 for (i
= 0; i
< 7; i
++)
2134 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2135 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2136 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2138 time
->wDayOfWeek
= i
;
2143 if (time
->wDayOfWeek
> 6) return TRUE
;
2144 while (*s
&& !isdigitW( *s
)) s
++;
2145 time
->wDay
= strtolW( s
, &end
, 10 );
2148 while (*s
&& !isalphaW( *s
)) s
++;
2149 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2152 for (i
= 0; i
< 12; i
++)
2154 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2155 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2156 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2158 time
->wMonth
= i
+ 1;
2162 if (time
->wMonth
== 0) return TRUE
;
2164 while (*s
&& !isdigitW( *s
)) s
++;
2165 if (*s
== '\0') return TRUE
;
2166 time
->wYear
= strtolW( s
, &end
, 10 );
2169 while (*s
&& !isdigitW( *s
)) s
++;
2170 if (*s
== '\0') return TRUE
;
2171 time
->wHour
= strtolW( s
, &end
, 10 );
2174 while (*s
&& !isdigitW( *s
)) s
++;
2175 if (*s
== '\0') return TRUE
;
2176 time
->wMinute
= strtolW( s
, &end
, 10 );
2179 while (*s
&& !isdigitW( *s
)) s
++;
2180 if (*s
== '\0') return TRUE
;
2181 time
->wSecond
= strtolW( s
, &end
, 10 );
2183 time
->wMilliseconds
= 0;