cmd.exe: Remove redundant call to SetConsoleMode.
[wine.git] / dlls / winhttp / session.c
blob9becdfe16d492bfd721bb2b977cdf43aece67518
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 #ifdef HAVE_CORESERVICES_CORESERVICES_H
27 #define GetCurrentThread MacGetCurrentThread
28 #define LoadResource MacLoadResource
29 #include <CoreServices/CoreServices.h>
30 #undef GetCurrentThread
31 #undef LoadResource
32 #undef DPRINTF
33 #endif
35 #include "windef.h"
36 #include "winbase.h"
37 #ifndef __MINGW32__
38 #define USE_WS_PREFIX
39 #endif
40 #include "winsock2.h"
41 #include "ws2ipdef.h"
42 #include "winhttp.h"
43 #include "wincrypt.h"
44 #include "winreg.h"
45 #define COBJMACROS
46 #include "ole2.h"
47 #include "dispex.h"
48 #include "activscp.h"
50 #include "winhttp_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
54 #define DEFAULT_RESOLVE_TIMEOUT 0
55 #define DEFAULT_CONNECT_TIMEOUT 20000
56 #define DEFAULT_SEND_TIMEOUT 30000
57 #define DEFAULT_RECEIVE_TIMEOUT 30000
59 void set_last_error( DWORD error )
61 /* FIXME */
62 SetLastError( error );
65 DWORD get_last_error( void )
67 /* FIXME */
68 return GetLastError();
71 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
73 if (hdr->callback && (hdr->notify_mask & status))
75 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
76 hdr->callback( hdr->handle, hdr->context, status, info, buflen );
77 TRACE("returning from 0x%08x callback\n", status);
81 /***********************************************************************
82 * WinHttpCheckPlatform (winhttp.@)
84 BOOL WINAPI WinHttpCheckPlatform( void )
86 TRACE("\n");
87 return TRUE;
90 /***********************************************************************
91 * session_destroy (internal)
93 static void session_destroy( object_header_t *hdr )
95 session_t *session = (session_t *)hdr;
96 struct list *item, *next;
97 domain_t *domain;
99 TRACE("%p\n", session);
101 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
103 domain = LIST_ENTRY( item, domain_t, entry );
104 delete_domain( domain );
106 heap_free( session->agent );
107 heap_free( session->proxy_server );
108 heap_free( session->proxy_bypass );
109 heap_free( session->proxy_username );
110 heap_free( session->proxy_password );
111 heap_free( session );
114 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
116 session_t *session = (session_t *)hdr;
118 switch (option)
120 case WINHTTP_OPTION_REDIRECT_POLICY:
122 if (!buffer || *buflen < sizeof(DWORD))
124 *buflen = sizeof(DWORD);
125 set_last_error( ERROR_INSUFFICIENT_BUFFER );
126 return FALSE;
129 *(DWORD *)buffer = hdr->redirect_policy;
130 *buflen = sizeof(DWORD);
131 return TRUE;
133 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
134 *(DWORD *)buffer = session->resolve_timeout;
135 *buflen = sizeof(DWORD);
136 return TRUE;
137 case WINHTTP_OPTION_CONNECT_TIMEOUT:
138 *(DWORD *)buffer = session->connect_timeout;
139 *buflen = sizeof(DWORD);
140 return TRUE;
141 case WINHTTP_OPTION_SEND_TIMEOUT:
142 *(DWORD *)buffer = session->send_timeout;
143 *buflen = sizeof(DWORD);
144 return TRUE;
145 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
146 *(DWORD *)buffer = session->recv_timeout;
147 *buflen = sizeof(DWORD);
148 return TRUE;
149 default:
150 FIXME("unimplemented option %u\n", option);
151 set_last_error( ERROR_INVALID_PARAMETER );
152 return FALSE;
156 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
158 session_t *session = (session_t *)hdr;
160 switch (option)
162 case WINHTTP_OPTION_PROXY:
164 WINHTTP_PROXY_INFO *pi = buffer;
166 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
167 return TRUE;
169 case WINHTTP_OPTION_REDIRECT_POLICY:
171 DWORD policy;
173 if (buflen != sizeof(policy))
175 set_last_error( ERROR_INSUFFICIENT_BUFFER );
176 return FALSE;
179 policy = *(DWORD *)buffer;
180 TRACE("0x%x\n", policy);
181 hdr->redirect_policy = policy;
182 return TRUE;
184 case WINHTTP_OPTION_DISABLE_FEATURE:
185 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
186 return FALSE;
187 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
188 session->resolve_timeout = *(DWORD *)buffer;
189 return TRUE;
190 case WINHTTP_OPTION_CONNECT_TIMEOUT:
191 session->connect_timeout = *(DWORD *)buffer;
192 return TRUE;
193 case WINHTTP_OPTION_SEND_TIMEOUT:
194 session->send_timeout = *(DWORD *)buffer;
195 return TRUE;
196 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
197 session->recv_timeout = *(DWORD *)buffer;
198 return TRUE;
199 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
200 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD *)buffer);
201 return TRUE;
202 default:
203 FIXME("unimplemented option %u\n", option);
204 set_last_error( ERROR_INVALID_PARAMETER );
205 return FALSE;
209 static const object_vtbl_t session_vtbl =
211 session_destroy,
212 session_query_option,
213 session_set_option
216 /***********************************************************************
217 * WinHttpOpen (winhttp.@)
219 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
221 session_t *session;
222 HINTERNET handle = NULL;
224 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
226 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
228 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
229 session->hdr.vtbl = &session_vtbl;
230 session->hdr.flags = flags;
231 session->hdr.refs = 1;
232 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
233 list_init( &session->hdr.children );
234 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
235 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
236 session->send_timeout = DEFAULT_SEND_TIMEOUT;
237 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
238 list_init( &session->cookie_cache );
240 if (agent && !(session->agent = strdupW( agent ))) goto end;
241 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
243 WINHTTP_PROXY_INFO info;
245 WinHttpGetDefaultProxyConfiguration( &info );
246 session->access = info.dwAccessType;
247 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
249 GlobalFree( info.lpszProxy );
250 GlobalFree( info.lpszProxyBypass );
251 goto end;
253 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
255 GlobalFree( info.lpszProxy );
256 GlobalFree( info.lpszProxyBypass );
257 goto end;
260 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
262 session->access = access;
263 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
264 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
267 if (!(handle = alloc_handle( &session->hdr ))) goto end;
268 session->hdr.handle = handle;
270 end:
271 release_object( &session->hdr );
272 TRACE("returning %p\n", handle);
273 return handle;
276 /***********************************************************************
277 * connect_destroy (internal)
279 static void connect_destroy( object_header_t *hdr )
281 connect_t *connect = (connect_t *)hdr;
283 TRACE("%p\n", connect);
285 release_object( &connect->session->hdr );
287 heap_free( connect->hostname );
288 heap_free( connect->servername );
289 heap_free( connect->username );
290 heap_free( connect->password );
291 heap_free( connect );
294 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
296 connect_t *connect = (connect_t *)hdr;
298 switch (option)
300 case WINHTTP_OPTION_PARENT_HANDLE:
302 if (!buffer || *buflen < sizeof(HINTERNET))
304 *buflen = sizeof(HINTERNET);
305 set_last_error( ERROR_INSUFFICIENT_BUFFER );
306 return FALSE;
309 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
310 *buflen = sizeof(HINTERNET);
311 return TRUE;
313 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
314 *(DWORD *)buffer = connect->session->resolve_timeout;
315 *buflen = sizeof(DWORD);
316 return TRUE;
317 case WINHTTP_OPTION_CONNECT_TIMEOUT:
318 *(DWORD *)buffer = connect->session->connect_timeout;
319 *buflen = sizeof(DWORD);
320 return TRUE;
321 case WINHTTP_OPTION_SEND_TIMEOUT:
322 *(DWORD *)buffer = connect->session->send_timeout;
323 *buflen = sizeof(DWORD);
324 return TRUE;
325 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
326 *(DWORD *)buffer = connect->session->recv_timeout;
327 *buflen = sizeof(DWORD);
328 return TRUE;
329 default:
330 FIXME("unimplemented option %u\n", option);
331 set_last_error( ERROR_INVALID_PARAMETER );
332 return FALSE;
336 static const object_vtbl_t connect_vtbl =
338 connect_destroy,
339 connect_query_option,
340 NULL
343 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
345 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
346 BOOL ret = FALSE;
348 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
349 ret = TRUE;
350 else if (*domain == '*')
352 if (domain[1] == '.')
354 LPCWSTR dot;
356 /* For a hostname to match a wildcard, the last domain must match
357 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
358 * hostname is www.foo.a.b, it matches, but a.b does not.
360 dot = strchrW( server, '.' );
361 if (dot)
363 int len = strlenW( dot + 1 );
365 if (len > strlenW( domain + 2 ))
367 LPCWSTR ptr;
369 /* The server's domain is longer than the wildcard, so it
370 * could be a subdomain. Compare the last portion of the
371 * server's domain.
373 ptr = dot + len + 1 - strlenW( domain + 2 );
374 if (!strcmpiW( ptr, domain + 2 ))
376 /* This is only a match if the preceding character is
377 * a '.', i.e. that it is a matching domain. E.g.
378 * if domain is '*.b.c' and server is 'www.ab.c' they
379 * do not match.
381 ret = *(ptr - 1) == '.';
384 else
385 ret = !strcmpiW( dot + 1, domain + 2 );
389 else
390 ret = !strcmpiW( server, domain );
391 return ret;
394 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
395 #define MAX_HOST_NAME_LENGTH 256
397 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
399 LPCWSTR ptr;
400 BOOL ret = FALSE;
402 if (!session->proxy_bypass) return FALSE;
403 ptr = session->proxy_bypass;
404 do {
405 LPCWSTR tmp = ptr;
407 ptr = strchrW( ptr, ';' );
408 if (!ptr)
409 ptr = strchrW( tmp, ' ' );
410 if (ptr)
412 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
414 WCHAR domain[MAX_HOST_NAME_LENGTH];
416 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
417 domain[ptr - tmp] = 0;
418 ret = domain_matches( server, domain );
420 ptr += 1;
422 else if (*tmp)
423 ret = domain_matches( server, tmp );
424 } while (ptr && !ret);
425 return ret;
428 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
430 session_t *session = connect->session;
431 BOOL ret = TRUE;
433 if (session->proxy_server && !should_bypass_proxy(session, server))
435 LPCWSTR colon;
437 if ((colon = strchrW( session->proxy_server, ':' )))
439 if (!connect->servername || strncmpiW( connect->servername,
440 session->proxy_server, colon - session->proxy_server - 1 ))
442 heap_free( connect->servername );
443 connect->resolved = FALSE;
444 if (!(connect->servername = heap_alloc(
445 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
447 ret = FALSE;
448 goto end;
450 memcpy( connect->servername, session->proxy_server,
451 (colon - session->proxy_server) * sizeof(WCHAR) );
452 connect->servername[colon - session->proxy_server] = 0;
453 if (*(colon + 1))
454 connect->serverport = atoiW( colon + 1 );
455 else
456 connect->serverport = INTERNET_DEFAULT_PORT;
459 else
461 if (!connect->servername || strcmpiW( connect->servername,
462 session->proxy_server ))
464 heap_free( connect->servername );
465 connect->resolved = FALSE;
466 if (!(connect->servername = strdupW( session->proxy_server )))
468 ret = FALSE;
469 goto end;
471 connect->serverport = INTERNET_DEFAULT_PORT;
475 else if (server)
477 heap_free( connect->servername );
478 connect->resolved = FALSE;
479 if (!(connect->servername = strdupW( server )))
481 ret = FALSE;
482 goto end;
484 connect->serverport = port;
486 end:
487 return ret;
490 /***********************************************************************
491 * WinHttpConnect (winhttp.@)
493 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
495 connect_t *connect;
496 session_t *session;
497 HINTERNET hconnect = NULL;
499 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
501 if (!server)
503 set_last_error( ERROR_INVALID_PARAMETER );
504 return NULL;
506 if (!(session = (session_t *)grab_object( hsession )))
508 set_last_error( ERROR_INVALID_HANDLE );
509 return NULL;
511 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
513 release_object( &session->hdr );
514 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
515 return NULL;
517 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
519 release_object( &session->hdr );
520 return NULL;
522 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
523 connect->hdr.vtbl = &connect_vtbl;
524 connect->hdr.refs = 1;
525 connect->hdr.flags = session->hdr.flags;
526 connect->hdr.callback = session->hdr.callback;
527 connect->hdr.notify_mask = session->hdr.notify_mask;
528 connect->hdr.context = session->hdr.context;
529 connect->hdr.redirect_policy = session->hdr.redirect_policy;
530 list_init( &connect->hdr.children );
532 addref_object( &session->hdr );
533 connect->session = session;
534 list_add_head( &session->hdr.children, &connect->hdr.entry );
536 if (!(connect->hostname = strdupW( server ))) goto end;
537 connect->hostport = port;
538 if (!set_server_for_hostname( connect, server, port )) goto end;
540 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
541 connect->hdr.handle = hconnect;
543 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
545 end:
546 release_object( &connect->hdr );
547 release_object( &session->hdr );
548 TRACE("returning %p\n", hconnect);
549 return hconnect;
552 /***********************************************************************
553 * request_destroy (internal)
555 static void request_destroy( object_header_t *hdr )
557 request_t *request = (request_t *)hdr;
558 unsigned int i;
560 TRACE("%p\n", request);
562 if (request->task_thread)
564 SetEvent( request->task_cancel );
565 WaitForSingleObject( request->task_thread, INFINITE );
566 CloseHandle( request->task_thread );
567 CloseHandle( request->task_cancel );
568 CloseHandle( request->task_wait );
569 request->task_cs.DebugInfo->Spare[0] = 0;
570 DeleteCriticalSection( &request->task_cs );
572 release_object( &request->connect->hdr );
574 destroy_authinfo( request->authinfo );
575 destroy_authinfo( request->proxy_authinfo );
577 heap_free( request->verb );
578 heap_free( request->path );
579 heap_free( request->version );
580 heap_free( request->raw_headers );
581 heap_free( request->status_text );
582 for (i = 0; i < request->num_headers; i++)
584 heap_free( request->headers[i].field );
585 heap_free( request->headers[i].value );
587 heap_free( request->headers );
588 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
589 heap_free( request->accept_types );
590 heap_free( request );
593 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
595 int len = 0;
596 if (str) len = strlenW( str );
597 if (buffer && *buflen > len)
599 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
600 buffer[len] = 0;
602 *buflen = len * sizeof(WCHAR);
605 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
607 WCHAR *ret;
608 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
610 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
611 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
612 CertNameToStrW( encoding, blob, format, ret, size );
614 return ret;
617 static BOOL convert_sockaddr( const struct sockaddr *addr, SOCKADDR_STORAGE *addr_storage )
619 #ifndef __MINGW32__
620 switch (addr->sa_family)
622 case AF_INET:
624 const struct sockaddr_in *addr_unix = (const struct sockaddr_in *)addr;
625 struct WS_sockaddr_in *addr_win = (struct WS_sockaddr_in *)addr_storage;
626 char *p;
628 addr_win->sin_family = WS_AF_INET;
629 addr_win->sin_port = addr_unix->sin_port;
630 memcpy( &addr_win->sin_addr, &addr_unix->sin_addr, 4 );
631 p = (char *)&addr_win->sin_addr + 4;
632 memset( p, 0, sizeof(*addr_storage) - (p - (char *)addr_win) );
633 return TRUE;
635 case AF_INET6:
637 const struct sockaddr_in6 *addr_unix = (const struct sockaddr_in6 *)addr;
638 struct WS_sockaddr_in6 *addr_win = (struct WS_sockaddr_in6 *)addr_storage;
640 addr_win->sin6_family = WS_AF_INET6;
641 addr_win->sin6_port = addr_unix->sin6_port;
642 addr_win->sin6_flowinfo = addr_unix->sin6_flowinfo;
643 memcpy( &addr_win->sin6_addr, &addr_unix->sin6_addr, 16 );
644 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
645 addr_win->sin6_scope_id = addr_unix->sin6_scope_id;
646 #else
647 addr_win->sin6_scope_id = 0;
648 #endif
649 memset( addr_win + 1, 0, sizeof(*addr_storage) - sizeof(*addr_win) );
650 return TRUE;
652 default:
653 ERR("unhandled family %u\n", addr->sa_family);
654 return FALSE;
656 #else
657 switch (addr->sa_family)
659 case AF_INET:
661 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_storage;
663 memcpy( addr_in, addr, sizeof(*addr_in) );
664 memset( addr_in + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in) );
665 return TRUE;
667 case AF_INET6:
669 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr_storage;
671 memcpy( addr_in6, addr, sizeof(*addr_in6) );
672 memset( addr_in6 + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in6) );
673 return TRUE;
675 default:
676 ERR("unhandled family %u\n", addr->sa_family);
677 return FALSE;
679 #endif
682 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
684 request_t *request = (request_t *)hdr;
686 switch (option)
688 case WINHTTP_OPTION_SECURITY_FLAGS:
690 DWORD flags;
691 int bits;
693 if (!buffer || *buflen < sizeof(flags))
695 *buflen = sizeof(flags);
696 set_last_error( ERROR_INSUFFICIENT_BUFFER );
697 return FALSE;
700 flags = 0;
701 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
702 flags |= request->netconn.security_flags;
703 bits = netconn_get_cipher_strength( &request->netconn );
704 if (bits >= 128)
705 flags |= SECURITY_FLAG_STRENGTH_STRONG;
706 else if (bits >= 56)
707 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
708 else
709 flags |= SECURITY_FLAG_STRENGTH_WEAK;
710 *(DWORD *)buffer = flags;
711 *buflen = sizeof(flags);
712 return TRUE;
714 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
716 const CERT_CONTEXT *cert;
718 if (!buffer || *buflen < sizeof(cert))
720 *buflen = sizeof(cert);
721 set_last_error( ERROR_INSUFFICIENT_BUFFER );
722 return FALSE;
725 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
726 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
727 *buflen = sizeof(cert);
728 return TRUE;
730 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
732 const CERT_CONTEXT *cert;
733 const CRYPT_OID_INFO *oidInfo;
734 WINHTTP_CERTIFICATE_INFO *ci = buffer;
736 FIXME("partial stub\n");
738 if (!buffer || *buflen < sizeof(*ci))
740 *buflen = sizeof(*ci);
741 set_last_error( ERROR_INSUFFICIENT_BUFFER );
742 return FALSE;
744 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
746 ci->ftExpiry = cert->pCertInfo->NotAfter;
747 ci->ftStart = cert->pCertInfo->NotBefore;
748 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
749 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
750 ci->lpszProtocolName = NULL;
751 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
752 cert->pCertInfo->SignatureAlgorithm.pszObjId,
753 0 );
754 if (oidInfo)
755 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
756 else
757 ci->lpszSignatureAlgName = NULL;
758 ci->lpszEncryptionAlgName = NULL;
759 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
761 CertFreeCertificateContext( cert );
762 *buflen = sizeof(*ci);
763 return TRUE;
765 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
767 if (!buffer || *buflen < sizeof(DWORD))
769 *buflen = sizeof(DWORD);
770 set_last_error( ERROR_INSUFFICIENT_BUFFER );
771 return FALSE;
774 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
775 *buflen = sizeof(DWORD);
776 return TRUE;
778 case WINHTTP_OPTION_CONNECTION_INFO:
780 WINHTTP_CONNECTION_INFO *info = buffer;
781 struct sockaddr local;
782 socklen_t len = sizeof(local);
783 const struct sockaddr *remote = (const struct sockaddr *)&request->connect->sockaddr;
785 if (!buffer || *buflen < sizeof(*info))
787 *buflen = sizeof(*info);
788 set_last_error( ERROR_INSUFFICIENT_BUFFER );
789 return FALSE;
791 if (!netconn_connected( &request->netconn ))
793 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
794 return FALSE;
796 if (getsockname( request->netconn.socket, &local, &len )) return FALSE;
797 if (!convert_sockaddr( &local, &info->LocalAddress )) return FALSE;
798 if (!convert_sockaddr( remote, &info->RemoteAddress )) return FALSE;
799 info->cbSize = sizeof(*info);
800 return TRUE;
802 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
803 *(DWORD *)buffer = request->resolve_timeout;
804 *buflen = sizeof(DWORD);
805 return TRUE;
806 case WINHTTP_OPTION_CONNECT_TIMEOUT:
807 *(DWORD *)buffer = request->connect_timeout;
808 *buflen = sizeof(DWORD);
809 return TRUE;
810 case WINHTTP_OPTION_SEND_TIMEOUT:
811 *(DWORD *)buffer = request->send_timeout;
812 *buflen = sizeof(DWORD);
813 return TRUE;
814 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
815 *(DWORD *)buffer = request->recv_timeout;
816 *buflen = sizeof(DWORD);
817 return TRUE;
819 case WINHTTP_OPTION_USERNAME:
820 str_to_buffer( buffer, request->connect->username, buflen );
821 return TRUE;
823 case WINHTTP_OPTION_PASSWORD:
824 str_to_buffer( buffer, request->connect->password, buflen );
825 return TRUE;
827 case WINHTTP_OPTION_PROXY_USERNAME:
828 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
829 return TRUE;
831 case WINHTTP_OPTION_PROXY_PASSWORD:
832 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
833 return TRUE;
835 default:
836 FIXME("unimplemented option %u\n", option);
837 set_last_error( ERROR_INVALID_PARAMETER );
838 return FALSE;
842 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
844 WCHAR *ret;
845 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
847 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
848 ret[buflen] = 0;
849 return ret;
851 set_last_error( ERROR_OUTOFMEMORY );
852 return NULL;
855 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
857 request_t *request = (request_t *)hdr;
859 switch (option)
861 case WINHTTP_OPTION_PROXY:
863 WINHTTP_PROXY_INFO *pi = buffer;
865 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
866 return TRUE;
868 case WINHTTP_OPTION_DISABLE_FEATURE:
870 DWORD disable;
872 if (buflen != sizeof(DWORD))
874 set_last_error( ERROR_INSUFFICIENT_BUFFER );
875 return FALSE;
878 disable = *(DWORD *)buffer;
879 TRACE("0x%x\n", disable);
880 hdr->disable_flags |= disable;
881 return TRUE;
883 case WINHTTP_OPTION_AUTOLOGON_POLICY:
885 DWORD policy;
887 if (buflen != sizeof(DWORD))
889 set_last_error( ERROR_INSUFFICIENT_BUFFER );
890 return FALSE;
893 policy = *(DWORD *)buffer;
894 TRACE("0x%x\n", policy);
895 hdr->logon_policy = policy;
896 return TRUE;
898 case WINHTTP_OPTION_REDIRECT_POLICY:
900 DWORD policy;
902 if (buflen != sizeof(DWORD))
904 set_last_error( ERROR_INSUFFICIENT_BUFFER );
905 return FALSE;
908 policy = *(DWORD *)buffer;
909 TRACE("0x%x\n", policy);
910 hdr->redirect_policy = policy;
911 return TRUE;
913 case WINHTTP_OPTION_SECURITY_FLAGS:
915 DWORD flags;
917 if (buflen < sizeof(DWORD))
919 set_last_error( ERROR_INSUFFICIENT_BUFFER );
920 return FALSE;
922 flags = *(DWORD *)buffer;
923 TRACE("0x%x\n", flags);
924 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
925 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
926 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
927 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
929 set_last_error( ERROR_INVALID_PARAMETER );
930 return FALSE;
932 request->netconn.security_flags = flags;
933 return TRUE;
935 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
936 request->resolve_timeout = *(DWORD *)buffer;
937 return TRUE;
938 case WINHTTP_OPTION_CONNECT_TIMEOUT:
939 request->connect_timeout = *(DWORD *)buffer;
940 return TRUE;
941 case WINHTTP_OPTION_SEND_TIMEOUT:
942 request->send_timeout = *(DWORD *)buffer;
943 return TRUE;
944 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
945 request->recv_timeout = *(DWORD *)buffer;
946 return TRUE;
948 case WINHTTP_OPTION_USERNAME:
950 connect_t *connect = request->connect;
952 heap_free( connect->username );
953 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
954 return TRUE;
956 case WINHTTP_OPTION_PASSWORD:
958 connect_t *connect = request->connect;
960 heap_free( connect->password );
961 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
962 return TRUE;
964 case WINHTTP_OPTION_PROXY_USERNAME:
966 session_t *session = request->connect->session;
968 heap_free( session->proxy_username );
969 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
970 return TRUE;
972 case WINHTTP_OPTION_PROXY_PASSWORD:
974 session_t *session = request->connect->session;
976 heap_free( session->proxy_password );
977 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
978 return TRUE;
980 default:
981 FIXME("unimplemented option %u\n", option);
982 set_last_error( ERROR_INVALID_PARAMETER );
983 return TRUE;
987 static const object_vtbl_t request_vtbl =
989 request_destroy,
990 request_query_option,
991 request_set_option
994 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
996 const WCHAR **types = accept_types;
997 DWORD i;
999 if (!types) return TRUE;
1000 while (*types)
1002 request->num_accept_types++;
1003 types++;
1005 if (!request->num_accept_types) return TRUE;
1006 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
1008 request->num_accept_types = 0;
1009 return FALSE;
1011 types = accept_types;
1012 for (i = 0; i < request->num_accept_types; i++)
1014 if (!(request->accept_types[i] = strdupW( *types )))
1016 for ( ; i > 0; --i) heap_free( request->accept_types[i - 1] );
1017 heap_free( request->accept_types );
1018 request->accept_types = NULL;
1019 request->num_accept_types = 0;
1020 return FALSE;
1022 types++;
1024 return TRUE;
1027 /***********************************************************************
1028 * WinHttpOpenRequest (winhttp.@)
1030 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
1031 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
1033 request_t *request;
1034 connect_t *connect;
1035 HINTERNET hrequest = NULL;
1037 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
1038 debugstr_w(version), debugstr_w(referrer), types, flags);
1040 if(types && TRACE_ON(winhttp)) {
1041 const WCHAR **iter;
1043 TRACE("accept types:\n");
1044 for(iter = types; *iter; iter++)
1045 TRACE(" %s\n", debugstr_w(*iter));
1048 if (!(connect = (connect_t *)grab_object( hconnect )))
1050 set_last_error( ERROR_INVALID_HANDLE );
1051 return NULL;
1053 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
1055 release_object( &connect->hdr );
1056 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1057 return NULL;
1059 if (!(request = heap_alloc_zero( sizeof(request_t) )))
1061 release_object( &connect->hdr );
1062 return NULL;
1064 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
1065 request->hdr.vtbl = &request_vtbl;
1066 request->hdr.refs = 1;
1067 request->hdr.flags = flags;
1068 request->hdr.callback = connect->hdr.callback;
1069 request->hdr.notify_mask = connect->hdr.notify_mask;
1070 request->hdr.context = connect->hdr.context;
1071 request->hdr.redirect_policy = connect->hdr.redirect_policy;
1072 list_init( &request->hdr.children );
1073 list_init( &request->task_queue );
1075 addref_object( &connect->hdr );
1076 request->connect = connect;
1077 list_add_head( &connect->hdr.children, &request->hdr.entry );
1079 if (!netconn_init( &request->netconn )) goto end;
1080 request->resolve_timeout = connect->session->resolve_timeout;
1081 request->connect_timeout = connect->session->connect_timeout;
1082 request->send_timeout = connect->session->send_timeout;
1083 request->recv_timeout = connect->session->recv_timeout;
1085 if (!verb || !verb[0]) verb = getW;
1086 if (!(request->verb = strdupW( verb ))) goto end;
1088 if (object)
1090 WCHAR *path, *p;
1091 unsigned int len;
1093 len = strlenW( object ) + 1;
1094 if (object[0] != '/') len++;
1095 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
1097 if (object[0] != '/') *p++ = '/';
1098 strcpyW( p, object );
1099 request->path = path;
1101 else if (!(request->path = strdupW( slashW ))) goto end;
1103 if (!version || !version[0]) version = http1_1;
1104 if (!(request->version = strdupW( version ))) goto end;
1105 if (!(store_accept_types( request, types ))) goto end;
1107 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
1108 request->hdr.handle = hrequest;
1110 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
1112 end:
1113 release_object( &request->hdr );
1114 release_object( &connect->hdr );
1115 TRACE("returning %p\n", hrequest);
1116 return hrequest;
1119 /***********************************************************************
1120 * WinHttpCloseHandle (winhttp.@)
1122 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1124 object_header_t *hdr;
1126 TRACE("%p\n", handle);
1128 if (!(hdr = grab_object( handle )))
1130 set_last_error( ERROR_INVALID_HANDLE );
1131 return FALSE;
1133 release_object( hdr );
1134 free_handle( handle );
1135 return TRUE;
1138 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1140 BOOL ret = FALSE;
1142 if (!buflen)
1144 set_last_error( ERROR_INVALID_PARAMETER );
1145 return FALSE;
1148 switch (option)
1150 case WINHTTP_OPTION_CONTEXT_VALUE:
1152 if (!buffer || *buflen < sizeof(DWORD_PTR))
1154 *buflen = sizeof(DWORD_PTR);
1155 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1156 return FALSE;
1159 *(DWORD_PTR *)buffer = hdr->context;
1160 *buflen = sizeof(DWORD_PTR);
1161 return TRUE;
1163 default:
1164 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1165 else
1167 FIXME("unimplemented option %u\n", option);
1168 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1169 return FALSE;
1171 break;
1173 return ret;
1176 /***********************************************************************
1177 * WinHttpQueryOption (winhttp.@)
1179 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1181 BOOL ret = FALSE;
1182 object_header_t *hdr;
1184 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1186 if (!(hdr = grab_object( handle )))
1188 set_last_error( ERROR_INVALID_HANDLE );
1189 return FALSE;
1192 ret = query_option( hdr, option, buffer, buflen );
1194 release_object( hdr );
1195 return ret;
1198 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1200 BOOL ret = TRUE;
1202 if (!buffer)
1204 set_last_error( ERROR_INVALID_PARAMETER );
1205 return FALSE;
1208 switch (option)
1210 case WINHTTP_OPTION_CONTEXT_VALUE:
1212 if (buflen != sizeof(DWORD_PTR))
1214 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1215 return FALSE;
1218 hdr->context = *(DWORD_PTR *)buffer;
1219 return TRUE;
1221 default:
1222 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1223 else
1225 FIXME("unimplemented option %u\n", option);
1226 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1227 return FALSE;
1229 break;
1231 return ret;
1234 /***********************************************************************
1235 * WinHttpSetOption (winhttp.@)
1237 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1239 BOOL ret = FALSE;
1240 object_header_t *hdr;
1242 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1244 if (!(hdr = grab_object( handle )))
1246 set_last_error( ERROR_INVALID_HANDLE );
1247 return FALSE;
1250 ret = set_option( hdr, option, buffer, buflen );
1252 release_object( hdr );
1253 return ret;
1256 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1258 char *ret;
1259 DWORD size = 0;
1261 GetComputerNameExA( format, NULL, &size );
1262 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1263 if (!(ret = heap_alloc( size ))) return NULL;
1264 if (!GetComputerNameExA( format, ret, &size ))
1266 heap_free( ret );
1267 return NULL;
1269 return ret;
1272 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1274 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1276 if (len_suffix > len_domain) return FALSE;
1277 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1278 return FALSE;
1281 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1283 int ret = -1;
1284 #ifdef HAVE_GETNAMEINFO
1285 ret = getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1286 #endif
1287 return ret;
1290 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1292 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1293 static const WCHAR wpadW[] = {'/','w','p','a','d','.','d','a','t',0};
1294 char name[NI_MAXHOST];
1295 WCHAR *ret, *p;
1296 int len;
1298 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1299 if (!ai) return NULL;
1301 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1303 len = strlenW( httpW ) + strlen( hostname ) + strlenW( wpadW );
1304 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1305 strcpyW( p, httpW );
1306 p += strlenW( httpW );
1307 while (*hostname) { *p++ = *hostname++; }
1308 strcpyW( p, wpadW );
1309 return ret;
1312 static BOOL get_system_proxy_autoconfig_url( char *buf, DWORD buflen )
1314 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1315 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
1316 const void *ref;
1317 BOOL ret = FALSE;
1319 if (!settings) return FALSE;
1321 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
1323 CFRelease( settings );
1324 return FALSE;
1326 if (CFStringGetCString( ref, buf, buflen, kCFStringEncodingASCII ))
1328 TRACE( "returning %s\n", debugstr_a(buf) );
1329 ret = TRUE;
1331 CFRelease( settings );
1332 return ret;
1333 #else
1334 FIXME( "no support on this platform\n" );
1335 return FALSE;
1336 #endif
1339 #define INTERNET_MAX_URL_LENGTH 2084
1341 /***********************************************************************
1342 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1344 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1346 BOOL ret = FALSE;
1347 char system_url[INTERNET_MAX_URL_LENGTH + 1];
1349 TRACE("0x%08x, %p\n", flags, url);
1351 if (!flags || !url)
1353 set_last_error( ERROR_INVALID_PARAMETER );
1354 return FALSE;
1356 if (get_system_proxy_autoconfig_url( system_url, sizeof(system_url) ))
1358 WCHAR *urlW;
1360 if (!(urlW = strdupAW( system_url ))) return FALSE;
1361 *url = urlW;
1362 return TRUE;
1364 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1366 static int fixme_shown;
1367 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1369 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1371 #ifdef HAVE_GETADDRINFO
1372 char *fqdn, *domain, *p;
1374 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1375 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1377 heap_free( fqdn );
1378 return FALSE;
1380 p = fqdn;
1381 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1383 struct addrinfo *ai;
1384 char *name;
1385 int res;
1387 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1389 heap_free( fqdn );
1390 heap_free( domain );
1391 return FALSE;
1393 strcpy( name, "wpad" );
1394 strcat( name, p );
1395 res = getaddrinfo( name, NULL, NULL, &ai );
1396 if (!res)
1398 *url = build_wpad_url( name, ai );
1399 freeaddrinfo( ai );
1400 if (*url)
1402 TRACE("returning %s\n", debugstr_w(*url));
1403 heap_free( name );
1404 ret = TRUE;
1405 break;
1408 heap_free( name );
1409 p++;
1411 heap_free( domain );
1412 heap_free( fqdn );
1413 #else
1414 FIXME("getaddrinfo not found at build time\n");
1415 #endif
1417 if (!ret)
1419 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1420 *url = NULL;
1422 return ret;
1425 static const WCHAR Connections[] = {
1426 'S','o','f','t','w','a','r','e','\\',
1427 'M','i','c','r','o','s','o','f','t','\\',
1428 'W','i','n','d','o','w','s','\\',
1429 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1430 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1431 'C','o','n','n','e','c','t','i','o','n','s',0 };
1432 static const WCHAR WinHttpSettings[] = {
1433 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1434 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1435 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1436 static const DWORD PROXY_TYPE_DIRECT = 1;
1437 static const DWORD PROXY_TYPE_PROXY = 2;
1438 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1439 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1441 struct connection_settings_header
1443 DWORD magic;
1444 DWORD unknown; /* always zero? */
1445 DWORD flags; /* one or more of PROXY_* */
1448 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1450 const BYTE *begin;
1452 for (begin = src; src - begin < len; src++, dst++)
1453 *dst = *src;
1454 *dst = 0;
1457 /***********************************************************************
1458 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1460 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1462 LONG l;
1463 HKEY key;
1464 BOOL got_from_reg = FALSE, direct = TRUE;
1465 char *envproxy;
1467 TRACE("%p\n", info);
1469 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1470 if (!l)
1472 DWORD type, size = 0;
1474 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1475 if (!l && type == REG_BINARY &&
1476 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1478 BYTE *buf = heap_alloc( size );
1480 if (buf)
1482 struct connection_settings_header *hdr =
1483 (struct connection_settings_header *)buf;
1484 DWORD *len = (DWORD *)(hdr + 1);
1486 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1487 &size );
1488 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1489 hdr->unknown == 0)
1491 if (hdr->flags & PROXY_TYPE_PROXY)
1493 BOOL sane = FALSE;
1494 LPWSTR proxy = NULL;
1495 LPWSTR proxy_bypass = NULL;
1497 /* Sanity-check length of proxy string */
1498 if ((BYTE *)len - buf + *len <= size)
1500 sane = TRUE;
1501 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1502 if (proxy)
1503 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1504 len = (DWORD *)((BYTE *)(len + 1) + *len);
1506 if (sane)
1508 /* Sanity-check length of proxy bypass string */
1509 if ((BYTE *)len - buf + *len <= size)
1511 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1512 if (proxy_bypass)
1513 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1515 else
1517 sane = FALSE;
1518 GlobalFree( proxy );
1519 proxy = NULL;
1522 info->lpszProxy = proxy;
1523 info->lpszProxyBypass = proxy_bypass;
1524 if (sane)
1526 got_from_reg = TRUE;
1527 direct = FALSE;
1528 info->dwAccessType =
1529 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1530 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1531 debugstr_w(info->lpszProxy),
1532 debugstr_w(info->lpszProxyBypass));
1536 heap_free( buf );
1539 RegCloseKey( key );
1541 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1543 char *colon, *http_proxy;
1545 if ((colon = strchr( envproxy, ':' )))
1547 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1549 static const char http[] = "http://";
1551 /* It's a scheme, check that it's http */
1552 if (!strncmp( envproxy, http, strlen( http ) ))
1553 http_proxy = envproxy + strlen( http );
1554 else
1556 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1557 http_proxy = NULL;
1560 else
1561 http_proxy = envproxy;
1563 else
1564 http_proxy = envproxy;
1565 if (http_proxy)
1567 WCHAR *http_proxyW;
1568 int len;
1570 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1571 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1573 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1574 direct = FALSE;
1575 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1576 info->lpszProxy = http_proxyW;
1577 info->lpszProxyBypass = NULL;
1578 TRACE("http proxy (from environment) = %s\n",
1579 debugstr_w(info->lpszProxy));
1583 if (direct)
1585 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1586 info->lpszProxy = NULL;
1587 info->lpszProxyBypass = NULL;
1589 return TRUE;
1592 /***********************************************************************
1593 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1595 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1597 static const WCHAR settingsW[] =
1598 {'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};
1599 HKEY hkey = NULL;
1600 struct connection_settings_header *hdr = NULL;
1601 DWORD type, offset, len, size = 0;
1602 BOOL ret = FALSE;
1604 TRACE("%p\n", config);
1606 if (!config)
1608 set_last_error( ERROR_INVALID_PARAMETER );
1609 return FALSE;
1611 memset( config, 0, sizeof(*config) );
1612 config->fAutoDetect = TRUE;
1614 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1615 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1616 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1618 ret = TRUE;
1619 goto done;
1621 if (!(hdr = heap_alloc( size ))) goto done;
1622 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1623 hdr->magic != WININET_SETTINGS_MAGIC)
1625 ret = TRUE;
1626 goto done;
1629 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1630 offset = sizeof(*hdr);
1631 if (offset + sizeof(DWORD) > size) goto done;
1632 len = *(DWORD *)((char *)hdr + offset);
1633 offset += sizeof(DWORD);
1634 if (len && hdr->flags & PROXY_TYPE_PROXY)
1636 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1637 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1639 offset += len;
1640 if (offset + sizeof(DWORD) > size) goto done;
1641 len = *(DWORD *)((char *)hdr + offset);
1642 offset += sizeof(DWORD);
1643 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1645 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1646 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1648 offset += len;
1649 if (offset + sizeof(DWORD) > size) goto done;
1650 len = *(DWORD *)((char *)hdr + offset);
1651 offset += sizeof(DWORD);
1652 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1654 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1655 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1657 ret = TRUE;
1659 done:
1660 RegCloseKey( hkey );
1661 heap_free( hdr );
1662 if (!ret)
1664 GlobalFree( config->lpszAutoConfigUrl );
1665 config->lpszAutoConfigUrl = NULL;
1666 GlobalFree( config->lpszProxy );
1667 config->lpszProxy = NULL;
1668 GlobalFree( config->lpszProxyBypass );
1669 config->lpszProxyBypass = NULL;
1671 return ret;
1674 static BOOL parse_script_result( const char *result, WINHTTP_PROXY_INFO *info )
1676 const char *p;
1677 WCHAR *q;
1678 int len;
1680 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1681 info->lpszProxy = NULL;
1682 info->lpszProxyBypass = NULL;
1684 TRACE("%s\n", debugstr_a( result ));
1686 p = result;
1687 while (*p == ' ') p++;
1688 len = strlen( p );
1689 if (len >= 5 && !strncasecmp( p, "PROXY", sizeof("PROXY") - 1 ))
1691 p += 5;
1692 while (*p == ' ') p++;
1693 if (!*p || *p == ';') return TRUE;
1694 if (!(info->lpszProxy = q = strdupAW( p ))) return FALSE;
1695 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1696 for (; *q; q++)
1698 if (*q == ' ' || *q == ';')
1700 *q = 0;
1701 break;
1705 return TRUE;
1708 static char *download_script( const WCHAR *url, DWORD *out_size )
1710 static const WCHAR typeW[] = {'*','/','*',0};
1711 static const WCHAR *acceptW[] = {typeW, NULL};
1712 HINTERNET ses, con = NULL, req = NULL;
1713 WCHAR *hostname;
1714 URL_COMPONENTSW uc;
1715 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1716 char *tmp, *buffer = NULL;
1718 *out_size = 0;
1720 memset( &uc, 0, sizeof(uc) );
1721 uc.dwStructSize = sizeof(uc);
1722 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1723 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1724 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1725 hostname[uc.dwHostNameLength] = 0;
1727 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1728 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1729 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1730 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1731 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1733 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1734 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1735 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1737 size = 4096;
1738 if (!(buffer = heap_alloc( size ))) goto done;
1739 to_read = size;
1740 offset = 0;
1741 for (;;)
1743 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1744 if (!bytes_read) break;
1745 to_read -= bytes_read;
1746 offset += bytes_read;
1747 *out_size += bytes_read;
1748 if (!to_read)
1750 to_read = size;
1751 size *= 2;
1752 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1753 buffer = tmp;
1757 done:
1758 WinHttpCloseHandle( req );
1759 WinHttpCloseHandle( con );
1760 WinHttpCloseHandle( ses );
1761 heap_free( hostname );
1762 if (!buffer) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1763 return buffer;
1766 struct AUTO_PROXY_SCRIPT_BUFFER
1768 DWORD dwStructSize;
1769 LPSTR lpszScriptBuffer;
1770 DWORD dwScriptBufferSize;
1773 BOOL WINAPI InternetDeInitializeAutoProxyDll(LPSTR, DWORD);
1774 BOOL WINAPI InternetGetProxyInfo(LPCSTR, DWORD, LPSTR, DWORD, LPSTR *, LPDWORD);
1775 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD, LPSTR, LPSTR, void *, struct AUTO_PROXY_SCRIPT_BUFFER *);
1777 static BOOL run_script( char *script, DWORD size, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1779 BOOL ret;
1780 char *result, *urlA;
1781 DWORD len_result;
1782 struct AUTO_PROXY_SCRIPT_BUFFER buffer;
1784 buffer.dwStructSize = sizeof(buffer);
1785 buffer.lpszScriptBuffer = script;
1786 buffer.dwScriptBufferSize = size;
1788 if (!(urlA = strdupWA( url ))) return FALSE;
1789 if (!(ret = InternetInitializeAutoProxyDll( 0, NULL, NULL, NULL, &buffer )))
1791 heap_free( urlA );
1792 return FALSE;
1794 if ((ret = InternetGetProxyInfo( urlA, strlen(urlA), NULL, 0, &result, &len_result )))
1796 ret = parse_script_result( result, info );
1797 heap_free( result );
1799 heap_free( urlA );
1800 return InternetDeInitializeAutoProxyDll( NULL, 0 );
1803 /***********************************************************************
1804 * WinHttpGetProxyForUrl (winhttp.@)
1806 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1807 WINHTTP_PROXY_INFO *info )
1809 WCHAR *detected_pac_url = NULL;
1810 const WCHAR *pac_url;
1811 session_t *session;
1812 char *script;
1813 DWORD size;
1814 BOOL ret = FALSE;
1816 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1818 if (!(session = (session_t *)grab_object( hsession )))
1820 set_last_error( ERROR_INVALID_HANDLE );
1821 return FALSE;
1823 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1825 release_object( &session->hdr );
1826 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1827 return FALSE;
1829 if (!url || !options || !info ||
1830 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1831 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1832 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1833 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1835 release_object( &session->hdr );
1836 set_last_error( ERROR_INVALID_PARAMETER );
1837 return FALSE;
1839 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1840 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1841 goto done;
1843 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1844 else pac_url = detected_pac_url;
1846 if ((script = download_script( pac_url, &size )))
1848 ret = run_script( script, size, url, info );
1849 heap_free( script );
1852 done:
1853 GlobalFree( detected_pac_url );
1854 release_object( &session->hdr );
1855 return ret;
1858 /***********************************************************************
1859 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1861 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1863 LONG l;
1864 HKEY key;
1865 BOOL ret = FALSE;
1866 const WCHAR *src;
1868 TRACE("%p\n", info);
1870 if (!info)
1872 set_last_error( ERROR_INVALID_PARAMETER );
1873 return FALSE;
1875 switch (info->dwAccessType)
1877 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1878 break;
1879 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1880 if (!info->lpszProxy)
1882 set_last_error( ERROR_INVALID_PARAMETER );
1883 return FALSE;
1885 /* Only ASCII characters are allowed */
1886 for (src = info->lpszProxy; *src; src++)
1887 if (*src > 0x7f)
1889 set_last_error( ERROR_INVALID_PARAMETER );
1890 return FALSE;
1892 if (info->lpszProxyBypass)
1894 for (src = info->lpszProxyBypass; *src; src++)
1895 if (*src > 0x7f)
1897 set_last_error( ERROR_INVALID_PARAMETER );
1898 return FALSE;
1901 break;
1902 default:
1903 set_last_error( ERROR_INVALID_PARAMETER );
1904 return FALSE;
1907 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1908 KEY_WRITE, NULL, &key, NULL );
1909 if (!l)
1911 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1912 BYTE *buf;
1914 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1916 size += strlenW( info->lpszProxy );
1917 if (info->lpszProxyBypass)
1918 size += strlenW( info->lpszProxyBypass );
1920 buf = heap_alloc( size );
1921 if (buf)
1923 struct connection_settings_header *hdr =
1924 (struct connection_settings_header *)buf;
1925 DWORD *len = (DWORD *)(hdr + 1);
1927 hdr->magic = WINHTTP_SETTINGS_MAGIC;
1928 hdr->unknown = 0;
1929 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1931 BYTE *dst;
1933 hdr->flags = PROXY_TYPE_PROXY;
1934 *len++ = strlenW( info->lpszProxy );
1935 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1936 src++, dst++)
1937 *dst = *src;
1938 len = (DWORD *)dst;
1939 if (info->lpszProxyBypass)
1941 *len++ = strlenW( info->lpszProxyBypass );
1942 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1943 src++, dst++)
1944 *dst = *src;
1946 else
1947 *len++ = 0;
1949 else
1951 hdr->flags = PROXY_TYPE_DIRECT;
1952 *len++ = 0;
1953 *len++ = 0;
1955 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1956 if (!l)
1957 ret = TRUE;
1958 heap_free( buf );
1960 RegCloseKey( key );
1962 return ret;
1965 /***********************************************************************
1966 * WinHttpSetStatusCallback (winhttp.@)
1968 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1969 DWORD flags, DWORD_PTR reserved )
1971 object_header_t *hdr;
1972 WINHTTP_STATUS_CALLBACK ret;
1974 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1976 if (!(hdr = grab_object( handle )))
1978 set_last_error( ERROR_INVALID_HANDLE );
1979 return WINHTTP_INVALID_STATUS_CALLBACK;
1981 ret = hdr->callback;
1982 hdr->callback = callback;
1983 hdr->notify_mask = flags;
1985 release_object( hdr );
1986 return ret;
1989 /***********************************************************************
1990 * WinHttpSetTimeouts (winhttp.@)
1992 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1994 BOOL ret = TRUE;
1995 object_header_t *hdr;
1996 request_t *request;
1997 session_t *session;
1999 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2001 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2003 set_last_error( ERROR_INVALID_PARAMETER );
2004 return FALSE;
2007 if (!(hdr = grab_object( handle )))
2009 set_last_error( ERROR_INVALID_HANDLE );
2010 return FALSE;
2013 switch(hdr->type)
2015 case WINHTTP_HANDLE_TYPE_REQUEST:
2016 request = (request_t *)hdr;
2017 request->connect_timeout = connect;
2019 if (resolve < 0) resolve = 0;
2020 request->resolve_timeout = resolve;
2022 if (send < 0) send = 0;
2023 request->send_timeout = send;
2025 if (receive < 0) receive = 0;
2026 request->recv_timeout = receive;
2028 if (netconn_connected( &request->netconn ))
2030 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
2031 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
2034 release_object( &request->hdr );
2035 break;
2037 case WINHTTP_HANDLE_TYPE_SESSION:
2038 session = (session_t *)hdr;
2039 session->connect_timeout = connect;
2041 if (resolve < 0) resolve = 0;
2042 session->resolve_timeout = resolve;
2044 if (send < 0) send = 0;
2045 session->send_timeout = send;
2047 if (receive < 0) receive = 0;
2048 session->recv_timeout = receive;
2049 break;
2051 default:
2052 release_object( hdr );
2053 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2054 return FALSE;
2056 return ret;
2059 static const WCHAR wkday[7][4] =
2060 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2061 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2062 static const WCHAR month[12][4] =
2063 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2064 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2065 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2067 /***********************************************************************
2068 * WinHttpTimeFromSystemTime (WININET.@)
2070 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2072 static const WCHAR format[] =
2073 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2074 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2076 TRACE("%p, %p\n", time, string);
2078 if (!time || !string) return FALSE;
2080 sprintfW( string, format,
2081 wkday[time->wDayOfWeek],
2082 time->wDay,
2083 month[time->wMonth - 1],
2084 time->wYear,
2085 time->wHour,
2086 time->wMinute,
2087 time->wSecond );
2089 return TRUE;
2092 /***********************************************************************
2093 * WinHttpTimeToSystemTime (WININET.@)
2095 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2097 unsigned int i;
2098 const WCHAR *s = string;
2099 WCHAR *end;
2101 TRACE("%s, %p\n", debugstr_w(string), time);
2103 if (!string || !time) return FALSE;
2105 /* Windows does this too */
2106 GetSystemTime( time );
2108 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2109 * a SYSTEMTIME structure.
2112 while (*s && !isalphaW( *s )) s++;
2113 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2114 time->wDayOfWeek = 7;
2116 for (i = 0; i < 7; i++)
2118 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2119 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2120 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2122 time->wDayOfWeek = i;
2123 break;
2127 if (time->wDayOfWeek > 6) return TRUE;
2128 while (*s && !isdigitW( *s )) s++;
2129 time->wDay = strtolW( s, &end, 10 );
2130 s = end;
2132 while (*s && !isalphaW( *s )) s++;
2133 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2134 time->wMonth = 0;
2136 for (i = 0; i < 12; i++)
2138 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2139 toupperW( month[i][1]) == toupperW( s[1] ) &&
2140 toupperW( month[i][2]) == toupperW( s[2] ) )
2142 time->wMonth = i + 1;
2143 break;
2146 if (time->wMonth == 0) return TRUE;
2148 while (*s && !isdigitW( *s )) s++;
2149 if (*s == '\0') return TRUE;
2150 time->wYear = strtolW( s, &end, 10 );
2151 s = end;
2153 while (*s && !isdigitW( *s )) s++;
2154 if (*s == '\0') return TRUE;
2155 time->wHour = strtolW( s, &end, 10 );
2156 s = end;
2158 while (*s && !isdigitW( *s )) s++;
2159 if (*s == '\0') return TRUE;
2160 time->wMinute = strtolW( s, &end, 10 );
2161 s = end;
2163 while (*s && !isdigitW( *s )) s++;
2164 if (*s == '\0') return TRUE;
2165 time->wSecond = strtolW( s, &end, 10 );
2167 time->wMilliseconds = 0;
2168 return TRUE;