makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / winhttp / session.c
blobdabbe9b195464a2890a8bcad492fb14d6d9f802f
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 <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winsock2.h"
24 #include "ws2ipdef.h"
25 #include "ws2tcpip.h"
26 #include "winhttp.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "iphlpapi.h"
30 #include "dhcpcsdk.h"
31 #define COBJMACROS
32 #include "ole2.h"
33 #include "dispex.h"
34 #include "activscp.h"
36 #include "wine/debug.h"
37 #include "winhttp_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
41 #define DEFAULT_RESOLVE_TIMEOUT 0
42 #define DEFAULT_CONNECT_TIMEOUT 20000
43 #define DEFAULT_SEND_TIMEOUT 30000
44 #define DEFAULT_RECEIVE_TIMEOUT 30000
45 #define DEFAULT_RECEIVE_RESPONSE_TIMEOUT ~0u
47 void send_callback( struct object_header *hdr, DWORD status, void *info, DWORD buflen )
49 if (hdr->callback && (hdr->notify_mask & status))
51 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
52 hdr->callback( hdr->handle, hdr->context, status, info, buflen );
53 TRACE("returning from 0x%08x callback\n", status);
57 /***********************************************************************
58 * WinHttpCheckPlatform (winhttp.@)
60 BOOL WINAPI WinHttpCheckPlatform( void )
62 TRACE("\n");
63 return TRUE;
66 /***********************************************************************
67 * session_destroy (internal)
69 static void session_destroy( struct object_header *hdr )
71 struct session *session = (struct session *)hdr;
73 TRACE("%p\n", session);
75 if (session->unload_event) SetEvent( session->unload_event );
76 destroy_cookies( session );
78 session->cs.DebugInfo->Spare[0] = 0;
79 DeleteCriticalSection( &session->cs );
80 heap_free( session->agent );
81 heap_free( session->proxy_server );
82 heap_free( session->proxy_bypass );
83 heap_free( session->proxy_username );
84 heap_free( session->proxy_password );
85 heap_free( session );
88 static BOOL session_query_option( struct object_header *hdr, DWORD option, void *buffer, DWORD *buflen )
90 struct session *session = (struct session *)hdr;
92 switch (option)
94 case WINHTTP_OPTION_REDIRECT_POLICY:
96 if (!buffer || *buflen < sizeof(DWORD))
98 *buflen = sizeof(DWORD);
99 SetLastError( ERROR_INSUFFICIENT_BUFFER );
100 return FALSE;
103 *(DWORD *)buffer = hdr->redirect_policy;
104 *buflen = sizeof(DWORD);
105 return TRUE;
107 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
108 *(DWORD *)buffer = session->resolve_timeout;
109 *buflen = sizeof(DWORD);
110 return TRUE;
112 case WINHTTP_OPTION_CONNECT_TIMEOUT:
113 *(DWORD *)buffer = session->connect_timeout;
114 *buflen = sizeof(DWORD);
115 return TRUE;
117 case WINHTTP_OPTION_SEND_TIMEOUT:
118 *(DWORD *)buffer = session->send_timeout;
119 *buflen = sizeof(DWORD);
120 return TRUE;
122 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
123 *(DWORD *)buffer = session->receive_timeout;
124 *buflen = sizeof(DWORD);
125 return TRUE;
127 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT:
128 *(DWORD *)buffer = session->receive_response_timeout;
129 *buflen = sizeof(DWORD);
130 return TRUE;
132 default:
133 FIXME("unimplemented option %u\n", option);
134 SetLastError( ERROR_INVALID_PARAMETER );
135 return FALSE;
139 static BOOL session_set_option( struct object_header *hdr, DWORD option, void *buffer, DWORD buflen )
141 struct session *session = (struct session *)hdr;
143 switch (option)
145 case WINHTTP_OPTION_PROXY:
147 WINHTTP_PROXY_INFO *pi = buffer;
149 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
150 return TRUE;
152 case WINHTTP_OPTION_REDIRECT_POLICY:
154 DWORD policy;
156 if (buflen != sizeof(policy))
158 SetLastError( ERROR_INSUFFICIENT_BUFFER );
159 return FALSE;
162 policy = *(DWORD *)buffer;
163 TRACE("0x%x\n", policy);
164 hdr->redirect_policy = policy;
165 return TRUE;
167 case WINHTTP_OPTION_SECURE_PROTOCOLS:
169 if (buflen != sizeof(session->secure_protocols))
171 SetLastError( ERROR_INSUFFICIENT_BUFFER );
172 return FALSE;
174 EnterCriticalSection( &session->cs );
175 session->secure_protocols = *(DWORD *)buffer;
176 LeaveCriticalSection( &session->cs );
177 TRACE("0x%x\n", session->secure_protocols);
178 return TRUE;
180 case WINHTTP_OPTION_DISABLE_FEATURE:
181 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
182 return FALSE;
184 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
185 session->resolve_timeout = *(DWORD *)buffer;
186 return TRUE;
188 case WINHTTP_OPTION_CONNECT_TIMEOUT:
189 session->connect_timeout = *(DWORD *)buffer;
190 return TRUE;
192 case WINHTTP_OPTION_SEND_TIMEOUT:
193 session->send_timeout = *(DWORD *)buffer;
194 return TRUE;
196 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
197 session->receive_timeout = *(DWORD *)buffer;
198 return TRUE;
200 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT:
201 session->receive_response_timeout = *(DWORD *)buffer;
202 return TRUE;
204 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
205 session->passport_flags = *(DWORD *)buffer;
206 return TRUE;
208 case WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT:
209 TRACE("WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT: %p\n", *(HANDLE *)buffer);
210 session->unload_event = *(HANDLE *)buffer;
211 return TRUE;
213 case WINHTTP_OPTION_MAX_CONNS_PER_SERVER:
214 FIXME("WINHTTP_OPTION_MAX_CONNS_PER_SERVER: %d\n", *(DWORD *)buffer);
215 return TRUE;
217 case WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER:
218 FIXME("WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER: %d\n", *(DWORD *)buffer);
219 return TRUE;
221 default:
222 FIXME("unimplemented option %u\n", option);
223 SetLastError( ERROR_WINHTTP_INVALID_OPTION );
224 return FALSE;
228 static const struct object_vtbl session_vtbl =
230 session_destroy,
231 session_query_option,
232 session_set_option
235 /***********************************************************************
236 * WinHttpOpen (winhttp.@)
238 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
240 struct session *session;
241 HINTERNET handle = NULL;
243 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
245 if (!(session = heap_alloc_zero( sizeof(struct session) ))) return NULL;
247 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
248 session->hdr.vtbl = &session_vtbl;
249 session->hdr.flags = flags;
250 session->hdr.refs = 1;
251 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
252 list_init( &session->hdr.children );
253 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
254 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
255 session->send_timeout = DEFAULT_SEND_TIMEOUT;
256 session->receive_timeout = DEFAULT_RECEIVE_TIMEOUT;
257 session->receive_response_timeout = DEFAULT_RECEIVE_RESPONSE_TIMEOUT;
258 list_init( &session->cookie_cache );
259 InitializeCriticalSection( &session->cs );
260 session->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": session.cs");
262 if (agent && !(session->agent = strdupW( agent ))) goto end;
263 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
265 WINHTTP_PROXY_INFO info;
267 WinHttpGetDefaultProxyConfiguration( &info );
268 session->access = info.dwAccessType;
269 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
271 GlobalFree( info.lpszProxy );
272 GlobalFree( info.lpszProxyBypass );
273 goto end;
275 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
277 GlobalFree( info.lpszProxy );
278 GlobalFree( info.lpszProxyBypass );
279 goto end;
282 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
284 session->access = access;
285 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
286 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
289 if (!(handle = alloc_handle( &session->hdr ))) goto end;
290 session->hdr.handle = handle;
292 end:
293 release_object( &session->hdr );
294 TRACE("returning %p\n", handle);
295 if (handle) SetLastError( ERROR_SUCCESS );
296 return handle;
299 /***********************************************************************
300 * connect_destroy (internal)
302 static void connect_destroy( struct object_header *hdr )
304 struct connect *connect = (struct connect *)hdr;
306 TRACE("%p\n", connect);
308 release_object( &connect->session->hdr );
310 heap_free( connect->hostname );
311 heap_free( connect->servername );
312 heap_free( connect->username );
313 heap_free( connect->password );
314 heap_free( connect );
317 static BOOL connect_query_option( struct object_header *hdr, DWORD option, void *buffer, DWORD *buflen )
319 struct connect *connect = (struct connect *)hdr;
321 switch (option)
323 case WINHTTP_OPTION_PARENT_HANDLE:
325 if (!buffer || *buflen < sizeof(HINTERNET))
327 *buflen = sizeof(HINTERNET);
328 SetLastError( ERROR_INSUFFICIENT_BUFFER );
329 return FALSE;
332 *(HINTERNET *)buffer = ((struct object_header *)connect->session)->handle;
333 *buflen = sizeof(HINTERNET);
334 return TRUE;
336 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
337 *(DWORD *)buffer = connect->session->resolve_timeout;
338 *buflen = sizeof(DWORD);
339 return TRUE;
341 case WINHTTP_OPTION_CONNECT_TIMEOUT:
342 *(DWORD *)buffer = connect->session->connect_timeout;
343 *buflen = sizeof(DWORD);
344 return TRUE;
346 case WINHTTP_OPTION_SEND_TIMEOUT:
347 *(DWORD *)buffer = connect->session->send_timeout;
348 *buflen = sizeof(DWORD);
349 return TRUE;
351 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
352 *(DWORD *)buffer = connect->session->receive_timeout;
353 *buflen = sizeof(DWORD);
354 return TRUE;
356 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT:
357 *(DWORD *)buffer = connect->session->receive_response_timeout;
358 *buflen = sizeof(DWORD);
359 return TRUE;
361 default:
362 FIXME("unimplemented option %u\n", option);
363 SetLastError( ERROR_INVALID_PARAMETER );
364 return FALSE;
368 static const struct object_vtbl connect_vtbl =
370 connect_destroy,
371 connect_query_option,
372 NULL
375 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
377 BOOL ret = FALSE;
379 if (!wcsicmp( domain, L"<local>" ) && !wcschr( server, '.' ))
380 ret = TRUE;
381 else if (*domain == '*')
383 if (domain[1] == '.')
385 LPCWSTR dot;
387 /* For a hostname to match a wildcard, the last domain must match
388 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
389 * hostname is www.foo.a.b, it matches, but a.b does not.
391 dot = wcschr( server, '.' );
392 if (dot)
394 int len = lstrlenW( dot + 1 );
396 if (len > lstrlenW( domain + 2 ))
398 LPCWSTR ptr;
400 /* The server's domain is longer than the wildcard, so it
401 * could be a subdomain. Compare the last portion of the
402 * server's domain.
404 ptr = dot + len + 1 - lstrlenW( domain + 2 );
405 if (!wcsicmp( ptr, domain + 2 ))
407 /* This is only a match if the preceding character is
408 * a '.', i.e. that it is a matching domain. E.g.
409 * if domain is '*.b.c' and server is 'www.ab.c' they
410 * do not match.
412 ret = *(ptr - 1) == '.';
415 else
416 ret = !wcsicmp( dot + 1, domain + 2 );
420 else
421 ret = !wcsicmp( server, domain );
422 return ret;
425 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
426 #define MAX_HOST_NAME_LENGTH 256
428 static BOOL should_bypass_proxy(struct session *session, LPCWSTR server)
430 LPCWSTR ptr;
431 BOOL ret = FALSE;
433 if (!session->proxy_bypass) return FALSE;
434 ptr = session->proxy_bypass;
435 do {
436 LPCWSTR tmp = ptr;
438 ptr = wcschr( ptr, ';' );
439 if (!ptr)
440 ptr = wcschr( tmp, ' ' );
441 if (ptr)
443 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
445 WCHAR domain[MAX_HOST_NAME_LENGTH];
447 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
448 domain[ptr - tmp] = 0;
449 ret = domain_matches( server, domain );
451 ptr += 1;
453 else if (*tmp)
454 ret = domain_matches( server, tmp );
455 } while (ptr && !ret);
456 return ret;
459 BOOL set_server_for_hostname( struct connect *connect, const WCHAR *server, INTERNET_PORT port )
461 struct session *session = connect->session;
462 BOOL ret = TRUE;
464 if (session->proxy_server && !should_bypass_proxy(session, server))
466 LPCWSTR colon;
468 if ((colon = wcschr( session->proxy_server, ':' )))
470 if (!connect->servername || wcsnicmp( connect->servername,
471 session->proxy_server, colon - session->proxy_server - 1 ))
473 heap_free( connect->servername );
474 connect->resolved = FALSE;
475 if (!(connect->servername = heap_alloc(
476 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
478 ret = FALSE;
479 goto end;
481 memcpy( connect->servername, session->proxy_server,
482 (colon - session->proxy_server) * sizeof(WCHAR) );
483 connect->servername[colon - session->proxy_server] = 0;
484 if (*(colon + 1))
485 connect->serverport = wcstol( colon + 1, NULL, 10 );
486 else
487 connect->serverport = INTERNET_DEFAULT_PORT;
490 else
492 if (!connect->servername || wcsicmp( connect->servername,
493 session->proxy_server ))
495 heap_free( connect->servername );
496 connect->resolved = FALSE;
497 if (!(connect->servername = strdupW( session->proxy_server )))
499 ret = FALSE;
500 goto end;
502 connect->serverport = INTERNET_DEFAULT_PORT;
506 else if (server)
508 heap_free( connect->servername );
509 connect->resolved = FALSE;
510 if (!(connect->servername = strdupW( server )))
512 ret = FALSE;
513 goto end;
515 connect->serverport = port;
517 end:
518 return ret;
521 /***********************************************************************
522 * WinHttpConnect (winhttp.@)
524 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
526 struct connect *connect;
527 struct session *session;
528 HINTERNET hconnect = NULL;
530 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
532 if (!server)
534 SetLastError( ERROR_INVALID_PARAMETER );
535 return NULL;
537 if (!(session = (struct session *)grab_object( hsession )))
539 SetLastError( ERROR_INVALID_HANDLE );
540 return NULL;
542 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
544 release_object( &session->hdr );
545 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
546 return NULL;
548 if (!(connect = heap_alloc_zero( sizeof(struct connect) )))
550 release_object( &session->hdr );
551 return NULL;
553 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
554 connect->hdr.vtbl = &connect_vtbl;
555 connect->hdr.refs = 1;
556 connect->hdr.flags = session->hdr.flags;
557 connect->hdr.callback = session->hdr.callback;
558 connect->hdr.notify_mask = session->hdr.notify_mask;
559 connect->hdr.context = session->hdr.context;
560 connect->hdr.redirect_policy = session->hdr.redirect_policy;
561 list_init( &connect->hdr.children );
563 addref_object( &session->hdr );
564 connect->session = session;
565 list_add_head( &session->hdr.children, &connect->hdr.entry );
567 if (!(connect->hostname = strdupW( server ))) goto end;
568 connect->hostport = port;
569 if (!set_server_for_hostname( connect, server, port )) goto end;
571 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
572 connect->hdr.handle = hconnect;
574 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
576 end:
577 release_object( &connect->hdr );
578 release_object( &session->hdr );
579 TRACE("returning %p\n", hconnect);
580 if (hconnect) SetLastError( ERROR_SUCCESS );
581 return hconnect;
584 /***********************************************************************
585 * request_destroy (internal)
587 static void request_destroy( struct object_header *hdr )
589 struct request *request = (struct request *)hdr;
590 unsigned int i, j;
592 TRACE("%p\n", request);
594 if (request->task_proc_running)
596 /* Signal to the task proc to quit. It will call this again when it does. */
597 request->task_proc_running = FALSE;
598 SetEvent( request->task_cancel );
599 return;
601 release_object( &request->connect->hdr );
603 if (request->cred_handle_initialized) FreeCredentialsHandle( &request->cred_handle );
604 CertFreeCertificateContext( request->server_cert );
605 CertFreeCertificateContext( request->client_cert );
607 destroy_authinfo( request->authinfo );
608 destroy_authinfo( request->proxy_authinfo );
610 heap_free( request->verb );
611 heap_free( request->path );
612 heap_free( request->version );
613 heap_free( request->raw_headers );
614 heap_free( request->status_text );
615 for (i = 0; i < request->num_headers; i++)
617 heap_free( request->headers[i].field );
618 heap_free( request->headers[i].value );
620 heap_free( request->headers );
621 for (i = 0; i < TARGET_MAX; i++)
623 for (j = 0; j < SCHEME_MAX; j++)
625 heap_free( request->creds[i][j].username );
626 heap_free( request->creds[i][j].password );
629 heap_free( request );
632 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
634 int len = 0;
635 if (str) len = lstrlenW( str );
636 if (buffer && *buflen > len)
638 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
639 buffer[len] = 0;
641 *buflen = len * sizeof(WCHAR);
644 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
646 WCHAR *ret;
647 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
649 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
650 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
651 CertNameToStrW( encoding, blob, format, ret, size );
653 return ret;
656 static BOOL copy_sockaddr( const struct sockaddr *addr, SOCKADDR_STORAGE *addr_storage )
658 switch (addr->sa_family)
660 case AF_INET:
662 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_storage;
664 memcpy( addr_in, addr, sizeof(*addr_in) );
665 memset( addr_in + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in) );
666 return TRUE;
668 case AF_INET6:
670 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr_storage;
672 memcpy( addr_in6, addr, sizeof(*addr_in6) );
673 memset( addr_in6 + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in6) );
674 return TRUE;
676 default:
677 ERR("unhandled family %u\n", addr->sa_family);
678 return FALSE;
682 static BOOL request_query_option( struct object_header *hdr, DWORD option, void *buffer, DWORD *buflen )
684 struct request *request = (struct request *)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 SetLastError( ERROR_INSUFFICIENT_BUFFER );
697 return FALSE;
700 flags = request->security_flags;
701 if (request->netconn)
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;
711 *(DWORD *)buffer = flags;
712 *buflen = sizeof(flags);
713 return TRUE;
715 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
717 const CERT_CONTEXT *cert;
719 if (!buffer || *buflen < sizeof(cert))
721 *buflen = sizeof(cert);
722 SetLastError( ERROR_INSUFFICIENT_BUFFER );
723 return FALSE;
726 if (!(cert = CertDuplicateCertificateContext( request->server_cert ))) return FALSE;
727 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
728 *buflen = sizeof(cert);
729 return TRUE;
731 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
733 const CERT_CONTEXT *cert = request->server_cert;
734 const CRYPT_OID_INFO *oidInfo;
735 WINHTTP_CERTIFICATE_INFO *ci = buffer;
737 FIXME("partial stub\n");
739 if (!buffer || *buflen < sizeof(*ci))
741 *buflen = sizeof(*ci);
742 SetLastError( ERROR_INSUFFICIENT_BUFFER );
743 return FALSE;
745 if (!cert) return FALSE;
747 ci->ftExpiry = cert->pCertInfo->NotAfter;
748 ci->ftStart = cert->pCertInfo->NotBefore;
749 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
750 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
751 ci->lpszProtocolName = NULL;
752 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY, cert->pCertInfo->SignatureAlgorithm.pszObjId, 0 );
753 if (oidInfo)
754 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
755 else
756 ci->lpszSignatureAlgName = NULL;
757 ci->lpszEncryptionAlgName = NULL;
758 ci->dwKeySize = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
760 *buflen = sizeof(*ci);
761 return TRUE;
763 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
765 if (!buffer || *buflen < sizeof(DWORD))
767 *buflen = sizeof(DWORD);
768 SetLastError( ERROR_INSUFFICIENT_BUFFER );
769 return FALSE;
772 *(DWORD *)buffer = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
773 *buflen = sizeof(DWORD);
774 return TRUE;
776 case WINHTTP_OPTION_CONNECTION_INFO:
778 WINHTTP_CONNECTION_INFO *info = buffer;
779 struct sockaddr local;
780 socklen_t len = sizeof(local);
781 const struct sockaddr *remote = (const struct sockaddr *)&request->connect->sockaddr;
783 if (!buffer || *buflen < sizeof(*info))
785 *buflen = sizeof(*info);
786 SetLastError( ERROR_INSUFFICIENT_BUFFER );
787 return FALSE;
789 if (!request->netconn)
791 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
792 return FALSE;
794 if (getsockname( request->netconn->socket, &local, &len )) return FALSE;
795 if (!copy_sockaddr( &local, &info->LocalAddress )) return FALSE;
796 if (!copy_sockaddr( remote, &info->RemoteAddress )) return FALSE;
797 info->cbSize = sizeof(*info);
798 return TRUE;
800 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
801 *(DWORD *)buffer = request->resolve_timeout;
802 *buflen = sizeof(DWORD);
803 return TRUE;
805 case WINHTTP_OPTION_CONNECT_TIMEOUT:
806 *(DWORD *)buffer = request->connect_timeout;
807 *buflen = sizeof(DWORD);
808 return TRUE;
810 case WINHTTP_OPTION_SEND_TIMEOUT:
811 *(DWORD *)buffer = request->send_timeout;
812 *buflen = sizeof(DWORD);
813 return TRUE;
815 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
816 *(DWORD *)buffer = request->receive_timeout;
817 *buflen = sizeof(DWORD);
818 return TRUE;
820 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT:
821 *(DWORD *)buffer = request->receive_response_timeout;
822 *buflen = sizeof(DWORD);
823 return TRUE;
825 case WINHTTP_OPTION_USERNAME:
826 str_to_buffer( buffer, request->connect->username, buflen );
827 return TRUE;
829 case WINHTTP_OPTION_PASSWORD:
830 str_to_buffer( buffer, request->connect->password, buflen );
831 return TRUE;
833 case WINHTTP_OPTION_PROXY_USERNAME:
834 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
835 return TRUE;
837 case WINHTTP_OPTION_PROXY_PASSWORD:
838 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
839 return TRUE;
841 default:
842 FIXME("unimplemented option %u\n", option);
843 SetLastError( ERROR_INVALID_PARAMETER );
844 return FALSE;
848 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
850 WCHAR *ret;
851 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
853 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
854 ret[buflen] = 0;
855 return ret;
857 SetLastError( ERROR_OUTOFMEMORY );
858 return NULL;
861 static BOOL request_set_option( struct object_header *hdr, DWORD option, void *buffer, DWORD buflen )
863 struct request *request = (struct request *)hdr;
865 switch (option)
867 case WINHTTP_OPTION_PROXY:
869 WINHTTP_PROXY_INFO *pi = buffer;
871 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
872 return TRUE;
874 case WINHTTP_OPTION_DISABLE_FEATURE:
876 DWORD disable;
878 if (buflen != sizeof(DWORD))
880 SetLastError( ERROR_INSUFFICIENT_BUFFER );
881 return FALSE;
884 disable = *(DWORD *)buffer;
885 TRACE("0x%x\n", disable);
886 hdr->disable_flags |= disable;
887 return TRUE;
889 case WINHTTP_OPTION_AUTOLOGON_POLICY:
891 DWORD policy;
893 if (buflen != sizeof(DWORD))
895 SetLastError( ERROR_INSUFFICIENT_BUFFER );
896 return FALSE;
899 policy = *(DWORD *)buffer;
900 TRACE("0x%x\n", policy);
901 hdr->logon_policy = policy;
902 return TRUE;
904 case WINHTTP_OPTION_REDIRECT_POLICY:
906 DWORD policy;
908 if (buflen != sizeof(DWORD))
910 SetLastError( ERROR_INSUFFICIENT_BUFFER );
911 return FALSE;
914 policy = *(DWORD *)buffer;
915 TRACE("0x%x\n", policy);
916 hdr->redirect_policy = policy;
917 return TRUE;
919 case WINHTTP_OPTION_SECURITY_FLAGS:
921 DWORD flags;
922 static const DWORD accepted = SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
923 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
924 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
925 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
927 if (buflen < sizeof(DWORD))
929 SetLastError( ERROR_INSUFFICIENT_BUFFER );
930 return FALSE;
932 flags = *(DWORD *)buffer;
933 TRACE("0x%x\n", flags);
934 if (flags && (flags & ~accepted))
936 SetLastError( ERROR_INVALID_PARAMETER );
937 return FALSE;
939 request->security_flags = flags;
940 return TRUE;
942 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
943 request->resolve_timeout = *(DWORD *)buffer;
944 return TRUE;
946 case WINHTTP_OPTION_CONNECT_TIMEOUT:
947 request->connect_timeout = *(DWORD *)buffer;
948 return TRUE;
950 case WINHTTP_OPTION_SEND_TIMEOUT:
951 request->send_timeout = *(DWORD *)buffer;
952 return TRUE;
954 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
955 request->receive_timeout = *(DWORD *)buffer;
956 return TRUE;
958 case WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT:
959 request->receive_response_timeout = *(DWORD *)buffer;
960 return TRUE;
962 case WINHTTP_OPTION_USERNAME:
964 struct connect *connect = request->connect;
966 heap_free( connect->username );
967 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
968 return TRUE;
970 case WINHTTP_OPTION_PASSWORD:
972 struct connect *connect = request->connect;
974 heap_free( connect->password );
975 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
976 return TRUE;
978 case WINHTTP_OPTION_PROXY_USERNAME:
980 struct session *session = request->connect->session;
982 heap_free( session->proxy_username );
983 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
984 return TRUE;
986 case WINHTTP_OPTION_PROXY_PASSWORD:
988 struct session *session = request->connect->session;
990 heap_free( session->proxy_password );
991 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
992 return TRUE;
994 case WINHTTP_OPTION_CLIENT_CERT_CONTEXT:
996 const CERT_CONTEXT *cert;
998 if (!(hdr->flags & WINHTTP_FLAG_SECURE))
1000 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
1001 return FALSE;
1003 if (!buffer)
1005 CertFreeCertificateContext( request->client_cert );
1006 request->client_cert = NULL;
1008 else if (buflen >= sizeof(cert))
1010 if (!(cert = CertDuplicateCertificateContext( buffer ))) return FALSE;
1011 CertFreeCertificateContext( request->client_cert );
1012 request->client_cert = cert;
1014 else
1016 SetLastError( ERROR_INVALID_PARAMETER );
1017 return FALSE;
1020 if (request->cred_handle_initialized)
1022 FreeCredentialsHandle( &request->cred_handle );
1023 request->cred_handle_initialized = FALSE;
1026 return TRUE;
1028 case WINHTTP_OPTION_ENABLE_FEATURE:
1029 if(buflen == sizeof( DWORD ) && *(DWORD *)buffer == WINHTTP_ENABLE_SSL_REVOCATION)
1031 request->check_revocation = TRUE;
1032 SetLastError( NO_ERROR );
1033 return TRUE;
1035 else
1037 SetLastError( ERROR_INVALID_PARAMETER );
1038 return FALSE;
1041 case WINHTTP_OPTION_CONNECT_RETRIES:
1042 FIXME("WINHTTP_OPTION_CONNECT_RETRIES\n");
1043 return TRUE;
1045 default:
1046 FIXME("unimplemented option %u\n", option);
1047 SetLastError( ERROR_WINHTTP_INVALID_OPTION );
1048 return FALSE;
1052 static const struct object_vtbl request_vtbl =
1054 request_destroy,
1055 request_query_option,
1056 request_set_option
1059 static BOOL add_accept_types_header( struct request *request, const WCHAR **types )
1061 static const DWORD flags = WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA;
1063 if (!types) return TRUE;
1064 while (*types)
1066 if (!process_header( request, L"Accept", *types, flags, TRUE )) return FALSE;
1067 types++;
1069 return TRUE;
1072 static WCHAR *get_request_path( const WCHAR *object )
1074 int len = object ? lstrlenW(object) : 0;
1075 WCHAR *p, *ret;
1077 if (!object || object[0] != '/') len++;
1078 if (!(p = ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1079 if (!object || object[0] != '/') *p++ = '/';
1080 if (object) lstrcpyW( p, object );
1081 ret[len] = 0;
1082 return ret;
1085 /***********************************************************************
1086 * WinHttpOpenRequest (winhttp.@)
1088 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
1089 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
1091 struct request *request;
1092 struct connect *connect;
1093 HINTERNET hrequest = NULL;
1095 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
1096 debugstr_w(version), debugstr_w(referrer), types, flags);
1098 if (types && TRACE_ON(winhttp))
1100 const WCHAR **iter;
1101 TRACE("accept types:\n");
1102 for (iter = types; *iter; iter++) TRACE(" %s\n", debugstr_w(*iter));
1105 if (!(connect = (struct connect *)grab_object( hconnect )))
1107 SetLastError( ERROR_INVALID_HANDLE );
1108 return NULL;
1110 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
1112 release_object( &connect->hdr );
1113 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1114 return NULL;
1116 if (!(request = heap_alloc_zero( sizeof(struct request) )))
1118 release_object( &connect->hdr );
1119 return NULL;
1121 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
1122 request->hdr.vtbl = &request_vtbl;
1123 request->hdr.refs = 1;
1124 request->hdr.flags = flags;
1125 request->hdr.callback = connect->hdr.callback;
1126 request->hdr.notify_mask = connect->hdr.notify_mask;
1127 request->hdr.context = connect->hdr.context;
1128 request->hdr.redirect_policy = connect->hdr.redirect_policy;
1129 list_init( &request->hdr.children );
1130 list_init( &request->task_queue );
1132 addref_object( &connect->hdr );
1133 request->connect = connect;
1134 list_add_head( &connect->hdr.children, &request->hdr.entry );
1136 request->resolve_timeout = connect->session->resolve_timeout;
1137 request->connect_timeout = connect->session->connect_timeout;
1138 request->send_timeout = connect->session->send_timeout;
1139 request->receive_timeout = connect->session->receive_timeout;
1140 request->receive_response_timeout = connect->session->receive_response_timeout;
1142 if (!verb || !verb[0]) verb = L"GET";
1143 if (!(request->verb = strdupW( verb ))) goto end;
1144 if (!(request->path = get_request_path( object ))) goto end;
1146 if (!version || !version[0]) version = L"HTTP/1.1";
1147 if (!(request->version = strdupW( version ))) goto end;
1148 if (!(add_accept_types_header( request, types ))) goto end;
1150 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
1151 request->hdr.handle = hrequest;
1153 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
1155 end:
1156 release_object( &request->hdr );
1157 release_object( &connect->hdr );
1158 TRACE("returning %p\n", hrequest);
1159 if (hrequest) SetLastError( ERROR_SUCCESS );
1160 return hrequest;
1163 /***********************************************************************
1164 * WinHttpCloseHandle (winhttp.@)
1166 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1168 struct object_header *hdr;
1170 TRACE("%p\n", handle);
1172 if (!(hdr = grab_object( handle )))
1174 SetLastError( ERROR_INVALID_HANDLE );
1175 return FALSE;
1177 release_object( hdr );
1178 free_handle( handle );
1179 SetLastError( ERROR_SUCCESS );
1180 return TRUE;
1183 static BOOL query_option( struct object_header *hdr, DWORD option, void *buffer, DWORD *buflen )
1185 BOOL ret = FALSE;
1187 if (!buflen)
1189 SetLastError( ERROR_INVALID_PARAMETER );
1190 return FALSE;
1193 switch (option)
1195 case WINHTTP_OPTION_CONTEXT_VALUE:
1197 if (!buffer || *buflen < sizeof(DWORD_PTR))
1199 *buflen = sizeof(DWORD_PTR);
1200 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1201 return FALSE;
1204 *(DWORD_PTR *)buffer = hdr->context;
1205 *buflen = sizeof(DWORD_PTR);
1206 return TRUE;
1208 default:
1209 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1210 else
1212 FIXME("unimplemented option %u\n", option);
1213 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1214 return FALSE;
1216 break;
1218 return ret;
1221 /***********************************************************************
1222 * WinHttpQueryOption (winhttp.@)
1224 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1226 BOOL ret = FALSE;
1227 struct object_header *hdr;
1229 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1231 if (!(hdr = grab_object( handle )))
1233 SetLastError( ERROR_INVALID_HANDLE );
1234 return FALSE;
1237 ret = query_option( hdr, option, buffer, buflen );
1239 release_object( hdr );
1240 if (ret) SetLastError( ERROR_SUCCESS );
1241 return ret;
1244 static BOOL set_option( struct object_header *hdr, DWORD option, void *buffer, DWORD buflen )
1246 BOOL ret = TRUE;
1248 if (!buffer && buflen)
1250 SetLastError( ERROR_INVALID_PARAMETER );
1251 return FALSE;
1254 switch (option)
1256 case WINHTTP_OPTION_CONTEXT_VALUE:
1258 if (buflen != sizeof(DWORD_PTR))
1260 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1261 return FALSE;
1264 hdr->context = *(DWORD_PTR *)buffer;
1265 return TRUE;
1267 default:
1268 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1269 else
1271 FIXME("unimplemented option %u\n", option);
1272 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1273 return FALSE;
1275 break;
1277 return ret;
1280 /***********************************************************************
1281 * WinHttpSetOption (winhttp.@)
1283 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1285 BOOL ret = FALSE;
1286 struct object_header *hdr;
1288 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1290 if (!(hdr = grab_object( handle )))
1292 SetLastError( ERROR_INVALID_HANDLE );
1293 return FALSE;
1296 ret = set_option( hdr, option, buffer, buflen );
1298 release_object( hdr );
1299 if (ret) SetLastError( ERROR_SUCCESS );
1300 return ret;
1303 static IP_ADAPTER_ADDRESSES *get_adapters(void)
1305 ULONG err, size = 1024, flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
1306 GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME;
1307 IP_ADAPTER_ADDRESSES *tmp, *ret;
1309 if (!(ret = heap_alloc( size ))) return NULL;
1310 err = GetAdaptersAddresses( AF_UNSPEC, flags, NULL, ret, &size );
1311 while (err == ERROR_BUFFER_OVERFLOW)
1313 if (!(tmp = heap_realloc( ret, size ))) break;
1314 ret = tmp;
1315 err = GetAdaptersAddresses( AF_UNSPEC, flags, NULL, ret, &size );
1317 if (err == ERROR_SUCCESS) return ret;
1318 heap_free( ret );
1319 return NULL;
1322 static WCHAR *detect_autoproxyconfig_url_dhcp(void)
1324 IP_ADAPTER_ADDRESSES *adapters, *ptr;
1325 DHCPCAPI_PARAMS_ARRAY send_params, recv_params;
1326 DHCPCAPI_PARAMS param;
1327 WCHAR name[MAX_ADAPTER_NAME_LENGTH + 1], *ret = NULL;
1328 DWORD err, size;
1329 BYTE *tmp, *buf = NULL;
1331 if (!(adapters = get_adapters())) return NULL;
1333 memset( &send_params, 0, sizeof(send_params) );
1334 memset( &param, 0, sizeof(param) );
1335 param.OptionId = OPTION_MSFT_IE_PROXY;
1336 recv_params.nParams = 1;
1337 recv_params.Params = &param;
1339 for (ptr = adapters; ptr; ptr = ptr->Next)
1341 MultiByteToWideChar( CP_ACP, 0, ptr->AdapterName, -1, name, ARRAY_SIZE(name) );
1342 TRACE( "adapter '%s' type %u dhcpv4 enabled %d\n", wine_dbgstr_w(name), ptr->IfType, ptr->Dhcpv4Enabled );
1344 if (ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK) continue;
1345 /* FIXME: also skip adapters where DHCP is disabled */
1347 size = 256;
1348 if (!(buf = heap_alloc( size ))) goto done;
1349 err = DhcpRequestParams( DHCPCAPI_REQUEST_SYNCHRONOUS, NULL, name, NULL, send_params, recv_params,
1350 buf, &size, NULL );
1351 while (err == ERROR_MORE_DATA)
1353 if (!(tmp = heap_realloc( buf, size ))) goto done;
1354 buf = tmp;
1355 err = DhcpRequestParams( DHCPCAPI_REQUEST_SYNCHRONOUS, NULL, name, NULL, send_params, recv_params,
1356 buf, &size, NULL );
1358 if (err == ERROR_SUCCESS && param.nBytesData)
1360 int len = MultiByteToWideChar( CP_ACP, 0, (const char *)param.Data, param.nBytesData, NULL, 0 );
1361 if ((ret = heap_alloc( (len + 1) * sizeof(WCHAR) )))
1363 MultiByteToWideChar( CP_ACP, 0, (const char *)param.Data, param.nBytesData, ret, len );
1364 ret[len] = 0;
1366 TRACE("returning %s\n", debugstr_w(ret));
1367 break;
1371 done:
1372 heap_free( buf );
1373 heap_free( adapters );
1374 return ret;
1377 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1379 char *ret;
1380 DWORD size = 0;
1382 GetComputerNameExA( format, NULL, &size );
1383 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1384 if (!(ret = heap_alloc( size ))) return NULL;
1385 if (!GetComputerNameExA( format, ret, &size ))
1387 heap_free( ret );
1388 return NULL;
1390 return ret;
1393 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1395 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1397 if (len_suffix > len_domain) return FALSE;
1398 if (!stricmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1399 return FALSE;
1402 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1404 return getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1407 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1409 char name[NI_MAXHOST];
1410 WCHAR *ret, *p;
1411 int len;
1413 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1414 if (!ai) return NULL;
1416 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1418 len = lstrlenW( L"http://" ) + strlen( hostname ) + lstrlenW( L"/wpad.dat" );
1419 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1420 lstrcpyW( p, L"http://" );
1421 p += lstrlenW( L"http://" );
1422 while (*hostname) { *p++ = *hostname++; }
1423 lstrcpyW( p, L"/wpad.dat" );
1424 return ret;
1427 static WCHAR *detect_autoproxyconfig_url_dns(void)
1429 char *fqdn, *domain, *p;
1430 WCHAR *ret = NULL;
1432 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return NULL;
1433 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1435 heap_free( fqdn );
1436 return NULL;
1438 p = fqdn;
1439 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1441 char *name;
1442 struct addrinfo *ai;
1443 int res;
1445 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1447 heap_free( fqdn );
1448 heap_free( domain );
1449 return NULL;
1451 strcpy( name, "wpad" );
1452 strcat( name, p );
1453 res = getaddrinfo( name, NULL, NULL, &ai );
1454 if (!res)
1456 ret = build_wpad_url( name, ai );
1457 freeaddrinfo( ai );
1458 if (ret)
1460 TRACE("returning %s\n", debugstr_w(ret));
1461 heap_free( name );
1462 break;
1465 heap_free( name );
1466 p++;
1468 heap_free( domain );
1469 heap_free( fqdn );
1470 return ret;
1473 /***********************************************************************
1474 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1476 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1478 TRACE("0x%08x, %p\n", flags, url);
1480 if (!flags || !url)
1482 SetLastError( ERROR_INVALID_PARAMETER );
1483 return FALSE;
1485 *url = NULL;
1486 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1488 *url = detect_autoproxyconfig_url_dhcp();
1490 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1492 if (!*url) *url = detect_autoproxyconfig_url_dns();
1494 if (!*url)
1496 SetLastError( ERROR_WINHTTP_AUTODETECTION_FAILED );
1497 return FALSE;
1499 SetLastError( ERROR_SUCCESS );
1500 return TRUE;
1503 static const WCHAR path_connections[] =
1504 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections";
1506 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1507 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1508 static const DWORD PROXY_TYPE_DIRECT = 1;
1509 static const DWORD PROXY_TYPE_PROXY = 2;
1510 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1511 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1513 struct connection_settings_header
1515 DWORD magic;
1516 DWORD unknown; /* always zero? */
1517 DWORD flags; /* one or more of PROXY_* */
1520 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1522 const BYTE *begin;
1524 for (begin = src; src - begin < len; src++, dst++)
1525 *dst = *src;
1526 *dst = 0;
1529 /***********************************************************************
1530 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1532 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1534 LONG l;
1535 HKEY key;
1536 BOOL got_from_reg = FALSE, direct = TRUE;
1537 WCHAR *envproxy;
1539 TRACE("%p\n", info);
1541 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, path_connections, 0, KEY_READ, &key );
1542 if (!l)
1544 DWORD type, size = 0;
1546 l = RegQueryValueExW( key, L"WinHttpSettings", NULL, &type, NULL, &size );
1547 if (!l && type == REG_BINARY &&
1548 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1550 BYTE *buf = heap_alloc( size );
1552 if (buf)
1554 struct connection_settings_header *hdr =
1555 (struct connection_settings_header *)buf;
1556 DWORD *len = (DWORD *)(hdr + 1);
1558 l = RegQueryValueExW( key, L"WinHttpSettings", NULL, NULL, buf,
1559 &size );
1560 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1561 hdr->unknown == 0)
1563 if (hdr->flags & PROXY_TYPE_PROXY)
1565 BOOL sane = FALSE;
1566 LPWSTR proxy = NULL;
1567 LPWSTR proxy_bypass = NULL;
1569 /* Sanity-check length of proxy string */
1570 if ((BYTE *)len - buf + *len <= size)
1572 sane = TRUE;
1573 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1574 if (proxy)
1575 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1576 len = (DWORD *)((BYTE *)(len + 1) + *len);
1578 if (sane)
1580 /* Sanity-check length of proxy bypass string */
1581 if ((BYTE *)len - buf + *len <= size)
1583 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1584 if (proxy_bypass)
1585 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1587 else
1589 sane = FALSE;
1590 GlobalFree( proxy );
1591 proxy = NULL;
1594 info->lpszProxy = proxy;
1595 info->lpszProxyBypass = proxy_bypass;
1596 if (sane)
1598 got_from_reg = TRUE;
1599 direct = FALSE;
1600 info->dwAccessType =
1601 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1602 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1603 debugstr_w(info->lpszProxy),
1604 debugstr_w(info->lpszProxyBypass));
1608 heap_free( buf );
1611 RegCloseKey( key );
1613 if (!got_from_reg && (envproxy = _wgetenv( L"http_proxy" )))
1615 WCHAR *colon, *http_proxy = NULL;
1617 if (!(colon = wcschr( envproxy, ':' ))) http_proxy = envproxy;
1618 else
1620 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1622 /* It's a scheme, check that it's http */
1623 if (!wcsncmp( envproxy, L"http://", 7 )) http_proxy = envproxy + 7;
1624 else WARN("unsupported scheme in $http_proxy: %s\n", debugstr_w(envproxy));
1626 else http_proxy = envproxy;
1629 if (http_proxy && http_proxy[0])
1631 direct = FALSE;
1632 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1633 info->lpszProxy = GlobalAlloc( 0, (lstrlenW(http_proxy) + 1) * sizeof(WCHAR) );
1634 wcscpy( info->lpszProxy, http_proxy );
1635 info->lpszProxyBypass = NULL;
1636 TRACE("http proxy (from environment) = %s\n", debugstr_w(info->lpszProxy));
1639 if (direct)
1641 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1642 info->lpszProxy = NULL;
1643 info->lpszProxyBypass = NULL;
1645 SetLastError( ERROR_SUCCESS );
1646 return TRUE;
1649 /***********************************************************************
1650 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1652 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1654 HKEY hkey = NULL;
1655 struct connection_settings_header *hdr = NULL;
1656 DWORD type, offset, len, size = 0;
1657 BOOL ret = FALSE;
1659 TRACE("%p\n", config);
1661 if (!config)
1663 SetLastError( ERROR_INVALID_PARAMETER );
1664 return FALSE;
1666 memset( config, 0, sizeof(*config) );
1667 config->fAutoDetect = TRUE;
1669 if (RegOpenKeyExW( HKEY_CURRENT_USER, path_connections, 0, KEY_READ, &hkey ) ||
1670 RegQueryValueExW( hkey, L"DefaultConnectionSettings", NULL, &type, NULL, &size ) ||
1671 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1673 ret = TRUE;
1674 goto done;
1676 if (!(hdr = heap_alloc( size ))) goto done;
1677 if (RegQueryValueExW( hkey, L"DefaultConnectionSettings", NULL, &type, (BYTE *)hdr, &size ) ||
1678 hdr->magic != WININET_SETTINGS_MAGIC)
1680 ret = TRUE;
1681 goto done;
1684 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1685 offset = sizeof(*hdr);
1686 if (offset + sizeof(DWORD) > size) goto done;
1687 len = *(DWORD *)((char *)hdr + offset);
1688 offset += sizeof(DWORD);
1689 if (len && hdr->flags & PROXY_TYPE_PROXY)
1691 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1692 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1694 offset += len;
1695 if (offset + sizeof(DWORD) > size) goto done;
1696 len = *(DWORD *)((char *)hdr + offset);
1697 offset += sizeof(DWORD);
1698 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1700 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1701 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1703 offset += len;
1704 if (offset + sizeof(DWORD) > size) goto done;
1705 len = *(DWORD *)((char *)hdr + offset);
1706 offset += sizeof(DWORD);
1707 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1709 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1710 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1712 ret = TRUE;
1714 done:
1715 RegCloseKey( hkey );
1716 heap_free( hdr );
1717 if (!ret)
1719 GlobalFree( config->lpszAutoConfigUrl );
1720 config->lpszAutoConfigUrl = NULL;
1721 GlobalFree( config->lpszProxy );
1722 config->lpszProxy = NULL;
1723 GlobalFree( config->lpszProxyBypass );
1724 config->lpszProxyBypass = NULL;
1726 else SetLastError( ERROR_SUCCESS );
1727 return ret;
1730 static BOOL parse_script_result( const char *result, WINHTTP_PROXY_INFO *info )
1732 const char *p;
1733 WCHAR *q;
1734 int len;
1736 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1737 info->lpszProxy = NULL;
1738 info->lpszProxyBypass = NULL;
1740 TRACE("%s\n", debugstr_a( result ));
1742 p = result;
1743 while (*p == ' ') p++;
1744 len = strlen( p );
1745 if (len >= 5 && !_strnicmp( p, "PROXY", sizeof("PROXY") - 1 ))
1747 p += 5;
1748 while (*p == ' ') p++;
1749 if (!*p || *p == ';') return TRUE;
1750 if (!(info->lpszProxy = q = strdupAW( p ))) return FALSE;
1751 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1752 for (; *q; q++)
1754 if (*q == ' ' || *q == ';')
1756 *q = 0;
1757 break;
1761 return TRUE;
1764 static char *download_script( const WCHAR *url, DWORD *out_size )
1766 static const WCHAR *acceptW[] = {L"*/*", NULL};
1767 HINTERNET ses, con = NULL, req = NULL;
1768 WCHAR *hostname;
1769 URL_COMPONENTSW uc;
1770 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1771 char *tmp, *buffer = NULL;
1773 *out_size = 0;
1775 memset( &uc, 0, sizeof(uc) );
1776 uc.dwStructSize = sizeof(uc);
1777 uc.dwHostNameLength = -1;
1778 uc.dwUrlPathLength = -1;
1779 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1780 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1781 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1782 hostname[uc.dwHostNameLength] = 0;
1784 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1785 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1786 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1787 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1788 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1790 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1791 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1792 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1794 size = 4096;
1795 if (!(buffer = heap_alloc( size ))) goto done;
1796 to_read = size;
1797 offset = 0;
1798 for (;;)
1800 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1801 if (!bytes_read) break;
1802 to_read -= bytes_read;
1803 offset += bytes_read;
1804 *out_size += bytes_read;
1805 if (!to_read)
1807 to_read = size;
1808 size *= 2;
1809 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1810 buffer = tmp;
1814 done:
1815 WinHttpCloseHandle( req );
1816 WinHttpCloseHandle( con );
1817 WinHttpCloseHandle( ses );
1818 heap_free( hostname );
1819 if (!buffer) SetLastError( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1820 return buffer;
1823 struct AUTO_PROXY_SCRIPT_BUFFER
1825 DWORD dwStructSize;
1826 LPSTR lpszScriptBuffer;
1827 DWORD dwScriptBufferSize;
1830 BOOL WINAPI InternetDeInitializeAutoProxyDll(LPSTR, DWORD);
1831 BOOL WINAPI InternetGetProxyInfo(LPCSTR, DWORD, LPSTR, DWORD, LPSTR *, LPDWORD);
1832 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD, LPSTR, LPSTR, void *, struct AUTO_PROXY_SCRIPT_BUFFER *);
1834 static BOOL run_script( char *script, DWORD size, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1836 BOOL ret;
1837 char *result, *urlA;
1838 DWORD len_result;
1839 struct AUTO_PROXY_SCRIPT_BUFFER buffer;
1840 URL_COMPONENTSW uc;
1842 buffer.dwStructSize = sizeof(buffer);
1843 buffer.lpszScriptBuffer = script;
1844 buffer.dwScriptBufferSize = size;
1846 if (!(urlA = strdupWA( url ))) return FALSE;
1847 if (!(ret = InternetInitializeAutoProxyDll( 0, NULL, NULL, NULL, &buffer )))
1849 heap_free( urlA );
1850 return FALSE;
1853 memset( &uc, 0, sizeof(uc) );
1854 uc.dwStructSize = sizeof(uc);
1855 uc.dwHostNameLength = -1;
1857 if (WinHttpCrackUrl( url, 0, 0, &uc ))
1859 char *hostnameA = strdupWA_sized( uc.lpszHostName, uc.dwHostNameLength );
1861 if ((ret = InternetGetProxyInfo( urlA, strlen(urlA),
1862 hostnameA, strlen(hostnameA), &result, &len_result )))
1864 ret = parse_script_result( result, info );
1865 heap_free( result );
1868 heap_free( hostnameA );
1870 heap_free( urlA );
1871 return InternetDeInitializeAutoProxyDll( NULL, 0 );
1874 /***********************************************************************
1875 * WinHttpGetProxyForUrl (winhttp.@)
1877 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1878 WINHTTP_PROXY_INFO *info )
1880 WCHAR *detected_pac_url = NULL;
1881 const WCHAR *pac_url;
1882 struct session *session;
1883 char *script;
1884 DWORD size;
1885 BOOL ret = FALSE;
1887 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1889 if (!(session = (struct session *)grab_object( hsession )))
1891 SetLastError( ERROR_INVALID_HANDLE );
1892 return FALSE;
1894 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1896 release_object( &session->hdr );
1897 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1898 return FALSE;
1900 if (!url || !options || !info ||
1901 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1902 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1903 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1904 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1906 release_object( &session->hdr );
1907 SetLastError( ERROR_INVALID_PARAMETER );
1908 return FALSE;
1910 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1911 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1912 goto done;
1914 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1915 else pac_url = detected_pac_url;
1917 if ((script = download_script( pac_url, &size )))
1919 ret = run_script( script, size, url, info );
1920 heap_free( script );
1923 done:
1924 GlobalFree( detected_pac_url );
1925 release_object( &session->hdr );
1926 if (ret) SetLastError( ERROR_SUCCESS );
1927 return ret;
1930 /***********************************************************************
1931 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1933 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1935 LONG l;
1936 HKEY key;
1937 BOOL ret = FALSE;
1938 const WCHAR *src;
1940 TRACE("%p\n", info);
1942 if (!info)
1944 SetLastError( ERROR_INVALID_PARAMETER );
1945 return FALSE;
1947 switch (info->dwAccessType)
1949 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1950 break;
1951 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1952 if (!info->lpszProxy)
1954 SetLastError( ERROR_INVALID_PARAMETER );
1955 return FALSE;
1957 /* Only ASCII characters are allowed */
1958 for (src = info->lpszProxy; *src; src++)
1959 if (*src > 0x7f)
1961 SetLastError( ERROR_INVALID_PARAMETER );
1962 return FALSE;
1964 if (info->lpszProxyBypass)
1966 for (src = info->lpszProxyBypass; *src; src++)
1967 if (*src > 0x7f)
1969 SetLastError( ERROR_INVALID_PARAMETER );
1970 return FALSE;
1973 break;
1974 default:
1975 SetLastError( ERROR_INVALID_PARAMETER );
1976 return FALSE;
1979 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, path_connections, 0, NULL, 0,
1980 KEY_WRITE, NULL, &key, NULL );
1981 if (!l)
1983 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1984 BYTE *buf;
1986 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1988 size += lstrlenW( info->lpszProxy );
1989 if (info->lpszProxyBypass)
1990 size += lstrlenW( info->lpszProxyBypass );
1992 buf = heap_alloc( size );
1993 if (buf)
1995 struct connection_settings_header *hdr =
1996 (struct connection_settings_header *)buf;
1997 DWORD *len = (DWORD *)(hdr + 1);
1999 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2000 hdr->unknown = 0;
2001 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2003 BYTE *dst;
2005 hdr->flags = PROXY_TYPE_PROXY;
2006 *len++ = lstrlenW( info->lpszProxy );
2007 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2008 src++, dst++)
2009 *dst = *src;
2010 len = (DWORD *)dst;
2011 if (info->lpszProxyBypass)
2013 *len++ = lstrlenW( info->lpszProxyBypass );
2014 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2015 src++, dst++)
2016 *dst = *src;
2018 else
2019 *len++ = 0;
2021 else
2023 hdr->flags = PROXY_TYPE_DIRECT;
2024 *len++ = 0;
2025 *len++ = 0;
2027 l = RegSetValueExW( key, L"WinHttpSettings", 0, REG_BINARY, buf, size );
2028 if (!l)
2029 ret = TRUE;
2030 heap_free( buf );
2032 RegCloseKey( key );
2034 if (ret) SetLastError( ERROR_SUCCESS );
2035 return ret;
2038 /***********************************************************************
2039 * WinHttpSetStatusCallback (winhttp.@)
2041 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2042 DWORD flags, DWORD_PTR reserved )
2044 struct object_header *hdr;
2045 WINHTTP_STATUS_CALLBACK ret;
2047 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2049 if (!(hdr = grab_object( handle )))
2051 SetLastError( ERROR_INVALID_HANDLE );
2052 return WINHTTP_INVALID_STATUS_CALLBACK;
2054 ret = hdr->callback;
2055 hdr->callback = callback;
2056 hdr->notify_mask = flags;
2058 release_object( hdr );
2059 SetLastError( ERROR_SUCCESS );
2060 return ret;
2063 /***********************************************************************
2064 * WinHttpSetTimeouts (winhttp.@)
2066 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2068 BOOL ret = TRUE;
2069 struct object_header *hdr;
2071 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2073 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2075 SetLastError( ERROR_INVALID_PARAMETER );
2076 return FALSE;
2079 if (!(hdr = grab_object( handle )))
2081 SetLastError( ERROR_INVALID_HANDLE );
2082 return FALSE;
2085 switch(hdr->type)
2087 case WINHTTP_HANDLE_TYPE_REQUEST:
2089 struct request *request = (struct request *)hdr;
2090 request->connect_timeout = connect;
2092 if (resolve < 0) resolve = 0;
2093 request->resolve_timeout = resolve;
2095 if (send < 0) send = 0;
2096 request->send_timeout = send;
2098 if (receive < 0) receive = 0;
2099 request->receive_timeout = receive;
2101 if (request->netconn)
2103 if (netconn_set_timeout( request->netconn, TRUE, send )) ret = FALSE;
2104 if (netconn_set_timeout( request->netconn, FALSE, receive )) ret = FALSE;
2106 break;
2108 case WINHTTP_HANDLE_TYPE_SESSION:
2110 struct session *session = (struct session *)hdr;
2111 session->connect_timeout = connect;
2113 if (resolve < 0) resolve = 0;
2114 session->resolve_timeout = resolve;
2116 if (send < 0) send = 0;
2117 session->send_timeout = send;
2119 if (receive < 0) receive = 0;
2120 session->receive_timeout = receive;
2121 break;
2123 default:
2124 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2125 ret = FALSE;
2127 release_object( hdr );
2128 if (ret) SetLastError( ERROR_SUCCESS );
2129 return ret;
2132 static const WCHAR wkday[7][4] =
2133 {L"Sun", L"Mon", L"Tue", L"Wed", L"Thu", L"Fri", L"Sat"};
2134 static const WCHAR month[12][4] =
2135 {L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun", L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec"};
2137 /***********************************************************************
2138 * WinHttpTimeFromSystemTime (WININET.@)
2140 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2142 TRACE("%p, %p\n", time, string);
2144 if (!time || !string)
2146 SetLastError( ERROR_INVALID_PARAMETER );
2147 return FALSE;
2150 swprintf( string, WINHTTP_TIME_FORMAT_BUFSIZE / sizeof(WCHAR),
2151 L"%s, %02d %s %4d %02d:%02d:%02d GMT",
2152 wkday[time->wDayOfWeek],
2153 time->wDay,
2154 month[time->wMonth - 1],
2155 time->wYear,
2156 time->wHour,
2157 time->wMinute,
2158 time->wSecond );
2160 SetLastError( ERROR_SUCCESS );
2161 return TRUE;
2164 /***********************************************************************
2165 * WinHttpTimeToSystemTime (WININET.@)
2167 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2169 unsigned int i;
2170 const WCHAR *s = string;
2171 WCHAR *end;
2173 TRACE("%s, %p\n", debugstr_w(string), time);
2175 if (!string || !time)
2177 SetLastError( ERROR_INVALID_PARAMETER );
2178 return FALSE;
2181 /* Windows does this too */
2182 GetSystemTime( time );
2184 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2185 * a SYSTEMTIME structure.
2188 SetLastError( ERROR_SUCCESS );
2190 while (*s && !iswalpha( *s )) s++;
2191 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2192 time->wDayOfWeek = 7;
2194 for (i = 0; i < 7; i++)
2196 if (towupper( wkday[i][0] ) == towupper( s[0] ) &&
2197 towupper( wkday[i][1] ) == towupper( s[1] ) &&
2198 towupper( wkday[i][2] ) == towupper( s[2] ) )
2200 time->wDayOfWeek = i;
2201 break;
2205 if (time->wDayOfWeek > 6) return TRUE;
2206 while (*s && !iswdigit( *s )) s++;
2207 time->wDay = wcstol( s, &end, 10 );
2208 s = end;
2210 while (*s && !iswalpha( *s )) s++;
2211 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2212 time->wMonth = 0;
2214 for (i = 0; i < 12; i++)
2216 if (towupper( month[i][0]) == towupper( s[0] ) &&
2217 towupper( month[i][1]) == towupper( s[1] ) &&
2218 towupper( month[i][2]) == towupper( s[2] ) )
2220 time->wMonth = i + 1;
2221 break;
2224 if (time->wMonth == 0) return TRUE;
2226 while (*s && !iswdigit( *s )) s++;
2227 if (*s == '\0') return TRUE;
2228 time->wYear = wcstol( s, &end, 10 );
2229 s = end;
2231 while (*s && !iswdigit( *s )) s++;
2232 if (*s == '\0') return TRUE;
2233 time->wHour = wcstol( s, &end, 10 );
2234 s = end;
2236 while (*s && !iswdigit( *s )) s++;
2237 if (*s == '\0') return TRUE;
2238 time->wMinute = wcstol( s, &end, 10 );
2239 s = end;
2241 while (*s && !iswdigit( *s )) s++;
2242 if (*s == '\0') return TRUE;
2243 time->wSecond = wcstol( s, &end, 10 );
2245 time->wMilliseconds = 0;
2246 return TRUE;