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
20 #include "wine/port.h"
21 #include "wine/debug.h"
32 #include "winhttp_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
36 #define DEFAULT_RESOLVE_TIMEOUT 0
37 #define DEFAULT_CONNECT_TIMEOUT 20000
38 #define DEFAULT_SEND_TIMEOUT 30000
39 #define DEFAULT_RECEIVE_TIMEOUT 30000
41 void set_last_error( DWORD error )
44 SetLastError( error );
47 DWORD get_last_error( void )
50 return GetLastError();
53 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
55 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
57 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
60 /***********************************************************************
61 * WinHttpCheckPlatform (winhttp.@)
63 BOOL WINAPI WinHttpCheckPlatform( void )
69 /***********************************************************************
70 * session_destroy (internal)
72 static void session_destroy( object_header_t *hdr )
74 session_t *session = (session_t *)hdr;
75 struct list *item, *next;
78 TRACE("%p\n", session);
80 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
82 domain = LIST_ENTRY( item, domain_t, entry );
83 delete_domain( domain );
85 heap_free( session->agent );
86 heap_free( session->proxy_server );
87 heap_free( session->proxy_bypass );
88 heap_free( session->proxy_username );
89 heap_free( session->proxy_password );
93 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
95 session_t *session = (session_t *)hdr;
99 case WINHTTP_OPTION_REDIRECT_POLICY:
101 if (!buffer || *buflen < sizeof(DWORD))
103 *buflen = sizeof(DWORD);
104 set_last_error( ERROR_INSUFFICIENT_BUFFER );
108 *(DWORD *)buffer = hdr->redirect_policy;
109 *buflen = sizeof(DWORD);
112 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
113 *(DWORD *)buffer = session->resolve_timeout;
114 *buflen = sizeof(DWORD);
116 case WINHTTP_OPTION_CONNECT_TIMEOUT:
117 *(DWORD *)buffer = session->connect_timeout;
118 *buflen = sizeof(DWORD);
120 case WINHTTP_OPTION_SEND_TIMEOUT:
121 *(DWORD *)buffer = session->send_timeout;
122 *buflen = sizeof(DWORD);
124 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
125 *(DWORD *)buffer = session->recv_timeout;
126 *buflen = sizeof(DWORD);
129 FIXME("unimplemented option %u\n", option);
130 set_last_error( ERROR_INVALID_PARAMETER );
135 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
137 session_t *session = (session_t *)hdr;
141 case WINHTTP_OPTION_PROXY:
143 WINHTTP_PROXY_INFO *pi = buffer;
145 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
148 case WINHTTP_OPTION_REDIRECT_POLICY:
152 if (buflen != sizeof(policy))
154 set_last_error( ERROR_INSUFFICIENT_BUFFER );
158 policy = *(DWORD *)buffer;
159 TRACE("0x%x\n", policy);
160 hdr->redirect_policy = policy;
163 case WINHTTP_OPTION_DISABLE_FEATURE:
164 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
166 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
167 session->resolve_timeout = *(DWORD *)buffer;
169 case WINHTTP_OPTION_CONNECT_TIMEOUT:
170 session->connect_timeout = *(DWORD *)buffer;
172 case WINHTTP_OPTION_SEND_TIMEOUT:
173 session->send_timeout = *(DWORD *)buffer;
175 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
176 session->recv_timeout = *(DWORD *)buffer;
179 FIXME("unimplemented option %u\n", option);
180 set_last_error( ERROR_INVALID_PARAMETER );
185 static const object_vtbl_t session_vtbl =
188 session_query_option,
192 /***********************************************************************
193 * WinHttpOpen (winhttp.@)
195 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
198 HINTERNET handle = NULL;
200 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
202 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
204 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
205 session->hdr.vtbl = &session_vtbl;
206 session->hdr.flags = flags;
207 session->hdr.refs = 1;
208 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
209 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
210 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
211 session->send_timeout = DEFAULT_SEND_TIMEOUT;
212 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
213 list_init( &session->cookie_cache );
215 if (agent && !(session->agent = strdupW( agent ))) goto end;
216 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
218 WINHTTP_PROXY_INFO info;
220 WinHttpGetDefaultProxyConfiguration( &info );
221 session->access = info.dwAccessType;
222 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
224 GlobalFree( (LPWSTR)info.lpszProxy );
225 GlobalFree( (LPWSTR)info.lpszProxyBypass );
228 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
230 GlobalFree( (LPWSTR)info.lpszProxy );
231 GlobalFree( (LPWSTR)info.lpszProxyBypass );
235 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
237 session->access = access;
238 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
239 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
242 if (!(handle = alloc_handle( &session->hdr ))) goto end;
243 session->hdr.handle = handle;
246 release_object( &session->hdr );
247 TRACE("returning %p\n", handle);
251 /***********************************************************************
252 * connect_destroy (internal)
254 static void connect_destroy( object_header_t *hdr )
256 connect_t *connect = (connect_t *)hdr;
258 TRACE("%p\n", connect);
260 release_object( &connect->session->hdr );
262 heap_free( connect->hostname );
263 heap_free( connect->servername );
264 heap_free( connect->username );
265 heap_free( connect->password );
266 heap_free( connect );
269 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
271 connect_t *connect = (connect_t *)hdr;
275 case WINHTTP_OPTION_PARENT_HANDLE:
277 if (!buffer || *buflen < sizeof(HINTERNET))
279 *buflen = sizeof(HINTERNET);
280 set_last_error( ERROR_INSUFFICIENT_BUFFER );
284 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
285 *buflen = sizeof(HINTERNET);
288 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
289 *(DWORD *)buffer = connect->session->resolve_timeout;
290 *buflen = sizeof(DWORD);
292 case WINHTTP_OPTION_CONNECT_TIMEOUT:
293 *(DWORD *)buffer = connect->session->connect_timeout;
294 *buflen = sizeof(DWORD);
296 case WINHTTP_OPTION_SEND_TIMEOUT:
297 *(DWORD *)buffer = connect->session->send_timeout;
298 *buflen = sizeof(DWORD);
300 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
301 *(DWORD *)buffer = connect->session->recv_timeout;
302 *buflen = sizeof(DWORD);
305 FIXME("unimplemented option %u\n", option);
306 set_last_error( ERROR_INVALID_PARAMETER );
311 static const object_vtbl_t connect_vtbl =
314 connect_query_option,
318 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
320 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
323 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
325 else if (*domain == '*')
327 if (domain[1] == '.')
331 /* For a hostname to match a wildcard, the last domain must match
332 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
333 * hostname is www.foo.a.b, it matches, but a.b does not.
335 dot = strchrW( server, '.' );
338 int len = strlenW( dot + 1 );
340 if (len > strlenW( domain + 2 ))
344 /* The server's domain is longer than the wildcard, so it
345 * could be a subdomain. Compare the last portion of the
348 ptr = dot + len + 1 - strlenW( domain + 2 );
349 if (!strcmpiW( ptr, domain + 2 ))
351 /* This is only a match if the preceding character is
352 * a '.', i.e. that it is a matching domain. E.g.
353 * if domain is '*.b.c' and server is 'www.ab.c' they
356 ret = *(ptr - 1) == '.';
360 ret = !strcmpiW( dot + 1, domain + 2 );
365 ret = !strcmpiW( server, domain );
369 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
370 #define MAX_HOST_NAME_LENGTH 256
372 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
377 if (!session->proxy_bypass) return FALSE;
378 ptr = session->proxy_bypass;
382 ptr = strchrW( ptr, ';' );
384 ptr = strchrW( tmp, ' ' );
387 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
389 WCHAR domain[MAX_HOST_NAME_LENGTH];
391 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
392 domain[ptr - tmp] = 0;
393 ret = domain_matches( server, domain );
398 ret = domain_matches( server, tmp );
399 } while (ptr && !ret);
403 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
405 session_t *session = connect->session;
408 if (session->proxy_server && !should_bypass_proxy(session, server))
412 if ((colon = strchrW( session->proxy_server, ':' )))
414 if (!connect->servername || strncmpiW( connect->servername,
415 session->proxy_server, colon - session->proxy_server - 1 ))
417 heap_free( connect->servername );
418 if (!(connect->servername = heap_alloc(
419 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
424 memcpy( connect->servername, session->proxy_server,
425 (colon - session->proxy_server) * sizeof(WCHAR) );
426 connect->servername[colon - session->proxy_server] = 0;
428 connect->serverport = atoiW( colon + 1 );
430 connect->serverport = INTERNET_DEFAULT_PORT;
435 if (!connect->servername || strcmpiW( connect->servername,
436 session->proxy_server ))
438 heap_free( connect->servername );
439 if (!(connect->servername = strdupW( session->proxy_server )))
444 connect->serverport = INTERNET_DEFAULT_PORT;
450 heap_free( connect->servername );
451 if (!(connect->servername = strdupW( server )))
456 connect->serverport = port;
462 /***********************************************************************
463 * WinHttpConnect (winhttp.@)
465 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
469 HINTERNET hconnect = NULL;
471 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
475 set_last_error( ERROR_INVALID_PARAMETER );
478 if (!(session = (session_t *)grab_object( hsession )))
480 set_last_error( ERROR_INVALID_HANDLE );
483 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
485 release_object( &session->hdr );
486 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
489 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
491 release_object( &session->hdr );
494 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
495 connect->hdr.vtbl = &connect_vtbl;
496 connect->hdr.refs = 1;
497 connect->hdr.flags = session->hdr.flags;
498 connect->hdr.callback = session->hdr.callback;
499 connect->hdr.notify_mask = session->hdr.notify_mask;
500 connect->hdr.context = session->hdr.context;
502 addref_object( &session->hdr );
503 connect->session = session;
504 list_add_head( &session->hdr.children, &connect->hdr.entry );
506 if (!(connect->hostname = strdupW( server ))) goto end;
507 connect->hostport = port;
509 if (!set_server_for_hostname( connect, server, port ))
512 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
513 connect->hdr.handle = hconnect;
515 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
518 release_object( &connect->hdr );
520 TRACE("returning %p\n", hconnect);
524 /***********************************************************************
525 * request_destroy (internal)
527 static void request_destroy( object_header_t *hdr )
529 request_t *request = (request_t *)hdr;
532 TRACE("%p\n", request);
534 release_object( &request->connect->hdr );
536 heap_free( request->verb );
537 heap_free( request->path );
538 heap_free( request->version );
539 heap_free( request->raw_headers );
540 heap_free( request->status_text );
541 for (i = 0; i < request->num_headers; i++)
543 heap_free( request->headers[i].field );
544 heap_free( request->headers[i].value );
546 heap_free( request->headers );
547 heap_free( request );
550 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
553 if (str) len = strlenW( str );
554 if (buffer && *buflen > len)
556 memcpy( buffer, str, len * sizeof(WCHAR) );
559 *buflen = len * sizeof(WCHAR);
562 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
565 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
567 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
568 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
569 CertNameToStrW( encoding, blob, format, ret, size );
574 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
576 request_t *request = (request_t *)hdr;
580 case WINHTTP_OPTION_SECURITY_FLAGS:
585 if (!buffer || *buflen < sizeof(flags))
587 *buflen = sizeof(flags);
588 set_last_error( ERROR_INSUFFICIENT_BUFFER );
593 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
594 flags |= request->netconn.security_flags;
595 bits = netconn_get_cipher_strength( &request->netconn );
597 flags |= SECURITY_FLAG_STRENGTH_STRONG;
599 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
601 flags |= SECURITY_FLAG_STRENGTH_WEAK;
602 *(DWORD *)buffer = flags;
603 *buflen = sizeof(flags);
606 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
608 const CERT_CONTEXT *cert;
610 if (!buffer || *buflen < sizeof(cert))
612 *buflen = sizeof(cert);
613 set_last_error( ERROR_INSUFFICIENT_BUFFER );
617 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
618 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
619 *buflen = sizeof(cert);
622 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
624 const CERT_CONTEXT *cert;
625 const CRYPT_OID_INFO *oidInfo;
626 WINHTTP_CERTIFICATE_INFO *ci = buffer;
628 FIXME("partial stub\n");
630 if (!buffer || *buflen < sizeof(*ci))
632 *buflen = sizeof(*ci);
633 set_last_error( ERROR_INSUFFICIENT_BUFFER );
636 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
638 ci->ftExpiry = cert->pCertInfo->NotAfter;
639 ci->ftStart = cert->pCertInfo->NotBefore;
640 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
641 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
642 ci->lpszProtocolName = NULL;
643 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
644 cert->pCertInfo->SignatureAlgorithm.pszObjId,
647 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
649 ci->lpszSignatureAlgName = NULL;
650 ci->lpszEncryptionAlgName = NULL;
651 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
653 CertFreeCertificateContext( cert );
654 *buflen = sizeof(*ci);
657 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
659 if (!buffer || *buflen < sizeof(DWORD))
661 *buflen = sizeof(DWORD);
662 set_last_error( ERROR_INSUFFICIENT_BUFFER );
666 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
667 *buflen = sizeof(DWORD);
670 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
671 *(DWORD *)buffer = request->resolve_timeout;
672 *buflen = sizeof(DWORD);
674 case WINHTTP_OPTION_CONNECT_TIMEOUT:
675 *(DWORD *)buffer = request->connect_timeout;
676 *buflen = sizeof(DWORD);
678 case WINHTTP_OPTION_SEND_TIMEOUT:
679 *(DWORD *)buffer = request->send_timeout;
680 *buflen = sizeof(DWORD);
682 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
683 *(DWORD *)buffer = request->recv_timeout;
684 *buflen = sizeof(DWORD);
687 case WINHTTP_OPTION_USERNAME:
688 str_to_buffer( buffer, request->connect->username, buflen );
691 case WINHTTP_OPTION_PASSWORD:
692 str_to_buffer( buffer, request->connect->password, buflen );
695 case WINHTTP_OPTION_PROXY_USERNAME:
696 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
699 case WINHTTP_OPTION_PROXY_PASSWORD:
700 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
704 FIXME("unimplemented option %u\n", option);
705 set_last_error( ERROR_INVALID_PARAMETER );
710 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
713 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
715 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
719 set_last_error( ERROR_OUTOFMEMORY );
723 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
725 request_t *request = (request_t *)hdr;
729 case WINHTTP_OPTION_PROXY:
731 WINHTTP_PROXY_INFO *pi = buffer;
733 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
736 case WINHTTP_OPTION_DISABLE_FEATURE:
740 if (buflen != sizeof(DWORD))
742 set_last_error( ERROR_INSUFFICIENT_BUFFER );
746 disable = *(DWORD *)buffer;
747 TRACE("0x%x\n", disable);
748 hdr->disable_flags |= disable;
751 case WINHTTP_OPTION_AUTOLOGON_POLICY:
755 if (buflen != sizeof(DWORD))
757 set_last_error( ERROR_INSUFFICIENT_BUFFER );
761 policy = *(DWORD *)buffer;
762 TRACE("0x%x\n", policy);
763 hdr->logon_policy = policy;
766 case WINHTTP_OPTION_REDIRECT_POLICY:
770 if (buflen != sizeof(DWORD))
772 set_last_error( ERROR_INSUFFICIENT_BUFFER );
776 policy = *(DWORD *)buffer;
777 TRACE("0x%x\n", policy);
778 hdr->redirect_policy = policy;
781 case WINHTTP_OPTION_SECURITY_FLAGS:
785 if (buflen < sizeof(DWORD))
787 set_last_error( ERROR_INSUFFICIENT_BUFFER );
790 flags = *(DWORD *)buffer;
791 TRACE("0x%x\n", flags);
792 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
793 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
794 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
795 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
797 set_last_error( ERROR_INVALID_PARAMETER );
800 request->netconn.security_flags = flags;
803 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
804 request->resolve_timeout = *(DWORD *)buffer;
806 case WINHTTP_OPTION_CONNECT_TIMEOUT:
807 request->connect_timeout = *(DWORD *)buffer;
809 case WINHTTP_OPTION_SEND_TIMEOUT:
810 request->send_timeout = *(DWORD *)buffer;
812 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
813 request->recv_timeout = *(DWORD *)buffer;
816 case WINHTTP_OPTION_USERNAME:
818 connect_t *connect = request->connect;
820 heap_free( connect->username );
821 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
824 case WINHTTP_OPTION_PASSWORD:
826 connect_t *connect = request->connect;
828 heap_free( connect->password );
829 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
832 case WINHTTP_OPTION_PROXY_USERNAME:
834 session_t *session = request->connect->session;
836 heap_free( session->proxy_username );
837 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
840 case WINHTTP_OPTION_PROXY_PASSWORD:
842 session_t *session = request->connect->session;
844 heap_free( session->proxy_password );
845 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
849 FIXME("unimplemented option %u\n", option);
850 set_last_error( ERROR_INVALID_PARAMETER );
855 static const object_vtbl_t request_vtbl =
858 request_query_option,
862 /***********************************************************************
863 * WinHttpOpenRequest (winhttp.@)
865 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
866 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
870 HINTERNET hrequest = NULL;
872 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
873 debugstr_w(version), debugstr_w(referrer), types, flags);
875 if (!(connect = (connect_t *)grab_object( hconnect )))
877 set_last_error( ERROR_INVALID_HANDLE );
880 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
882 release_object( &connect->hdr );
883 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
886 if (!(request = heap_alloc_zero( sizeof(request_t) )))
888 release_object( &connect->hdr );
891 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
892 request->hdr.vtbl = &request_vtbl;
893 request->hdr.refs = 1;
894 request->hdr.flags = flags;
895 request->hdr.callback = connect->hdr.callback;
896 request->hdr.notify_mask = connect->hdr.notify_mask;
897 request->hdr.context = connect->hdr.context;
899 addref_object( &connect->hdr );
900 request->connect = connect;
901 list_add_head( &connect->hdr.children, &request->hdr.entry );
903 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
904 request->resolve_timeout = connect->session->resolve_timeout;
905 request->connect_timeout = connect->session->connect_timeout;
906 request->send_timeout = connect->session->send_timeout;
907 request->recv_timeout = connect->session->recv_timeout;
909 if (!verb || !verb[0]) verb = getW;
910 if (!(request->verb = strdupW( verb ))) goto end;
917 len = strlenW( object ) + 1;
918 if (object[0] != '/') len++;
919 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
921 if (object[0] != '/') *p++ = '/';
922 strcpyW( p, object );
923 request->path = path;
925 else if (!(request->path = strdupW( slashW ))) goto end;
927 if (!version || !version[0]) version = http1_1;
928 if (!(request->version = strdupW( version ))) goto end;
930 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
931 request->hdr.handle = hrequest;
933 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
936 release_object( &request->hdr );
938 TRACE("returning %p\n", hrequest);
942 /***********************************************************************
943 * WinHttpCloseHandle (winhttp.@)
945 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
947 object_header_t *hdr;
949 TRACE("%p\n", handle);
951 if (!(hdr = grab_object( handle )))
953 set_last_error( ERROR_INVALID_HANDLE );
956 release_object( hdr );
957 free_handle( handle );
961 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
967 set_last_error( ERROR_INVALID_PARAMETER );
973 case WINHTTP_OPTION_CONTEXT_VALUE:
975 if (!buffer || *buflen < sizeof(DWORD_PTR))
977 *buflen = sizeof(DWORD_PTR);
978 set_last_error( ERROR_INSUFFICIENT_BUFFER );
982 *(DWORD_PTR *)buffer = hdr->context;
983 *buflen = sizeof(DWORD_PTR);
987 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
990 FIXME("unimplemented option %u\n", option);
991 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
999 /***********************************************************************
1000 * WinHttpQueryOption (winhttp.@)
1002 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1005 object_header_t *hdr;
1007 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1009 if (!(hdr = grab_object( handle )))
1011 set_last_error( ERROR_INVALID_HANDLE );
1015 ret = query_option( hdr, option, buffer, buflen );
1017 release_object( hdr );
1021 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1027 set_last_error( ERROR_INVALID_PARAMETER );
1033 case WINHTTP_OPTION_CONTEXT_VALUE:
1035 if (buflen != sizeof(DWORD_PTR))
1037 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1041 hdr->context = *(DWORD_PTR *)buffer;
1045 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1048 FIXME("unimplemented option %u\n", option);
1049 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1057 /***********************************************************************
1058 * WinHttpSetOption (winhttp.@)
1060 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1063 object_header_t *hdr;
1065 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1067 if (!(hdr = grab_object( handle )))
1069 set_last_error( ERROR_INVALID_HANDLE );
1073 ret = set_option( hdr, option, buffer, buflen );
1075 release_object( hdr );
1079 /***********************************************************************
1080 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1082 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1084 FIXME("0x%08x, %p\n", flags, url);
1086 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1090 static const WCHAR Connections[] = {
1091 'S','o','f','t','w','a','r','e','\\',
1092 'M','i','c','r','o','s','o','f','t','\\',
1093 'W','i','n','d','o','w','s','\\',
1094 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1095 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1096 'C','o','n','n','e','c','t','i','o','n','s',0 };
1097 static const WCHAR WinHttpSettings[] = {
1098 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1099 static const DWORD WINHTTPSETTINGS_MAGIC = 0x18;
1100 static const DWORD WINHTTP_PROXY_TYPE_DIRECT = 1;
1101 static const DWORD WINHTTP_PROXY_TYPE_PROXY = 2;
1103 struct winhttp_settings_header
1106 DWORD unknown; /* always zero? */
1107 DWORD flags; /* one of WINHTTP_PROXY_TYPE_* */
1110 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1114 for (begin = src; src - begin < len; src++, dst++)
1119 /***********************************************************************
1120 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1122 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1126 BOOL got_from_reg = FALSE, direct = TRUE;
1129 TRACE("%p\n", info);
1131 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1134 DWORD type, size = 0;
1136 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1137 if (!l && type == REG_BINARY &&
1138 size >= sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD))
1140 BYTE *buf = heap_alloc( size );
1144 struct winhttp_settings_header *hdr =
1145 (struct winhttp_settings_header *)buf;
1146 DWORD *len = (DWORD *)(hdr + 1);
1148 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1150 if (!l && hdr->magic == WINHTTPSETTINGS_MAGIC &&
1153 if (hdr->flags & WINHTTP_PROXY_TYPE_PROXY)
1156 LPWSTR proxy = NULL;
1157 LPWSTR proxy_bypass = NULL;
1159 /* Sanity-check length of proxy string */
1160 if ((BYTE *)len - buf + *len <= size)
1163 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1165 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1166 len = (DWORD *)((BYTE *)(len + 1) + *len);
1170 /* Sanity-check length of proxy bypass string */
1171 if ((BYTE *)len - buf + *len <= size)
1173 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1175 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1180 GlobalFree( proxy );
1184 info->lpszProxy = proxy;
1185 info->lpszProxyBypass = proxy_bypass;
1188 got_from_reg = TRUE;
1190 info->dwAccessType =
1191 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1192 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1193 debugstr_w(info->lpszProxy),
1194 debugstr_w(info->lpszProxyBypass));
1203 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1205 char *colon, *http_proxy;
1207 if ((colon = strchr( envproxy, ':' )))
1209 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1211 static const char http[] = "http://";
1213 /* It's a scheme, check that it's http */
1214 if (!strncmp( envproxy, http, strlen( http ) ))
1215 http_proxy = envproxy + strlen( http );
1218 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1223 http_proxy = envproxy;
1226 http_proxy = envproxy;
1232 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1233 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1235 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1237 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1238 info->lpszProxy = http_proxyW;
1239 info->lpszProxyBypass = NULL;
1240 TRACE("http proxy (from environment) = %s\n",
1241 debugstr_w(info->lpszProxy));
1247 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1248 info->lpszProxy = NULL;
1249 info->lpszProxyBypass = NULL;
1254 /***********************************************************************
1255 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1257 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1259 TRACE("%p\n", config);
1263 set_last_error( ERROR_INVALID_PARAMETER );
1267 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1269 FIXME("returning no proxy used\n");
1270 config->fAutoDetect = FALSE;
1271 config->lpszAutoConfigUrl = NULL;
1272 config->lpszProxy = NULL;
1273 config->lpszProxyBypass = NULL;
1278 /***********************************************************************
1279 * WinHttpGetProxyForUrl (winhttp.@)
1281 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1282 WINHTTP_PROXY_INFO *info )
1284 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1286 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1290 /***********************************************************************
1291 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1293 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1300 TRACE("%p\n", info);
1304 set_last_error( ERROR_INVALID_PARAMETER );
1307 switch (info->dwAccessType)
1309 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1311 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1312 if (!info->lpszProxy)
1314 set_last_error( ERROR_INVALID_PARAMETER );
1317 /* Only ASCII characters are allowed */
1318 for (src = info->lpszProxy; *src; src++)
1321 set_last_error( ERROR_INVALID_PARAMETER );
1324 if (info->lpszProxyBypass)
1326 for (src = info->lpszProxyBypass; *src; src++)
1329 set_last_error( ERROR_INVALID_PARAMETER );
1335 set_last_error( ERROR_INVALID_PARAMETER );
1339 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1340 KEY_WRITE, NULL, &key, NULL );
1343 DWORD size = sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD);
1346 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1348 size += strlenW( info->lpszProxy );
1349 if (info->lpszProxyBypass)
1350 size += strlenW( info->lpszProxyBypass );
1352 buf = heap_alloc( size );
1355 struct winhttp_settings_header *hdr =
1356 (struct winhttp_settings_header *)buf;
1357 DWORD *len = (DWORD *)(hdr + 1);
1359 hdr->magic = WINHTTPSETTINGS_MAGIC;
1361 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1365 hdr->flags = WINHTTP_PROXY_TYPE_PROXY;
1366 *len++ = strlenW( info->lpszProxy );
1367 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1371 if (info->lpszProxyBypass)
1373 *len++ = strlenW( info->lpszProxyBypass );
1374 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1383 hdr->flags = WINHTTP_PROXY_TYPE_DIRECT;
1387 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1397 /***********************************************************************
1398 * WinHttpSetStatusCallback (winhttp.@)
1400 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1401 DWORD flags, DWORD_PTR reserved )
1403 object_header_t *hdr;
1404 WINHTTP_STATUS_CALLBACK ret;
1406 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1408 if (!(hdr = grab_object( handle )))
1410 set_last_error( ERROR_INVALID_HANDLE );
1411 return WINHTTP_INVALID_STATUS_CALLBACK;
1413 ret = hdr->callback;
1414 hdr->callback = callback;
1415 hdr->notify_mask = flags;
1417 release_object( hdr );
1421 /***********************************************************************
1422 * WinHttpSetTimeouts (winhttp.@)
1424 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1427 object_header_t *hdr;
1431 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1433 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1435 set_last_error( ERROR_INVALID_PARAMETER );
1439 if (!(hdr = grab_object( handle )))
1441 set_last_error( ERROR_INVALID_HANDLE );
1447 case WINHTTP_HANDLE_TYPE_REQUEST:
1448 request = (request_t *)hdr;
1449 request->connect_timeout = connect;
1451 if (resolve < 0) resolve = 0;
1452 request->resolve_timeout = resolve;
1454 if (send < 0) send = 0;
1455 request->send_timeout = send;
1457 if (receive < 0) receive = 0;
1458 request->recv_timeout = receive;
1460 if (netconn_connected( &request->netconn ))
1462 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1463 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1466 release_object( &request->hdr );
1469 case WINHTTP_HANDLE_TYPE_SESSION:
1470 session = (session_t *)hdr;
1471 session->connect_timeout = connect;
1473 if (resolve < 0) resolve = 0;
1474 session->resolve_timeout = resolve;
1476 if (send < 0) send = 0;
1477 session->send_timeout = send;
1479 if (receive < 0) receive = 0;
1480 session->recv_timeout = receive;
1484 release_object( hdr );
1485 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1491 static const WCHAR wkday[7][4] =
1492 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1493 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1494 static const WCHAR month[12][4] =
1495 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1496 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1497 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1499 /***********************************************************************
1500 * WinHttpTimeFromSystemTime (WININET.@)
1502 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
1504 static const WCHAR format[] =
1505 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1506 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1508 TRACE("%p, %p\n", time, string);
1510 if (!time || !string) return FALSE;
1512 sprintfW( string, format,
1513 wkday[time->wDayOfWeek],
1515 month[time->wMonth - 1],
1524 /***********************************************************************
1525 * WinHttpTimeToSystemTime (WININET.@)
1527 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
1530 const WCHAR *s = string;
1533 TRACE("%s, %p\n", debugstr_w(string), time);
1535 if (!string || !time) return FALSE;
1537 /* Windows does this too */
1538 GetSystemTime( time );
1540 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1541 * a SYSTEMTIME structure.
1544 while (*s && !isalphaW( *s )) s++;
1545 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1546 time->wDayOfWeek = 7;
1548 for (i = 0; i < 7; i++)
1550 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
1551 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
1552 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
1554 time->wDayOfWeek = i;
1559 if (time->wDayOfWeek > 6) return TRUE;
1560 while (*s && !isdigitW( *s )) s++;
1561 time->wDay = strtolW( s, &end, 10 );
1564 while (*s && !isalphaW( *s )) s++;
1565 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1568 for (i = 0; i < 12; i++)
1570 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
1571 toupperW( month[i][1]) == toupperW( s[1] ) &&
1572 toupperW( month[i][2]) == toupperW( s[2] ) )
1574 time->wMonth = i + 1;
1578 if (time->wMonth == 0) return TRUE;
1580 while (*s && !isdigitW( *s )) s++;
1581 if (*s == '\0') return TRUE;
1582 time->wYear = strtolW( s, &end, 10 );
1585 while (*s && !isdigitW( *s )) s++;
1586 if (*s == '\0') return TRUE;
1587 time->wHour = strtolW( s, &end, 10 );
1590 while (*s && !isdigitW( *s )) s++;
1591 if (*s == '\0') return TRUE;
1592 time->wMinute = strtolW( s, &end, 10 );
1595 while (*s && !isdigitW( *s )) s++;
1596 if (*s == '\0') return TRUE;
1597 time->wSecond = strtolW( s, &end, 10 );
1600 time->wMilliseconds = 0;