winhttp: Reimplement WinHttpGetProxyForUrl on top of jsproxy.
[wine/wine-gecko.git] / dlls / winhttp / session.c
blobe773f050e1f05319bb35a03df84b8a515bcfae34
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #ifndef __MINGW32__
29 #define USE_WS_PREFIX
30 #endif
31 #include "winsock2.h"
32 #include "ws2ipdef.h"
33 #include "winhttp.h"
34 #include "wincrypt.h"
35 #include "winreg.h"
36 #define COBJMACROS
37 #include "ole2.h"
38 #include "dispex.h"
39 #include "activscp.h"
41 #include "winhttp_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
45 #define DEFAULT_RESOLVE_TIMEOUT 0
46 #define DEFAULT_CONNECT_TIMEOUT 20000
47 #define DEFAULT_SEND_TIMEOUT 30000
48 #define DEFAULT_RECEIVE_TIMEOUT 30000
50 void set_last_error( DWORD error )
52 /* FIXME */
53 SetLastError( error );
56 DWORD get_last_error( void )
58 /* FIXME */
59 return GetLastError();
62 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
64 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
66 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
69 /***********************************************************************
70 * WinHttpCheckPlatform (winhttp.@)
72 BOOL WINAPI WinHttpCheckPlatform( void )
74 TRACE("\n");
75 return TRUE;
78 /***********************************************************************
79 * session_destroy (internal)
81 static void session_destroy( object_header_t *hdr )
83 session_t *session = (session_t *)hdr;
84 struct list *item, *next;
85 domain_t *domain;
87 TRACE("%p\n", session);
89 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
91 domain = LIST_ENTRY( item, domain_t, entry );
92 delete_domain( domain );
94 heap_free( session->agent );
95 heap_free( session->proxy_server );
96 heap_free( session->proxy_bypass );
97 heap_free( session->proxy_username );
98 heap_free( session->proxy_password );
99 heap_free( session );
102 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
104 session_t *session = (session_t *)hdr;
106 switch (option)
108 case WINHTTP_OPTION_REDIRECT_POLICY:
110 if (!buffer || *buflen < sizeof(DWORD))
112 *buflen = sizeof(DWORD);
113 set_last_error( ERROR_INSUFFICIENT_BUFFER );
114 return FALSE;
117 *(DWORD *)buffer = hdr->redirect_policy;
118 *buflen = sizeof(DWORD);
119 return TRUE;
121 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
122 *(DWORD *)buffer = session->resolve_timeout;
123 *buflen = sizeof(DWORD);
124 return TRUE;
125 case WINHTTP_OPTION_CONNECT_TIMEOUT:
126 *(DWORD *)buffer = session->connect_timeout;
127 *buflen = sizeof(DWORD);
128 return TRUE;
129 case WINHTTP_OPTION_SEND_TIMEOUT:
130 *(DWORD *)buffer = session->send_timeout;
131 *buflen = sizeof(DWORD);
132 return TRUE;
133 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
134 *(DWORD *)buffer = session->recv_timeout;
135 *buflen = sizeof(DWORD);
136 return TRUE;
137 default:
138 FIXME("unimplemented option %u\n", option);
139 set_last_error( ERROR_INVALID_PARAMETER );
140 return FALSE;
144 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
146 session_t *session = (session_t *)hdr;
148 switch (option)
150 case WINHTTP_OPTION_PROXY:
152 WINHTTP_PROXY_INFO *pi = buffer;
154 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
155 return TRUE;
157 case WINHTTP_OPTION_REDIRECT_POLICY:
159 DWORD policy;
161 if (buflen != sizeof(policy))
163 set_last_error( ERROR_INSUFFICIENT_BUFFER );
164 return FALSE;
167 policy = *(DWORD *)buffer;
168 TRACE("0x%x\n", policy);
169 hdr->redirect_policy = policy;
170 return TRUE;
172 case WINHTTP_OPTION_DISABLE_FEATURE:
173 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
174 return FALSE;
175 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
176 session->resolve_timeout = *(DWORD *)buffer;
177 return TRUE;
178 case WINHTTP_OPTION_CONNECT_TIMEOUT:
179 session->connect_timeout = *(DWORD *)buffer;
180 return TRUE;
181 case WINHTTP_OPTION_SEND_TIMEOUT:
182 session->send_timeout = *(DWORD *)buffer;
183 return TRUE;
184 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
185 session->recv_timeout = *(DWORD *)buffer;
186 return TRUE;
187 case WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH:
188 FIXME("WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH: 0x%x\n", *(DWORD *)buffer);
189 return TRUE;
190 default:
191 FIXME("unimplemented option %u\n", option);
192 set_last_error( ERROR_INVALID_PARAMETER );
193 return FALSE;
197 static const object_vtbl_t session_vtbl =
199 session_destroy,
200 session_query_option,
201 session_set_option
204 /***********************************************************************
205 * WinHttpOpen (winhttp.@)
207 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
209 session_t *session;
210 HINTERNET handle = NULL;
212 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
214 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
216 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
217 session->hdr.vtbl = &session_vtbl;
218 session->hdr.flags = flags;
219 session->hdr.refs = 1;
220 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
221 list_init( &session->hdr.children );
222 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
223 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
224 session->send_timeout = DEFAULT_SEND_TIMEOUT;
225 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
226 list_init( &session->cookie_cache );
228 if (agent && !(session->agent = strdupW( agent ))) goto end;
229 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
231 WINHTTP_PROXY_INFO info;
233 WinHttpGetDefaultProxyConfiguration( &info );
234 session->access = info.dwAccessType;
235 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
237 GlobalFree( info.lpszProxy );
238 GlobalFree( info.lpszProxyBypass );
239 goto end;
241 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
243 GlobalFree( info.lpszProxy );
244 GlobalFree( info.lpszProxyBypass );
245 goto end;
248 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
250 session->access = access;
251 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
252 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
255 if (!(handle = alloc_handle( &session->hdr ))) goto end;
256 session->hdr.handle = handle;
258 end:
259 release_object( &session->hdr );
260 TRACE("returning %p\n", handle);
261 return handle;
264 /***********************************************************************
265 * connect_destroy (internal)
267 static void connect_destroy( object_header_t *hdr )
269 connect_t *connect = (connect_t *)hdr;
271 TRACE("%p\n", connect);
273 release_object( &connect->session->hdr );
275 heap_free( connect->hostname );
276 heap_free( connect->servername );
277 heap_free( connect->username );
278 heap_free( connect->password );
279 heap_free( connect );
282 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
284 connect_t *connect = (connect_t *)hdr;
286 switch (option)
288 case WINHTTP_OPTION_PARENT_HANDLE:
290 if (!buffer || *buflen < sizeof(HINTERNET))
292 *buflen = sizeof(HINTERNET);
293 set_last_error( ERROR_INSUFFICIENT_BUFFER );
294 return FALSE;
297 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
298 *buflen = sizeof(HINTERNET);
299 return TRUE;
301 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
302 *(DWORD *)buffer = connect->session->resolve_timeout;
303 *buflen = sizeof(DWORD);
304 return TRUE;
305 case WINHTTP_OPTION_CONNECT_TIMEOUT:
306 *(DWORD *)buffer = connect->session->connect_timeout;
307 *buflen = sizeof(DWORD);
308 return TRUE;
309 case WINHTTP_OPTION_SEND_TIMEOUT:
310 *(DWORD *)buffer = connect->session->send_timeout;
311 *buflen = sizeof(DWORD);
312 return TRUE;
313 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
314 *(DWORD *)buffer = connect->session->recv_timeout;
315 *buflen = sizeof(DWORD);
316 return TRUE;
317 default:
318 FIXME("unimplemented option %u\n", option);
319 set_last_error( ERROR_INVALID_PARAMETER );
320 return FALSE;
324 static const object_vtbl_t connect_vtbl =
326 connect_destroy,
327 connect_query_option,
328 NULL
331 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
333 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
334 BOOL ret = FALSE;
336 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
337 ret = TRUE;
338 else if (*domain == '*')
340 if (domain[1] == '.')
342 LPCWSTR dot;
344 /* For a hostname to match a wildcard, the last domain must match
345 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
346 * hostname is www.foo.a.b, it matches, but a.b does not.
348 dot = strchrW( server, '.' );
349 if (dot)
351 int len = strlenW( dot + 1 );
353 if (len > strlenW( domain + 2 ))
355 LPCWSTR ptr;
357 /* The server's domain is longer than the wildcard, so it
358 * could be a subdomain. Compare the last portion of the
359 * server's domain.
361 ptr = dot + len + 1 - strlenW( domain + 2 );
362 if (!strcmpiW( ptr, domain + 2 ))
364 /* This is only a match if the preceding character is
365 * a '.', i.e. that it is a matching domain. E.g.
366 * if domain is '*.b.c' and server is 'www.ab.c' they
367 * do not match.
369 ret = *(ptr - 1) == '.';
372 else
373 ret = !strcmpiW( dot + 1, domain + 2 );
377 else
378 ret = !strcmpiW( server, domain );
379 return ret;
382 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
383 #define MAX_HOST_NAME_LENGTH 256
385 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
387 LPCWSTR ptr;
388 BOOL ret = FALSE;
390 if (!session->proxy_bypass) return FALSE;
391 ptr = session->proxy_bypass;
392 do {
393 LPCWSTR tmp = ptr;
395 ptr = strchrW( ptr, ';' );
396 if (!ptr)
397 ptr = strchrW( tmp, ' ' );
398 if (ptr)
400 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
402 WCHAR domain[MAX_HOST_NAME_LENGTH];
404 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
405 domain[ptr - tmp] = 0;
406 ret = domain_matches( server, domain );
408 ptr += 1;
410 else if (*tmp)
411 ret = domain_matches( server, tmp );
412 } while (ptr && !ret);
413 return ret;
416 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
418 session_t *session = connect->session;
419 BOOL ret = TRUE;
421 if (session->proxy_server && !should_bypass_proxy(session, server))
423 LPCWSTR colon;
425 if ((colon = strchrW( session->proxy_server, ':' )))
427 if (!connect->servername || strncmpiW( connect->servername,
428 session->proxy_server, colon - session->proxy_server - 1 ))
430 heap_free( connect->servername );
431 connect->resolved = FALSE;
432 if (!(connect->servername = heap_alloc(
433 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
435 ret = FALSE;
436 goto end;
438 memcpy( connect->servername, session->proxy_server,
439 (colon - session->proxy_server) * sizeof(WCHAR) );
440 connect->servername[colon - session->proxy_server] = 0;
441 if (*(colon + 1))
442 connect->serverport = atoiW( colon + 1 );
443 else
444 connect->serverport = INTERNET_DEFAULT_PORT;
447 else
449 if (!connect->servername || strcmpiW( connect->servername,
450 session->proxy_server ))
452 heap_free( connect->servername );
453 connect->resolved = FALSE;
454 if (!(connect->servername = strdupW( session->proxy_server )))
456 ret = FALSE;
457 goto end;
459 connect->serverport = INTERNET_DEFAULT_PORT;
463 else if (server)
465 heap_free( connect->servername );
466 connect->resolved = FALSE;
467 if (!(connect->servername = strdupW( server )))
469 ret = FALSE;
470 goto end;
472 connect->serverport = port;
474 end:
475 return ret;
478 /***********************************************************************
479 * WinHttpConnect (winhttp.@)
481 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
483 connect_t *connect;
484 session_t *session;
485 HINTERNET hconnect = NULL;
487 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
489 if (!server)
491 set_last_error( ERROR_INVALID_PARAMETER );
492 return NULL;
494 if (!(session = (session_t *)grab_object( hsession )))
496 set_last_error( ERROR_INVALID_HANDLE );
497 return NULL;
499 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
501 release_object( &session->hdr );
502 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
503 return NULL;
505 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
507 release_object( &session->hdr );
508 return NULL;
510 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
511 connect->hdr.vtbl = &connect_vtbl;
512 connect->hdr.refs = 1;
513 connect->hdr.flags = session->hdr.flags;
514 connect->hdr.callback = session->hdr.callback;
515 connect->hdr.notify_mask = session->hdr.notify_mask;
516 connect->hdr.context = session->hdr.context;
517 connect->hdr.redirect_policy = session->hdr.redirect_policy;
518 list_init( &connect->hdr.children );
520 addref_object( &session->hdr );
521 connect->session = session;
522 list_add_head( &session->hdr.children, &connect->hdr.entry );
524 if (!(connect->hostname = strdupW( server ))) goto end;
525 connect->hostport = port;
526 if (!set_server_for_hostname( connect, server, port )) goto end;
528 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
529 connect->hdr.handle = hconnect;
531 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
533 end:
534 release_object( &connect->hdr );
535 release_object( &session->hdr );
536 TRACE("returning %p\n", hconnect);
537 return hconnect;
540 /***********************************************************************
541 * request_destroy (internal)
543 static void request_destroy( object_header_t *hdr )
545 request_t *request = (request_t *)hdr;
546 unsigned int i;
548 TRACE("%p\n", request);
550 release_object( &request->connect->hdr );
552 destroy_authinfo( request->authinfo );
553 destroy_authinfo( request->proxy_authinfo );
555 heap_free( request->verb );
556 heap_free( request->path );
557 heap_free( request->version );
558 heap_free( request->raw_headers );
559 heap_free( request->status_text );
560 for (i = 0; i < request->num_headers; i++)
562 heap_free( request->headers[i].field );
563 heap_free( request->headers[i].value );
565 heap_free( request->headers );
566 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
567 heap_free( request->accept_types );
568 heap_free( request );
571 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
573 int len = 0;
574 if (str) len = strlenW( str );
575 if (buffer && *buflen > len)
577 if (str) memcpy( buffer, str, len * sizeof(WCHAR) );
578 buffer[len] = 0;
580 *buflen = len * sizeof(WCHAR);
583 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
585 WCHAR *ret;
586 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
588 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
589 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
590 CertNameToStrW( encoding, blob, format, ret, size );
592 return ret;
595 static BOOL convert_sockaddr( const struct sockaddr *addr, SOCKADDR_STORAGE *addr_storage )
597 #ifndef __MINGW32__
598 switch (addr->sa_family)
600 case AF_INET:
602 const struct sockaddr_in *addr_unix = (const struct sockaddr_in *)addr;
603 struct WS_sockaddr_in *addr_win = (struct WS_sockaddr_in *)addr_storage;
604 char *p;
606 addr_win->sin_family = WS_AF_INET;
607 addr_win->sin_port = addr_unix->sin_port;
608 memcpy( &addr_win->sin_addr, &addr_unix->sin_addr, 4 );
609 p = (char *)&addr_win->sin_addr + 4;
610 memset( p, 0, sizeof(*addr_storage) - (p - (char *)addr_win) );
611 return TRUE;
613 case AF_INET6:
615 const struct sockaddr_in6 *addr_unix = (const struct sockaddr_in6 *)addr;
616 struct WS_sockaddr_in6 *addr_win = (struct WS_sockaddr_in6 *)addr_storage;
618 addr_win->sin6_family = WS_AF_INET6;
619 addr_win->sin6_port = addr_unix->sin6_port;
620 addr_win->sin6_flowinfo = addr_unix->sin6_flowinfo;
621 memcpy( &addr_win->sin6_addr, &addr_unix->sin6_addr, 16 );
622 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
623 addr_win->sin6_scope_id = addr_unix->sin6_scope_id;
624 #else
625 addr_win->sin6_scope_id = 0;
626 #endif
627 memset( addr_win + 1, 0, sizeof(*addr_storage) - sizeof(*addr_win) );
628 return TRUE;
630 default:
631 ERR("unhandled family %u\n", addr->sa_family);
632 return FALSE;
634 #else
635 switch (addr->sa_family)
637 case AF_INET:
639 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr_storage;
641 memcpy( addr_in, addr, sizeof(*addr_in) );
642 memset( addr_in + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in) );
643 return TRUE;
645 case AF_INET6:
647 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr_storage;
649 memcpy( addr_in6, addr, sizeof(*addr_in6) );
650 memset( addr_in6 + 1, 0, sizeof(*addr_storage) - sizeof(*addr_in6) );
651 return TRUE;
653 default:
654 ERR("unhandled family %u\n", addr->sa_family);
655 return FALSE;
657 #endif
660 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
662 request_t *request = (request_t *)hdr;
664 switch (option)
666 case WINHTTP_OPTION_SECURITY_FLAGS:
668 DWORD flags;
669 int bits;
671 if (!buffer || *buflen < sizeof(flags))
673 *buflen = sizeof(flags);
674 set_last_error( ERROR_INSUFFICIENT_BUFFER );
675 return FALSE;
678 flags = 0;
679 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
680 flags |= request->netconn.security_flags;
681 bits = netconn_get_cipher_strength( &request->netconn );
682 if (bits >= 128)
683 flags |= SECURITY_FLAG_STRENGTH_STRONG;
684 else if (bits >= 56)
685 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
686 else
687 flags |= SECURITY_FLAG_STRENGTH_WEAK;
688 *(DWORD *)buffer = flags;
689 *buflen = sizeof(flags);
690 return TRUE;
692 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
694 const CERT_CONTEXT *cert;
696 if (!buffer || *buflen < sizeof(cert))
698 *buflen = sizeof(cert);
699 set_last_error( ERROR_INSUFFICIENT_BUFFER );
700 return FALSE;
703 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
704 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
705 *buflen = sizeof(cert);
706 return TRUE;
708 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
710 const CERT_CONTEXT *cert;
711 const CRYPT_OID_INFO *oidInfo;
712 WINHTTP_CERTIFICATE_INFO *ci = buffer;
714 FIXME("partial stub\n");
716 if (!buffer || *buflen < sizeof(*ci))
718 *buflen = sizeof(*ci);
719 set_last_error( ERROR_INSUFFICIENT_BUFFER );
720 return FALSE;
722 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
724 ci->ftExpiry = cert->pCertInfo->NotAfter;
725 ci->ftStart = cert->pCertInfo->NotBefore;
726 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
727 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
728 ci->lpszProtocolName = NULL;
729 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
730 cert->pCertInfo->SignatureAlgorithm.pszObjId,
731 0 );
732 if (oidInfo)
733 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
734 else
735 ci->lpszSignatureAlgName = NULL;
736 ci->lpszEncryptionAlgName = NULL;
737 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
739 CertFreeCertificateContext( cert );
740 *buflen = sizeof(*ci);
741 return TRUE;
743 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
745 if (!buffer || *buflen < sizeof(DWORD))
747 *buflen = sizeof(DWORD);
748 set_last_error( ERROR_INSUFFICIENT_BUFFER );
749 return FALSE;
752 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
753 *buflen = sizeof(DWORD);
754 return TRUE;
756 case WINHTTP_OPTION_CONNECTION_INFO:
758 WINHTTP_CONNECTION_INFO *info = buffer;
759 struct sockaddr local;
760 socklen_t len = sizeof(local);
761 const struct sockaddr *remote = (const struct sockaddr *)&request->connect->sockaddr;
763 if (!buffer || *buflen < sizeof(*info))
765 *buflen = sizeof(*info);
766 set_last_error( ERROR_INSUFFICIENT_BUFFER );
767 return FALSE;
769 if (!netconn_connected( &request->netconn ))
771 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
772 return FALSE;
774 if (getsockname( request->netconn.socket, &local, &len )) return FALSE;
775 if (!convert_sockaddr( &local, &info->LocalAddress )) return FALSE;
776 if (!convert_sockaddr( remote, &info->RemoteAddress )) return FALSE;
777 info->cbSize = sizeof(*info);
778 return TRUE;
780 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
781 *(DWORD *)buffer = request->resolve_timeout;
782 *buflen = sizeof(DWORD);
783 return TRUE;
784 case WINHTTP_OPTION_CONNECT_TIMEOUT:
785 *(DWORD *)buffer = request->connect_timeout;
786 *buflen = sizeof(DWORD);
787 return TRUE;
788 case WINHTTP_OPTION_SEND_TIMEOUT:
789 *(DWORD *)buffer = request->send_timeout;
790 *buflen = sizeof(DWORD);
791 return TRUE;
792 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
793 *(DWORD *)buffer = request->recv_timeout;
794 *buflen = sizeof(DWORD);
795 return TRUE;
797 case WINHTTP_OPTION_USERNAME:
798 str_to_buffer( buffer, request->connect->username, buflen );
799 return TRUE;
801 case WINHTTP_OPTION_PASSWORD:
802 str_to_buffer( buffer, request->connect->password, buflen );
803 return TRUE;
805 case WINHTTP_OPTION_PROXY_USERNAME:
806 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
807 return TRUE;
809 case WINHTTP_OPTION_PROXY_PASSWORD:
810 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
811 return TRUE;
813 default:
814 FIXME("unimplemented option %u\n", option);
815 set_last_error( ERROR_INVALID_PARAMETER );
816 return FALSE;
820 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
822 WCHAR *ret;
823 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
825 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
826 ret[buflen] = 0;
827 return ret;
829 set_last_error( ERROR_OUTOFMEMORY );
830 return NULL;
833 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
835 request_t *request = (request_t *)hdr;
837 switch (option)
839 case WINHTTP_OPTION_PROXY:
841 WINHTTP_PROXY_INFO *pi = buffer;
843 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
844 return TRUE;
846 case WINHTTP_OPTION_DISABLE_FEATURE:
848 DWORD disable;
850 if (buflen != sizeof(DWORD))
852 set_last_error( ERROR_INSUFFICIENT_BUFFER );
853 return FALSE;
856 disable = *(DWORD *)buffer;
857 TRACE("0x%x\n", disable);
858 hdr->disable_flags |= disable;
859 return TRUE;
861 case WINHTTP_OPTION_AUTOLOGON_POLICY:
863 DWORD policy;
865 if (buflen != sizeof(DWORD))
867 set_last_error( ERROR_INSUFFICIENT_BUFFER );
868 return FALSE;
871 policy = *(DWORD *)buffer;
872 TRACE("0x%x\n", policy);
873 hdr->logon_policy = policy;
874 return TRUE;
876 case WINHTTP_OPTION_REDIRECT_POLICY:
878 DWORD policy;
880 if (buflen != sizeof(DWORD))
882 set_last_error( ERROR_INSUFFICIENT_BUFFER );
883 return FALSE;
886 policy = *(DWORD *)buffer;
887 TRACE("0x%x\n", policy);
888 hdr->redirect_policy = policy;
889 return TRUE;
891 case WINHTTP_OPTION_SECURITY_FLAGS:
893 DWORD flags;
895 if (buflen < sizeof(DWORD))
897 set_last_error( ERROR_INSUFFICIENT_BUFFER );
898 return FALSE;
900 flags = *(DWORD *)buffer;
901 TRACE("0x%x\n", flags);
902 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
903 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
904 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
905 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
907 set_last_error( ERROR_INVALID_PARAMETER );
908 return FALSE;
910 request->netconn.security_flags = flags;
911 return TRUE;
913 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
914 request->resolve_timeout = *(DWORD *)buffer;
915 return TRUE;
916 case WINHTTP_OPTION_CONNECT_TIMEOUT:
917 request->connect_timeout = *(DWORD *)buffer;
918 return TRUE;
919 case WINHTTP_OPTION_SEND_TIMEOUT:
920 request->send_timeout = *(DWORD *)buffer;
921 return TRUE;
922 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
923 request->recv_timeout = *(DWORD *)buffer;
924 return TRUE;
926 case WINHTTP_OPTION_USERNAME:
928 connect_t *connect = request->connect;
930 heap_free( connect->username );
931 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
932 return TRUE;
934 case WINHTTP_OPTION_PASSWORD:
936 connect_t *connect = request->connect;
938 heap_free( connect->password );
939 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
940 return TRUE;
942 case WINHTTP_OPTION_PROXY_USERNAME:
944 session_t *session = request->connect->session;
946 heap_free( session->proxy_username );
947 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
948 return TRUE;
950 case WINHTTP_OPTION_PROXY_PASSWORD:
952 session_t *session = request->connect->session;
954 heap_free( session->proxy_password );
955 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
956 return TRUE;
958 default:
959 FIXME("unimplemented option %u\n", option);
960 set_last_error( ERROR_INVALID_PARAMETER );
961 return TRUE;
965 static const object_vtbl_t request_vtbl =
967 request_destroy,
968 request_query_option,
969 request_set_option
972 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
974 const WCHAR **types = accept_types;
975 DWORD i;
977 if (!types) return TRUE;
978 while (*types)
980 request->num_accept_types++;
981 types++;
983 if (!request->num_accept_types) return TRUE;
984 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
986 request->num_accept_types = 0;
987 return FALSE;
989 types = accept_types;
990 for (i = 0; i < request->num_accept_types; i++)
992 if (!(request->accept_types[i] = strdupW( *types )))
994 for ( ; i > 0; --i) heap_free( request->accept_types[i - 1] );
995 heap_free( request->accept_types );
996 request->accept_types = NULL;
997 request->num_accept_types = 0;
998 return FALSE;
1000 types++;
1002 return TRUE;
1005 /***********************************************************************
1006 * WinHttpOpenRequest (winhttp.@)
1008 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
1009 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
1011 request_t *request;
1012 connect_t *connect;
1013 HINTERNET hrequest = NULL;
1015 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
1016 debugstr_w(version), debugstr_w(referrer), types, flags);
1018 if(types && TRACE_ON(winhttp)) {
1019 const WCHAR **iter;
1021 TRACE("accept types:\n");
1022 for(iter = types; *iter; iter++)
1023 TRACE(" %s\n", debugstr_w(*iter));
1026 if (!(connect = (connect_t *)grab_object( hconnect )))
1028 set_last_error( ERROR_INVALID_HANDLE );
1029 return NULL;
1031 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
1033 release_object( &connect->hdr );
1034 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1035 return NULL;
1037 if (!(request = heap_alloc_zero( sizeof(request_t) )))
1039 release_object( &connect->hdr );
1040 return NULL;
1042 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
1043 request->hdr.vtbl = &request_vtbl;
1044 request->hdr.refs = 1;
1045 request->hdr.flags = flags;
1046 request->hdr.callback = connect->hdr.callback;
1047 request->hdr.notify_mask = connect->hdr.notify_mask;
1048 request->hdr.context = connect->hdr.context;
1049 request->hdr.redirect_policy = connect->hdr.redirect_policy;
1050 list_init( &request->hdr.children );
1052 addref_object( &connect->hdr );
1053 request->connect = connect;
1054 list_add_head( &connect->hdr.children, &request->hdr.entry );
1056 if (!netconn_init( &request->netconn )) goto end;
1057 request->resolve_timeout = connect->session->resolve_timeout;
1058 request->connect_timeout = connect->session->connect_timeout;
1059 request->send_timeout = connect->session->send_timeout;
1060 request->recv_timeout = connect->session->recv_timeout;
1062 if (!verb || !verb[0]) verb = getW;
1063 if (!(request->verb = strdupW( verb ))) goto end;
1065 if (object)
1067 WCHAR *path, *p;
1068 unsigned int len;
1070 len = strlenW( object ) + 1;
1071 if (object[0] != '/') len++;
1072 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
1074 if (object[0] != '/') *p++ = '/';
1075 strcpyW( p, object );
1076 request->path = path;
1078 else if (!(request->path = strdupW( slashW ))) goto end;
1080 if (!version || !version[0]) version = http1_1;
1081 if (!(request->version = strdupW( version ))) goto end;
1082 if (!(store_accept_types( request, types ))) goto end;
1084 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
1085 request->hdr.handle = hrequest;
1087 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
1089 end:
1090 release_object( &request->hdr );
1091 release_object( &connect->hdr );
1092 TRACE("returning %p\n", hrequest);
1093 return hrequest;
1096 /***********************************************************************
1097 * WinHttpCloseHandle (winhttp.@)
1099 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
1101 object_header_t *hdr;
1103 TRACE("%p\n", handle);
1105 if (!(hdr = grab_object( handle )))
1107 set_last_error( ERROR_INVALID_HANDLE );
1108 return FALSE;
1110 release_object( hdr );
1111 free_handle( handle );
1112 return TRUE;
1115 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1117 BOOL ret = FALSE;
1119 if (!buflen)
1121 set_last_error( ERROR_INVALID_PARAMETER );
1122 return FALSE;
1125 switch (option)
1127 case WINHTTP_OPTION_CONTEXT_VALUE:
1129 if (!buffer || *buflen < sizeof(DWORD_PTR))
1131 *buflen = sizeof(DWORD_PTR);
1132 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1133 return FALSE;
1136 *(DWORD_PTR *)buffer = hdr->context;
1137 *buflen = sizeof(DWORD_PTR);
1138 return TRUE;
1140 default:
1141 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1142 else
1144 FIXME("unimplemented option %u\n", option);
1145 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1146 return FALSE;
1148 break;
1150 return ret;
1153 /***********************************************************************
1154 * WinHttpQueryOption (winhttp.@)
1156 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1158 BOOL ret = FALSE;
1159 object_header_t *hdr;
1161 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1163 if (!(hdr = grab_object( handle )))
1165 set_last_error( ERROR_INVALID_HANDLE );
1166 return FALSE;
1169 ret = query_option( hdr, option, buffer, buflen );
1171 release_object( hdr );
1172 return ret;
1175 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1177 BOOL ret = TRUE;
1179 if (!buffer)
1181 set_last_error( ERROR_INVALID_PARAMETER );
1182 return FALSE;
1185 switch (option)
1187 case WINHTTP_OPTION_CONTEXT_VALUE:
1189 if (buflen != sizeof(DWORD_PTR))
1191 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1192 return FALSE;
1195 hdr->context = *(DWORD_PTR *)buffer;
1196 return TRUE;
1198 default:
1199 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1200 else
1202 FIXME("unimplemented option %u\n", option);
1203 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1204 return FALSE;
1206 break;
1208 return ret;
1211 /***********************************************************************
1212 * WinHttpSetOption (winhttp.@)
1214 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1216 BOOL ret = FALSE;
1217 object_header_t *hdr;
1219 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1221 if (!(hdr = grab_object( handle )))
1223 set_last_error( ERROR_INVALID_HANDLE );
1224 return FALSE;
1227 ret = set_option( hdr, option, buffer, buflen );
1229 release_object( hdr );
1230 return ret;
1233 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
1235 char *ret;
1236 DWORD size = 0;
1238 GetComputerNameExA( format, NULL, &size );
1239 if (GetLastError() != ERROR_MORE_DATA) return NULL;
1240 if (!(ret = heap_alloc( size ))) return NULL;
1241 if (!GetComputerNameExA( format, ret, &size ))
1243 heap_free( ret );
1244 return NULL;
1246 return ret;
1249 static BOOL is_domain_suffix( const char *domain, const char *suffix )
1251 int len_domain = strlen( domain ), len_suffix = strlen( suffix );
1253 if (len_suffix > len_domain) return FALSE;
1254 if (!strcasecmp( domain + len_domain - len_suffix, suffix )) return TRUE;
1255 return FALSE;
1258 static int reverse_lookup( const struct addrinfo *ai, char *hostname, size_t len )
1260 int ret = -1;
1261 #ifdef HAVE_GETNAMEINFO
1262 ret = getnameinfo( ai->ai_addr, ai->ai_addrlen, hostname, len, NULL, 0, 0 );
1263 #endif
1264 return ret;
1267 static WCHAR *build_wpad_url( const char *hostname, const struct addrinfo *ai )
1269 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1270 static const WCHAR wpadW[] = {'/','w','p','a','d','.','d','a','t',0};
1271 char name[NI_MAXHOST];
1272 WCHAR *ret, *p;
1273 int len;
1275 while (ai && ai->ai_family != AF_INET && ai->ai_family != AF_INET6) ai = ai->ai_next;
1276 if (!ai) return NULL;
1278 if (!reverse_lookup( ai, name, sizeof(name) )) hostname = name;
1280 len = strlenW( httpW ) + strlen( hostname ) + strlenW( wpadW );
1281 if (!(ret = p = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1282 strcpyW( p, httpW );
1283 p += strlenW( httpW );
1284 while (*hostname) { *p++ = *hostname++; }
1285 strcpyW( p, wpadW );
1286 return ret;
1289 /***********************************************************************
1290 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1292 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1294 BOOL ret = FALSE;
1296 TRACE("0x%08x, %p\n", flags, url);
1298 if (!flags || !url)
1300 set_last_error( ERROR_INVALID_PARAMETER );
1301 return FALSE;
1303 if (flags & WINHTTP_AUTO_DETECT_TYPE_DHCP)
1305 static int fixme_shown;
1306 if (!fixme_shown++) FIXME("discovery via DHCP not supported\n");
1308 if (flags & WINHTTP_AUTO_DETECT_TYPE_DNS_A)
1310 #ifdef HAVE_GETADDRINFO
1311 char *fqdn, *domain, *p;
1313 if (!(fqdn = get_computer_name( ComputerNamePhysicalDnsFullyQualified ))) return FALSE;
1314 if (!(domain = get_computer_name( ComputerNamePhysicalDnsDomain )))
1316 heap_free( fqdn );
1317 return FALSE;
1319 p = fqdn;
1320 while ((p = strchr( p, '.' )) && is_domain_suffix( p + 1, domain ))
1322 struct addrinfo *ai;
1323 char *name;
1324 int res;
1326 if (!(name = heap_alloc( sizeof("wpad") + strlen(p) )))
1328 heap_free( fqdn );
1329 heap_free( domain );
1330 return FALSE;
1332 strcpy( name, "wpad" );
1333 strcat( name, p );
1334 res = getaddrinfo( name, NULL, NULL, &ai );
1335 if (!res)
1337 *url = build_wpad_url( name, ai );
1338 freeaddrinfo( ai );
1339 if (*url)
1341 TRACE("returning %s\n", debugstr_w(*url));
1342 heap_free( name );
1343 ret = TRUE;
1344 break;
1347 heap_free( name );
1348 p++;
1350 heap_free( domain );
1351 heap_free( fqdn );
1352 #else
1353 FIXME("getaddrinfo not found at build time\n");
1354 #endif
1356 if (!ret)
1358 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1359 *url = NULL;
1361 return ret;
1364 static const WCHAR Connections[] = {
1365 'S','o','f','t','w','a','r','e','\\',
1366 'M','i','c','r','o','s','o','f','t','\\',
1367 'W','i','n','d','o','w','s','\\',
1368 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1369 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1370 'C','o','n','n','e','c','t','i','o','n','s',0 };
1371 static const WCHAR WinHttpSettings[] = {
1372 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1373 static const DWORD WINHTTP_SETTINGS_MAGIC = 0x18;
1374 static const DWORD WININET_SETTINGS_MAGIC = 0x46;
1375 static const DWORD PROXY_TYPE_DIRECT = 1;
1376 static const DWORD PROXY_TYPE_PROXY = 2;
1377 static const DWORD PROXY_USE_PAC_SCRIPT = 4;
1378 static const DWORD PROXY_AUTODETECT_SETTINGS = 8;
1380 struct connection_settings_header
1382 DWORD magic;
1383 DWORD unknown; /* always zero? */
1384 DWORD flags; /* one or more of PROXY_* */
1387 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1389 const BYTE *begin;
1391 for (begin = src; src - begin < len; src++, dst++)
1392 *dst = *src;
1393 *dst = 0;
1396 /***********************************************************************
1397 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1399 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1401 LONG l;
1402 HKEY key;
1403 BOOL got_from_reg = FALSE, direct = TRUE;
1404 char *envproxy;
1406 TRACE("%p\n", info);
1408 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1409 if (!l)
1411 DWORD type, size = 0;
1413 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1414 if (!l && type == REG_BINARY &&
1415 size >= sizeof(struct connection_settings_header) + 2 * sizeof(DWORD))
1417 BYTE *buf = heap_alloc( size );
1419 if (buf)
1421 struct connection_settings_header *hdr =
1422 (struct connection_settings_header *)buf;
1423 DWORD *len = (DWORD *)(hdr + 1);
1425 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1426 &size );
1427 if (!l && hdr->magic == WINHTTP_SETTINGS_MAGIC &&
1428 hdr->unknown == 0)
1430 if (hdr->flags & PROXY_TYPE_PROXY)
1432 BOOL sane = FALSE;
1433 LPWSTR proxy = NULL;
1434 LPWSTR proxy_bypass = NULL;
1436 /* Sanity-check length of proxy string */
1437 if ((BYTE *)len - buf + *len <= size)
1439 sane = TRUE;
1440 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1441 if (proxy)
1442 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1443 len = (DWORD *)((BYTE *)(len + 1) + *len);
1445 if (sane)
1447 /* Sanity-check length of proxy bypass string */
1448 if ((BYTE *)len - buf + *len <= size)
1450 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1451 if (proxy_bypass)
1452 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1454 else
1456 sane = FALSE;
1457 GlobalFree( proxy );
1458 proxy = NULL;
1461 info->lpszProxy = proxy;
1462 info->lpszProxyBypass = proxy_bypass;
1463 if (sane)
1465 got_from_reg = TRUE;
1466 direct = FALSE;
1467 info->dwAccessType =
1468 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1469 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1470 debugstr_w(info->lpszProxy),
1471 debugstr_w(info->lpszProxyBypass));
1475 heap_free( buf );
1478 RegCloseKey( key );
1480 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1482 char *colon, *http_proxy;
1484 if ((colon = strchr( envproxy, ':' )))
1486 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1488 static const char http[] = "http://";
1490 /* It's a scheme, check that it's http */
1491 if (!strncmp( envproxy, http, strlen( http ) ))
1492 http_proxy = envproxy + strlen( http );
1493 else
1495 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1496 http_proxy = NULL;
1499 else
1500 http_proxy = envproxy;
1502 else
1503 http_proxy = envproxy;
1504 if (http_proxy)
1506 WCHAR *http_proxyW;
1507 int len;
1509 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1510 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1512 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1513 direct = FALSE;
1514 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1515 info->lpszProxy = http_proxyW;
1516 info->lpszProxyBypass = NULL;
1517 TRACE("http proxy (from environment) = %s\n",
1518 debugstr_w(info->lpszProxy));
1522 if (direct)
1524 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1525 info->lpszProxy = NULL;
1526 info->lpszProxyBypass = NULL;
1528 return TRUE;
1531 /***********************************************************************
1532 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1534 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1536 static const WCHAR settingsW[] =
1537 {'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};
1538 HKEY hkey = NULL;
1539 struct connection_settings_header *hdr = NULL;
1540 DWORD type, offset, len, size = 0;
1541 BOOL ret = FALSE;
1543 TRACE("%p\n", config);
1545 if (!config)
1547 set_last_error( ERROR_INVALID_PARAMETER );
1548 return FALSE;
1550 memset( config, 0, sizeof(*config) );
1551 config->fAutoDetect = TRUE;
1553 if (RegOpenKeyExW( HKEY_CURRENT_USER, Connections, 0, KEY_READ, &hkey ) ||
1554 RegQueryValueExW( hkey, settingsW, NULL, &type, NULL, &size ) ||
1555 type != REG_BINARY || size < sizeof(struct connection_settings_header))
1557 ret = TRUE;
1558 goto done;
1560 if (!(hdr = heap_alloc( size ))) goto done;
1561 if (RegQueryValueExW( hkey, settingsW, NULL, &type, (BYTE *)hdr, &size ) ||
1562 hdr->magic != WININET_SETTINGS_MAGIC)
1564 ret = TRUE;
1565 goto done;
1568 config->fAutoDetect = (hdr->flags & PROXY_AUTODETECT_SETTINGS) != 0;
1569 offset = sizeof(*hdr);
1570 if (offset + sizeof(DWORD) > size) goto done;
1571 len = *(DWORD *)((char *)hdr + offset);
1572 offset += sizeof(DWORD);
1573 if (len && hdr->flags & PROXY_TYPE_PROXY)
1575 if (!(config->lpszProxy = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1576 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxy );
1578 offset += len;
1579 if (offset + sizeof(DWORD) > size) goto done;
1580 len = *(DWORD *)((char *)hdr + offset);
1581 offset += sizeof(DWORD);
1582 if (len && (hdr->flags & PROXY_TYPE_PROXY))
1584 if (!(config->lpszProxyBypass = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1585 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszProxyBypass );
1587 offset += len;
1588 if (offset + sizeof(DWORD) > size) goto done;
1589 len = *(DWORD *)((char *)hdr + offset);
1590 offset += sizeof(DWORD);
1591 if (len && (hdr->flags & PROXY_USE_PAC_SCRIPT))
1593 if (!(config->lpszAutoConfigUrl = GlobalAlloc( 0, (len + 1) * sizeof(WCHAR) ))) goto done;
1594 copy_char_to_wchar_sz( (const BYTE *)hdr + offset , len, config->lpszAutoConfigUrl );
1596 ret = TRUE;
1598 done:
1599 RegCloseKey( hkey );
1600 heap_free( hdr );
1601 if (!ret)
1603 GlobalFree( config->lpszAutoConfigUrl );
1604 config->lpszAutoConfigUrl = NULL;
1605 GlobalFree( config->lpszProxy );
1606 config->lpszProxy = NULL;
1607 GlobalFree( config->lpszProxyBypass );
1608 config->lpszProxyBypass = NULL;
1610 return ret;
1613 static BOOL parse_script_result( const char *result, WINHTTP_PROXY_INFO *info )
1615 const char *p;
1616 WCHAR *q;
1617 int len;
1619 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1620 info->lpszProxy = NULL;
1621 info->lpszProxyBypass = NULL;
1623 TRACE("%s\n", debugstr_a( result ));
1625 p = result;
1626 while (*p == ' ') p++;
1627 len = strlen( p );
1628 if (len >= 5 && !strncasecmp( p, "PROXY", sizeof("PROXY") - 1 ))
1630 p += 5;
1631 while (*p == ' ') p++;
1632 if (!*p || *p == ';') return TRUE;
1633 if (!(info->lpszProxy = q = strdupAW( p ))) return FALSE;
1634 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1635 for (; *q; q++)
1637 if (*q == ' ' || *q == ';')
1639 *q = 0;
1640 break;
1644 return TRUE;
1647 static char *download_script( const WCHAR *url, DWORD *out_size )
1649 static const WCHAR typeW[] = {'*','/','*',0};
1650 static const WCHAR *acceptW[] = {typeW, NULL};
1651 HINTERNET ses, con = NULL, req = NULL;
1652 WCHAR *hostname;
1653 URL_COMPONENTSW uc;
1654 DWORD status, size = sizeof(status), offset, to_read, bytes_read, flags = 0;
1655 char *tmp, *buffer = NULL;
1657 *out_size = 0;
1659 memset( &uc, 0, sizeof(uc) );
1660 uc.dwStructSize = sizeof(uc);
1661 if (!WinHttpCrackUrl( url, 0, 0, &uc )) return NULL;
1662 if (!(hostname = heap_alloc( (uc.dwHostNameLength + 1) * sizeof(WCHAR) ))) return NULL;
1663 memcpy( hostname, uc.lpszHostName, uc.dwHostNameLength * sizeof(WCHAR) );
1664 hostname[uc.dwHostNameLength] = 0;
1666 if (!(ses = WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0 ))) goto done;
1667 if (!(con = WinHttpConnect( ses, hostname, uc.nPort, 0 ))) goto done;
1668 if (uc.nScheme == INTERNET_SCHEME_HTTPS) flags |= WINHTTP_FLAG_SECURE;
1669 if (!(req = WinHttpOpenRequest( con, NULL, uc.lpszUrlPath, NULL, NULL, acceptW, flags ))) goto done;
1670 if (!WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 )) goto done;
1672 if (!(WinHttpReceiveResponse( req, 0 ))) goto done;
1673 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, NULL, &status,
1674 &size, NULL ) || status != HTTP_STATUS_OK) goto done;
1676 size = 4096;
1677 if (!(buffer = heap_alloc( size ))) goto done;
1678 to_read = size;
1679 offset = 0;
1680 for (;;)
1682 if (!WinHttpReadData( req, buffer + offset, to_read, &bytes_read )) goto done;
1683 if (!bytes_read) break;
1684 to_read -= bytes_read;
1685 offset += bytes_read;
1686 *out_size += bytes_read;
1687 if (!to_read)
1689 to_read = size;
1690 size *= 2;
1691 if (!(tmp = heap_realloc( buffer, size ))) goto done;
1692 buffer = tmp;
1696 done:
1697 WinHttpCloseHandle( req );
1698 WinHttpCloseHandle( con );
1699 WinHttpCloseHandle( ses );
1700 heap_free( hostname );
1701 if (!buffer) set_last_error( ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT );
1702 return buffer;
1705 struct AUTO_PROXY_SCRIPT_BUFFER
1707 DWORD dwStructSize;
1708 LPSTR lpszScriptBuffer;
1709 DWORD dwScriptBufferSize;
1712 BOOL WINAPI InternetDeInitializeAutoProxyDll(LPSTR, DWORD);
1713 BOOL WINAPI InternetGetProxyInfo(LPCSTR, DWORD, LPSTR, DWORD, LPSTR *, LPDWORD);
1714 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD, LPSTR, LPSTR, void *, struct AUTO_PROXY_SCRIPT_BUFFER *);
1716 static BOOL run_script( char *script, DWORD size, const WCHAR *url, WINHTTP_PROXY_INFO *info )
1718 BOOL ret;
1719 char *result, *urlA;
1720 DWORD len_result;
1721 struct AUTO_PROXY_SCRIPT_BUFFER buffer;
1723 buffer.dwStructSize = sizeof(buffer);
1724 buffer.lpszScriptBuffer = script;
1725 buffer.dwScriptBufferSize = size;
1727 if (!(urlA = strdupWA( url ))) return FALSE;
1728 if (!(ret = InternetInitializeAutoProxyDll( 0, NULL, NULL, NULL, &buffer )))
1730 heap_free( urlA );
1731 return FALSE;
1733 if ((ret = InternetGetProxyInfo( urlA, strlen(urlA), NULL, 0, &result, &len_result )))
1735 ret = parse_script_result( result, info );
1736 heap_free( result );
1738 heap_free( urlA );
1739 return InternetDeInitializeAutoProxyDll( NULL, 0 );
1742 /***********************************************************************
1743 * WinHttpGetProxyForUrl (winhttp.@)
1745 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1746 WINHTTP_PROXY_INFO *info )
1748 WCHAR *detected_pac_url = NULL;
1749 const WCHAR *pac_url;
1750 session_t *session;
1751 char *script;
1752 DWORD size;
1753 BOOL ret = FALSE;
1755 TRACE("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1757 if (!(session = (session_t *)grab_object( hsession )))
1759 set_last_error( ERROR_INVALID_HANDLE );
1760 return FALSE;
1762 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
1764 release_object( &session->hdr );
1765 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1766 return FALSE;
1768 if (!url || !options || !info ||
1769 !(options->dwFlags & (WINHTTP_AUTOPROXY_AUTO_DETECT|WINHTTP_AUTOPROXY_CONFIG_URL)) ||
1770 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) && !options->dwAutoDetectFlags) ||
1771 ((options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT) &&
1772 (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL)))
1774 release_object( &session->hdr );
1775 set_last_error( ERROR_INVALID_PARAMETER );
1776 return FALSE;
1778 if (options->dwFlags & WINHTTP_AUTOPROXY_AUTO_DETECT &&
1779 !WinHttpDetectAutoProxyConfigUrl( options->dwAutoDetectFlags, &detected_pac_url ))
1780 goto done;
1782 if (options->dwFlags & WINHTTP_AUTOPROXY_CONFIG_URL) pac_url = options->lpszAutoConfigUrl;
1783 else pac_url = detected_pac_url;
1785 if ((script = download_script( pac_url, &size )))
1787 ret = run_script( script, size, url, info );
1788 heap_free( script );
1791 done:
1792 GlobalFree( detected_pac_url );
1793 release_object( &session->hdr );
1794 return ret;
1797 /***********************************************************************
1798 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1800 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1802 LONG l;
1803 HKEY key;
1804 BOOL ret = FALSE;
1805 const WCHAR *src;
1807 TRACE("%p\n", info);
1809 if (!info)
1811 set_last_error( ERROR_INVALID_PARAMETER );
1812 return FALSE;
1814 switch (info->dwAccessType)
1816 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1817 break;
1818 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1819 if (!info->lpszProxy)
1821 set_last_error( ERROR_INVALID_PARAMETER );
1822 return FALSE;
1824 /* Only ASCII characters are allowed */
1825 for (src = info->lpszProxy; *src; src++)
1826 if (*src > 0x7f)
1828 set_last_error( ERROR_INVALID_PARAMETER );
1829 return FALSE;
1831 if (info->lpszProxyBypass)
1833 for (src = info->lpszProxyBypass; *src; src++)
1834 if (*src > 0x7f)
1836 set_last_error( ERROR_INVALID_PARAMETER );
1837 return FALSE;
1840 break;
1841 default:
1842 set_last_error( ERROR_INVALID_PARAMETER );
1843 return FALSE;
1846 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1847 KEY_WRITE, NULL, &key, NULL );
1848 if (!l)
1850 DWORD size = sizeof(struct connection_settings_header) + 2 * sizeof(DWORD);
1851 BYTE *buf;
1853 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1855 size += strlenW( info->lpszProxy );
1856 if (info->lpszProxyBypass)
1857 size += strlenW( info->lpszProxyBypass );
1859 buf = heap_alloc( size );
1860 if (buf)
1862 struct connection_settings_header *hdr =
1863 (struct connection_settings_header *)buf;
1864 DWORD *len = (DWORD *)(hdr + 1);
1866 hdr->magic = WINHTTP_SETTINGS_MAGIC;
1867 hdr->unknown = 0;
1868 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1870 BYTE *dst;
1872 hdr->flags = PROXY_TYPE_PROXY;
1873 *len++ = strlenW( info->lpszProxy );
1874 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1875 src++, dst++)
1876 *dst = *src;
1877 len = (DWORD *)dst;
1878 if (info->lpszProxyBypass)
1880 *len++ = strlenW( info->lpszProxyBypass );
1881 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1882 src++, dst++)
1883 *dst = *src;
1885 else
1886 *len++ = 0;
1888 else
1890 hdr->flags = PROXY_TYPE_DIRECT;
1891 *len++ = 0;
1892 *len++ = 0;
1894 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1895 if (!l)
1896 ret = TRUE;
1897 heap_free( buf );
1899 RegCloseKey( key );
1901 return ret;
1904 /***********************************************************************
1905 * WinHttpSetStatusCallback (winhttp.@)
1907 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1908 DWORD flags, DWORD_PTR reserved )
1910 object_header_t *hdr;
1911 WINHTTP_STATUS_CALLBACK ret;
1913 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1915 if (!(hdr = grab_object( handle )))
1917 set_last_error( ERROR_INVALID_HANDLE );
1918 return WINHTTP_INVALID_STATUS_CALLBACK;
1920 ret = hdr->callback;
1921 hdr->callback = callback;
1922 hdr->notify_mask = flags;
1924 release_object( hdr );
1925 return ret;
1928 /***********************************************************************
1929 * WinHttpSetTimeouts (winhttp.@)
1931 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1933 BOOL ret = TRUE;
1934 object_header_t *hdr;
1935 request_t *request;
1936 session_t *session;
1938 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1940 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1942 set_last_error( ERROR_INVALID_PARAMETER );
1943 return FALSE;
1946 if (!(hdr = grab_object( handle )))
1948 set_last_error( ERROR_INVALID_HANDLE );
1949 return FALSE;
1952 switch(hdr->type)
1954 case WINHTTP_HANDLE_TYPE_REQUEST:
1955 request = (request_t *)hdr;
1956 request->connect_timeout = connect;
1958 if (resolve < 0) resolve = 0;
1959 request->resolve_timeout = resolve;
1961 if (send < 0) send = 0;
1962 request->send_timeout = send;
1964 if (receive < 0) receive = 0;
1965 request->recv_timeout = receive;
1967 if (netconn_connected( &request->netconn ))
1969 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1970 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1973 release_object( &request->hdr );
1974 break;
1976 case WINHTTP_HANDLE_TYPE_SESSION:
1977 session = (session_t *)hdr;
1978 session->connect_timeout = connect;
1980 if (resolve < 0) resolve = 0;
1981 session->resolve_timeout = resolve;
1983 if (send < 0) send = 0;
1984 session->send_timeout = send;
1986 if (receive < 0) receive = 0;
1987 session->recv_timeout = receive;
1988 break;
1990 default:
1991 release_object( hdr );
1992 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1993 return FALSE;
1995 return ret;
1998 static const WCHAR wkday[7][4] =
1999 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
2000 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
2001 static const WCHAR month[12][4] =
2002 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
2003 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
2004 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
2006 /***********************************************************************
2007 * WinHttpTimeFromSystemTime (WININET.@)
2009 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
2011 static const WCHAR format[] =
2012 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2013 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
2015 TRACE("%p, %p\n", time, string);
2017 if (!time || !string) return FALSE;
2019 sprintfW( string, format,
2020 wkday[time->wDayOfWeek],
2021 time->wDay,
2022 month[time->wMonth - 1],
2023 time->wYear,
2024 time->wHour,
2025 time->wMinute,
2026 time->wSecond );
2028 return TRUE;
2031 /***********************************************************************
2032 * WinHttpTimeToSystemTime (WININET.@)
2034 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
2036 unsigned int i;
2037 const WCHAR *s = string;
2038 WCHAR *end;
2040 TRACE("%s, %p\n", debugstr_w(string), time);
2042 if (!string || !time) return FALSE;
2044 /* Windows does this too */
2045 GetSystemTime( time );
2047 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2048 * a SYSTEMTIME structure.
2051 while (*s && !isalphaW( *s )) s++;
2052 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2053 time->wDayOfWeek = 7;
2055 for (i = 0; i < 7; i++)
2057 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
2058 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
2059 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
2061 time->wDayOfWeek = i;
2062 break;
2066 if (time->wDayOfWeek > 6) return TRUE;
2067 while (*s && !isdigitW( *s )) s++;
2068 time->wDay = strtolW( s, &end, 10 );
2069 s = end;
2071 while (*s && !isalphaW( *s )) s++;
2072 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2073 time->wMonth = 0;
2075 for (i = 0; i < 12; i++)
2077 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
2078 toupperW( month[i][1]) == toupperW( s[1] ) &&
2079 toupperW( month[i][2]) == toupperW( s[2] ) )
2081 time->wMonth = i + 1;
2082 break;
2085 if (time->wMonth == 0) return TRUE;
2087 while (*s && !isdigitW( *s )) s++;
2088 if (*s == '\0') return TRUE;
2089 time->wYear = strtolW( s, &end, 10 );
2090 s = end;
2092 while (*s && !isdigitW( *s )) s++;
2093 if (*s == '\0') return TRUE;
2094 time->wHour = strtolW( s, &end, 10 );
2095 s = end;
2097 while (*s && !isdigitW( *s )) s++;
2098 if (*s == '\0') return TRUE;
2099 time->wMinute = strtolW( s, &end, 10 );
2100 s = end;
2102 while (*s && !isdigitW( *s )) s++;
2103 if (*s == '\0') return TRUE;
2104 time->wSecond = strtolW( s, &end, 10 );
2106 time->wMilliseconds = 0;
2107 return TRUE;