dsound: Merge IUnknown into the main DirectSound object.
[wine/multimedia.git] / dlls / winhttp / session.c
blobe3e5e2bd703506450a3783c65575302564a8e5c0
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( info.lpszProxy );
233 GlobalFree( info.lpszProxyBypass );
234 goto end;
236 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
238 GlobalFree( info.lpszProxy );
239 GlobalFree( 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 connect->resolved = FALSE;
427 if (!(connect->servername = heap_alloc(
428 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
430 ret = FALSE;
431 goto end;
433 memcpy( connect->servername, session->proxy_server,
434 (colon - session->proxy_server) * sizeof(WCHAR) );
435 connect->servername[colon - session->proxy_server] = 0;
436 if (*(colon + 1))
437 connect->serverport = atoiW( colon + 1 );
438 else
439 connect->serverport = INTERNET_DEFAULT_PORT;
442 else
444 if (!connect->servername || strcmpiW( connect->servername,
445 session->proxy_server ))
447 heap_free( connect->servername );
448 connect->resolved = FALSE;
449 if (!(connect->servername = strdupW( session->proxy_server )))
451 ret = FALSE;
452 goto end;
454 connect->serverport = INTERNET_DEFAULT_PORT;
458 else if (server)
460 heap_free( connect->servername );
461 connect->resolved = FALSE;
462 if (!(connect->servername = strdupW( server )))
464 ret = FALSE;
465 goto end;
467 connect->serverport = port;
469 end:
470 return ret;
473 /***********************************************************************
474 * WinHttpConnect (winhttp.@)
476 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
478 connect_t *connect;
479 session_t *session;
480 HINTERNET hconnect = NULL;
482 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
484 if (!server)
486 set_last_error( ERROR_INVALID_PARAMETER );
487 return NULL;
489 if (!(session = (session_t *)grab_object( hsession )))
491 set_last_error( ERROR_INVALID_HANDLE );
492 return NULL;
494 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
496 release_object( &session->hdr );
497 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
498 return NULL;
500 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
502 release_object( &session->hdr );
503 return NULL;
505 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
506 connect->hdr.vtbl = &connect_vtbl;
507 connect->hdr.refs = 1;
508 connect->hdr.flags = session->hdr.flags;
509 connect->hdr.callback = session->hdr.callback;
510 connect->hdr.notify_mask = session->hdr.notify_mask;
511 connect->hdr.context = session->hdr.context;
512 list_init( &connect->hdr.children );
514 addref_object( &session->hdr );
515 connect->session = session;
516 list_add_head( &session->hdr.children, &connect->hdr.entry );
518 if (!(connect->hostname = strdupW( server ))) goto end;
519 connect->hostport = port;
520 if (!set_server_for_hostname( connect, server, port )) goto end;
522 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
523 connect->hdr.handle = hconnect;
525 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
527 end:
528 release_object( &connect->hdr );
529 release_object( &session->hdr );
530 TRACE("returning %p\n", hconnect);
531 return hconnect;
534 /***********************************************************************
535 * request_destroy (internal)
537 static void request_destroy( object_header_t *hdr )
539 request_t *request = (request_t *)hdr;
540 unsigned int i;
542 TRACE("%p\n", request);
544 release_object( &request->connect->hdr );
546 heap_free( request->verb );
547 heap_free( request->path );
548 heap_free( request->version );
549 heap_free( request->raw_headers );
550 heap_free( request->status_text );
551 for (i = 0; i < request->num_headers; i++)
553 heap_free( request->headers[i].field );
554 heap_free( request->headers[i].value );
556 heap_free( request->headers );
557 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
558 heap_free( request->accept_types );
559 heap_free( request );
562 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
564 int len = 0;
565 if (str) len = strlenW( str );
566 if (buffer && *buflen > len)
568 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
569 buffer[len] = 0;
571 *buflen = len * sizeof(WCHAR);
574 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
576 WCHAR *ret;
577 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
579 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
580 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
581 CertNameToStrW( encoding, blob, format, ret, size );
583 return ret;
586 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
588 request_t *request = (request_t *)hdr;
590 switch (option)
592 case WINHTTP_OPTION_SECURITY_FLAGS:
594 DWORD flags;
595 int bits;
597 if (!buffer || *buflen < sizeof(flags))
599 *buflen = sizeof(flags);
600 set_last_error( ERROR_INSUFFICIENT_BUFFER );
601 return FALSE;
604 flags = 0;
605 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
606 flags |= request->netconn.security_flags;
607 bits = netconn_get_cipher_strength( &request->netconn );
608 if (bits >= 128)
609 flags |= SECURITY_FLAG_STRENGTH_STRONG;
610 else if (bits >= 56)
611 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
612 else
613 flags |= SECURITY_FLAG_STRENGTH_WEAK;
614 *(DWORD *)buffer = flags;
615 *buflen = sizeof(flags);
616 return TRUE;
618 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
620 const CERT_CONTEXT *cert;
622 if (!buffer || *buflen < sizeof(cert))
624 *buflen = sizeof(cert);
625 set_last_error( ERROR_INSUFFICIENT_BUFFER );
626 return FALSE;
629 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
630 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
631 *buflen = sizeof(cert);
632 return TRUE;
634 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
636 const CERT_CONTEXT *cert;
637 const CRYPT_OID_INFO *oidInfo;
638 WINHTTP_CERTIFICATE_INFO *ci = buffer;
640 FIXME("partial stub\n");
642 if (!buffer || *buflen < sizeof(*ci))
644 *buflen = sizeof(*ci);
645 set_last_error( ERROR_INSUFFICIENT_BUFFER );
646 return FALSE;
648 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
650 ci->ftExpiry = cert->pCertInfo->NotAfter;
651 ci->ftStart = cert->pCertInfo->NotBefore;
652 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
653 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
654 ci->lpszProtocolName = NULL;
655 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
656 cert->pCertInfo->SignatureAlgorithm.pszObjId,
657 0 );
658 if (oidInfo)
659 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
660 else
661 ci->lpszSignatureAlgName = NULL;
662 ci->lpszEncryptionAlgName = NULL;
663 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
665 CertFreeCertificateContext( cert );
666 *buflen = sizeof(*ci);
667 return TRUE;
669 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
671 if (!buffer || *buflen < sizeof(DWORD))
673 *buflen = sizeof(DWORD);
674 set_last_error( ERROR_INSUFFICIENT_BUFFER );
675 return FALSE;
678 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
679 *buflen = sizeof(DWORD);
680 return TRUE;
682 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
683 *(DWORD *)buffer = request->resolve_timeout;
684 *buflen = sizeof(DWORD);
685 return TRUE;
686 case WINHTTP_OPTION_CONNECT_TIMEOUT:
687 *(DWORD *)buffer = request->connect_timeout;
688 *buflen = sizeof(DWORD);
689 return TRUE;
690 case WINHTTP_OPTION_SEND_TIMEOUT:
691 *(DWORD *)buffer = request->send_timeout;
692 *buflen = sizeof(DWORD);
693 return TRUE;
694 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
695 *(DWORD *)buffer = request->recv_timeout;
696 *buflen = sizeof(DWORD);
697 return TRUE;
699 case WINHTTP_OPTION_USERNAME:
700 str_to_buffer( buffer, request->connect->username, buflen );
701 return TRUE;
703 case WINHTTP_OPTION_PASSWORD:
704 str_to_buffer( buffer, request->connect->password, buflen );
705 return TRUE;
707 case WINHTTP_OPTION_PROXY_USERNAME:
708 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
709 return TRUE;
711 case WINHTTP_OPTION_PROXY_PASSWORD:
712 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
713 return TRUE;
715 default:
716 FIXME("unimplemented option %u\n", option);
717 set_last_error( ERROR_INVALID_PARAMETER );
718 return FALSE;
722 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
724 WCHAR *ret;
725 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
727 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
728 ret[buflen] = 0;
729 return ret;
731 set_last_error( ERROR_OUTOFMEMORY );
732 return NULL;
735 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
737 request_t *request = (request_t *)hdr;
739 switch (option)
741 case WINHTTP_OPTION_PROXY:
743 WINHTTP_PROXY_INFO *pi = buffer;
745 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
746 return TRUE;
748 case WINHTTP_OPTION_DISABLE_FEATURE:
750 DWORD disable;
752 if (buflen != sizeof(DWORD))
754 set_last_error( ERROR_INSUFFICIENT_BUFFER );
755 return FALSE;
758 disable = *(DWORD *)buffer;
759 TRACE("0x%x\n", disable);
760 hdr->disable_flags |= disable;
761 return TRUE;
763 case WINHTTP_OPTION_AUTOLOGON_POLICY:
765 DWORD policy;
767 if (buflen != sizeof(DWORD))
769 set_last_error( ERROR_INSUFFICIENT_BUFFER );
770 return FALSE;
773 policy = *(DWORD *)buffer;
774 TRACE("0x%x\n", policy);
775 hdr->logon_policy = policy;
776 return TRUE;
778 case WINHTTP_OPTION_REDIRECT_POLICY:
780 DWORD policy;
782 if (buflen != sizeof(DWORD))
784 set_last_error( ERROR_INSUFFICIENT_BUFFER );
785 return FALSE;
788 policy = *(DWORD *)buffer;
789 TRACE("0x%x\n", policy);
790 hdr->redirect_policy = policy;
791 return TRUE;
793 case WINHTTP_OPTION_SECURITY_FLAGS:
795 DWORD flags;
797 if (buflen < sizeof(DWORD))
799 set_last_error( ERROR_INSUFFICIENT_BUFFER );
800 return FALSE;
802 flags = *(DWORD *)buffer;
803 TRACE("0x%x\n", flags);
804 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
805 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
806 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
807 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
809 set_last_error( ERROR_INVALID_PARAMETER );
810 return FALSE;
812 request->netconn.security_flags = flags;
813 return TRUE;
815 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
816 request->resolve_timeout = *(DWORD *)buffer;
817 return TRUE;
818 case WINHTTP_OPTION_CONNECT_TIMEOUT:
819 request->connect_timeout = *(DWORD *)buffer;
820 return TRUE;
821 case WINHTTP_OPTION_SEND_TIMEOUT:
822 request->send_timeout = *(DWORD *)buffer;
823 return TRUE;
824 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
825 request->recv_timeout = *(DWORD *)buffer;
826 return TRUE;
828 case WINHTTP_OPTION_USERNAME:
830 connect_t *connect = request->connect;
832 heap_free( connect->username );
833 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
834 return TRUE;
836 case WINHTTP_OPTION_PASSWORD:
838 connect_t *connect = request->connect;
840 heap_free( connect->password );
841 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
842 return TRUE;
844 case WINHTTP_OPTION_PROXY_USERNAME:
846 session_t *session = request->connect->session;
848 heap_free( session->proxy_username );
849 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
850 return TRUE;
852 case WINHTTP_OPTION_PROXY_PASSWORD:
854 session_t *session = request->connect->session;
856 heap_free( session->proxy_password );
857 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
858 return TRUE;
860 default:
861 FIXME("unimplemented option %u\n", option);
862 set_last_error( ERROR_INVALID_PARAMETER );
863 return TRUE;
867 static const object_vtbl_t request_vtbl =
869 request_destroy,
870 request_query_option,
871 request_set_option
874 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
876 const WCHAR **types = accept_types;
877 int i;
879 if (!types) return TRUE;
880 while (*types)
882 request->num_accept_types++;
883 types++;
885 if (!request->num_accept_types) return TRUE;
886 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
888 request->num_accept_types = 0;
889 return FALSE;
891 types = accept_types;
892 for (i = 0; i < request->num_accept_types; i++)
894 if (!(request->accept_types[i] = strdupW( *types )))
896 for (; i >= 0; i--) heap_free( request->accept_types[i] );
897 heap_free( request->accept_types );
898 request->accept_types = NULL;
899 request->num_accept_types = 0;
900 return FALSE;
902 types++;
904 return TRUE;
907 /***********************************************************************
908 * WinHttpOpenRequest (winhttp.@)
910 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
911 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
913 request_t *request;
914 connect_t *connect;
915 HINTERNET hrequest = NULL;
917 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
918 debugstr_w(version), debugstr_w(referrer), types, flags);
920 if(types && TRACE_ON(winhttp)) {
921 const WCHAR **iter;
923 TRACE("accept types:\n");
924 for(iter = types; *iter; iter++)
925 TRACE(" %s\n", debugstr_w(*iter));
928 if (!(connect = (connect_t *)grab_object( hconnect )))
930 set_last_error( ERROR_INVALID_HANDLE );
931 return NULL;
933 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
935 release_object( &connect->hdr );
936 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
937 return NULL;
939 if (!(request = heap_alloc_zero( sizeof(request_t) )))
941 release_object( &connect->hdr );
942 return NULL;
944 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
945 request->hdr.vtbl = &request_vtbl;
946 request->hdr.refs = 1;
947 request->hdr.flags = flags;
948 request->hdr.callback = connect->hdr.callback;
949 request->hdr.notify_mask = connect->hdr.notify_mask;
950 request->hdr.context = connect->hdr.context;
951 list_init( &request->hdr.children );
953 addref_object( &connect->hdr );
954 request->connect = connect;
955 list_add_head( &connect->hdr.children, &request->hdr.entry );
957 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
958 request->resolve_timeout = connect->session->resolve_timeout;
959 request->connect_timeout = connect->session->connect_timeout;
960 request->send_timeout = connect->session->send_timeout;
961 request->recv_timeout = connect->session->recv_timeout;
963 if (!verb || !verb[0]) verb = getW;
964 if (!(request->verb = strdupW( verb ))) goto end;
966 if (object)
968 WCHAR *path, *p;
969 unsigned int len;
971 len = strlenW( object ) + 1;
972 if (object[0] != '/') len++;
973 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
975 if (object[0] != '/') *p++ = '/';
976 strcpyW( p, object );
977 request->path = path;
979 else if (!(request->path = strdupW( slashW ))) goto end;
981 if (!version || !version[0]) version = http1_1;
982 if (!(request->version = strdupW( version ))) goto end;
983 if (!(store_accept_types( request, types ))) goto end;
985 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
986 request->hdr.handle = hrequest;
988 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
990 end:
991 release_object( &request->hdr );
992 release_object( &connect->hdr );
993 TRACE("returning %p\n", hrequest);
994 return hrequest;
997 /***********************************************************************
998 * WinHttpCloseHandle (winhttp.@)
1000 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1002 object_header_t *hdr;
1004 TRACE("%p\n", handle);
1006 if (!(hdr = grab_object( handle )))
1008 set_last_error( ERROR_INVALID_HANDLE );
1009 return FALSE;
1011 release_object( hdr );
1012 free_handle( handle );
1013 return TRUE;
1016 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1018 BOOL ret = FALSE;
1020 if (!buflen)
1022 set_last_error( ERROR_INVALID_PARAMETER );
1023 return FALSE;
1026 switch (option)
1028 case WINHTTP_OPTION_CONTEXT_VALUE:
1030 if (!buffer || *buflen < sizeof(DWORD_PTR))
1032 *buflen = sizeof(DWORD_PTR);
1033 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1034 return FALSE;
1037 *(DWORD_PTR *)buffer = hdr->context;
1038 *buflen = sizeof(DWORD_PTR);
1039 return TRUE;
1041 default:
1042 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1043 else
1045 FIXME("unimplemented option %u\n", option);
1046 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1047 return FALSE;
1049 break;
1051 return ret;
1054 /***********************************************************************
1055 * WinHttpQueryOption (winhttp.@)
1057 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1059 BOOL ret = FALSE;
1060 object_header_t *hdr;
1062 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1064 if (!(hdr = grab_object( handle )))
1066 set_last_error( ERROR_INVALID_HANDLE );
1067 return FALSE;
1070 ret = query_option( hdr, option, buffer, buflen );
1072 release_object( hdr );
1073 return ret;
1076 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1078 BOOL ret = TRUE;
1080 if (!buffer)
1082 set_last_error( ERROR_INVALID_PARAMETER );
1083 return FALSE;
1086 switch (option)
1088 case WINHTTP_OPTION_CONTEXT_VALUE:
1090 if (buflen != sizeof(DWORD_PTR))
1092 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1093 return FALSE;
1096 hdr->context = *(DWORD_PTR *)buffer;
1097 return TRUE;
1099 default:
1100 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1101 else
1103 FIXME("unimplemented option %u\n", option);
1104 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1105 return FALSE;
1107 break;
1109 return ret;
1112 /***********************************************************************
1113 * WinHttpSetOption (winhttp.@)
1115 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1117 BOOL ret = FALSE;
1118 object_header_t *hdr;
1120 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1122 if (!(hdr = grab_object( handle )))
1124 set_last_error( ERROR_INVALID_HANDLE );
1125 return FALSE;
1128 ret = set_option( hdr, option, buffer, buflen );
1130 release_object( hdr );
1131 return ret;
1134 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1136 char *ret;
1137 DWORD size = 0;
1139 GetComputerNameExA( format, NULL, &size );
1140 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1141 if (!(ret = heap_alloc( size ))) return NULL;
1142 if (!GetComputerNameExA( format, ret, &size ))
1144 heap_free( ret );
1145 return NULL;
1147 return ret;
1150 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1152 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1154 if (len_suffix > len_domain) return FALSE;
1155 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1156 return FALSE;
1159 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
1161 sprintfW( buf, fmt,
1162 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
1163 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
1164 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
1165 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
1168 static WCHAR *build_wpad_url( const struct addrinfo *ai )
1170 static const WCHAR fmtW[] =
1171 {'h','t','t','p',':','/','/','%','u','.','%','u','.','%','u','.','%','u',
1172 '/','w','p','a','d','.','d','a','t',0};
1173 WCHAR *ret;
1175 while (ai && ai->ai_family != AF_INET) ai = ai->ai_next;
1176 if (!ai) return NULL;
1178 if (!(ret = GlobalAlloc( 0, sizeof(fmtW) + 12 * sizeof(WCHAR) ))) return NULL;
1179 printf_addr( fmtW, ret, (struct sockaddr_in *)ai->ai_addr );
1180 return ret;
1183 /***********************************************************************
1184 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1186 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1188 BOOL ret = FALSE;
1190 TRACE("0x%08x, %p\n", flags, url);
1192 if (!flags || !url)
1194 set_last_error( ERROR_INVALID_PARAMETER );
1195 return FALSE;
1197 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1199 static int fixme_shown;
1200 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1202 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1204 #ifdef HAVE_GETADDRINFO
1205 char *fqdn, *domain, *p;
1207 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1208 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1210 heap_free( fqdn );
1211 return FALSE;
1213 p = fqdn;
1214 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1216 struct addrinfo *ai;
1217 char *name;
1218 int res;
1220 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1222 heap_free( fqdn );
1223 heap_free( domain );
1224 return FALSE;
1226 strcpy( name, "wpad" );
1227 strcat( name, p );
1228 res = getaddrinfo( name, NULL, NULL, &ai );
1229 heap_free( name );
1230 if (!res)
1232 *url = build_wpad_url( ai );
1233 freeaddrinfo( ai );
1234 if (*url)
1236 TRACE("returning %s\n", debugstr_w(*url));
1237 ret = TRUE;
1238 break;
1241 p++;
1243 heap_free( domain );
1244 heap_free( fqdn );
1245 #else
1246 FIXME("getaddrinfo not found at build time\n");
1247 #endif
1249 if (!ret) set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1250 return ret;
1253 static const WCHAR Connections[] = {
1254 'S','o','f','t','w','a','r','e','\\',
1255 'M','i','c','r','o','s','o','f','t','\\',
1256 'W','i','n','d','o','w','s','\\',
1257 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1258 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1259 'C','o','n','n','e','c','t','i','o','n','s',0 };
1260 static const WCHAR WinHttpSettings[] = {
1261 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1262 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1263 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1264 static const DWORD PROXY_TYPE_DIRECT = 1;
1265 static const DWORD PROXY_TYPE_PROXY = 2;
1266 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1267 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1269 struct connection_settings_header
1271 DWORD magic;
1272 DWORD unknown; /* always zero? */
1273 DWORD flags; /* one or more of PROXY_* */
1276 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1278 const BYTE *begin;
1280 for (begin = src; src - begin < len; src++, dst++)
1281 *dst = *src;
1282 *dst = 0;
1285 /***********************************************************************
1286 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1288 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1290 LONG l;
1291 HKEY key;
1292 BOOL got_from_reg = FALSE, direct = TRUE;
1293 char *envproxy;
1295 TRACE("%p\n", info);
1297 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1298 if (!l)
1300 DWORD type, size = 0;
1302 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1303 if (!l && type == REG_BINARY &&
1304 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1306 BYTE *buf = heap_alloc( size );
1308 if (buf)
1310 struct connection_settings_header *hdr =
1311 (struct connection_settings_header *)buf;
1312 DWORD *len = (DWORD *)(hdr + 1);
1314 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1315 &size );
1316 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1317 hdr->unknown == 0)
1319 if (hdr->flags & PROXY_TYPE_PROXY)
1321 BOOL sane = FALSE;
1322 LPWSTR proxy = NULL;
1323 LPWSTR proxy_bypass = NULL;
1325 /* Sanity-check length of proxy string */
1326 if ((BYTE *)len - buf + *len <= size)
1328 sane = TRUE;
1329 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1330 if (proxy)
1331 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1332 len = (DWORD *)((BYTE *)(len + 1) + *len);
1334 if (sane)
1336 /* Sanity-check length of proxy bypass string */
1337 if ((BYTE *)len - buf + *len <= size)
1339 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1340 if (proxy_bypass)
1341 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1343 else
1345 sane = FALSE;
1346 GlobalFree( proxy );
1347 proxy = NULL;
1350 info->lpszProxy = proxy;
1351 info->lpszProxyBypass = proxy_bypass;
1352 if (sane)
1354 got_from_reg = TRUE;
1355 direct = FALSE;
1356 info->dwAccessType =
1357 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1358 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1359 debugstr_w(info->lpszProxy),
1360 debugstr_w(info->lpszProxyBypass));
1364 heap_free( buf );
1367 RegCloseKey( key );
1369 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1371 char *colon, *http_proxy;
1373 if ((colon = strchr( envproxy, ':' )))
1375 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1377 static const char http[] = "http://";
1379 /* It's a scheme, check that it's http */
1380 if (!strncmp( envproxy, http, strlen( http ) ))
1381 http_proxy = envproxy + strlen( http );
1382 else
1384 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1385 http_proxy = NULL;
1388 else
1389 http_proxy = envproxy;
1391 else
1392 http_proxy = envproxy;
1393 if (http_proxy)
1395 WCHAR *http_proxyW;
1396 int len;
1398 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1399 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1401 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1402 direct = FALSE;
1403 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1404 info->lpszProxy = http_proxyW;
1405 info->lpszProxyBypass = NULL;
1406 TRACE("http proxy (from environment) = %s\n",
1407 debugstr_w(info->lpszProxy));
1411 if (direct)
1413 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1414 info->lpszProxy = NULL;
1415 info->lpszProxyBypass = NULL;
1417 return TRUE;
1420 /***********************************************************************
1421 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1423 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1425 static const WCHAR settingsW[] =
1426 {'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};
1427 HKEY hkey = NULL;
1428 struct connection_settings_header *hdr = NULL;
1429 DWORD type, offset, len, size = 0;
1430 BOOL ret = FALSE;
1432 TRACE("%p\n", config);
1434 if (!config)
1436 set_last_error( ERROR_INVALID_PARAMETER );
1437 return FALSE;
1439 memset( config, 0, sizeof(*config) );
1440 config->fAutoDetect = TRUE;
1442 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1443 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1444 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1446 ret = TRUE;
1447 goto done;
1449 if (!(hdr = heap_alloc( size ))) goto done;
1450 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1451 hdr->magic != WININET_SETTINGS_MAGIC)
1453 ret = TRUE;
1454 goto done;
1457 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1458 offset = sizeof(*hdr);
1459 if (offset + sizeof(DWORD) > size) goto done;
1460 len = *(DWORD *)((char *)hdr + offset);
1461 offset += sizeof(DWORD);
1462 if (len && hdr->flags & PROXY_TYPE_PROXY)
1464 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1465 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1467 offset += len;
1468 if (offset + sizeof(DWORD) > size) goto done;
1469 len = *(DWORD *)((char *)hdr + offset);
1470 offset += sizeof(DWORD);
1471 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1473 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1474 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1476 offset += len;
1477 if (offset + sizeof(DWORD) > size) goto done;
1478 len = *(DWORD *)((char *)hdr + offset);
1479 offset += sizeof(DWORD);
1480 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1482 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1483 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1485 ret = TRUE;
1487 done:
1488 RegCloseKey( hkey );
1489 heap_free( hdr );
1490 if (!ret)
1492 heap_free( config->lpszAutoConfigUrl );
1493 config->lpszAutoConfigUrl = NULL;
1494 heap_free( config->lpszProxy );
1495 config->lpszProxy = NULL;
1496 heap_free( config->lpszProxyBypass );
1497 config->lpszProxyBypass = NULL;
1499 return ret;
1502 static HRESULT WINAPI dispex_QueryInterface(
1503 IDispatchEx *iface, REFIID riid, void **ppv )
1505 *ppv = NULL;
1507 if (IsEqualGUID( riid, &IID_IUnknown ) ||
1508 IsEqualGUID( riid, &IID_IDispatch ) ||
1509 IsEqualGUID( riid, &IID_IDispatchEx ))
1510 *ppv = iface;
1511 else
1512 return E_NOINTERFACE;
1514 return S_OK;
1517 static ULONG WINAPI dispex_AddRef(
1518 IDispatchEx *iface )
1520 return 2;
1523 static ULONG WINAPI dispex_Release(
1524 IDispatchEx *iface )
1526 return 1;
1529 static HRESULT WINAPI dispex_GetTypeInfoCount(
1530 IDispatchEx *iface, UINT *info )
1532 return E_NOTIMPL;
1535 static HRESULT WINAPI dispex_GetTypeInfo(
1536 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
1538 return E_NOTIMPL;
1541 static HRESULT WINAPI dispex_GetIDsOfNames(
1542 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
1544 return E_NOTIMPL;
1547 static HRESULT WINAPI dispex_Invoke(
1548 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
1549 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
1551 return E_NOTIMPL;
1554 static HRESULT WINAPI dispex_DeleteMemberByName(
1555 IDispatchEx *iface, BSTR name, DWORD flags )
1557 return E_NOTIMPL;
1560 static HRESULT WINAPI dispex_DeleteMemberByDispID(
1561 IDispatchEx *iface, DISPID id )
1563 return E_NOTIMPL;
1566 static HRESULT WINAPI dispex_GetMemberProperties(
1567 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
1569 return E_NOTIMPL;
1572 static HRESULT WINAPI dispex_GetMemberName(
1573 IDispatchEx *iface, DISPID id, BSTR *name )
1575 return E_NOTIMPL;
1578 static HRESULT WINAPI dispex_GetNextDispID(
1579 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
1581 return E_NOTIMPL;
1584 static HRESULT WINAPI dispex_GetNameSpaceParent(
1585 IDispatchEx *iface, IUnknown **unk )
1587 return E_NOTIMPL;
1590 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
1592 static HRESULT WINAPI dispex_GetDispID(
1593 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
1595 if (!strcmpW( name, dns_resolveW ))
1597 *id = DISPID_GLOBAL_DNSRESOLVE;
1598 return S_OK;
1600 return DISP_E_UNKNOWNNAME;
1603 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
1605 #ifdef HAVE_GETADDRINFO
1606 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1607 WCHAR addr[16];
1608 struct addrinfo *ai, *elem;
1609 char *hostnameA;
1610 int res;
1612 if (hostname[0])
1613 hostnameA = strdupWA( hostname );
1614 else
1615 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
1617 if (!hostnameA) return E_OUTOFMEMORY;
1618 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
1619 heap_free( hostnameA );
1620 if (res) return S_FALSE;
1622 elem = ai;
1623 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
1624 if (!elem)
1626 freeaddrinfo( ai );
1627 return S_FALSE;
1629 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
1630 freeaddrinfo( ai );
1631 V_VT( result ) = VT_BSTR;
1632 V_BSTR( result ) = SysAllocString( addr );
1633 return S_OK;
1634 #else
1635 FIXME("getaddrinfo not found at build time\n");
1636 return S_FALSE;
1637 #endif
1640 static HRESULT WINAPI dispex_InvokeEx(
1641 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1642 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
1644 if (id == DISPID_GLOBAL_DNSRESOLVE)
1646 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
1647 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
1648 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
1650 return DISP_E_MEMBERNOTFOUND;
1653 static const IDispatchExVtbl dispex_vtbl =
1655 dispex_QueryInterface,
1656 dispex_AddRef,
1657 dispex_Release,
1658 dispex_GetTypeInfoCount,
1659 dispex_GetTypeInfo,
1660 dispex_GetIDsOfNames,
1661 dispex_Invoke,
1662 dispex_GetDispID,
1663 dispex_InvokeEx,
1664 dispex_DeleteMemberByName,
1665 dispex_DeleteMemberByDispID,
1666 dispex_GetMemberProperties,
1667 dispex_GetMemberName,
1668 dispex_GetNextDispID,
1669 dispex_GetNameSpaceParent
1672 static IDispatchEx global_dispex = { &dispex_vtbl };
1674 static HRESULT WINAPI site_QueryInterface(
1675 IActiveScriptSite *iface, REFIID riid, void **ppv )
1677 *ppv = NULL;
1679 if (IsEqualGUID( &IID_IUnknown, riid ))
1680 *ppv = iface;
1681 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
1682 *ppv = iface;
1683 else
1684 return E_NOINTERFACE;
1686 IUnknown_AddRef( (IUnknown *)*ppv );
1687 return S_OK;
1690 static ULONG WINAPI site_AddRef(
1691 IActiveScriptSite *iface )
1693 return 2;
1696 static ULONG WINAPI site_Release(
1697 IActiveScriptSite *iface )
1699 return 1;
1702 static HRESULT WINAPI site_GetLCID(
1703 IActiveScriptSite *iface, LCID *lcid )
1705 return E_NOTIMPL;
1708 static HRESULT WINAPI site_GetItemInfo(
1709 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
1710 IUnknown **item, ITypeInfo **type_info )
1712 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
1714 *item = (IUnknown *)&global_dispex;
1715 return S_OK;
1717 return E_NOTIMPL;
1720 static HRESULT WINAPI site_GetDocVersionString(
1721 IActiveScriptSite *iface, BSTR *version )
1723 return E_NOTIMPL;
1726 static HRESULT WINAPI site_OnScriptTerminate(
1727 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
1729 return E_NOTIMPL;
1732 static HRESULT WINAPI site_OnStateChange(
1733 IActiveScriptSite *iface, SCRIPTSTATE state )
1735 return E_NOTIMPL;
1738 static HRESULT WINAPI site_OnScriptError(
1739 IActiveScriptSite *iface, IActiveScriptError *error )
1741 return E_NOTIMPL;
1744 static HRESULT WINAPI site_OnEnterScript(
1745 IActiveScriptSite *iface )
1747 return E_NOTIMPL;
1750 static HRESULT WINAPI site_OnLeaveScript(
1751 IActiveScriptSite *iface )
1753 return E_NOTIMPL;
1756 static const IActiveScriptSiteVtbl site_vtbl =
1758 site_QueryInterface,
1759 site_AddRef,
1760 site_Release,
1761 site_GetLCID,
1762 site_GetItemInfo,
1763 site_GetDocVersionString,
1764 site_OnScriptTerminate,
1765 site_OnStateChange,
1766 site_OnScriptError,
1767 site_OnEnterScript,
1768 site_OnLeaveScript
1771 static IActiveScriptSite script_site = { &site_vtbl };
1773 static BOOL parse_script_result( VARIANT result, WINHTTP_PROXY_INFO *info )
1775 static const WCHAR proxyW[] = {'P','R','O','X','Y'};
1776 const WCHAR *p;
1777 WCHAR *q;
1778 int len;
1780 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1781 info->lpszProxy = NULL;
1782 info->lpszProxyBypass = NULL;
1784 if (V_VT( &result ) != VT_BSTR) return TRUE;
1785 TRACE("%s\n", debugstr_w( V_BSTR( &result ) ));
1787 p = V_BSTR( &result );
1788 while (*p == ' ') p++;
1789 len = strlenW( p );
1790 if (len >= 5 && !memicmpW( p, proxyW, sizeof(proxyW)/sizeof(WCHAR) ))
1792 p += 5;
1793 while (*p == ' ') p++;
1794 if (!*p || *p == ';') return TRUE;
1795 if (!(info->lpszProxy = q = strdupW( p ))) return FALSE;
1796 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1797 for (; *q; q++)
1799 if (*q == ' ' || *q == ';')
1801 *q = 0;
1802 break;
1806 return TRUE;
1809 static BSTR include_pac_utils( BSTR script )
1811 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
1812 HMODULE hmod = GetModuleHandleA( "winhttp.dll" );
1813 HRSRC rsrc;
1814 DWORD size;
1815 const char *data;
1816 BSTR ret;
1817 int len;
1819 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
1820 size = SizeofResource( hmod, rsrc );
1821 data = LoadResource( hmod, rsrc );
1823 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
1824 if (!(ret = SysAllocStringLen( NULL, len + SysStringLen( script ) + 1 ))) return NULL;
1825 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
1826 ret[len] = 0;
1827 strcatW( ret, script );
1828 return ret;
1831 static BOOL run_script( const BSTR script, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1833 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
1834 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
1835 IActiveScriptParse *parser = NULL;
1836 IActiveScript *engine = NULL;
1837 IDispatch *dispatch = NULL;
1838 BOOL ret = FALSE;
1839 CLSID clsid;
1840 DISPID dispid;
1841 BSTR func = NULL, hostname = NULL, full_script = NULL;
1842 URL_COMPONENTSW uc;
1843 VARIANT args[2], result;
1844 DISPPARAMS params;
1845 HRESULT hr, init;
1847 memset( &uc, 0, sizeof(uc) );
1848 uc.dwStructSize = sizeof(uc);
1849 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return FALSE;
1850 if (!(hostname = SysAllocStringLen( NULL, uc.dwHostNameLength + 1 ))) return FALSE;
1851 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1852 hostname[uc.dwHostNameLength] = 0;
1854 init = CoInitialize( NULL );
1855 hr = CLSIDFromProgID( jscriptW, &clsid );
1856 if (hr != S_OK) goto done;
1858 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1859 &IID_IActiveScript, (void **)&engine );
1860 if (hr != S_OK) goto done;
1862 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
1863 if (hr != S_OK) goto done;
1865 hr = IActiveScriptParse64_InitNew( parser );
1866 if (hr != S_OK) goto done;
1868 hr = IActiveScript_SetScriptSite( engine, &script_site );
1869 if (hr != S_OK) goto done;
1871 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
1872 if (hr != S_OK) goto done;
1874 if (!(full_script = include_pac_utils( script ))) goto done;
1876 hr = IActiveScriptParse64_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
1877 if (hr != S_OK) goto done;
1879 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
1880 if (hr != S_OK) goto done;
1882 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
1883 if (hr != S_OK) goto done;
1885 if (!(func = SysAllocString( findproxyW ))) goto done;
1886 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
1887 if (hr != S_OK) goto done;
1889 V_VT( &args[0] ) = VT_BSTR;
1890 V_BSTR( &args[0] ) = hostname;
1891 V_VT( &args[1] ) = VT_BSTR;
1892 V_BSTR( &args[1] ) = SysAllocString( url );
1894 params.rgvarg = args;
1895 params.rgdispidNamedArgs = NULL;
1896 params.cArgs = 2;
1897 params.cNamedArgs = 0;
1898 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
1899 &params, &result, NULL, NULL );
1900 VariantClear( &args[1] );
1901 if (hr != S_OK)
1903 WARN("script failed 0x%08x\n", hr);
1904 goto done;
1906 ret = parse_script_result( result, info );
1908 done:
1909 SysFreeString( full_script );
1910 SysFreeString( hostname );
1911 SysFreeString( func );
1912 if (dispatch) IDispatch_Release( dispatch );
1913 if (parser) IUnknown_Release( parser );
1914 if (engine) IActiveScript_Release( engine );
1915 if (SUCCEEDED( init )) CoUninitialize();
1916 if (!ret) set_last_error( ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT );
1917 return ret;
1920 static BSTR download_script( const WCHAR *url )
1922 static const WCHAR typeW[] = {'*','/','*',0};
1923 static const WCHAR *acceptW[] = {typeW, NULL};
1924 HINTERNET ses, con = NULL, req = NULL;
1925 WCHAR *hostname;
1926 URL_COMPONENTSW uc;
1927 DWORD size = 4096, offset, to_read, bytes_read, flags = 0;
1928 char *tmp, *buffer = NULL;
1929 BSTR script = NULL;
1930 int len;
1932 memset( &uc, 0, sizeof(uc) );
1933 uc.dwStructSize = sizeof(uc);
1934 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1935 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1936 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1937 hostname[uc.dwHostNameLength] = 0;
1939 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1940 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1941 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1942 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1943 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1944 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1946 if (!(buffer = heap_alloc( size ))) goto done;
1947 to_read = size;
1948 offset = 0;
1949 for (;;)
1951 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1952 if (!bytes_read) break;
1953 to_read -= bytes_read;
1954 offset += bytes_read;
1955 if (!to_read)
1957 to_read = size;
1958 size *= 2;
1959 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1960 buffer = tmp;
1963 len = MultiByteToWideChar( CP_ACP, 0, buffer, offset, NULL, 0 );
1964 if (!(script = SysAllocStringLen( NULL, len ))) goto done;
1965 MultiByteToWideChar( CP_ACP, 0, buffer, offset, script, len );
1966 script[len] = 0;
1968 done:
1969 WinHttpCloseHandle( req );
1970 WinHttpCloseHandle( con );
1971 WinHttpCloseHandle( ses );
1972 heap_free( buffer );
1973 heap_free( hostname );
1974 if (!script) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1975 return script;
1978 /***********************************************************************
1979 * WinHttpGetProxyForUrl (winhttp.@)
1981 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1982 WINHTTP_PROXY_INFO *info )
1984 WCHAR *detected_pac_url = NULL;
1985 const WCHAR *pac_url;
1986 session_t *session;
1987 BSTR script;
1988 BOOL ret = FALSE;
1990 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1992 if (!(session = (session_t *)grab_object( hsession )))
1994 set_last_error( ERROR_INVALID_HANDLE );
1995 return FALSE;
1997 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1999 release_object( &session->hdr );
2000 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2001 return FALSE;
2003 if (!url || !options || !info ||
2004 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
2005 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
2006 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
2007 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
2009 release_object( &session->hdr );
2010 set_last_error( ERROR_INVALID_PARAMETER );
2011 return FALSE;
2013 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
2014 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
2016 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
2017 goto done;
2019 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
2020 else pac_url = detected_pac_url;
2022 if (!(script = download_script( pac_url ))) goto done;
2023 ret = run_script( script, url, info );
2024 SysFreeString( script );
2026 done:
2027 GlobalFree( detected_pac_url );
2028 release_object( &session->hdr );
2029 return ret;
2032 /***********************************************************************
2033 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
2035 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
2037 LONG l;
2038 HKEY key;
2039 BOOL ret = FALSE;
2040 const WCHAR *src;
2042 TRACE("%p\n", info);
2044 if (!info)
2046 set_last_error( ERROR_INVALID_PARAMETER );
2047 return FALSE;
2049 switch (info->dwAccessType)
2051 case WINHTTP_ACCESS_TYPE_NO_PROXY:
2052 break;
2053 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
2054 if (!info->lpszProxy)
2056 set_last_error( ERROR_INVALID_PARAMETER );
2057 return FALSE;
2059 /* Only ASCII characters are allowed */
2060 for (src = info->lpszProxy; *src; src++)
2061 if (*src > 0x7f)
2063 set_last_error( ERROR_INVALID_PARAMETER );
2064 return FALSE;
2066 if (info->lpszProxyBypass)
2068 for (src = info->lpszProxyBypass; *src; src++)
2069 if (*src > 0x7f)
2071 set_last_error( ERROR_INVALID_PARAMETER );
2072 return FALSE;
2075 break;
2076 default:
2077 set_last_error( ERROR_INVALID_PARAMETER );
2078 return FALSE;
2081 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
2082 KEY_WRITE, NULL, &key, NULL );
2083 if (!l)
2085 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
2086 BYTE *buf;
2088 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2090 size += strlenW( info->lpszProxy );
2091 if (info->lpszProxyBypass)
2092 size += strlenW( info->lpszProxyBypass );
2094 buf = heap_alloc( size );
2095 if (buf)
2097 struct connection_settings_header *hdr =
2098 (struct connection_settings_header *)buf;
2099 DWORD *len = (DWORD *)(hdr + 1);
2101 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2102 hdr->unknown = 0;
2103 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2105 BYTE *dst;
2107 hdr->flags = PROXY_TYPE_PROXY;
2108 *len++ = strlenW( info->lpszProxy );
2109 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2110 src++, dst++)
2111 *dst = *src;
2112 len = (DWORD *)dst;
2113 if (info->lpszProxyBypass)
2115 *len++ = strlenW( info->lpszProxyBypass );
2116 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2117 src++, dst++)
2118 *dst = *src;
2120 else
2121 *len++ = 0;
2123 else
2125 hdr->flags = PROXY_TYPE_DIRECT;
2126 *len++ = 0;
2127 *len++ = 0;
2129 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2130 if (!l)
2131 ret = TRUE;
2132 heap_free( buf );
2134 RegCloseKey( key );
2136 return ret;
2139 /***********************************************************************
2140 * WinHttpSetStatusCallback (winhttp.@)
2142 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2143 DWORD flags, DWORD_PTR reserved )
2145 object_header_t *hdr;
2146 WINHTTP_STATUS_CALLBACK ret;
2148 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2150 if (!(hdr = grab_object( handle )))
2152 set_last_error( ERROR_INVALID_HANDLE );
2153 return WINHTTP_INVALID_STATUS_CALLBACK;
2155 ret = hdr->callback;
2156 hdr->callback = callback;
2157 hdr->notify_mask = flags;
2159 release_object( hdr );
2160 return ret;
2163 /***********************************************************************
2164 * WinHttpSetTimeouts (winhttp.@)
2166 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2168 BOOL ret = TRUE;
2169 object_header_t *hdr;
2170 request_t *request;
2171 session_t *session;
2173 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2175 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2177 set_last_error( ERROR_INVALID_PARAMETER );
2178 return FALSE;
2181 if (!(hdr = grab_object( handle )))
2183 set_last_error( ERROR_INVALID_HANDLE );
2184 return FALSE;
2187 switch(hdr->type)
2189 case WINHTTP_HANDLE_TYPE_REQUEST:
2190 request = (request_t *)hdr;
2191 request->connect_timeout = connect;
2193 if (resolve < 0) resolve = 0;
2194 request->resolve_timeout = resolve;
2196 if (send < 0) send = 0;
2197 request->send_timeout = send;
2199 if (receive < 0) receive = 0;
2200 request->recv_timeout = receive;
2202 if (netconn_connected( &request->netconn ))
2204 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2205 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2208 release_object( &request->hdr );
2209 break;
2211 case WINHTTP_HANDLE_TYPE_SESSION:
2212 session = (session_t *)hdr;
2213 session->connect_timeout = connect;
2215 if (resolve < 0) resolve = 0;
2216 session->resolve_timeout = resolve;
2218 if (send < 0) send = 0;
2219 session->send_timeout = send;
2221 if (receive < 0) receive = 0;
2222 session->recv_timeout = receive;
2223 break;
2225 default:
2226 release_object( hdr );
2227 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2228 return FALSE;
2230 return ret;
2233 static const WCHAR wkday[7][4] =
2234 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2235 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2236 static const WCHAR month[12][4] =
2237 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2238 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2239 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2241 /***********************************************************************
2242 * WinHttpTimeFromSystemTime (WININET.@)
2244 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2246 static const WCHAR format[] =
2247 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2248 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2250 TRACE("%p, %p\n", time, string);
2252 if (!time || !string) return FALSE;
2254 sprintfW( string, format,
2255 wkday[time->wDayOfWeek],
2256 time->wDay,
2257 month[time->wMonth - 1],
2258 time->wYear,
2259 time->wHour,
2260 time->wMinute,
2261 time->wSecond );
2263 return TRUE;
2266 /***********************************************************************
2267 * WinHttpTimeToSystemTime (WININET.@)
2269 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2271 unsigned int i;
2272 const WCHAR *s = string;
2273 WCHAR *end;
2275 TRACE("%s, %p\n", debugstr_w(string), time);
2277 if (!string || !time) return FALSE;
2279 /* Windows does this too */
2280 GetSystemTime( time );
2282 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2283 * a SYSTEMTIME structure.
2286 while (*s && !isalphaW( *s )) s++;
2287 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2288 time->wDayOfWeek = 7;
2290 for (i = 0; i < 7; i++)
2292 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2293 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2294 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2296 time->wDayOfWeek = i;
2297 break;
2301 if (time->wDayOfWeek > 6) return TRUE;
2302 while (*s && !isdigitW( *s )) s++;
2303 time->wDay = strtolW( s, &end, 10 );
2304 s = end;
2306 while (*s && !isalphaW( *s )) s++;
2307 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2308 time->wMonth = 0;
2310 for (i = 0; i < 12; i++)
2312 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2313 toupperW( month[i][1]) == toupperW( s[1] ) &&
2314 toupperW( month[i][2]) == toupperW( s[2] ) )
2316 time->wMonth = i + 1;
2317 break;
2320 if (time->wMonth == 0) return TRUE;
2322 while (*s && !isdigitW( *s )) s++;
2323 if (*s == '\0') return TRUE;
2324 time->wYear = strtolW( s, &end, 10 );
2325 s = end;
2327 while (*s && !isdigitW( *s )) s++;
2328 if (*s == '\0') return TRUE;
2329 time->wHour = strtolW( s, &end, 10 );
2330 s = end;
2332 while (*s && !isdigitW( *s )) s++;
2333 if (*s == '\0') return TRUE;
2334 time->wMinute = strtolW( s, &end, 10 );
2335 s = end;
2337 while (*s && !isdigitW( *s )) s++;
2338 if (*s == '\0') return TRUE;
2339 time->wSecond = strtolW( s, &end, 10 );
2341 time->wMilliseconds = 0;
2342 return TRUE;