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_CONNECT_TIMEOUT 20000
37 #define DEFAULT_SEND_TIMEOUT 30000
38 #define DEFAULT_RECEIVE_TIMEOUT 30000
40 void set_last_error( DWORD error )
43 SetLastError( error );
46 DWORD get_last_error( void )
49 return GetLastError();
52 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
54 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
56 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
59 /***********************************************************************
60 * WinHttpCheckPlatform (winhttp.@)
62 BOOL WINAPI WinHttpCheckPlatform( void )
68 /***********************************************************************
69 * session_destroy (internal)
71 static void session_destroy( object_header_t *hdr )
73 session_t *session = (session_t *)hdr;
74 struct list *item, *next;
77 TRACE("%p\n", session);
79 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
81 domain = LIST_ENTRY( item, domain_t, entry );
82 delete_domain( domain );
84 heap_free( session->agent );
85 heap_free( session->proxy_server );
86 heap_free( session->proxy_bypass );
87 heap_free( session->proxy_username );
88 heap_free( session->proxy_password );
92 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
94 session_t *session = (session_t *)hdr;
98 case WINHTTP_OPTION_REDIRECT_POLICY:
100 if (!buffer || *buflen < sizeof(DWORD))
102 *buflen = sizeof(DWORD);
103 set_last_error( ERROR_INSUFFICIENT_BUFFER );
107 *(DWORD *)buffer = hdr->redirect_policy;
108 *buflen = sizeof(DWORD);
111 case WINHTTP_OPTION_CONNECT_TIMEOUT:
112 *(DWORD *)buffer = session->connect_timeout;
113 *buflen = sizeof(DWORD);
115 case WINHTTP_OPTION_SEND_TIMEOUT:
116 *(DWORD *)buffer = session->send_timeout;
117 *buflen = sizeof(DWORD);
119 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
120 *(DWORD *)buffer = session->recv_timeout;
121 *buflen = sizeof(DWORD);
124 FIXME("unimplemented option %u\n", option);
125 set_last_error( ERROR_INVALID_PARAMETER );
130 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
132 session_t *session = (session_t *)hdr;
136 case WINHTTP_OPTION_PROXY:
138 WINHTTP_PROXY_INFO *pi = buffer;
140 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
143 case WINHTTP_OPTION_REDIRECT_POLICY:
147 if (buflen != sizeof(policy))
149 set_last_error( ERROR_INSUFFICIENT_BUFFER );
153 policy = *(DWORD *)buffer;
154 TRACE("0x%x\n", policy);
155 hdr->redirect_policy = policy;
158 case WINHTTP_OPTION_DISABLE_FEATURE:
159 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
161 case WINHTTP_OPTION_CONNECT_TIMEOUT:
162 session->connect_timeout = *(DWORD *)buffer;
164 case WINHTTP_OPTION_SEND_TIMEOUT:
165 session->send_timeout = *(DWORD *)buffer;
167 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
168 session->recv_timeout = *(DWORD *)buffer;
171 FIXME("unimplemented option %u\n", option);
172 set_last_error( ERROR_INVALID_PARAMETER );
177 static const object_vtbl_t session_vtbl =
180 session_query_option,
184 /***********************************************************************
185 * WinHttpOpen (winhttp.@)
187 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
190 HINTERNET handle = NULL;
192 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
194 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
196 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
197 session->hdr.vtbl = &session_vtbl;
198 session->hdr.flags = flags;
199 session->hdr.refs = 1;
200 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
201 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
202 session->send_timeout = DEFAULT_SEND_TIMEOUT;
203 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
204 list_init( &session->cookie_cache );
206 if (agent && !(session->agent = strdupW( agent ))) goto end;
207 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
209 WINHTTP_PROXY_INFO info;
211 WinHttpGetDefaultProxyConfiguration( &info );
212 session->access = info.dwAccessType;
213 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
215 GlobalFree( (LPWSTR)info.lpszProxy );
216 GlobalFree( (LPWSTR)info.lpszProxyBypass );
219 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
221 GlobalFree( (LPWSTR)info.lpszProxy );
222 GlobalFree( (LPWSTR)info.lpszProxyBypass );
226 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
228 session->access = access;
229 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
230 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
233 if (!(handle = alloc_handle( &session->hdr ))) goto end;
234 session->hdr.handle = handle;
237 release_object( &session->hdr );
238 TRACE("returning %p\n", handle);
242 /***********************************************************************
243 * connect_destroy (internal)
245 static void connect_destroy( object_header_t *hdr )
247 connect_t *connect = (connect_t *)hdr;
249 TRACE("%p\n", connect);
251 release_object( &connect->session->hdr );
253 heap_free( connect->hostname );
254 heap_free( connect->servername );
255 heap_free( connect->username );
256 heap_free( connect->password );
257 heap_free( connect );
260 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
262 connect_t *connect = (connect_t *)hdr;
266 case WINHTTP_OPTION_PARENT_HANDLE:
268 if (!buffer || *buflen < sizeof(HINTERNET))
270 *buflen = sizeof(HINTERNET);
271 set_last_error( ERROR_INSUFFICIENT_BUFFER );
275 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
276 *buflen = sizeof(HINTERNET);
279 case WINHTTP_OPTION_CONNECT_TIMEOUT:
280 *(DWORD *)buffer = connect->session->connect_timeout;
281 *buflen = sizeof(DWORD);
283 case WINHTTP_OPTION_SEND_TIMEOUT:
284 *(DWORD *)buffer = connect->session->send_timeout;
285 *buflen = sizeof(DWORD);
287 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
288 *(DWORD *)buffer = connect->session->recv_timeout;
289 *buflen = sizeof(DWORD);
292 FIXME("unimplemented option %u\n", option);
293 set_last_error( ERROR_INVALID_PARAMETER );
298 static const object_vtbl_t connect_vtbl =
301 connect_query_option,
305 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
307 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
310 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
312 else if (*domain == '*')
314 if (domain[1] == '.')
318 /* For a hostname to match a wildcard, the last domain must match
319 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
320 * hostname is www.foo.a.b, it matches, but a.b does not.
322 dot = strchrW( server, '.' );
325 int len = strlenW( dot + 1 );
327 if (len > strlenW( domain + 2 ))
331 /* The server's domain is longer than the wildcard, so it
332 * could be a subdomain. Compare the last portion of the
335 ptr = dot + len + 1 - strlenW( domain + 2 );
336 if (!strcmpiW( ptr, domain + 2 ))
338 /* This is only a match if the preceding character is
339 * a '.', i.e. that it is a matching domain. E.g.
340 * if domain is '*.b.c' and server is 'www.ab.c' they
343 ret = *(ptr - 1) == '.';
347 ret = !strcmpiW( dot + 1, domain + 2 );
352 ret = !strcmpiW( server, domain );
356 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
357 #define MAX_HOST_NAME_LENGTH 256
359 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
364 if (!session->proxy_bypass) return FALSE;
365 ptr = session->proxy_bypass;
369 ptr = strchrW( ptr, ';' );
371 ptr = strchrW( tmp, ' ' );
374 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
376 WCHAR domain[MAX_HOST_NAME_LENGTH];
378 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
379 domain[ptr - tmp] = 0;
380 ret = domain_matches( server, domain );
385 ret = domain_matches( server, tmp );
386 } while (ptr && !ret);
390 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
392 session_t *session = connect->session;
395 if (session->proxy_server && !should_bypass_proxy(session, server))
399 if ((colon = strchrW( session->proxy_server, ':' )))
401 if (!connect->servername || strncmpiW( connect->servername,
402 session->proxy_server, colon - session->proxy_server - 1 ))
404 heap_free( connect->servername );
405 if (!(connect->servername = heap_alloc(
406 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
411 memcpy( connect->servername, session->proxy_server,
412 (colon - session->proxy_server) * sizeof(WCHAR) );
413 connect->servername[colon - session->proxy_server] = 0;
415 connect->serverport = atoiW( colon + 1 );
417 connect->serverport = INTERNET_DEFAULT_PORT;
422 if (!connect->servername || strcmpiW( connect->servername,
423 session->proxy_server ))
425 heap_free( connect->servername );
426 if (!(connect->servername = strdupW( session->proxy_server )))
431 connect->serverport = INTERNET_DEFAULT_PORT;
437 heap_free( connect->servername );
438 if (!(connect->servername = strdupW( server )))
443 connect->serverport = port;
449 /***********************************************************************
450 * WinHttpConnect (winhttp.@)
452 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
456 HINTERNET hconnect = NULL;
458 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
462 set_last_error( ERROR_INVALID_PARAMETER );
465 if (!(session = (session_t *)grab_object( hsession )))
467 set_last_error( ERROR_INVALID_HANDLE );
470 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
472 release_object( &session->hdr );
473 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
476 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
478 release_object( &session->hdr );
481 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
482 connect->hdr.vtbl = &connect_vtbl;
483 connect->hdr.refs = 1;
484 connect->hdr.flags = session->hdr.flags;
485 connect->hdr.callback = session->hdr.callback;
486 connect->hdr.notify_mask = session->hdr.notify_mask;
487 connect->hdr.context = session->hdr.context;
489 addref_object( &session->hdr );
490 connect->session = session;
491 list_add_head( &session->hdr.children, &connect->hdr.entry );
493 if (server && !(connect->hostname = strdupW( server ))) goto end;
494 connect->hostport = port;
496 if (!set_server_for_hostname( connect, server, port ))
499 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
500 connect->hdr.handle = hconnect;
502 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
505 release_object( &connect->hdr );
507 TRACE("returning %p\n", hconnect);
511 /***********************************************************************
512 * request_destroy (internal)
514 static void request_destroy( object_header_t *hdr )
516 request_t *request = (request_t *)hdr;
519 TRACE("%p\n", request);
521 release_object( &request->connect->hdr );
523 heap_free( request->verb );
524 heap_free( request->path );
525 heap_free( request->version );
526 heap_free( request->raw_headers );
527 heap_free( request->status_text );
528 for (i = 0; i < request->num_headers; i++)
530 heap_free( request->headers[i].field );
531 heap_free( request->headers[i].value );
533 heap_free( request->headers );
534 heap_free( request );
537 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
539 request_t *request = (request_t *)hdr;
543 case WINHTTP_OPTION_SECURITY_FLAGS:
547 if (!buffer || *buflen < sizeof(flags))
549 *buflen = sizeof(flags);
550 set_last_error( ERROR_INSUFFICIENT_BUFFER );
555 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
556 *(DWORD *)buffer = flags;
557 *buflen = sizeof(flags);
560 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
562 const CERT_CONTEXT *cert;
564 if (!buffer || *buflen < sizeof(cert))
566 *buflen = sizeof(cert);
567 set_last_error( ERROR_INSUFFICIENT_BUFFER );
571 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
572 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
573 *buflen = sizeof(cert);
576 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
578 if (!buffer || *buflen < sizeof(DWORD))
580 *buflen = sizeof(DWORD);
581 set_last_error( ERROR_INSUFFICIENT_BUFFER );
585 *(DWORD *)buffer = 128; /* FIXME */
586 *buflen = sizeof(DWORD);
589 case WINHTTP_OPTION_CONNECT_TIMEOUT:
590 *(DWORD *)buffer = request->connect_timeout;
591 *buflen = sizeof(DWORD);
593 case WINHTTP_OPTION_SEND_TIMEOUT:
594 *(DWORD *)buffer = request->send_timeout;
595 *buflen = sizeof(DWORD);
597 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
598 *(DWORD *)buffer = request->recv_timeout;
599 *buflen = sizeof(DWORD);
602 FIXME("unimplemented option %u\n", option);
603 set_last_error( ERROR_INVALID_PARAMETER );
608 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
610 request_t *request = (request_t *)hdr;
614 case WINHTTP_OPTION_PROXY:
616 WINHTTP_PROXY_INFO *pi = buffer;
618 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
621 case WINHTTP_OPTION_DISABLE_FEATURE:
625 if (buflen != sizeof(DWORD))
627 set_last_error( ERROR_INSUFFICIENT_BUFFER );
631 disable = *(DWORD *)buffer;
632 TRACE("0x%x\n", disable);
633 hdr->disable_flags |= disable;
636 case WINHTTP_OPTION_AUTOLOGON_POLICY:
640 if (buflen != sizeof(DWORD))
642 set_last_error( ERROR_INSUFFICIENT_BUFFER );
646 policy = *(DWORD *)buffer;
647 TRACE("0x%x\n", policy);
648 hdr->logon_policy = policy;
651 case WINHTTP_OPTION_REDIRECT_POLICY:
655 if (buflen != sizeof(DWORD))
657 set_last_error( ERROR_INSUFFICIENT_BUFFER );
661 policy = *(DWORD *)buffer;
662 TRACE("0x%x\n", policy);
663 hdr->redirect_policy = policy;
666 case WINHTTP_OPTION_SECURITY_FLAGS:
667 FIXME("WINHTTP_OPTION_SECURITY_FLAGS unimplemented (%08x)\n",
670 case WINHTTP_OPTION_CONNECT_TIMEOUT:
671 request->connect_timeout = *(DWORD *)buffer;
673 case WINHTTP_OPTION_SEND_TIMEOUT:
674 request->send_timeout = *(DWORD *)buffer;
676 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
677 request->recv_timeout = *(DWORD *)buffer;
680 FIXME("unimplemented option %u\n", option);
681 set_last_error( ERROR_INVALID_PARAMETER );
686 static const object_vtbl_t request_vtbl =
689 request_query_option,
693 /***********************************************************************
694 * WinHttpOpenRequest (winhttp.@)
696 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
697 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
701 HINTERNET hrequest = NULL;
703 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
704 debugstr_w(version), debugstr_w(referrer), types, flags);
706 if (!(connect = (connect_t *)grab_object( hconnect )))
708 set_last_error( ERROR_INVALID_HANDLE );
711 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
713 release_object( &connect->hdr );
714 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
717 if (!(request = heap_alloc_zero( sizeof(request_t) )))
719 release_object( &connect->hdr );
722 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
723 request->hdr.vtbl = &request_vtbl;
724 request->hdr.refs = 1;
725 request->hdr.flags = flags;
726 request->hdr.callback = connect->hdr.callback;
727 request->hdr.notify_mask = connect->hdr.notify_mask;
728 request->hdr.context = connect->hdr.context;
730 addref_object( &connect->hdr );
731 request->connect = connect;
732 list_add_head( &connect->hdr.children, &request->hdr.entry );
734 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
735 request->connect_timeout = connect->session->connect_timeout;
736 request->send_timeout = connect->session->send_timeout;
737 request->recv_timeout = connect->session->recv_timeout;
739 if (!verb || !verb[0]) verb = getW;
740 if (!(request->verb = strdupW( verb ))) goto end;
747 len = strlenW( object ) + 1;
748 if (object[0] != '/') len++;
749 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
751 if (object[0] != '/') *p++ = '/';
752 strcpyW( p, object );
753 request->path = path;
755 else if (!(request->path = strdupW( slashW ))) goto end;
757 if (!version || !version[0]) version = http1_1;
758 if (!(request->version = strdupW( version ))) goto end;
760 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
761 request->hdr.handle = hrequest;
763 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
766 release_object( &request->hdr );
768 TRACE("returning %p\n", hrequest);
772 /***********************************************************************
773 * WinHttpCloseHandle (winhttp.@)
775 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
777 object_header_t *hdr;
779 TRACE("%p\n", handle);
781 if (!(hdr = grab_object( handle )))
783 set_last_error( ERROR_INVALID_HANDLE );
786 release_object( hdr );
787 free_handle( handle );
791 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
797 set_last_error( ERROR_INVALID_PARAMETER );
803 case WINHTTP_OPTION_CONTEXT_VALUE:
805 if (!buffer || *buflen < sizeof(DWORD_PTR))
807 *buflen = sizeof(DWORD_PTR);
808 set_last_error( ERROR_INSUFFICIENT_BUFFER );
812 *(DWORD_PTR *)buffer = hdr->context;
813 *buflen = sizeof(DWORD_PTR);
817 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
820 FIXME("unimplemented option %u\n", option);
821 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
829 /***********************************************************************
830 * WinHttpQueryOption (winhttp.@)
832 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
835 object_header_t *hdr;
837 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
839 if (!(hdr = grab_object( handle )))
841 set_last_error( ERROR_INVALID_HANDLE );
845 ret = query_option( hdr, option, buffer, buflen );
847 release_object( hdr );
851 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
857 set_last_error( ERROR_INVALID_PARAMETER );
863 case WINHTTP_OPTION_CONTEXT_VALUE:
865 if (buflen != sizeof(DWORD_PTR))
867 set_last_error( ERROR_INSUFFICIENT_BUFFER );
871 hdr->context = *(DWORD_PTR *)buffer;
875 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
878 FIXME("unimplemented option %u\n", option);
879 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
887 /***********************************************************************
888 * WinHttpSetOption (winhttp.@)
890 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
893 object_header_t *hdr;
895 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
897 if (!(hdr = grab_object( handle )))
899 set_last_error( ERROR_INVALID_HANDLE );
903 ret = set_option( hdr, option, buffer, buflen );
905 release_object( hdr );
909 /***********************************************************************
910 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
912 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
914 FIXME("0x%08x, %p\n", flags, url);
916 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
920 static const WCHAR Connections[] = {
921 'S','o','f','t','w','a','r','e','\\',
922 'M','i','c','r','o','s','o','f','t','\\',
923 'W','i','n','d','o','w','s','\\',
924 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
925 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
926 'C','o','n','n','e','c','t','i','o','n','s',0 };
927 static const WCHAR WinHttpSettings[] = {
928 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
929 static const DWORD WINHTTPSETTINGS_MAGIC = 0x18;
930 static const DWORD WINHTTP_PROXY_TYPE_DIRECT = 1;
931 static const DWORD WINHTTP_PROXY_TYPE_PROXY = 2;
933 struct winhttp_settings_header
936 DWORD unknown; /* always zero? */
937 DWORD flags; /* one of WINHTTP_PROXY_TYPE_* */
940 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
944 for (begin = src; src - begin < len; src++, dst++)
949 /***********************************************************************
950 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
952 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
956 BOOL got_from_reg = FALSE, direct = TRUE;
961 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
964 DWORD type, size = 0;
966 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
967 if (!l && type == REG_BINARY &&
968 size >= sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD))
970 BYTE *buf = heap_alloc( size );
974 struct winhttp_settings_header *hdr =
975 (struct winhttp_settings_header *)buf;
976 DWORD *len = (DWORD *)(hdr + 1);
978 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
980 if (!l && hdr->magic == WINHTTPSETTINGS_MAGIC &&
983 if (hdr->flags & WINHTTP_PROXY_TYPE_PROXY)
987 LPWSTR proxy_bypass = NULL;
989 /* Sanity-check length of proxy string */
990 if ((BYTE *)len - buf + *len <= size)
993 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
995 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
996 len = (DWORD *)((BYTE *)(len + 1) + *len);
1000 /* Sanity-check length of proxy bypass string */
1001 if ((BYTE *)len - buf + *len <= size)
1003 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1005 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1010 GlobalFree( proxy );
1014 info->lpszProxy = proxy;
1015 info->lpszProxyBypass = proxy_bypass;
1018 got_from_reg = TRUE;
1020 info->dwAccessType =
1021 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1022 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1023 debugstr_w(info->lpszProxy),
1024 debugstr_w(info->lpszProxyBypass));
1033 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1035 char *colon, *http_proxy;
1037 if ((colon = strchr( envproxy, ':' )))
1039 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1041 static const char http[] = "http://";
1043 /* It's a scheme, check that it's http */
1044 if (!strncmp( envproxy, http, strlen( http ) ))
1045 http_proxy = envproxy + strlen( http );
1048 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1053 http_proxy = envproxy;
1056 http_proxy = envproxy;
1062 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1063 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1065 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1067 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1068 info->lpszProxy = http_proxyW;
1069 info->lpszProxyBypass = NULL;
1070 TRACE("http proxy (from environment) = %s\n",
1071 debugstr_w(info->lpszProxy));
1077 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1078 info->lpszProxy = NULL;
1079 info->lpszProxyBypass = NULL;
1084 /***********************************************************************
1085 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1087 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1089 TRACE("%p\n", config);
1093 set_last_error( ERROR_INVALID_PARAMETER );
1097 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1099 FIXME("returning no proxy used\n");
1100 config->fAutoDetect = FALSE;
1101 config->lpszAutoConfigUrl = NULL;
1102 config->lpszProxy = NULL;
1103 config->lpszProxyBypass = NULL;
1108 /***********************************************************************
1109 * WinHttpGetProxyForUrl (winhttp.@)
1111 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1112 WINHTTP_PROXY_INFO *info )
1114 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1116 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1120 /***********************************************************************
1121 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1123 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1130 TRACE("%p\n", info);
1134 set_last_error( ERROR_INVALID_PARAMETER );
1137 switch (info->dwAccessType)
1139 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1141 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1142 if (!info->lpszProxy)
1144 set_last_error( ERROR_INVALID_PARAMETER );
1147 /* Only ASCII characters are allowed */
1148 for (src = info->lpszProxy; *src; src++)
1151 set_last_error( ERROR_INVALID_PARAMETER );
1154 if (info->lpszProxyBypass)
1156 for (src = info->lpszProxyBypass; *src; src++)
1159 set_last_error( ERROR_INVALID_PARAMETER );
1165 set_last_error( ERROR_INVALID_PARAMETER );
1169 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1170 KEY_WRITE, NULL, &key, NULL );
1173 DWORD size = sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD);
1176 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1178 size += strlenW( info->lpszProxy );
1179 if (info->lpszProxyBypass)
1180 size += strlenW( info->lpszProxyBypass );
1182 buf = heap_alloc( size );
1185 struct winhttp_settings_header *hdr =
1186 (struct winhttp_settings_header *)buf;
1187 DWORD *len = (DWORD *)(hdr + 1);
1189 hdr->magic = WINHTTPSETTINGS_MAGIC;
1191 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1195 hdr->flags = WINHTTP_PROXY_TYPE_PROXY;
1196 *len++ = strlenW( info->lpszProxy );
1197 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1201 if (info->lpszProxyBypass)
1203 *len++ = strlenW( info->lpszProxyBypass );
1204 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1213 hdr->flags = WINHTTP_PROXY_TYPE_DIRECT;
1217 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1227 /***********************************************************************
1228 * WinHttpSetStatusCallback (winhttp.@)
1230 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1231 DWORD flags, DWORD_PTR reserved )
1233 object_header_t *hdr;
1234 WINHTTP_STATUS_CALLBACK ret;
1236 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1238 if (!(hdr = grab_object( handle )))
1240 set_last_error( ERROR_INVALID_HANDLE );
1241 return WINHTTP_INVALID_STATUS_CALLBACK;
1243 ret = hdr->callback;
1244 hdr->callback = callback;
1245 hdr->notify_mask = flags;
1247 release_object( hdr );
1251 /***********************************************************************
1252 * WinHttpSetTimeouts (winhttp.@)
1254 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1257 object_header_t *hdr;
1261 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1263 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1265 set_last_error( ERROR_INVALID_PARAMETER );
1270 FIXME("resolve timeout (%d) not supported\n", resolve);
1272 if (!(hdr = grab_object( handle )))
1274 set_last_error( ERROR_INVALID_HANDLE );
1280 case WINHTTP_HANDLE_TYPE_REQUEST:
1281 request = (request_t *)hdr;
1282 request->connect_timeout = connect;
1284 if (send < 0) send = 0;
1285 request->send_timeout = send;
1287 if (receive < 0) receive = 0;
1288 request->recv_timeout = receive;
1290 if (netconn_connected( &request->netconn ))
1292 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1293 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1296 release_object( &request->hdr );
1299 case WINHTTP_HANDLE_TYPE_SESSION:
1300 session = (session_t *)hdr;
1301 session->connect_timeout = connect;
1303 if (send < 0) send = 0;
1304 session->send_timeout = send;
1306 if (receive < 0) receive = 0;
1307 session->recv_timeout = receive;
1311 release_object( hdr );
1312 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1318 static const WCHAR wkday[7][4] =
1319 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1320 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1321 static const WCHAR month[12][4] =
1322 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1323 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1324 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1326 /***********************************************************************
1327 * WinHttpTimeFromSystemTime (WININET.@)
1329 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
1331 static const WCHAR format[] =
1332 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1333 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1335 TRACE("%p, %p\n", time, string);
1337 if (!time || !string) return FALSE;
1339 sprintfW( string, format,
1340 wkday[time->wDayOfWeek],
1342 month[time->wMonth - 1],
1351 /***********************************************************************
1352 * WinHttpTimeToSystemTime (WININET.@)
1354 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
1357 const WCHAR *s = string;
1360 TRACE("%s, %p\n", debugstr_w(string), time);
1362 if (!string || !time) return FALSE;
1364 /* Windows does this too */
1365 GetSystemTime( time );
1367 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1368 * a SYSTEMTIME structure.
1371 while (*s && !isalphaW( *s )) s++;
1372 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1373 time->wDayOfWeek = 7;
1375 for (i = 0; i < 7; i++)
1377 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
1378 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
1379 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
1381 time->wDayOfWeek = i;
1386 if (time->wDayOfWeek > 6) return TRUE;
1387 while (*s && !isdigitW( *s )) s++;
1388 time->wDay = strtolW( s, &end, 10 );
1391 while (*s && !isalphaW( *s )) s++;
1392 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1395 for (i = 0; i < 12; i++)
1397 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
1398 toupperW( month[i][1]) == toupperW( s[1] ) &&
1399 toupperW( month[i][2]) == toupperW( s[2] ) )
1401 time->wMonth = i + 1;
1405 if (time->wMonth == 0) return TRUE;
1407 while (*s && !isdigitW( *s )) s++;
1408 if (*s == '\0') return TRUE;
1409 time->wYear = strtolW( s, &end, 10 );
1412 while (*s && !isdigitW( *s )) s++;
1413 if (*s == '\0') return TRUE;
1414 time->wHour = strtolW( s, &end, 10 );
1417 while (*s && !isdigitW( *s )) s++;
1418 if (*s == '\0') return TRUE;
1419 time->wMinute = strtolW( s, &end, 10 );
1422 while (*s && !isdigitW( *s )) s++;
1423 if (*s == '\0') return TRUE;
1424 time->wSecond = strtolW( s, &end, 10 );
1427 time->wMilliseconds = 0;