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 60000
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 )
96 case WINHTTP_OPTION_REDIRECT_POLICY:
98 if (!buffer || *buflen < sizeof(DWORD))
100 *buflen = sizeof(DWORD);
101 set_last_error( ERROR_INSUFFICIENT_BUFFER );
105 *(DWORD *)buffer = hdr->redirect_policy;
106 *buflen = sizeof(DWORD);
110 FIXME("unimplemented option %u\n", option);
111 set_last_error( ERROR_INVALID_PARAMETER );
116 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
120 case WINHTTP_OPTION_PROXY:
122 WINHTTP_PROXY_INFO *pi = buffer;
124 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
127 case WINHTTP_OPTION_REDIRECT_POLICY:
131 if (buflen != sizeof(policy))
133 set_last_error( ERROR_INSUFFICIENT_BUFFER );
137 policy = *(DWORD *)buffer;
138 TRACE("0x%x\n", policy);
139 hdr->redirect_policy = policy;
142 case WINHTTP_OPTION_DISABLE_FEATURE:
143 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
146 FIXME("unimplemented option %u\n", option);
147 set_last_error( ERROR_INVALID_PARAMETER );
152 static const object_vtbl_t session_vtbl =
155 session_query_option,
159 /***********************************************************************
160 * WinHttpOpen (winhttp.@)
162 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
165 HINTERNET handle = NULL;
167 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
169 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
171 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
172 session->hdr.vtbl = &session_vtbl;
173 session->hdr.flags = flags;
174 session->hdr.refs = 1;
175 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
176 list_init( &session->cookie_cache );
178 if (agent && !(session->agent = strdupW( agent ))) goto end;
179 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
181 WINHTTP_PROXY_INFO info;
183 WinHttpGetDefaultProxyConfiguration( &info );
184 session->access = info.dwAccessType;
185 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
187 GlobalFree( (LPWSTR)info.lpszProxy );
188 GlobalFree( (LPWSTR)info.lpszProxyBypass );
191 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
193 GlobalFree( (LPWSTR)info.lpszProxy );
194 GlobalFree( (LPWSTR)info.lpszProxyBypass );
198 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
200 session->access = access;
201 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
202 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
205 if (!(handle = alloc_handle( &session->hdr ))) goto end;
206 session->hdr.handle = handle;
209 release_object( &session->hdr );
210 TRACE("returning %p\n", handle);
214 /***********************************************************************
215 * connect_destroy (internal)
217 static void connect_destroy( object_header_t *hdr )
219 connect_t *connect = (connect_t *)hdr;
221 TRACE("%p\n", connect);
223 release_object( &connect->session->hdr );
225 heap_free( connect->hostname );
226 heap_free( connect->servername );
227 heap_free( connect->username );
228 heap_free( connect->password );
229 heap_free( connect );
232 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
234 connect_t *connect = (connect_t *)hdr;
238 case WINHTTP_OPTION_PARENT_HANDLE:
240 if (!buffer || *buflen < sizeof(HINTERNET))
242 *buflen = sizeof(HINTERNET);
243 set_last_error( ERROR_INSUFFICIENT_BUFFER );
247 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
248 *buflen = sizeof(HINTERNET);
252 FIXME("unimplemented option %u\n", option);
253 set_last_error( ERROR_INVALID_PARAMETER );
258 static const object_vtbl_t connect_vtbl =
261 connect_query_option,
265 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
267 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
270 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
272 else if (*domain == '*')
274 if (domain[1] == '.')
278 /* For a hostname to match a wildcard, the last domain must match
279 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
280 * hostname is www.foo.a.b, it matches, but a.b does not.
282 dot = strchrW( server, '.' );
285 int len = strlenW( dot + 1 );
287 if (len > strlenW( domain + 2 ))
291 /* The server's domain is longer than the wildcard, so it
292 * could be a subdomain. Compare the last portion of the
295 ptr = dot + len + 1 - strlenW( domain + 2 );
296 if (!strcmpiW( ptr, domain + 2 ))
298 /* This is only a match if the preceding character is
299 * a '.', i.e. that it is a matching domain. E.g.
300 * if domain is '*.b.c' and server is 'www.ab.c' they
303 ret = *(ptr - 1) == '.';
307 ret = !strcmpiW( dot + 1, domain + 2 );
312 ret = !strcmpiW( server, domain );
316 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
317 #define MAX_HOST_NAME_LENGTH 256
319 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
324 if (!session->proxy_bypass) return FALSE;
325 ptr = session->proxy_bypass;
329 ptr = strchrW( ptr, ';' );
331 ptr = strchrW( tmp, ' ' );
334 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
336 WCHAR domain[MAX_HOST_NAME_LENGTH];
338 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
339 domain[ptr - tmp] = 0;
340 ret = domain_matches( server, domain );
345 ret = domain_matches( server, tmp );
346 } while (ptr && !ret);
350 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
352 session_t *session = connect->session;
355 if (session->proxy_server && !should_bypass_proxy(session, server))
359 if ((colon = strchrW( session->proxy_server, ':' )))
361 if (!connect->servername || strncmpiW( connect->servername,
362 session->proxy_server, colon - session->proxy_server - 1 ))
364 heap_free( connect->servername );
365 if (!(connect->servername = heap_alloc(
366 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
371 memcpy( connect->servername, session->proxy_server,
372 (colon - session->proxy_server) * sizeof(WCHAR) );
373 connect->servername[colon - session->proxy_server] = 0;
375 connect->serverport = atoiW( colon + 1 );
377 connect->serverport = INTERNET_DEFAULT_PORT;
382 if (!connect->servername || strcmpiW( connect->servername,
383 session->proxy_server ))
385 heap_free( connect->servername );
386 if (!(connect->servername = strdupW( session->proxy_server )))
391 connect->serverport = INTERNET_DEFAULT_PORT;
397 heap_free( connect->servername );
398 if (!(connect->servername = strdupW( server )))
403 connect->serverport = port;
409 /***********************************************************************
410 * WinHttpConnect (winhttp.@)
412 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
416 HINTERNET hconnect = NULL;
418 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
422 set_last_error( ERROR_INVALID_PARAMETER );
425 if (!(session = (session_t *)grab_object( hsession )))
427 set_last_error( ERROR_INVALID_HANDLE );
430 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
432 release_object( &session->hdr );
433 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
436 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
438 release_object( &session->hdr );
441 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
442 connect->hdr.vtbl = &connect_vtbl;
443 connect->hdr.refs = 1;
444 connect->hdr.flags = session->hdr.flags;
445 connect->hdr.callback = session->hdr.callback;
446 connect->hdr.notify_mask = session->hdr.notify_mask;
447 connect->hdr.context = session->hdr.context;
449 addref_object( &session->hdr );
450 connect->session = session;
451 list_add_head( &session->hdr.children, &connect->hdr.entry );
453 if (server && !(connect->hostname = strdupW( server ))) goto end;
454 connect->hostport = port;
456 if (!set_server_for_hostname( connect, server, port ))
459 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
460 connect->hdr.handle = hconnect;
462 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
465 release_object( &connect->hdr );
467 TRACE("returning %p\n", hconnect);
471 /***********************************************************************
472 * request_destroy (internal)
474 static void request_destroy( object_header_t *hdr )
476 request_t *request = (request_t *)hdr;
479 TRACE("%p\n", request);
481 release_object( &request->connect->hdr );
483 heap_free( request->verb );
484 heap_free( request->path );
485 heap_free( request->version );
486 heap_free( request->raw_headers );
487 heap_free( request->status_text );
488 for (i = 0; i < request->num_headers; i++)
490 heap_free( request->headers[i].field );
491 heap_free( request->headers[i].value );
493 heap_free( request->headers );
494 heap_free( request );
497 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
501 case WINHTTP_OPTION_SECURITY_FLAGS:
505 if (!buffer || *buflen < sizeof(flags))
507 *buflen = sizeof(flags);
508 set_last_error( ERROR_INSUFFICIENT_BUFFER );
513 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
514 *(DWORD *)buffer = flags;
515 *buflen = sizeof(flags);
518 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
520 const CERT_CONTEXT *cert;
521 request_t *request = (request_t *)hdr;
523 if (!buffer || *buflen < sizeof(cert))
525 *buflen = sizeof(cert);
526 set_last_error( ERROR_INSUFFICIENT_BUFFER );
530 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
531 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
532 *buflen = sizeof(cert);
535 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
537 if (!buffer || *buflen < sizeof(DWORD))
539 *buflen = sizeof(DWORD);
540 set_last_error( ERROR_INSUFFICIENT_BUFFER );
544 *(DWORD *)buffer = 128; /* FIXME */
545 *buflen = sizeof(DWORD);
549 FIXME("unimplemented option %u\n", option);
550 set_last_error( ERROR_INVALID_PARAMETER );
555 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
559 case WINHTTP_OPTION_PROXY:
561 WINHTTP_PROXY_INFO *pi = buffer;
563 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
566 case WINHTTP_OPTION_DISABLE_FEATURE:
570 if (buflen != sizeof(DWORD))
572 set_last_error( ERROR_INSUFFICIENT_BUFFER );
576 disable = *(DWORD *)buffer;
577 TRACE("0x%x\n", disable);
578 hdr->disable_flags |= disable;
581 case WINHTTP_OPTION_AUTOLOGON_POLICY:
585 if (buflen != sizeof(DWORD))
587 set_last_error( ERROR_INSUFFICIENT_BUFFER );
591 policy = *(DWORD *)buffer;
592 TRACE("0x%x\n", policy);
593 hdr->logon_policy = policy;
596 case WINHTTP_OPTION_REDIRECT_POLICY:
600 if (buflen != sizeof(DWORD))
602 set_last_error( ERROR_INSUFFICIENT_BUFFER );
606 policy = *(DWORD *)buffer;
607 TRACE("0x%x\n", policy);
608 hdr->redirect_policy = policy;
611 case WINHTTP_OPTION_SECURITY_FLAGS:
612 FIXME("WINHTTP_OPTION_SECURITY_FLAGS unimplemented (%08x)\n",
616 FIXME("unimplemented option %u\n", option);
617 set_last_error( ERROR_INVALID_PARAMETER );
622 static const object_vtbl_t request_vtbl =
625 request_query_option,
629 /***********************************************************************
630 * WinHttpOpenRequest (winhttp.@)
632 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
633 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
637 HINTERNET hrequest = NULL;
639 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
640 debugstr_w(version), debugstr_w(referrer), types, flags);
642 if (!(connect = (connect_t *)grab_object( hconnect )))
644 set_last_error( ERROR_INVALID_HANDLE );
647 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
649 release_object( &connect->hdr );
650 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
653 if (!(request = heap_alloc_zero( sizeof(request_t) )))
655 release_object( &connect->hdr );
658 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
659 request->hdr.vtbl = &request_vtbl;
660 request->hdr.refs = 1;
661 request->hdr.flags = flags;
662 request->hdr.callback = connect->hdr.callback;
663 request->hdr.notify_mask = connect->hdr.notify_mask;
664 request->hdr.context = connect->hdr.context;
666 addref_object( &connect->hdr );
667 request->connect = connect;
668 list_add_head( &connect->hdr.children, &request->hdr.entry );
670 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
671 request->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
672 request->send_timeout = DEFAULT_SEND_TIMEOUT;
673 request->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
675 if (!verb || !verb[0]) verb = getW;
676 if (!(request->verb = strdupW( verb ))) goto end;
683 len = strlenW( object ) + 1;
684 if (object[0] != '/') len++;
685 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
687 if (object[0] != '/') *p++ = '/';
688 strcpyW( p, object );
689 request->path = path;
691 else if (!(request->path = strdupW( slashW ))) goto end;
693 if (!version || !version[0]) version = http1_1;
694 if (!(request->version = strdupW( version ))) goto end;
696 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
697 request->hdr.handle = hrequest;
699 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
702 release_object( &request->hdr );
704 TRACE("returning %p\n", hrequest);
708 /***********************************************************************
709 * WinHttpCloseHandle (winhttp.@)
711 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
713 object_header_t *hdr;
715 TRACE("%p\n", handle);
717 if (!(hdr = grab_object( handle )))
719 set_last_error( ERROR_INVALID_HANDLE );
722 release_object( hdr );
723 free_handle( handle );
727 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
733 set_last_error( ERROR_INVALID_PARAMETER );
739 case WINHTTP_OPTION_CONTEXT_VALUE:
741 if (!buffer || *buflen < sizeof(DWORD_PTR))
743 *buflen = sizeof(DWORD_PTR);
744 set_last_error( ERROR_INSUFFICIENT_BUFFER );
748 *(DWORD_PTR *)buffer = hdr->context;
749 *buflen = sizeof(DWORD_PTR);
753 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
756 FIXME("unimplemented option %u\n", option);
757 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
765 /***********************************************************************
766 * WinHttpQueryOption (winhttp.@)
768 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
771 object_header_t *hdr;
773 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
775 if (!(hdr = grab_object( handle )))
777 set_last_error( ERROR_INVALID_HANDLE );
781 ret = query_option( hdr, option, buffer, buflen );
783 release_object( hdr );
787 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
793 set_last_error( ERROR_INVALID_PARAMETER );
799 case WINHTTP_OPTION_CONTEXT_VALUE:
801 if (buflen != sizeof(DWORD_PTR))
803 set_last_error( ERROR_INSUFFICIENT_BUFFER );
807 hdr->context = *(DWORD_PTR *)buffer;
811 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
814 FIXME("unimplemented option %u\n", option);
815 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
823 /***********************************************************************
824 * WinHttpSetOption (winhttp.@)
826 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
829 object_header_t *hdr;
831 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
833 if (!(hdr = grab_object( handle )))
835 set_last_error( ERROR_INVALID_HANDLE );
839 ret = set_option( hdr, option, buffer, buflen );
841 release_object( hdr );
845 /***********************************************************************
846 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
848 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
850 FIXME("0x%08x, %p\n", flags, url);
852 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
856 static const WCHAR Connections[] = {
857 'S','o','f','t','w','a','r','e','\\',
858 'M','i','c','r','o','s','o','f','t','\\',
859 'W','i','n','d','o','w','s','\\',
860 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
861 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
862 'C','o','n','n','e','c','t','i','o','n','s',0 };
863 static const WCHAR WinHttpSettings[] = {
864 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
865 static const DWORD WINHTTPSETTINGS_MAGIC = 0x18;
866 static const DWORD WINHTTP_PROXY_TYPE_DIRECT = 1;
867 static const DWORD WINHTTP_PROXY_TYPE_PROXY = 2;
869 struct winhttp_settings_header
872 DWORD unknown; /* always zero? */
873 DWORD flags; /* one of WINHTTP_PROXY_TYPE_* */
876 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
880 for (begin = src; src - begin < len; src++, dst++)
885 /***********************************************************************
886 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
888 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
892 BOOL got_from_reg = FALSE, direct = TRUE;
897 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
900 DWORD type, size = 0;
902 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
903 if (!l && type == REG_BINARY &&
904 size >= sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD))
906 BYTE *buf = heap_alloc( size );
910 struct winhttp_settings_header *hdr =
911 (struct winhttp_settings_header *)buf;
912 DWORD *len = (DWORD *)(hdr + 1);
914 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
916 if (!l && hdr->magic == WINHTTPSETTINGS_MAGIC &&
919 if (hdr->flags & WINHTTP_PROXY_TYPE_PROXY)
923 LPWSTR proxy_bypass = NULL;
925 /* Sanity-check length of proxy string */
926 if ((BYTE *)len - buf + *len <= size)
929 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
931 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
932 len = (DWORD *)((BYTE *)(len + 1) + *len);
936 /* Sanity-check length of proxy bypass string */
937 if ((BYTE *)len - buf + *len <= size)
939 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
941 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
950 info->lpszProxy = proxy;
951 info->lpszProxyBypass = proxy_bypass;
957 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
958 TRACE("http proxy (from registry) = %s, bypass = %s\n",
959 debugstr_w(info->lpszProxy),
960 debugstr_w(info->lpszProxyBypass));
969 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
971 char *colon, *http_proxy;
973 if ((colon = strchr( envproxy, ':' )))
975 if (*(colon + 1) == '/' && *(colon + 2) == '/')
977 static const char http[] = "http://";
979 /* It's a scheme, check that it's http */
980 if (!strncmp( envproxy, http, strlen( http ) ))
981 http_proxy = envproxy + strlen( http );
984 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
989 http_proxy = envproxy;
992 http_proxy = envproxy;
998 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
999 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1001 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1003 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1004 info->lpszProxy = http_proxyW;
1005 info->lpszProxyBypass = NULL;
1006 TRACE("http proxy (from environment) = %s\n",
1007 debugstr_w(info->lpszProxy));
1013 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1014 info->lpszProxy = NULL;
1015 info->lpszProxyBypass = NULL;
1020 /***********************************************************************
1021 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1023 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1025 TRACE("%p\n", config);
1029 set_last_error( ERROR_INVALID_PARAMETER );
1033 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1035 FIXME("returning no proxy used\n");
1036 config->fAutoDetect = FALSE;
1037 config->lpszAutoConfigUrl = NULL;
1038 config->lpszProxy = NULL;
1039 config->lpszProxyBypass = NULL;
1044 /***********************************************************************
1045 * WinHttpGetProxyForUrl (winhttp.@)
1047 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1048 WINHTTP_PROXY_INFO *info )
1050 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1052 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1056 /***********************************************************************
1057 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1059 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1066 TRACE("%p\n", info);
1070 set_last_error( ERROR_INVALID_PARAMETER );
1073 switch (info->dwAccessType)
1075 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1077 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1078 if (!info->lpszProxy)
1080 set_last_error( ERROR_INVALID_PARAMETER );
1083 /* Only ASCII characters are allowed */
1084 for (src = info->lpszProxy; *src; src++)
1087 set_last_error( ERROR_INVALID_PARAMETER );
1090 if (info->lpszProxyBypass)
1092 for (src = info->lpszProxyBypass; *src; src++)
1095 set_last_error( ERROR_INVALID_PARAMETER );
1101 set_last_error( ERROR_INVALID_PARAMETER );
1105 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1106 KEY_WRITE, NULL, &key, NULL );
1109 DWORD size = sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD);
1112 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1114 size += strlenW( info->lpszProxy );
1115 if (info->lpszProxyBypass)
1116 size += strlenW( info->lpszProxyBypass );
1118 buf = heap_alloc( size );
1121 struct winhttp_settings_header *hdr =
1122 (struct winhttp_settings_header *)buf;
1123 DWORD *len = (DWORD *)(hdr + 1);
1125 hdr->magic = WINHTTPSETTINGS_MAGIC;
1127 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1131 hdr->flags = WINHTTP_PROXY_TYPE_PROXY;
1132 *len++ = strlenW( info->lpszProxy );
1133 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1137 if (info->lpszProxyBypass)
1139 *len++ = strlenW( info->lpszProxyBypass );
1140 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1149 hdr->flags = WINHTTP_PROXY_TYPE_DIRECT;
1153 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1163 /***********************************************************************
1164 * WinHttpSetStatusCallback (winhttp.@)
1166 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1167 DWORD flags, DWORD_PTR reserved )
1169 object_header_t *hdr;
1170 WINHTTP_STATUS_CALLBACK ret;
1172 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1174 if (!(hdr = grab_object( handle )))
1176 set_last_error( ERROR_INVALID_HANDLE );
1177 return WINHTTP_INVALID_STATUS_CALLBACK;
1179 ret = hdr->callback;
1180 hdr->callback = callback;
1181 hdr->notify_mask = flags;
1183 release_object( hdr );
1187 /***********************************************************************
1188 * WinHttpSetTimeouts (winhttp.@)
1190 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1195 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1197 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1199 set_last_error( ERROR_INVALID_PARAMETER );
1204 FIXME("resolve timeout (%d) not supported\n", resolve);
1206 if (!(request = (request_t *)grab_object( handle )))
1208 set_last_error( ERROR_INVALID_HANDLE );
1212 if (request->hdr.type != WINHTTP_HANDLE_TYPE_REQUEST)
1214 release_object( &request->hdr );
1215 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1219 request->connect_timeout = connect;
1221 if (send < 0) send = 0;
1222 request->send_timeout = send;
1224 if (receive < 0) receive = 0;
1225 request->recv_timeout = receive;
1227 if (netconn_connected( &request->netconn ))
1229 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1230 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1233 release_object( &request->hdr );
1237 static const WCHAR wkday[7][4] =
1238 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1239 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1240 static const WCHAR month[12][4] =
1241 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1242 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1243 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1245 /***********************************************************************
1246 * WinHttpTimeFromSystemTime (WININET.@)
1248 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
1250 static const WCHAR format[] =
1251 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1252 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1254 TRACE("%p, %p\n", time, string);
1256 if (!time || !string) return FALSE;
1258 sprintfW( string, format,
1259 wkday[time->wDayOfWeek],
1261 month[time->wMonth - 1],
1270 /***********************************************************************
1271 * WinHttpTimeToSystemTime (WININET.@)
1273 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
1276 const WCHAR *s = string;
1279 TRACE("%s, %p\n", debugstr_w(string), time);
1281 if (!string || !time) return FALSE;
1283 /* Windows does this too */
1284 GetSystemTime( time );
1286 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1287 * a SYSTEMTIME structure.
1290 while (*s && !isalphaW( *s )) s++;
1291 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1292 time->wDayOfWeek = 7;
1294 for (i = 0; i < 7; i++)
1296 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
1297 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
1298 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
1300 time->wDayOfWeek = i;
1305 if (time->wDayOfWeek > 6) return TRUE;
1306 while (*s && !isdigitW( *s )) s++;
1307 time->wDay = strtolW( s, &end, 10 );
1310 while (*s && !isalphaW( *s )) s++;
1311 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1314 for (i = 0; i < 12; i++)
1316 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
1317 toupperW( month[i][1]) == toupperW( s[1] ) &&
1318 toupperW( month[i][2]) == toupperW( s[2] ) )
1320 time->wMonth = i + 1;
1324 if (time->wMonth == 0) return TRUE;
1326 while (*s && !isdigitW( *s )) s++;
1327 if (*s == '\0') return TRUE;
1328 time->wYear = strtolW( s, &end, 10 );
1331 while (*s && !isdigitW( *s )) s++;
1332 if (*s == '\0') return TRUE;
1333 time->wHour = strtolW( s, &end, 10 );
1336 while (*s && !isdigitW( *s )) s++;
1337 if (*s == '\0') return TRUE;
1338 time->wMinute = strtolW( s, &end, 10 );
1341 while (*s && !isdigitW( *s )) s++;
1342 if (*s == '\0') return TRUE;
1343 time->wSecond = strtolW( s, &end, 10 );
1346 time->wMilliseconds = 0;