2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
32 #include "winhttp_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
36 #define DEFAULT_CONNECT_TIMEOUT 60000
37 #define DEFAULT_SEND_TIMEOUT 30000
38 #define DEFAULT_RECEIVE_TIMEOUT 30000
40 void set_last_error( DWORD error
)
43 SetLastError( error
);
46 DWORD
get_last_error( void )
49 return GetLastError();
52 void send_callback( object_header_t
*hdr
, DWORD status
, LPVOID info
, DWORD buflen
)
54 TRACE("%p, 0x%08x, %p, %u\n", hdr
, status
, info
, buflen
);
56 if (hdr
->callback
&& (hdr
->notify_mask
& status
)) hdr
->callback( hdr
->handle
, hdr
->context
, status
, info
, buflen
);
59 /***********************************************************************
60 * WinHttpCheckPlatform (winhttp.@)
62 BOOL WINAPI
WinHttpCheckPlatform( void )
68 /***********************************************************************
69 * session_destroy (internal)
71 static void session_destroy( object_header_t
*hdr
)
73 session_t
*session
= (session_t
*)hdr
;
74 struct list
*item
, *next
;
77 TRACE("%p\n", session
);
79 LIST_FOR_EACH_SAFE( item
, next
, &session
->cookie_cache
)
81 domain
= LIST_ENTRY( item
, domain_t
, entry
);
82 delete_domain( domain
);
84 heap_free( session
->agent
);
85 heap_free( session
->proxy_server
);
86 heap_free( session
->proxy_bypass
);
87 heap_free( session
->proxy_username
);
88 heap_free( session
->proxy_password
);
92 static BOOL
session_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
96 case WINHTTP_OPTION_REDIRECT_POLICY
:
98 if (!buffer
|| *buflen
< sizeof(DWORD
))
100 *buflen
= sizeof(DWORD
);
101 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
105 *(DWORD
*)buffer
= hdr
->redirect_policy
;
106 *buflen
= sizeof(DWORD
);
110 FIXME("unimplemented option %u\n", option
);
111 set_last_error( ERROR_INVALID_PARAMETER
);
116 static BOOL
session_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
120 case WINHTTP_OPTION_PROXY
:
122 WINHTTP_PROXY_INFO
*pi
= buffer
;
124 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
127 case WINHTTP_OPTION_REDIRECT_POLICY
:
131 if (buflen
!= sizeof(policy
))
133 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
137 policy
= *(DWORD
*)buffer
;
138 TRACE("0x%x\n", policy
);
139 hdr
->redirect_policy
= policy
;
142 case WINHTTP_OPTION_DISABLE_FEATURE
:
143 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
146 FIXME("unimplemented option %u\n", option
);
147 set_last_error( ERROR_INVALID_PARAMETER
);
152 static const object_vtbl_t session_vtbl
=
155 session_query_option
,
159 /***********************************************************************
160 * WinHttpOpen (winhttp.@)
162 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
165 HINTERNET handle
= NULL
;
167 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent
), access
, debugstr_w(proxy
), debugstr_w(bypass
), flags
);
169 if (!(session
= heap_alloc_zero( sizeof(session_t
) ))) return NULL
;
171 session
->hdr
.type
= WINHTTP_HANDLE_TYPE_SESSION
;
172 session
->hdr
.vtbl
= &session_vtbl
;
173 session
->hdr
.flags
= flags
;
174 session
->hdr
.refs
= 1;
175 session
->hdr
.redirect_policy
= WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
;
176 list_init( &session
->cookie_cache
);
178 if (agent
&& !(session
->agent
= strdupW( agent
))) goto end
;
179 if (access
== WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
)
181 WINHTTP_PROXY_INFO info
;
183 WinHttpGetDefaultProxyConfiguration( &info
);
184 session
->access
= info
.dwAccessType
;
185 if (info
.lpszProxy
&& !(session
->proxy_server
= strdupW( info
.lpszProxy
)))
187 GlobalFree( (LPWSTR
)info
.lpszProxy
);
188 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
191 if (info
.lpszProxyBypass
&& !(session
->proxy_bypass
= strdupW( info
.lpszProxyBypass
)))
193 GlobalFree( (LPWSTR
)info
.lpszProxy
);
194 GlobalFree( (LPWSTR
)info
.lpszProxyBypass
);
198 else if (access
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
200 session
->access
= access
;
201 if (proxy
&& !(session
->proxy_server
= strdupW( proxy
))) goto end
;
202 if (bypass
&& !(session
->proxy_bypass
= strdupW( bypass
))) goto end
;
205 if (!(handle
= alloc_handle( &session
->hdr
))) goto end
;
206 session
->hdr
.handle
= handle
;
209 release_object( &session
->hdr
);
210 TRACE("returning %p\n", handle
);
214 /***********************************************************************
215 * connect_destroy (internal)
217 static void connect_destroy( object_header_t
*hdr
)
219 connect_t
*connect
= (connect_t
*)hdr
;
221 TRACE("%p\n", connect
);
223 release_object( &connect
->session
->hdr
);
225 heap_free( connect
->hostname
);
226 heap_free( connect
->servername
);
227 heap_free( connect
->username
);
228 heap_free( connect
->password
);
229 heap_free( connect
);
232 static const object_vtbl_t connect_vtbl
=
239 static BOOL
domain_matches(LPCWSTR server
, LPCWSTR domain
)
241 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
244 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
246 else if (*domain
== '*')
248 if (domain
[1] == '.')
252 /* For a hostname to match a wildcard, the last domain must match
253 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
254 * hostname is www.foo.a.b, it matches, but a.b does not.
256 dot
= strchrW( server
, '.' );
259 int len
= strlenW( dot
+ 1 );
261 if (len
> strlenW( domain
+ 2 ))
265 /* The server's domain is longer than the wildcard, so it
266 * could be a subdomain. Compare the last portion of the
269 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
270 if (!strcmpiW( ptr
, domain
+ 2 ))
272 /* This is only a match if the preceding character is
273 * a '.', i.e. that it is a matching domain. E.g.
274 * if domain is '*.b.c' and server is 'www.ab.c' they
277 ret
= *(ptr
- 1) == '.';
281 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
286 ret
= !strcmpiW( server
, domain
);
290 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
291 #define MAX_HOST_NAME_LENGTH 256
293 static BOOL
should_bypass_proxy(session_t
*session
, LPCWSTR server
)
298 if (!session
->proxy_bypass
) return FALSE
;
299 ptr
= session
->proxy_bypass
;
303 ptr
= strchrW( ptr
, ';' );
305 ptr
= strchrW( tmp
, ' ' );
308 if (ptr
- tmp
< MAX_HOST_NAME_LENGTH
)
310 WCHAR domain
[MAX_HOST_NAME_LENGTH
];
312 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
313 domain
[ptr
- tmp
] = 0;
314 ret
= domain_matches( server
, domain
);
319 ret
= domain_matches( server
, tmp
);
320 } while (ptr
&& !ret
);
324 BOOL
set_server_for_hostname( connect_t
*connect
, LPCWSTR server
, INTERNET_PORT port
)
326 session_t
*session
= connect
->session
;
329 if (session
->proxy_server
&& !should_bypass_proxy(session
, server
))
333 if ((colon
= strchrW( session
->proxy_server
, ':' )))
335 if (!connect
->servername
|| strncmpiW( connect
->servername
,
336 session
->proxy_server
, colon
- session
->proxy_server
- 1 ))
338 heap_free( connect
->servername
);
339 if (!(connect
->servername
= heap_alloc(
340 (colon
- session
->proxy_server
+ 1) * sizeof(WCHAR
) )))
345 memcpy( connect
->servername
, session
->proxy_server
,
346 (colon
- session
->proxy_server
) * sizeof(WCHAR
) );
347 connect
->servername
[colon
- session
->proxy_server
] = 0;
349 connect
->serverport
= atoiW( colon
+ 1 );
351 connect
->serverport
= INTERNET_DEFAULT_PORT
;
356 if (!connect
->servername
|| strcmpiW( connect
->servername
,
357 session
->proxy_server
))
359 heap_free( connect
->servername
);
360 if (!(connect
->servername
= strdupW( session
->proxy_server
)))
365 connect
->serverport
= INTERNET_DEFAULT_PORT
;
371 if (!(connect
->servername
= strdupW( server
)))
376 connect
->serverport
= port
;
382 /***********************************************************************
383 * WinHttpConnect (winhttp.@)
385 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
389 HINTERNET hconnect
= NULL
;
391 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
395 set_last_error( ERROR_INVALID_PARAMETER
);
398 if (!(session
= (session_t
*)grab_object( hsession
)))
400 set_last_error( ERROR_INVALID_HANDLE
);
403 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
405 release_object( &session
->hdr
);
406 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
409 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
411 release_object( &session
->hdr
);
414 connect
->hdr
.type
= WINHTTP_HANDLE_TYPE_CONNECT
;
415 connect
->hdr
.vtbl
= &connect_vtbl
;
416 connect
->hdr
.refs
= 1;
417 connect
->hdr
.flags
= session
->hdr
.flags
;
418 connect
->hdr
.callback
= session
->hdr
.callback
;
419 connect
->hdr
.notify_mask
= session
->hdr
.notify_mask
;
420 connect
->hdr
.context
= session
->hdr
.context
;
422 addref_object( &session
->hdr
);
423 connect
->session
= session
;
424 list_add_head( &session
->hdr
.children
, &connect
->hdr
.entry
);
426 if (server
&& !(connect
->hostname
= strdupW( server
))) goto end
;
427 connect
->hostport
= port
;
429 if (!set_server_for_hostname( connect
, server
, port
))
432 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
433 connect
->hdr
.handle
= hconnect
;
435 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
438 release_object( &connect
->hdr
);
440 TRACE("returning %p\n", hconnect
);
444 /***********************************************************************
445 * request_destroy (internal)
447 static void request_destroy( object_header_t
*hdr
)
449 request_t
*request
= (request_t
*)hdr
;
452 TRACE("%p\n", request
);
454 release_object( &request
->connect
->hdr
);
456 heap_free( request
->verb
);
457 heap_free( request
->path
);
458 heap_free( request
->version
);
459 heap_free( request
->raw_headers
);
460 heap_free( request
->status_text
);
461 for (i
= 0; i
< request
->num_headers
; i
++)
463 heap_free( request
->headers
[i
].field
);
464 heap_free( request
->headers
[i
].value
);
466 heap_free( request
->headers
);
467 heap_free( request
);
470 static BOOL
request_query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
474 case WINHTTP_OPTION_SECURITY_FLAGS
:
478 if (!buffer
|| *buflen
< sizeof(flags
))
480 *buflen
= sizeof(flags
);
481 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
486 if (hdr
->flags
& WINHTTP_FLAG_SECURE
) flags
|= SECURITY_FLAG_SECURE
;
487 *(DWORD
*)buffer
= flags
;
488 *buflen
= sizeof(flags
);
491 case WINHTTP_OPTION_SERVER_CERT_CONTEXT
:
493 const CERT_CONTEXT
*cert
;
494 request_t
*request
= (request_t
*)hdr
;
496 if (!buffer
|| *buflen
< sizeof(cert
))
498 *buflen
= sizeof(cert
);
499 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
503 if (!(cert
= netconn_get_certificate( &request
->netconn
))) return FALSE
;
504 *(CERT_CONTEXT
**)buffer
= (CERT_CONTEXT
*)cert
;
505 *buflen
= sizeof(cert
);
508 case WINHTTP_OPTION_SECURITY_KEY_BITNESS
:
510 if (!buffer
|| *buflen
< sizeof(DWORD
))
512 *buflen
= sizeof(DWORD
);
513 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
517 *(DWORD
*)buffer
= 128; /* FIXME */
518 *buflen
= sizeof(DWORD
);
522 FIXME("unimplemented option %u\n", option
);
523 set_last_error( ERROR_INVALID_PARAMETER
);
528 static BOOL
request_set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
532 case WINHTTP_OPTION_PROXY
:
534 WINHTTP_PROXY_INFO
*pi
= buffer
;
536 FIXME("%u %s %s\n", pi
->dwAccessType
, debugstr_w(pi
->lpszProxy
), debugstr_w(pi
->lpszProxyBypass
));
539 case WINHTTP_OPTION_DISABLE_FEATURE
:
543 if (buflen
!= sizeof(DWORD
))
545 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
549 disable
= *(DWORD
*)buffer
;
550 TRACE("0x%x\n", disable
);
551 hdr
->disable_flags
|= disable
;
554 case WINHTTP_OPTION_AUTOLOGON_POLICY
:
558 if (buflen
!= sizeof(DWORD
))
560 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
564 policy
= *(DWORD
*)buffer
;
565 TRACE("0x%x\n", policy
);
566 hdr
->logon_policy
= policy
;
569 case WINHTTP_OPTION_REDIRECT_POLICY
:
573 if (buflen
!= sizeof(DWORD
))
575 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
579 policy
= *(DWORD
*)buffer
;
580 TRACE("0x%x\n", policy
);
581 hdr
->redirect_policy
= policy
;
585 FIXME("unimplemented option %u\n", option
);
586 set_last_error( ERROR_INVALID_PARAMETER
);
591 static const object_vtbl_t request_vtbl
=
594 request_query_option
,
598 /***********************************************************************
599 * WinHttpOpenRequest (winhttp.@)
601 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
602 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
606 HINTERNET hrequest
= NULL
;
608 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
609 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
611 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
613 set_last_error( ERROR_INVALID_HANDLE
);
616 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
618 release_object( &connect
->hdr
);
619 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
622 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
624 release_object( &connect
->hdr
);
627 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
628 request
->hdr
.vtbl
= &request_vtbl
;
629 request
->hdr
.refs
= 1;
630 request
->hdr
.flags
= flags
;
631 request
->hdr
.callback
= connect
->hdr
.callback
;
632 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
633 request
->hdr
.context
= connect
->hdr
.context
;
635 addref_object( &connect
->hdr
);
636 request
->connect
= connect
;
637 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
639 if (!netconn_init( &request
->netconn
, request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)) goto end
;
640 request
->connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
641 request
->send_timeout
= DEFAULT_SEND_TIMEOUT
;
642 request
->recv_timeout
= DEFAULT_RECEIVE_TIMEOUT
;
644 if (!verb
|| !verb
[0]) verb
= getW
;
645 if (!(request
->verb
= strdupW( verb
))) goto end
;
652 len
= strlenW( object
) + 1;
653 if (object
[0] != '/') len
++;
654 if (!(p
= path
= heap_alloc( len
* sizeof(WCHAR
) ))) goto end
;
656 if (object
[0] != '/') *p
++ = '/';
657 strcpyW( p
, object
);
658 request
->path
= path
;
660 else if (!(request
->path
= strdupW( slashW
))) goto end
;
662 if (!version
|| !version
[0]) version
= http1_1
;
663 if (!(request
->version
= strdupW( version
))) goto end
;
665 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
666 request
->hdr
.handle
= hrequest
;
668 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
671 release_object( &request
->hdr
);
673 TRACE("returning %p\n", hrequest
);
677 /***********************************************************************
678 * WinHttpCloseHandle (winhttp.@)
680 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
682 object_header_t
*hdr
;
684 TRACE("%p\n", handle
);
686 if (!(hdr
= grab_object( handle
)))
688 set_last_error( ERROR_INVALID_HANDLE
);
691 release_object( hdr
);
692 free_handle( handle
);
696 static BOOL
query_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
702 set_last_error( ERROR_INVALID_PARAMETER
);
708 case WINHTTP_OPTION_CONTEXT_VALUE
:
710 if (!buffer
|| *buflen
< sizeof(DWORD_PTR
))
712 *buflen
= sizeof(DWORD_PTR
);
713 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
717 *(DWORD_PTR
*)buffer
= hdr
->context
;
718 *buflen
= sizeof(DWORD_PTR
);
722 if (hdr
->vtbl
->query_option
) ret
= hdr
->vtbl
->query_option( hdr
, option
, buffer
, buflen
);
725 FIXME("unimplemented option %u\n", option
);
726 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
734 /***********************************************************************
735 * WinHttpQueryOption (winhttp.@)
737 BOOL WINAPI
WinHttpQueryOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, LPDWORD buflen
)
740 object_header_t
*hdr
;
742 TRACE("%p, %u, %p, %p\n", handle
, option
, buffer
, buflen
);
744 if (!(hdr
= grab_object( handle
)))
746 set_last_error( ERROR_INVALID_HANDLE
);
750 ret
= query_option( hdr
, option
, buffer
, buflen
);
752 release_object( hdr
);
756 static BOOL
set_option( object_header_t
*hdr
, DWORD option
, LPVOID buffer
, DWORD buflen
)
762 set_last_error( ERROR_INVALID_PARAMETER
);
768 case WINHTTP_OPTION_CONTEXT_VALUE
:
770 if (buflen
!= sizeof(DWORD_PTR
))
772 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
776 hdr
->context
= *(DWORD_PTR
*)buffer
;
780 if (hdr
->vtbl
->set_option
) ret
= hdr
->vtbl
->set_option( hdr
, option
, buffer
, buflen
);
783 FIXME("unimplemented option %u\n", option
);
784 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
792 /***********************************************************************
793 * WinHttpSetOption (winhttp.@)
795 BOOL WINAPI
WinHttpSetOption( HINTERNET handle
, DWORD option
, LPVOID buffer
, DWORD buflen
)
798 object_header_t
*hdr
;
800 TRACE("%p, %u, %p, %u\n", handle
, option
, buffer
, buflen
);
802 if (!(hdr
= grab_object( handle
)))
804 set_last_error( ERROR_INVALID_HANDLE
);
808 ret
= set_option( hdr
, option
, buffer
, buflen
);
810 release_object( hdr
);
814 /***********************************************************************
815 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
817 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
819 FIXME("0x%08x, %p\n", flags
, url
);
821 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
825 static const WCHAR Connections
[] = {
826 'S','o','f','t','w','a','r','e','\\',
827 'M','i','c','r','o','s','o','f','t','\\',
828 'W','i','n','d','o','w','s','\\',
829 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
830 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
831 'C','o','n','n','e','c','t','i','o','n','s',0 };
832 static const WCHAR WinHttpSettings
[] = {
833 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
834 static const DWORD WINHTTPSETTINGS_MAGIC
= 0x18;
835 static const DWORD WINHTTP_PROXY_TYPE_DIRECT
= 1;
836 static const DWORD WINHTTP_PROXY_TYPE_PROXY
= 2;
838 struct winhttp_settings_header
841 DWORD unknown
; /* always zero? */
842 DWORD flags
; /* one of WINHTTP_PROXY_TYPE_* */
845 static inline void copy_char_to_wchar_sz(const BYTE
*src
, DWORD len
, WCHAR
*dst
)
849 for (begin
= src
; src
- begin
< len
; src
++, dst
++)
854 /***********************************************************************
855 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
857 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
861 BOOL got_from_reg
= FALSE
, direct
= TRUE
;
866 l
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, KEY_READ
, &key
);
869 DWORD type
, size
= 0;
871 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, &type
, NULL
, &size
);
872 if (!l
&& type
== REG_BINARY
&&
873 size
>= sizeof(struct winhttp_settings_header
) + 2 * sizeof(DWORD
))
875 BYTE
*buf
= heap_alloc( size
);
879 struct winhttp_settings_header
*hdr
=
880 (struct winhttp_settings_header
*)buf
;
881 DWORD
*len
= (DWORD
*)(hdr
+ 1);
883 l
= RegQueryValueExW( key
, WinHttpSettings
, NULL
, NULL
, buf
,
885 if (!l
&& hdr
->magic
== WINHTTPSETTINGS_MAGIC
&&
888 if (hdr
->flags
& WINHTTP_PROXY_TYPE_PROXY
)
892 LPWSTR proxy_bypass
= NULL
;
894 /* Sanity-check length of proxy string */
895 if ((BYTE
*)len
- buf
+ *len
<= size
)
898 proxy
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
900 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy
);
901 len
= (DWORD
*)((BYTE
*)(len
+ 1) + *len
);
905 /* Sanity-check length of proxy bypass string */
906 if ((BYTE
*)len
- buf
+ *len
<= size
)
908 proxy_bypass
= GlobalAlloc( 0, (*len
+ 1) * sizeof(WCHAR
) );
910 copy_char_to_wchar_sz( (BYTE
*)(len
+ 1), *len
, proxy_bypass
);
919 info
->lpszProxy
= proxy
;
920 info
->lpszProxyBypass
= proxy_bypass
;
926 WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
927 TRACE("http proxy (from registry) = %s, bypass = %s\n",
928 debugstr_w(info
->lpszProxy
),
929 debugstr_w(info
->lpszProxyBypass
));
938 if (!got_from_reg
&& (envproxy
= getenv( "http_proxy" )))
940 char *colon
, *http_proxy
;
942 if ((colon
= strchr( envproxy
, ':' )))
944 if (*(colon
+ 1) == '/' && *(colon
+ 2) == '/')
946 static const char http
[] = "http://";
948 /* It's a scheme, check that it's http */
949 if (!strncmp( envproxy
, http
, strlen( http
) ))
950 http_proxy
= envproxy
+ strlen( http
);
953 WARN("unsupported scheme in $http_proxy: %s\n", envproxy
);
958 http_proxy
= envproxy
;
961 http_proxy
= envproxy
;
967 len
= MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, NULL
, 0 );
968 if ((http_proxyW
= GlobalAlloc( 0, len
* sizeof(WCHAR
))))
970 MultiByteToWideChar( CP_UNIXCP
, 0, http_proxy
, -1, http_proxyW
, len
);
972 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
973 info
->lpszProxy
= http_proxyW
;
974 info
->lpszProxyBypass
= NULL
;
975 TRACE("http proxy (from environment) = %s\n",
976 debugstr_w(info
->lpszProxy
));
982 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
983 info
->lpszProxy
= NULL
;
984 info
->lpszProxyBypass
= NULL
;
989 /***********************************************************************
990 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
992 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
994 TRACE("%p\n", config
);
998 set_last_error( ERROR_INVALID_PARAMETER
);
1002 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1004 FIXME("returning no proxy used\n");
1005 config
->fAutoDetect
= FALSE
;
1006 config
->lpszAutoConfigUrl
= NULL
;
1007 config
->lpszProxy
= NULL
;
1008 config
->lpszProxyBypass
= NULL
;
1013 /***********************************************************************
1014 * WinHttpGetProxyForUrl (winhttp.@)
1016 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
1017 WINHTTP_PROXY_INFO
*info
)
1019 FIXME("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
1021 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
);
1025 /***********************************************************************
1026 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1028 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
1035 TRACE("%p\n", info
);
1039 set_last_error( ERROR_INVALID_PARAMETER
);
1042 switch (info
->dwAccessType
)
1044 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
1046 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
1047 if (!info
->lpszProxy
)
1049 set_last_error( ERROR_INVALID_PARAMETER
);
1052 /* Only ASCII characters are allowed */
1053 for (src
= info
->lpszProxy
; *src
; src
++)
1056 set_last_error( ERROR_INVALID_PARAMETER
);
1059 if (info
->lpszProxyBypass
)
1061 for (src
= info
->lpszProxyBypass
; *src
; src
++)
1064 set_last_error( ERROR_INVALID_PARAMETER
);
1070 set_last_error( ERROR_INVALID_PARAMETER
);
1074 l
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, Connections
, 0, NULL
, 0,
1075 KEY_WRITE
, NULL
, &key
, NULL
);
1078 DWORD size
= sizeof(struct winhttp_settings_header
) + 2 * sizeof(DWORD
);
1081 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1083 size
+= strlenW( info
->lpszProxy
);
1084 if (info
->lpszProxyBypass
)
1085 size
+= strlenW( info
->lpszProxyBypass
);
1087 buf
= heap_alloc( size
);
1090 struct winhttp_settings_header
*hdr
=
1091 (struct winhttp_settings_header
*)buf
;
1092 DWORD
*len
= (DWORD
*)(hdr
+ 1);
1094 hdr
->magic
= WINHTTPSETTINGS_MAGIC
;
1096 if (info
->dwAccessType
== WINHTTP_ACCESS_TYPE_NAMED_PROXY
)
1100 hdr
->flags
= WINHTTP_PROXY_TYPE_PROXY
;
1101 *len
++ = strlenW( info
->lpszProxy
);
1102 for (dst
= (BYTE
*)len
, src
= info
->lpszProxy
; *src
;
1106 if (info
->lpszProxyBypass
)
1108 *len
++ = strlenW( info
->lpszProxyBypass
);
1109 for (dst
= (BYTE
*)len
, src
= info
->lpszProxyBypass
; *src
;
1118 hdr
->flags
= WINHTTP_PROXY_TYPE_DIRECT
;
1122 l
= RegSetValueExW( key
, WinHttpSettings
, 0, REG_BINARY
, buf
, size
);
1132 /***********************************************************************
1133 * WinHttpSetStatusCallback (winhttp.@)
1135 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
1136 DWORD flags
, DWORD_PTR reserved
)
1138 object_header_t
*hdr
;
1139 WINHTTP_STATUS_CALLBACK ret
;
1141 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
1143 if (!(hdr
= grab_object( handle
)))
1145 set_last_error( ERROR_INVALID_HANDLE
);
1146 return WINHTTP_INVALID_STATUS_CALLBACK
;
1148 ret
= hdr
->callback
;
1149 hdr
->callback
= callback
;
1150 hdr
->notify_mask
= flags
;
1152 release_object( hdr
);
1156 /***********************************************************************
1157 * WinHttpSetTimeouts (winhttp.@)
1159 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
1164 TRACE("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
1166 if (resolve
< -1 || connect
< -1 || send
< -1 || receive
< -1)
1168 set_last_error( ERROR_INVALID_PARAMETER
);
1173 FIXME("resolve timeout (%d) not supported\n", resolve
);
1175 if (!(request
= (request_t
*)grab_object( handle
)))
1177 set_last_error( ERROR_INVALID_HANDLE
);
1181 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1183 release_object( &request
->hdr
);
1184 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1188 request
->connect_timeout
= connect
;
1190 if (send
< 0) send
= 0;
1191 request
->send_timeout
= send
;
1193 if (receive
< 0) receive
= 0;
1194 request
->recv_timeout
= receive
;
1196 if (netconn_connected( &request
->netconn
))
1198 if (netconn_set_timeout( &request
->netconn
, TRUE
, send
)) ret
= FALSE
;
1199 if (netconn_set_timeout( &request
->netconn
, FALSE
, receive
)) ret
= FALSE
;
1202 release_object( &request
->hdr
);
1206 static const WCHAR wkday
[7][4] =
1207 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1208 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1209 static const WCHAR month
[12][4] =
1210 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1211 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1212 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1214 /***********************************************************************
1215 * WinHttpTimeFromSystemTime (WININET.@)
1217 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
1219 static const WCHAR format
[] =
1220 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1221 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1223 TRACE("%p, %p\n", time
, string
);
1225 if (!time
|| !string
) return FALSE
;
1227 sprintfW( string
, format
,
1228 wkday
[time
->wDayOfWeek
],
1230 month
[time
->wMonth
- 1],
1239 /***********************************************************************
1240 * WinHttpTimeToSystemTime (WININET.@)
1242 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
1245 const WCHAR
*s
= string
;
1248 TRACE("%s, %p\n", debugstr_w(string
), time
);
1250 if (!string
|| !time
) return FALSE
;
1252 /* Windows does this too */
1253 GetSystemTime( time
);
1255 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1256 * a SYSTEMTIME structure.
1259 while (*s
&& !isalphaW( *s
)) s
++;
1260 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
1261 time
->wDayOfWeek
= 7;
1263 for (i
= 0; i
< 7; i
++)
1265 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
1266 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
1267 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
1269 time
->wDayOfWeek
= i
;
1274 if (time
->wDayOfWeek
> 6) return TRUE
;
1275 while (*s
&& !isdigitW( *s
)) s
++;
1276 time
->wDay
= strtolW( s
, &end
, 10 );
1279 while (*s
&& !isalphaW( *s
)) s
++;
1280 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
1283 for (i
= 0; i
< 12; i
++)
1285 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
1286 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
1287 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
1289 time
->wMonth
= i
+ 1;
1293 if (time
->wMonth
== 0) return TRUE
;
1295 while (*s
&& !isdigitW( *s
)) s
++;
1296 if (*s
== '\0') return TRUE
;
1297 time
->wYear
= strtolW( s
, &end
, 10 );
1300 while (*s
&& !isdigitW( *s
)) s
++;
1301 if (*s
== '\0') return TRUE
;
1302 time
->wHour
= strtolW( s
, &end
, 10 );
1305 while (*s
&& !isdigitW( *s
)) s
++;
1306 if (*s
== '\0') return TRUE
;
1307 time
->wMinute
= strtolW( s
, &end
, 10 );
1310 while (*s
&& !isdigitW( *s
)) s
++;
1311 if (*s
== '\0') return TRUE
;
1312 time
->wSecond
= strtolW( s
, &end
, 10 );
1315 time
->wMilliseconds
= 0;