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 const object_vtbl_t connect_vtbl =
239 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
241 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
244 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
246 else if (*domain == '*')
248 if (domain[1] == '.')
252 /* For a hostname to match a wildcard, the last domain must match
253 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
254 * hostname is www.foo.a.b, it matches, but a.b does not.
256 dot = strchrW( server, '.' );
259 int len = strlenW( dot + 1 );
261 if (len > strlenW( domain + 2 ))
265 /* The server's domain is longer than the wildcard, so it
266 * could be a subdomain. Compare the last portion of the
269 ptr = dot + len + 1 - strlenW( domain + 2 );
270 if (!strcmpiW( ptr, domain + 2 ))
272 /* This is only a match if the preceding character is
273 * a '.', i.e. that it is a matching domain. E.g.
274 * if domain is '*.b.c' and server is 'www.ab.c' they
277 ret = *(ptr - 1) == '.';
281 ret = !strcmpiW( dot + 1, domain + 2 );
286 ret = !strcmpiW( server, domain );
290 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
291 #define MAX_HOST_NAME_LENGTH 256
293 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
298 if (!session->proxy_bypass) return FALSE;
299 ptr = session->proxy_bypass;
303 ptr = strchrW( ptr, ';' );
305 ptr = strchrW( tmp, ' ' );
308 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
310 WCHAR domain[MAX_HOST_NAME_LENGTH];
312 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
313 domain[ptr - tmp] = 0;
314 ret = domain_matches( server, domain );
319 ret = domain_matches( server, tmp );
320 } while (ptr && !ret);
324 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
326 session_t *session = connect->session;
329 if (session->proxy_server && !should_bypass_proxy(session, server))
333 if ((colon = strchrW( session->proxy_server, ':' )))
335 if (!connect->servername || strncmpiW( connect->servername,
336 session->proxy_server, colon - session->proxy_server - 1 ))
338 heap_free( connect->servername );
339 if (!(connect->servername = heap_alloc(
340 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
345 memcpy( connect->servername, session->proxy_server,
346 (colon - session->proxy_server) * sizeof(WCHAR) );
347 connect->servername[colon - session->proxy_server] = 0;
349 connect->serverport = atoiW( colon + 1 );
351 connect->serverport = INTERNET_DEFAULT_PORT;
356 if (!connect->servername || strcmpiW( connect->servername,
357 session->proxy_server ))
359 heap_free( connect->servername );
360 if (!(connect->servername = strdupW( session->proxy_server )))
365 connect->serverport = INTERNET_DEFAULT_PORT;
371 heap_free( connect->servername );
372 if (!(connect->servername = strdupW( server )))
377 connect->serverport = port;
383 /***********************************************************************
384 * WinHttpConnect (winhttp.@)
386 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
390 HINTERNET hconnect = NULL;
392 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
396 set_last_error( ERROR_INVALID_PARAMETER );
399 if (!(session = (session_t *)grab_object( hsession )))
401 set_last_error( ERROR_INVALID_HANDLE );
404 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
406 release_object( &session->hdr );
407 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
410 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
412 release_object( &session->hdr );
415 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
416 connect->hdr.vtbl = &connect_vtbl;
417 connect->hdr.refs = 1;
418 connect->hdr.flags = session->hdr.flags;
419 connect->hdr.callback = session->hdr.callback;
420 connect->hdr.notify_mask = session->hdr.notify_mask;
421 connect->hdr.context = session->hdr.context;
423 addref_object( &session->hdr );
424 connect->session = session;
425 list_add_head( &session->hdr.children, &connect->hdr.entry );
427 if (server && !(connect->hostname = strdupW( server ))) goto end;
428 connect->hostport = port;
430 if (!set_server_for_hostname( connect, server, port ))
433 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
434 connect->hdr.handle = hconnect;
436 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
439 release_object( &connect->hdr );
441 TRACE("returning %p\n", hconnect);
445 /***********************************************************************
446 * request_destroy (internal)
448 static void request_destroy( object_header_t *hdr )
450 request_t *request = (request_t *)hdr;
453 TRACE("%p\n", request);
455 release_object( &request->connect->hdr );
457 heap_free( request->verb );
458 heap_free( request->path );
459 heap_free( request->version );
460 heap_free( request->raw_headers );
461 heap_free( request->status_text );
462 for (i = 0; i < request->num_headers; i++)
464 heap_free( request->headers[i].field );
465 heap_free( request->headers[i].value );
467 heap_free( request->headers );
468 heap_free( request );
471 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
475 case WINHTTP_OPTION_SECURITY_FLAGS:
479 if (!buffer || *buflen < sizeof(flags))
481 *buflen = sizeof(flags);
482 set_last_error( ERROR_INSUFFICIENT_BUFFER );
487 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
488 *(DWORD *)buffer = flags;
489 *buflen = sizeof(flags);
492 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
494 const CERT_CONTEXT *cert;
495 request_t *request = (request_t *)hdr;
497 if (!buffer || *buflen < sizeof(cert))
499 *buflen = sizeof(cert);
500 set_last_error( ERROR_INSUFFICIENT_BUFFER );
504 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
505 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
506 *buflen = sizeof(cert);
509 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
511 if (!buffer || *buflen < sizeof(DWORD))
513 *buflen = sizeof(DWORD);
514 set_last_error( ERROR_INSUFFICIENT_BUFFER );
518 *(DWORD *)buffer = 128; /* FIXME */
519 *buflen = sizeof(DWORD);
523 FIXME("unimplemented option %u\n", option);
524 set_last_error( ERROR_INVALID_PARAMETER );
529 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
533 case WINHTTP_OPTION_PROXY:
535 WINHTTP_PROXY_INFO *pi = buffer;
537 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
540 case WINHTTP_OPTION_DISABLE_FEATURE:
544 if (buflen != sizeof(DWORD))
546 set_last_error( ERROR_INSUFFICIENT_BUFFER );
550 disable = *(DWORD *)buffer;
551 TRACE("0x%x\n", disable);
552 hdr->disable_flags |= disable;
555 case WINHTTP_OPTION_AUTOLOGON_POLICY:
559 if (buflen != sizeof(DWORD))
561 set_last_error( ERROR_INSUFFICIENT_BUFFER );
565 policy = *(DWORD *)buffer;
566 TRACE("0x%x\n", policy);
567 hdr->logon_policy = policy;
570 case WINHTTP_OPTION_REDIRECT_POLICY:
574 if (buflen != sizeof(DWORD))
576 set_last_error( ERROR_INSUFFICIENT_BUFFER );
580 policy = *(DWORD *)buffer;
581 TRACE("0x%x\n", policy);
582 hdr->redirect_policy = policy;
585 case WINHTTP_OPTION_SECURITY_FLAGS:
586 FIXME("WINHTTP_OPTION_SECURITY_FLAGS unimplemented (%08x)\n",
590 FIXME("unimplemented option %u\n", option);
591 set_last_error( ERROR_INVALID_PARAMETER );
596 static const object_vtbl_t request_vtbl =
599 request_query_option,
603 /***********************************************************************
604 * WinHttpOpenRequest (winhttp.@)
606 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
607 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
611 HINTERNET hrequest = NULL;
613 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
614 debugstr_w(version), debugstr_w(referrer), types, flags);
616 if (!(connect = (connect_t *)grab_object( hconnect )))
618 set_last_error( ERROR_INVALID_HANDLE );
621 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
623 release_object( &connect->hdr );
624 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
627 if (!(request = heap_alloc_zero( sizeof(request_t) )))
629 release_object( &connect->hdr );
632 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
633 request->hdr.vtbl = &request_vtbl;
634 request->hdr.refs = 1;
635 request->hdr.flags = flags;
636 request->hdr.callback = connect->hdr.callback;
637 request->hdr.notify_mask = connect->hdr.notify_mask;
638 request->hdr.context = connect->hdr.context;
640 addref_object( &connect->hdr );
641 request->connect = connect;
642 list_add_head( &connect->hdr.children, &request->hdr.entry );
644 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
645 request->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
646 request->send_timeout = DEFAULT_SEND_TIMEOUT;
647 request->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
649 if (!verb || !verb[0]) verb = getW;
650 if (!(request->verb = strdupW( verb ))) goto end;
657 len = strlenW( object ) + 1;
658 if (object[0] != '/') len++;
659 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
661 if (object[0] != '/') *p++ = '/';
662 strcpyW( p, object );
663 request->path = path;
665 else if (!(request->path = strdupW( slashW ))) goto end;
667 if (!version || !version[0]) version = http1_1;
668 if (!(request->version = strdupW( version ))) goto end;
670 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
671 request->hdr.handle = hrequest;
673 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
676 release_object( &request->hdr );
678 TRACE("returning %p\n", hrequest);
682 /***********************************************************************
683 * WinHttpCloseHandle (winhttp.@)
685 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
687 object_header_t *hdr;
689 TRACE("%p\n", handle);
691 if (!(hdr = grab_object( handle )))
693 set_last_error( ERROR_INVALID_HANDLE );
696 release_object( hdr );
697 free_handle( handle );
701 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
707 set_last_error( ERROR_INVALID_PARAMETER );
713 case WINHTTP_OPTION_CONTEXT_VALUE:
715 if (!buffer || *buflen < sizeof(DWORD_PTR))
717 *buflen = sizeof(DWORD_PTR);
718 set_last_error( ERROR_INSUFFICIENT_BUFFER );
722 *(DWORD_PTR *)buffer = hdr->context;
723 *buflen = sizeof(DWORD_PTR);
727 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
730 FIXME("unimplemented option %u\n", option);
731 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
739 /***********************************************************************
740 * WinHttpQueryOption (winhttp.@)
742 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
745 object_header_t *hdr;
747 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
749 if (!(hdr = grab_object( handle )))
751 set_last_error( ERROR_INVALID_HANDLE );
755 ret = query_option( hdr, option, buffer, buflen );
757 release_object( hdr );
761 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
767 set_last_error( ERROR_INVALID_PARAMETER );
773 case WINHTTP_OPTION_CONTEXT_VALUE:
775 if (buflen != sizeof(DWORD_PTR))
777 set_last_error( ERROR_INSUFFICIENT_BUFFER );
781 hdr->context = *(DWORD_PTR *)buffer;
785 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
788 FIXME("unimplemented option %u\n", option);
789 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
797 /***********************************************************************
798 * WinHttpSetOption (winhttp.@)
800 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
803 object_header_t *hdr;
805 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
807 if (!(hdr = grab_object( handle )))
809 set_last_error( ERROR_INVALID_HANDLE );
813 ret = set_option( hdr, option, buffer, buflen );
815 release_object( hdr );
819 /***********************************************************************
820 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
822 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
824 FIXME("0x%08x, %p\n", flags, url);
826 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
830 static const WCHAR Connections[] = {
831 'S','o','f','t','w','a','r','e','\\',
832 'M','i','c','r','o','s','o','f','t','\\',
833 'W','i','n','d','o','w','s','\\',
834 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
835 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
836 'C','o','n','n','e','c','t','i','o','n','s',0 };
837 static const WCHAR WinHttpSettings[] = {
838 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
839 static const DWORD WINHTTPSETTINGS_MAGIC = 0x18;
840 static const DWORD WINHTTP_PROXY_TYPE_DIRECT = 1;
841 static const DWORD WINHTTP_PROXY_TYPE_PROXY = 2;
843 struct winhttp_settings_header
846 DWORD unknown; /* always zero? */
847 DWORD flags; /* one of WINHTTP_PROXY_TYPE_* */
850 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
854 for (begin = src; src - begin < len; src++, dst++)
859 /***********************************************************************
860 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
862 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
866 BOOL got_from_reg = FALSE, direct = TRUE;
871 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
874 DWORD type, size = 0;
876 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
877 if (!l && type == REG_BINARY &&
878 size >= sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD))
880 BYTE *buf = heap_alloc( size );
884 struct winhttp_settings_header *hdr =
885 (struct winhttp_settings_header *)buf;
886 DWORD *len = (DWORD *)(hdr + 1);
888 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
890 if (!l && hdr->magic == WINHTTPSETTINGS_MAGIC &&
893 if (hdr->flags & WINHTTP_PROXY_TYPE_PROXY)
897 LPWSTR proxy_bypass = NULL;
899 /* Sanity-check length of proxy string */
900 if ((BYTE *)len - buf + *len <= size)
903 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
905 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
906 len = (DWORD *)((BYTE *)(len + 1) + *len);
910 /* Sanity-check length of proxy bypass string */
911 if ((BYTE *)len - buf + *len <= size)
913 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
915 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
924 info->lpszProxy = proxy;
925 info->lpszProxyBypass = proxy_bypass;
931 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
932 TRACE("http proxy (from registry) = %s, bypass = %s\n",
933 debugstr_w(info->lpszProxy),
934 debugstr_w(info->lpszProxyBypass));
943 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
945 char *colon, *http_proxy;
947 if ((colon = strchr( envproxy, ':' )))
949 if (*(colon + 1) == '/' && *(colon + 2) == '/')
951 static const char http[] = "http://";
953 /* It's a scheme, check that it's http */
954 if (!strncmp( envproxy, http, strlen( http ) ))
955 http_proxy = envproxy + strlen( http );
958 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
963 http_proxy = envproxy;
966 http_proxy = envproxy;
972 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
973 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
975 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
977 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
978 info->lpszProxy = http_proxyW;
979 info->lpszProxyBypass = NULL;
980 TRACE("http proxy (from environment) = %s\n",
981 debugstr_w(info->lpszProxy));
987 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
988 info->lpszProxy = NULL;
989 info->lpszProxyBypass = NULL;
994 /***********************************************************************
995 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
997 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
999 TRACE("%p\n", config);
1003 set_last_error( ERROR_INVALID_PARAMETER );
1007 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1009 FIXME("returning no proxy used\n");
1010 config->fAutoDetect = FALSE;
1011 config->lpszAutoConfigUrl = NULL;
1012 config->lpszProxy = NULL;
1013 config->lpszProxyBypass = NULL;
1018 /***********************************************************************
1019 * WinHttpGetProxyForUrl (winhttp.@)
1021 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1022 WINHTTP_PROXY_INFO *info )
1024 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1026 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1030 /***********************************************************************
1031 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1033 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1040 TRACE("%p\n", info);
1044 set_last_error( ERROR_INVALID_PARAMETER );
1047 switch (info->dwAccessType)
1049 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1051 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1052 if (!info->lpszProxy)
1054 set_last_error( ERROR_INVALID_PARAMETER );
1057 /* Only ASCII characters are allowed */
1058 for (src = info->lpszProxy; *src; src++)
1061 set_last_error( ERROR_INVALID_PARAMETER );
1064 if (info->lpszProxyBypass)
1066 for (src = info->lpszProxyBypass; *src; src++)
1069 set_last_error( ERROR_INVALID_PARAMETER );
1075 set_last_error( ERROR_INVALID_PARAMETER );
1079 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1080 KEY_WRITE, NULL, &key, NULL );
1083 DWORD size = sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD);
1086 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1088 size += strlenW( info->lpszProxy );
1089 if (info->lpszProxyBypass)
1090 size += strlenW( info->lpszProxyBypass );
1092 buf = heap_alloc( size );
1095 struct winhttp_settings_header *hdr =
1096 (struct winhttp_settings_header *)buf;
1097 DWORD *len = (DWORD *)(hdr + 1);
1099 hdr->magic = WINHTTPSETTINGS_MAGIC;
1101 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1105 hdr->flags = WINHTTP_PROXY_TYPE_PROXY;
1106 *len++ = strlenW( info->lpszProxy );
1107 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1111 if (info->lpszProxyBypass)
1113 *len++ = strlenW( info->lpszProxyBypass );
1114 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1123 hdr->flags = WINHTTP_PROXY_TYPE_DIRECT;
1127 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1137 /***********************************************************************
1138 * WinHttpSetStatusCallback (winhttp.@)
1140 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1141 DWORD flags, DWORD_PTR reserved )
1143 object_header_t *hdr;
1144 WINHTTP_STATUS_CALLBACK ret;
1146 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1148 if (!(hdr = grab_object( handle )))
1150 set_last_error( ERROR_INVALID_HANDLE );
1151 return WINHTTP_INVALID_STATUS_CALLBACK;
1153 ret = hdr->callback;
1154 hdr->callback = callback;
1155 hdr->notify_mask = flags;
1157 release_object( hdr );
1161 /***********************************************************************
1162 * WinHttpSetTimeouts (winhttp.@)
1164 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1169 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1171 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1173 set_last_error( ERROR_INVALID_PARAMETER );
1178 FIXME("resolve timeout (%d) not supported\n", resolve);
1180 if (!(request = (request_t *)grab_object( handle )))
1182 set_last_error( ERROR_INVALID_HANDLE );
1186 if (request->hdr.type != WINHTTP_HANDLE_TYPE_REQUEST)
1188 release_object( &request->hdr );
1189 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1193 request->connect_timeout = connect;
1195 if (send < 0) send = 0;
1196 request->send_timeout = send;
1198 if (receive < 0) receive = 0;
1199 request->recv_timeout = receive;
1201 if (netconn_connected( &request->netconn ))
1203 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1204 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1207 release_object( &request->hdr );
1211 static const WCHAR wkday[7][4] =
1212 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1213 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1214 static const WCHAR month[12][4] =
1215 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1216 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1217 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1219 /***********************************************************************
1220 * WinHttpTimeFromSystemTime (WININET.@)
1222 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
1224 static const WCHAR format[] =
1225 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1226 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1228 TRACE("%p, %p\n", time, string);
1230 if (!time || !string) return FALSE;
1232 sprintfW( string, format,
1233 wkday[time->wDayOfWeek],
1235 month[time->wMonth - 1],
1244 /***********************************************************************
1245 * WinHttpTimeToSystemTime (WININET.@)
1247 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
1250 const WCHAR *s = string;
1253 TRACE("%s, %p\n", debugstr_w(string), time);
1255 if (!string || !time) return FALSE;
1257 /* Windows does this too */
1258 GetSystemTime( time );
1260 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1261 * a SYSTEMTIME structure.
1264 while (*s && !isalphaW( *s )) s++;
1265 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1266 time->wDayOfWeek = 7;
1268 for (i = 0; i < 7; i++)
1270 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
1271 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
1272 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
1274 time->wDayOfWeek = i;
1279 if (time->wDayOfWeek > 6) return TRUE;
1280 while (*s && !isdigitW( *s )) s++;
1281 time->wDay = strtolW( s, &end, 10 );
1284 while (*s && !isalphaW( *s )) s++;
1285 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1288 for (i = 0; i < 12; i++)
1290 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
1291 toupperW( month[i][1]) == toupperW( s[1] ) &&
1292 toupperW( month[i][2]) == toupperW( s[2] ) )
1294 time->wMonth = i + 1;
1298 if (time->wMonth == 0) return TRUE;
1300 while (*s && !isdigitW( *s )) s++;
1301 if (*s == '\0') return TRUE;
1302 time->wYear = strtolW( s, &end, 10 );
1305 while (*s && !isdigitW( *s )) s++;
1306 if (*s == '\0') return TRUE;
1307 time->wHour = strtolW( s, &end, 10 );
1310 while (*s && !isdigitW( *s )) s++;
1311 if (*s == '\0') return TRUE;
1312 time->wMinute = strtolW( s, &end, 10 );
1315 while (*s && !isdigitW( *s )) s++;
1316 if (*s == '\0') return TRUE;
1317 time->wSecond = strtolW( s, &end, 10 );
1320 time->wMilliseconds = 0;