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
36 #include "wine/debug.h"
37 #include "winhttp_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
41 #define DEFAULT_RESOLVE_TIMEOUT 0
42 #define DEFAULT_CONNECT_TIMEOUT 20000
43 #define DEFAULT_SEND_TIMEOUT 30000
44 #define DEFAULT_RECEIVE_TIMEOUT 30000
45 #define DEFAULT_RECEIVE_RESPONSE_TIMEOUT ~0u
47 void send_callback( struct object_header
*hdr
, DWORD status
, void *info
, DWORD buflen
)
49 if (hdr
->callback
&& (hdr
->notify_mask
& status
))
51 TRACE( "%p, %#lx, %p, %lu, %lu\n", hdr
, status
, info
, buflen
, hdr
->recursion_count
);
52 InterlockedIncrement( &hdr
->recursion_count
);
53 hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
54 InterlockedDecrement( &hdr
->recursion_count
);
55 TRACE("returning from %#lx callback\n", status
);
59 /***********************************************************************
60 * WinHttpCheckPlatform (winhttp.@)
62 BOOL WINAPI
WinHttpCheckPlatform( void )
68 static void session_destroy( struct object_header
*hdr
)
70 struct session
*session
= (struct session
*)hdr
;
72 TRACE("%p\n", session
);
74 if (session
->unload_event
) SetEvent( session
->unload_event
);
75 destroy_cookies( session
);
77 session
->cs
.DebugInfo
->Spare
[0] = 0;
78 DeleteCriticalSection( &session
->cs
);
79 free( session
->agent
);
80 free( session
->proxy_server
);
81 free( session
->proxy_bypass
);
82 free( session
->proxy_username
);
83 free( session
->proxy_password
);
87 static BOOL
validate_buffer( void *buffer
, DWORD
*buflen
, DWORD required
)
89 if (!buffer
|| *buflen
< required
)
92 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
98 static BOOL
session_query_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD
*buflen
)
100 struct session
*session
= (struct session
*)hdr
;
104 case WINHTTP_OPTION_REDIRECT_POLICY
:
106 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
108 *(DWORD
*)buffer
= hdr
->redirect_policy
;
109 *buflen
= sizeof(DWORD
);
112 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
113 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
115 *(DWORD
*)buffer
= session
->resolve_timeout
;
116 *buflen
= sizeof(DWORD
);
119 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
120 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
122 *(DWORD
*)buffer
= session
->connect_timeout
;
123 *buflen
= sizeof(DWORD
);
126 case WINHTTP_OPTION_SEND_TIMEOUT
:
127 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
129 *(DWORD
*)buffer
= session
->send_timeout
;
130 *buflen
= sizeof(DWORD
);
133 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
134 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
136 *(DWORD
*)buffer
= session
->receive_timeout
;
137 *buflen
= sizeof(DWORD
);
140 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT
:
141 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
143 *(DWORD
*)buffer
= session
->receive_response_timeout
;
144 *buflen
= sizeof(DWORD
);
147 case WINHTTP_OPTION_WEB_SOCKET_RECEIVE_BUFFER_SIZE
:
148 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
150 *(DWORD
*)buffer
= session
->websocket_receive_buffer_size
;
151 *buflen
= sizeof(DWORD
);
154 case WINHTTP_OPTION_WEB_SOCKET_SEND_BUFFER_SIZE
:
155 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
157 *(DWORD
*)buffer
= session
->websocket_send_buffer_size
;
158 *buflen
= sizeof(DWORD
);
162 FIXME( "unimplemented option %lu\n", option
);
163 SetLastError( ERROR_INVALID_PARAMETER
);
168 static BOOL
session_set_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD buflen
)
170 struct session
*session
= (struct session
*)hdr
;
174 case WINHTTP_OPTION_PROXY
:
176 WINHTTP_PROXY_INFO
*pi
= buffer
;
178 FIXME( "%lu %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
) );
181 case WINHTTP_OPTION_REDIRECT_POLICY
:
185 if (buflen
!= sizeof(policy
))
187 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
191 policy
= *(DWORD
*)buffer
;
192 TRACE( "%#lx\n", policy
);
193 hdr
->redirect_policy
= policy
;
196 case WINHTTP_OPTION_SECURE_PROTOCOLS
:
198 if (buflen
!= sizeof(session
->secure_protocols
))
200 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
203 EnterCriticalSection( &session
->cs
);
204 session
->secure_protocols
= *(DWORD
*)buffer
;
205 LeaveCriticalSection( &session
->cs
);
206 TRACE( "%#lx\n", session
->secure_protocols
);
209 case WINHTTP_OPTION_DISABLE_FEATURE
:
210 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
213 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
214 session
->resolve_timeout
= *(DWORD
*)buffer
;
217 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
218 session
->connect_timeout
= *(DWORD
*)buffer
;
221 case WINHTTP_OPTION_SEND_TIMEOUT
:
222 session
->send_timeout
= *(DWORD
*)buffer
;
225 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
226 session
->receive_timeout
= *(DWORD
*)buffer
;
229 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT
:
230 session
->receive_response_timeout
= *(DWORD
*)buffer
;
233 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
234 session
->passport_flags
= *(DWORD
*)buffer
;
237 case WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT
:
238 TRACE("WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT: %p\n", *(HANDLE
*)buffer
);
239 session
->unload_event
= *(HANDLE
*)buffer
;
242 case WINHTTP_OPTION_MAX_CONNS_PER_SERVER
:
243 FIXME( "WINHTTP_OPTION_MAX_CONNS_PER_SERVER: %lu\n", *(DWORD
*)buffer
);
246 case WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER
:
247 FIXME( "WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER: %lu\n", *(DWORD
*)buffer
);
250 case WINHTTP_OPTION_WEB_SOCKET_RECEIVE_BUFFER_SIZE
:
254 if (buflen
!= sizeof(buffer_size
))
256 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
260 buffer_size
= *(DWORD
*)buffer
;
261 TRACE( "%#lx\n", buffer_size
);
262 session
->websocket_receive_buffer_size
= buffer_size
;
266 case WINHTTP_OPTION_WEB_SOCKET_SEND_BUFFER_SIZE
:
270 if (buflen
!= sizeof(buffer_size
))
272 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
276 buffer_size
= *(DWORD
*)buffer
;
277 TRACE( "%#lx\n", buffer_size
);
278 session
->websocket_send_buffer_size
= buffer_size
;
283 FIXME( "unimplemented option %lu\n", option
);
284 SetLastError( ERROR_WINHTTP_INVALID_OPTION
);
289 static const struct object_vtbl session_vtbl
=
293 session_query_option
,
297 /***********************************************************************
298 * WinHttpOpen (winhttp.@)
300 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
302 struct session
*session
;
303 HINTERNET handle
= NULL
;
305 TRACE( "%s, %lu, %s, %s, %#lx\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
307 if (!(session
= calloc( 1, sizeof(*session
) ))) return NULL
;
309 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
310 session
->hdr
.vtbl
= &session_vtbl
;
311 session
->hdr
.flags
= flags
;
312 session
->hdr
.refs
= 1;
313 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
314 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
315 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
316 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
317 session
->receive_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
318 session
->receive_response_timeout
= DEFAULT_RECEIVE_RESPONSE_TIMEOUT
;
319 session
->websocket_receive_buffer_size
= 32768;
320 session
->websocket_send_buffer_size
= 32768;
321 list_init( &session
->cookie_cache
);
322 InitializeCriticalSection( &session
->cs
);
323 session
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": session.cs");
325 if (agent
&& !(session
->agent
= wcsdup( agent
))) goto end
;
326 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
328 WINHTTP_PROXY_INFO info
;
330 WinHttpGetDefaultProxyConfiguration( &info
);
331 session
->access
= info
.dwAccessType
;
332 if (info
.lpszProxy
&& !(session
->proxy_server
= wcsdup( info
.lpszProxy
)))
334 GlobalFree( info
.lpszProxy
);
335 GlobalFree( info
.lpszProxyBypass
);
338 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= wcsdup( info
.lpszProxyBypass
)))
340 GlobalFree( info
.lpszProxy
);
341 GlobalFree( info
.lpszProxyBypass
);
345 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
347 session
->access
= access
;
348 if (proxy
&& !(session
->proxy_server
= wcsdup( proxy
))) goto end
;
349 if (bypass
&& !(session
->proxy_bypass
= wcsdup( bypass
))) goto end
;
352 handle
= alloc_handle( &session
->hdr
);
355 release_object( &session
->hdr
);
356 TRACE("returning %p\n", handle
);
357 if (handle
) SetLastError( ERROR_SUCCESS
);
361 static void connect_destroy( struct object_header
*hdr
)
363 struct connect
*connect
= (struct connect
*)hdr
;
365 TRACE("%p\n", connect
);
367 release_object( &connect
->session
->hdr
);
369 free( connect
->hostname
);
370 free( connect
->servername
);
371 free( connect
->username
);
372 free( connect
->password
);
376 static BOOL
connect_query_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD
*buflen
)
378 struct connect
*connect
= (struct connect
*)hdr
;
382 case WINHTTP_OPTION_PARENT_HANDLE
:
384 if (!validate_buffer( buffer
, buflen
, sizeof(HINTERNET
) )) return FALSE
;
386 *(HINTERNET
*)buffer
= connect
->session
->hdr
.handle
;
387 *buflen
= sizeof(HINTERNET
);
390 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
391 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
393 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
394 *buflen
= sizeof(DWORD
);
397 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
398 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
400 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
401 *buflen
= sizeof(DWORD
);
404 case WINHTTP_OPTION_SEND_TIMEOUT
:
405 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
407 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
408 *buflen
= sizeof(DWORD
);
411 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
412 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
414 *(DWORD
*)buffer
= connect
->session
->receive_timeout
;
415 *buflen
= sizeof(DWORD
);
418 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT
:
419 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
421 *(DWORD
*)buffer
= connect
->session
->receive_response_timeout
;
422 *buflen
= sizeof(DWORD
);
426 FIXME( "unimplemented option %lu\n", option
);
427 SetLastError( ERROR_INVALID_PARAMETER
);
432 static const struct object_vtbl connect_vtbl
=
436 connect_query_option
,
440 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
444 if (!wcsicmp( domain
, L
"<local>" ) && !wcschr( server
, '.' ))
446 else if (*domain
== '*')
448 if (domain
[1] == '.')
452 /* For a hostname to match a wildcard, the last domain must match
453 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
454 * hostname is www.foo.a.b, it matches, but a.b does not.
456 dot
= wcschr( server
, '.' );
459 int len
= lstrlenW( dot
+ 1 );
461 if (len
> lstrlenW( domain
+ 2 ))
465 /* The server's domain is longer than the wildcard, so it
466 * could be a subdomain. Compare the last portion of the
469 ptr
= dot
+ len
+ 1 - lstrlenW( domain
+ 2 );
470 if (!wcsicmp( ptr
, domain
+ 2 ))
472 /* This is only a match if the preceding character is
473 * a '.', i.e. that it is a matching domain. E.g.
474 * if domain is '*.b.c' and server is 'www.ab.c' they
477 ret
= *(ptr
- 1) == '.';
481 ret
= !wcsicmp( dot
+ 1, domain
+ 2 );
486 ret
= !wcsicmp( server
, domain
);
490 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
491 #define MAX_HOST_NAME_LENGTH 256
493 static BOOL
should_bypass_proxy(struct session
*session
, LPCWSTR server
)
498 if (!session
->proxy_bypass
) return FALSE
;
499 ptr
= session
->proxy_bypass
;
503 ptr
= wcschr( ptr
, ';' );
505 ptr
= wcschr( tmp
, ' ' );
508 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
510 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
512 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
513 domain
[ptr
- tmp
] = 0;
514 ret
= domain_matches( server
, domain
);
519 ret
= domain_matches( server
, tmp
);
520 } while (ptr
&& !ret
);
524 BOOL
set_server_for_hostname( struct connect
*connect
, const WCHAR
*server
, INTERNET_PORT port
)
526 struct session
*session
= connect
->session
;
529 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
533 if ((colon
= wcschr( session
->proxy_server
, ':' )))
535 if (!connect
->servername
|| wcsnicmp( connect
->servername
,
536 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
538 free( connect
->servername
);
539 connect
->resolved
= FALSE
;
540 if (!(connect
->servername
= malloc( (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
545 memcpy( connect
->servername
, session
->proxy_server
, (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
546 connect
->servername
[colon
- session
->proxy_server
] = 0;
548 connect
->serverport
= wcstol( colon
+ 1, NULL
, 10 );
550 connect
->serverport
= INTERNET_DEFAULT_PORT
;
555 if (!connect
->servername
|| wcsicmp( connect
->servername
, session
->proxy_server
))
557 free( connect
->servername
);
558 connect
->resolved
= FALSE
;
559 if (!(connect
->servername
= wcsdup( session
->proxy_server
)))
564 connect
->serverport
= INTERNET_DEFAULT_PORT
;
570 free( connect
->servername
);
571 connect
->resolved
= FALSE
;
572 if (!(connect
->servername
= wcsdup( server
)))
577 connect
->serverport
= port
;
583 /***********************************************************************
584 * WinHttpConnect (winhttp.@)
586 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, const WCHAR
*server
, INTERNET_PORT port
, DWORD reserved
)
588 struct connect
*connect
;
589 struct session
*session
;
590 HINTERNET hconnect
= NULL
;
592 TRACE( "%p, %s, %u, %#lx\n", hsession
, debugstr_w(server
), port
, reserved
);
596 SetLastError( ERROR_INVALID_PARAMETER
);
599 if (!(session
= (struct session
*)grab_object( hsession
)))
601 SetLastError( ERROR_INVALID_HANDLE
);
604 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
606 release_object( &session
->hdr
);
607 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
610 if (!(connect
= calloc( 1, sizeof(*connect
) )))
612 release_object( &session
->hdr
);
615 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
616 connect
->hdr
.vtbl
= &connect_vtbl
;
617 connect
->hdr
.refs
= 1;
618 connect
->hdr
.flags
= session
->hdr
.flags
;
619 connect
->hdr
.callback
= session
->hdr
.callback
;
620 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
621 connect
->hdr
.context
= session
->hdr
.context
;
622 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
624 addref_object( &session
->hdr
);
625 connect
->session
= session
;
627 if (!(connect
->hostname
= wcsdup( server
))) goto end
;
628 connect
->hostport
= port
;
629 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
631 if ((hconnect
= alloc_handle( &connect
->hdr
)))
633 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
637 release_object( &connect
->hdr
);
638 release_object( &session
->hdr
);
639 TRACE("returning %p\n", hconnect
);
640 if (hconnect
) SetLastError( ERROR_SUCCESS
);
644 static void request_destroy( struct object_header
*hdr
)
646 struct request
*request
= (struct request
*)hdr
;
649 TRACE("%p\n", request
);
651 stop_queue( &request
->queue
);
652 release_object( &request
->connect
->hdr
);
654 if (request
->cred_handle_initialized
) FreeCredentialsHandle( &request
->cred_handle
);
655 CertFreeCertificateContext( request
->server_cert
);
656 CertFreeCertificateContext( request
->client_cert
);
658 destroy_authinfo( request
->authinfo
);
659 destroy_authinfo( request
->proxy_authinfo
);
661 free( request
->verb
);
662 free( request
->path
);
663 free( request
->version
);
664 free( request
->raw_headers
);
665 free( request
->status_text
);
666 for (i
= 0; i
< request
->num_headers
; i
++)
668 free( request
->headers
[i
].field
);
669 free( request
->headers
[i
].value
);
671 free( request
->headers
);
672 for (i
= 0; i
< TARGET_MAX
; i
++)
674 for (j
= 0; j
< SCHEME_MAX
; j
++)
676 free( request
->creds
[i
][j
].username
);
677 free( request
->creds
[i
][j
].password
);
684 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
687 if (str
) len
= lstrlenW( str
);
688 if (buffer
&& *buflen
> len
)
690 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
693 *buflen
= len
* sizeof(WCHAR
);
696 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
699 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
701 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
702 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
703 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
708 static BOOL
copy_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
710 switch (addr
->sa_family
)
714 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
716 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
717 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
722 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
724 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
725 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
729 ERR("unhandled family %u\n", addr
->sa_family
);
734 static BOOL
request_query_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD
*buflen
)
736 struct request
*request
= (struct request
*)hdr
;
740 case WINHTTP_OPTION_PARENT_HANDLE
:
742 if (!validate_buffer( buffer
, buflen
, sizeof(HINTERNET
) )) return FALSE
;
744 *(HINTERNET
*)buffer
= request
->connect
->hdr
.handle
;
745 *buflen
= sizeof(HINTERNET
);
748 case WINHTTP_OPTION_SECURITY_FLAGS
:
753 if (!validate_buffer( buffer
, buflen
, sizeof(flags
) )) return FALSE
;
755 flags
= request
->security_flags
;
756 if (request
->netconn
)
758 bits
= netconn_get_cipher_strength( request
->netconn
);
760 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
762 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
764 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
766 *(DWORD
*)buffer
= flags
;
767 *buflen
= sizeof(flags
);
770 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
772 const CERT_CONTEXT
*cert
;
774 if (!validate_buffer( buffer
, buflen
, sizeof(cert
) )) return FALSE
;
776 if (!(cert
= CertDuplicateCertificateContext( request
->server_cert
))) return FALSE
;
777 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
778 *buflen
= sizeof(cert
);
781 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
783 const CERT_CONTEXT
*cert
= request
->server_cert
;
784 const CRYPT_OID_INFO
*oidInfo
;
785 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
787 FIXME("partial stub\n");
789 if (!validate_buffer( buffer
, buflen
, sizeof(*ci
) ) || !cert
) return FALSE
;
791 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
792 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
793 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
794 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
795 ci
->lpszProtocolName
= NULL
;
796 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
, cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
, 0 );
798 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
800 ci
->lpszSignatureAlgName
= NULL
;
801 ci
->lpszEncryptionAlgName
= NULL
;
802 ci
->dwKeySize
= request
->netconn
? netconn_get_cipher_strength( request
->netconn
) : 0;
804 *buflen
= sizeof(*ci
);
807 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
809 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
811 *(DWORD
*)buffer
= request
->netconn
? netconn_get_cipher_strength( request
->netconn
) : 0;
812 *buflen
= sizeof(DWORD
);
815 case WINHTTP_OPTION_CONNECTION_INFO
:
817 WINHTTP_CONNECTION_INFO
*info
= buffer
;
818 struct sockaddr local
;
819 socklen_t len
= sizeof(local
);
820 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
822 if (!validate_buffer( buffer
, buflen
, sizeof(*info
) )) return FALSE
;
824 if (!request
->netconn
)
826 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
829 if (getsockname( request
->netconn
->socket
, &local
, &len
)) return FALSE
;
830 if (!copy_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
831 if (!copy_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
832 info
->cbSize
= sizeof(*info
);
835 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
836 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
838 *(DWORD
*)buffer
= request
->resolve_timeout
;
839 *buflen
= sizeof(DWORD
);
842 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
843 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
845 *(DWORD
*)buffer
= request
->connect_timeout
;
846 *buflen
= sizeof(DWORD
);
849 case WINHTTP_OPTION_SEND_TIMEOUT
:
850 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
852 *(DWORD
*)buffer
= request
->send_timeout
;
853 *buflen
= sizeof(DWORD
);
856 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
857 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
859 *(DWORD
*)buffer
= request
->receive_timeout
;
860 *buflen
= sizeof(DWORD
);
863 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT
:
864 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
866 *(DWORD
*)buffer
= request
->receive_response_timeout
;
867 *buflen
= sizeof(DWORD
);
870 case WINHTTP_OPTION_USERNAME
:
871 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
874 case WINHTTP_OPTION_PASSWORD
:
875 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
878 case WINHTTP_OPTION_PROXY_USERNAME
:
879 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
882 case WINHTTP_OPTION_PROXY_PASSWORD
:
883 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
886 case WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS
:
887 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
889 *(DWORD
*)buffer
= request
->max_redirects
;
890 *buflen
= sizeof(DWORD
);
893 case WINHTTP_OPTION_HTTP_PROTOCOL_USED
:
894 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
896 FIXME("WINHTTP_OPTION_HTTP_PROTOCOL_USED\n");
897 *(DWORD
*)buffer
= 0;
898 *buflen
= sizeof(DWORD
);
901 case WINHTTP_OPTION_WEB_SOCKET_RECEIVE_BUFFER_SIZE
:
902 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
904 *(DWORD
*)buffer
= request
->websocket_receive_buffer_size
;
905 *buflen
= sizeof(DWORD
);
908 case WINHTTP_OPTION_WEB_SOCKET_SEND_BUFFER_SIZE
:
909 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
911 *(DWORD
*)buffer
= request
->websocket_set_send_buffer_size
;
912 *buflen
= sizeof(DWORD
);
916 FIXME( "unimplemented option %lu\n", option
);
917 SetLastError( ERROR_INVALID_PARAMETER
);
922 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
925 if ((ret
= malloc( (buflen
+ 1) * sizeof(WCHAR
))))
927 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
931 SetLastError( ERROR_OUTOFMEMORY
);
935 static BOOL
request_set_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD buflen
)
937 struct request
*request
= (struct request
*)hdr
;
941 case WINHTTP_OPTION_PROXY
:
943 WINHTTP_PROXY_INFO
*pi
= buffer
;
945 FIXME( "%lu %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
) );
948 case WINHTTP_OPTION_DISABLE_FEATURE
:
952 if (buflen
!= sizeof(DWORD
))
954 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
958 disable
= *(DWORD
*)buffer
;
959 TRACE( "%#lx\n", disable
);
960 hdr
->disable_flags
|= disable
;
963 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
967 if (buflen
!= sizeof(DWORD
))
969 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
973 policy
= *(DWORD
*)buffer
;
974 TRACE( "%#lx\n", policy
);
975 hdr
->logon_policy
= policy
;
978 case WINHTTP_OPTION_REDIRECT_POLICY
:
982 if (buflen
!= sizeof(DWORD
))
984 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
988 policy
= *(DWORD
*)buffer
;
989 TRACE( "%#lx\n", policy
);
990 hdr
->redirect_policy
= policy
;
993 case WINHTTP_OPTION_SECURITY_FLAGS
:
996 static const DWORD accepted
= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
997 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
998 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
999 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
;
1001 if (buflen
< sizeof(DWORD
))
1003 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1006 flags
= *(DWORD
*)buffer
;
1007 TRACE( "%#lx\n", flags
);
1008 if (flags
&& (flags
& ~accepted
))
1010 SetLastError( ERROR_INVALID_PARAMETER
);
1013 request
->security_flags
= flags
;
1016 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
1017 request
->resolve_timeout
= *(DWORD
*)buffer
;
1020 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
1021 request
->connect_timeout
= *(DWORD
*)buffer
;
1024 case WINHTTP_OPTION_SEND_TIMEOUT
:
1025 request
->send_timeout
= *(DWORD
*)buffer
;
1028 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
1029 request
->receive_timeout
= *(DWORD
*)buffer
;
1032 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT
:
1033 request
->receive_response_timeout
= *(DWORD
*)buffer
;
1036 case WINHTTP_OPTION_USERNAME
:
1038 struct connect
*connect
= request
->connect
;
1040 free( connect
->username
);
1041 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
1044 case WINHTTP_OPTION_PASSWORD
:
1046 struct connect
*connect
= request
->connect
;
1048 free( connect
->password
);
1049 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
1052 case WINHTTP_OPTION_PROXY_USERNAME
:
1054 struct session
*session
= request
->connect
->session
;
1056 free( session
->proxy_username
);
1057 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
1060 case WINHTTP_OPTION_PROXY_PASSWORD
:
1062 struct session
*session
= request
->connect
->session
;
1064 free( session
->proxy_password
);
1065 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
1068 case WINHTTP_OPTION_CLIENT_CERT_CONTEXT
:
1070 const CERT_CONTEXT
*cert
;
1072 if (!(hdr
->flags
& WINHTTP_FLAG_SECURE
))
1074 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
1079 CertFreeCertificateContext( request
->client_cert
);
1080 request
->client_cert
= NULL
;
1082 else if (buflen
>= sizeof(*cert
))
1084 if (!(cert
= CertDuplicateCertificateContext( buffer
))) return FALSE
;
1085 CertFreeCertificateContext( request
->client_cert
);
1086 request
->client_cert
= cert
;
1090 SetLastError( ERROR_INVALID_PARAMETER
);
1094 if (request
->cred_handle_initialized
)
1096 FreeCredentialsHandle( &request
->cred_handle
);
1097 request
->cred_handle_initialized
= FALSE
;
1102 case WINHTTP_OPTION_ENABLE_FEATURE
:
1103 if(buflen
== sizeof( DWORD
) && *(DWORD
*)buffer
== WINHTTP_ENABLE_SSL_REVOCATION
)
1105 request
->check_revocation
= TRUE
;
1106 SetLastError( NO_ERROR
);
1111 SetLastError( ERROR_INVALID_PARAMETER
);
1115 case WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET
:
1116 request
->flags
|= REQUEST_FLAG_WEBSOCKET_UPGRADE
;
1119 case WINHTTP_OPTION_CONNECT_RETRIES
:
1120 FIXME("WINHTTP_OPTION_CONNECT_RETRIES\n");
1123 case WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS
:
1124 if (buflen
== sizeof(DWORD
))
1126 request
->max_redirects
= *(DWORD
*)buffer
;
1127 SetLastError(NO_ERROR
);
1131 SetLastError(ERROR_INVALID_PARAMETER
);
1134 case WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE
:
1135 FIXME("WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE\n");
1138 case WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE
:
1139 FIXME("WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE\n");
1142 case WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL
:
1143 if (buflen
== sizeof(DWORD
))
1145 FIXME( "WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL %#lx\n", *(DWORD
*)buffer
);
1148 SetLastError(ERROR_INVALID_PARAMETER
);
1151 case WINHTTP_OPTION_WEB_SOCKET_RECEIVE_BUFFER_SIZE
:
1155 if (buflen
!= sizeof(buffer_size
))
1157 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1161 buffer_size
= *(DWORD
*)buffer
;
1162 WARN( "Setting websocket receive buffer size currently has not effct, size %lu\n", buffer_size
);
1163 request
->websocket_receive_buffer_size
= buffer_size
;
1167 case WINHTTP_OPTION_WEB_SOCKET_SEND_BUFFER_SIZE
:
1171 if (buflen
!= sizeof(buffer_size
))
1173 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1177 buffer_size
= *(DWORD
*)buffer
;
1178 request
->websocket_set_send_buffer_size
= buffer_size
;
1179 TRACE( "Websocket send buffer size %lu.\n", buffer_size
);
1184 FIXME( "unimplemented option %lu\n", option
);
1185 SetLastError( ERROR_WINHTTP_INVALID_OPTION
);
1190 static const struct object_vtbl request_vtbl
=
1194 request_query_option
,
1198 static BOOL
add_accept_types_header( struct request
*request
, const WCHAR
**types
)
1200 static const DWORD flags
= WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
;
1202 if (!types
) return TRUE
;
1205 if (process_header( request
, L
"Accept", *types
, flags
, TRUE
)) return FALSE
;
1211 static WCHAR
*get_request_path( const WCHAR
*object
)
1213 int len
= object
? lstrlenW(object
) : 0;
1216 if (!object
|| object
[0] != '/') len
++;
1217 if (!(p
= ret
= malloc( (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1218 if (!object
|| object
[0] != '/') *p
++ = '/';
1219 if (object
) lstrcpyW( p
, object
);
1224 /***********************************************************************
1225 * WinHttpOpenRequest (winhttp.@)
1227 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, const WCHAR
*verb
, const WCHAR
*object
, const WCHAR
*version
,
1228 const WCHAR
*referrer
, const WCHAR
**types
, DWORD flags
)
1230 struct request
*request
;
1231 struct connect
*connect
;
1232 HINTERNET hrequest
= NULL
;
1234 TRACE( "%p, %s, %s, %s, %s, %p, %#lx\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1235 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1237 if (types
&& TRACE_ON(winhttp
))
1240 TRACE("accept types:\n");
1241 for (iter
= types
; *iter
; iter
++) TRACE(" %s\n", debugstr_w(*iter
));
1244 if (!(connect
= (struct connect
*)grab_object( hconnect
)))
1246 SetLastError( ERROR_INVALID_HANDLE
);
1249 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1251 release_object( &connect
->hdr
);
1252 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1255 if (!(request
= calloc( 1, sizeof(*request
) )))
1257 release_object( &connect
->hdr
);
1260 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1261 request
->hdr
.vtbl
= &request_vtbl
;
1262 request
->hdr
.refs
= 1;
1263 request
->hdr
.flags
= flags
;
1264 request
->hdr
.callback
= connect
->hdr
.callback
;
1265 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1266 request
->hdr
.context
= connect
->hdr
.context
;
1267 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1268 init_queue( &request
->queue
);
1270 addref_object( &connect
->hdr
);
1271 request
->connect
= connect
;
1273 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1274 request
->connect_timeout
= connect
->session
->connect_timeout
;
1275 request
->send_timeout
= connect
->session
->send_timeout
;
1276 request
->receive_timeout
= connect
->session
->receive_timeout
;
1277 request
->receive_response_timeout
= connect
->session
->receive_response_timeout
;
1278 request
->max_redirects
= 10;
1279 request
->websocket_receive_buffer_size
= connect
->session
->websocket_receive_buffer_size
;
1280 request
->websocket_send_buffer_size
= connect
->session
->websocket_send_buffer_size
;
1281 request
->websocket_set_send_buffer_size
= request
->websocket_send_buffer_size
;
1282 request
->read_reply_status
= ERROR_WINHTTP_INCORRECT_HANDLE_STATE
;
1284 if (!verb
|| !verb
[0]) verb
= L
"GET";
1285 if (!(request
->verb
= wcsdup( verb
))) goto end
;
1286 if (!(request
->path
= get_request_path( object
))) goto end
;
1288 if (!version
|| !version
[0]) version
= L
"HTTP/1.1";
1289 if (!(request
->version
= wcsdup( version
))) goto end
;
1290 if (!(add_accept_types_header( request
, types
))) goto end
;
1292 if ((hrequest
= alloc_handle( &request
->hdr
)))
1294 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1298 release_object( &request
->hdr
);
1299 release_object( &connect
->hdr
);
1300 TRACE("returning %p\n", hrequest
);
1301 if (hrequest
) SetLastError( ERROR_SUCCESS
);
1305 /***********************************************************************
1306 * WinHttpCloseHandle (winhttp.@)
1308 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1310 struct object_header
*hdr
;
1312 TRACE("%p\n", handle
);
1314 if (!(hdr
= grab_object( handle
)))
1316 SetLastError( ERROR_INVALID_HANDLE
);
1319 release_object( hdr
);
1320 free_handle( handle
);
1321 SetLastError( ERROR_SUCCESS
);
1325 static BOOL
query_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD
*buflen
)
1331 SetLastError( ERROR_INVALID_PARAMETER
);
1337 case WINHTTP_OPTION_WORKER_THREAD_COUNT
:
1339 FIXME( "WINHTTP_OPTION_WORKER_THREAD_COUNT semi-stub.\n" );
1340 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD
) )) return FALSE
;
1342 *(DWORD
*)buffer
= 0;
1343 *buflen
= sizeof(DWORD
);
1346 case WINHTTP_OPTION_CONTEXT_VALUE
:
1348 if (!validate_buffer( buffer
, buflen
, sizeof(DWORD_PTR
) )) return FALSE
;
1350 *(DWORD_PTR
*)buffer
= hdr
->context
;
1351 *buflen
= sizeof(DWORD_PTR
);
1355 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1358 FIXME( "unimplemented option %lu\n", option
);
1359 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1367 /***********************************************************************
1368 * WinHttpQueryOption (winhttp.@)
1370 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, void *buffer
, DWORD
*buflen
)
1373 struct object_header
*hdr
;
1375 TRACE( "%p, %lu, %p, %p\n", handle
, option
, buffer
, buflen
);
1377 if (!(hdr
= grab_object( handle
)))
1379 SetLastError( ERROR_INVALID_HANDLE
);
1383 ret
= query_option( hdr
, option
, buffer
, buflen
);
1385 release_object( hdr
);
1386 if (ret
) SetLastError( ERROR_SUCCESS
);
1390 static BOOL
set_option( struct object_header
*hdr
, DWORD option
, void *buffer
, DWORD buflen
)
1394 if (!buffer
&& buflen
)
1396 SetLastError( ERROR_INVALID_PARAMETER
);
1402 case WINHTTP_OPTION_CONTEXT_VALUE
:
1404 if (buflen
!= sizeof(DWORD_PTR
))
1406 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1410 hdr
->context
= *(DWORD_PTR
*)buffer
;
1414 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1417 FIXME( "unimplemented option %lu\n", option
);
1418 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1426 /***********************************************************************
1427 * WinHttpSetOption (winhttp.@)
1429 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, void *buffer
, DWORD buflen
)
1432 struct object_header
*hdr
;
1434 TRACE( "%p, %lu, %p, %lu\n", handle
, option
, buffer
, buflen
);
1436 if (!(hdr
= grab_object( handle
)))
1438 SetLastError( ERROR_INVALID_HANDLE
);
1442 ret
= set_option( hdr
, option
, buffer
, buflen
);
1444 release_object( hdr
);
1445 if (ret
) SetLastError( ERROR_SUCCESS
);
1449 static IP_ADAPTER_ADDRESSES
*get_adapters(void)
1451 ULONG err
, size
= 1024, flags
= GAA_FLAG_SKIP_ANYCAST
| GAA_FLAG_SKIP_MULTICAST
|
1452 GAA_FLAG_SKIP_DNS_SERVER
| GAA_FLAG_SKIP_FRIENDLY_NAME
;
1453 IP_ADAPTER_ADDRESSES
*tmp
, *ret
;
1455 if (!(ret
= malloc( size
))) return NULL
;
1456 err
= GetAdaptersAddresses( AF_UNSPEC
, flags
, NULL
, ret
, &size
);
1457 while (err
== ERROR_BUFFER_OVERFLOW
)
1459 if (!(tmp
= realloc( ret
, size
))) break;
1461 err
= GetAdaptersAddresses( AF_UNSPEC
, flags
, NULL
, ret
, &size
);
1463 if (err
== ERROR_SUCCESS
) return ret
;
1468 static WCHAR
*detect_autoproxyconfig_url_dhcp(void)
1470 IP_ADAPTER_ADDRESSES
*adapters
, *ptr
;
1471 DHCPCAPI_PARAMS_ARRAY send_params
, recv_params
;
1472 DHCPCAPI_PARAMS param
;
1473 WCHAR name
[MAX_ADAPTER_NAME_LENGTH
+ 1], *ret
= NULL
;
1475 BYTE
*tmp
, *buf
= NULL
;
1477 if (!(adapters
= get_adapters())) return NULL
;
1479 memset( &send_params
, 0, sizeof(send_params
) );
1480 memset( ¶m
, 0, sizeof(param
) );
1481 param
.OptionId
= OPTION_MSFT_IE_PROXY
;
1482 recv_params
.nParams
= 1;
1483 recv_params
.Params
= ¶m
;
1485 for (ptr
= adapters
; ptr
; ptr
= ptr
->Next
)
1487 MultiByteToWideChar( CP_ACP
, 0, ptr
->AdapterName
, -1, name
, ARRAY_SIZE(name
) );
1488 TRACE( "adapter '%s' type %lu dhcpv4 enabled %d\n", wine_dbgstr_w(name
), ptr
->IfType
, ptr
->Dhcpv4Enabled
);
1490 if (ptr
->IfType
== IF_TYPE_SOFTWARE_LOOPBACK
) continue;
1491 /* FIXME: also skip adapters where DHCP is disabled */
1494 if (!(buf
= malloc( size
))) goto done
;
1495 err
= DhcpRequestParams( DHCPCAPI_REQUEST_SYNCHRONOUS
, NULL
, name
, NULL
, send_params
, recv_params
,
1497 while (err
== ERROR_MORE_DATA
)
1499 if (!(tmp
= realloc( buf
, size
))) goto done
;
1501 err
= DhcpRequestParams( DHCPCAPI_REQUEST_SYNCHRONOUS
, NULL
, name
, NULL
, send_params
, recv_params
,
1504 if (err
== ERROR_SUCCESS
&& param
.nBytesData
)
1506 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)param
.Data
, param
.nBytesData
, NULL
, 0 );
1507 if ((ret
= malloc( (len
+ 1) * sizeof(WCHAR
) )))
1509 MultiByteToWideChar( CP_ACP
, 0, (const char *)param
.Data
, param
.nBytesData
, ret
, len
);
1512 TRACE("returning %s\n", debugstr_w(ret
));
1523 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1528 GetComputerNameExA( format
, NULL
, &size
);
1529 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1530 if (!(ret
= malloc( size
))) return NULL
;
1531 if (!GetComputerNameExA( format
, ret
, &size
))
1539 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1541 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1543 if (len_suffix
> len_domain
) return FALSE
;
1544 if (!stricmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1548 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1550 return getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1553 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1555 char name
[NI_MAXHOST
];
1559 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1560 if (!ai
) return NULL
;
1562 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1564 len
= lstrlenW( L
"http://" ) + strlen( hostname
) + lstrlenW( L
"/wpad.dat" );
1565 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1566 lstrcpyW( p
, L
"http://" );
1567 p
+= lstrlenW( L
"http://" );
1568 while (*hostname
) { *p
++ = *hostname
++; }
1569 lstrcpyW( p
, L
"/wpad.dat" );
1573 static WCHAR
*detect_autoproxyconfig_url_dns(void)
1575 char *fqdn
, *domain
, *p
;
1578 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return NULL
;
1579 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1585 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1588 struct addrinfo
*ai
, hints
;
1591 if (!(name
= malloc( sizeof("wpad") + strlen(p
) )))
1597 strcpy( name
, "wpad" );
1599 memset( &hints
, 0, sizeof(hints
) );
1600 hints
.ai_flags
= AI_ALL
| AI_DNS_ONLY
;
1601 hints
.ai_family
= AF_UNSPEC
;
1602 res
= getaddrinfo( name
, NULL
, &hints
, &ai
);
1605 ret
= build_wpad_url( name
, ai
);
1609 TRACE("returning %s\n", debugstr_w(ret
));
1622 /***********************************************************************
1623 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1625 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, WCHAR
**url
)
1627 TRACE( "%#lx, %p\n", flags
, url
);
1631 SetLastError( ERROR_INVALID_PARAMETER
);
1635 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1637 *url
= detect_autoproxyconfig_url_dhcp();
1639 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1641 if (!*url
) *url
= detect_autoproxyconfig_url_dns();
1645 SetLastError( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1648 SetLastError( ERROR_SUCCESS
);
1652 static const WCHAR path_connections
[] =
1653 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections";
1655 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1656 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1657 static const DWORD PROXY_TYPE_DIRECT
= 1;
1658 static const DWORD PROXY_TYPE_PROXY
= 2;
1659 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1660 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1662 struct connection_settings_header
1665 DWORD unknown
; /* always zero? */
1666 DWORD flags
; /* one or more of PROXY_* */
1669 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1673 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1678 /***********************************************************************
1679 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1681 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1685 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1688 TRACE("%p\n", info
);
1690 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, path_connections
, 0, KEY_READ
, &key
);
1693 DWORD type
, size
= 0;
1695 l
= RegQueryValueExW( key
, L
"WinHttpSettings", NULL
, &type
, NULL
, &size
);
1696 if (!l
&& type
== REG_BINARY
&&
1697 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1699 BYTE
*buf
= malloc( size
);
1703 struct connection_settings_header
*hdr
=
1704 (struct connection_settings_header
*)buf
;
1705 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1707 l
= RegQueryValueExW( key
, L
"WinHttpSettings", NULL
, NULL
, buf
,
1709 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1712 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1715 LPWSTR proxy
= NULL
;
1716 LPWSTR proxy_bypass
= NULL
;
1718 /* Sanity-check length of proxy string */
1719 if ((BYTE
*)len
- buf
+ *len
<= size
)
1722 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1724 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1725 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1729 /* Sanity-check length of proxy bypass string */
1730 if ((BYTE
*)len
- buf
+ *len
<= size
)
1732 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1734 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1739 GlobalFree( proxy
);
1743 info
->lpszProxy
= proxy
;
1744 info
->lpszProxyBypass
= proxy_bypass
;
1747 got_from_reg
= TRUE
;
1749 info
->dwAccessType
=
1750 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1751 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1752 debugstr_w(info
->lpszProxy
),
1753 debugstr_w(info
->lpszProxyBypass
));
1762 if (!got_from_reg
&& (envproxy
= _wgetenv( L
"http_proxy" )))
1764 WCHAR
*colon
, *http_proxy
= NULL
;
1766 if (!(colon
= wcschr( envproxy
, ':' ))) http_proxy
= envproxy
;
1769 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1771 /* It's a scheme, check that it's http */
1772 if (!wcsncmp( envproxy
, L
"http://", 7 )) http_proxy
= envproxy
+ 7;
1773 else WARN("unsupported scheme in $http_proxy: %s\n", debugstr_w(envproxy
));
1775 else http_proxy
= envproxy
;
1778 if (http_proxy
&& http_proxy
[0])
1781 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1782 info
->lpszProxy
= GlobalAlloc( 0, (lstrlenW(http_proxy
) + 1) * sizeof(WCHAR
) );
1783 wcscpy( info
->lpszProxy
, http_proxy
);
1784 info
->lpszProxyBypass
= NULL
;
1785 TRACE("http proxy (from environment) = %s\n", debugstr_w(info
->lpszProxy
));
1790 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1791 info
->lpszProxy
= NULL
;
1792 info
->lpszProxyBypass
= NULL
;
1794 SetLastError( ERROR_SUCCESS
);
1798 /***********************************************************************
1799 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1801 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1804 struct connection_settings_header
*hdr
= NULL
;
1805 DWORD type
, offset
, len
, size
= 0;
1808 TRACE("%p\n", config
);
1812 SetLastError( ERROR_INVALID_PARAMETER
);
1815 memset( config
, 0, sizeof(*config
) );
1816 config
->fAutoDetect
= TRUE
;
1818 if (RegOpenKeyExW( HKEY_CURRENT_USER
, path_connections
, 0, KEY_READ
, &hkey
) ||
1819 RegQueryValueExW( hkey
, L
"DefaultConnectionSettings", NULL
, &type
, NULL
, &size
) ||
1820 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1825 if (!(hdr
= malloc( size
))) goto done
;
1826 if (RegQueryValueExW( hkey
, L
"DefaultConnectionSettings", NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1827 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1833 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1834 offset
= sizeof(*hdr
);
1835 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1836 len
= *(DWORD
*)((char *)hdr
+ offset
);
1837 offset
+= sizeof(DWORD
);
1838 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1840 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1841 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1844 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1845 len
= *(DWORD
*)((char *)hdr
+ offset
);
1846 offset
+= sizeof(DWORD
);
1847 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1849 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1850 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1853 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1854 len
= *(DWORD
*)((char *)hdr
+ offset
);
1855 offset
+= sizeof(DWORD
);
1856 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1858 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1859 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1864 RegCloseKey( hkey
);
1868 GlobalFree( config
->lpszAutoConfigUrl
);
1869 config
->lpszAutoConfigUrl
= NULL
;
1870 GlobalFree( config
->lpszProxy
);
1871 config
->lpszProxy
= NULL
;
1872 GlobalFree( config
->lpszProxyBypass
);
1873 config
->lpszProxyBypass
= NULL
;
1875 else SetLastError( ERROR_SUCCESS
);
1879 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1885 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1886 info
->lpszProxy
= NULL
;
1887 info
->lpszProxyBypass
= NULL
;
1889 TRACE("%s\n", debugstr_a( result
));
1892 while (*p
== ' ') p
++;
1894 if (len
>= 5 && !_strnicmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1897 while (*p
== ' ') p
++;
1898 if (!*p
|| *p
== ';') return TRUE
;
1899 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1900 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1903 if (*q
== ' ' || *q
== ';')
1913 static SRWLOCK cache_lock
= SRWLOCK_INIT
;
1914 static DWORD cached_script_size
;
1915 static ULONGLONG cache_update_time
;
1916 static char *cached_script
;
1917 static WCHAR
*cached_url
;
1919 static BOOL
get_cached_script( const WCHAR
*url
, char **buffer
, DWORD
*out_size
)
1926 AcquireSRWLockExclusive( &cache_lock
);
1927 if (cached_url
&& !wcscmp( cached_url
, url
) && GetTickCount64() - cache_update_time
< 60000)
1930 if (cached_script
&& (*buffer
= malloc( cached_script_size
)))
1932 memcpy( *buffer
, cached_script
, cached_script_size
);
1933 *out_size
= cached_script_size
;
1936 ReleaseSRWLockExclusive( &cache_lock
);
1940 static void cache_script( const WCHAR
*url
, char *buffer
, DWORD size
)
1942 AcquireSRWLockExclusive( &cache_lock
);
1944 free( cached_script
);
1945 cached_script_size
= 0;
1946 cached_script
= NULL
;
1948 if ((cached_url
= wcsdup( url
)) && buffer
&& (cached_script
= malloc( size
)))
1950 memcpy( cached_script
, buffer
, size
);
1951 cached_script_size
= size
;
1953 cache_update_time
= GetTickCount64();
1954 ReleaseSRWLockExclusive( &cache_lock
);
1957 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1959 static const WCHAR
*acceptW
[] = {L
"*/*", NULL
};
1960 HINTERNET ses
, con
= NULL
, req
= NULL
;
1963 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1966 if (get_cached_script( url
, &buffer
, out_size
))
1968 TRACE( "Returning cached result.\n" );
1969 if (!buffer
) SetLastError( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1973 memset( &uc
, 0, sizeof(uc
) );
1974 uc
.dwStructSize
= sizeof(uc
);
1975 uc
.dwHostNameLength
= -1;
1976 uc
.dwUrlPathLength
= -1;
1977 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1978 if (!(hostname
= malloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1979 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1980 hostname
[uc
.dwHostNameLength
] = 0;
1982 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1983 WinHttpSetTimeouts( ses
, 5000, 60000, 30000, 30000 );
1984 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1985 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1986 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1987 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1989 if (!WinHttpReceiveResponse( req
, 0 )) goto done
;
1990 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1991 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1994 if (!(buffer
= malloc( size
))) goto done
;
1999 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
2000 if (!bytes_read
) break;
2001 to_read
-= bytes_read
;
2002 offset
+= bytes_read
;
2003 *out_size
+= bytes_read
;
2008 if (!(tmp
= realloc( buffer
, size
))) goto done
;
2014 WinHttpCloseHandle( req
);
2015 WinHttpCloseHandle( con
);
2016 WinHttpCloseHandle( ses
);
2018 cache_script( url
, buffer
, *out_size
);
2019 if (!buffer
) SetLastError( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
2023 struct AUTO_PROXY_SCRIPT_BUFFER
2026 LPSTR lpszScriptBuffer
;
2027 DWORD dwScriptBufferSize
;
2030 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
2031 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
2032 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
2034 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
2037 char *result
, *urlA
;
2039 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
2042 buffer
.dwStructSize
= sizeof(buffer
);
2043 buffer
.lpszScriptBuffer
= script
;
2044 buffer
.dwScriptBufferSize
= size
;
2046 if (!(urlA
= strdupWA( url
))) return FALSE
;
2047 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
2053 memset( &uc
, 0, sizeof(uc
) );
2054 uc
.dwStructSize
= sizeof(uc
);
2055 uc
.dwHostNameLength
= -1;
2057 if ((ret
= WinHttpCrackUrl( url
, 0, 0, &uc
)))
2059 char *hostnameA
= strdupWA_sized( uc
.lpszHostName
, uc
.dwHostNameLength
);
2061 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
),
2062 hostnameA
, strlen(hostnameA
), &result
, &len_result
)))
2064 ret
= parse_script_result( result
, info
);
2071 InternetDeInitializeAutoProxyDll( NULL
, 0 );
2075 /***********************************************************************
2076 * WinHttpGetProxyForUrl (winhttp.@)
2078 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2079 WINHTTP_PROXY_INFO
*info
)
2081 WCHAR
*detected_pac_url
= NULL
;
2082 const WCHAR
*pac_url
;
2083 struct session
*session
;
2088 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
2090 if (!(session
= (struct session
*)grab_object( hsession
)))
2092 SetLastError( ERROR_INVALID_HANDLE
);
2095 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
2097 release_object( &session
->hdr
);
2098 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2101 if (!url
|| !options
|| !info
||
2102 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2103 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
2104 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
2105 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2106 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
&& !options
->lpszAutoConfigUrl
))
2108 release_object( &session
->hdr
);
2109 SetLastError( ERROR_INVALID_PARAMETER
);
2112 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
2113 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
2116 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
2117 else pac_url
= detected_pac_url
;
2119 if ((script
= download_script( pac_url
, &size
)))
2121 ret
= run_script( script
, size
, url
, info
);
2126 GlobalFree( detected_pac_url
);
2127 release_object( &session
->hdr
);
2128 if (ret
) SetLastError( ERROR_SUCCESS
);
2132 /***********************************************************************
2133 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2135 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
2142 TRACE("%p\n", info
);
2146 SetLastError( ERROR_INVALID_PARAMETER
);
2149 switch (info
->dwAccessType
)
2151 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
2153 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
2154 if (!info
->lpszProxy
)
2156 SetLastError( ERROR_INVALID_PARAMETER
);
2159 /* Only ASCII characters are allowed */
2160 for (src
= info
->lpszProxy
; *src
; src
++)
2163 SetLastError( ERROR_INVALID_PARAMETER
);
2166 if (info
->lpszProxyBypass
)
2168 for (src
= info
->lpszProxyBypass
; *src
; src
++)
2171 SetLastError( ERROR_INVALID_PARAMETER
);
2177 SetLastError( ERROR_INVALID_PARAMETER
);
2181 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, path_connections
, 0, NULL
, 0,
2182 KEY_WRITE
, NULL
, &key
, NULL
);
2185 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
2188 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2190 size
+= lstrlenW( info
->lpszProxy
);
2191 if (info
->lpszProxyBypass
)
2192 size
+= lstrlenW( info
->lpszProxyBypass
);
2194 if ((buf
= malloc( size
)))
2196 struct connection_settings_header
*hdr
=
2197 (struct connection_settings_header
*)buf
;
2198 DWORD
*len
= (DWORD
*)(hdr
+ 1);
2200 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
2202 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2206 hdr
->flags
= PROXY_TYPE_PROXY
;
2207 *len
++ = lstrlenW( info
->lpszProxy
);
2208 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
2212 if (info
->lpszProxyBypass
)
2214 *len
++ = lstrlenW( info
->lpszProxyBypass
);
2215 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
2224 hdr
->flags
= PROXY_TYPE_DIRECT
;
2228 l
= RegSetValueExW( key
, L
"WinHttpSettings", 0, REG_BINARY
, buf
, size
);
2235 if (ret
) SetLastError( ERROR_SUCCESS
);
2239 /***********************************************************************
2240 * WinHttpCreateProxyResolver (winhttp.@)
2242 DWORD WINAPI
WinHttpCreateProxyResolver( HINTERNET hsession
, HINTERNET
*hresolver
)
2244 FIXME("%p, %p\n", hsession
, hresolver
);
2245 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2248 /***********************************************************************
2249 * WinHttpFreeProxyResult (winhttp.@)
2251 void WINAPI
WinHttpFreeProxyResult( WINHTTP_PROXY_RESULT
*result
)
2253 FIXME("%p\n", result
);
2256 /***********************************************************************
2257 * WinHttpFreeProxyResultEx (winhttp.@)
2259 void WINAPI
WinHttpFreeProxyResultEx( WINHTTP_PROXY_RESULT_EX
*result
)
2261 FIXME("%p\n", result
);
2264 /***********************************************************************
2265 * WinHttpFreeProxySettings (winhttp.@)
2267 void WINAPI
WinHttpFreeProxySettings( WINHTTP_PROXY_SETTINGS
*settings
)
2269 FIXME("%p\n", settings
);
2272 /***********************************************************************
2273 * WinHttpGetProxyForUrlEx (winhttp.@)
2275 DWORD WINAPI
WinHttpGetProxyForUrlEx( HINTERNET hresolver
, const WCHAR
*url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2278 FIXME( "%p, %s, %p, %Ix\n", hresolver
, debugstr_w(url
), options
, ctx
);
2279 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2282 /***********************************************************************
2283 * WinHttpGetProxyForUrlEx2 (winhttp.@)
2285 DWORD WINAPI
WinHttpGetProxyForUrlEx2( HINTERNET hresolver
, const WCHAR
*url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2286 DWORD selection_len
, BYTE
*selection
, DWORD_PTR ctx
)
2288 FIXME( "%p, %s, %p, %lu, %p, %Ix\n", hresolver
, debugstr_w(url
), options
, selection_len
, selection
, ctx
);
2289 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2292 /***********************************************************************
2293 * WinHttpGetProxyResult (winhttp.@)
2295 DWORD WINAPI
WinHttpGetProxyResult( HINTERNET hresolver
, WINHTTP_PROXY_RESULT
*result
)
2297 FIXME("%p, %p\n", hresolver
, result
);
2298 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2301 /***********************************************************************
2302 * WinHttpGetProxyResultEx (winhttp.@)
2304 DWORD WINAPI
WinHttpGetProxyResultEx( HINTERNET hresolver
, WINHTTP_PROXY_RESULT_EX
*result
)
2306 FIXME("%p, %p\n", hresolver
, result
);
2307 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2310 /***********************************************************************
2311 * WinHttpGetProxySettingsVersion (winhttp.@)
2313 DWORD WINAPI
WinHttpGetProxySettingsVersion( HINTERNET hsession
, DWORD
*version
)
2315 FIXME("%p, %p\n", hsession
, version
);
2316 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2319 /***********************************************************************
2320 * WinHttpReadProxySettings (winhttp.@)
2322 DWORD WINAPI
WinHttpReadProxySettings( HINTERNET hsession
, const WCHAR
*connection
, BOOL use_defaults
,
2323 BOOL set_autodiscover
, DWORD
*version
, BOOL
*defaults_returned
,
2324 WINHTTP_PROXY_SETTINGS
*settings
)
2326 FIXME("%p, %s, %d, %d, %p, %p, %p\n", hsession
, debugstr_w(connection
), use_defaults
, set_autodiscover
,
2327 version
, defaults_returned
, settings
);
2328 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2331 /***********************************************************************
2332 * WinHttpResetAutoProxy (winhttp.@)
2334 DWORD WINAPI
WinHttpResetAutoProxy( HINTERNET hsession
, DWORD flags
)
2336 FIXME( "%p, %#lx\n", hsession
, flags
);
2337 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2340 DWORD WINAPI
WinHttpWriteProxySettings( HINTERNET hsession
, BOOL force
, WINHTTP_PROXY_SETTINGS
*settings
)
2342 FIXME("%p, %d, %p\n", hsession
, force
, settings
);
2343 return ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
;
2346 /***********************************************************************
2347 * WinHttpSetStatusCallback (winhttp.@)
2349 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
2350 DWORD flags
, DWORD_PTR reserved
)
2352 struct object_header
*hdr
;
2353 WINHTTP_STATUS_CALLBACK ret
;
2355 TRACE( "%p, %p, %#lx, %Ix\n", handle
, callback
, flags
, reserved
);
2357 if (!(hdr
= grab_object( handle
)))
2359 SetLastError( ERROR_INVALID_HANDLE
);
2360 return WINHTTP_INVALID_STATUS_CALLBACK
;
2362 ret
= hdr
->callback
;
2363 hdr
->callback
= callback
;
2364 hdr
->notify_mask
= flags
;
2366 release_object( hdr
);
2367 SetLastError( ERROR_SUCCESS
);
2371 /***********************************************************************
2372 * WinHttpSetTimeouts (winhttp.@)
2374 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2377 struct object_header
*hdr
;
2379 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2381 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2383 SetLastError( ERROR_INVALID_PARAMETER
);
2387 if (!(hdr
= grab_object( handle
)))
2389 SetLastError( ERROR_INVALID_HANDLE
);
2395 case WINHTTP_HANDLE_TYPE_REQUEST
:
2397 struct request
*request
= (struct request
*)hdr
;
2398 request
->connect_timeout
= connect
;
2400 if (resolve
< 0) resolve
= 0;
2401 request
->resolve_timeout
= resolve
;
2403 if (send
< 0) send
= 0;
2404 request
->send_timeout
= send
;
2406 if (receive
< 0) receive
= 0;
2407 request
->receive_timeout
= receive
;
2409 if (request
->netconn
)
2411 if (netconn_set_timeout( request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2412 if (netconn_set_timeout( request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2416 case WINHTTP_HANDLE_TYPE_SESSION
:
2418 struct session
*session
= (struct session
*)hdr
;
2419 session
->connect_timeout
= connect
;
2421 if (resolve
< 0) resolve
= 0;
2422 session
->resolve_timeout
= resolve
;
2424 if (send
< 0) send
= 0;
2425 session
->send_timeout
= send
;
2427 if (receive
< 0) receive
= 0;
2428 session
->receive_timeout
= receive
;
2432 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2435 release_object( hdr
);
2436 if (ret
) SetLastError( ERROR_SUCCESS
);
2440 static const WCHAR wkday
[7][4] =
2441 {L
"Sun", L
"Mon", L
"Tue", L
"Wed", L
"Thu", L
"Fri", L
"Sat"};
2442 static const WCHAR month
[12][4] =
2443 {L
"Jan", L
"Feb", L
"Mar", L
"Apr", L
"May", L
"Jun", L
"Jul", L
"Aug", L
"Sep", L
"Oct", L
"Nov", L
"Dec"};
2445 /***********************************************************************
2446 * WinHttpTimeFromSystemTime (WININET.@)
2448 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2450 TRACE("%p, %p\n", time
, string
);
2452 if (!time
|| !string
)
2454 SetLastError( ERROR_INVALID_PARAMETER
);
2458 swprintf( string
, WINHTTP_TIME_FORMAT_BUFSIZE
/ sizeof(WCHAR
),
2459 L
"%s, %02d %s %4d %02d:%02d:%02d GMT",
2460 wkday
[time
->wDayOfWeek
],
2462 month
[time
->wMonth
- 1],
2468 SetLastError( ERROR_SUCCESS
);
2472 /***********************************************************************
2473 * WinHttpTimeToSystemTime (WININET.@)
2475 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2478 const WCHAR
*s
= string
;
2481 TRACE("%s, %p\n", debugstr_w(string
), time
);
2483 if (!string
|| !time
)
2485 SetLastError( ERROR_INVALID_PARAMETER
);
2489 /* Windows does this too */
2490 GetSystemTime( time
);
2492 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2493 * a SYSTEMTIME structure.
2496 SetLastError( ERROR_SUCCESS
);
2498 while (*s
&& !iswalpha( *s
)) s
++;
2499 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2500 time
->wDayOfWeek
= 7;
2502 for (i
= 0; i
< 7; i
++)
2504 if (towupper( wkday
[i
][0] ) == towupper( s
[0] ) &&
2505 towupper( wkday
[i
][1] ) == towupper( s
[1] ) &&
2506 towupper( wkday
[i
][2] ) == towupper( s
[2] ) )
2508 time
->wDayOfWeek
= i
;
2513 if (time
->wDayOfWeek
> 6) return TRUE
;
2514 while (*s
&& !iswdigit( *s
)) s
++;
2515 time
->wDay
= wcstol( s
, &end
, 10 );
2518 while (*s
&& !iswalpha( *s
)) s
++;
2519 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2522 for (i
= 0; i
< 12; i
++)
2524 if (towupper( month
[i
][0]) == towupper( s
[0] ) &&
2525 towupper( month
[i
][1]) == towupper( s
[1] ) &&
2526 towupper( month
[i
][2]) == towupper( s
[2] ) )
2528 time
->wMonth
= i
+ 1;
2532 if (time
->wMonth
== 0) return TRUE
;
2534 while (*s
&& !iswdigit( *s
)) s
++;
2535 if (*s
== '\0') return TRUE
;
2536 time
->wYear
= wcstol( s
, &end
, 10 );
2539 while (*s
&& !iswdigit( *s
)) s
++;
2540 if (*s
== '\0') return TRUE
;
2541 time
->wHour
= wcstol( s
, &end
, 10 );
2544 while (*s
&& !iswdigit( *s
)) s
++;
2545 if (*s
== '\0') return TRUE
;
2546 time
->wMinute
= wcstol( s
, &end
, 10 );
2549 while (*s
&& !iswdigit( *s
)) s
++;
2550 if (*s
== '\0') return TRUE
;
2551 time
->wSecond
= wcstol( s
, &end
, 10 );
2553 time
->wMilliseconds
= 0;