wined3d: Get rid of the WINED3DADAPTER_IDENTIFIER typedef.
[wine/multimedia.git] / dlls / winhttp / session.c
blobf33508e16543f69e2827b49f21bb13380f284695
1 /*
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 "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winhttp.h"
29 #include "wincrypt.h"
30 #include "winreg.h"
31 #define COBJMACROS
32 #include "ole2.h"
33 #include "dispex.h"
34 #include "activscp.h"
36 #include "winhttp_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
40 #define DEFAULT_RESOLVE_TIMEOUT 0
41 #define DEFAULT_CONNECT_TIMEOUT 20000
42 #define DEFAULT_SEND_TIMEOUT 30000
43 #define DEFAULT_RECEIVE_TIMEOUT 30000
45 static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
46 static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
48 void set_last_error( DWORD error )
50 /* FIXME */
51 SetLastError( error );
54 DWORD get_last_error( void )
56 /* FIXME */
57 return GetLastError();
60 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
62 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
64 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
67 /***********************************************************************
68 * WinHttpCheckPlatform (winhttp.@)
70 BOOL WINAPI WinHttpCheckPlatform( void )
72 TRACE("\n");
73 return TRUE;
76 /***********************************************************************
77 * session_destroy (internal)
79 static void session_destroy( object_header_t *hdr )
81 session_t *session = (session_t *)hdr;
82 struct list *item, *next;
83 domain_t *domain;
85 TRACE("%p\n", session);
87 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
89 domain = LIST_ENTRY( item, domain_t, entry );
90 delete_domain( domain );
92 heap_free( session->agent );
93 heap_free( session->proxy_server );
94 heap_free( session->proxy_bypass );
95 heap_free( session->proxy_username );
96 heap_free( session->proxy_password );
97 heap_free( session );
100 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
102 session_t *session = (session_t *)hdr;
104 switch (option)
106 case WINHTTP_OPTION_REDIRECT_POLICY:
108 if (!buffer || *buflen < sizeof(DWORD))
110 *buflen = sizeof(DWORD);
111 set_last_error( ERROR_INSUFFICIENT_BUFFER );
112 return FALSE;
115 *(DWORD *)buffer = hdr->redirect_policy;
116 *buflen = sizeof(DWORD);
117 return TRUE;
119 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
120 *(DWORD *)buffer = session->resolve_timeout;
121 *buflen = sizeof(DWORD);
122 return TRUE;
123 case WINHTTP_OPTION_CONNECT_TIMEOUT:
124 *(DWORD *)buffer = session->connect_timeout;
125 *buflen = sizeof(DWORD);
126 return TRUE;
127 case WINHTTP_OPTION_SEND_TIMEOUT:
128 *(DWORD *)buffer = session->send_timeout;
129 *buflen = sizeof(DWORD);
130 return TRUE;
131 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
132 *(DWORD *)buffer = session->recv_timeout;
133 *buflen = sizeof(DWORD);
134 return TRUE;
135 default:
136 FIXME("unimplemented option %u\n", option);
137 set_last_error( ERROR_INVALID_PARAMETER );
138 return FALSE;
142 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
144 session_t *session = (session_t *)hdr;
146 switch (option)
148 case WINHTTP_OPTION_PROXY:
150 WINHTTP_PROXY_INFO *pi = buffer;
152 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
153 return TRUE;
155 case WINHTTP_OPTION_REDIRECT_POLICY:
157 DWORD policy;
159 if (buflen != sizeof(policy))
161 set_last_error( ERROR_INSUFFICIENT_BUFFER );
162 return FALSE;
165 policy = *(DWORD *)buffer;
166 TRACE("0x%x\n", policy);
167 hdr->redirect_policy = policy;
168 return TRUE;
170 case WINHTTP_OPTION_DISABLE_FEATURE:
171 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
172 return FALSE;
173 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
174 session->resolve_timeout = *(DWORD *)buffer;
175 return TRUE;
176 case WINHTTP_OPTION_CONNECT_TIMEOUT:
177 session->connect_timeout = *(DWORD *)buffer;
178 return TRUE;
179 case WINHTTP_OPTION_SEND_TIMEOUT:
180 session->send_timeout = *(DWORD *)buffer;
181 return TRUE;
182 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
183 session->recv_timeout = *(DWORD *)buffer;
184 return TRUE;
185 default:
186 FIXME("unimplemented option %u\n", option);
187 set_last_error( ERROR_INVALID_PARAMETER );
188 return FALSE;
192 static const object_vtbl_t session_vtbl =
194 session_destroy,
195 session_query_option,
196 session_set_option
199 /***********************************************************************
200 * WinHttpOpen (winhttp.@)
202 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
204 session_t *session;
205 HINTERNET handle = NULL;
207 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
209 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
211 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
212 session->hdr.vtbl = &session_vtbl;
213 session->hdr.flags = flags;
214 session->hdr.refs = 1;
215 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
216 list_init( &session->hdr.children );
217 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
218 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
219 session->send_timeout = DEFAULT_SEND_TIMEOUT;
220 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
221 list_init( &session->cookie_cache );
223 if (agent && !(session->agent = strdupW( agent ))) goto end;
224 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
226 WINHTTP_PROXY_INFO info;
228 WinHttpGetDefaultProxyConfiguration( &info );
229 session->access = info.dwAccessType;
230 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
232 GlobalFree( (LPWSTR)info.lpszProxy );
233 GlobalFree( (LPWSTR)info.lpszProxyBypass );
234 goto end;
236 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
238 GlobalFree( (LPWSTR)info.lpszProxy );
239 GlobalFree( (LPWSTR)info.lpszProxyBypass );
240 goto end;
243 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
245 session->access = access;
246 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
247 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
250 if (!(handle = alloc_handle( &session->hdr ))) goto end;
251 session->hdr.handle = handle;
253 end:
254 release_object( &session->hdr );
255 TRACE("returning %p\n", handle);
256 return handle;
259 /***********************************************************************
260 * connect_destroy (internal)
262 static void connect_destroy( object_header_t *hdr )
264 connect_t *connect = (connect_t *)hdr;
266 TRACE("%p\n", connect);
268 release_object( &connect->session->hdr );
270 heap_free( connect->hostname );
271 heap_free( connect->servername );
272 heap_free( connect->username );
273 heap_free( connect->password );
274 heap_free( connect );
277 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
279 connect_t *connect = (connect_t *)hdr;
281 switch (option)
283 case WINHTTP_OPTION_PARENT_HANDLE:
285 if (!buffer || *buflen < sizeof(HINTERNET))
287 *buflen = sizeof(HINTERNET);
288 set_last_error( ERROR_INSUFFICIENT_BUFFER );
289 return FALSE;
292 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
293 *buflen = sizeof(HINTERNET);
294 return TRUE;
296 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
297 *(DWORD *)buffer = connect->session->resolve_timeout;
298 *buflen = sizeof(DWORD);
299 return TRUE;
300 case WINHTTP_OPTION_CONNECT_TIMEOUT:
301 *(DWORD *)buffer = connect->session->connect_timeout;
302 *buflen = sizeof(DWORD);
303 return TRUE;
304 case WINHTTP_OPTION_SEND_TIMEOUT:
305 *(DWORD *)buffer = connect->session->send_timeout;
306 *buflen = sizeof(DWORD);
307 return TRUE;
308 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
309 *(DWORD *)buffer = connect->session->recv_timeout;
310 *buflen = sizeof(DWORD);
311 return TRUE;
312 default:
313 FIXME("unimplemented option %u\n", option);
314 set_last_error( ERROR_INVALID_PARAMETER );
315 return FALSE;
319 static const object_vtbl_t connect_vtbl =
321 connect_destroy,
322 connect_query_option,
323 NULL
326 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
328 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
329 BOOL ret = FALSE;
331 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
332 ret = TRUE;
333 else if (*domain == '*')
335 if (domain[1] == '.')
337 LPCWSTR dot;
339 /* For a hostname to match a wildcard, the last domain must match
340 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
341 * hostname is www.foo.a.b, it matches, but a.b does not.
343 dot = strchrW( server, '.' );
344 if (dot)
346 int len = strlenW( dot + 1 );
348 if (len > strlenW( domain + 2 ))
350 LPCWSTR ptr;
352 /* The server's domain is longer than the wildcard, so it
353 * could be a subdomain. Compare the last portion of the
354 * server's domain.
356 ptr = dot + len + 1 - strlenW( domain + 2 );
357 if (!strcmpiW( ptr, domain + 2 ))
359 /* This is only a match if the preceding character is
360 * a '.', i.e. that it is a matching domain. E.g.
361 * if domain is '*.b.c' and server is 'www.ab.c' they
362 * do not match.
364 ret = *(ptr - 1) == '.';
367 else
368 ret = !strcmpiW( dot + 1, domain + 2 );
372 else
373 ret = !strcmpiW( server, domain );
374 return ret;
377 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
378 #define MAX_HOST_NAME_LENGTH 256
380 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
382 LPCWSTR ptr;
383 BOOL ret = FALSE;
385 if (!session->proxy_bypass) return FALSE;
386 ptr = session->proxy_bypass;
387 do {
388 LPCWSTR tmp = ptr;
390 ptr = strchrW( ptr, ';' );
391 if (!ptr)
392 ptr = strchrW( tmp, ' ' );
393 if (ptr)
395 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
397 WCHAR domain[MAX_HOST_NAME_LENGTH];
399 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
400 domain[ptr - tmp] = 0;
401 ret = domain_matches( server, domain );
403 ptr += 1;
405 else if (*tmp)
406 ret = domain_matches( server, tmp );
407 } while (ptr && !ret);
408 return ret;
411 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
413 session_t *session = connect->session;
414 BOOL ret = TRUE;
416 if (session->proxy_server && !should_bypass_proxy(session, server))
418 LPCWSTR colon;
420 if ((colon = strchrW( session->proxy_server, ':' )))
422 if (!connect->servername || strncmpiW( connect->servername,
423 session->proxy_server, colon - session->proxy_server - 1 ))
425 heap_free( connect->servername );
426 if (!(connect->servername = heap_alloc(
427 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
429 ret = FALSE;
430 goto end;
432 memcpy( connect->servername, session->proxy_server,
433 (colon - session->proxy_server) * sizeof(WCHAR) );
434 connect->servername[colon - session->proxy_server] = 0;
435 if (*(colon + 1))
436 connect->serverport = atoiW( colon + 1 );
437 else
438 connect->serverport = INTERNET_DEFAULT_PORT;
441 else
443 if (!connect->servername || strcmpiW( connect->servername,
444 session->proxy_server ))
446 heap_free( connect->servername );
447 if (!(connect->servername = strdupW( session->proxy_server )))
449 ret = FALSE;
450 goto end;
452 connect->serverport = INTERNET_DEFAULT_PORT;
456 else if (server)
458 heap_free( connect->servername );
459 if (!(connect->servername = strdupW( server )))
461 ret = FALSE;
462 goto end;
464 connect->serverport = port;
466 end:
467 return ret;
470 /***********************************************************************
471 * WinHttpConnect (winhttp.@)
473 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
475 connect_t *connect;
476 session_t *session;
477 HINTERNET hconnect = NULL;
479 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
481 if (!server)
483 set_last_error( ERROR_INVALID_PARAMETER );
484 return NULL;
486 if (!(session = (session_t *)grab_object( hsession )))
488 set_last_error( ERROR_INVALID_HANDLE );
489 return NULL;
491 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
493 release_object( &session->hdr );
494 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
495 return NULL;
497 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
499 release_object( &session->hdr );
500 return NULL;
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 list_init( &connect->hdr.children );
511 addref_object( &session->hdr );
512 connect->session = session;
513 list_add_head( &session->hdr.children, &connect->hdr.entry );
515 if (!(connect->hostname = strdupW( server ))) goto end;
516 connect->hostport = port;
517 if (!set_server_for_hostname( connect, server, port )) goto end;
519 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
520 connect->hdr.handle = hconnect;
522 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
524 end:
525 release_object( &connect->hdr );
526 release_object( &session->hdr );
527 TRACE("returning %p\n", hconnect);
528 return hconnect;
531 /***********************************************************************
532 * request_destroy (internal)
534 static void request_destroy( object_header_t *hdr )
536 request_t *request = (request_t *)hdr;
537 unsigned int i;
539 TRACE("%p\n", request);
541 release_object( &request->connect->hdr );
543 heap_free( request->verb );
544 heap_free( request->path );
545 heap_free( request->version );
546 heap_free( request->raw_headers );
547 heap_free( request->status_text );
548 for (i = 0; i < request->num_headers; i++)
550 heap_free( request->headers[i].field );
551 heap_free( request->headers[i].value );
553 heap_free( request->headers );
554 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
555 heap_free( request->accept_types );
556 heap_free( request );
559 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
561 int len = 0;
562 if (str) len = strlenW( str );
563 if (buffer && *buflen > len)
565 memcpy( buffer, str, len * sizeof(WCHAR) );
566 buffer[len] = 0;
568 *buflen = len * sizeof(WCHAR);
571 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
573 WCHAR *ret;
574 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
576 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
577 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
578 CertNameToStrW( encoding, blob, format, ret, size );
580 return ret;
583 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
585 request_t *request = (request_t *)hdr;
587 switch (option)
589 case WINHTTP_OPTION_SECURITY_FLAGS:
591 DWORD flags;
592 int bits;
594 if (!buffer || *buflen < sizeof(flags))
596 *buflen = sizeof(flags);
597 set_last_error( ERROR_INSUFFICIENT_BUFFER );
598 return FALSE;
601 flags = 0;
602 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
603 flags |= request->netconn.security_flags;
604 bits = netconn_get_cipher_strength( &request->netconn );
605 if (bits >= 128)
606 flags |= SECURITY_FLAG_STRENGTH_STRONG;
607 else if (bits >= 56)
608 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
609 else
610 flags |= SECURITY_FLAG_STRENGTH_WEAK;
611 *(DWORD *)buffer = flags;
612 *buflen = sizeof(flags);
613 return TRUE;
615 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
617 const CERT_CONTEXT *cert;
619 if (!buffer || *buflen < sizeof(cert))
621 *buflen = sizeof(cert);
622 set_last_error( ERROR_INSUFFICIENT_BUFFER );
623 return FALSE;
626 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
627 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
628 *buflen = sizeof(cert);
629 return TRUE;
631 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
633 const CERT_CONTEXT *cert;
634 const CRYPT_OID_INFO *oidInfo;
635 WINHTTP_CERTIFICATE_INFO *ci = buffer;
637 FIXME("partial stub\n");
639 if (!buffer || *buflen < sizeof(*ci))
641 *buflen = sizeof(*ci);
642 set_last_error( ERROR_INSUFFICIENT_BUFFER );
643 return FALSE;
645 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
647 ci->ftExpiry = cert->pCertInfo->NotAfter;
648 ci->ftStart = cert->pCertInfo->NotBefore;
649 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
650 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
651 ci->lpszProtocolName = NULL;
652 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
653 cert->pCertInfo->SignatureAlgorithm.pszObjId,
654 0 );
655 if (oidInfo)
656 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
657 else
658 ci->lpszSignatureAlgName = NULL;
659 ci->lpszEncryptionAlgName = NULL;
660 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
662 CertFreeCertificateContext( cert );
663 *buflen = sizeof(*ci);
664 return TRUE;
666 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
668 if (!buffer || *buflen < sizeof(DWORD))
670 *buflen = sizeof(DWORD);
671 set_last_error( ERROR_INSUFFICIENT_BUFFER );
672 return FALSE;
675 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
676 *buflen = sizeof(DWORD);
677 return TRUE;
679 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
680 *(DWORD *)buffer = request->resolve_timeout;
681 *buflen = sizeof(DWORD);
682 return TRUE;
683 case WINHTTP_OPTION_CONNECT_TIMEOUT:
684 *(DWORD *)buffer = request->connect_timeout;
685 *buflen = sizeof(DWORD);
686 return TRUE;
687 case WINHTTP_OPTION_SEND_TIMEOUT:
688 *(DWORD *)buffer = request->send_timeout;
689 *buflen = sizeof(DWORD);
690 return TRUE;
691 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
692 *(DWORD *)buffer = request->recv_timeout;
693 *buflen = sizeof(DWORD);
694 return TRUE;
696 case WINHTTP_OPTION_USERNAME:
697 str_to_buffer( buffer, request->connect->username, buflen );
698 return TRUE;
700 case WINHTTP_OPTION_PASSWORD:
701 str_to_buffer( buffer, request->connect->password, buflen );
702 return TRUE;
704 case WINHTTP_OPTION_PROXY_USERNAME:
705 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
706 return TRUE;
708 case WINHTTP_OPTION_PROXY_PASSWORD:
709 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
710 return TRUE;
712 default:
713 FIXME("unimplemented option %u\n", option);
714 set_last_error( ERROR_INVALID_PARAMETER );
715 return FALSE;
719 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
721 WCHAR *ret;
722 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
724 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
725 ret[buflen] = 0;
726 return ret;
728 set_last_error( ERROR_OUTOFMEMORY );
729 return NULL;
732 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
734 request_t *request = (request_t *)hdr;
736 switch (option)
738 case WINHTTP_OPTION_PROXY:
740 WINHTTP_PROXY_INFO *pi = buffer;
742 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
743 return TRUE;
745 case WINHTTP_OPTION_DISABLE_FEATURE:
747 DWORD disable;
749 if (buflen != sizeof(DWORD))
751 set_last_error( ERROR_INSUFFICIENT_BUFFER );
752 return FALSE;
755 disable = *(DWORD *)buffer;
756 TRACE("0x%x\n", disable);
757 hdr->disable_flags |= disable;
758 return TRUE;
760 case WINHTTP_OPTION_AUTOLOGON_POLICY:
762 DWORD policy;
764 if (buflen != sizeof(DWORD))
766 set_last_error( ERROR_INSUFFICIENT_BUFFER );
767 return FALSE;
770 policy = *(DWORD *)buffer;
771 TRACE("0x%x\n", policy);
772 hdr->logon_policy = policy;
773 return TRUE;
775 case WINHTTP_OPTION_REDIRECT_POLICY:
777 DWORD policy;
779 if (buflen != sizeof(DWORD))
781 set_last_error( ERROR_INSUFFICIENT_BUFFER );
782 return FALSE;
785 policy = *(DWORD *)buffer;
786 TRACE("0x%x\n", policy);
787 hdr->redirect_policy = policy;
788 return TRUE;
790 case WINHTTP_OPTION_SECURITY_FLAGS:
792 DWORD flags;
794 if (buflen < sizeof(DWORD))
796 set_last_error( ERROR_INSUFFICIENT_BUFFER );
797 return FALSE;
799 flags = *(DWORD *)buffer;
800 TRACE("0x%x\n", flags);
801 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
802 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
803 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
804 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
806 set_last_error( ERROR_INVALID_PARAMETER );
807 return FALSE;
809 request->netconn.security_flags = flags;
810 return TRUE;
812 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
813 request->resolve_timeout = *(DWORD *)buffer;
814 return TRUE;
815 case WINHTTP_OPTION_CONNECT_TIMEOUT:
816 request->connect_timeout = *(DWORD *)buffer;
817 return TRUE;
818 case WINHTTP_OPTION_SEND_TIMEOUT:
819 request->send_timeout = *(DWORD *)buffer;
820 return TRUE;
821 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
822 request->recv_timeout = *(DWORD *)buffer;
823 return TRUE;
825 case WINHTTP_OPTION_USERNAME:
827 connect_t *connect = request->connect;
829 heap_free( connect->username );
830 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
831 return TRUE;
833 case WINHTTP_OPTION_PASSWORD:
835 connect_t *connect = request->connect;
837 heap_free( connect->password );
838 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
839 return TRUE;
841 case WINHTTP_OPTION_PROXY_USERNAME:
843 session_t *session = request->connect->session;
845 heap_free( session->proxy_username );
846 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
847 return TRUE;
849 case WINHTTP_OPTION_PROXY_PASSWORD:
851 session_t *session = request->connect->session;
853 heap_free( session->proxy_password );
854 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
855 return TRUE;
857 default:
858 FIXME("unimplemented option %u\n", option);
859 set_last_error( ERROR_INVALID_PARAMETER );
860 return TRUE;
864 static const object_vtbl_t request_vtbl =
866 request_destroy,
867 request_query_option,
868 request_set_option
871 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
873 const WCHAR **types = accept_types;
874 int i;
876 if (!types) return TRUE;
877 while (*types)
879 request->num_accept_types++;
880 types++;
882 if (!request->num_accept_types) return TRUE;
883 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
885 request->num_accept_types = 0;
886 return FALSE;
888 types = accept_types;
889 for (i = 0; i < request->num_accept_types; i++)
891 if (!(request->accept_types[i] = strdupW( *types )))
893 for (; i >= 0; i--) heap_free( request->accept_types[i] );
894 heap_free( request->accept_types );
895 request->accept_types = NULL;
896 request->num_accept_types = 0;
897 return FALSE;
899 types++;
901 return TRUE;
904 /***********************************************************************
905 * WinHttpOpenRequest (winhttp.@)
907 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
908 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
910 request_t *request;
911 connect_t *connect;
912 HINTERNET hrequest = NULL;
914 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
915 debugstr_w(version), debugstr_w(referrer), types, flags);
917 if (!(connect = (connect_t *)grab_object( hconnect )))
919 set_last_error( ERROR_INVALID_HANDLE );
920 return NULL;
922 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
924 release_object( &connect->hdr );
925 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
926 return NULL;
928 if (!(request = heap_alloc_zero( sizeof(request_t) )))
930 release_object( &connect->hdr );
931 return NULL;
933 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
934 request->hdr.vtbl = &request_vtbl;
935 request->hdr.refs = 1;
936 request->hdr.flags = flags;
937 request->hdr.callback = connect->hdr.callback;
938 request->hdr.notify_mask = connect->hdr.notify_mask;
939 request->hdr.context = connect->hdr.context;
940 list_init( &request->hdr.children );
942 addref_object( &connect->hdr );
943 request->connect = connect;
944 list_add_head( &connect->hdr.children, &request->hdr.entry );
946 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
947 request->resolve_timeout = connect->session->resolve_timeout;
948 request->connect_timeout = connect->session->connect_timeout;
949 request->send_timeout = connect->session->send_timeout;
950 request->recv_timeout = connect->session->recv_timeout;
952 if (!verb || !verb[0]) verb = getW;
953 if (!(request->verb = strdupW( verb ))) goto end;
955 if (object)
957 WCHAR *path, *p;
958 unsigned int len;
960 len = strlenW( object ) + 1;
961 if (object[0] != '/') len++;
962 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
964 if (object[0] != '/') *p++ = '/';
965 strcpyW( p, object );
966 request->path = path;
968 else if (!(request->path = strdupW( slashW ))) goto end;
970 if (!version || !version[0]) version = http1_1;
971 if (!(request->version = strdupW( version ))) goto end;
972 if (!(store_accept_types( request, types ))) goto end;
974 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
975 request->hdr.handle = hrequest;
977 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
979 end:
980 release_object( &request->hdr );
981 release_object( &connect->hdr );
982 TRACE("returning %p\n", hrequest);
983 return hrequest;
986 /***********************************************************************
987 * WinHttpCloseHandle (winhttp.@)
989 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
991 object_header_t *hdr;
993 TRACE("%p\n", handle);
995 if (!(hdr = grab_object( handle )))
997 set_last_error( ERROR_INVALID_HANDLE );
998 return FALSE;
1000 release_object( hdr );
1001 free_handle( handle );
1002 return TRUE;
1005 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1007 BOOL ret = FALSE;
1009 if (!buflen)
1011 set_last_error( ERROR_INVALID_PARAMETER );
1012 return FALSE;
1015 switch (option)
1017 case WINHTTP_OPTION_CONTEXT_VALUE:
1019 if (!buffer || *buflen < sizeof(DWORD_PTR))
1021 *buflen = sizeof(DWORD_PTR);
1022 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1023 return FALSE;
1026 *(DWORD_PTR *)buffer = hdr->context;
1027 *buflen = sizeof(DWORD_PTR);
1028 return TRUE;
1030 default:
1031 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1032 else
1034 FIXME("unimplemented option %u\n", option);
1035 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1036 return FALSE;
1038 break;
1040 return ret;
1043 /***********************************************************************
1044 * WinHttpQueryOption (winhttp.@)
1046 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1048 BOOL ret = FALSE;
1049 object_header_t *hdr;
1051 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1053 if (!(hdr = grab_object( handle )))
1055 set_last_error( ERROR_INVALID_HANDLE );
1056 return FALSE;
1059 ret = query_option( hdr, option, buffer, buflen );
1061 release_object( hdr );
1062 return ret;
1065 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1067 BOOL ret = TRUE;
1069 if (!buffer)
1071 set_last_error( ERROR_INVALID_PARAMETER );
1072 return FALSE;
1075 switch (option)
1077 case WINHTTP_OPTION_CONTEXT_VALUE:
1079 if (buflen != sizeof(DWORD_PTR))
1081 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1082 return FALSE;
1085 hdr->context = *(DWORD_PTR *)buffer;
1086 return TRUE;
1088 default:
1089 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1090 else
1092 FIXME("unimplemented option %u\n", option);
1093 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1094 return FALSE;
1096 break;
1098 return ret;
1101 /***********************************************************************
1102 * WinHttpSetOption (winhttp.@)
1104 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1106 BOOL ret = FALSE;
1107 object_header_t *hdr;
1109 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1111 if (!(hdr = grab_object( handle )))
1113 set_last_error( ERROR_INVALID_HANDLE );
1114 return FALSE;
1117 ret = set_option( hdr, option, buffer, buflen );
1119 release_object( hdr );
1120 return ret;
1123 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1125 char *ret;
1126 DWORD size = 0;
1128 GetComputerNameExA( format, NULL, &size );
1129 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1130 if (!(ret = heap_alloc( size ))) return NULL;
1131 if (!GetComputerNameExA( format, ret, &size ))
1133 heap_free( ret );
1134 return NULL;
1136 return ret;
1139 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1141 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1143 if (len_suffix > len_domain) return FALSE;
1144 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1145 return FALSE;
1148 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1150 sprintfW( buf, fmt,
1151 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1152 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1153 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1154 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1157 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1159 static const WCHAR fmtW[] =
1160 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1161 '/','w','p','a','d','.','d','a','t',0};
1162 WCHAR *ret;
1164 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1165 if (!ai) return NULL;
1167 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1168 printf_addr( fmtW, ret, (struct sockaddr_in *)ai->ai_addr );
1169 return ret;
1172 /***********************************************************************
1173 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1175 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1177 BOOL ret = FALSE;
1179 TRACE("0x%08x, %p\n", flags, url);
1181 if (!flags || !url)
1183 set_last_error( ERROR_INVALID_PARAMETER );
1184 return FALSE;
1186 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP) FIXME("discovery via DHCP not supported\n");
1187 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1189 #ifdef HAVE_GETADDRINFO
1190 char *fqdn, *domain, *p;
1192 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1193 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1195 heap_free( fqdn );
1196 return FALSE;
1198 p = fqdn;
1199 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1201 struct addrinfo *ai;
1202 char *name;
1203 int res;
1205 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1207 heap_free( fqdn );
1208 heap_free( domain );
1209 return FALSE;
1211 strcpy( name, "wpad" );
1212 strcat( name, p );
1213 res = getaddrinfo( name, NULL, NULL, &ai );
1214 heap_free( name );
1215 if (!res)
1217 *url = build_wpad_url( ai );
1218 freeaddrinfo( ai );
1219 if (*url)
1221 TRACE("returning %s\n", debugstr_w(*url));
1222 ret = TRUE;
1223 break;
1226 p++;
1228 heap_free( domain );
1229 heap_free( fqdn );
1230 #else
1231 FIXME("getaddrinfo not found at build time\n");
1232 #endif
1234 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1235 return ret;
1238 static const WCHAR Connections[] = {
1239 'S','o','f','t','w','a','r','e','\\',
1240 'M','i','c','r','o','s','o','f','t','\\',
1241 'W','i','n','d','o','w','s','\\',
1242 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1243 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1244 'C','o','n','n','e','c','t','i','o','n','s',0 };
1245 static const WCHAR WinHttpSettings[] = {
1246 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1247 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1248 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1249 static const DWORD PROXY_TYPE_DIRECT = 1;
1250 static const DWORD PROXY_TYPE_PROXY = 2;
1251 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1252 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1254 struct connection_settings_header
1256 DWORD magic;
1257 DWORD unknown; /* always zero? */
1258 DWORD flags; /* one or more of PROXY_* */
1261 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1263 const BYTE *begin;
1265 for (begin = src; src - begin < len; src++, dst++)
1266 *dst = *src;
1267 *dst = 0;
1270 /***********************************************************************
1271 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1273 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1275 LONG l;
1276 HKEY key;
1277 BOOL got_from_reg = FALSE, direct = TRUE;
1278 char *envproxy;
1280 TRACE("%p\n", info);
1282 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1283 if (!l)
1285 DWORD type, size = 0;
1287 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1288 if (!l && type == REG_BINARY &&
1289 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1291 BYTE *buf = heap_alloc( size );
1293 if (buf)
1295 struct connection_settings_header *hdr =
1296 (struct connection_settings_header *)buf;
1297 DWORD *len = (DWORD *)(hdr + 1);
1299 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1300 &size );
1301 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1302 hdr->unknown == 0)
1304 if (hdr->flags & PROXY_TYPE_PROXY)
1306 BOOL sane = FALSE;
1307 LPWSTR proxy = NULL;
1308 LPWSTR proxy_bypass = NULL;
1310 /* Sanity-check length of proxy string */
1311 if ((BYTE *)len - buf + *len <= size)
1313 sane = TRUE;
1314 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1315 if (proxy)
1316 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1317 len = (DWORD *)((BYTE *)(len + 1) + *len);
1319 if (sane)
1321 /* Sanity-check length of proxy bypass string */
1322 if ((BYTE *)len - buf + *len <= size)
1324 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1325 if (proxy_bypass)
1326 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1328 else
1330 sane = FALSE;
1331 GlobalFree( proxy );
1332 proxy = NULL;
1335 info->lpszProxy = proxy;
1336 info->lpszProxyBypass = proxy_bypass;
1337 if (sane)
1339 got_from_reg = TRUE;
1340 direct = FALSE;
1341 info->dwAccessType =
1342 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1343 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1344 debugstr_w(info->lpszProxy),
1345 debugstr_w(info->lpszProxyBypass));
1349 heap_free( buf );
1352 RegCloseKey( key );
1354 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1356 char *colon, *http_proxy;
1358 if ((colon = strchr( envproxy, ':' )))
1360 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1362 static const char http[] = "http://";
1364 /* It's a scheme, check that it's http */
1365 if (!strncmp( envproxy, http, strlen( http ) ))
1366 http_proxy = envproxy + strlen( http );
1367 else
1369 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1370 http_proxy = NULL;
1373 else
1374 http_proxy = envproxy;
1376 else
1377 http_proxy = envproxy;
1378 if (http_proxy)
1380 WCHAR *http_proxyW;
1381 int len;
1383 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1384 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1386 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1387 direct = FALSE;
1388 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1389 info->lpszProxy = http_proxyW;
1390 info->lpszProxyBypass = NULL;
1391 TRACE("http proxy (from environment) = %s\n",
1392 debugstr_w(info->lpszProxy));
1396 if (direct)
1398 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1399 info->lpszProxy = NULL;
1400 info->lpszProxyBypass = NULL;
1402 return TRUE;
1405 /***********************************************************************
1406 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1408 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1410 static const WCHAR settingsW[] =
1411 {'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};
1412 HKEY hkey = NULL;
1413 struct connection_settings_header *hdr = NULL;
1414 DWORD type, offset, len, size = 0;
1415 BOOL ret = FALSE;
1417 TRACE("%p\n", config);
1419 if (!config)
1421 set_last_error( ERROR_INVALID_PARAMETER );
1422 return FALSE;
1424 memset( config, 0, sizeof(*config) );
1425 config->fAutoDetect = TRUE;
1427 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1428 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1429 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1431 ret = TRUE;
1432 goto done;
1434 if (!(hdr = heap_alloc( size ))) goto done;
1435 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1436 hdr->magic != WININET_SETTINGS_MAGIC)
1438 ret = TRUE;
1439 goto done;
1442 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1443 offset = sizeof(*hdr);
1444 if (offset + sizeof(DWORD) > size) goto done;
1445 len = *(DWORD *)((char *)hdr + offset);
1446 offset += sizeof(DWORD);
1447 if (len && hdr->flags & PROXY_TYPE_PROXY)
1449 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1450 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1452 offset += len;
1453 if (offset + sizeof(DWORD) > size) goto done;
1454 len = *(DWORD *)((char *)hdr + offset);
1455 offset += sizeof(DWORD);
1456 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1458 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1459 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1461 offset += len;
1462 if (offset + sizeof(DWORD) > size) goto done;
1463 len = *(DWORD *)((char *)hdr + offset);
1464 offset += sizeof(DWORD);
1465 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1467 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1468 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1470 ret = TRUE;
1472 done:
1473 RegCloseKey( hkey );
1474 heap_free( hdr );
1475 if (!ret)
1477 heap_free( config->lpszAutoConfigUrl );
1478 config->lpszAutoConfigUrl = NULL;
1479 heap_free( config->lpszProxy );
1480 config->lpszProxy = NULL;
1481 heap_free( config->lpszProxyBypass );
1482 config->lpszProxyBypass = NULL;
1484 return ret;
1487 static HRESULT WINAPI dispex_QueryInterface(
1488 IDispatchEx *iface, REFIID riid, void **ppv )
1490 *ppv = NULL;
1492 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1493 IsEqualGUID( riid, &IID_IDispatch ) ||
1494 IsEqualGUID( riid, &IID_IDispatchEx ))
1495 *ppv = iface;
1496 else
1497 return E_NOINTERFACE;
1499 return S_OK;
1502 static ULONG WINAPI dispex_AddRef(
1503 IDispatchEx *iface )
1505 return 2;
1508 static ULONG WINAPI dispex_Release(
1509 IDispatchEx *iface )
1511 return 1;
1514 static HRESULT WINAPI dispex_GetTypeInfoCount(
1515 IDispatchEx *iface, UINT *info )
1517 return E_NOTIMPL;
1520 static HRESULT WINAPI dispex_GetTypeInfo(
1521 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1523 return E_NOTIMPL;
1526 static HRESULT WINAPI dispex_GetIDsOfNames(
1527 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1529 return E_NOTIMPL;
1532 static HRESULT WINAPI dispex_Invoke(
1533 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1534 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1536 return E_NOTIMPL;
1539 static HRESULT WINAPI dispex_DeleteMemberByName(
1540 IDispatchEx *iface, BSTR name, DWORD flags )
1542 return E_NOTIMPL;
1545 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1546 IDispatchEx *iface, DISPID id )
1548 return E_NOTIMPL;
1551 static HRESULT WINAPI dispex_GetMemberProperties(
1552 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1554 return E_NOTIMPL;
1557 static HRESULT WINAPI dispex_GetMemberName(
1558 IDispatchEx *iface, DISPID id, BSTR *name )
1560 return E_NOTIMPL;
1563 static HRESULT WINAPI dispex_GetNextDispID(
1564 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1566 return E_NOTIMPL;
1569 static HRESULT WINAPI dispex_GetNameSpaceParent(
1570 IDispatchEx *iface, IUnknown **unk )
1572 return E_NOTIMPL;
1575 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1577 static HRESULT WINAPI dispex_GetDispID(
1578 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1580 if (!strcmpW( name, dns_resolveW ))
1582 *id = DISPID_GLOBAL_DNSRESOLVE;
1583 return S_OK;
1585 return DISP_E_UNKNOWNNAME;
1588 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1590 #ifdef HAVE_GETADDRINFO
1591 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1592 WCHAR addr[16];
1593 struct addrinfo *ai, *elem;
1594 char *hostnameA;
1595 int res;
1597 if (hostname[0])
1598 hostnameA = strdupWA( hostname );
1599 else
1600 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1602 if (!hostnameA) return E_OUTOFMEMORY;
1603 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1604 heap_free( hostnameA );
1605 if (res) return S_FALSE;
1607 elem = ai;
1608 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1609 if (!elem)
1611 freeaddrinfo( ai );
1612 return S_FALSE;
1614 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1615 freeaddrinfo( ai );
1616 V_VT( result ) = VT_BSTR;
1617 V_BSTR( result ) = SysAllocString( addr );
1618 return S_OK;
1619 #else
1620 FIXME("getaddrinfo not found at build time\n");
1621 return S_FALSE;
1622 #endif
1625 static HRESULT WINAPI dispex_InvokeEx(
1626 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1627 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1629 if (id == DISPID_GLOBAL_DNSRESOLVE)
1631 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1632 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1633 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
1635 return DISP_E_MEMBERNOTFOUND;
1638 static const IDispatchExVtbl dispex_vtbl =
1640 dispex_QueryInterface,
1641 dispex_AddRef,
1642 dispex_Release,
1643 dispex_GetTypeInfoCount,
1644 dispex_GetTypeInfo,
1645 dispex_GetIDsOfNames,
1646 dispex_Invoke,
1647 dispex_GetDispID,
1648 dispex_InvokeEx,
1649 dispex_DeleteMemberByName,
1650 dispex_DeleteMemberByDispID,
1651 dispex_GetMemberProperties,
1652 dispex_GetMemberName,
1653 dispex_GetNextDispID,
1654 dispex_GetNameSpaceParent
1657 static IDispatchEx global_dispex = { &dispex_vtbl };
1659 static HRESULT WINAPI site_QueryInterface(
1660 IActiveScriptSite *iface, REFIID riid, void **ppv )
1662 *ppv = NULL;
1664 if (IsEqualGUID( &IID_IUnknown, riid ))
1665 *ppv = iface;
1666 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1667 *ppv = iface;
1668 else
1669 return E_NOINTERFACE;
1671 IUnknown_AddRef( (IUnknown *)*ppv );
1672 return S_OK;
1675 static ULONG WINAPI site_AddRef(
1676 IActiveScriptSite *iface )
1678 return 2;
1681 static ULONG WINAPI site_Release(
1682 IActiveScriptSite *iface )
1684 return 1;
1687 static HRESULT WINAPI site_GetLCID(
1688 IActiveScriptSite *iface, LCID *lcid )
1690 return E_NOTIMPL;
1693 static HRESULT WINAPI site_GetItemInfo(
1694 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1695 IUnknown **item, ITypeInfo **type_info )
1697 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1699 *item = (IUnknown *)&global_dispex;
1700 return S_OK;
1702 return E_NOTIMPL;
1705 static HRESULT WINAPI site_GetDocVersionString(
1706 IActiveScriptSite *iface, BSTR *version )
1708 return E_NOTIMPL;
1711 static HRESULT WINAPI site_OnScriptTerminate(
1712 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1714 return E_NOTIMPL;
1717 static HRESULT WINAPI site_OnStateChange(
1718 IActiveScriptSite *iface, SCRIPTSTATE state )
1720 return E_NOTIMPL;
1723 static HRESULT WINAPI site_OnScriptError(
1724 IActiveScriptSite *iface, IActiveScriptError *error )
1726 return E_NOTIMPL;
1729 static HRESULT WINAPI site_OnEnterScript(
1730 IActiveScriptSite *iface )
1732 return E_NOTIMPL;
1735 static HRESULT WINAPI site_OnLeaveScript(
1736 IActiveScriptSite *iface )
1738 return E_NOTIMPL;
1741 static const IActiveScriptSiteVtbl site_vtbl =
1743 site_QueryInterface,
1744 site_AddRef,
1745 site_Release,
1746 site_GetLCID,
1747 site_GetItemInfo,
1748 site_GetDocVersionString,
1749 site_OnScriptTerminate,
1750 site_OnStateChange,
1751 site_OnScriptError,
1752 site_OnEnterScript,
1753 site_OnLeaveScript
1756 static IActiveScriptSite script_site = { &site_vtbl };
1758 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1760 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1761 const WCHAR *p;
1762 WCHAR *q;
1763 int len;
1765 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1766 info->lpszProxy = NULL;
1767 info->lpszProxyBypass = NULL;
1769 if (V_VT( &result ) != VT_BSTR) return TRUE;
1770 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1772 p = V_BSTR( &result );
1773 while (*p == ' ') p++;
1774 len = strlenW( p );
1775 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1777 p += 5;
1778 while (*p == ' ') p++;
1779 if (!*p || *p == ';') return TRUE;
1780 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1781 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1782 for (; *q; q++)
1784 if (*q == ' ' || *q == ';')
1786 *q = 0;
1787 break;
1791 return TRUE;
1794 static BSTR include_pac_utils( BSTR script )
1796 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1797 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1798 HRSRC rsrc;
1799 DWORD size;
1800 const char *data;
1801 BSTR ret;
1802 int len;
1804 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1805 size = SizeofResource( hmod, rsrc );
1806 data = LoadResource( hmod, rsrc );
1808 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1809 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1810 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1811 ret[len] = 0;
1812 strcatW( ret, script );
1813 return ret;
1816 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1818 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1819 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1820 IActiveScriptParse *parser = NULL;
1821 IActiveScript *engine = NULL;
1822 IDispatch *dispatch = NULL;
1823 BOOL ret = FALSE;
1824 CLSID clsid;
1825 DISPID dispid;
1826 BSTR func = NULL, hostname = NULL, full_script = NULL;
1827 URL_COMPONENTSW uc;
1828 VARIANT args[2], result;
1829 DISPPARAMS params;
1830 HRESULT hr, init;
1832 memset( &uc, 0, sizeof(uc) );
1833 uc.dwStructSize = sizeof(uc);
1834 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1835 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1836 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1837 hostname[uc.dwHostNameLength] = 0;
1839 init = CoInitialize( NULL );
1840 CLSIDFromProgID( jscriptW, &clsid );
1841 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1842 &IID_IActiveScript, (void **)&engine );
1843 if (hr != S_OK) goto done;
1845 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1846 if (hr != S_OK) goto done;
1848 hr = IActiveScriptParse64_InitNew( parser );
1849 if (hr != S_OK) goto done;
1851 hr = IActiveScript_SetScriptSite( engine, &script_site );
1852 if (hr != S_OK) goto done;
1854 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1855 if (hr != S_OK) goto done;
1857 if (!(full_script = include_pac_utils( script ))) goto done;
1859 hr = IActiveScriptParse64_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1860 if (hr != S_OK) goto done;
1862 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1863 if (hr != S_OK) goto done;
1865 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1866 if (hr != S_OK) goto done;
1868 if (!(func = SysAllocString( findproxyW ))) goto done;
1869 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1870 if (hr != S_OK) goto done;
1872 V_VT( &args[0] ) = VT_BSTR;
1873 V_BSTR( &args[0] ) = hostname;
1874 V_VT( &args[1] ) = VT_BSTR;
1875 V_BSTR( &args[1] ) = SysAllocString( url );
1877 params.rgvarg = args;
1878 params.rgdispidNamedArgs = NULL;
1879 params.cArgs = 2;
1880 params.cNamedArgs = 0;
1881 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1882 &params, &result, NULL, NULL );
1883 VariantClear( &args[1] );
1884 if (hr != S_OK)
1886 WARN("script failed 0x%08x\n", hr);
1887 goto done;
1889 ret = parse_script_result( result, info );
1891 done:
1892 SysFreeString( full_script );
1893 SysFreeString( hostname );
1894 SysFreeString( func );
1895 if (dispatch) IDispatch_Release( dispatch );
1896 if (parser) IUnknown_Release( parser );
1897 if (engine) IActiveScript_Release( engine );
1898 if (SUCCEEDED( init )) CoUninitialize();
1899 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1900 return ret;
1903 static BSTR download_script( HINTERNET ses, const WCHAR *url )
1905 static const WCHAR typeW[] = {'*','/','*',0};
1906 static const WCHAR *acceptW[] = {typeW, NULL};
1907 HINTERNET con, req = NULL;
1908 WCHAR *hostname;
1909 URL_COMPONENTSW uc;
1910 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1911 char *tmp, *buffer = NULL;
1912 BSTR script = NULL;
1913 int len;
1915 memset( &uc, 0, sizeof(uc) );
1916 uc.dwStructSize = sizeof(uc);
1917 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1918 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1919 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1920 hostname[uc.dwHostNameLength] = 0;
1922 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1923 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1924 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1925 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1926 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1928 if (!(buffer = heap_alloc( size ))) goto done;
1929 to_read = size;
1930 offset = 0;
1931 for (;;)
1933 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1934 if (!bytes_read) break;
1935 to_read -= bytes_read;
1936 offset += bytes_read;
1937 if (!to_read)
1939 to_read = size;
1940 size *= 2;
1941 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1942 buffer = tmp;
1945 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1946 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1947 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1948 script[len] = 0;
1950 done:
1951 WinHttpCloseHandle( req );
1952 WinHttpCloseHandle( con );
1953 heap_free( buffer );
1954 heap_free( hostname );
1955 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1956 return script;
1959 /***********************************************************************
1960 * WinHttpGetProxyForUrl (winhttp.@)
1962 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1963 WINHTTP_PROXY_INFO *info )
1965 WCHAR *detected_pac_url = NULL;
1966 const WCHAR *pac_url;
1967 session_t *session;
1968 BSTR script;
1969 BOOL ret = FALSE;
1971 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1973 if (!(session = (session_t *)grab_object( hsession )))
1975 set_last_error( ERROR_INVALID_HANDLE );
1976 return FALSE;
1978 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1980 release_object( &session->hdr );
1981 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1982 return FALSE;
1984 if (!url || !options || !info ||
1985 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1986 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1987 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1988 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1990 release_object( &session->hdr );
1991 set_last_error( ERROR_INVALID_PARAMETER );
1992 return FALSE;
1994 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1995 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1997 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1998 goto done;
2000 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2001 else pac_url = detected_pac_url;
2003 if (!(script = download_script( hsession, pac_url ))) goto done;
2004 ret = run_script( script, url, info );
2005 SysFreeString( script );
2007 done:
2008 GlobalFree( detected_pac_url );
2009 release_object( &session->hdr );
2010 return ret;
2013 /***********************************************************************
2014 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2016 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2018 LONG l;
2019 HKEY key;
2020 BOOL ret = FALSE;
2021 const WCHAR *src;
2023 TRACE("%p\n", info);
2025 if (!info)
2027 set_last_error( ERROR_INVALID_PARAMETER );
2028 return FALSE;
2030 switch (info->dwAccessType)
2032 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2033 break;
2034 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2035 if (!info->lpszProxy)
2037 set_last_error( ERROR_INVALID_PARAMETER );
2038 return FALSE;
2040 /* Only ASCII characters are allowed */
2041 for (src = info->lpszProxy; *src; src++)
2042 if (*src > 0x7f)
2044 set_last_error( ERROR_INVALID_PARAMETER );
2045 return FALSE;
2047 if (info->lpszProxyBypass)
2049 for (src = info->lpszProxyBypass; *src; src++)
2050 if (*src > 0x7f)
2052 set_last_error( ERROR_INVALID_PARAMETER );
2053 return FALSE;
2056 break;
2057 default:
2058 set_last_error( ERROR_INVALID_PARAMETER );
2059 return FALSE;
2062 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2063 KEY_WRITE, NULL, &key, NULL );
2064 if (!l)
2066 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2067 BYTE *buf;
2069 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2071 size += strlenW( info->lpszProxy );
2072 if (info->lpszProxyBypass)
2073 size += strlenW( info->lpszProxyBypass );
2075 buf = heap_alloc( size );
2076 if (buf)
2078 struct connection_settings_header *hdr =
2079 (struct connection_settings_header *)buf;
2080 DWORD *len = (DWORD *)(hdr + 1);
2082 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2083 hdr->unknown = 0;
2084 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2086 BYTE *dst;
2088 hdr->flags = PROXY_TYPE_PROXY;
2089 *len++ = strlenW( info->lpszProxy );
2090 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2091 src++, dst++)
2092 *dst = *src;
2093 len = (DWORD *)dst;
2094 if (info->lpszProxyBypass)
2096 *len++ = strlenW( info->lpszProxyBypass );
2097 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2098 src++, dst++)
2099 *dst = *src;
2101 else
2102 *len++ = 0;
2104 else
2106 hdr->flags = PROXY_TYPE_DIRECT;
2107 *len++ = 0;
2108 *len++ = 0;
2110 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2111 if (!l)
2112 ret = TRUE;
2113 heap_free( buf );
2115 RegCloseKey( key );
2117 return ret;
2120 /***********************************************************************
2121 * WinHttpSetStatusCallback (winhttp.@)
2123 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2124 DWORD flags, DWORD_PTR reserved )
2126 object_header_t *hdr;
2127 WINHTTP_STATUS_CALLBACK ret;
2129 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2131 if (!(hdr = grab_object( handle )))
2133 set_last_error( ERROR_INVALID_HANDLE );
2134 return WINHTTP_INVALID_STATUS_CALLBACK;
2136 ret = hdr->callback;
2137 hdr->callback = callback;
2138 hdr->notify_mask = flags;
2140 release_object( hdr );
2141 return ret;
2144 /***********************************************************************
2145 * WinHttpSetTimeouts (winhttp.@)
2147 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2149 BOOL ret = TRUE;
2150 object_header_t *hdr;
2151 request_t *request;
2152 session_t *session;
2154 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2156 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2158 set_last_error( ERROR_INVALID_PARAMETER );
2159 return FALSE;
2162 if (!(hdr = grab_object( handle )))
2164 set_last_error( ERROR_INVALID_HANDLE );
2165 return FALSE;
2168 switch(hdr->type)
2170 case WINHTTP_HANDLE_TYPE_REQUEST:
2171 request = (request_t *)hdr;
2172 request->connect_timeout = connect;
2174 if (resolve < 0) resolve = 0;
2175 request->resolve_timeout = resolve;
2177 if (send < 0) send = 0;
2178 request->send_timeout = send;
2180 if (receive < 0) receive = 0;
2181 request->recv_timeout = receive;
2183 if (netconn_connected( &request->netconn ))
2185 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2186 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2189 release_object( &request->hdr );
2190 break;
2192 case WINHTTP_HANDLE_TYPE_SESSION:
2193 session = (session_t *)hdr;
2194 session->connect_timeout = connect;
2196 if (resolve < 0) resolve = 0;
2197 session->resolve_timeout = resolve;
2199 if (send < 0) send = 0;
2200 session->send_timeout = send;
2202 if (receive < 0) receive = 0;
2203 session->recv_timeout = receive;
2204 break;
2206 default:
2207 release_object( hdr );
2208 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2209 return FALSE;
2211 return ret;
2214 static const WCHAR wkday[7][4] =
2215 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2216 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2217 static const WCHAR month[12][4] =
2218 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2219 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2220 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2222 /***********************************************************************
2223 * WinHttpTimeFromSystemTime (WININET.@)
2225 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2227 static const WCHAR format[] =
2228 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2229 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2231 TRACE("%p, %p\n", time, string);
2233 if (!time || !string) return FALSE;
2235 sprintfW( string, format,
2236 wkday[time->wDayOfWeek],
2237 time->wDay,
2238 month[time->wMonth - 1],
2239 time->wYear,
2240 time->wHour,
2241 time->wMinute,
2242 time->wSecond );
2244 return TRUE;
2247 /***********************************************************************
2248 * WinHttpTimeToSystemTime (WININET.@)
2250 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2252 unsigned int i;
2253 const WCHAR *s = string;
2254 WCHAR *end;
2256 TRACE("%s, %p\n", debugstr_w(string), time);
2258 if (!string || !time) return FALSE;
2260 /* Windows does this too */
2261 GetSystemTime( time );
2263 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2264 * a SYSTEMTIME structure.
2267 while (*s && !isalphaW( *s )) s++;
2268 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2269 time->wDayOfWeek = 7;
2271 for (i = 0; i < 7; i++)
2273 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2274 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2275 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2277 time->wDayOfWeek = i;
2278 break;
2282 if (time->wDayOfWeek > 6) return TRUE;
2283 while (*s && !isdigitW( *s )) s++;
2284 time->wDay = strtolW( s, &end, 10 );
2285 s = end;
2287 while (*s && !isalphaW( *s )) s++;
2288 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2289 time->wMonth = 0;
2291 for (i = 0; i < 12; i++)
2293 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2294 toupperW( month[i][1]) == toupperW( s[1] ) &&
2295 toupperW( month[i][2]) == toupperW( s[2] ) )
2297 time->wMonth = i + 1;
2298 break;
2301 if (time->wMonth == 0) return TRUE;
2303 while (*s && !isdigitW( *s )) s++;
2304 if (*s == '\0') return TRUE;
2305 time->wYear = strtolW( s, &end, 10 );
2306 s = end;
2308 while (*s && !isdigitW( *s )) s++;
2309 if (*s == '\0') return TRUE;
2310 time->wHour = strtolW( s, &end, 10 );
2311 s = end;
2313 while (*s && !isdigitW( *s )) s++;
2314 if (*s == '\0') return TRUE;
2315 time->wMinute = strtolW( s, &end, 10 );
2316 s = end;
2318 while (*s && !isdigitW( *s )) s++;
2319 if (*s == '\0') return TRUE;
2320 time->wSecond = strtolW( s, &end, 10 );
2322 time->wMilliseconds = 0;
2323 return TRUE;