2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "winhttp_private.h"
26 #define DEFAULT_RESOLVE_TIMEOUT 0
27 #define DEFAULT_CONNECT_TIMEOUT 20000
28 #define DEFAULT_SEND_TIMEOUT 30000
29 #define DEFAULT_RECEIVE_TIMEOUT 30000
31 void set_last_error( DWORD error
)
34 SetLastError( error
);
37 DWORD
get_last_error( void )
40 return GetLastError();
43 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
45 if (hdr
->callback
&& (hdr
->notify_mask
& status
))
47 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
48 hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
49 TRACE("returning from 0x%08x callback\n", status
);
53 /***********************************************************************
54 * WinHttpCheckPlatform (winhttp.@)
56 BOOL WINAPI
WinHttpCheckPlatform( void )
62 /***********************************************************************
63 * session_destroy (internal)
65 static void session_destroy( object_header_t
*hdr
)
67 session_t
*session
= (session_t
*)hdr
;
68 struct list
*item
, *next
;
71 TRACE("%p\n", session
);
73 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
75 domain
= LIST_ENTRY( item
, domain_t
, entry
);
76 delete_domain( domain
);
78 heap_free( session
->agent
);
79 heap_free( session
->proxy_server
);
80 heap_free( session
->proxy_bypass
);
81 heap_free( session
->proxy_username
);
82 heap_free( session
->proxy_password
);
89 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
91 session_t
*session
= (session_t
*)hdr
;
95 case WINHTTP_OPTION_REDIRECT_POLICY
:
97 if (!buffer
|| *buflen
< sizeof(DWORD
))
99 *buflen
= sizeof(DWORD
);
100 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
104 *(DWORD
*)buffer
= hdr
->redirect_policy
;
105 *buflen
= sizeof(DWORD
);
108 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
109 *(DWORD
*)buffer
= session
->resolve_timeout
;
110 *buflen
= sizeof(DWORD
);
112 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
113 *(DWORD
*)buffer
= session
->connect_timeout
;
114 *buflen
= sizeof(DWORD
);
116 case WINHTTP_OPTION_SEND_TIMEOUT
:
117 *(DWORD
*)buffer
= session
->send_timeout
;
118 *buflen
= sizeof(DWORD
);
120 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
121 *(DWORD
*)buffer
= session
->recv_timeout
;
122 *buflen
= sizeof(DWORD
);
125 FIXME("unimplemented option %u\n", option
);
126 set_last_error( ERROR_INVALID_PARAMETER
);
131 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
133 session_t
*session
= (session_t
*)hdr
;
137 case WINHTTP_OPTION_PROXY
:
139 WINHTTP_PROXY_INFO
*pi
= buffer
;
141 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
144 case WINHTTP_OPTION_REDIRECT_POLICY
:
148 if (buflen
!= sizeof(policy
))
150 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
154 policy
= *(DWORD
*)buffer
;
155 TRACE("0x%x\n", policy
);
156 hdr
->redirect_policy
= policy
;
159 case WINHTTP_OPTION_DISABLE_FEATURE
:
160 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
162 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
163 session
->resolve_timeout
= *(DWORD
*)buffer
;
165 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
166 session
->connect_timeout
= *(DWORD
*)buffer
;
168 case WINHTTP_OPTION_SEND_TIMEOUT
:
169 session
->send_timeout
= *(DWORD
*)buffer
;
171 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
172 session
->recv_timeout
= *(DWORD
*)buffer
;
174 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
175 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
178 FIXME("unimplemented option %u\n", option
);
179 set_last_error( ERROR_INVALID_PARAMETER
);
184 static const object_vtbl_t session_vtbl
=
187 session_query_option
,
191 /***********************************************************************
192 * WinHttpOpen (winhttp.@)
194 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
197 HINTERNET handle
= NULL
;
200 int error
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
201 if (error
) ERR("WSAStartup failed: %d\n", error
);
204 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
206 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
208 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
209 session
->hdr
.vtbl
= &session_vtbl
;
210 session
->hdr
.flags
= flags
;
211 session
->hdr
.refs
= 1;
212 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
213 list_init( &session
->hdr
.children
);
214 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
215 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
216 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
217 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
218 list_init( &session
->cookie_cache
);
220 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
221 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
223 WINHTTP_PROXY_INFO info
;
225 WinHttpGetDefaultProxyConfiguration( &info
);
226 session
->access
= info
.dwAccessType
;
227 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
229 GlobalFree( (LPWSTR
)info
.lpszProxy
);
230 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
233 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
235 GlobalFree( (LPWSTR
)info
.lpszProxy
);
236 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
240 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
242 session
->access
= access
;
243 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
244 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
247 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
248 session
->hdr
.handle
= handle
;
251 release_object( &session
->hdr
);
252 TRACE("returning %p\n", handle
);
256 /***********************************************************************
257 * connect_destroy (internal)
259 static void connect_destroy( object_header_t
*hdr
)
261 connect_t
*connect
= (connect_t
*)hdr
;
263 TRACE("%p\n", connect
);
265 release_object( &connect
->session
->hdr
);
267 heap_free( connect
->hostname
);
268 heap_free( connect
->servername
);
269 heap_free( connect
->username
);
270 heap_free( connect
->password
);
271 heap_free( connect
);
274 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
276 connect_t
*connect
= (connect_t
*)hdr
;
280 case WINHTTP_OPTION_PARENT_HANDLE
:
282 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
284 *buflen
= sizeof(HINTERNET
);
285 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
289 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
290 *buflen
= sizeof(HINTERNET
);
293 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
294 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
295 *buflen
= sizeof(DWORD
);
297 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
298 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
299 *buflen
= sizeof(DWORD
);
301 case WINHTTP_OPTION_SEND_TIMEOUT
:
302 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
303 *buflen
= sizeof(DWORD
);
305 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
306 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
307 *buflen
= sizeof(DWORD
);
310 FIXME("unimplemented option %u\n", option
);
311 set_last_error( ERROR_INVALID_PARAMETER
);
316 static const object_vtbl_t connect_vtbl
=
319 connect_query_option
,
323 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
325 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
328 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
330 else if (*domain
== '*')
332 if (domain
[1] == '.')
336 /* For a hostname to match a wildcard, the last domain must match
337 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
338 * hostname is www.foo.a.b, it matches, but a.b does not.
340 dot
= strchrW( server
, '.' );
343 int len
= strlenW( dot
+ 1 );
345 if (len
> strlenW( domain
+ 2 ))
349 /* The server's domain is longer than the wildcard, so it
350 * could be a subdomain. Compare the last portion of the
353 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
354 if (!strcmpiW( ptr
, domain
+ 2 ))
356 /* This is only a match if the preceding character is
357 * a '.', i.e. that it is a matching domain. E.g.
358 * if domain is '*.b.c' and server is 'www.ab.c' they
361 ret
= *(ptr
- 1) == '.';
365 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
370 ret
= !strcmpiW( server
, domain
);
374 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
375 #define MAX_HOST_NAME_LENGTH 256
377 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
382 if (!session
->proxy_bypass
) return FALSE
;
383 ptr
= session
->proxy_bypass
;
387 ptr
= strchrW( ptr
, ';' );
389 ptr
= strchrW( tmp
, ' ' );
392 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
394 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
396 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
397 domain
[ptr
- tmp
] = 0;
398 ret
= domain_matches( server
, domain
);
403 ret
= domain_matches( server
, tmp
);
404 } while (ptr
&& !ret
);
408 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
410 session_t
*session
= connect
->session
;
413 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
417 if ((colon
= strchrW( session
->proxy_server
, ':' )))
419 if (!connect
->servername
|| strncmpiW( connect
->servername
,
420 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
422 heap_free( connect
->servername
);
423 connect
->resolved
= FALSE
;
424 if (!(connect
->servername
= heap_alloc(
425 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
430 memcpy( connect
->servername
, session
->proxy_server
,
431 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
432 connect
->servername
[colon
- session
->proxy_server
] = 0;
434 connect
->serverport
= atoiW( colon
+ 1 );
436 connect
->serverport
= INTERNET_DEFAULT_PORT
;
441 if (!connect
->servername
|| strcmpiW( connect
->servername
,
442 session
->proxy_server
))
444 heap_free( connect
->servername
);
445 connect
->resolved
= FALSE
;
446 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
451 connect
->serverport
= INTERNET_DEFAULT_PORT
;
457 heap_free( connect
->servername
);
458 connect
->resolved
= FALSE
;
459 if (!(connect
->servername
= strdupW( server
)))
464 connect
->serverport
= port
;
470 /***********************************************************************
471 * WinHttpConnect (winhttp.@)
473 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
477 HINTERNET hconnect
= NULL
;
479 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
483 set_last_error( ERROR_INVALID_PARAMETER
);
486 if (!(session
= (session_t
*)grab_object( hsession
)))
488 set_last_error( ERROR_INVALID_HANDLE
);
491 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
493 release_object( &session
->hdr
);
494 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
497 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
499 release_object( &session
->hdr
);
502 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
503 connect
->hdr
.vtbl
= &connect_vtbl
;
504 connect
->hdr
.refs
= 1;
505 connect
->hdr
.flags
= session
->hdr
.flags
;
506 connect
->hdr
.callback
= session
->hdr
.callback
;
507 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
508 connect
->hdr
.context
= session
->hdr
.context
;
509 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
510 list_init( &connect
->hdr
.children
);
512 addref_object( &session
->hdr
);
513 connect
->session
= session
;
514 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
516 if (!(connect
->hostname
= strdupW( server
))) goto end
;
517 connect
->hostport
= port
;
518 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
520 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
521 connect
->hdr
.handle
= hconnect
;
523 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
526 release_object( &connect
->hdr
);
527 release_object( &session
->hdr
);
528 TRACE("returning %p\n", hconnect
);
532 /***********************************************************************
533 * request_destroy (internal)
535 static void request_destroy( object_header_t
*hdr
)
537 request_t
*request
= (request_t
*)hdr
;
540 TRACE("%p\n", request
);
542 if (request
->task_thread
)
544 SetEvent( request
->task_cancel
);
545 WaitForSingleObject( request
->task_thread
, INFINITE
);
546 CloseHandle( request
->task_thread
);
547 CloseHandle( request
->task_cancel
);
548 CloseHandle( request
->task_wait
);
549 DeleteCriticalSection( &request
->task_cs
);
551 release_object( &request
->connect
->hdr
);
553 destroy_authinfo( request
->authinfo
);
554 destroy_authinfo( request
->proxy_authinfo
);
556 heap_free( request
->verb
);
557 heap_free( request
->path
);
558 heap_free( request
->version
);
559 heap_free( request
->raw_headers
);
560 heap_free( request
->status_text
);
561 for (i
= 0; i
< request
->num_headers
; i
++)
563 heap_free( request
->headers
[i
].field
);
564 heap_free( request
->headers
[i
].value
);
566 heap_free( request
->headers
);
567 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
568 heap_free( request
->accept_types
);
569 heap_free( request
);
572 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
575 if (str
) len
= strlenW( str
);
576 if (buffer
&& *buflen
> len
)
578 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
581 *buflen
= len
* sizeof(WCHAR
);
584 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
587 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
589 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
590 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
591 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
596 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
598 #if !defined(__MINGW32__) && !defined(_MSC_VER)
599 switch (addr
->sa_family
)
603 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
604 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
607 addr_win
->sin_family
= WS_AF_INET
;
608 addr_win
->sin_port
= addr_unix
->sin_port
;
609 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
610 p
= (char *)&addr_win
->sin_addr
+ 4;
611 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
616 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
617 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
619 addr_win
->sin6_family
= WS_AF_INET6
;
620 addr_win
->sin6_port
= addr_unix
->sin6_port
;
621 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
622 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
623 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
624 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
626 addr_win
->sin6_scope_id
= 0;
628 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
632 ERR("unhandled family %u\n", addr
->sa_family
);
636 switch (addr
->sa_family
)
640 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
642 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
643 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
648 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
650 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
651 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
655 ERR("unhandled family %u\n", addr
->sa_family
);
661 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
663 request_t
*request
= (request_t
*)hdr
;
667 case WINHTTP_OPTION_SECURITY_FLAGS
:
672 if (!buffer
|| *buflen
< sizeof(flags
))
674 *buflen
= sizeof(flags
);
675 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
680 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
681 flags
|= request
->netconn
.security_flags
;
682 bits
= netconn_get_cipher_strength( &request
->netconn
);
684 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
686 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
688 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
689 *(DWORD
*)buffer
= flags
;
690 *buflen
= sizeof(flags
);
693 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
695 const CERT_CONTEXT
*cert
;
697 if (!buffer
|| *buflen
< sizeof(cert
))
699 *buflen
= sizeof(cert
);
700 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
704 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
705 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
706 *buflen
= sizeof(cert
);
709 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
711 const CERT_CONTEXT
*cert
;
712 const CRYPT_OID_INFO
*oidInfo
;
713 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
715 FIXME("partial stub\n");
717 if (!buffer
|| *buflen
< sizeof(*ci
))
719 *buflen
= sizeof(*ci
);
720 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
723 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
725 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
726 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
727 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
728 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
729 ci
->lpszProtocolName
= NULL
;
730 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
731 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
734 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
736 ci
->lpszSignatureAlgName
= NULL
;
737 ci
->lpszEncryptionAlgName
= NULL
;
738 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
740 CertFreeCertificateContext( cert
);
741 *buflen
= sizeof(*ci
);
744 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
746 if (!buffer
|| *buflen
< sizeof(DWORD
))
748 *buflen
= sizeof(DWORD
);
749 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
753 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
754 *buflen
= sizeof(DWORD
);
757 case WINHTTP_OPTION_CONNECTION_INFO
:
759 WINHTTP_CONNECTION_INFO
*info
= buffer
;
760 struct sockaddr local
;
761 socklen_t len
= sizeof(local
);
762 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
764 if (!buffer
|| *buflen
< sizeof(*info
))
766 *buflen
= sizeof(*info
);
767 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
770 if (!netconn_connected( &request
->netconn
))
772 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
775 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
776 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
777 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
778 info
->cbSize
= sizeof(*info
);
781 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
782 *(DWORD
*)buffer
= request
->resolve_timeout
;
783 *buflen
= sizeof(DWORD
);
785 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
786 *(DWORD
*)buffer
= request
->connect_timeout
;
787 *buflen
= sizeof(DWORD
);
789 case WINHTTP_OPTION_SEND_TIMEOUT
:
790 *(DWORD
*)buffer
= request
->send_timeout
;
791 *buflen
= sizeof(DWORD
);
793 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
794 *(DWORD
*)buffer
= request
->recv_timeout
;
795 *buflen
= sizeof(DWORD
);
798 case WINHTTP_OPTION_USERNAME
:
799 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
802 case WINHTTP_OPTION_PASSWORD
:
803 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
806 case WINHTTP_OPTION_PROXY_USERNAME
:
807 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
810 case WINHTTP_OPTION_PROXY_PASSWORD
:
811 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
815 FIXME("unimplemented option %u\n", option
);
816 set_last_error( ERROR_INVALID_PARAMETER
);
821 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
824 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
826 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
830 set_last_error( ERROR_OUTOFMEMORY
);
834 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
836 request_t
*request
= (request_t
*)hdr
;
840 case WINHTTP_OPTION_PROXY
:
842 WINHTTP_PROXY_INFO
*pi
= buffer
;
844 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
847 case WINHTTP_OPTION_DISABLE_FEATURE
:
851 if (buflen
!= sizeof(DWORD
))
853 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
857 disable
= *(DWORD
*)buffer
;
858 TRACE("0x%x\n", disable
);
859 hdr
->disable_flags
|= disable
;
862 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
866 if (buflen
!= sizeof(DWORD
))
868 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
872 policy
= *(DWORD
*)buffer
;
873 TRACE("0x%x\n", policy
);
874 hdr
->logon_policy
= policy
;
877 case WINHTTP_OPTION_REDIRECT_POLICY
:
881 if (buflen
!= sizeof(DWORD
))
883 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
887 policy
= *(DWORD
*)buffer
;
888 TRACE("0x%x\n", policy
);
889 hdr
->redirect_policy
= policy
;
892 case WINHTTP_OPTION_SECURITY_FLAGS
:
896 if (buflen
< sizeof(DWORD
))
898 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
901 flags
= *(DWORD
*)buffer
;
902 TRACE("0x%x\n", flags
);
903 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
904 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
905 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
906 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
908 set_last_error( ERROR_INVALID_PARAMETER
);
911 request
->netconn
.security_flags
= flags
;
914 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
915 request
->resolve_timeout
= *(DWORD
*)buffer
;
917 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
918 request
->connect_timeout
= *(DWORD
*)buffer
;
920 case WINHTTP_OPTION_SEND_TIMEOUT
:
921 request
->send_timeout
= *(DWORD
*)buffer
;
923 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
924 request
->recv_timeout
= *(DWORD
*)buffer
;
927 case WINHTTP_OPTION_USERNAME
:
929 connect_t
*connect
= request
->connect
;
931 heap_free( connect
->username
);
932 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
935 case WINHTTP_OPTION_PASSWORD
:
937 connect_t
*connect
= request
->connect
;
939 heap_free( connect
->password
);
940 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
943 case WINHTTP_OPTION_PROXY_USERNAME
:
945 session_t
*session
= request
->connect
->session
;
947 heap_free( session
->proxy_username
);
948 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
951 case WINHTTP_OPTION_PROXY_PASSWORD
:
953 session_t
*session
= request
->connect
->session
;
955 heap_free( session
->proxy_password
);
956 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
960 FIXME("unimplemented option %u\n", option
);
961 set_last_error( ERROR_INVALID_PARAMETER
);
966 static const object_vtbl_t request_vtbl
=
969 request_query_option
,
973 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
975 const WCHAR
**types
= accept_types
;
978 if (!types
) return TRUE
;
981 request
->num_accept_types
++;
984 if (!request
->num_accept_types
) return TRUE
;
985 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
987 request
->num_accept_types
= 0;
990 types
= accept_types
;
991 for (i
= 0; i
< request
->num_accept_types
; i
++)
993 if (!(request
->accept_types
[i
] = strdupW( *types
)))
995 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
996 heap_free( request
->accept_types
);
997 request
->accept_types
= NULL
;
998 request
->num_accept_types
= 0;
1006 /***********************************************************************
1007 * WinHttpOpenRequest (winhttp.@)
1009 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1010 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1014 HINTERNET hrequest
= NULL
;
1016 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1017 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1019 if(types
&& TRACE_ON(winhttp
)) {
1022 TRACE("accept types:\n");
1023 for(iter
= types
; *iter
; iter
++)
1024 TRACE(" %s\n", debugstr_w(*iter
));
1027 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1029 set_last_error( ERROR_INVALID_HANDLE
);
1032 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1034 release_object( &connect
->hdr
);
1035 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1038 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1040 release_object( &connect
->hdr
);
1043 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1044 request
->hdr
.vtbl
= &request_vtbl
;
1045 request
->hdr
.refs
= 1;
1046 request
->hdr
.flags
= flags
;
1047 request
->hdr
.callback
= connect
->hdr
.callback
;
1048 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1049 request
->hdr
.context
= connect
->hdr
.context
;
1050 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1051 list_init( &request
->hdr
.children
);
1052 list_init( &request
->task_queue
);
1054 addref_object( &connect
->hdr
);
1055 request
->connect
= connect
;
1056 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1058 if (!netconn_init( &request
->netconn
)) goto end
;
1059 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1060 request
->connect_timeout
= connect
->session
->connect_timeout
;
1061 request
->send_timeout
= connect
->session
->send_timeout
;
1062 request
->recv_timeout
= connect
->session
->recv_timeout
;
1064 if (!verb
|| !verb
[0]) verb
= getW
;
1065 if (!(request
->verb
= strdupW( verb
))) goto end
;
1072 len
= strlenW( object
) + 1;
1073 if (object
[0] != '/') len
++;
1074 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1076 if (object
[0] != '/') *p
++ = '/';
1077 strcpyW( p
, object
);
1078 request
->path
= path
;
1080 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1082 if (!version
|| !version
[0]) version
= http1_1
;
1083 if (!(request
->version
= strdupW( version
))) goto end
;
1084 if (!(store_accept_types( request
, types
))) goto end
;
1086 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1087 request
->hdr
.handle
= hrequest
;
1089 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1092 release_object( &request
->hdr
);
1093 release_object( &connect
->hdr
);
1094 TRACE("returning %p\n", hrequest
);
1098 /***********************************************************************
1099 * WinHttpCloseHandle (winhttp.@)
1101 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1103 object_header_t
*hdr
;
1105 TRACE("%p\n", handle
);
1107 if (!(hdr
= grab_object( handle
)))
1109 set_last_error( ERROR_INVALID_HANDLE
);
1112 release_object( hdr
);
1113 free_handle( handle
);
1117 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1123 set_last_error( ERROR_INVALID_PARAMETER
);
1129 case WINHTTP_OPTION_CONTEXT_VALUE
:
1131 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1133 *buflen
= sizeof(DWORD_PTR
);
1134 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1138 *(DWORD_PTR
*)buffer
= hdr
->context
;
1139 *buflen
= sizeof(DWORD_PTR
);
1143 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1146 FIXME("unimplemented option %u\n", option
);
1147 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1155 /***********************************************************************
1156 * WinHttpQueryOption (winhttp.@)
1158 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1161 object_header_t
*hdr
;
1163 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1165 if (!(hdr
= grab_object( handle
)))
1167 set_last_error( ERROR_INVALID_HANDLE
);
1171 ret
= query_option( hdr
, option
, buffer
, buflen
);
1173 release_object( hdr
);
1177 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1183 set_last_error( ERROR_INVALID_PARAMETER
);
1189 case WINHTTP_OPTION_CONTEXT_VALUE
:
1191 if (buflen
!= sizeof(DWORD_PTR
))
1193 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1197 hdr
->context
= *(DWORD_PTR
*)buffer
;
1201 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1204 FIXME("unimplemented option %u\n", option
);
1205 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1213 /***********************************************************************
1214 * WinHttpSetOption (winhttp.@)
1216 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1219 object_header_t
*hdr
;
1221 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1223 if (!(hdr
= grab_object( handle
)))
1225 set_last_error( ERROR_INVALID_HANDLE
);
1229 ret
= set_option( hdr
, option
, buffer
, buflen
);
1231 release_object( hdr
);
1235 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1240 GetComputerNameExA( format
, NULL
, &size
);
1241 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1242 if (!(ret
= heap_alloc( size
))) return NULL
;
1243 if (!GetComputerNameExA( format
, ret
, &size
))
1251 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1253 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1255 if (len_suffix
> len_domain
) return FALSE
;
1256 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1260 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1263 #ifdef HAVE_GETNAMEINFO
1264 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1269 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1271 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1272 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1273 char name
[NI_MAXHOST
];
1277 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1278 if (!ai
) return NULL
;
1280 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1282 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1283 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1284 strcpyW( p
, httpW
);
1285 p
+= strlenW( httpW
);
1286 while (*hostname
) { *p
++ = *hostname
++; }
1287 strcpyW( p
, wpadW
);
1291 static BOOL
get_system_proxy_autoconfig_url( char *buf
, DWORD buflen
)
1293 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1294 CFDictionaryRef settings
= CFNetworkCopySystemProxySettings();
1298 if (!settings
) return FALSE
;
1300 if (!(ref
= CFDictionaryGetValue( settings
, kCFNetworkProxiesProxyAutoConfigURLString
)))
1302 CFRelease( settings
);
1305 if (CFStringGetCString( ref
, buf
, buflen
, kCFStringEncodingASCII
))
1307 TRACE( "returning %s\n", debugstr_a(buf
) );
1310 CFRelease( settings
);
1313 FIXME( "no support on this platform\n" );
1318 #define INTERNET_MAX_URL_LENGTH 2084
1320 /***********************************************************************
1321 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1323 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1326 char system_url
[INTERNET_MAX_URL_LENGTH
+ 1];
1328 TRACE("0x%08x, %p\n", flags
, url
);
1332 set_last_error( ERROR_INVALID_PARAMETER
);
1335 if (get_system_proxy_autoconfig_url( system_url
, sizeof(system_url
) ))
1339 if (!(urlW
= strdupAW( system_url
))) return FALSE
;
1343 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1345 static int fixme_shown
;
1346 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1348 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1350 #ifdef HAVE_GETADDRINFO
1351 char *fqdn
, *domain
, *p
;
1353 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1354 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1360 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1362 struct addrinfo
*ai
;
1366 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1369 heap_free( domain
);
1372 strcpy( name
, "wpad" );
1374 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1377 *url
= build_wpad_url( name
, ai
);
1381 TRACE("returning %s\n", debugstr_w(*url
));
1390 heap_free( domain
);
1393 FIXME("getaddrinfo not found at build time\n");
1398 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1404 static const WCHAR Connections
[] = {
1405 'S','o','f','t','w','a','r','e','\\',
1406 'M','i','c','r','o','s','o','f','t','\\',
1407 'W','i','n','d','o','w','s','\\',
1408 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1409 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1410 'C','o','n','n','e','c','t','i','o','n','s',0 };
1411 static const WCHAR WinHttpSettings
[] = {
1412 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1413 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1414 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1415 static const DWORD PROXY_TYPE_DIRECT
= 1;
1416 static const DWORD PROXY_TYPE_PROXY
= 2;
1417 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1418 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1420 struct connection_settings_header
1423 DWORD unknown
; /* always zero? */
1424 DWORD flags
; /* one or more of PROXY_* */
1427 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1431 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1436 /***********************************************************************
1437 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1439 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1443 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1446 TRACE("%p\n", info
);
1448 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1451 DWORD type
, size
= 0;
1453 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1454 if (!l
&& type
== REG_BINARY
&&
1455 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1457 BYTE
*buf
= heap_alloc( size
);
1461 struct connection_settings_header
*hdr
=
1462 (struct connection_settings_header
*)buf
;
1463 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1465 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1467 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1470 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1473 LPWSTR proxy
= NULL
;
1474 LPWSTR proxy_bypass
= NULL
;
1476 /* Sanity-check length of proxy string */
1477 if ((BYTE
*)len
- buf
+ *len
<= size
)
1480 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1482 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1483 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1487 /* Sanity-check length of proxy bypass string */
1488 if ((BYTE
*)len
- buf
+ *len
<= size
)
1490 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1492 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1497 GlobalFree( proxy
);
1501 info
->lpszProxy
= proxy
;
1502 info
->lpszProxyBypass
= proxy_bypass
;
1505 got_from_reg
= TRUE
;
1507 info
->dwAccessType
=
1508 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1509 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1510 debugstr_w(info
->lpszProxy
),
1511 debugstr_w(info
->lpszProxyBypass
));
1520 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1522 char *colon
, *http_proxy
;
1524 if ((colon
= strchr( envproxy
, ':' )))
1526 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1528 static const char http
[] = "http://";
1530 /* It's a scheme, check that it's http */
1531 if (!strncmp( envproxy
, http
, strlen( http
) ))
1532 http_proxy
= envproxy
+ strlen( http
);
1535 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1540 http_proxy
= envproxy
;
1543 http_proxy
= envproxy
;
1549 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1550 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1552 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1554 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1555 info
->lpszProxy
= http_proxyW
;
1556 info
->lpszProxyBypass
= NULL
;
1557 TRACE("http proxy (from environment) = %s\n",
1558 debugstr_w(info
->lpszProxy
));
1564 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1565 info
->lpszProxy
= NULL
;
1566 info
->lpszProxyBypass
= NULL
;
1571 /***********************************************************************
1572 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1574 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1576 static const WCHAR settingsW
[] =
1577 {'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};
1579 struct connection_settings_header
*hdr
= NULL
;
1580 DWORD type
, offset
, len
, size
= 0;
1583 TRACE("%p\n", config
);
1587 set_last_error( ERROR_INVALID_PARAMETER
);
1590 memset( config
, 0, sizeof(*config
) );
1591 config
->fAutoDetect
= TRUE
;
1593 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1594 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1595 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1600 if (!(hdr
= heap_alloc( size
))) goto done
;
1601 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1602 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1608 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1609 offset
= sizeof(*hdr
);
1610 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1611 len
= *(DWORD
*)((char *)hdr
+ offset
);
1612 offset
+= sizeof(DWORD
);
1613 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1615 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1616 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1619 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1620 len
= *(DWORD
*)((char *)hdr
+ offset
);
1621 offset
+= sizeof(DWORD
);
1622 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1624 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1625 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1628 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1629 len
= *(DWORD
*)((char *)hdr
+ offset
);
1630 offset
+= sizeof(DWORD
);
1631 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1633 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1634 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1639 RegCloseKey( hkey
);
1643 GlobalFree( config
->lpszAutoConfigUrl
);
1644 config
->lpszAutoConfigUrl
= NULL
;
1645 GlobalFree( config
->lpszProxy
);
1646 config
->lpszProxy
= NULL
;
1647 GlobalFree( config
->lpszProxyBypass
);
1648 config
->lpszProxyBypass
= NULL
;
1653 static BOOL
parse_script_result( const char *result
, WINHTTP_PROXY_INFO
*info
)
1659 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1660 info
->lpszProxy
= NULL
;
1661 info
->lpszProxyBypass
= NULL
;
1663 TRACE("%s\n", debugstr_a( result
));
1666 while (*p
== ' ') p
++;
1668 if (len
>= 5 && !strncasecmp( p
, "PROXY", sizeof("PROXY") - 1 ))
1671 while (*p
== ' ') p
++;
1672 if (!*p
|| *p
== ';') return TRUE
;
1673 if (!(info
->lpszProxy
= q
= strdupAW( p
))) return FALSE
;
1674 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1677 if (*q
== ' ' || *q
== ';')
1687 static char *download_script( const WCHAR
*url
, DWORD
*out_size
)
1689 static const WCHAR typeW
[] = {'*','/','*',0};
1690 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
1691 HINTERNET ses
, con
= NULL
, req
= NULL
;
1694 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
1695 char *tmp
, *buffer
= NULL
;
1699 memset( &uc
, 0, sizeof(uc
) );
1700 uc
.dwStructSize
= sizeof(uc
);
1701 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
1702 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1703 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1704 hostname
[uc
.dwHostNameLength
] = 0;
1706 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
1707 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
1708 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
1709 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
1710 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
1712 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
1713 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
1714 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
1717 if (!(buffer
= heap_alloc( size
))) goto done
;
1722 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
1723 if (!bytes_read
) break;
1724 to_read
-= bytes_read
;
1725 offset
+= bytes_read
;
1726 *out_size
+= bytes_read
;
1731 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
1737 WinHttpCloseHandle( req
);
1738 WinHttpCloseHandle( con
);
1739 WinHttpCloseHandle( ses
);
1740 heap_free( hostname
);
1741 if (!buffer
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
1745 struct AUTO_PROXY_SCRIPT_BUFFER
1748 LPSTR lpszScriptBuffer
;
1749 DWORD dwScriptBufferSize
;
1752 BOOL WINAPI
InternetDeInitializeAutoProxyDll(LPSTR
, DWORD
);
1753 BOOL WINAPI
InternetGetProxyInfo(LPCSTR
, DWORD
, LPSTR
, DWORD
, LPSTR
*, LPDWORD
);
1754 BOOL WINAPI
InternetInitializeAutoProxyDll(DWORD
, LPSTR
, LPSTR
, void *, struct AUTO_PROXY_SCRIPT_BUFFER
*);
1756 static BOOL
run_script( char *script
, DWORD size
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1759 char *result
, *urlA
;
1761 struct AUTO_PROXY_SCRIPT_BUFFER buffer
;
1763 buffer
.dwStructSize
= sizeof(buffer
);
1764 buffer
.lpszScriptBuffer
= script
;
1765 buffer
.dwScriptBufferSize
= size
;
1767 if (!(urlA
= strdupWA( url
))) return FALSE
;
1768 if (!(ret
= InternetInitializeAutoProxyDll( 0, NULL
, NULL
, NULL
, &buffer
)))
1773 if ((ret
= InternetGetProxyInfo( urlA
, strlen(urlA
), NULL
, 0, &result
, &len_result
)))
1775 ret
= parse_script_result( result
, info
);
1776 heap_free( result
);
1779 return InternetDeInitializeAutoProxyDll( NULL
, 0 );
1782 /***********************************************************************
1783 * WinHttpGetProxyForUrl (winhttp.@)
1785 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1786 WINHTTP_PROXY_INFO
*info
)
1788 WCHAR
*detected_pac_url
= NULL
;
1789 const WCHAR
*pac_url
;
1795 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1797 if (!(session
= (session_t
*)grab_object( hsession
)))
1799 set_last_error( ERROR_INVALID_HANDLE
);
1802 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
1804 release_object( &session
->hdr
);
1805 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1808 if (!url
|| !options
|| !info
||
1809 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
1810 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
1811 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
1812 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
1814 release_object( &session
->hdr
);
1815 set_last_error( ERROR_INVALID_PARAMETER
);
1818 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
1819 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
1822 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
1823 else pac_url
= detected_pac_url
;
1825 if ((script
= download_script( pac_url
, &size
)))
1827 ret
= run_script( script
, size
, url
, info
);
1828 heap_free( script
);
1832 GlobalFree( detected_pac_url
);
1833 release_object( &session
->hdr
);
1837 /***********************************************************************
1838 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1840 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1847 TRACE("%p\n", info
);
1851 set_last_error( ERROR_INVALID_PARAMETER
);
1854 switch (info
->dwAccessType
)
1856 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1858 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1859 if (!info
->lpszProxy
)
1861 set_last_error( ERROR_INVALID_PARAMETER
);
1864 /* Only ASCII characters are allowed */
1865 for (src
= info
->lpszProxy
; *src
; src
++)
1868 set_last_error( ERROR_INVALID_PARAMETER
);
1871 if (info
->lpszProxyBypass
)
1873 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1876 set_last_error( ERROR_INVALID_PARAMETER
);
1882 set_last_error( ERROR_INVALID_PARAMETER
);
1886 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1887 KEY_WRITE
, NULL
, &key
, NULL
);
1890 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
1893 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1895 size
+= strlenW( info
->lpszProxy
);
1896 if (info
->lpszProxyBypass
)
1897 size
+= strlenW( info
->lpszProxyBypass
);
1899 buf
= heap_alloc( size
);
1902 struct connection_settings_header
*hdr
=
1903 (struct connection_settings_header
*)buf
;
1904 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1906 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
1908 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1912 hdr
->flags
= PROXY_TYPE_PROXY
;
1913 *len
++ = strlenW( info
->lpszProxy
);
1914 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1918 if (info
->lpszProxyBypass
)
1920 *len
++ = strlenW( info
->lpszProxyBypass
);
1921 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1930 hdr
->flags
= PROXY_TYPE_DIRECT
;
1934 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1944 /***********************************************************************
1945 * WinHttpSetStatusCallback (winhttp.@)
1947 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1948 DWORD flags
, DWORD_PTR reserved
)
1950 object_header_t
*hdr
;
1951 WINHTTP_STATUS_CALLBACK ret
;
1953 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1955 if (!(hdr
= grab_object( handle
)))
1957 set_last_error( ERROR_INVALID_HANDLE
);
1958 return WINHTTP_INVALID_STATUS_CALLBACK
;
1960 ret
= hdr
->callback
;
1961 hdr
->callback
= callback
;
1962 hdr
->notify_mask
= flags
;
1964 release_object( hdr
);
1968 /***********************************************************************
1969 * WinHttpSetTimeouts (winhttp.@)
1971 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
1974 object_header_t
*hdr
;
1978 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
1980 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
1982 set_last_error( ERROR_INVALID_PARAMETER
);
1986 if (!(hdr
= grab_object( handle
)))
1988 set_last_error( ERROR_INVALID_HANDLE
);
1994 case WINHTTP_HANDLE_TYPE_REQUEST
:
1995 request
= (request_t
*)hdr
;
1996 request
->connect_timeout
= connect
;
1998 if (resolve
< 0) resolve
= 0;
1999 request
->resolve_timeout
= resolve
;
2001 if (send
< 0) send
= 0;
2002 request
->send_timeout
= send
;
2004 if (receive
< 0) receive
= 0;
2005 request
->recv_timeout
= receive
;
2007 if (netconn_connected( &request
->netconn
))
2009 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2010 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2013 release_object( &request
->hdr
);
2016 case WINHTTP_HANDLE_TYPE_SESSION
:
2017 session
= (session_t
*)hdr
;
2018 session
->connect_timeout
= connect
;
2020 if (resolve
< 0) resolve
= 0;
2021 session
->resolve_timeout
= resolve
;
2023 if (send
< 0) send
= 0;
2024 session
->send_timeout
= send
;
2026 if (receive
< 0) receive
= 0;
2027 session
->recv_timeout
= receive
;
2031 release_object( hdr
);
2032 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2038 static const WCHAR wkday
[7][4] =
2039 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2040 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2041 static const WCHAR month
[12][4] =
2042 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2043 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2044 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2046 /***********************************************************************
2047 * WinHttpTimeFromSystemTime (WININET.@)
2049 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2051 static const WCHAR format
[] =
2052 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2053 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2055 TRACE("%p, %p\n", time
, string
);
2057 if (!time
|| !string
) return FALSE
;
2059 sprintfW( string
, format
,
2060 wkday
[time
->wDayOfWeek
],
2062 month
[time
->wMonth
- 1],
2071 /***********************************************************************
2072 * WinHttpTimeToSystemTime (WININET.@)
2074 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2077 const WCHAR
*s
= string
;
2080 TRACE("%s, %p\n", debugstr_w(string
), time
);
2082 if (!string
|| !time
) return FALSE
;
2084 /* Windows does this too */
2085 GetSystemTime( time
);
2087 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2088 * a SYSTEMTIME structure.
2091 while (*s
&& !isalphaW( *s
)) s
++;
2092 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2093 time
->wDayOfWeek
= 7;
2095 for (i
= 0; i
< 7; i
++)
2097 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2098 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2099 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2101 time
->wDayOfWeek
= i
;
2106 if (time
->wDayOfWeek
> 6) return TRUE
;
2107 while (*s
&& !isdigitW( *s
)) s
++;
2108 time
->wDay
= strtolW( s
, &end
, 10 );
2111 while (*s
&& !isalphaW( *s
)) s
++;
2112 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2115 for (i
= 0; i
< 12; i
++)
2117 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2118 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2119 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2121 time
->wMonth
= i
+ 1;
2125 if (time
->wMonth
== 0) return TRUE
;
2127 while (*s
&& !isdigitW( *s
)) s
++;
2128 if (*s
== '\0') return TRUE
;
2129 time
->wYear
= strtolW( s
, &end
, 10 );
2132 while (*s
&& !isdigitW( *s
)) s
++;
2133 if (*s
== '\0') return TRUE
;
2134 time
->wHour
= strtolW( s
, &end
, 10 );
2137 while (*s
&& !isdigitW( *s
)) s
++;
2138 if (*s
== '\0') return TRUE
;
2139 time
->wMinute
= strtolW( s
, &end
, 10 );
2142 while (*s
&& !isdigitW( *s
)) s
++;
2143 if (*s
== '\0') return TRUE
;
2144 time
->wSecond
= strtolW( s
, &end
, 10 );
2146 time
->wMilliseconds
= 0;