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 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
24 //#include "wine/port.h"
25 #include <wine/debug.h>
41 #include "winhttp_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
45 #define DEFAULT_RESOLVE_TIMEOUT 0
46 #define DEFAULT_CONNECT_TIMEOUT 20000
47 #define DEFAULT_SEND_TIMEOUT 30000
48 #define DEFAULT_RECEIVE_TIMEOUT 30000
50 static const WCHAR global_funcsW
[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
51 static const WCHAR dns_resolveW
[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
53 void set_last_error( DWORD error
)
56 SetLastError( error
);
59 DWORD
get_last_error( void )
62 return GetLastError();
65 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
67 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
69 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
72 /***********************************************************************
73 * WinHttpCheckPlatform (winhttp.@)
75 BOOL WINAPI
WinHttpCheckPlatform( void )
81 /***********************************************************************
82 * session_destroy (internal)
84 static void session_destroy( object_header_t
*hdr
)
86 session_t
*session
= (session_t
*)hdr
;
87 struct list
*item
, *next
;
90 TRACE("%p\n", session
);
92 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
94 domain
= LIST_ENTRY( item
, domain_t
, entry
);
95 delete_domain( domain
);
97 heap_free( session
->agent
);
98 heap_free( session
->proxy_server
);
99 heap_free( session
->proxy_bypass
);
100 heap_free( session
->proxy_username
);
101 heap_free( session
->proxy_password
);
102 heap_free( session
);
108 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
110 session_t
*session
= (session_t
*)hdr
;
114 case WINHTTP_OPTION_REDIRECT_POLICY
:
116 if (!buffer
|| *buflen
< sizeof(DWORD
))
118 *buflen
= sizeof(DWORD
);
119 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
123 *(DWORD
*)buffer
= hdr
->redirect_policy
;
124 *buflen
= sizeof(DWORD
);
127 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
128 *(DWORD
*)buffer
= session
->resolve_timeout
;
129 *buflen
= sizeof(DWORD
);
131 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
132 *(DWORD
*)buffer
= session
->connect_timeout
;
133 *buflen
= sizeof(DWORD
);
135 case WINHTTP_OPTION_SEND_TIMEOUT
:
136 *(DWORD
*)buffer
= session
->send_timeout
;
137 *buflen
= sizeof(DWORD
);
139 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
140 *(DWORD
*)buffer
= session
->recv_timeout
;
141 *buflen
= sizeof(DWORD
);
144 FIXME("unimplemented option %u\n", option
);
145 set_last_error( ERROR_INVALID_PARAMETER
);
150 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
152 session_t
*session
= (session_t
*)hdr
;
156 case WINHTTP_OPTION_PROXY
:
158 WINHTTP_PROXY_INFO
*pi
= buffer
;
160 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
163 case WINHTTP_OPTION_REDIRECT_POLICY
:
167 if (buflen
!= sizeof(policy
))
169 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
173 policy
= *(DWORD
*)buffer
;
174 TRACE("0x%x\n", policy
);
175 hdr
->redirect_policy
= policy
;
178 case WINHTTP_OPTION_DISABLE_FEATURE
:
179 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
181 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
182 session
->resolve_timeout
= *(DWORD
*)buffer
;
184 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
185 session
->connect_timeout
= *(DWORD
*)buffer
;
187 case WINHTTP_OPTION_SEND_TIMEOUT
:
188 session
->send_timeout
= *(DWORD
*)buffer
;
190 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
191 session
->recv_timeout
= *(DWORD
*)buffer
;
193 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH
:
194 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD
*)buffer
);
197 FIXME("unimplemented option %u\n", option
);
198 set_last_error( ERROR_INVALID_PARAMETER
);
203 static const object_vtbl_t session_vtbl
=
206 session_query_option
,
210 /***********************************************************************
211 * WinHttpOpen (winhttp.@)
213 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
216 HINTERNET handle
= NULL
;
219 int error
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
220 if (error
) ERR("WSAStartup failed: %d\n", error
);
223 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
225 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
227 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
228 session
->hdr
.vtbl
= &session_vtbl
;
229 session
->hdr
.flags
= flags
;
230 session
->hdr
.refs
= 1;
231 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
232 list_init( &session
->hdr
.children
);
233 session
->resolve_timeout
= DEFAULT_RESOLVE_TIMEOUT
;
234 session
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
235 session
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
236 session
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
237 list_init( &session
->cookie_cache
);
239 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
240 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
242 WINHTTP_PROXY_INFO info
;
244 WinHttpGetDefaultProxyConfiguration( &info
);
245 session
->access
= info
.dwAccessType
;
246 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
248 GlobalFree( (LPWSTR
)info
.lpszProxy
);
249 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
252 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
254 GlobalFree( (LPWSTR
)info
.lpszProxy
);
255 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
259 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
261 session
->access
= access
;
262 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
263 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
266 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
267 session
->hdr
.handle
= handle
;
270 release_object( &session
->hdr
);
271 TRACE("returning %p\n", handle
);
275 /***********************************************************************
276 * connect_destroy (internal)
278 static void connect_destroy( object_header_t
*hdr
)
280 connect_t
*connect
= (connect_t
*)hdr
;
282 TRACE("%p\n", connect
);
284 release_object( &connect
->session
->hdr
);
286 heap_free( connect
->hostname
);
287 heap_free( connect
->servername
);
288 heap_free( connect
->username
);
289 heap_free( connect
->password
);
290 heap_free( connect
);
293 static BOOL
connect_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
295 connect_t
*connect
= (connect_t
*)hdr
;
299 case WINHTTP_OPTION_PARENT_HANDLE
:
301 if (!buffer
|| *buflen
< sizeof(HINTERNET
))
303 *buflen
= sizeof(HINTERNET
);
304 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
308 *(HINTERNET
*)buffer
= ((object_header_t
*)connect
->session
)->handle
;
309 *buflen
= sizeof(HINTERNET
);
312 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
313 *(DWORD
*)buffer
= connect
->session
->resolve_timeout
;
314 *buflen
= sizeof(DWORD
);
316 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
317 *(DWORD
*)buffer
= connect
->session
->connect_timeout
;
318 *buflen
= sizeof(DWORD
);
320 case WINHTTP_OPTION_SEND_TIMEOUT
:
321 *(DWORD
*)buffer
= connect
->session
->send_timeout
;
322 *buflen
= sizeof(DWORD
);
324 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
325 *(DWORD
*)buffer
= connect
->session
->recv_timeout
;
326 *buflen
= sizeof(DWORD
);
329 FIXME("unimplemented option %u\n", option
);
330 set_last_error( ERROR_INVALID_PARAMETER
);
335 static const object_vtbl_t connect_vtbl
=
338 connect_query_option
,
342 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
344 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
347 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
349 else if (*domain
== '*')
351 if (domain
[1] == '.')
355 /* For a hostname to match a wildcard, the last domain must match
356 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
357 * hostname is www.foo.a.b, it matches, but a.b does not.
359 dot
= strchrW( server
, '.' );
362 int len
= strlenW( dot
+ 1 );
364 if (len
> strlenW( domain
+ 2 ))
368 /* The server's domain is longer than the wildcard, so it
369 * could be a subdomain. Compare the last portion of the
372 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
373 if (!strcmpiW( ptr
, domain
+ 2 ))
375 /* This is only a match if the preceding character is
376 * a '.', i.e. that it is a matching domain. E.g.
377 * if domain is '*.b.c' and server is 'www.ab.c' they
380 ret
= *(ptr
- 1) == '.';
384 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
389 ret
= !strcmpiW( server
, domain
);
393 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
394 #define MAX_HOST_NAME_LENGTH 256
396 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
401 if (!session
->proxy_bypass
) return FALSE
;
402 ptr
= session
->proxy_bypass
;
406 ptr
= strchrW( ptr
, ';' );
408 ptr
= strchrW( tmp
, ' ' );
411 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
413 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
415 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
416 domain
[ptr
- tmp
] = 0;
417 ret
= domain_matches( server
, domain
);
422 ret
= domain_matches( server
, tmp
);
423 } while (ptr
&& !ret
);
427 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
429 session_t
*session
= connect
->session
;
432 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
436 if ((colon
= strchrW( session
->proxy_server
, ':' )))
438 if (!connect
->servername
|| strncmpiW( connect
->servername
,
439 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
441 heap_free( connect
->servername
);
442 connect
->resolved
= FALSE
;
443 if (!(connect
->servername
= heap_alloc(
444 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
449 memcpy( connect
->servername
, session
->proxy_server
,
450 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
451 connect
->servername
[colon
- session
->proxy_server
] = 0;
453 connect
->serverport
= atoiW( colon
+ 1 );
455 connect
->serverport
= INTERNET_DEFAULT_PORT
;
460 if (!connect
->servername
|| strcmpiW( connect
->servername
,
461 session
->proxy_server
))
463 heap_free( connect
->servername
);
464 connect
->resolved
= FALSE
;
465 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
470 connect
->serverport
= INTERNET_DEFAULT_PORT
;
476 heap_free( connect
->servername
);
477 connect
->resolved
= FALSE
;
478 if (!(connect
->servername
= strdupW( server
)))
483 connect
->serverport
= port
;
489 /***********************************************************************
490 * WinHttpConnect (winhttp.@)
492 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
496 HINTERNET hconnect
= NULL
;
498 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
502 set_last_error( ERROR_INVALID_PARAMETER
);
505 if (!(session
= (session_t
*)grab_object( hsession
)))
507 set_last_error( ERROR_INVALID_HANDLE
);
510 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
512 release_object( &session
->hdr
);
513 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
516 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
518 release_object( &session
->hdr
);
521 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
522 connect
->hdr
.vtbl
= &connect_vtbl
;
523 connect
->hdr
.refs
= 1;
524 connect
->hdr
.flags
= session
->hdr
.flags
;
525 connect
->hdr
.callback
= session
->hdr
.callback
;
526 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
527 connect
->hdr
.context
= session
->hdr
.context
;
528 connect
->hdr
.redirect_policy
= session
->hdr
.redirect_policy
;
529 list_init( &connect
->hdr
.children
);
531 addref_object( &session
->hdr
);
532 connect
->session
= session
;
533 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
535 if (!(connect
->hostname
= strdupW( server
))) goto end
;
536 connect
->hostport
= port
;
537 if (!set_server_for_hostname( connect
, server
, port
)) goto end
;
539 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
540 connect
->hdr
.handle
= hconnect
;
542 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
545 release_object( &connect
->hdr
);
546 release_object( &session
->hdr
);
547 TRACE("returning %p\n", hconnect
);
551 /***********************************************************************
552 * request_destroy (internal)
554 static void request_destroy( object_header_t
*hdr
)
556 request_t
*request
= (request_t
*)hdr
;
559 TRACE("%p\n", request
);
561 release_object( &request
->connect
->hdr
);
563 heap_free( request
->verb
);
564 heap_free( request
->path
);
565 heap_free( request
->version
);
566 heap_free( request
->raw_headers
);
567 heap_free( request
->status_text
);
568 for (i
= 0; i
< request
->num_headers
; i
++)
570 heap_free( request
->headers
[i
].field
);
571 heap_free( request
->headers
[i
].value
);
573 heap_free( request
->headers
);
574 for (i
= 0; i
< request
->num_accept_types
; i
++) heap_free( request
->accept_types
[i
] );
575 heap_free( request
->accept_types
);
576 heap_free( request
);
579 static void str_to_buffer( WCHAR
*buffer
, const WCHAR
*str
, LPDWORD buflen
)
582 if (str
) len
= strlenW( str
);
583 if (buffer
&& *buflen
> len
)
585 if (str
) memcpy( buffer
, str
, len
* sizeof(WCHAR
) );
588 *buflen
= len
* sizeof(WCHAR
);
591 static WCHAR
*blob_to_str( DWORD encoding
, CERT_NAME_BLOB
*blob
)
594 DWORD size
, format
= CERT_SIMPLE_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
;
596 size
= CertNameToStrW( encoding
, blob
, format
, NULL
, 0 );
597 if ((ret
= LocalAlloc( 0, size
* sizeof(WCHAR
) )))
598 CertNameToStrW( encoding
, blob
, format
, ret
, size
);
603 static BOOL
convert_sockaddr( const struct sockaddr
*addr
, SOCKADDR_STORAGE
*addr_storage
)
605 #if !defined(__MINGW32__) && !defined(_MSC_VER)
606 switch (addr
->sa_family
)
610 const struct sockaddr_in
*addr_unix
= (const struct sockaddr_in
*)addr
;
611 struct WS_sockaddr_in
*addr_win
= (struct WS_sockaddr_in
*)addr_storage
;
614 addr_win
->sin_family
= WS_AF_INET
;
615 addr_win
->sin_port
= addr_unix
->sin_port
;
616 memcpy( &addr_win
->sin_addr
, &addr_unix
->sin_addr
, 4 );
617 p
= (char *)&addr_win
->sin_addr
+ 4;
618 memset( p
, 0, sizeof(*addr_storage
) - (p
- (char *)addr_win
) );
623 const struct sockaddr_in6
*addr_unix
= (const struct sockaddr_in6
*)addr
;
624 struct WS_sockaddr_in6
*addr_win
= (struct WS_sockaddr_in6
*)addr_storage
;
626 addr_win
->sin6_family
= WS_AF_INET6
;
627 addr_win
->sin6_port
= addr_unix
->sin6_port
;
628 addr_win
->sin6_flowinfo
= addr_unix
->sin6_flowinfo
;
629 memcpy( &addr_win
->sin6_addr
, &addr_unix
->sin6_addr
, 16 );
630 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
631 addr_win
->sin6_scope_id
= addr_unix
->sin6_scope_id
;
633 addr_win
->sin6_scope_id
= 0;
635 memset( addr_win
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_win
) );
639 ERR("unhandled family %u\n", addr
->sa_family
);
643 switch (addr
->sa_family
)
647 struct sockaddr_in
*addr_in
= (struct sockaddr_in
*)addr_storage
;
649 memcpy( addr_in
, addr
, sizeof(*addr_in
) );
650 memset( addr_in
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in
) );
655 struct sockaddr_in6
*addr_in6
= (struct sockaddr_in6
*)addr_storage
;
657 memcpy( addr_in6
, addr
, sizeof(*addr_in6
) );
658 memset( addr_in6
+ 1, 0, sizeof(*addr_storage
) - sizeof(*addr_in6
) );
662 ERR("unhandled family %u\n", addr
->sa_family
);
668 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
670 request_t
*request
= (request_t
*)hdr
;
674 case WINHTTP_OPTION_SECURITY_FLAGS
:
679 if (!buffer
|| *buflen
< sizeof(flags
))
681 *buflen
= sizeof(flags
);
682 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
687 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
688 flags
|= request
->netconn
.security_flags
;
689 bits
= netconn_get_cipher_strength( &request
->netconn
);
691 flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
693 flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
695 flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
696 *(DWORD
*)buffer
= flags
;
697 *buflen
= sizeof(flags
);
700 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
702 const CERT_CONTEXT
*cert
;
704 if (!buffer
|| *buflen
< sizeof(cert
))
706 *buflen
= sizeof(cert
);
707 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
711 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
712 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
713 *buflen
= sizeof(cert
);
716 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT
:
718 const CERT_CONTEXT
*cert
;
719 const CRYPT_OID_INFO
*oidInfo
;
720 WINHTTP_CERTIFICATE_INFO
*ci
= buffer
;
722 FIXME("partial stub\n");
724 if (!buffer
|| *buflen
< sizeof(*ci
))
726 *buflen
= sizeof(*ci
);
727 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
730 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
732 ci
->ftExpiry
= cert
->pCertInfo
->NotAfter
;
733 ci
->ftStart
= cert
->pCertInfo
->NotBefore
;
734 ci
->lpszSubjectInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
);
735 ci
->lpszIssuerInfo
= blob_to_str( cert
->dwCertEncodingType
, &cert
->pCertInfo
->Issuer
);
736 ci
->lpszProtocolName
= NULL
;
737 oidInfo
= CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY
,
738 cert
->pCertInfo
->SignatureAlgorithm
.pszObjId
,
741 ci
->lpszSignatureAlgName
= (LPWSTR
)oidInfo
->pwszName
;
743 ci
->lpszSignatureAlgName
= NULL
;
744 ci
->lpszEncryptionAlgName
= NULL
;
745 ci
->dwKeySize
= netconn_get_cipher_strength( &request
->netconn
);
747 CertFreeCertificateContext( cert
);
748 *buflen
= sizeof(*ci
);
751 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
753 if (!buffer
|| *buflen
< sizeof(DWORD
))
755 *buflen
= sizeof(DWORD
);
756 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
760 *(DWORD
*)buffer
= netconn_get_cipher_strength( &request
->netconn
);
761 *buflen
= sizeof(DWORD
);
764 case WINHTTP_OPTION_CONNECTION_INFO
:
766 WINHTTP_CONNECTION_INFO
*info
= buffer
;
767 struct sockaddr local
;
768 socklen_t len
= sizeof(local
);
769 const struct sockaddr
*remote
= (const struct sockaddr
*)&request
->connect
->sockaddr
;
771 if (!buffer
|| *buflen
< sizeof(*info
))
773 *buflen
= sizeof(*info
);
774 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
777 if (!netconn_connected( &request
->netconn
))
779 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE
);
782 if (getsockname( request
->netconn
.socket
, &local
, &len
)) return FALSE
;
783 if (!convert_sockaddr( &local
, &info
->LocalAddress
)) return FALSE
;
784 if (!convert_sockaddr( remote
, &info
->RemoteAddress
)) return FALSE
;
785 info
->cbSize
= sizeof(*info
);
788 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
789 *(DWORD
*)buffer
= request
->resolve_timeout
;
790 *buflen
= sizeof(DWORD
);
792 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
793 *(DWORD
*)buffer
= request
->connect_timeout
;
794 *buflen
= sizeof(DWORD
);
796 case WINHTTP_OPTION_SEND_TIMEOUT
:
797 *(DWORD
*)buffer
= request
->send_timeout
;
798 *buflen
= sizeof(DWORD
);
800 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
801 *(DWORD
*)buffer
= request
->recv_timeout
;
802 *buflen
= sizeof(DWORD
);
805 case WINHTTP_OPTION_USERNAME
:
806 str_to_buffer( buffer
, request
->connect
->username
, buflen
);
809 case WINHTTP_OPTION_PASSWORD
:
810 str_to_buffer( buffer
, request
->connect
->password
, buflen
);
813 case WINHTTP_OPTION_PROXY_USERNAME
:
814 str_to_buffer( buffer
, request
->connect
->session
->proxy_username
, buflen
);
817 case WINHTTP_OPTION_PROXY_PASSWORD
:
818 str_to_buffer( buffer
, request
->connect
->session
->proxy_password
, buflen
);
822 FIXME("unimplemented option %u\n", option
);
823 set_last_error( ERROR_INVALID_PARAMETER
);
828 static WCHAR
*buffer_to_str( WCHAR
*buffer
, DWORD buflen
)
831 if ((ret
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
))))
833 memcpy( ret
, buffer
, buflen
* sizeof(WCHAR
) );
837 set_last_error( ERROR_OUTOFMEMORY
);
841 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
843 request_t
*request
= (request_t
*)hdr
;
847 case WINHTTP_OPTION_PROXY
:
849 WINHTTP_PROXY_INFO
*pi
= buffer
;
851 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
854 case WINHTTP_OPTION_DISABLE_FEATURE
:
858 if (buflen
!= sizeof(DWORD
))
860 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
864 disable
= *(DWORD
*)buffer
;
865 TRACE("0x%x\n", disable
);
866 hdr
->disable_flags
|= disable
;
869 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
873 if (buflen
!= sizeof(DWORD
))
875 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
879 policy
= *(DWORD
*)buffer
;
880 TRACE("0x%x\n", policy
);
881 hdr
->logon_policy
= policy
;
884 case WINHTTP_OPTION_REDIRECT_POLICY
:
888 if (buflen
!= sizeof(DWORD
))
890 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
894 policy
= *(DWORD
*)buffer
;
895 TRACE("0x%x\n", policy
);
896 hdr
->redirect_policy
= policy
;
899 case WINHTTP_OPTION_SECURITY_FLAGS
:
903 if (buflen
< sizeof(DWORD
))
905 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
908 flags
= *(DWORD
*)buffer
;
909 TRACE("0x%x\n", flags
);
910 if (!(flags
& (SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
911 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
912 SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
913 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
)))
915 set_last_error( ERROR_INVALID_PARAMETER
);
918 request
->netconn
.security_flags
= flags
;
921 case WINHTTP_OPTION_RESOLVE_TIMEOUT
:
922 request
->resolve_timeout
= *(DWORD
*)buffer
;
924 case WINHTTP_OPTION_CONNECT_TIMEOUT
:
925 request
->connect_timeout
= *(DWORD
*)buffer
;
927 case WINHTTP_OPTION_SEND_TIMEOUT
:
928 request
->send_timeout
= *(DWORD
*)buffer
;
930 case WINHTTP_OPTION_RECEIVE_TIMEOUT
:
931 request
->recv_timeout
= *(DWORD
*)buffer
;
934 case WINHTTP_OPTION_USERNAME
:
936 connect_t
*connect
= request
->connect
;
938 heap_free( connect
->username
);
939 if (!(connect
->username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
942 case WINHTTP_OPTION_PASSWORD
:
944 connect_t
*connect
= request
->connect
;
946 heap_free( connect
->password
);
947 if (!(connect
->password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
950 case WINHTTP_OPTION_PROXY_USERNAME
:
952 session_t
*session
= request
->connect
->session
;
954 heap_free( session
->proxy_username
);
955 if (!(session
->proxy_username
= buffer_to_str( buffer
, buflen
))) return FALSE
;
958 case WINHTTP_OPTION_PROXY_PASSWORD
:
960 session_t
*session
= request
->connect
->session
;
962 heap_free( session
->proxy_password
);
963 if (!(session
->proxy_password
= buffer_to_str( buffer
, buflen
))) return FALSE
;
967 FIXME("unimplemented option %u\n", option
);
968 set_last_error( ERROR_INVALID_PARAMETER
);
973 static const object_vtbl_t request_vtbl
=
976 request_query_option
,
980 static BOOL
store_accept_types( request_t
*request
, const WCHAR
**accept_types
)
982 const WCHAR
**types
= accept_types
;
985 if (!types
) return TRUE
;
988 request
->num_accept_types
++;
991 if (!request
->num_accept_types
) return TRUE
;
992 if (!(request
->accept_types
= heap_alloc( request
->num_accept_types
* sizeof(WCHAR
*))))
994 request
->num_accept_types
= 0;
997 types
= accept_types
;
998 for (i
= 0; i
< request
->num_accept_types
; i
++)
1000 if (!(request
->accept_types
[i
] = strdupW( *types
)))
1002 for ( ; i
> 0; --i
) heap_free( request
->accept_types
[i
- 1] );
1003 heap_free( request
->accept_types
);
1004 request
->accept_types
= NULL
;
1005 request
->num_accept_types
= 0;
1013 /***********************************************************************
1014 * WinHttpOpenRequest (winhttp.@)
1016 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
1017 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
1021 HINTERNET hrequest
= NULL
;
1023 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
1024 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
1026 if(types
&& TRACE_ON(winhttp
)) {
1029 TRACE("accept types:\n");
1030 for(iter
= types
; *iter
; iter
++)
1031 TRACE(" %s\n", debugstr_w(*iter
));
1034 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
1036 set_last_error( ERROR_INVALID_HANDLE
);
1039 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
1041 release_object( &connect
->hdr
);
1042 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1045 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
1047 release_object( &connect
->hdr
);
1050 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
1051 request
->hdr
.vtbl
= &request_vtbl
;
1052 request
->hdr
.refs
= 1;
1053 request
->hdr
.flags
= flags
;
1054 request
->hdr
.callback
= connect
->hdr
.callback
;
1055 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
1056 request
->hdr
.context
= connect
->hdr
.context
;
1057 request
->hdr
.redirect_policy
= connect
->hdr
.redirect_policy
;
1058 list_init( &request
->hdr
.children
);
1060 addref_object( &connect
->hdr
);
1061 request
->connect
= connect
;
1062 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
1064 if (!netconn_init( &request
->netconn
)) goto end
;
1065 request
->resolve_timeout
= connect
->session
->resolve_timeout
;
1066 request
->connect_timeout
= connect
->session
->connect_timeout
;
1067 request
->send_timeout
= connect
->session
->send_timeout
;
1068 request
->recv_timeout
= connect
->session
->recv_timeout
;
1070 if (!verb
|| !verb
[0]) verb
= getW
;
1071 if (!(request
->verb
= strdupW( verb
))) goto end
;
1078 len
= strlenW( object
) + 1;
1079 if (object
[0] != '/') len
++;
1080 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
1082 if (object
[0] != '/') *p
++ = '/';
1083 strcpyW( p
, object
);
1084 request
->path
= path
;
1086 else if (!(request
->path
= strdupW( slashW
))) goto end
;
1088 if (!version
|| !version
[0]) version
= http1_1
;
1089 if (!(request
->version
= strdupW( version
))) goto end
;
1090 if (!(store_accept_types( request
, types
))) goto end
;
1092 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
1093 request
->hdr
.handle
= hrequest
;
1095 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
1098 release_object( &request
->hdr
);
1099 release_object( &connect
->hdr
);
1100 TRACE("returning %p\n", hrequest
);
1104 /***********************************************************************
1105 * WinHttpCloseHandle (winhttp.@)
1107 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
1109 object_header_t
*hdr
;
1111 TRACE("%p\n", handle
);
1113 if (!(hdr
= grab_object( handle
)))
1115 set_last_error( ERROR_INVALID_HANDLE
);
1118 release_object( hdr
);
1119 free_handle( handle
);
1123 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1129 set_last_error( ERROR_INVALID_PARAMETER
);
1135 case WINHTTP_OPTION_CONTEXT_VALUE
:
1137 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
1139 *buflen
= sizeof(DWORD_PTR
);
1140 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1144 *(DWORD_PTR
*)buffer
= hdr
->context
;
1145 *buflen
= sizeof(DWORD_PTR
);
1149 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
1152 FIXME("unimplemented option %u\n", option
);
1153 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1161 /***********************************************************************
1162 * WinHttpQueryOption (winhttp.@)
1164 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
1167 object_header_t
*hdr
;
1169 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
1171 if (!(hdr
= grab_object( handle
)))
1173 set_last_error( ERROR_INVALID_HANDLE
);
1177 ret
= query_option( hdr
, option
, buffer
, buflen
);
1179 release_object( hdr
);
1183 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1189 set_last_error( ERROR_INVALID_PARAMETER
);
1195 case WINHTTP_OPTION_CONTEXT_VALUE
:
1197 if (buflen
!= sizeof(DWORD_PTR
))
1199 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
1203 hdr
->context
= *(DWORD_PTR
*)buffer
;
1207 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
1210 FIXME("unimplemented option %u\n", option
);
1211 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1219 /***********************************************************************
1220 * WinHttpSetOption (winhttp.@)
1222 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
1225 object_header_t
*hdr
;
1227 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
1229 if (!(hdr
= grab_object( handle
)))
1231 set_last_error( ERROR_INVALID_HANDLE
);
1235 ret
= set_option( hdr
, option
, buffer
, buflen
);
1237 release_object( hdr
);
1241 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
1246 GetComputerNameExA( format
, NULL
, &size
);
1247 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
1248 if (!(ret
= heap_alloc( size
))) return NULL
;
1249 if (!GetComputerNameExA( format
, ret
, &size
))
1257 static BOOL
is_domain_suffix( const char *domain
, const char *suffix
)
1259 int len_domain
= strlen( domain
), len_suffix
= strlen( suffix
);
1261 if (len_suffix
> len_domain
) return FALSE
;
1262 if (!strcasecmp( domain
+ len_domain
- len_suffix
, suffix
)) return TRUE
;
1266 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
1269 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
1270 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
1271 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
1272 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
1275 static int reverse_lookup( const struct addrinfo
*ai
, char *hostname
, size_t len
)
1278 #ifdef HAVE_GETNAMEINFO
1279 ret
= getnameinfo( ai
->ai_addr
, ai
->ai_addrlen
, hostname
, len
, NULL
, 0, 0 );
1284 static WCHAR
*build_wpad_url( const char *hostname
, const struct addrinfo
*ai
)
1286 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1287 static const WCHAR wpadW
[] = {'/','w','p','a','d','.','d','a','t',0};
1288 char name
[NI_MAXHOST
];
1292 while (ai
&& ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
) ai
= ai
->ai_next
;
1293 if (!ai
) return NULL
;
1295 if (!reverse_lookup( ai
, name
, sizeof(name
) )) hostname
= name
;
1297 len
= strlenW( httpW
) + strlen( hostname
) + strlenW( wpadW
);
1298 if (!(ret
= p
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1299 strcpyW( p
, httpW
);
1300 p
+= strlenW( httpW
);
1301 while (*hostname
) { *p
++ = *hostname
++; }
1302 strcpyW( p
, wpadW
);
1306 /***********************************************************************
1307 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1309 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
1313 TRACE("0x%08x, %p\n", flags
, url
);
1317 set_last_error( ERROR_INVALID_PARAMETER
);
1320 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DHCP
)
1322 static int fixme_shown
;
1323 if (!fixme_shown
++) FIXME("discovery via DHCP not supported\n");
1325 if (flags
& WINHTTP_AUTO_DETECT_TYPE_DNS_A
)
1327 #ifdef HAVE_GETADDRINFO
1328 char *fqdn
, *domain
, *p
;
1330 if (!(fqdn
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
))) return FALSE
;
1331 if (!(domain
= get_computer_name( ComputerNamePhysicalDnsDomain
)))
1337 while ((p
= strchr( p
, '.' )) && is_domain_suffix( p
+ 1, domain
))
1339 struct addrinfo
*ai
;
1343 if (!(name
= heap_alloc( sizeof("wpad") + strlen(p
) )))
1346 heap_free( domain
);
1349 strcpy( name
, "wpad" );
1351 res
= getaddrinfo( name
, NULL
, NULL
, &ai
);
1354 *url
= build_wpad_url( name
, ai
);
1358 TRACE("returning %s\n", debugstr_w(*url
));
1367 heap_free( domain
);
1370 FIXME("getaddrinfo not found at build time\n");
1373 if (!ret
) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
1377 static const WCHAR Connections
[] = {
1378 'S','o','f','t','w','a','r','e','\\',
1379 'M','i','c','r','o','s','o','f','t','\\',
1380 'W','i','n','d','o','w','s','\\',
1381 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1382 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1383 'C','o','n','n','e','c','t','i','o','n','s',0 };
1384 static const WCHAR WinHttpSettings
[] = {
1385 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1386 static const DWORD WINHTTP_SETTINGS_MAGIC
= 0x18;
1387 static const DWORD WININET_SETTINGS_MAGIC
= 0x46;
1388 static const DWORD PROXY_TYPE_DIRECT
= 1;
1389 static const DWORD PROXY_TYPE_PROXY
= 2;
1390 static const DWORD PROXY_USE_PAC_SCRIPT
= 4;
1391 static const DWORD PROXY_AUTODETECT_SETTINGS
= 8;
1393 struct connection_settings_header
1396 DWORD unknown
; /* always zero? */
1397 DWORD flags
; /* one or more of PROXY_* */
1400 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
1404 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
1409 /***********************************************************************
1410 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1412 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1416 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
1419 TRACE("%p\n", info
);
1421 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
1424 DWORD type
, size
= 0;
1426 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
1427 if (!l
&& type
== REG_BINARY
&&
1428 size
>= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
))
1430 BYTE
*buf
= heap_alloc( size
);
1434 struct connection_settings_header
*hdr
=
1435 (struct connection_settings_header
*)buf
;
1436 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1438 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
1440 if (!l
&& hdr
->magic
== WINHTTP_SETTINGS_MAGIC
&&
1443 if (hdr
->flags
& PROXY_TYPE_PROXY
)
1446 LPWSTR proxy
= NULL
;
1447 LPWSTR proxy_bypass
= NULL
;
1449 /* Sanity-check length of proxy string */
1450 if ((BYTE
*)len
- buf
+ *len
<= size
)
1453 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1455 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
1456 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
1460 /* Sanity-check length of proxy bypass string */
1461 if ((BYTE
*)len
- buf
+ *len
<= size
)
1463 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
1465 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
1470 GlobalFree( proxy
);
1474 info
->lpszProxy
= proxy
;
1475 info
->lpszProxyBypass
= proxy_bypass
;
1478 got_from_reg
= TRUE
;
1480 info
->dwAccessType
=
1481 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1482 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1483 debugstr_w(info
->lpszProxy
),
1484 debugstr_w(info
->lpszProxyBypass
));
1493 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
1495 char *colon
, *http_proxy
;
1497 if ((colon
= strchr( envproxy
, ':' )))
1499 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
1501 static const char http
[] = "http://";
1503 /* It's a scheme, check that it's http */
1504 if (!strncmp( envproxy
, http
, strlen( http
) ))
1505 http_proxy
= envproxy
+ strlen( http
);
1508 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
1513 http_proxy
= envproxy
;
1516 http_proxy
= envproxy
;
1522 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
1523 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
1525 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
1527 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1528 info
->lpszProxy
= http_proxyW
;
1529 info
->lpszProxyBypass
= NULL
;
1530 TRACE("http proxy (from environment) = %s\n",
1531 debugstr_w(info
->lpszProxy
));
1537 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1538 info
->lpszProxy
= NULL
;
1539 info
->lpszProxyBypass
= NULL
;
1544 /***********************************************************************
1545 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1547 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
1549 static const WCHAR settingsW
[] =
1550 {'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};
1552 struct connection_settings_header
*hdr
= NULL
;
1553 DWORD type
, offset
, len
, size
= 0;
1556 TRACE("%p\n", config
);
1560 set_last_error( ERROR_INVALID_PARAMETER
);
1563 memset( config
, 0, sizeof(*config
) );
1564 config
->fAutoDetect
= TRUE
;
1566 if (RegOpenKeyExW( HKEY_CURRENT_USER
, Connections
, 0, KEY_READ
, &hkey
) ||
1567 RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, NULL
, &size
) ||
1568 type
!= REG_BINARY
|| size
< sizeof(struct connection_settings_header
))
1573 if (!(hdr
= heap_alloc( size
))) goto done
;
1574 if (RegQueryValueExW( hkey
, settingsW
, NULL
, &type
, (BYTE
*)hdr
, &size
) ||
1575 hdr
->magic
!= WININET_SETTINGS_MAGIC
)
1581 config
->fAutoDetect
= (hdr
->flags
& PROXY_AUTODETECT_SETTINGS
) != 0;
1582 offset
= sizeof(*hdr
);
1583 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1584 len
= *(DWORD
*)((char *)hdr
+ offset
);
1585 offset
+= sizeof(DWORD
);
1586 if (len
&& hdr
->flags
& PROXY_TYPE_PROXY
)
1588 if (!(config
->lpszProxy
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1589 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxy
);
1592 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1593 len
= *(DWORD
*)((char *)hdr
+ offset
);
1594 offset
+= sizeof(DWORD
);
1595 if (len
&& (hdr
->flags
& PROXY_TYPE_PROXY
))
1597 if (!(config
->lpszProxyBypass
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1598 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszProxyBypass
);
1601 if (offset
+ sizeof(DWORD
) > size
) goto done
;
1602 len
= *(DWORD
*)((char *)hdr
+ offset
);
1603 offset
+= sizeof(DWORD
);
1604 if (len
&& (hdr
->flags
& PROXY_USE_PAC_SCRIPT
))
1606 if (!(config
->lpszAutoConfigUrl
= GlobalAlloc( 0, (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
1607 copy_char_to_wchar_sz( (const BYTE
*)hdr
+ offset
, len
, config
->lpszAutoConfigUrl
);
1612 RegCloseKey( hkey
);
1616 heap_free( config
->lpszAutoConfigUrl
);
1617 config
->lpszAutoConfigUrl
= NULL
;
1618 heap_free( config
->lpszProxy
);
1619 config
->lpszProxy
= NULL
;
1620 heap_free( config
->lpszProxyBypass
);
1621 config
->lpszProxyBypass
= NULL
;
1626 static HRESULT WINAPI
dispex_QueryInterface(
1627 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1631 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
1632 IsEqualGUID( riid
, &IID_IDispatch
) ||
1633 IsEqualGUID( riid
, &IID_IDispatchEx
))
1636 return E_NOINTERFACE
;
1641 static ULONG WINAPI
dispex_AddRef(
1642 IDispatchEx
*iface
)
1647 static ULONG WINAPI
dispex_Release(
1648 IDispatchEx
*iface
)
1653 static HRESULT WINAPI
dispex_GetTypeInfoCount(
1654 IDispatchEx
*iface
, UINT
*info
)
1659 static HRESULT WINAPI
dispex_GetTypeInfo(
1660 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
1665 static HRESULT WINAPI
dispex_GetIDsOfNames(
1666 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
1671 static HRESULT WINAPI
dispex_Invoke(
1672 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
1673 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
1678 static HRESULT WINAPI
dispex_DeleteMemberByName(
1679 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
1684 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
1685 IDispatchEx
*iface
, DISPID id
)
1690 static HRESULT WINAPI
dispex_GetMemberProperties(
1691 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
1696 static HRESULT WINAPI
dispex_GetMemberName(
1697 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
1702 static HRESULT WINAPI
dispex_GetNextDispID(
1703 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
1708 static HRESULT WINAPI
dispex_GetNameSpaceParent(
1709 IDispatchEx
*iface
, IUnknown
**unk
)
1714 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1716 static HRESULT WINAPI
dispex_GetDispID(
1717 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
1719 if (!strcmpW( name
, dns_resolveW
))
1721 *id
= DISPID_GLOBAL_DNSRESOLVE
;
1724 return DISP_E_UNKNOWNNAME
;
1727 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
1729 #ifdef HAVE_GETADDRINFO
1730 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1732 struct addrinfo
*ai
, *elem
;
1737 hostnameA
= strdupWA( hostname
);
1739 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
1741 if (!hostnameA
) return E_OUTOFMEMORY
;
1742 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
1743 heap_free( hostnameA
);
1744 if (res
) return S_FALSE
;
1747 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
1753 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
1755 V_VT( result
) = VT_BSTR
;
1756 V_BSTR( result
) = SysAllocString( addr
);
1759 FIXME("getaddrinfo not found at build time\n");
1764 static HRESULT WINAPI
dispex_InvokeEx(
1765 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
1766 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
1768 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
1770 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
1771 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
1772 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
1774 return DISP_E_MEMBERNOTFOUND
;
1777 static const IDispatchExVtbl dispex_vtbl
=
1779 dispex_QueryInterface
,
1782 dispex_GetTypeInfoCount
,
1784 dispex_GetIDsOfNames
,
1788 dispex_DeleteMemberByName
,
1789 dispex_DeleteMemberByDispID
,
1790 dispex_GetMemberProperties
,
1791 dispex_GetMemberName
,
1792 dispex_GetNextDispID
,
1793 dispex_GetNameSpaceParent
1796 static IDispatchEx global_dispex
= { &dispex_vtbl
};
1798 static HRESULT WINAPI
site_QueryInterface(
1799 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
1803 if (IsEqualGUID( &IID_IUnknown
, riid
))
1805 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
1808 return E_NOINTERFACE
;
1810 IUnknown_AddRef( (IUnknown
*)*ppv
);
1814 static ULONG WINAPI
site_AddRef(
1815 IActiveScriptSite
*iface
)
1820 static ULONG WINAPI
site_Release(
1821 IActiveScriptSite
*iface
)
1826 static HRESULT WINAPI
site_GetLCID(
1827 IActiveScriptSite
*iface
, LCID
*lcid
)
1832 static HRESULT WINAPI
site_GetItemInfo(
1833 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
1834 IUnknown
**item
, ITypeInfo
**type_info
)
1836 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
1838 *item
= (IUnknown
*)&global_dispex
;
1844 static HRESULT WINAPI
site_GetDocVersionString(
1845 IActiveScriptSite
*iface
, BSTR
*version
)
1850 static HRESULT WINAPI
site_OnScriptTerminate(
1851 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
1856 static HRESULT WINAPI
site_OnStateChange(
1857 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
1862 static HRESULT WINAPI
site_OnScriptError(
1863 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
1868 static HRESULT WINAPI
site_OnEnterScript(
1869 IActiveScriptSite
*iface
)
1874 static HRESULT WINAPI
site_OnLeaveScript(
1875 IActiveScriptSite
*iface
)
1880 static const IActiveScriptSiteVtbl site_vtbl
=
1882 site_QueryInterface
,
1887 site_GetDocVersionString
,
1888 site_OnScriptTerminate
,
1895 static IActiveScriptSite script_site
= { &site_vtbl
};
1897 static BOOL
parse_script_result( VARIANT result
, WINHTTP_PROXY_INFO
*info
)
1899 static const WCHAR proxyW
[] = {'P','R','O','X','Y'};
1904 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
1905 info
->lpszProxy
= NULL
;
1906 info
->lpszProxyBypass
= NULL
;
1908 if (V_VT( &result
) != VT_BSTR
) return TRUE
;
1909 TRACE("%s\n", debugstr_w( V_BSTR( &result
) ));
1911 p
= V_BSTR( &result
);
1912 while (*p
== ' ') p
++;
1914 if (len
>= 5 && !memicmpW( p
, proxyW
, sizeof(proxyW
)/sizeof(WCHAR
) ))
1917 while (*p
== ' ') p
++;
1918 if (!*p
|| *p
== ';') return TRUE
;
1919 if (!(info
->lpszProxy
= q
= strdupW( p
))) return FALSE
;
1920 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
1923 if (*q
== ' ' || *q
== ';')
1933 static BSTR
include_pac_utils( BSTR script
)
1935 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
1936 HMODULE hmod
= GetModuleHandleA( "winhttp.dll" );
1943 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
1944 size
= SizeofResource( hmod
, rsrc
);
1945 data
= LoadResource( hmod
, rsrc
);
1947 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
1948 if (!(ret
= SysAllocStringLen( NULL
, len
+ SysStringLen( script
) + 1 ))) return NULL
;
1949 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
1951 strcatW( ret
, script
);
1956 #define IActiveScriptParse_Release IActiveScriptParse64_Release
1957 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
1958 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
1960 #define IActiveScriptParse_Release IActiveScriptParse32_Release
1961 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
1962 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
1965 static BOOL
run_script( const BSTR script
, const WCHAR
*url
, WINHTTP_PROXY_INFO
*info
)
1967 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
1968 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1969 IActiveScriptParse
*parser
= NULL
;
1970 IActiveScript
*engine
= NULL
;
1971 IDispatch
*dispatch
= NULL
;
1975 BSTR func
= NULL
, hostname
= NULL
, full_script
= NULL
;
1977 VARIANT args
[2], result
;
1981 memset( &uc
, 0, sizeof(uc
) );
1982 uc
.dwStructSize
= sizeof(uc
);
1983 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return FALSE
;
1984 if (!(hostname
= SysAllocStringLen( NULL
, uc
.dwHostNameLength
+ 1 ))) return FALSE
;
1985 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
1986 hostname
[uc
.dwHostNameLength
] = 0;
1988 init
= CoInitialize( NULL
);
1989 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
1990 if (hr
!= S_OK
) goto done
;
1992 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
1993 &IID_IActiveScript
, (void **)&engine
);
1994 if (hr
!= S_OK
) goto done
;
1996 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
1997 if (hr
!= S_OK
) goto done
;
1999 hr
= IActiveScriptParse_InitNew( parser
);
2000 if (hr
!= S_OK
) goto done
;
2002 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
2003 if (hr
!= S_OK
) goto done
;
2005 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
2006 if (hr
!= S_OK
) goto done
;
2008 if (!(full_script
= include_pac_utils( script
))) goto done
;
2010 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
2011 if (hr
!= S_OK
) goto done
;
2013 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
2014 if (hr
!= S_OK
) goto done
;
2016 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
2017 if (hr
!= S_OK
) goto done
;
2019 if (!(func
= SysAllocString( findproxyW
))) goto done
;
2020 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
2021 if (hr
!= S_OK
) goto done
;
2023 V_VT( &args
[0] ) = VT_BSTR
;
2024 V_BSTR( &args
[0] ) = hostname
;
2025 V_VT( &args
[1] ) = VT_BSTR
;
2026 V_BSTR( &args
[1] ) = SysAllocString( url
);
2028 params
.rgvarg
= args
;
2029 params
.rgdispidNamedArgs
= NULL
;
2031 params
.cNamedArgs
= 0;
2032 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
2033 ¶ms
, &result
, NULL
, NULL
);
2034 VariantClear( &args
[1] );
2037 WARN("script failed 0x%08x\n", hr
);
2040 ret
= parse_script_result( result
, info
);
2043 SysFreeString( full_script
);
2044 SysFreeString( hostname
);
2045 SysFreeString( func
);
2046 if (dispatch
) IDispatch_Release( dispatch
);
2047 if (parser
) IActiveScriptParse_Release( parser
);
2048 if (engine
) IActiveScript_Release( engine
);
2049 if (SUCCEEDED( init
)) CoUninitialize();
2050 if (!ret
) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT
);
2054 static BSTR
download_script( const WCHAR
*url
)
2056 static const WCHAR typeW
[] = {'*','/','*',0};
2057 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
2058 HINTERNET ses
, con
= NULL
, req
= NULL
;
2061 DWORD status
, size
= sizeof(status
), offset
, to_read
, bytes_read
, flags
= 0;
2062 char *tmp
, *buffer
= NULL
;
2066 memset( &uc
, 0, sizeof(uc
) );
2067 uc
.dwStructSize
= sizeof(uc
);
2068 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return NULL
;
2069 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) return NULL
;
2070 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
2071 hostname
[uc
.dwHostNameLength
] = 0;
2073 if (!(ses
= WinHttpOpen( NULL
, WINHTTP_ACCESS_TYPE_NO_PROXY
, NULL
, NULL
, 0 ))) goto done
;
2074 if (!(con
= WinHttpConnect( ses
, hostname
, uc
.nPort
, 0 ))) goto done
;
2075 if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
) flags
|= WINHTTP_FLAG_SECURE
;
2076 if (!(req
= WinHttpOpenRequest( con
, NULL
, uc
.lpszUrlPath
, NULL
, NULL
, acceptW
, flags
))) goto done
;
2077 if (!WinHttpSendRequest( req
, NULL
, 0, NULL
, 0, 0, 0 )) goto done
;
2079 if (!(WinHttpReceiveResponse( req
, 0 ))) goto done
;
2080 if (!WinHttpQueryHeaders( req
, WINHTTP_QUERY_STATUS_CODE
|WINHTTP_QUERY_FLAG_NUMBER
, NULL
, &status
,
2081 &size
, NULL
) || status
!= HTTP_STATUS_OK
) goto done
;
2084 if (!(buffer
= heap_alloc( size
))) goto done
;
2089 if (!WinHttpReadData( req
, buffer
+ offset
, to_read
, &bytes_read
)) goto done
;
2090 if (!bytes_read
) break;
2091 to_read
-= bytes_read
;
2092 offset
+= bytes_read
;
2097 if (!(tmp
= heap_realloc( buffer
, size
))) goto done
;
2101 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, NULL
, 0 );
2102 if (!(script
= SysAllocStringLen( NULL
, len
))) goto done
;
2103 MultiByteToWideChar( CP_ACP
, 0, buffer
, offset
, script
, len
);
2107 WinHttpCloseHandle( req
);
2108 WinHttpCloseHandle( con
);
2109 WinHttpCloseHandle( ses
);
2110 heap_free( buffer
);
2111 heap_free( hostname
);
2112 if (!script
) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT
);
2116 /***********************************************************************
2117 * WinHttpGetProxyForUrl (winhttp.@)
2119 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
2120 WINHTTP_PROXY_INFO
*info
)
2122 WCHAR
*detected_pac_url
= NULL
;
2123 const WCHAR
*pac_url
;
2128 TRACE("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
2130 if (!(session
= (session_t
*)grab_object( hsession
)))
2132 set_last_error( ERROR_INVALID_HANDLE
);
2135 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
2137 release_object( &session
->hdr
);
2138 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2141 if (!url
|| !options
|| !info
||
2142 !(options
->dwFlags
& (WINHTTP_AUTOPROXY_AUTO_DETECT
|WINHTTP_AUTOPROXY_CONFIG_URL
)) ||
2143 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) && !options
->dwAutoDetectFlags
) ||
2144 ((options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
) &&
2145 (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
)))
2147 release_object( &session
->hdr
);
2148 set_last_error( ERROR_INVALID_PARAMETER
);
2151 if (options
->dwFlags
& WINHTTP_AUTOPROXY_AUTO_DETECT
&&
2152 !WinHttpDetectAutoProxyConfigUrl( options
->dwAutoDetectFlags
, &detected_pac_url
))
2155 if (options
->dwFlags
& WINHTTP_AUTOPROXY_CONFIG_URL
) pac_url
= options
->lpszAutoConfigUrl
;
2156 else pac_url
= detected_pac_url
;
2158 if (!(script
= download_script( pac_url
))) goto done
;
2159 ret
= run_script( script
, url
, info
);
2160 SysFreeString( script
);
2163 GlobalFree( detected_pac_url
);
2164 release_object( &session
->hdr
);
2168 /***********************************************************************
2169 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2171 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
2178 TRACE("%p\n", info
);
2182 set_last_error( ERROR_INVALID_PARAMETER
);
2185 switch (info
->dwAccessType
)
2187 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
2189 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
2190 if (!info
->lpszProxy
)
2192 set_last_error( ERROR_INVALID_PARAMETER
);
2195 /* Only ASCII characters are allowed */
2196 for (src
= info
->lpszProxy
; *src
; src
++)
2199 set_last_error( ERROR_INVALID_PARAMETER
);
2202 if (info
->lpszProxyBypass
)
2204 for (src
= info
->lpszProxyBypass
; *src
; src
++)
2207 set_last_error( ERROR_INVALID_PARAMETER
);
2213 set_last_error( ERROR_INVALID_PARAMETER
);
2217 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
2218 KEY_WRITE
, NULL
, &key
, NULL
);
2221 DWORD size
= sizeof(struct connection_settings_header
) + 2 * sizeof(DWORD
);
2224 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2226 size
+= strlenW( info
->lpszProxy
);
2227 if (info
->lpszProxyBypass
)
2228 size
+= strlenW( info
->lpszProxyBypass
);
2230 buf
= heap_alloc( size
);
2233 struct connection_settings_header
*hdr
=
2234 (struct connection_settings_header
*)buf
;
2235 DWORD
*len
= (DWORD
*)(hdr
+ 1);
2237 hdr
->magic
= WINHTTP_SETTINGS_MAGIC
;
2239 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
2243 hdr
->flags
= PROXY_TYPE_PROXY
;
2244 *len
++ = strlenW( info
->lpszProxy
);
2245 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
2249 if (info
->lpszProxyBypass
)
2251 *len
++ = strlenW( info
->lpszProxyBypass
);
2252 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
2261 hdr
->flags
= PROXY_TYPE_DIRECT
;
2265 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
2275 /***********************************************************************
2276 * WinHttpSetStatusCallback (winhttp.@)
2278 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
2279 DWORD flags
, DWORD_PTR reserved
)
2281 object_header_t
*hdr
;
2282 WINHTTP_STATUS_CALLBACK ret
;
2284 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
2286 if (!(hdr
= grab_object( handle
)))
2288 set_last_error( ERROR_INVALID_HANDLE
);
2289 return WINHTTP_INVALID_STATUS_CALLBACK
;
2291 ret
= hdr
->callback
;
2292 hdr
->callback
= callback
;
2293 hdr
->notify_mask
= flags
;
2295 release_object( hdr
);
2299 /***********************************************************************
2300 * WinHttpSetTimeouts (winhttp.@)
2302 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
2305 object_header_t
*hdr
;
2309 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
2311 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
2313 set_last_error( ERROR_INVALID_PARAMETER
);
2317 if (!(hdr
= grab_object( handle
)))
2319 set_last_error( ERROR_INVALID_HANDLE
);
2325 case WINHTTP_HANDLE_TYPE_REQUEST
:
2326 request
= (request_t
*)hdr
;
2327 request
->connect_timeout
= connect
;
2329 if (resolve
< 0) resolve
= 0;
2330 request
->resolve_timeout
= resolve
;
2332 if (send
< 0) send
= 0;
2333 request
->send_timeout
= send
;
2335 if (receive
< 0) receive
= 0;
2336 request
->recv_timeout
= receive
;
2338 if (netconn_connected( &request
->netconn
))
2340 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
2341 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
2344 release_object( &request
->hdr
);
2347 case WINHTTP_HANDLE_TYPE_SESSION
:
2348 session
= (session_t
*)hdr
;
2349 session
->connect_timeout
= connect
;
2351 if (resolve
< 0) resolve
= 0;
2352 session
->resolve_timeout
= resolve
;
2354 if (send
< 0) send
= 0;
2355 session
->send_timeout
= send
;
2357 if (receive
< 0) receive
= 0;
2358 session
->recv_timeout
= receive
;
2362 release_object( hdr
);
2363 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2369 static const WCHAR wkday
[7][4] =
2370 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2371 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2372 static const WCHAR month
[12][4] =
2373 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2374 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2375 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2377 /***********************************************************************
2378 * WinHttpTimeFromSystemTime (WININET.@)
2380 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
2382 static const WCHAR format
[] =
2383 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2384 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2386 TRACE("%p, %p\n", time
, string
);
2388 if (!time
|| !string
) return FALSE
;
2390 sprintfW( string
, format
,
2391 wkday
[time
->wDayOfWeek
],
2393 month
[time
->wMonth
- 1],
2402 /***********************************************************************
2403 * WinHttpTimeToSystemTime (WININET.@)
2405 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
2408 const WCHAR
*s
= string
;
2411 TRACE("%s, %p\n", debugstr_w(string
), time
);
2413 if (!string
|| !time
) return FALSE
;
2415 /* Windows does this too */
2416 GetSystemTime( time
);
2418 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2419 * a SYSTEMTIME structure.
2422 while (*s
&& !isalphaW( *s
)) s
++;
2423 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2424 time
->wDayOfWeek
= 7;
2426 for (i
= 0; i
< 7; i
++)
2428 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
2429 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
2430 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
2432 time
->wDayOfWeek
= i
;
2437 if (time
->wDayOfWeek
> 6) return TRUE
;
2438 while (*s
&& !isdigitW( *s
)) s
++;
2439 time
->wDay
= strtolW( s
, &end
, 10 );
2442 while (*s
&& !isalphaW( *s
)) s
++;
2443 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
2446 for (i
= 0; i
< 12; i
++)
2448 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
2449 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
2450 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
2452 time
->wMonth
= i
+ 1;
2456 if (time
->wMonth
== 0) return TRUE
;
2458 while (*s
&& !isdigitW( *s
)) s
++;
2459 if (*s
== '\0') return TRUE
;
2460 time
->wYear
= strtolW( s
, &end
, 10 );
2463 while (*s
&& !isdigitW( *s
)) s
++;
2464 if (*s
== '\0') return TRUE
;
2465 time
->wHour
= strtolW( s
, &end
, 10 );
2468 while (*s
&& !isdigitW( *s
)) s
++;
2469 if (*s
== '\0') return TRUE
;
2470 time
->wMinute
= strtolW( s
, &end
, 10 );
2473 while (*s
&& !isdigitW( *s
)) s
++;
2474 if (*s
== '\0') return TRUE
;
2475 time
->wSecond
= strtolW( s
, &end
, 10 );
2477 time
->wMilliseconds
= 0;