wined3d: Merge volumetexture_init() and texture_init().
[wine.git] / dlls / winhttp / session.c
blobb54e77bc4e90eeb85e25aaba8da0a42c37eacd58
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 if (session->unload_event) SetEvent( session->unload_event );
102 if (session->cred_handle_initialized) FreeCredentialsHandle( &session->cred_handle );
104 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
106 domain = LIST_ENTRY( item, domain_t, entry );
107 delete_domain( domain );
109 heap_free( session->agent );
110 heap_free( session->proxy_server );
111 heap_free( session->proxy_bypass );
112 heap_free( session->proxy_username );
113 heap_free( session->proxy_password );
114 heap_free( session );
117 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
119 session_t *session = (session_t *)hdr;
121 switch (option)
123 case WINHTTP_OPTION_REDIRECT_POLICY:
125 if (!buffer || *buflen < sizeof(DWORD))
127 *buflen = sizeof(DWORD);
128 set_last_error( ERROR_INSUFFICIENT_BUFFER );
129 return FALSE;
132 *(DWORD *)buffer = hdr->redirect_policy;
133 *buflen = sizeof(DWORD);
134 return TRUE;
136 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
137 *(DWORD *)buffer = session->resolve_timeout;
138 *buflen = sizeof(DWORD);
139 return TRUE;
140 case WINHTTP_OPTION_CONNECT_TIMEOUT:
141 *(DWORD *)buffer = session->connect_timeout;
142 *buflen = sizeof(DWORD);
143 return TRUE;
144 case WINHTTP_OPTION_SEND_TIMEOUT:
145 *(DWORD *)buffer = session->send_timeout;
146 *buflen = sizeof(DWORD);
147 return TRUE;
148 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
149 *(DWORD *)buffer = session->recv_timeout;
150 *buflen = sizeof(DWORD);
151 return TRUE;
152 default:
153 FIXME("unimplemented option %u\n", option);
154 set_last_error( ERROR_INVALID_PARAMETER );
155 return FALSE;
159 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
161 session_t *session = (session_t *)hdr;
163 switch (option)
165 case WINHTTP_OPTION_PROXY:
167 WINHTTP_PROXY_INFO *pi = buffer;
169 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
170 return TRUE;
172 case WINHTTP_OPTION_REDIRECT_POLICY:
174 DWORD policy;
176 if (buflen != sizeof(policy))
178 set_last_error( ERROR_INSUFFICIENT_BUFFER );
179 return FALSE;
182 policy = *(DWORD *)buffer;
183 TRACE("0x%x\n", policy);
184 hdr->redirect_policy = policy;
185 return TRUE;
187 case WINHTTP_OPTION_SECURE_PROTOCOLS:
189 if (buflen != sizeof(session->secure_protocols))
191 set_last_error( ERROR_INSUFFICIENT_BUFFER );
192 return FALSE;
194 session->secure_protocols = *(DWORD *)buffer;
195 TRACE("0x%x\n", session->secure_protocols);
196 return TRUE;
198 case WINHTTP_OPTION_DISABLE_FEATURE:
199 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
200 return FALSE;
201 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
202 session->resolve_timeout = *(DWORD *)buffer;
203 return TRUE;
204 case WINHTTP_OPTION_CONNECT_TIMEOUT:
205 session->connect_timeout = *(DWORD *)buffer;
206 return TRUE;
207 case WINHTTP_OPTION_SEND_TIMEOUT:
208 session->send_timeout = *(DWORD *)buffer;
209 return TRUE;
210 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
211 session->recv_timeout = *(DWORD *)buffer;
212 return TRUE;
213 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
214 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD *)buffer);
215 return TRUE;
216 case WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT:
217 TRACE("WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT: %p\n", *(HANDLE *)buffer);
218 session->unload_event = *(HANDLE *)buffer;
219 return TRUE;
220 case WINHTTP_OPTION_MAX_CONNS_PER_SERVER:
221 FIXME("WINHTTP_OPTION_MAX_CONNS_PER_SERVER: %d\n", *(DWORD *)buffer);
222 return TRUE;
223 case WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER:
224 FIXME("WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER: %d\n", *(DWORD *)buffer);
225 return TRUE;
226 default:
227 FIXME("unimplemented option %u\n", option);
228 set_last_error( ERROR_INVALID_PARAMETER );
229 return FALSE;
233 static const object_vtbl_t session_vtbl =
235 session_destroy,
236 session_query_option,
237 session_set_option
240 /***********************************************************************
241 * WinHttpOpen (winhttp.@)
243 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
245 session_t *session;
246 HINTERNET handle = NULL;
248 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
250 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
252 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
253 session->hdr.vtbl = &session_vtbl;
254 session->hdr.flags = flags;
255 session->hdr.refs = 1;
256 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
257 list_init( &session->hdr.children );
258 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
259 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
260 session->send_timeout = DEFAULT_SEND_TIMEOUT;
261 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
262 list_init( &session->cookie_cache );
264 if (agent && !(session->agent = strdupW( agent ))) goto end;
265 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
267 WINHTTP_PROXY_INFO info;
269 WinHttpGetDefaultProxyConfiguration( &info );
270 session->access = info.dwAccessType;
271 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
273 GlobalFree( info.lpszProxy );
274 GlobalFree( info.lpszProxyBypass );
275 goto end;
277 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
279 GlobalFree( info.lpszProxy );
280 GlobalFree( info.lpszProxyBypass );
281 goto end;
284 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
286 session->access = access;
287 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
288 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
291 if (!(handle = alloc_handle( &session->hdr ))) goto end;
292 session->hdr.handle = handle;
294 end:
295 release_object( &session->hdr );
296 TRACE("returning %p\n", handle);
297 if (handle) set_last_error( ERROR_SUCCESS );
298 return handle;
301 /***********************************************************************
302 * connect_destroy (internal)
304 static void connect_destroy( object_header_t *hdr )
306 connect_t *connect = (connect_t *)hdr;
308 TRACE("%p\n", connect);
310 release_object( &connect->session->hdr );
312 heap_free( connect->hostname );
313 heap_free( connect->servername );
314 heap_free( connect->username );
315 heap_free( connect->password );
316 heap_free( connect );
319 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
321 connect_t *connect = (connect_t *)hdr;
323 switch (option)
325 case WINHTTP_OPTION_PARENT_HANDLE:
327 if (!buffer || *buflen < sizeof(HINTERNET))
329 *buflen = sizeof(HINTERNET);
330 set_last_error( ERROR_INSUFFICIENT_BUFFER );
331 return FALSE;
334 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
335 *buflen = sizeof(HINTERNET);
336 return TRUE;
338 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
339 *(DWORD *)buffer = connect->session->resolve_timeout;
340 *buflen = sizeof(DWORD);
341 return TRUE;
342 case WINHTTP_OPTION_CONNECT_TIMEOUT:
343 *(DWORD *)buffer = connect->session->connect_timeout;
344 *buflen = sizeof(DWORD);
345 return TRUE;
346 case WINHTTP_OPTION_SEND_TIMEOUT:
347 *(DWORD *)buffer = connect->session->send_timeout;
348 *buflen = sizeof(DWORD);
349 return TRUE;
350 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
351 *(DWORD *)buffer = connect->session->recv_timeout;
352 *buflen = sizeof(DWORD);
353 return TRUE;
354 default:
355 FIXME("unimplemented option %u\n", option);
356 set_last_error( ERROR_INVALID_PARAMETER );
357 return FALSE;
361 static const object_vtbl_t connect_vtbl =
363 connect_destroy,
364 connect_query_option,
365 NULL
368 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
370 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
371 BOOL ret = FALSE;
373 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
374 ret = TRUE;
375 else if (*domain == '*')
377 if (domain[1] == '.')
379 LPCWSTR dot;
381 /* For a hostname to match a wildcard, the last domain must match
382 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
383 * hostname is www.foo.a.b, it matches, but a.b does not.
385 dot = strchrW( server, '.' );
386 if (dot)
388 int len = strlenW( dot + 1 );
390 if (len > strlenW( domain + 2 ))
392 LPCWSTR ptr;
394 /* The server's domain is longer than the wildcard, so it
395 * could be a subdomain. Compare the last portion of the
396 * server's domain.
398 ptr = dot + len + 1 - strlenW( domain + 2 );
399 if (!strcmpiW( ptr, domain + 2 ))
401 /* This is only a match if the preceding character is
402 * a '.', i.e. that it is a matching domain. E.g.
403 * if domain is '*.b.c' and server is 'www.ab.c' they
404 * do not match.
406 ret = *(ptr - 1) == '.';
409 else
410 ret = !strcmpiW( dot + 1, domain + 2 );
414 else
415 ret = !strcmpiW( server, domain );
416 return ret;
419 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
420 #define MAX_HOST_NAME_LENGTH 256
422 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
424 LPCWSTR ptr;
425 BOOL ret = FALSE;
427 if (!session->proxy_bypass) return FALSE;
428 ptr = session->proxy_bypass;
429 do {
430 LPCWSTR tmp = ptr;
432 ptr = strchrW( ptr, ';' );
433 if (!ptr)
434 ptr = strchrW( tmp, ' ' );
435 if (ptr)
437 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
439 WCHAR domain[MAX_HOST_NAME_LENGTH];
441 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
442 domain[ptr - tmp] = 0;
443 ret = domain_matches( server, domain );
445 ptr += 1;
447 else if (*tmp)
448 ret = domain_matches( server, tmp );
449 } while (ptr && !ret);
450 return ret;
453 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
455 session_t *session = connect->session;
456 BOOL ret = TRUE;
458 if (session->proxy_server && !should_bypass_proxy(session, server))
460 LPCWSTR colon;
462 if ((colon = strchrW( session->proxy_server, ':' )))
464 if (!connect->servername || strncmpiW( connect->servername,
465 session->proxy_server, colon - session->proxy_server - 1 ))
467 heap_free( connect->servername );
468 connect->resolved = FALSE;
469 if (!(connect->servername = heap_alloc(
470 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
472 ret = FALSE;
473 goto end;
475 memcpy( connect->servername, session->proxy_server,
476 (colon - session->proxy_server) * sizeof(WCHAR) );
477 connect->servername[colon - session->proxy_server] = 0;
478 if (*(colon + 1))
479 connect->serverport = atoiW( colon + 1 );
480 else
481 connect->serverport = INTERNET_DEFAULT_PORT;
484 else
486 if (!connect->servername || strcmpiW( connect->servername,
487 session->proxy_server ))
489 heap_free( connect->servername );
490 connect->resolved = FALSE;
491 if (!(connect->servername = strdupW( session->proxy_server )))
493 ret = FALSE;
494 goto end;
496 connect->serverport = INTERNET_DEFAULT_PORT;
500 else if (server)
502 heap_free( connect->servername );
503 connect->resolved = FALSE;
504 if (!(connect->servername = strdupW( server )))
506 ret = FALSE;
507 goto end;
509 connect->serverport = port;
511 end:
512 return ret;
515 /***********************************************************************
516 * WinHttpConnect (winhttp.@)
518 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
520 connect_t *connect;
521 session_t *session;
522 HINTERNET hconnect = NULL;
524 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
526 if (!server)
528 set_last_error( ERROR_INVALID_PARAMETER );
529 return NULL;
531 if (!(session = (session_t *)grab_object( hsession )))
533 set_last_error( ERROR_INVALID_HANDLE );
534 return NULL;
536 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
538 release_object( &session->hdr );
539 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
540 return NULL;
542 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
544 release_object( &session->hdr );
545 return NULL;
547 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
548 connect->hdr.vtbl = &connect_vtbl;
549 connect->hdr.refs = 1;
550 connect->hdr.flags = session->hdr.flags;
551 connect->hdr.callback = session->hdr.callback;
552 connect->hdr.notify_mask = session->hdr.notify_mask;
553 connect->hdr.context = session->hdr.context;
554 connect->hdr.redirect_policy = session->hdr.redirect_policy;
555 list_init( &connect->hdr.children );
557 addref_object( &session->hdr );
558 connect->session = session;
559 list_add_head( &session->hdr.children, &connect->hdr.entry );
561 if (!(connect->hostname = strdupW( server ))) goto end;
562 connect->hostport = port;
563 if (!set_server_for_hostname( connect, server, port )) goto end;
565 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
566 connect->hdr.handle = hconnect;
568 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
570 end:
571 release_object( &connect->hdr );
572 release_object( &session->hdr );
573 TRACE("returning %p\n", hconnect);
574 if (hconnect) set_last_error( ERROR_SUCCESS );
575 return hconnect;
578 /***********************************************************************
579 * request_destroy (internal)
581 static void request_destroy( object_header_t *hdr )
583 request_t *request = (request_t *)hdr;
584 unsigned int i, j;
586 TRACE("%p\n", request);
588 if (request->task_thread)
590 /* Signal to the task proc to quit. It will call
591 this again when it does. */
592 HANDLE thread = request->task_thread;
593 request->task_thread = 0;
594 SetEvent( request->task_cancel );
595 CloseHandle( thread );
596 return;
598 release_object( &request->connect->hdr );
600 destroy_authinfo( request->authinfo );
601 destroy_authinfo( request->proxy_authinfo );
603 heap_free( request->verb );
604 heap_free( request->path );
605 heap_free( request->version );
606 heap_free( request->raw_headers );
607 heap_free( request->status_text );
608 for (i = 0; i < request->num_headers; i++)
610 heap_free( request->headers[i].field );
611 heap_free( request->headers[i].value );
613 heap_free( request->headers );
614 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
615 heap_free( request->accept_types );
616 for (i = 0; i < TARGET_MAX; i++)
618 for (j = 0; j < SCHEME_MAX; j++)
620 heap_free( request->creds[i][j].username );
621 heap_free( request->creds[i][j].password );
624 heap_free( request );
627 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
629 int len = 0;
630 if (str) len = strlenW( str );
631 if (buffer && *buflen > len)
633 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
634 buffer[len] = 0;
636 *buflen = len * sizeof(WCHAR);
639 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
641 WCHAR *ret;
642 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
644 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
645 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
646 CertNameToStrW( encoding, blob, format, ret, size );
648 return ret;
651 static BOOL convert_sockaddr( const struct sockaddr *addr, SOCKADDR_STORAGE *addr_storage )
653 #ifndef __MINGW32__
654 switch (addr->sa_family)
656 case AF_INET:
658 const struct sockaddr_in *addr_unix = (const struct sockaddr_in *)addr;
659 struct WS_sockaddr_in *addr_win = (struct WS_sockaddr_in *)addr_storage;
660 char *p;
662 addr_win->sin_family = WS_AF_INET;
663 addr_win->sin_port = addr_unix->sin_port;
664 memcpy( &addr_win->sin_addr, &addr_unix->sin_addr, 4 );
665 p = (char *)&addr_win->sin_addr + 4;
666 memset( p, 0, sizeof(*addr_storage) - (p - (char *)addr_win) );
667 return TRUE;
669 case AF_INET6:
671 const struct sockaddr_in6 *addr_unix = (const struct sockaddr_in6 *)addr;
672 struct WS_sockaddr_in6 *addr_win = (struct WS_sockaddr_in6 *)addr_storage;
674 addr_win->sin6_family = WS_AF_INET6;
675 addr_win->sin6_port = addr_unix->sin6_port;
676 addr_win->sin6_flowinfo = addr_unix->sin6_flowinfo;
677 memcpy( &addr_win->sin6_addr, &addr_unix->sin6_addr, 16 );
678 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
679 addr_win->sin6_scope_id = addr_unix->sin6_scope_id;
680 #else
681 addr_win->sin6_scope_id = 0;
682 #endif
683 memset( addr_win + 1, 0, sizeof(*addr_storage) - sizeof(*addr_win) );
684 return TRUE;
686 default:
687 ERR("unhandled family %u\n", addr->sa_family);
688 return FALSE;
690 #else
691 switch (addr->sa_family)
693 case AF_INET:
695 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_storage;
697 memcpy( addr_in, addr, sizeof(*addr_in) );
698 memset( addr_in + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in) );
699 return TRUE;
701 case AF_INET6:
703 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr_storage;
705 memcpy( addr_in6, addr, sizeof(*addr_in6) );
706 memset( addr_in6 + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in6) );
707 return TRUE;
709 default:
710 ERR("unhandled family %u\n", addr->sa_family);
711 return FALSE;
713 #endif
716 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
718 request_t *request = (request_t *)hdr;
720 switch (option)
722 case WINHTTP_OPTION_SECURITY_FLAGS:
724 DWORD flags = 0;
725 int bits;
727 if (!buffer || *buflen < sizeof(flags))
729 *buflen = sizeof(flags);
730 set_last_error( ERROR_INSUFFICIENT_BUFFER );
731 return FALSE;
734 flags = 0;
735 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
736 flags |= request->security_flags;
737 if (request->netconn)
739 bits = netconn_get_cipher_strength( request->netconn );
740 if (bits >= 128)
741 flags |= SECURITY_FLAG_STRENGTH_STRONG;
742 else if (bits >= 56)
743 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
744 else
745 flags |= SECURITY_FLAG_STRENGTH_WEAK;
747 *(DWORD *)buffer = flags;
748 *buflen = sizeof(flags);
749 return TRUE;
751 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
753 const CERT_CONTEXT *cert;
755 if (!buffer || *buflen < sizeof(cert))
757 *buflen = sizeof(cert);
758 set_last_error( ERROR_INSUFFICIENT_BUFFER );
759 return FALSE;
762 if (!request->netconn || !(cert = netconn_get_certificate( request->netconn ))) return FALSE;
763 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
764 *buflen = sizeof(cert);
765 return TRUE;
767 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
769 const CERT_CONTEXT *cert;
770 const CRYPT_OID_INFO *oidInfo;
771 WINHTTP_CERTIFICATE_INFO *ci = buffer;
773 FIXME("partial stub\n");
775 if (!buffer || *buflen < sizeof(*ci))
777 *buflen = sizeof(*ci);
778 set_last_error( ERROR_INSUFFICIENT_BUFFER );
779 return FALSE;
781 if (!request->netconn || !(cert = netconn_get_certificate( request->netconn ))) return FALSE;
783 ci->ftExpiry = cert->pCertInfo->NotAfter;
784 ci->ftStart = cert->pCertInfo->NotBefore;
785 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
786 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
787 ci->lpszProtocolName = NULL;
788 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
789 cert->pCertInfo->SignatureAlgorithm.pszObjId,
790 0 );
791 if (oidInfo)
792 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
793 else
794 ci->lpszSignatureAlgName = NULL;
795 ci->lpszEncryptionAlgName = NULL;
796 ci->dwKeySize = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
798 CertFreeCertificateContext( cert );
799 *buflen = sizeof(*ci);
800 return TRUE;
802 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
804 if (!buffer || *buflen < sizeof(DWORD))
806 *buflen = sizeof(DWORD);
807 set_last_error( ERROR_INSUFFICIENT_BUFFER );
808 return FALSE;
811 *(DWORD *)buffer = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
812 *buflen = sizeof(DWORD);
813 return TRUE;
815 case WINHTTP_OPTION_CONNECTION_INFO:
817 WINHTTP_CONNECTION_INFO *info = buffer;
818 struct sockaddr local;
819 socklen_t len = sizeof(local);
820 const struct sockaddr *remote = (const struct sockaddr *)&request->connect->sockaddr;
822 if (!buffer || *buflen < sizeof(*info))
824 *buflen = sizeof(*info);
825 set_last_error( ERROR_INSUFFICIENT_BUFFER );
826 return FALSE;
828 if (!request->netconn)
830 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
831 return FALSE;
833 if (getsockname( request->netconn->socket, &local, &len )) return FALSE;
834 if (!convert_sockaddr( &local, &info->LocalAddress )) return FALSE;
835 if (!convert_sockaddr( remote, &info->RemoteAddress )) return FALSE;
836 info->cbSize = sizeof(*info);
837 return TRUE;
839 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
840 *(DWORD *)buffer = request->resolve_timeout;
841 *buflen = sizeof(DWORD);
842 return TRUE;
843 case WINHTTP_OPTION_CONNECT_TIMEOUT:
844 *(DWORD *)buffer = request->connect_timeout;
845 *buflen = sizeof(DWORD);
846 return TRUE;
847 case WINHTTP_OPTION_SEND_TIMEOUT:
848 *(DWORD *)buffer = request->send_timeout;
849 *buflen = sizeof(DWORD);
850 return TRUE;
851 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
852 *(DWORD *)buffer = request->recv_timeout;
853 *buflen = sizeof(DWORD);
854 return TRUE;
856 case WINHTTP_OPTION_USERNAME:
857 str_to_buffer( buffer, request->connect->username, buflen );
858 return TRUE;
860 case WINHTTP_OPTION_PASSWORD:
861 str_to_buffer( buffer, request->connect->password, buflen );
862 return TRUE;
864 case WINHTTP_OPTION_PROXY_USERNAME:
865 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
866 return TRUE;
868 case WINHTTP_OPTION_PROXY_PASSWORD:
869 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
870 return TRUE;
872 default:
873 FIXME("unimplemented option %u\n", option);
874 set_last_error( ERROR_INVALID_PARAMETER );
875 return FALSE;
879 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
881 WCHAR *ret;
882 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
884 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
885 ret[buflen] = 0;
886 return ret;
888 set_last_error( ERROR_OUTOFMEMORY );
889 return NULL;
892 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
894 request_t *request = (request_t *)hdr;
896 switch (option)
898 case WINHTTP_OPTION_PROXY:
900 WINHTTP_PROXY_INFO *pi = buffer;
902 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
903 return TRUE;
905 case WINHTTP_OPTION_DISABLE_FEATURE:
907 DWORD disable;
909 if (buflen != sizeof(DWORD))
911 set_last_error( ERROR_INSUFFICIENT_BUFFER );
912 return FALSE;
915 disable = *(DWORD *)buffer;
916 TRACE("0x%x\n", disable);
917 hdr->disable_flags |= disable;
918 return TRUE;
920 case WINHTTP_OPTION_AUTOLOGON_POLICY:
922 DWORD policy;
924 if (buflen != sizeof(DWORD))
926 set_last_error( ERROR_INSUFFICIENT_BUFFER );
927 return FALSE;
930 policy = *(DWORD *)buffer;
931 TRACE("0x%x\n", policy);
932 hdr->logon_policy = policy;
933 return TRUE;
935 case WINHTTP_OPTION_REDIRECT_POLICY:
937 DWORD policy;
939 if (buflen != sizeof(DWORD))
941 set_last_error( ERROR_INSUFFICIENT_BUFFER );
942 return FALSE;
945 policy = *(DWORD *)buffer;
946 TRACE("0x%x\n", policy);
947 hdr->redirect_policy = policy;
948 return TRUE;
950 case WINHTTP_OPTION_SECURITY_FLAGS:
952 DWORD flags;
954 if (buflen < sizeof(DWORD))
956 set_last_error( ERROR_INSUFFICIENT_BUFFER );
957 return FALSE;
959 flags = *(DWORD *)buffer;
960 TRACE("0x%x\n", flags);
961 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
962 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
963 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
964 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
966 set_last_error( ERROR_INVALID_PARAMETER );
967 return FALSE;
969 request->security_flags = flags;
970 return TRUE;
972 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
973 request->resolve_timeout = *(DWORD *)buffer;
974 return TRUE;
975 case WINHTTP_OPTION_CONNECT_TIMEOUT:
976 request->connect_timeout = *(DWORD *)buffer;
977 return TRUE;
978 case WINHTTP_OPTION_SEND_TIMEOUT:
979 request->send_timeout = *(DWORD *)buffer;
980 return TRUE;
981 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
982 request->recv_timeout = *(DWORD *)buffer;
983 return TRUE;
985 case WINHTTP_OPTION_USERNAME:
987 connect_t *connect = request->connect;
989 heap_free( connect->username );
990 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
991 return TRUE;
993 case WINHTTP_OPTION_PASSWORD:
995 connect_t *connect = request->connect;
997 heap_free( connect->password );
998 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
999 return TRUE;
1001 case WINHTTP_OPTION_PROXY_USERNAME:
1003 session_t *session = request->connect->session;
1005 heap_free( session->proxy_username );
1006 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
1007 return TRUE;
1009 case WINHTTP_OPTION_PROXY_PASSWORD:
1011 session_t *session = request->connect->session;
1013 heap_free( session->proxy_password );
1014 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
1015 return TRUE;
1017 case WINHTTP_OPTION_CLIENT_CERT_CONTEXT:
1018 if (!(hdr->flags & WINHTTP_FLAG_SECURE))
1020 SetLastError( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
1021 return FALSE;
1023 FIXME("WINHTTP_OPTION_CLIENT_CERT_CONTEXT\n");
1024 return TRUE;
1025 default:
1026 FIXME("unimplemented option %u\n", option);
1027 set_last_error( ERROR_INVALID_PARAMETER );
1028 return TRUE;
1032 static const object_vtbl_t request_vtbl =
1034 request_destroy,
1035 request_query_option,
1036 request_set_option
1039 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
1041 const WCHAR **types = accept_types;
1042 DWORD i;
1044 if (!types) return TRUE;
1045 while (*types)
1047 request->num_accept_types++;
1048 types++;
1050 if (!request->num_accept_types) return TRUE;
1051 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
1053 request->num_accept_types = 0;
1054 return FALSE;
1056 types = accept_types;
1057 for (i = 0; i < request->num_accept_types; i++)
1059 if (!(request->accept_types[i] = strdupW( *types )))
1061 for ( ; i > 0; --i) heap_free( request->accept_types[i - 1] );
1062 heap_free( request->accept_types );
1063 request->accept_types = NULL;
1064 request->num_accept_types = 0;
1065 return FALSE;
1067 types++;
1069 return TRUE;
1072 /***********************************************************************
1073 * WinHttpOpenRequest (winhttp.@)
1075 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
1076 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
1078 request_t *request;
1079 connect_t *connect;
1080 HINTERNET hrequest = NULL;
1082 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
1083 debugstr_w(version), debugstr_w(referrer), types, flags);
1085 if(types && TRACE_ON(winhttp)) {
1086 const WCHAR **iter;
1088 TRACE("accept types:\n");
1089 for(iter = types; *iter; iter++)
1090 TRACE(" %s\n", debugstr_w(*iter));
1093 if (!(connect = (connect_t *)grab_object( hconnect )))
1095 set_last_error( ERROR_INVALID_HANDLE );
1096 return NULL;
1098 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
1100 release_object( &connect->hdr );
1101 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1102 return NULL;
1104 if (!(request = heap_alloc_zero( sizeof(request_t) )))
1106 release_object( &connect->hdr );
1107 return NULL;
1109 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
1110 request->hdr.vtbl = &request_vtbl;
1111 request->hdr.refs = 1;
1112 request->hdr.flags = flags;
1113 request->hdr.callback = connect->hdr.callback;
1114 request->hdr.notify_mask = connect->hdr.notify_mask;
1115 request->hdr.context = connect->hdr.context;
1116 request->hdr.redirect_policy = connect->hdr.redirect_policy;
1117 list_init( &request->hdr.children );
1118 list_init( &request->task_queue );
1120 addref_object( &connect->hdr );
1121 request->connect = connect;
1122 list_add_head( &connect->hdr.children, &request->hdr.entry );
1124 request->resolve_timeout = connect->session->resolve_timeout;
1125 request->connect_timeout = connect->session->connect_timeout;
1126 request->send_timeout = connect->session->send_timeout;
1127 request->recv_timeout = connect->session->recv_timeout;
1129 if (!verb || !verb[0]) verb = getW;
1130 if (!(request->verb = strdupW( verb ))) goto end;
1132 if (object)
1134 WCHAR *path, *p;
1135 unsigned int len;
1137 len = strlenW( object ) + 1;
1138 if (object[0] != '/') len++;
1139 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
1141 if (object[0] != '/') *p++ = '/';
1142 strcpyW( p, object );
1143 request->path = path;
1145 else if (!(request->path = strdupW( slashW ))) goto end;
1147 if (!version || !version[0]) version = http1_1;
1148 if (!(request->version = strdupW( version ))) goto end;
1149 if (!(store_accept_types( request, types ))) goto end;
1151 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
1152 request->hdr.handle = hrequest;
1154 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
1156 end:
1157 release_object( &request->hdr );
1158 release_object( &connect->hdr );
1159 TRACE("returning %p\n", hrequest);
1160 if (hrequest) set_last_error( ERROR_SUCCESS );
1161 return hrequest;
1164 /***********************************************************************
1165 * WinHttpCloseHandle (winhttp.@)
1167 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1169 object_header_t *hdr;
1171 TRACE("%p\n", handle);
1173 if (!(hdr = grab_object( handle )))
1175 set_last_error( ERROR_INVALID_HANDLE );
1176 return FALSE;
1178 release_object( hdr );
1179 free_handle( handle );
1180 set_last_error( ERROR_SUCCESS );
1181 return TRUE;
1184 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1186 BOOL ret = FALSE;
1188 if (!buflen)
1190 set_last_error( ERROR_INVALID_PARAMETER );
1191 return FALSE;
1194 switch (option)
1196 case WINHTTP_OPTION_CONTEXT_VALUE:
1198 if (!buffer || *buflen < sizeof(DWORD_PTR))
1200 *buflen = sizeof(DWORD_PTR);
1201 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1202 return FALSE;
1205 *(DWORD_PTR *)buffer = hdr->context;
1206 *buflen = sizeof(DWORD_PTR);
1207 return TRUE;
1209 default:
1210 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1211 else
1213 FIXME("unimplemented option %u\n", option);
1214 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1215 return FALSE;
1217 break;
1219 return ret;
1222 /***********************************************************************
1223 * WinHttpQueryOption (winhttp.@)
1225 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1227 BOOL ret = FALSE;
1228 object_header_t *hdr;
1230 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1232 if (!(hdr = grab_object( handle )))
1234 set_last_error( ERROR_INVALID_HANDLE );
1235 return FALSE;
1238 ret = query_option( hdr, option, buffer, buflen );
1240 release_object( hdr );
1241 if (ret) set_last_error( ERROR_SUCCESS );
1242 return ret;
1245 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1247 BOOL ret = TRUE;
1249 if (!buffer && buflen)
1251 set_last_error( ERROR_INVALID_PARAMETER );
1252 return FALSE;
1255 switch (option)
1257 case WINHTTP_OPTION_CONTEXT_VALUE:
1259 if (buflen != sizeof(DWORD_PTR))
1261 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1262 return FALSE;
1265 hdr->context = *(DWORD_PTR *)buffer;
1266 return TRUE;
1268 default:
1269 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1270 else
1272 FIXME("unimplemented option %u\n", option);
1273 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1274 return FALSE;
1276 break;
1278 return ret;
1281 /***********************************************************************
1282 * WinHttpSetOption (winhttp.@)
1284 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1286 BOOL ret = FALSE;
1287 object_header_t *hdr;
1289 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1291 if (!(hdr = grab_object( handle )))
1293 set_last_error( ERROR_INVALID_HANDLE );
1294 return FALSE;
1297 ret = set_option( hdr, option, buffer, buflen );
1299 release_object( hdr );
1300 if (ret) set_last_error( ERROR_SUCCESS );
1301 return ret;
1304 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1306 char *ret;
1307 DWORD size = 0;
1309 GetComputerNameExA( format, NULL, &size );
1310 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1311 if (!(ret = heap_alloc( size ))) return NULL;
1312 if (!GetComputerNameExA( format, ret, &size ))
1314 heap_free( ret );
1315 return NULL;
1317 return ret;
1320 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1322 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1324 if (len_suffix > len_domain) return FALSE;
1325 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1326 return FALSE;
1329 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1331 int ret = -1;
1332 #ifdef HAVE_GETNAMEINFO
1333 ret = getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1334 #endif
1335 return ret;
1338 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1340 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1341 static const WCHAR wpadW[] = {'/','w','p','a','d','.','d','a','t',0};
1342 char name[NI_MAXHOST];
1343 WCHAR *ret, *p;
1344 int len;
1346 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1347 if (!ai) return NULL;
1349 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1351 len = strlenW( httpW ) + strlen( hostname ) + strlenW( wpadW );
1352 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1353 strcpyW( p, httpW );
1354 p += strlenW( httpW );
1355 while (*hostname) { *p++ = *hostname++; }
1356 strcpyW( p, wpadW );
1357 return ret;
1360 static BOOL get_system_proxy_autoconfig_url( char *buf, DWORD buflen )
1362 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1363 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
1364 const void *ref;
1365 BOOL ret = FALSE;
1367 if (!settings) return FALSE;
1369 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
1371 CFRelease( settings );
1372 return FALSE;
1374 if (CFStringGetCString( ref, buf, buflen, kCFStringEncodingASCII ))
1376 TRACE( "returning %s\n", debugstr_a(buf) );
1377 ret = TRUE;
1379 CFRelease( settings );
1380 return ret;
1381 #else
1382 static BOOL first = TRUE;
1383 if (first)
1385 FIXME( "no support on this platform\n" );
1386 first = FALSE;
1388 else
1389 TRACE( "no support on this platform\n" );
1390 return FALSE;
1391 #endif
1394 #define INTERNET_MAX_URL_LENGTH 2084
1396 /***********************************************************************
1397 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1399 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1401 BOOL ret = FALSE;
1402 char system_url[INTERNET_MAX_URL_LENGTH + 1];
1404 TRACE("0x%08x, %p\n", flags, url);
1406 if (!flags || !url)
1408 set_last_error( ERROR_INVALID_PARAMETER );
1409 return FALSE;
1411 if (get_system_proxy_autoconfig_url( system_url, sizeof(system_url) ))
1413 WCHAR *urlW;
1415 if (!(urlW = strdupAW( system_url ))) return FALSE;
1416 *url = urlW;
1417 set_last_error( ERROR_SUCCESS );
1418 return TRUE;
1420 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1422 static int fixme_shown;
1423 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1425 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1427 #ifdef HAVE_GETADDRINFO
1428 char *fqdn, *domain, *p;
1430 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1431 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1433 heap_free( fqdn );
1434 return FALSE;
1436 p = fqdn;
1437 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1439 struct addrinfo *ai;
1440 char *name;
1441 int res;
1443 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1445 heap_free( fqdn );
1446 heap_free( domain );
1447 return FALSE;
1449 strcpy( name, "wpad" );
1450 strcat( name, p );
1451 res = getaddrinfo( name, NULL, NULL, &ai );
1452 if (!res)
1454 *url = build_wpad_url( name, ai );
1455 freeaddrinfo( ai );
1456 if (*url)
1458 TRACE("returning %s\n", debugstr_w(*url));
1459 heap_free( name );
1460 ret = TRUE;
1461 break;
1464 heap_free( name );
1465 p++;
1467 heap_free( domain );
1468 heap_free( fqdn );
1469 #else
1470 FIXME("getaddrinfo not found at build time\n");
1471 #endif
1473 if (!ret)
1475 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1476 *url = NULL;
1478 else set_last_error( ERROR_SUCCESS );
1479 return ret;
1482 static const WCHAR Connections[] = {
1483 'S','o','f','t','w','a','r','e','\\',
1484 'M','i','c','r','o','s','o','f','t','\\',
1485 'W','i','n','d','o','w','s','\\',
1486 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1487 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1488 'C','o','n','n','e','c','t','i','o','n','s',0 };
1489 static const WCHAR WinHttpSettings[] = {
1490 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1491 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1492 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1493 static const DWORD PROXY_TYPE_DIRECT = 1;
1494 static const DWORD PROXY_TYPE_PROXY = 2;
1495 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1496 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1498 struct connection_settings_header
1500 DWORD magic;
1501 DWORD unknown; /* always zero? */
1502 DWORD flags; /* one or more of PROXY_* */
1505 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1507 const BYTE *begin;
1509 for (begin = src; src - begin < len; src++, dst++)
1510 *dst = *src;
1511 *dst = 0;
1514 /***********************************************************************
1515 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1517 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1519 LONG l;
1520 HKEY key;
1521 BOOL got_from_reg = FALSE, direct = TRUE;
1522 char *envproxy;
1524 TRACE("%p\n", info);
1526 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1527 if (!l)
1529 DWORD type, size = 0;
1531 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1532 if (!l && type == REG_BINARY &&
1533 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1535 BYTE *buf = heap_alloc( size );
1537 if (buf)
1539 struct connection_settings_header *hdr =
1540 (struct connection_settings_header *)buf;
1541 DWORD *len = (DWORD *)(hdr + 1);
1543 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1544 &size );
1545 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1546 hdr->unknown == 0)
1548 if (hdr->flags & PROXY_TYPE_PROXY)
1550 BOOL sane = FALSE;
1551 LPWSTR proxy = NULL;
1552 LPWSTR proxy_bypass = NULL;
1554 /* Sanity-check length of proxy string */
1555 if ((BYTE *)len - buf + *len <= size)
1557 sane = TRUE;
1558 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1559 if (proxy)
1560 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1561 len = (DWORD *)((BYTE *)(len + 1) + *len);
1563 if (sane)
1565 /* Sanity-check length of proxy bypass string */
1566 if ((BYTE *)len - buf + *len <= size)
1568 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1569 if (proxy_bypass)
1570 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1572 else
1574 sane = FALSE;
1575 GlobalFree( proxy );
1576 proxy = NULL;
1579 info->lpszProxy = proxy;
1580 info->lpszProxyBypass = proxy_bypass;
1581 if (sane)
1583 got_from_reg = TRUE;
1584 direct = FALSE;
1585 info->dwAccessType =
1586 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1587 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1588 debugstr_w(info->lpszProxy),
1589 debugstr_w(info->lpszProxyBypass));
1593 heap_free( buf );
1596 RegCloseKey( key );
1598 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1600 char *colon, *http_proxy;
1602 if ((colon = strchr( envproxy, ':' )))
1604 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1606 static const char http[] = "http://";
1608 /* It's a scheme, check that it's http */
1609 if (!strncmp( envproxy, http, strlen( http ) ))
1610 http_proxy = envproxy + strlen( http );
1611 else
1613 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1614 http_proxy = NULL;
1617 else
1618 http_proxy = envproxy;
1620 else
1621 http_proxy = envproxy;
1622 if (http_proxy)
1624 WCHAR *http_proxyW;
1625 int len;
1627 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1628 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1630 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1631 direct = FALSE;
1632 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1633 info->lpszProxy = http_proxyW;
1634 info->lpszProxyBypass = NULL;
1635 TRACE("http proxy (from environment) = %s\n",
1636 debugstr_w(info->lpszProxy));
1640 if (direct)
1642 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1643 info->lpszProxy = NULL;
1644 info->lpszProxyBypass = NULL;
1646 set_last_error( ERROR_SUCCESS );
1647 return TRUE;
1650 /***********************************************************************
1651 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1653 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1655 static const WCHAR settingsW[] =
1656 {'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};
1657 HKEY hkey = NULL;
1658 struct connection_settings_header *hdr = NULL;
1659 DWORD type, offset, len, size = 0;
1660 BOOL ret = FALSE;
1662 TRACE("%p\n", config);
1664 if (!config)
1666 set_last_error( ERROR_INVALID_PARAMETER );
1667 return FALSE;
1669 memset( config, 0, sizeof(*config) );
1670 config->fAutoDetect = TRUE;
1672 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1673 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1674 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1676 ret = TRUE;
1677 goto done;
1679 if (!(hdr = heap_alloc( size ))) goto done;
1680 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1681 hdr->magic != WININET_SETTINGS_MAGIC)
1683 ret = TRUE;
1684 goto done;
1687 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1688 offset = sizeof(*hdr);
1689 if (offset + sizeof(DWORD) > size) goto done;
1690 len = *(DWORD *)((char *)hdr + offset);
1691 offset += sizeof(DWORD);
1692 if (len && hdr->flags & PROXY_TYPE_PROXY)
1694 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1695 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1697 offset += len;
1698 if (offset + sizeof(DWORD) > size) goto done;
1699 len = *(DWORD *)((char *)hdr + offset);
1700 offset += sizeof(DWORD);
1701 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1703 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1704 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1706 offset += len;
1707 if (offset + sizeof(DWORD) > size) goto done;
1708 len = *(DWORD *)((char *)hdr + offset);
1709 offset += sizeof(DWORD);
1710 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1712 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1713 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1715 ret = TRUE;
1717 done:
1718 RegCloseKey( hkey );
1719 heap_free( hdr );
1720 if (!ret)
1722 GlobalFree( config->lpszAutoConfigUrl );
1723 config->lpszAutoConfigUrl = NULL;
1724 GlobalFree( config->lpszProxy );
1725 config->lpszProxy = NULL;
1726 GlobalFree( config->lpszProxyBypass );
1727 config->lpszProxyBypass = NULL;
1729 else set_last_error( ERROR_SUCCESS );
1730 return ret;
1733 static BOOL parse_script_result( const char *result, WINHTTP_PROXY_INFO *info )
1735 const char *p;
1736 WCHAR *q;
1737 int len;
1739 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1740 info->lpszProxy = NULL;
1741 info->lpszProxyBypass = NULL;
1743 TRACE("%s\n", debugstr_a( result ));
1745 p = result;
1746 while (*p == ' ') p++;
1747 len = strlen( p );
1748 if (len >= 5 && !strncasecmp( p, "PROXY", sizeof("PROXY") - 1 ))
1750 p += 5;
1751 while (*p == ' ') p++;
1752 if (!*p || *p == ';') return TRUE;
1753 if (!(info->lpszProxy = q = strdupAW( p ))) return FALSE;
1754 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1755 for (; *q; q++)
1757 if (*q == ' ' || *q == ';')
1759 *q = 0;
1760 break;
1764 return TRUE;
1767 static char *download_script( const WCHAR *url, DWORD *out_size )
1769 static const WCHAR typeW[] = {'*','/','*',0};
1770 static const WCHAR *acceptW[] = {typeW, NULL};
1771 HINTERNET ses, con = NULL, req = NULL;
1772 WCHAR *hostname;
1773 URL_COMPONENTSW uc;
1774 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1775 char *tmp, *buffer = NULL;
1777 *out_size = 0;
1779 memset( &uc, 0, sizeof(uc) );
1780 uc.dwStructSize = sizeof(uc);
1781 uc.dwHostNameLength = -1;
1782 uc.dwUrlPathLength = -1;
1783 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1784 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1785 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1786 hostname[uc.dwHostNameLength] = 0;
1788 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1789 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1790 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1791 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1792 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1794 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1795 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1796 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1798 size = 4096;
1799 if (!(buffer = heap_alloc( size ))) goto done;
1800 to_read = size;
1801 offset = 0;
1802 for (;;)
1804 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1805 if (!bytes_read) break;
1806 to_read -= bytes_read;
1807 offset += bytes_read;
1808 *out_size += bytes_read;
1809 if (!to_read)
1811 to_read = size;
1812 size *= 2;
1813 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1814 buffer = tmp;
1818 done:
1819 WinHttpCloseHandle( req );
1820 WinHttpCloseHandle( con );
1821 WinHttpCloseHandle( ses );
1822 heap_free( hostname );
1823 if (!buffer) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1824 return buffer;
1827 struct AUTO_PROXY_SCRIPT_BUFFER
1829 DWORD dwStructSize;
1830 LPSTR lpszScriptBuffer;
1831 DWORD dwScriptBufferSize;
1834 BOOL WINAPI InternetDeInitializeAutoProxyDll(LPSTR, DWORD);
1835 BOOL WINAPI InternetGetProxyInfo(LPCSTR, DWORD, LPSTR, DWORD, LPSTR *, LPDWORD);
1836 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD, LPSTR, LPSTR, void *, struct AUTO_PROXY_SCRIPT_BUFFER *);
1838 static BOOL run_script( char *script, DWORD size, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1840 BOOL ret;
1841 char *result, *urlA;
1842 DWORD len_result;
1843 struct AUTO_PROXY_SCRIPT_BUFFER buffer;
1844 URL_COMPONENTSW uc;
1846 buffer.dwStructSize = sizeof(buffer);
1847 buffer.lpszScriptBuffer = script;
1848 buffer.dwScriptBufferSize = size;
1850 if (!(urlA = strdupWA( url ))) return FALSE;
1851 if (!(ret = InternetInitializeAutoProxyDll( 0, NULL, NULL, NULL, &buffer )))
1853 heap_free( urlA );
1854 return FALSE;
1857 memset( &uc, 0, sizeof(uc) );
1858 uc.dwStructSize = sizeof(uc);
1859 uc.dwHostNameLength = -1;
1861 if (WinHttpCrackUrl( url, 0, 0, &uc ))
1863 char *hostnameA = strdupWA_sized( uc.lpszHostName, uc.dwHostNameLength );
1865 if ((ret = InternetGetProxyInfo( urlA, strlen(urlA),
1866 hostnameA, strlen(hostnameA), &result, &len_result )))
1868 ret = parse_script_result( result, info );
1869 heap_free( result );
1872 heap_free( hostnameA );
1874 heap_free( urlA );
1875 return InternetDeInitializeAutoProxyDll( NULL, 0 );
1878 /***********************************************************************
1879 * WinHttpGetProxyForUrl (winhttp.@)
1881 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1882 WINHTTP_PROXY_INFO *info )
1884 WCHAR *detected_pac_url = NULL;
1885 const WCHAR *pac_url;
1886 session_t *session;
1887 char *script;
1888 DWORD size;
1889 BOOL ret = FALSE;
1891 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1893 if (!(session = (session_t *)grab_object( hsession )))
1895 set_last_error( ERROR_INVALID_HANDLE );
1896 return FALSE;
1898 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1900 release_object( &session->hdr );
1901 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1902 return FALSE;
1904 if (!url || !options || !info ||
1905 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1906 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1907 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1908 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1910 release_object( &session->hdr );
1911 set_last_error( ERROR_INVALID_PARAMETER );
1912 return FALSE;
1914 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1915 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1916 goto done;
1918 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1919 else pac_url = detected_pac_url;
1921 if ((script = download_script( pac_url, &size )))
1923 ret = run_script( script, size, url, info );
1924 heap_free( script );
1927 done:
1928 GlobalFree( detected_pac_url );
1929 release_object( &session->hdr );
1930 if (ret) set_last_error( ERROR_SUCCESS );
1931 return ret;
1934 /***********************************************************************
1935 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1937 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1939 LONG l;
1940 HKEY key;
1941 BOOL ret = FALSE;
1942 const WCHAR *src;
1944 TRACE("%p\n", info);
1946 if (!info)
1948 set_last_error( ERROR_INVALID_PARAMETER );
1949 return FALSE;
1951 switch (info->dwAccessType)
1953 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1954 break;
1955 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1956 if (!info->lpszProxy)
1958 set_last_error( ERROR_INVALID_PARAMETER );
1959 return FALSE;
1961 /* Only ASCII characters are allowed */
1962 for (src = info->lpszProxy; *src; src++)
1963 if (*src > 0x7f)
1965 set_last_error( ERROR_INVALID_PARAMETER );
1966 return FALSE;
1968 if (info->lpszProxyBypass)
1970 for (src = info->lpszProxyBypass; *src; src++)
1971 if (*src > 0x7f)
1973 set_last_error( ERROR_INVALID_PARAMETER );
1974 return FALSE;
1977 break;
1978 default:
1979 set_last_error( ERROR_INVALID_PARAMETER );
1980 return FALSE;
1983 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1984 KEY_WRITE, NULL, &key, NULL );
1985 if (!l)
1987 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1988 BYTE *buf;
1990 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1992 size += strlenW( info->lpszProxy );
1993 if (info->lpszProxyBypass)
1994 size += strlenW( info->lpszProxyBypass );
1996 buf = heap_alloc( size );
1997 if (buf)
1999 struct connection_settings_header *hdr =
2000 (struct connection_settings_header *)buf;
2001 DWORD *len = (DWORD *)(hdr + 1);
2003 hdr->magic = WINHTTP_SETTINGS_MAGIC;
2004 hdr->unknown = 0;
2005 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
2007 BYTE *dst;
2009 hdr->flags = PROXY_TYPE_PROXY;
2010 *len++ = strlenW( info->lpszProxy );
2011 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
2012 src++, dst++)
2013 *dst = *src;
2014 len = (DWORD *)dst;
2015 if (info->lpszProxyBypass)
2017 *len++ = strlenW( info->lpszProxyBypass );
2018 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
2019 src++, dst++)
2020 *dst = *src;
2022 else
2023 *len++ = 0;
2025 else
2027 hdr->flags = PROXY_TYPE_DIRECT;
2028 *len++ = 0;
2029 *len++ = 0;
2031 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
2032 if (!l)
2033 ret = TRUE;
2034 heap_free( buf );
2036 RegCloseKey( key );
2038 if (ret) set_last_error( ERROR_SUCCESS );
2039 return ret;
2042 /***********************************************************************
2043 * WinHttpSetStatusCallback (winhttp.@)
2045 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
2046 DWORD flags, DWORD_PTR reserved )
2048 object_header_t *hdr;
2049 WINHTTP_STATUS_CALLBACK ret;
2051 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
2053 if (!(hdr = grab_object( handle )))
2055 set_last_error( ERROR_INVALID_HANDLE );
2056 return WINHTTP_INVALID_STATUS_CALLBACK;
2058 ret = hdr->callback;
2059 hdr->callback = callback;
2060 hdr->notify_mask = flags;
2062 release_object( hdr );
2063 set_last_error( ERROR_SUCCESS );
2064 return ret;
2067 /***********************************************************************
2068 * WinHttpSetTimeouts (winhttp.@)
2070 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
2072 BOOL ret = TRUE;
2073 object_header_t *hdr;
2074 request_t *request;
2075 session_t *session;
2077 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
2079 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
2081 set_last_error( ERROR_INVALID_PARAMETER );
2082 return FALSE;
2085 if (!(hdr = grab_object( handle )))
2087 set_last_error( ERROR_INVALID_HANDLE );
2088 return FALSE;
2091 switch(hdr->type)
2093 case WINHTTP_HANDLE_TYPE_REQUEST:
2094 request = (request_t *)hdr;
2095 request->connect_timeout = connect;
2097 if (resolve < 0) resolve = 0;
2098 request->resolve_timeout = resolve;
2100 if (send < 0) send = 0;
2101 request->send_timeout = send;
2103 if (receive < 0) receive = 0;
2104 request->recv_timeout = receive;
2106 if (request->netconn)
2108 if (netconn_set_timeout( request->netconn, TRUE, send )) ret = FALSE;
2109 if (netconn_set_timeout( request->netconn, FALSE, receive )) ret = FALSE;
2111 break;
2113 case WINHTTP_HANDLE_TYPE_SESSION:
2114 session = (session_t *)hdr;
2115 session->connect_timeout = connect;
2117 if (resolve < 0) resolve = 0;
2118 session->resolve_timeout = resolve;
2120 if (send < 0) send = 0;
2121 session->send_timeout = send;
2123 if (receive < 0) receive = 0;
2124 session->recv_timeout = receive;
2125 break;
2127 default:
2128 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
2129 ret = FALSE;
2131 release_object( hdr );
2132 if (ret) set_last_error( ERROR_SUCCESS );
2133 return ret;
2136 static const WCHAR wkday[7][4] =
2137 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2138 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2139 static const WCHAR month[12][4] =
2140 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2141 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2142 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2144 /***********************************************************************
2145 * WinHttpTimeFromSystemTime (WININET.@)
2147 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2149 static const WCHAR format[] =
2150 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2151 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2153 TRACE("%p, %p\n", time, string);
2155 if (!time || !string)
2157 set_last_error( ERROR_INVALID_PARAMETER );
2158 return FALSE;
2161 sprintfW( string, format,
2162 wkday[time->wDayOfWeek],
2163 time->wDay,
2164 month[time->wMonth - 1],
2165 time->wYear,
2166 time->wHour,
2167 time->wMinute,
2168 time->wSecond );
2170 set_last_error( ERROR_SUCCESS );
2171 return TRUE;
2174 /***********************************************************************
2175 * WinHttpTimeToSystemTime (WININET.@)
2177 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2179 unsigned int i;
2180 const WCHAR *s = string;
2181 WCHAR *end;
2183 TRACE("%s, %p\n", debugstr_w(string), time);
2185 if (!string || !time)
2187 set_last_error( ERROR_INVALID_PARAMETER );
2188 return FALSE;
2191 /* Windows does this too */
2192 GetSystemTime( time );
2194 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2195 * a SYSTEMTIME structure.
2198 set_last_error( ERROR_SUCCESS );
2200 while (*s && !isalphaW( *s )) s++;
2201 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2202 time->wDayOfWeek = 7;
2204 for (i = 0; i < 7; i++)
2206 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2207 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2208 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2210 time->wDayOfWeek = i;
2211 break;
2215 if (time->wDayOfWeek > 6) return TRUE;
2216 while (*s && !isdigitW( *s )) s++;
2217 time->wDay = strtolW( s, &end, 10 );
2218 s = end;
2220 while (*s && !isalphaW( *s )) s++;
2221 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2222 time->wMonth = 0;
2224 for (i = 0; i < 12; i++)
2226 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2227 toupperW( month[i][1]) == toupperW( s[1] ) &&
2228 toupperW( month[i][2]) == toupperW( s[2] ) )
2230 time->wMonth = i + 1;
2231 break;
2234 if (time->wMonth == 0) return TRUE;
2236 while (*s && !isdigitW( *s )) s++;
2237 if (*s == '\0') return TRUE;
2238 time->wYear = strtolW( s, &end, 10 );
2239 s = end;
2241 while (*s && !isdigitW( *s )) s++;
2242 if (*s == '\0') return TRUE;
2243 time->wHour = strtolW( s, &end, 10 );
2244 s = end;
2246 while (*s && !isdigitW( *s )) s++;
2247 if (*s == '\0') return TRUE;
2248 time->wMinute = strtolW( s, &end, 10 );
2249 s = end;
2251 while (*s && !isdigitW( *s )) s++;
2252 if (*s == '\0') return TRUE;
2253 time->wSecond = strtolW( s, &end, 10 );
2255 time->wMilliseconds = 0;
2256 return TRUE;