2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
26 #ifdef HAVE_CORESERVICES_CORESERVICES_H
27 #define GetCurrentThread MacGetCurrentThread
28 #define LoadResource MacLoadResource
29 #include <CoreServices/CoreServices.h>
30 #undef GetCurrentThread
50 #include "winhttp_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
54 #define DEFAULT_RESOLVE_TIMEOUT 0
55 #define DEFAULT_CONNECT_TIMEOUT 20000
56 #define DEFAULT_SEND_TIMEOUT 30000
57 #define DEFAULT_RECEIVE_TIMEOUT 30000
59 void set_last_error( DWORD error
)
62 SetLastError( error
);
65 DWORD
get_last_error( void )
68 return GetLastError();
71 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
73 if (hdr
->callback
&& (hdr
->notify_mask
& status
))
75 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
76 hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
77 TRACE("returning from 0x%08x callback\n", status
);
81 /***********************************************************************
82 * WinHttpCheckPlatform (winhttp.@)
84 BOOL WINAPI
WinHttpCheckPlatform( void )
90 /***********************************************************************
91 * session_destroy (internal)
93 static void session_destroy( object_header_t
*hdr
)
95 session_t
*session
= (session_t
*)hdr
;
96 struct list
*item
, *next
;
99 TRACE("%p\n", session
);
101 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
103 domain
= LIST_ENTRY( item
, domain_t
, entry
);
104 delete_domain( domain
);
106 heap_free( session
->agent
);
107 heap_free( session
->proxy_server
);
108 heap_free( session
->proxy_bypass
);
109 heap_free( session
->proxy_username
);
110 heap_free( session
->proxy_password
);
111 heap_free( session
);
114 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
116 session_t
*session
= (session_t
*)hdr
;
120 case WINHTTP_OPTION_REDIRECT_POLICY
:
122 if (!buffer
|| *buflen
< sizeof(DWORD
))
124 *buflen
= sizeof(DWORD
);
125 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
129 *(DWORD
*)buffer
= hdr
->redirect_policy
;
130 *buflen
= sizeof(DWORD
);
133 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
134 *(DWORD
*)buffer
= session
->resolve_timeout
;
135 *buflen
= sizeof(DWORD
);
137 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
138 *(DWORD
*)buffer
= session
->connect_timeout
;
139 *buflen
= sizeof(DWORD
);
141 case WINHTTP_OPTION_SEND_TIMEOUT
:
142 *(DWORD
*)buffer
= session
->send_timeout
;
143 *buflen
= sizeof(DWORD
);
145 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
146 *(DWORD
*)buffer
= session
->recv_timeout
;
147 *buflen
= sizeof(DWORD
);
150 FIXME("unimplemented option %u\n", option
);
151 set_last_error( ERROR_INVALID_PARAMETER
);
156 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
158 session_t
*session
= (session_t
*)hdr
;
162 case WINHTTP_OPTION_PROXY
:
164 WINHTTP_PROXY_INFO
*pi
= buffer
;
166 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
169 case WINHTTP_OPTION_REDIRECT_POLICY
:
173 if (buflen
!= sizeof(policy
))
175 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
179 policy
= *(DWORD
*)buffer
;
180 TRACE("0x%x\n", policy
);
181 hdr
->redirect_policy
= policy
;
184 case WINHTTP_OPTION_DISABLE_FEATURE
:
185 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
187 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
188 session
->resolve_timeout
= *(DWORD
*)buffer
;
190 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
191 session
->connect_timeout
= *(DWORD
*)buffer
;
193 case WINHTTP_OPTION_SEND_TIMEOUT
:
194 session
->send_timeout
= *(DWORD
*)buffer
;
196 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
197 session
->recv_timeout
= *(DWORD
*)buffer
;
199 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
200 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
203 FIXME("unimplemented option %u\n", option
);
204 set_last_error( ERROR_INVALID_PARAMETER
);
209 static const object_vtbl_t session_vtbl
=
212 session_query_option
,
216 /***********************************************************************
217 * WinHttpOpen (winhttp.@)
219 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
222 HINTERNET handle
= NULL
;
224 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
226 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
228 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
229 session
->hdr
.vtbl
= &session_vtbl
;
230 session
->hdr
.flags
= flags
;
231 session
->hdr
.refs
= 1;
232 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
233 list_init( &session
->hdr
.children
);
234 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
235 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
236 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
237 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
238 list_init( &session
->cookie_cache
);
240 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
241 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
243 WINHTTP_PROXY_INFO info
;
245 WinHttpGetDefaultProxyConfiguration( &info
);
246 session
->access
= info
.dwAccessType
;
247 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
249 GlobalFree( info
.lpszProxy
);
250 GlobalFree( info
.lpszProxyBypass
);
253 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
255 GlobalFree( info
.lpszProxy
);
256 GlobalFree( info
.lpszProxyBypass
);
260 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
262 session
->access
= access
;
263 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
264 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
267 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
268 session
->hdr
.handle
= handle
;
271 release_object( &session
->hdr
);
272 TRACE("returning %p\n", handle
);
276 /***********************************************************************
277 * connect_destroy (internal)
279 static void connect_destroy( object_header_t
*hdr
)
281 connect_t
*connect
= (connect_t
*)hdr
;
283 TRACE("%p\n", connect
);
285 release_object( &connect
->session
->hdr
);
287 heap_free( connect
->hostname
);
288 heap_free( connect
->servername
);
289 heap_free( connect
->username
);
290 heap_free( connect
->password
);
291 heap_free( connect
);
294 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
296 connect_t
*connect
= (connect_t
*)hdr
;
300 case WINHTTP_OPTION_PARENT_HANDLE
:
302 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
304 *buflen
= sizeof(HINTERNET
);
305 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
309 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
310 *buflen
= sizeof(HINTERNET
);
313 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
314 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
315 *buflen
= sizeof(DWORD
);
317 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
318 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
319 *buflen
= sizeof(DWORD
);
321 case WINHTTP_OPTION_SEND_TIMEOUT
:
322 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
323 *buflen
= sizeof(DWORD
);
325 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
326 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
327 *buflen
= sizeof(DWORD
);
330 FIXME("unimplemented option %u\n", option
);
331 set_last_error( ERROR_INVALID_PARAMETER
);
336 static const object_vtbl_t connect_vtbl
=
339 connect_query_option
,
343 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
345 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
348 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
350 else if (*domain
== '*')
352 if (domain
[1] == '.')
356 /* For a hostname to match a wildcard, the last domain must match
357 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
358 * hostname is www.foo.a.b, it matches, but a.b does not.
360 dot
= strchrW( server
, '.' );
363 int len
= strlenW( dot
+ 1 );
365 if (len
> strlenW( domain
+ 2 ))
369 /* The server's domain is longer than the wildcard, so it
370 * could be a subdomain. Compare the last portion of the
373 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
374 if (!strcmpiW( ptr
, domain
+ 2 ))
376 /* This is only a match if the preceding character is
377 * a '.', i.e. that it is a matching domain. E.g.
378 * if domain is '*.b.c' and server is 'www.ab.c' they
381 ret
= *(ptr
- 1) == '.';
385 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
390 ret
= !strcmpiW( server
, domain
);
394 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
395 #define MAX_HOST_NAME_LENGTH 256
397 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
402 if (!session
->proxy_bypass
) return FALSE
;
403 ptr
= session
->proxy_bypass
;
407 ptr
= strchrW( ptr
, ';' );
409 ptr
= strchrW( tmp
, ' ' );
412 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
414 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
416 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
417 domain
[ptr
- tmp
] = 0;
418 ret
= domain_matches( server
, domain
);
423 ret
= domain_matches( server
, tmp
);
424 } while (ptr
&& !ret
);
428 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
430 session_t
*session
= connect
->session
;
433 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
437 if ((colon
= strchrW( session
->proxy_server
, ':' )))
439 if (!connect
->servername
|| strncmpiW( connect
->servername
,
440 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
442 heap_free( connect
->servername
);
443 connect
->resolved
= FALSE
;
444 if (!(connect
->servername
= heap_alloc(
445 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
450 memcpy( connect
->servername
, session
->proxy_server
,
451 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
452 connect
->servername
[colon
- session
->proxy_server
] = 0;
454 connect
->serverport
= atoiW( colon
+ 1 );
456 connect
->serverport
= INTERNET_DEFAULT_PORT
;
461 if (!connect
->servername
|| strcmpiW( connect
->servername
,
462 session
->proxy_server
))
464 heap_free( connect
->servername
);
465 connect
->resolved
= FALSE
;
466 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
471 connect
->serverport
= INTERNET_DEFAULT_PORT
;
477 heap_free( connect
->servername
);
478 connect
->resolved
= FALSE
;
479 if (!(connect
->servername
= strdupW( server
)))
484 connect
->serverport
= port
;
490 /***********************************************************************
491 * WinHttpConnect (winhttp.@)
493 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
497 HINTERNET hconnect
= NULL
;
499 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
503 set_last_error( ERROR_INVALID_PARAMETER
);
506 if (!(session
= (session_t
*)grab_object( hsession
)))
508 set_last_error( ERROR_INVALID_HANDLE
);
511 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
513 release_object( &session
->hdr
);
514 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
517 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
519 release_object( &session
->hdr
);
522 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
523 connect
->hdr
.vtbl
= &connect_vtbl
;
524 connect
->hdr
.refs
= 1;
525 connect
->hdr
.flags
= session
->hdr
.flags
;
526 connect
->hdr
.callback
= session
->hdr
.callback
;
527 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
528 connect
->hdr
.context
= session
->hdr
.context
;
529 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
530 list_init( &connect
->hdr
.children
);
532 addref_object( &session
->hdr
);
533 connect
->session
= session
;
534 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
536 if (!(connect
->hostname
= strdupW( server
))) goto end
;
537 connect
->hostport
= port
;
538 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
540 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
541 connect
->hdr
.handle
= hconnect
;
543 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
546 release_object( &connect
->hdr
);
547 release_object( &session
->hdr
);
548 TRACE("returning %p\n", hconnect
);
552 /***********************************************************************
553 * request_destroy (internal)
555 static void request_destroy( object_header_t
*hdr
)
557 request_t
*request
= (request_t
*)hdr
;
560 TRACE("%p\n", request
);
562 if (request
->task_thread
)
564 SetEvent( request
->task_cancel
);
565 WaitForSingleObject( request
->task_thread
, INFINITE
);
566 CloseHandle( request
->task_thread
);
567 CloseHandle( request
->task_cancel
);
568 CloseHandle( request
->task_wait
);
569 request
->task_cs
.DebugInfo
->Spare
[0] = 0;
570 DeleteCriticalSection( &request
->task_cs
);
572 release_object( &request
->connect
->hdr
);
574 destroy_authinfo( request
->authinfo
);
575 destroy_authinfo( request
->proxy_authinfo
);
577 heap_free( request
->verb
);
578 heap_free( request
->path
);
579 heap_free( request
->version
);
580 heap_free( request
->raw_headers
);
581 heap_free( request
->status_text
);
582 for (i
= 0; i
< request
->num_headers
; i
++)
584 heap_free( request
->headers
[i
].field
);
585 heap_free( request
->headers
[i
].value
);
587 heap_free( request
->headers
);
588 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
589 heap_free( request
->accept_types
);
590 heap_free( request
);
593 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
596 if (str
) len
= strlenW( str
);
597 if (buffer
&& *buflen
> len
)
599 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
602 *buflen
= len
* sizeof(WCHAR
);
605 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
608 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
610 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
611 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
612 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
617 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
620 switch (addr
->sa_family
)
624 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
625 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
628 addr_win
->sin_family
= WS_AF_INET
;
629 addr_win
->sin_port
= addr_unix
->sin_port
;
630 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
631 p
= (char *)&addr_win
->sin_addr
+ 4;
632 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
637 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
638 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
640 addr_win
->sin6_family
= WS_AF_INET6
;
641 addr_win
->sin6_port
= addr_unix
->sin6_port
;
642 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
643 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
644 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
645 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
647 addr_win
->sin6_scope_id
= 0;
649 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
653 ERR("unhandled family %u\n", addr
->sa_family
);
657 switch (addr
->sa_family
)
661 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
663 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
664 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
669 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
671 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
672 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
676 ERR("unhandled family %u\n", addr
->sa_family
);
682 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
684 request_t
*request
= (request_t
*)hdr
;
688 case WINHTTP_OPTION_SECURITY_FLAGS
:
693 if (!buffer
|| *buflen
< sizeof(flags
))
695 *buflen
= sizeof(flags
);
696 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
701 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
702 flags
|= request
->netconn
.security_flags
;
703 bits
= netconn_get_cipher_strength( &request
->netconn
);
705 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
707 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
709 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
710 *(DWORD
*)buffer
= flags
;
711 *buflen
= sizeof(flags
);
714 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
716 const CERT_CONTEXT
*cert
;
718 if (!buffer
|| *buflen
< sizeof(cert
))
720 *buflen
= sizeof(cert
);
721 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
725 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
726 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
727 *buflen
= sizeof(cert
);
730 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
732 const CERT_CONTEXT
*cert
;
733 const CRYPT_OID_INFO
*oidInfo
;
734 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
736 FIXME("partial stub\n");
738 if (!buffer
|| *buflen
< sizeof(*ci
))
740 *buflen
= sizeof(*ci
);
741 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
744 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
746 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
747 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
748 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
749 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
750 ci
->lpszProtocolName
= NULL
;
751 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
752 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
755 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
757 ci
->lpszSignatureAlgName
= NULL
;
758 ci
->lpszEncryptionAlgName
= NULL
;
759 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
761 CertFreeCertificateContext( cert
);
762 *buflen
= sizeof(*ci
);
765 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
767 if (!buffer
|| *buflen
< sizeof(DWORD
))
769 *buflen
= sizeof(DWORD
);
770 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
774 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
775 *buflen
= sizeof(DWORD
);
778 case WINHTTP_OPTION_CONNECTION_INFO
:
780 WINHTTP_CONNECTION_INFO
*info
= buffer
;
781 struct sockaddr local
;
782 socklen_t len
= sizeof(local
);
783 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
785 if (!buffer
|| *buflen
< sizeof(*info
))
787 *buflen
= sizeof(*info
);
788 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
791 if (!netconn_connected( &request
->netconn
))
793 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
796 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
797 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
798 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
799 info
->cbSize
= sizeof(*info
);
802 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
803 *(DWORD
*)buffer
= request
->resolve_timeout
;
804 *buflen
= sizeof(DWORD
);
806 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
807 *(DWORD
*)buffer
= request
->connect_timeout
;
808 *buflen
= sizeof(DWORD
);
810 case WINHTTP_OPTION_SEND_TIMEOUT
:
811 *(DWORD
*)buffer
= request
->send_timeout
;
812 *buflen
= sizeof(DWORD
);
814 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
815 *(DWORD
*)buffer
= request
->recv_timeout
;
816 *buflen
= sizeof(DWORD
);
819 case WINHTTP_OPTION_USERNAME
:
820 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
823 case WINHTTP_OPTION_PASSWORD
:
824 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
827 case WINHTTP_OPTION_PROXY_USERNAME
:
828 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
831 case WINHTTP_OPTION_PROXY_PASSWORD
:
832 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
836 FIXME("unimplemented option %u\n", option
);
837 set_last_error( ERROR_INVALID_PARAMETER
);
842 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
845 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
847 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
851 set_last_error( ERROR_OUTOFMEMORY
);
855 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
857 request_t
*request
= (request_t
*)hdr
;
861 case WINHTTP_OPTION_PROXY
:
863 WINHTTP_PROXY_INFO
*pi
= buffer
;
865 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
868 case WINHTTP_OPTION_DISABLE_FEATURE
:
872 if (buflen
!= sizeof(DWORD
))
874 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
878 disable
= *(DWORD
*)buffer
;
879 TRACE("0x%x\n", disable
);
880 hdr
->disable_flags
|= disable
;
883 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
887 if (buflen
!= sizeof(DWORD
))
889 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
893 policy
= *(DWORD
*)buffer
;
894 TRACE("0x%x\n", policy
);
895 hdr
->logon_policy
= policy
;
898 case WINHTTP_OPTION_REDIRECT_POLICY
:
902 if (buflen
!= sizeof(DWORD
))
904 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
908 policy
= *(DWORD
*)buffer
;
909 TRACE("0x%x\n", policy
);
910 hdr
->redirect_policy
= policy
;
913 case WINHTTP_OPTION_SECURITY_FLAGS
:
917 if (buflen
< sizeof(DWORD
))
919 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
922 flags
= *(DWORD
*)buffer
;
923 TRACE("0x%x\n", flags
);
924 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
925 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
926 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
927 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
929 set_last_error( ERROR_INVALID_PARAMETER
);
932 request
->netconn
.security_flags
= flags
;
935 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
936 request
->resolve_timeout
= *(DWORD
*)buffer
;
938 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
939 request
->connect_timeout
= *(DWORD
*)buffer
;
941 case WINHTTP_OPTION_SEND_TIMEOUT
:
942 request
->send_timeout
= *(DWORD
*)buffer
;
944 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
945 request
->recv_timeout
= *(DWORD
*)buffer
;
948 case WINHTTP_OPTION_USERNAME
:
950 connect_t
*connect
= request
->connect
;
952 heap_free( connect
->username
);
953 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
956 case WINHTTP_OPTION_PASSWORD
:
958 connect_t
*connect
= request
->connect
;
960 heap_free( connect
->password
);
961 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
964 case WINHTTP_OPTION_PROXY_USERNAME
:
966 session_t
*session
= request
->connect
->session
;
968 heap_free( session
->proxy_username
);
969 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
972 case WINHTTP_OPTION_PROXY_PASSWORD
:
974 session_t
*session
= request
->connect
->session
;
976 heap_free( session
->proxy_password
);
977 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
981 FIXME("unimplemented option %u\n", option
);
982 set_last_error( ERROR_INVALID_PARAMETER
);
987 static const object_vtbl_t request_vtbl
=
990 request_query_option
,
994 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
996 const WCHAR
**types
= accept_types
;
999 if (!types
) return TRUE
;
1002 request
->num_accept_types
++;
1005 if (!request
->num_accept_types
) return TRUE
;
1006 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
1008 request
->num_accept_types
= 0;
1011 types
= accept_types
;
1012 for (i
= 0; i
< request
->num_accept_types
; i
++)
1014 if (!(request
->accept_types
[i
] = strdupW( *types
)))
1016 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
1017 heap_free( request
->accept_types
);
1018 request
->accept_types
= NULL
;
1019 request
->num_accept_types
= 0;
1027 /***********************************************************************
1028 * WinHttpOpenRequest (winhttp.@)
1030 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1031 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1035 HINTERNET hrequest
= NULL
;
1037 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1038 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1040 if(types
&& TRACE_ON(winhttp
)) {
1043 TRACE("accept types:\n");
1044 for(iter
= types
; *iter
; iter
++)
1045 TRACE(" %s\n", debugstr_w(*iter
));
1048 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1050 set_last_error( ERROR_INVALID_HANDLE
);
1053 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1055 release_object( &connect
->hdr
);
1056 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1059 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1061 release_object( &connect
->hdr
);
1064 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1065 request
->hdr
.vtbl
= &request_vtbl
;
1066 request
->hdr
.refs
= 1;
1067 request
->hdr
.flags
= flags
;
1068 request
->hdr
.callback
= connect
->hdr
.callback
;
1069 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1070 request
->hdr
.context
= connect
->hdr
.context
;
1071 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1072 list_init( &request
->hdr
.children
);
1073 list_init( &request
->task_queue
);
1075 addref_object( &connect
->hdr
);
1076 request
->connect
= connect
;
1077 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1079 if (!netconn_init( &request
->netconn
)) goto end
;
1080 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1081 request
->connect_timeout
= connect
->session
->connect_timeout
;
1082 request
->send_timeout
= connect
->session
->send_timeout
;
1083 request
->recv_timeout
= connect
->session
->recv_timeout
;
1085 if (!verb
|| !verb
[0]) verb
= getW
;
1086 if (!(request
->verb
= strdupW( verb
))) goto end
;
1093 len
= strlenW( object
) + 1;
1094 if (object
[0] != '/') len
++;
1095 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1097 if (object
[0] != '/') *p
++ = '/';
1098 strcpyW( p
, object
);
1099 request
->path
= path
;
1101 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1103 if (!version
|| !version
[0]) version
= http1_1
;
1104 if (!(request
->version
= strdupW( version
))) goto end
;
1105 if (!(store_accept_types( request
, types
))) goto end
;
1107 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1108 request
->hdr
.handle
= hrequest
;
1110 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1113 release_object( &request
->hdr
);
1114 release_object( &connect
->hdr
);
1115 TRACE("returning %p\n", hrequest
);
1119 /***********************************************************************
1120 * WinHttpCloseHandle (winhttp.@)
1122 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1124 object_header_t
*hdr
;
1126 TRACE("%p\n", handle
);
1128 if (!(hdr
= grab_object( handle
)))
1130 set_last_error( ERROR_INVALID_HANDLE
);
1133 release_object( hdr
);
1134 free_handle( handle
);
1138 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1144 set_last_error( ERROR_INVALID_PARAMETER
);
1150 case WINHTTP_OPTION_CONTEXT_VALUE
:
1152 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1154 *buflen
= sizeof(DWORD_PTR
);
1155 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1159 *(DWORD_PTR
*)buffer
= hdr
->context
;
1160 *buflen
= sizeof(DWORD_PTR
);
1164 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1167 FIXME("unimplemented option %u\n", option
);
1168 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1176 /***********************************************************************
1177 * WinHttpQueryOption (winhttp.@)
1179 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1182 object_header_t
*hdr
;
1184 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1186 if (!(hdr
= grab_object( handle
)))
1188 set_last_error( ERROR_INVALID_HANDLE
);
1192 ret
= query_option( hdr
, option
, buffer
, buflen
);
1194 release_object( hdr
);
1198 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1204 set_last_error( ERROR_INVALID_PARAMETER
);
1210 case WINHTTP_OPTION_CONTEXT_VALUE
:
1212 if (buflen
!= sizeof(DWORD_PTR
))
1214 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1218 hdr
->context
= *(DWORD_PTR
*)buffer
;
1222 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1225 FIXME("unimplemented option %u\n", option
);
1226 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1234 /***********************************************************************
1235 * WinHttpSetOption (winhttp.@)
1237 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1240 object_header_t
*hdr
;
1242 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1244 if (!(hdr
= grab_object( handle
)))
1246 set_last_error( ERROR_INVALID_HANDLE
);
1250 ret
= set_option( hdr
, option
, buffer
, buflen
);
1252 release_object( hdr
);
1256 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1261 GetComputerNameExA( format
, NULL
, &size
);
1262 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1263 if (!(ret
= heap_alloc( size
))) return NULL
;
1264 if (!GetComputerNameExA( format
, ret
, &size
))
1272 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1274 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1276 if (len_suffix
> len_domain
) return FALSE
;
1277 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1281 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1284 #ifdef HAVE_GETNAMEINFO
1285 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1290 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1292 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1293 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1294 char name
[NI_MAXHOST
];
1298 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1299 if (!ai
) return NULL
;
1301 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1303 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1304 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1305 strcpyW( p
, httpW
);
1306 p
+= strlenW( httpW
);
1307 while (*hostname
) { *p
++ = *hostname
++; }
1308 strcpyW( p
, wpadW
);
1312 static BOOL
get_system_proxy_autoconfig_url( char *buf
, DWORD buflen
)
1314 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1315 CFDictionaryRef settings
= CFNetworkCopySystemProxySettings();
1319 if (!settings
) return FALSE
;
1321 if (!(ref
= CFDictionaryGetValue( settings
, kCFNetworkProxiesProxyAutoConfigURLString
)))
1323 CFRelease( settings
);
1326 if (CFStringGetCString( ref
, buf
, buflen
, kCFStringEncodingASCII
))
1328 TRACE( "returning %s\n", debugstr_a(buf
) );
1331 CFRelease( settings
);
1334 FIXME( "no support on this platform\n" );
1339 #define INTERNET_MAX_URL_LENGTH 2084
1341 /***********************************************************************
1342 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1344 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1347 char system_url
[INTERNET_MAX_URL_LENGTH
+ 1];
1349 TRACE("0x%08x, %p\n", flags
, url
);
1353 set_last_error( ERROR_INVALID_PARAMETER
);
1356 if (get_system_proxy_autoconfig_url( system_url
, sizeof(system_url
) ))
1360 if (!(urlW
= strdupAW( system_url
))) return FALSE
;
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
);
1425 static const WCHAR Connections
[] = {
1426 'S','o','f','t','w','a','r','e','\\',
1427 'M','i','c','r','o','s','o','f','t','\\',
1428 'W','i','n','d','o','w','s','\\',
1429 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1430 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1431 'C','o','n','n','e','c','t','i','o','n','s',0 };
1432 static const WCHAR WinHttpSettings
[] = {
1433 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1434 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1435 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1436 static const DWORD PROXY_TYPE_DIRECT
= 1;
1437 static const DWORD PROXY_TYPE_PROXY
= 2;
1438 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1439 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1441 struct connection_settings_header
1444 DWORD unknown
; /* always zero? */
1445 DWORD flags
; /* one or more of PROXY_* */
1448 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1452 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1457 /***********************************************************************
1458 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1460 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1464 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1467 TRACE("%p\n", info
);
1469 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1472 DWORD type
, size
= 0;
1474 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1475 if (!l
&& type
== REG_BINARY
&&
1476 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1478 BYTE
*buf
= heap_alloc( size
);
1482 struct connection_settings_header
*hdr
=
1483 (struct connection_settings_header
*)buf
;
1484 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1486 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1488 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1491 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1494 LPWSTR proxy
= NULL
;
1495 LPWSTR proxy_bypass
= NULL
;
1497 /* Sanity-check length of proxy string */
1498 if ((BYTE
*)len
- buf
+ *len
<= size
)
1501 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1503 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1504 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1508 /* Sanity-check length of proxy bypass string */
1509 if ((BYTE
*)len
- buf
+ *len
<= size
)
1511 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1513 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1518 GlobalFree( proxy
);
1522 info
->lpszProxy
= proxy
;
1523 info
->lpszProxyBypass
= proxy_bypass
;
1526 got_from_reg
= TRUE
;
1528 info
->dwAccessType
=
1529 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1530 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1531 debugstr_w(info
->lpszProxy
),
1532 debugstr_w(info
->lpszProxyBypass
));
1541 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1543 char *colon
, *http_proxy
;
1545 if ((colon
= strchr( envproxy
, ':' )))
1547 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1549 static const char http
[] = "http://";
1551 /* It's a scheme, check that it's http */
1552 if (!strncmp( envproxy
, http
, strlen( http
) ))
1553 http_proxy
= envproxy
+ strlen( http
);
1556 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1561 http_proxy
= envproxy
;
1564 http_proxy
= envproxy
;
1570 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1571 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1573 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1575 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1576 info
->lpszProxy
= http_proxyW
;
1577 info
->lpszProxyBypass
= NULL
;
1578 TRACE("http proxy (from environment) = %s\n",
1579 debugstr_w(info
->lpszProxy
));
1585 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1586 info
->lpszProxy
= NULL
;
1587 info
->lpszProxyBypass
= NULL
;
1592 /***********************************************************************
1593 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1595 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1597 static const WCHAR settingsW
[] =
1598 {'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};
1600 struct connection_settings_header
*hdr
= NULL
;
1601 DWORD type
, offset
, len
, size
= 0;
1604 TRACE("%p\n", config
);
1608 set_last_error( ERROR_INVALID_PARAMETER
);
1611 memset( config
, 0, sizeof(*config
) );
1612 config
->fAutoDetect
= TRUE
;
1614 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1615 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1616 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1621 if (!(hdr
= heap_alloc( size
))) goto done
;
1622 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1623 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1629 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1630 offset
= sizeof(*hdr
);
1631 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1632 len
= *(DWORD
*)((char *)hdr
+ offset
);
1633 offset
+= sizeof(DWORD
);
1634 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1636 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1637 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1640 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1641 len
= *(DWORD
*)((char *)hdr
+ offset
);
1642 offset
+= sizeof(DWORD
);
1643 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1645 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1646 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1649 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1650 len
= *(DWORD
*)((char *)hdr
+ offset
);
1651 offset
+= sizeof(DWORD
);
1652 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1654 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1655 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1660 RegCloseKey( hkey
);
1664 GlobalFree( config
->lpszAutoConfigUrl
);
1665 config
->lpszAutoConfigUrl
= NULL
;
1666 GlobalFree( config
->lpszProxy
);
1667 config
->lpszProxy
= NULL
;
1668 GlobalFree( config
->lpszProxyBypass
);
1669 config
->lpszProxyBypass
= NULL
;
1674 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1680 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1681 info
->lpszProxy
= NULL
;
1682 info
->lpszProxyBypass
= NULL
;
1684 TRACE("%s\n", debugstr_a( result
));
1687 while (*p
== ' ') p
++;
1689 if (len
>= 5 && !strncasecmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1692 while (*p
== ' ') p
++;
1693 if (!*p
|| *p
== ';') return TRUE
;
1694 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1695 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1698 if (*q
== ' ' || *q
== ';')
1708 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1710 static const WCHAR typeW
[] = {'*','/','*',0};
1711 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
1712 HINTERNET ses
, con
= NULL
, req
= NULL
;
1715 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1716 char *tmp
, *buffer
= NULL
;
1720 memset( &uc
, 0, sizeof(uc
) );
1721 uc
.dwStructSize
= sizeof(uc
);
1722 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1723 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1724 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1725 hostname
[uc
.dwHostNameLength
] = 0;
1727 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1728 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1729 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1730 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1731 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1733 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
1734 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1735 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1738 if (!(buffer
= heap_alloc( size
))) goto done
;
1743 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
1744 if (!bytes_read
) break;
1745 to_read
-= bytes_read
;
1746 offset
+= bytes_read
;
1747 *out_size
+= bytes_read
;
1752 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
1758 WinHttpCloseHandle( req
);
1759 WinHttpCloseHandle( con
);
1760 WinHttpCloseHandle( ses
);
1761 heap_free( hostname
);
1762 if (!buffer
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1766 struct AUTO_PROXY_SCRIPT_BUFFER
1769 LPSTR lpszScriptBuffer
;
1770 DWORD dwScriptBufferSize
;
1773 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
1774 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
1775 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
1777 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1780 char *result
, *urlA
;
1782 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
1784 buffer
.dwStructSize
= sizeof(buffer
);
1785 buffer
.lpszScriptBuffer
= script
;
1786 buffer
.dwScriptBufferSize
= size
;
1788 if (!(urlA
= strdupWA( url
))) return FALSE
;
1789 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
1794 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
), NULL
, 0, &result
, &len_result
)))
1796 ret
= parse_script_result( result
, info
);
1797 heap_free( result
);
1800 return InternetDeInitializeAutoProxyDll( NULL
, 0 );
1803 /***********************************************************************
1804 * WinHttpGetProxyForUrl (winhttp.@)
1806 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1807 WINHTTP_PROXY_INFO
*info
)
1809 WCHAR
*detected_pac_url
= NULL
;
1810 const WCHAR
*pac_url
;
1816 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1818 if (!(session
= (session_t
*)grab_object( hsession
)))
1820 set_last_error( ERROR_INVALID_HANDLE
);
1823 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
1825 release_object( &session
->hdr
);
1826 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1829 if (!url
|| !options
|| !info
||
1830 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
1831 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
1832 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
1833 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
1835 release_object( &session
->hdr
);
1836 set_last_error( ERROR_INVALID_PARAMETER
);
1839 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
1840 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
1843 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
1844 else pac_url
= detected_pac_url
;
1846 if ((script
= download_script( pac_url
, &size
)))
1848 ret
= run_script( script
, size
, url
, info
);
1849 heap_free( script
);
1853 GlobalFree( detected_pac_url
);
1854 release_object( &session
->hdr
);
1858 /***********************************************************************
1859 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1861 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1868 TRACE("%p\n", info
);
1872 set_last_error( ERROR_INVALID_PARAMETER
);
1875 switch (info
->dwAccessType
)
1877 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1879 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1880 if (!info
->lpszProxy
)
1882 set_last_error( ERROR_INVALID_PARAMETER
);
1885 /* Only ASCII characters are allowed */
1886 for (src
= info
->lpszProxy
; *src
; src
++)
1889 set_last_error( ERROR_INVALID_PARAMETER
);
1892 if (info
->lpszProxyBypass
)
1894 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1897 set_last_error( ERROR_INVALID_PARAMETER
);
1903 set_last_error( ERROR_INVALID_PARAMETER
);
1907 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1908 KEY_WRITE
, NULL
, &key
, NULL
);
1911 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
1914 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1916 size
+= strlenW( info
->lpszProxy
);
1917 if (info
->lpszProxyBypass
)
1918 size
+= strlenW( info
->lpszProxyBypass
);
1920 buf
= heap_alloc( size
);
1923 struct connection_settings_header
*hdr
=
1924 (struct connection_settings_header
*)buf
;
1925 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1927 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
1929 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1933 hdr
->flags
= PROXY_TYPE_PROXY
;
1934 *len
++ = strlenW( info
->lpszProxy
);
1935 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1939 if (info
->lpszProxyBypass
)
1941 *len
++ = strlenW( info
->lpszProxyBypass
);
1942 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1951 hdr
->flags
= PROXY_TYPE_DIRECT
;
1955 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1965 /***********************************************************************
1966 * WinHttpSetStatusCallback (winhttp.@)
1968 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1969 DWORD flags
, DWORD_PTR reserved
)
1971 object_header_t
*hdr
;
1972 WINHTTP_STATUS_CALLBACK ret
;
1974 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1976 if (!(hdr
= grab_object( handle
)))
1978 set_last_error( ERROR_INVALID_HANDLE
);
1979 return WINHTTP_INVALID_STATUS_CALLBACK
;
1981 ret
= hdr
->callback
;
1982 hdr
->callback
= callback
;
1983 hdr
->notify_mask
= flags
;
1985 release_object( hdr
);
1989 /***********************************************************************
1990 * WinHttpSetTimeouts (winhttp.@)
1992 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
1995 object_header_t
*hdr
;
1999 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2001 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2003 set_last_error( ERROR_INVALID_PARAMETER
);
2007 if (!(hdr
= grab_object( handle
)))
2009 set_last_error( ERROR_INVALID_HANDLE
);
2015 case WINHTTP_HANDLE_TYPE_REQUEST
:
2016 request
= (request_t
*)hdr
;
2017 request
->connect_timeout
= connect
;
2019 if (resolve
< 0) resolve
= 0;
2020 request
->resolve_timeout
= resolve
;
2022 if (send
< 0) send
= 0;
2023 request
->send_timeout
= send
;
2025 if (receive
< 0) receive
= 0;
2026 request
->recv_timeout
= receive
;
2028 if (netconn_connected( &request
->netconn
))
2030 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2031 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2034 release_object( &request
->hdr
);
2037 case WINHTTP_HANDLE_TYPE_SESSION
:
2038 session
= (session_t
*)hdr
;
2039 session
->connect_timeout
= connect
;
2041 if (resolve
< 0) resolve
= 0;
2042 session
->resolve_timeout
= resolve
;
2044 if (send
< 0) send
= 0;
2045 session
->send_timeout
= send
;
2047 if (receive
< 0) receive
= 0;
2048 session
->recv_timeout
= receive
;
2052 release_object( hdr
);
2053 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2059 static const WCHAR wkday
[7][4] =
2060 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2061 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2062 static const WCHAR month
[12][4] =
2063 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2064 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2065 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2067 /***********************************************************************
2068 * WinHttpTimeFromSystemTime (WININET.@)
2070 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2072 static const WCHAR format
[] =
2073 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2074 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2076 TRACE("%p, %p\n", time
, string
);
2078 if (!time
|| !string
) return FALSE
;
2080 sprintfW( string
, format
,
2081 wkday
[time
->wDayOfWeek
],
2083 month
[time
->wMonth
- 1],
2092 /***********************************************************************
2093 * WinHttpTimeToSystemTime (WININET.@)
2095 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2098 const WCHAR
*s
= string
;
2101 TRACE("%s, %p\n", debugstr_w(string
), time
);
2103 if (!string
|| !time
) return FALSE
;
2105 /* Windows does this too */
2106 GetSystemTime( time
);
2108 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2109 * a SYSTEMTIME structure.
2112 while (*s
&& !isalphaW( *s
)) s
++;
2113 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2114 time
->wDayOfWeek
= 7;
2116 for (i
= 0; i
< 7; i
++)
2118 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2119 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2120 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2122 time
->wDayOfWeek
= i
;
2127 if (time
->wDayOfWeek
> 6) return TRUE
;
2128 while (*s
&& !isdigitW( *s
)) s
++;
2129 time
->wDay
= strtolW( s
, &end
, 10 );
2132 while (*s
&& !isalphaW( *s
)) s
++;
2133 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2136 for (i
= 0; i
< 12; i
++)
2138 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2139 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2140 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2142 time
->wMonth
= i
+ 1;
2146 if (time
->wMonth
== 0) return TRUE
;
2148 while (*s
&& !isdigitW( *s
)) s
++;
2149 if (*s
== '\0') return TRUE
;
2150 time
->wYear
= strtolW( s
, &end
, 10 );
2153 while (*s
&& !isdigitW( *s
)) s
++;
2154 if (*s
== '\0') return TRUE
;
2155 time
->wHour
= strtolW( s
, &end
, 10 );
2158 while (*s
&& !isdigitW( *s
)) s
++;
2159 if (*s
== '\0') return TRUE
;
2160 time
->wMinute
= strtolW( s
, &end
, 10 );
2163 while (*s
&& !isdigitW( *s
)) s
++;
2164 if (*s
== '\0') return TRUE
;
2165 time
->wSecond
= strtolW( s
, &end
, 10 );
2167 time
->wMilliseconds
= 0;