dpnet/tests: A spelling fix in a variable name.
[wine.git] / dlls / wininet / internet.c
blob772e4c15b91f3e4ea9039048e6cfe0d715ffc84c
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
31 #ifdef HAVE_CORESERVICES_CORESERVICES_H
32 #define GetCurrentThread MacGetCurrentThread
33 #define LoadResource MacLoadResource
34 #include <CoreServices/CoreServices.h>
35 #undef GetCurrentThread
36 #undef LoadResource
37 #undef DPRINTF
38 #endif
40 #include "winsock2.h"
41 #include "ws2ipdef.h"
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <assert.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winuser.h"
54 #include "wininet.h"
55 #include "winnls.h"
56 #include "wine/debug.h"
57 #include "winerror.h"
58 #define NO_SHLWAPI_STREAM
59 #include "shlwapi.h"
61 #include "wine/exception.h"
63 #include "internet.h"
64 #include "resource.h"
66 #include "wine/unicode.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
70 typedef struct
72 DWORD dwError;
73 CHAR response[MAX_REPLY_LEN];
74 } WITHREADERROR, *LPWITHREADERROR;
76 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
77 HMODULE WININET_hModule;
79 static CRITICAL_SECTION WININET_cs;
80 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
82 0, 0, &WININET_cs,
83 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
84 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
86 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
88 static object_header_t **handle_table;
89 static UINT_PTR next_handle;
90 static UINT_PTR handle_table_size;
92 typedef struct
94 DWORD proxyEnabled;
95 LPWSTR proxy;
96 LPWSTR proxyBypass;
97 LPWSTR proxyUsername;
98 LPWSTR proxyPassword;
99 } proxyinfo_t;
101 static ULONG max_conns = 2, max_1_0_conns = 4;
102 static ULONG connect_timeout = 60000;
104 static const WCHAR szInternetSettings[] =
105 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
106 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
107 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
108 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
109 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
110 static const WCHAR szProxyOverride[] = { 'P','r','o','x','y','O','v','e','r','r','i','d','e', 0 };
112 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
114 UINT_PTR handle = 0, num;
115 object_header_t *ret;
116 object_header_t **p;
117 BOOL res = TRUE;
119 ret = heap_alloc_zero(size);
120 if(!ret)
121 return NULL;
123 list_init(&ret->children);
125 EnterCriticalSection( &WININET_cs );
127 if(!handle_table_size) {
128 num = 16;
129 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
130 if(p) {
131 handle_table = p;
132 handle_table_size = num;
133 next_handle = 1;
134 }else {
135 res = FALSE;
137 }else if(next_handle == handle_table_size) {
138 num = handle_table_size * 2;
139 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
140 if(p) {
141 handle_table = p;
142 handle_table_size = num;
143 }else {
144 res = FALSE;
148 if(res) {
149 handle = next_handle;
150 if(handle_table[handle])
151 ERR("handle isn't free but should be\n");
152 handle_table[handle] = ret;
153 ret->valid_handle = TRUE;
155 while(next_handle < handle_table_size && handle_table[next_handle])
156 next_handle++;
159 LeaveCriticalSection( &WININET_cs );
161 if(!res) {
162 heap_free(ret);
163 return NULL;
166 ret->vtbl = vtbl;
167 ret->refs = 1;
168 ret->hInternet = (HINTERNET)handle;
170 if(parent) {
171 ret->lpfnStatusCB = parent->lpfnStatusCB;
172 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
175 return ret;
178 object_header_t *WININET_AddRef( object_header_t *info )
180 ULONG refs = InterlockedIncrement(&info->refs);
181 TRACE("%p -> refcount = %d\n", info, refs );
182 return info;
185 object_header_t *get_handle_object( HINTERNET hinternet )
187 object_header_t *info = NULL;
188 UINT_PTR handle = (UINT_PTR) hinternet;
190 EnterCriticalSection( &WININET_cs );
192 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
193 info = WININET_AddRef(handle_table[handle]);
195 LeaveCriticalSection( &WININET_cs );
197 TRACE("handle %ld -> %p\n", handle, info);
199 return info;
202 static void invalidate_handle(object_header_t *info)
204 object_header_t *child, *next;
206 if(!info->valid_handle)
207 return;
208 info->valid_handle = FALSE;
210 /* Free all children as native does */
211 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
213 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
214 invalidate_handle( child );
217 WININET_Release(info);
220 BOOL WININET_Release( object_header_t *info )
222 ULONG refs = InterlockedDecrement(&info->refs);
223 TRACE( "object %p refcount = %d\n", info, refs );
224 if( !refs )
226 invalidate_handle(info);
227 if ( info->vtbl->CloseConnection )
229 TRACE( "closing connection %p\n", info);
230 info->vtbl->CloseConnection( info );
232 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
233 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
234 || !(info->dwInternalFlags & INET_OPENURL))
236 INTERNET_SendCallback(info, info->dwContext,
237 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
238 sizeof(HINTERNET));
240 TRACE( "destroying object %p\n", info);
241 if ( info->htype != WH_HINIT )
242 list_remove( &info->entry );
243 info->vtbl->Destroy( info );
245 if(info->hInternet) {
246 UINT_PTR handle = (UINT_PTR)info->hInternet;
248 EnterCriticalSection( &WININET_cs );
250 handle_table[handle] = NULL;
251 if(next_handle > handle)
252 next_handle = handle;
254 LeaveCriticalSection( &WININET_cs );
257 heap_free(info);
259 return TRUE;
262 /***********************************************************************
263 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
265 * PARAMS
266 * hinstDLL [I] handle to the DLL's instance
267 * fdwReason [I]
268 * lpvReserved [I] reserved, must be NULL
270 * RETURNS
271 * Success: TRUE
272 * Failure: FALSE
275 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
277 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
279 switch (fdwReason) {
280 case DLL_PROCESS_ATTACH:
282 g_dwTlsErrIndex = TlsAlloc();
284 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
285 return FALSE;
287 if(!init_urlcache())
289 TlsFree(g_dwTlsErrIndex);
290 return FALSE;
293 WININET_hModule = hinstDLL;
294 break;
296 case DLL_THREAD_ATTACH:
297 break;
299 case DLL_THREAD_DETACH:
300 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
302 heap_free(TlsGetValue(g_dwTlsErrIndex));
304 break;
306 case DLL_PROCESS_DETACH:
307 if (lpvReserved) break;
308 collect_connections(COLLECT_CLEANUP);
309 NETCON_unload();
310 free_urlcache();
311 free_cookie();
313 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
315 heap_free(TlsGetValue(g_dwTlsErrIndex));
316 TlsFree(g_dwTlsErrIndex);
318 break;
320 return TRUE;
323 /***********************************************************************
324 * DllInstall (WININET.@)
326 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
328 FIXME("(%x %s): stub\n", bInstall, debugstr_w(cmdline));
329 return S_OK;
332 /***********************************************************************
333 * INTERNET_SaveProxySettings
335 * Stores the proxy settings given by lpwai into the registry
337 * RETURNS
338 * ERROR_SUCCESS if no error, or error code on fail
340 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
342 HKEY key;
343 LONG ret;
345 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
346 return ret;
348 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
350 RegCloseKey( key );
351 return ret;
354 if (lpwpi->proxy)
356 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
358 RegCloseKey( key );
359 return ret;
362 else
364 if ((ret = RegDeleteValueW( key, szProxyServer )) && ret != ERROR_FILE_NOT_FOUND)
366 RegCloseKey( key );
367 return ret;
371 RegCloseKey(key);
372 return ERROR_SUCCESS;
375 /***********************************************************************
376 * INTERNET_FindProxyForProtocol
378 * Searches the proxy string for a proxy of the given protocol.
379 * Returns the found proxy, or the default proxy if none of the given
380 * protocol is found.
382 * PARAMETERS
383 * szProxy [In] proxy string to search
384 * proto [In] protocol to search for, e.g. "http"
385 * foundProxy [Out] found proxy
386 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
388 * RETURNS
389 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
390 * *foundProxyLen is set to the required size in WCHARs, including the
391 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
393 WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto)
395 WCHAR *ret = NULL;
396 const WCHAR *ptr;
398 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
400 /* First, look for the specified protocol (proto=scheme://host:port) */
401 for (ptr = szProxy; ptr && *ptr; )
403 LPCWSTR end, equal;
405 if (!(end = strchrW(ptr, ' ')))
406 end = ptr + strlenW(ptr);
407 if ((equal = strchrW(ptr, '=')) && equal < end &&
408 equal - ptr == strlenW(proto) &&
409 !strncmpiW(proto, ptr, strlenW(proto)))
411 ret = heap_strndupW(equal + 1, end - equal - 1);
412 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
413 return ret;
415 if (*end == ' ')
416 ptr = end + 1;
417 else
418 ptr = end;
421 /* It wasn't found: look for no protocol */
422 for (ptr = szProxy; ptr && *ptr; )
424 LPCWSTR end;
426 if (!(end = strchrW(ptr, ' ')))
427 end = ptr + strlenW(ptr);
428 if (!strchrW(ptr, '='))
430 ret = heap_strndupW(ptr, end - ptr);
431 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
432 return ret;
434 if (*end == ' ')
435 ptr = end + 1;
436 else
437 ptr = end;
440 return NULL;
443 /***********************************************************************
444 * InternetInitializeAutoProxyDll (WININET.@)
446 * Setup the internal proxy
448 * PARAMETERS
449 * dwReserved
451 * RETURNS
452 * FALSE on failure
455 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
457 FIXME("STUB\n");
458 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
459 return FALSE;
462 /***********************************************************************
463 * DetectAutoProxyUrl (WININET.@)
465 * Auto detect the proxy url
467 * RETURNS
468 * FALSE on failure
471 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
472 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
474 FIXME("STUB\n");
475 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
476 return FALSE;
479 static void FreeProxyInfo( proxyinfo_t *lpwpi )
481 heap_free(lpwpi->proxy);
482 heap_free(lpwpi->proxyBypass);
483 heap_free(lpwpi->proxyUsername);
484 heap_free(lpwpi->proxyPassword);
487 static proxyinfo_t *global_proxy;
489 static void free_global_proxy( void )
491 EnterCriticalSection( &WININET_cs );
492 if (global_proxy)
494 FreeProxyInfo( global_proxy );
495 heap_free( global_proxy );
497 LeaveCriticalSection( &WININET_cs );
500 static BOOL parse_proxy_url( proxyinfo_t *info, const WCHAR *url )
502 static const WCHAR fmt[] = {'%','.','*','s',':','%','u',0};
503 URL_COMPONENTSW uc = {sizeof(uc)};
505 uc.dwHostNameLength = 1;
506 uc.dwUserNameLength = 1;
507 uc.dwPasswordLength = 1;
509 if (!InternetCrackUrlW( url, 0, 0, &uc )) return FALSE;
510 if (!uc.dwHostNameLength)
512 if (!(info->proxy = heap_strdupW( url ))) return FALSE;
513 info->proxyUsername = NULL;
514 info->proxyPassword = NULL;
515 return TRUE;
517 if (!(info->proxy = heap_alloc( (uc.dwHostNameLength + 12) * sizeof(WCHAR) ))) return FALSE;
518 sprintfW( info->proxy, fmt, uc.dwHostNameLength, uc.lpszHostName, uc.nPort );
520 if (!uc.dwUserNameLength) info->proxyUsername = NULL;
521 else if (!(info->proxyUsername = heap_strndupW( uc.lpszUserName, uc.dwUserNameLength )))
523 heap_free( info->proxy );
524 return FALSE;
526 if (!uc.dwPasswordLength) info->proxyPassword = NULL;
527 else if (!(info->proxyPassword = heap_strndupW( uc.lpszPassword, uc.dwPasswordLength )))
529 heap_free( info->proxyUsername );
530 heap_free( info->proxy );
531 return FALSE;
533 return TRUE;
536 /***********************************************************************
537 * INTERNET_LoadProxySettings
539 * Loads proxy information from process-wide global settings, the registry,
540 * or the environment into lpwpi.
542 * The caller should call FreeProxyInfo when done with lpwpi.
544 * FIXME:
545 * The proxy may be specified in the form 'http=proxy.my.org'
546 * Presumably that means there can be ftp=ftpproxy.my.org too.
548 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
550 HKEY key;
551 DWORD type, len;
552 LPCSTR envproxy;
553 LONG ret;
555 memset( lpwpi, 0, sizeof(*lpwpi) );
557 EnterCriticalSection( &WININET_cs );
558 if (global_proxy)
560 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
561 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
562 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
564 LeaveCriticalSection( &WININET_cs );
566 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
568 FreeProxyInfo( lpwpi );
569 return ret;
572 len = sizeof(DWORD);
573 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
575 lpwpi->proxyEnabled = 0;
576 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
578 FreeProxyInfo( lpwpi );
579 RegCloseKey( key );
580 return ret;
584 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
586 /* figure out how much memory the proxy setting takes */
587 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
589 LPWSTR szProxy, p;
590 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
592 if (!(szProxy = heap_alloc(len)))
594 RegCloseKey( key );
595 FreeProxyInfo( lpwpi );
596 return ERROR_OUTOFMEMORY;
598 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
600 /* find the http proxy, and strip away everything else */
601 p = strstrW( szProxy, szHttp );
602 if (p)
604 p += lstrlenW( szHttp );
605 lstrcpyW( szProxy, p );
607 p = strchrW( szProxy, ';' );
608 if (p) *p = 0;
610 FreeProxyInfo( lpwpi );
611 lpwpi->proxy = szProxy;
612 lpwpi->proxyBypass = NULL;
614 TRACE("http proxy (from registry) = %s\n", debugstr_w(lpwpi->proxy));
616 else
618 TRACE("No proxy server settings in registry.\n");
619 FreeProxyInfo( lpwpi );
620 lpwpi->proxy = NULL;
621 lpwpi->proxyBypass = NULL;
624 else if (envproxy)
626 WCHAR *envproxyW;
628 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
629 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
631 RegCloseKey( key );
632 return ERROR_OUTOFMEMORY;
634 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
636 FreeProxyInfo( lpwpi );
637 if (parse_proxy_url( lpwpi, envproxyW ))
639 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
640 lpwpi->proxyEnabled = 1;
641 lpwpi->proxyBypass = NULL;
643 else
645 WARN("failed to parse http_proxy value %s\n", debugstr_w(envproxyW));
646 lpwpi->proxyEnabled = 0;
647 lpwpi->proxy = NULL;
648 lpwpi->proxyBypass = NULL;
650 heap_free( envproxyW );
653 if (lpwpi->proxyEnabled)
655 TRACE("Proxy is enabled.\n");
657 if (!(envproxy = getenv( "no_proxy" )))
659 /* figure out how much memory the proxy setting takes */
660 if (!RegQueryValueExW( key, szProxyOverride, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
662 LPWSTR szProxy;
664 if (!(szProxy = heap_alloc(len)))
666 RegCloseKey( key );
667 return ERROR_OUTOFMEMORY;
669 RegQueryValueExW( key, szProxyOverride, NULL, &type, (BYTE*)szProxy, &len );
671 heap_free( lpwpi->proxyBypass );
672 lpwpi->proxyBypass = szProxy;
674 TRACE("http proxy bypass (from registry) = %s\n", debugstr_w(lpwpi->proxyBypass));
676 else
678 heap_free( lpwpi->proxyBypass );
679 lpwpi->proxyBypass = NULL;
681 TRACE("No proxy bypass server settings in registry.\n");
684 else
686 WCHAR *envproxyW;
688 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
689 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
691 RegCloseKey( key );
692 return ERROR_OUTOFMEMORY;
694 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
696 heap_free( lpwpi->proxyBypass );
697 lpwpi->proxyBypass = envproxyW;
699 TRACE("http proxy bypass (from environment) = %s\n", debugstr_w(lpwpi->proxyBypass));
702 else TRACE("Proxy is disabled.\n");
704 RegCloseKey( key );
705 return ERROR_SUCCESS;
708 /***********************************************************************
709 * INTERNET_ConfigureProxy
711 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
713 proxyinfo_t wpi;
715 if (INTERNET_LoadProxySettings( &wpi ))
716 return FALSE;
718 if (wpi.proxyEnabled)
720 TRACE("http proxy = %s bypass = %s\n", debugstr_w(wpi.proxy), debugstr_w(wpi.proxyBypass));
722 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
723 lpwai->proxy = wpi.proxy;
724 lpwai->proxyBypass = wpi.proxyBypass;
725 lpwai->proxyUsername = wpi.proxyUsername;
726 lpwai->proxyPassword = wpi.proxyPassword;
727 return TRUE;
730 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
731 FreeProxyInfo(&wpi);
732 return FALSE;
735 /***********************************************************************
736 * dump_INTERNET_FLAGS
738 * Helper function to TRACE the internet flags.
740 * RETURNS
741 * None
744 static void dump_INTERNET_FLAGS(DWORD dwFlags)
746 #define FE(x) { x, #x }
747 static const wininet_flag_info flag[] = {
748 FE(INTERNET_FLAG_RELOAD),
749 FE(INTERNET_FLAG_RAW_DATA),
750 FE(INTERNET_FLAG_EXISTING_CONNECT),
751 FE(INTERNET_FLAG_ASYNC),
752 FE(INTERNET_FLAG_PASSIVE),
753 FE(INTERNET_FLAG_NO_CACHE_WRITE),
754 FE(INTERNET_FLAG_MAKE_PERSISTENT),
755 FE(INTERNET_FLAG_FROM_CACHE),
756 FE(INTERNET_FLAG_SECURE),
757 FE(INTERNET_FLAG_KEEP_CONNECTION),
758 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
759 FE(INTERNET_FLAG_READ_PREFETCH),
760 FE(INTERNET_FLAG_NO_COOKIES),
761 FE(INTERNET_FLAG_NO_AUTH),
762 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
763 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
764 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
765 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
766 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
767 FE(INTERNET_FLAG_RESYNCHRONIZE),
768 FE(INTERNET_FLAG_HYPERLINK),
769 FE(INTERNET_FLAG_NO_UI),
770 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
771 FE(INTERNET_FLAG_CACHE_ASYNC),
772 FE(INTERNET_FLAG_FORMS_SUBMIT),
773 FE(INTERNET_FLAG_NEED_FILE),
774 FE(INTERNET_FLAG_TRANSFER_ASCII),
775 FE(INTERNET_FLAG_TRANSFER_BINARY)
777 #undef FE
778 unsigned int i;
780 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
781 if (flag[i].val & dwFlags) {
782 TRACE(" %s", flag[i].name);
783 dwFlags &= ~flag[i].val;
786 if (dwFlags)
787 TRACE(" Unknown flags (%08x)\n", dwFlags);
788 else
789 TRACE("\n");
792 /***********************************************************************
793 * INTERNET_CloseHandle (internal)
795 * Close internet handle
798 static VOID APPINFO_Destroy(object_header_t *hdr)
800 appinfo_t *lpwai = (appinfo_t*)hdr;
802 TRACE("%p\n",lpwai);
804 heap_free(lpwai->agent);
805 heap_free(lpwai->proxy);
806 heap_free(lpwai->proxyBypass);
807 heap_free(lpwai->proxyUsername);
808 heap_free(lpwai->proxyPassword);
811 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
813 appinfo_t *ai = (appinfo_t*)hdr;
815 switch(option) {
816 case INTERNET_OPTION_HANDLE_TYPE:
817 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
819 if (*size < sizeof(ULONG))
820 return ERROR_INSUFFICIENT_BUFFER;
822 *size = sizeof(DWORD);
823 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
824 return ERROR_SUCCESS;
826 case INTERNET_OPTION_USER_AGENT: {
827 DWORD bufsize;
829 TRACE("INTERNET_OPTION_USER_AGENT\n");
831 bufsize = *size;
833 if (unicode) {
834 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
836 *size = (len + 1) * sizeof(WCHAR);
837 if(!buffer || bufsize < *size)
838 return ERROR_INSUFFICIENT_BUFFER;
840 if (ai->agent)
841 strcpyW(buffer, ai->agent);
842 else
843 *(WCHAR *)buffer = 0;
844 /* If the buffer is copied, the returned length doesn't include
845 * the NULL terminator.
847 *size = len;
848 }else {
849 if (ai->agent)
850 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
851 else
852 *size = 1;
853 if(!buffer || bufsize < *size)
854 return ERROR_INSUFFICIENT_BUFFER;
856 if (ai->agent)
857 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
858 else
859 *(char *)buffer = 0;
860 /* If the buffer is copied, the returned length doesn't include
861 * the NULL terminator.
863 *size -= 1;
866 return ERROR_SUCCESS;
869 case INTERNET_OPTION_PROXY:
870 if(!size) return ERROR_INVALID_PARAMETER;
871 if (unicode) {
872 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
873 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
874 LPWSTR proxy, proxy_bypass;
876 if (ai->proxy)
877 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
878 if (ai->proxyBypass)
879 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
880 if (!pi || *size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
882 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
883 return ERROR_INSUFFICIENT_BUFFER;
885 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
886 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
888 pi->dwAccessType = ai->accessType;
889 pi->lpszProxy = NULL;
890 pi->lpszProxyBypass = NULL;
891 if (ai->proxy) {
892 lstrcpyW(proxy, ai->proxy);
893 pi->lpszProxy = proxy;
896 if (ai->proxyBypass) {
897 lstrcpyW(proxy_bypass, ai->proxyBypass);
898 pi->lpszProxyBypass = proxy_bypass;
901 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
902 return ERROR_SUCCESS;
903 }else {
904 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
905 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
906 LPSTR proxy, proxy_bypass;
908 if (ai->proxy)
909 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
910 if (ai->proxyBypass)
911 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
912 NULL, 0, NULL, NULL);
913 if (!pi || *size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
915 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
916 return ERROR_INSUFFICIENT_BUFFER;
918 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
919 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
921 pi->dwAccessType = ai->accessType;
922 pi->lpszProxy = NULL;
923 pi->lpszProxyBypass = NULL;
924 if (ai->proxy) {
925 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
926 pi->lpszProxy = proxy;
929 if (ai->proxyBypass) {
930 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
931 proxyBypassBytesRequired, NULL, NULL);
932 pi->lpszProxyBypass = proxy_bypass;
935 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
936 return ERROR_SUCCESS;
939 case INTERNET_OPTION_CONNECT_TIMEOUT:
940 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
942 if (*size < sizeof(ULONG))
943 return ERROR_INSUFFICIENT_BUFFER;
945 *(ULONG*)buffer = ai->connect_timeout;
946 *size = sizeof(ULONG);
948 return ERROR_SUCCESS;
951 return INET_QueryOption(hdr, option, buffer, size, unicode);
954 static DWORD APPINFO_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
956 appinfo_t *ai = (appinfo_t*)hdr;
958 switch(option) {
959 case INTERNET_OPTION_CONNECT_TIMEOUT:
960 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
962 if(size != sizeof(connect_timeout))
963 return ERROR_INTERNET_BAD_OPTION_LENGTH;
964 if(!*(ULONG*)buf)
965 return ERROR_BAD_ARGUMENTS;
967 ai->connect_timeout = *(ULONG*)buf;
968 return ERROR_SUCCESS;
969 case INTERNET_OPTION_USER_AGENT:
970 heap_free(ai->agent);
971 if (!(ai->agent = heap_strdupW(buf))) return ERROR_OUTOFMEMORY;
972 return ERROR_SUCCESS;
975 return INET_SetOption(hdr, option, buf, size);
978 static const object_vtbl_t APPINFOVtbl = {
979 APPINFO_Destroy,
980 NULL,
981 APPINFO_QueryOption,
982 APPINFO_SetOption,
983 NULL,
984 NULL,
985 NULL,
986 NULL
990 /***********************************************************************
991 * InternetOpenW (WININET.@)
993 * Per-application initialization of wininet
995 * RETURNS
996 * HINTERNET on success
997 * NULL on failure
1000 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
1001 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
1003 appinfo_t *lpwai = NULL;
1005 if (TRACE_ON(wininet)) {
1006 #define FE(x) { x, #x }
1007 static const wininet_flag_info access_type[] = {
1008 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1009 FE(INTERNET_OPEN_TYPE_DIRECT),
1010 FE(INTERNET_OPEN_TYPE_PROXY),
1011 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1013 #undef FE
1014 DWORD i;
1015 const char *access_type_str = "Unknown";
1017 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1018 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1019 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1020 if (access_type[i].val == dwAccessType) {
1021 access_type_str = access_type[i].name;
1022 break;
1025 TRACE(" access type : %s\n", access_type_str);
1026 TRACE(" flags :");
1027 dump_INTERNET_FLAGS(dwFlags);
1030 /* Clear any error information */
1031 INTERNET_SetLastError(0);
1033 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1034 SetLastError(ERROR_INVALID_PARAMETER);
1035 return NULL;
1038 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1039 if (!lpwai) {
1040 SetLastError(ERROR_OUTOFMEMORY);
1041 return NULL;
1044 lpwai->hdr.htype = WH_HINIT;
1045 lpwai->hdr.dwFlags = dwFlags;
1046 lpwai->accessType = dwAccessType;
1047 lpwai->proxyUsername = NULL;
1048 lpwai->proxyPassword = NULL;
1049 lpwai->connect_timeout = connect_timeout;
1051 lpwai->agent = heap_strdupW(lpszAgent);
1052 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1053 INTERNET_ConfigureProxy( lpwai );
1054 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1055 lpwai->proxy = heap_strdupW(lpszProxy);
1056 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1059 TRACE("returning %p\n", lpwai);
1061 return lpwai->hdr.hInternet;
1065 /***********************************************************************
1066 * InternetOpenA (WININET.@)
1068 * Per-application initialization of wininet
1070 * RETURNS
1071 * HINTERNET on success
1072 * NULL on failure
1075 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1076 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1078 WCHAR *szAgent, *szProxy, *szBypass;
1079 HINTERNET rc;
1081 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1082 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1084 szAgent = heap_strdupAtoW(lpszAgent);
1085 szProxy = heap_strdupAtoW(lpszProxy);
1086 szBypass = heap_strdupAtoW(lpszProxyBypass);
1088 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1090 heap_free(szAgent);
1091 heap_free(szProxy);
1092 heap_free(szBypass);
1093 return rc;
1096 /***********************************************************************
1097 * InternetGetLastResponseInfoA (WININET.@)
1099 * Return last wininet error description on the calling thread
1101 * RETURNS
1102 * TRUE on success of writing to buffer
1103 * FALSE on failure
1106 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1107 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1109 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1111 TRACE("\n");
1113 if (lpwite)
1115 *lpdwError = lpwite->dwError;
1116 if (lpwite->dwError)
1118 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1119 *lpdwBufferLength = strlen(lpszBuffer);
1121 else
1122 *lpdwBufferLength = 0;
1124 else
1126 *lpdwError = 0;
1127 *lpdwBufferLength = 0;
1130 return TRUE;
1133 /***********************************************************************
1134 * InternetGetLastResponseInfoW (WININET.@)
1136 * Return last wininet error description on the calling thread
1138 * RETURNS
1139 * TRUE on success of writing to buffer
1140 * FALSE on failure
1143 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1144 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1146 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1148 TRACE("\n");
1150 if (lpwite)
1152 *lpdwError = lpwite->dwError;
1153 if (lpwite->dwError)
1155 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1156 *lpdwBufferLength = lstrlenW(lpszBuffer);
1158 else
1159 *lpdwBufferLength = 0;
1161 else
1163 *lpdwError = 0;
1164 *lpdwBufferLength = 0;
1167 return TRUE;
1170 /***********************************************************************
1171 * InternetGetConnectedState (WININET.@)
1173 * Return connected state
1175 * RETURNS
1176 * TRUE if connected
1177 * if lpdwStatus is not null, return the status (off line,
1178 * modem, lan...) in it.
1179 * FALSE if not connected
1181 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1183 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1185 if (lpdwStatus) {
1186 WARN("always returning LAN connection.\n");
1187 *lpdwStatus = INTERNET_CONNECTION_LAN;
1189 return TRUE;
1193 /***********************************************************************
1194 * InternetGetConnectedStateExW (WININET.@)
1196 * Return connected state
1198 * PARAMS
1200 * lpdwStatus [O] Flags specifying the status of the internet connection.
1201 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1202 * dwNameLen [I] Size of the buffer, in characters.
1203 * dwReserved [I] Reserved. Must be set to 0.
1205 * RETURNS
1206 * TRUE if connected
1207 * if lpdwStatus is not null, return the status (off line,
1208 * modem, lan...) in it.
1209 * FALSE if not connected
1211 * NOTES
1212 * If the system has no available network connections, an empty string is
1213 * stored in lpszConnectionName. If there is a LAN connection, a localized
1214 * "LAN Connection" string is stored. Presumably, if only a dial-up
1215 * connection is available then the name of the dial-up connection is
1216 * returned. Why any application, other than the "Internet Settings" CPL,
1217 * would want to use this function instead of the simpler InternetGetConnectedStateW
1218 * function is beyond me.
1220 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1221 DWORD dwNameLen, DWORD dwReserved)
1223 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1225 /* Must be zero */
1226 if(dwReserved)
1227 return FALSE;
1229 if (lpdwStatus) {
1230 WARN("always returning LAN connection.\n");
1231 *lpdwStatus = INTERNET_CONNECTION_LAN;
1234 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1235 * the resource, avoid it as we must not change the buffer in this case */
1236 if(lpszConnectionName && dwNameLen) {
1237 *lpszConnectionName = '\0';
1238 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1241 return TRUE;
1245 /***********************************************************************
1246 * InternetGetConnectedStateExA (WININET.@)
1248 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1249 DWORD dwNameLen, DWORD dwReserved)
1251 LPWSTR lpwszConnectionName = NULL;
1252 BOOL rc;
1254 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1256 if (lpszConnectionName && dwNameLen > 0)
1257 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1259 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1260 dwReserved);
1261 if (rc && lpwszConnectionName)
1262 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1263 dwNameLen, NULL, NULL);
1265 heap_free(lpwszConnectionName);
1266 return rc;
1270 /***********************************************************************
1271 * InternetConnectW (WININET.@)
1273 * Open a ftp, gopher or http session
1275 * RETURNS
1276 * HINTERNET a session handle on success
1277 * NULL on failure
1280 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1281 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1282 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1283 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1285 appinfo_t *hIC;
1286 HINTERNET rc = NULL;
1287 DWORD res = ERROR_SUCCESS;
1289 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1290 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1292 if (!lpszServerName)
1294 SetLastError(ERROR_INVALID_PARAMETER);
1295 return NULL;
1298 hIC = (appinfo_t*)get_handle_object( hInternet );
1299 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1301 res = ERROR_INVALID_HANDLE;
1302 goto lend;
1305 switch (dwService)
1307 case INTERNET_SERVICE_FTP:
1308 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1309 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1310 if(!rc)
1311 res = INTERNET_GetLastError();
1312 break;
1314 case INTERNET_SERVICE_HTTP:
1315 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1316 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1317 break;
1319 case INTERNET_SERVICE_GOPHER:
1320 default:
1321 break;
1323 lend:
1324 if( hIC )
1325 WININET_Release( &hIC->hdr );
1327 TRACE("returning %p\n", rc);
1328 SetLastError(res);
1329 return rc;
1333 /***********************************************************************
1334 * InternetConnectA (WININET.@)
1336 * Open a ftp, gopher or http session
1338 * RETURNS
1339 * HINTERNET a session handle on success
1340 * NULL on failure
1343 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1344 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1345 LPCSTR lpszUserName, LPCSTR lpszPassword,
1346 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1348 HINTERNET rc = NULL;
1349 LPWSTR szServerName;
1350 LPWSTR szUserName;
1351 LPWSTR szPassword;
1353 szServerName = heap_strdupAtoW(lpszServerName);
1354 szUserName = heap_strdupAtoW(lpszUserName);
1355 szPassword = heap_strdupAtoW(lpszPassword);
1357 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1358 szUserName, szPassword, dwService, dwFlags, dwContext);
1360 heap_free(szServerName);
1361 heap_free(szUserName);
1362 heap_free(szPassword);
1363 return rc;
1367 /***********************************************************************
1368 * InternetFindNextFileA (WININET.@)
1370 * Continues a file search from a previous call to FindFirstFile
1372 * RETURNS
1373 * TRUE on success
1374 * FALSE on failure
1377 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1379 BOOL ret;
1380 WIN32_FIND_DATAW fd;
1382 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1383 if(lpvFindData)
1384 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1385 return ret;
1388 /***********************************************************************
1389 * InternetFindNextFileW (WININET.@)
1391 * Continues a file search from a previous call to FindFirstFile
1393 * RETURNS
1394 * TRUE on success
1395 * FALSE on failure
1398 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1400 object_header_t *hdr;
1401 DWORD res;
1403 TRACE("\n");
1405 hdr = get_handle_object(hFind);
1406 if(!hdr) {
1407 WARN("Invalid handle\n");
1408 SetLastError(ERROR_INVALID_HANDLE);
1409 return FALSE;
1412 if(hdr->vtbl->FindNextFileW) {
1413 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1414 }else {
1415 WARN("Handle doesn't support NextFile\n");
1416 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1419 WININET_Release(hdr);
1421 if(res != ERROR_SUCCESS)
1422 SetLastError(res);
1423 return res == ERROR_SUCCESS;
1426 /***********************************************************************
1427 * InternetCloseHandle (WININET.@)
1429 * Generic close handle function
1431 * RETURNS
1432 * TRUE on success
1433 * FALSE on failure
1436 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1438 object_header_t *obj;
1440 TRACE("%p\n", hInternet);
1442 obj = get_handle_object( hInternet );
1443 if (!obj) {
1444 SetLastError(ERROR_INVALID_HANDLE);
1445 return FALSE;
1448 invalidate_handle(obj);
1449 WININET_Release(obj);
1451 return TRUE;
1454 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1456 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1458 if (!*component_length)
1459 return TRUE;
1461 if (!*component) {
1462 *(const WCHAR**)component = value;
1463 *component_length = len;
1464 return TRUE;
1467 if (*component_length < len+1) {
1468 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1469 return FALSE;
1472 *component_length = len;
1473 if(len)
1474 memcpy(*component, value, len*sizeof(WCHAR));
1475 (*component)[len] = 0;
1476 return TRUE;
1479 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1480 const char *url_a)
1482 size_t size, ret_size = *ret_length;
1484 if (!*ret_length)
1485 return TRUE;
1486 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1488 if (!*comp) {
1489 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1490 *ret_length = size;
1491 return TRUE;
1494 if (size+1 > ret_size) {
1495 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1496 *ret_length = size+1;
1497 return FALSE;
1500 *ret_length = size;
1501 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1502 (*comp)[size] = 0;
1503 return TRUE;
1506 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1508 *len_w = len_a;
1510 if(!comp_a) {
1511 *comp_w = NULL;
1512 return TRUE;
1515 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1516 SetLastError(ERROR_OUTOFMEMORY);
1517 return FALSE;
1520 return TRUE;
1523 /***********************************************************************
1524 * InternetCrackUrlA (WININET.@)
1526 * See InternetCrackUrlW.
1528 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1530 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1531 URL_COMPONENTSW comp;
1532 WCHAR *url_w = NULL;
1533 BOOL ret;
1535 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1537 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1538 SetLastError(ERROR_INVALID_PARAMETER);
1539 return FALSE;
1542 comp.dwStructSize = sizeof(comp);
1544 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1545 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1546 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1547 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1548 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1549 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1550 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1551 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1552 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1553 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1554 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1555 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1557 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1558 SetLastError(ERROR_OUTOFMEMORY);
1559 ret = FALSE;
1562 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1563 ret_comp->nScheme = comp.nScheme;
1564 ret_comp->nPort = comp.nPort;
1566 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1567 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1568 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1569 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1570 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1571 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1572 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1573 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1574 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1575 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1576 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1577 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1579 if(ret)
1580 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1581 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1582 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1583 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1584 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1587 heap_free(host);
1588 heap_free(user);
1589 heap_free(pass);
1590 heap_free(path);
1591 heap_free(scheme);
1592 heap_free(extra);
1593 heap_free(url_w);
1594 return ret;
1597 static const WCHAR url_schemes[][7] =
1599 {'f','t','p',0},
1600 {'g','o','p','h','e','r',0},
1601 {'h','t','t','p',0},
1602 {'h','t','t','p','s',0},
1603 {'f','i','l','e',0},
1604 {'n','e','w','s',0},
1605 {'m','a','i','l','t','o',0},
1606 {'r','e','s',0},
1609 /***********************************************************************
1610 * GetInternetSchemeW (internal)
1612 * Get scheme of url
1614 * RETURNS
1615 * scheme on success
1616 * INTERNET_SCHEME_UNKNOWN on failure
1619 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1621 int i;
1623 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1625 if(lpszScheme==NULL)
1626 return INTERNET_SCHEME_UNKNOWN;
1628 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1629 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1630 return INTERNET_SCHEME_FIRST + i;
1632 return INTERNET_SCHEME_UNKNOWN;
1635 /***********************************************************************
1636 * InternetCrackUrlW (WININET.@)
1638 * Break up URL into its components
1640 * RETURNS
1641 * TRUE on success
1642 * FALSE on failure
1644 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1647 * RFC 1808
1648 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1651 LPCWSTR lpszParam = NULL;
1652 BOOL found_colon = FALSE;
1653 LPCWSTR lpszap;
1654 LPCWSTR lpszcp = NULL, lpszNetLoc;
1656 TRACE("(%s %u %x %p)\n",
1657 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1658 dwUrlLength, dwFlags, lpUC);
1660 if (!lpszUrl || !*lpszUrl || !lpUC)
1662 SetLastError(ERROR_INVALID_PARAMETER);
1663 return FALSE;
1665 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1667 if (dwFlags & ICU_DECODE)
1669 WCHAR *url_tmp;
1670 DWORD len = dwUrlLength + 1;
1671 BOOL ret;
1673 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1675 SetLastError(ERROR_OUTOFMEMORY);
1676 return FALSE;
1678 ret = InternetCanonicalizeUrlW(url_tmp, url_tmp, &len, ICU_DECODE | ICU_NO_ENCODE);
1679 if (ret)
1680 ret = InternetCrackUrlW(url_tmp, len, dwFlags & ~ICU_DECODE, lpUC);
1681 heap_free(url_tmp);
1682 return ret;
1684 lpszap = lpszUrl;
1686 /* Determine if the URI is absolute. */
1687 while (lpszap - lpszUrl < dwUrlLength)
1689 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1691 lpszap++;
1692 continue;
1694 if (*lpszap == ':')
1696 found_colon = TRUE;
1697 lpszcp = lpszap;
1699 else
1701 lpszcp = lpszUrl; /* Relative url */
1704 break;
1707 if(!found_colon){
1708 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1709 return FALSE;
1712 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1713 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1715 /* Parse <params> */
1716 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1717 if(!lpszParam)
1718 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1720 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1721 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1722 return FALSE;
1725 /* Get scheme first. */
1726 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1727 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1728 return FALSE;
1730 /* Eat ':' in protocol. */
1731 lpszcp++;
1733 /* double slash indicates the net_loc portion is present */
1734 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1736 lpszcp += 2;
1738 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1739 if (lpszParam)
1741 if (lpszNetLoc)
1742 lpszNetLoc = min(lpszNetLoc, lpszParam);
1743 else
1744 lpszNetLoc = lpszParam;
1746 else if (!lpszNetLoc)
1747 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1749 /* Parse net-loc */
1750 if (lpszNetLoc)
1752 LPCWSTR lpszHost;
1753 LPCWSTR lpszPort;
1755 /* [<user>[<:password>]@]<host>[:<port>] */
1756 /* First find the user and password if they exist */
1758 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1759 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1761 /* username and password not specified. */
1762 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1763 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1765 else /* Parse out username and password */
1767 LPCWSTR lpszUser = lpszcp;
1768 LPCWSTR lpszPasswd = lpszHost;
1770 while (lpszcp < lpszHost)
1772 if (*lpszcp == ':')
1773 lpszPasswd = lpszcp;
1775 lpszcp++;
1778 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1779 return FALSE;
1781 if (lpszPasswd != lpszHost)
1782 lpszPasswd++;
1783 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1784 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1785 return FALSE;
1787 lpszcp++; /* Advance to beginning of host */
1790 /* Parse <host><:port> */
1792 lpszHost = lpszcp;
1793 lpszPort = lpszNetLoc;
1795 /* special case for res:// URLs: there is no port here, so the host is the
1796 entire string up to the first '/' */
1797 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1799 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1800 return FALSE;
1801 lpszcp=lpszNetLoc;
1803 else
1805 while (lpszcp < lpszNetLoc)
1807 if (*lpszcp == ':')
1808 lpszPort = lpszcp;
1810 lpszcp++;
1813 /* If the scheme is "file" and the host is just one letter, it's not a host */
1814 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1816 lpszcp=lpszHost;
1817 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1819 else
1821 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1822 return FALSE;
1823 if (lpszPort != lpszNetLoc)
1824 lpUC->nPort = atoiW(++lpszPort);
1825 else switch (lpUC->nScheme)
1827 case INTERNET_SCHEME_HTTP:
1828 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1829 break;
1830 case INTERNET_SCHEME_HTTPS:
1831 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1832 break;
1833 case INTERNET_SCHEME_FTP:
1834 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1835 break;
1836 case INTERNET_SCHEME_GOPHER:
1837 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1838 break;
1839 default:
1840 break;
1846 else
1848 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1849 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1850 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1853 /* Here lpszcp points to:
1855 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1856 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1858 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1860 DWORD len;
1862 /* Only truncate the parameter list if it's already been saved
1863 * in lpUC->lpszExtraInfo.
1865 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1866 len = lpszParam - lpszcp;
1867 else
1869 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1870 * newlines if necessary.
1872 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1873 if (lpsznewline != NULL)
1874 len = lpsznewline - lpszcp;
1875 else
1876 len = dwUrlLength-(lpszcp-lpszUrl);
1878 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1879 lpUC->nScheme == INTERNET_SCHEME_FILE)
1881 WCHAR tmppath[MAX_PATH];
1882 if (*lpszcp == '/')
1884 len = MAX_PATH;
1885 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1887 else
1889 WCHAR *iter;
1890 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1891 tmppath[len] = '\0';
1893 iter = tmppath;
1894 while (*iter) {
1895 if (*iter == '/')
1896 *iter = '\\';
1897 ++iter;
1900 /* if ends in \. or \.. append a backslash */
1901 if (tmppath[len - 1] == '.' &&
1902 (tmppath[len - 2] == '\\' ||
1903 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1905 if (len < MAX_PATH - 1)
1907 tmppath[len] = '\\';
1908 tmppath[len+1] = '\0';
1909 ++len;
1912 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1913 return FALSE;
1915 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1916 return FALSE;
1918 else
1920 set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, 0);
1923 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1924 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1925 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1926 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1927 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1929 return TRUE;
1932 /***********************************************************************
1933 * InternetAttemptConnect (WININET.@)
1935 * Attempt to make a connection to the internet
1937 * RETURNS
1938 * ERROR_SUCCESS on success
1939 * Error value on failure
1942 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1944 FIXME("Stub\n");
1945 return ERROR_SUCCESS;
1949 /***********************************************************************
1950 * convert_url_canonicalization_flags
1952 * Helper for InternetCanonicalizeUrl
1954 * PARAMS
1955 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1957 * RETURNS
1958 * Flags suitable for UrlCanonicalize
1960 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1962 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1964 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1965 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1966 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1967 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1968 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1969 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1970 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1972 return dwUrlFlags;
1975 /***********************************************************************
1976 * InternetCanonicalizeUrlA (WININET.@)
1978 * Escape unsafe characters and spaces
1980 * RETURNS
1981 * TRUE on success
1982 * FALSE on failure
1985 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1986 LPDWORD lpdwBufferLength, DWORD dwFlags)
1988 HRESULT hr;
1990 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1991 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1993 dwFlags = convert_url_canonicalization_flags(dwFlags);
1994 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1995 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1996 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1998 return hr == S_OK;
2001 /***********************************************************************
2002 * InternetCanonicalizeUrlW (WININET.@)
2004 * Escape unsafe characters and spaces
2006 * RETURNS
2007 * TRUE on success
2008 * FALSE on failure
2011 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2012 LPDWORD lpdwBufferLength, DWORD dwFlags)
2014 HRESULT hr;
2016 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2017 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2019 dwFlags = convert_url_canonicalization_flags(dwFlags);
2020 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2021 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2022 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2024 return hr == S_OK;
2027 /* #################################################### */
2029 static INTERNET_STATUS_CALLBACK set_status_callback(
2030 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2032 INTERNET_STATUS_CALLBACK ret;
2034 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2035 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2037 ret = lpwh->lpfnStatusCB;
2038 lpwh->lpfnStatusCB = callback;
2040 return ret;
2043 /***********************************************************************
2044 * InternetSetStatusCallbackA (WININET.@)
2046 * Sets up a callback function which is called as progress is made
2047 * during an operation.
2049 * RETURNS
2050 * Previous callback or NULL on success
2051 * INTERNET_INVALID_STATUS_CALLBACK on failure
2054 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2055 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2057 INTERNET_STATUS_CALLBACK retVal;
2058 object_header_t *lpwh;
2060 TRACE("%p\n", hInternet);
2062 if (!(lpwh = get_handle_object(hInternet)))
2063 return INTERNET_INVALID_STATUS_CALLBACK;
2065 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2067 WININET_Release( lpwh );
2068 return retVal;
2071 /***********************************************************************
2072 * InternetSetStatusCallbackW (WININET.@)
2074 * Sets up a callback function which is called as progress is made
2075 * during an operation.
2077 * RETURNS
2078 * Previous callback or NULL on success
2079 * INTERNET_INVALID_STATUS_CALLBACK on failure
2082 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2083 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2085 INTERNET_STATUS_CALLBACK retVal;
2086 object_header_t *lpwh;
2088 TRACE("%p\n", hInternet);
2090 if (!(lpwh = get_handle_object(hInternet)))
2091 return INTERNET_INVALID_STATUS_CALLBACK;
2093 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2095 WININET_Release( lpwh );
2096 return retVal;
2099 /***********************************************************************
2100 * InternetSetFilePointer (WININET.@)
2102 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2103 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2105 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2106 return FALSE;
2109 /***********************************************************************
2110 * InternetWriteFile (WININET.@)
2112 * Write data to an open internet file
2114 * RETURNS
2115 * TRUE on success
2116 * FALSE on failure
2119 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2120 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2122 object_header_t *lpwh;
2123 BOOL res;
2125 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2127 lpwh = get_handle_object( hFile );
2128 if (!lpwh) {
2129 WARN("Invalid handle\n");
2130 SetLastError(ERROR_INVALID_HANDLE);
2131 return FALSE;
2134 if(lpwh->vtbl->WriteFile) {
2135 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2136 }else {
2137 WARN("No Writefile method.\n");
2138 res = ERROR_INVALID_HANDLE;
2141 WININET_Release( lpwh );
2143 if(res != ERROR_SUCCESS)
2144 SetLastError(res);
2145 return res == ERROR_SUCCESS;
2149 /***********************************************************************
2150 * InternetReadFile (WININET.@)
2152 * Read data from an open internet file
2154 * RETURNS
2155 * TRUE on success
2156 * FALSE on failure
2159 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2160 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2162 object_header_t *hdr;
2163 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2165 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2167 hdr = get_handle_object(hFile);
2168 if (!hdr) {
2169 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2170 return FALSE;
2173 if(hdr->vtbl->ReadFile)
2174 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2176 WININET_Release(hdr);
2178 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2179 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2181 if(res != ERROR_SUCCESS)
2182 SetLastError(res);
2183 return res == ERROR_SUCCESS;
2186 /***********************************************************************
2187 * InternetReadFileExA (WININET.@)
2189 * Read data from an open internet file
2191 * PARAMS
2192 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2193 * lpBuffersOut [I/O] Buffer.
2194 * dwFlags [I] Flags. See notes.
2195 * dwContext [I] Context for callbacks.
2197 * RETURNS
2198 * TRUE on success
2199 * FALSE on failure
2201 * NOTES
2202 * The parameter dwFlags include zero or more of the following flags:
2203 *|IRF_ASYNC - Makes the call asynchronous.
2204 *|IRF_SYNC - Makes the call synchronous.
2205 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2206 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2208 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2210 * SEE
2211 * InternetOpenUrlA(), HttpOpenRequestA()
2213 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2214 DWORD dwFlags, DWORD_PTR dwContext)
2216 object_header_t *hdr;
2217 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2219 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2221 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2222 SetLastError(ERROR_INVALID_PARAMETER);
2223 return FALSE;
2226 hdr = get_handle_object(hFile);
2227 if (!hdr) {
2228 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2229 return FALSE;
2232 if(hdr->vtbl->ReadFileEx)
2233 res = hdr->vtbl->ReadFileEx(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2234 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2236 WININET_Release(hdr);
2238 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2239 res, lpBuffersOut->dwBufferLength);
2241 if(res != ERROR_SUCCESS)
2242 SetLastError(res);
2243 return res == ERROR_SUCCESS;
2246 /***********************************************************************
2247 * InternetReadFileExW (WININET.@)
2248 * SEE
2249 * InternetReadFileExA()
2251 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2252 DWORD dwFlags, DWORD_PTR dwContext)
2254 object_header_t *hdr;
2255 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2257 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2259 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2260 SetLastError(ERROR_INVALID_PARAMETER);
2261 return FALSE;
2264 hdr = get_handle_object(hFile);
2265 if (!hdr) {
2266 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2267 return FALSE;
2270 if(hdr->vtbl->ReadFileEx)
2271 res = hdr->vtbl->ReadFileEx(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2272 dwFlags, dwContext);
2274 WININET_Release(hdr);
2276 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2277 res, lpBuffer->dwBufferLength);
2279 if(res != ERROR_SUCCESS)
2280 SetLastError(res);
2281 return res == ERROR_SUCCESS;
2284 static WCHAR *get_proxy_autoconfig_url(void)
2286 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2288 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2289 WCHAR *ret = NULL;
2290 SIZE_T len;
2291 const void *ref;
2293 if (!settings) return NULL;
2295 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2297 CFRelease( settings );
2298 return NULL;
2300 len = CFStringGetLength( ref );
2301 if (len)
2302 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2303 if (ret)
2305 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2306 ret[len] = 0;
2308 TRACE( "returning %s\n", debugstr_w(ret) );
2309 CFRelease( settings );
2310 return ret;
2311 #else
2312 FIXME( "no support on this platform\n" );
2313 return NULL;
2314 #endif
2317 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2319 /* FIXME: This function currently handles more options than it should. Options requiring
2320 * proper handles should be moved to proper functions */
2321 switch(option) {
2322 case INTERNET_OPTION_HTTP_VERSION:
2323 if (*size < sizeof(HTTP_VERSION_INFO))
2324 return ERROR_INSUFFICIENT_BUFFER;
2327 * Presently hardcoded to 1.1
2329 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2330 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2331 *size = sizeof(HTTP_VERSION_INFO);
2333 return ERROR_SUCCESS;
2335 case INTERNET_OPTION_CONNECTED_STATE:
2336 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2338 if (*size < sizeof(ULONG))
2339 return ERROR_INSUFFICIENT_BUFFER;
2341 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2342 *size = sizeof(ULONG);
2344 return ERROR_SUCCESS;
2346 case INTERNET_OPTION_PROXY: {
2347 appinfo_t ai;
2348 BOOL ret;
2350 TRACE("Getting global proxy info\n");
2351 memset(&ai, 0, sizeof(appinfo_t));
2352 INTERNET_ConfigureProxy(&ai);
2354 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2355 APPINFO_Destroy(&ai.hdr);
2356 return ret;
2359 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2360 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2362 if (*size < sizeof(ULONG))
2363 return ERROR_INSUFFICIENT_BUFFER;
2365 *(ULONG*)buffer = max_conns;
2366 *size = sizeof(ULONG);
2368 return ERROR_SUCCESS;
2370 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2371 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2373 if (*size < sizeof(ULONG))
2374 return ERROR_INSUFFICIENT_BUFFER;
2376 *(ULONG*)buffer = max_1_0_conns;
2377 *size = sizeof(ULONG);
2379 return ERROR_SUCCESS;
2381 case INTERNET_OPTION_SECURITY_FLAGS:
2382 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2383 return ERROR_SUCCESS;
2385 case INTERNET_OPTION_VERSION: {
2386 static const INTERNET_VERSION_INFO info = { 1, 2 };
2388 TRACE("INTERNET_OPTION_VERSION\n");
2390 if (*size < sizeof(INTERNET_VERSION_INFO))
2391 return ERROR_INSUFFICIENT_BUFFER;
2393 memcpy(buffer, &info, sizeof(info));
2394 *size = sizeof(info);
2396 return ERROR_SUCCESS;
2399 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2400 WCHAR *url;
2401 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2402 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2403 DWORD res = ERROR_SUCCESS, i;
2404 proxyinfo_t pi;
2405 LONG ret;
2407 TRACE("Getting global proxy info\n");
2408 if((ret = INTERNET_LoadProxySettings(&pi)))
2409 return ret;
2411 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2413 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2414 FreeProxyInfo(&pi);
2415 return ERROR_INSUFFICIENT_BUFFER;
2418 url = get_proxy_autoconfig_url();
2420 for (i = 0; i < con->dwOptionCount; i++) {
2421 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2422 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2424 switch (optionW->dwOption) {
2425 case INTERNET_PER_CONN_FLAGS:
2426 if(pi.proxyEnabled)
2427 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2428 else
2429 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2430 if (url)
2431 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2432 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2433 break;
2435 case INTERNET_PER_CONN_PROXY_SERVER:
2436 if (unicode)
2437 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2438 else
2439 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2440 break;
2442 case INTERNET_PER_CONN_PROXY_BYPASS:
2443 if (unicode)
2444 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2445 else
2446 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2447 break;
2449 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2450 if (!url)
2451 optionW->Value.pszValue = NULL;
2452 else if (unicode)
2453 optionW->Value.pszValue = heap_strdupW(url);
2454 else
2455 optionA->Value.pszValue = heap_strdupWtoA(url);
2456 break;
2458 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2459 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2460 break;
2462 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2463 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2464 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2465 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2466 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2467 memset(&optionW->Value, 0, sizeof(optionW->Value));
2468 break;
2470 default:
2471 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2472 res = ERROR_INVALID_PARAMETER;
2473 break;
2476 heap_free(url);
2477 FreeProxyInfo(&pi);
2479 return res;
2481 case INTERNET_OPTION_REQUEST_FLAGS:
2482 case INTERNET_OPTION_USER_AGENT:
2483 *size = 0;
2484 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2485 case INTERNET_OPTION_POLICY:
2486 return ERROR_INVALID_PARAMETER;
2487 case INTERNET_OPTION_CONNECT_TIMEOUT:
2488 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2490 if (*size < sizeof(ULONG))
2491 return ERROR_INSUFFICIENT_BUFFER;
2493 *(ULONG*)buffer = connect_timeout;
2494 *size = sizeof(ULONG);
2496 return ERROR_SUCCESS;
2499 FIXME("Stub for %d\n", option);
2500 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2503 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2505 switch(option) {
2506 case INTERNET_OPTION_CONTEXT_VALUE:
2507 if (!size)
2508 return ERROR_INVALID_PARAMETER;
2510 if (*size < sizeof(DWORD_PTR)) {
2511 *size = sizeof(DWORD_PTR);
2512 return ERROR_INSUFFICIENT_BUFFER;
2514 if (!buffer)
2515 return ERROR_INVALID_PARAMETER;
2517 *(DWORD_PTR *)buffer = hdr->dwContext;
2518 *size = sizeof(DWORD_PTR);
2519 return ERROR_SUCCESS;
2521 case INTERNET_OPTION_REQUEST_FLAGS:
2522 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2523 *size = sizeof(DWORD);
2524 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2526 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2527 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2528 WARN("Called on global option %u\n", option);
2529 return ERROR_INTERNET_INVALID_OPERATION;
2532 /* FIXME: we shouldn't call it here */
2533 return query_global_option(option, buffer, size, unicode);
2536 /***********************************************************************
2537 * InternetQueryOptionW (WININET.@)
2539 * Queries an options on the specified handle
2541 * RETURNS
2542 * TRUE on success
2543 * FALSE on failure
2546 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2547 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2549 object_header_t *hdr;
2550 DWORD res = ERROR_INVALID_HANDLE;
2552 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2554 if(hInternet) {
2555 hdr = get_handle_object(hInternet);
2556 if (hdr) {
2557 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2558 WININET_Release(hdr);
2560 }else {
2561 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2564 if(res != ERROR_SUCCESS)
2565 SetLastError(res);
2566 return res == ERROR_SUCCESS;
2569 /***********************************************************************
2570 * InternetQueryOptionA (WININET.@)
2572 * Queries an options on the specified handle
2574 * RETURNS
2575 * TRUE on success
2576 * FALSE on failure
2579 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2580 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2582 object_header_t *hdr;
2583 DWORD res = ERROR_INVALID_HANDLE;
2585 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2587 if(hInternet) {
2588 hdr = get_handle_object(hInternet);
2589 if (hdr) {
2590 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2591 WININET_Release(hdr);
2593 }else {
2594 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2597 if(res != ERROR_SUCCESS)
2598 SetLastError(res);
2599 return res == ERROR_SUCCESS;
2602 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2604 switch(option) {
2605 case INTERNET_OPTION_CALLBACK:
2606 WARN("Not settable option %u\n", option);
2607 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2608 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2609 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2610 WARN("Called on global option %u\n", option);
2611 return ERROR_INTERNET_INVALID_OPERATION;
2614 return ERROR_INTERNET_INVALID_OPTION;
2617 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2619 switch(option) {
2620 case INTERNET_OPTION_CALLBACK:
2621 WARN("Not global option %u\n", option);
2622 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2624 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2625 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2627 if(size != sizeof(max_conns))
2628 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2629 if(!*(ULONG*)buf)
2630 return ERROR_BAD_ARGUMENTS;
2632 max_conns = *(ULONG*)buf;
2633 return ERROR_SUCCESS;
2635 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2636 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2638 if(size != sizeof(max_1_0_conns))
2639 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2640 if(!*(ULONG*)buf)
2641 return ERROR_BAD_ARGUMENTS;
2643 max_1_0_conns = *(ULONG*)buf;
2644 return ERROR_SUCCESS;
2646 case INTERNET_OPTION_CONNECT_TIMEOUT:
2647 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2649 if(size != sizeof(connect_timeout))
2650 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2651 if(!*(ULONG*)buf)
2652 return ERROR_BAD_ARGUMENTS;
2654 connect_timeout = *(ULONG*)buf;
2655 return ERROR_SUCCESS;
2657 case INTERNET_OPTION_SETTINGS_CHANGED:
2658 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2659 collect_connections(COLLECT_CONNECTIONS);
2660 return ERROR_SUCCESS;
2662 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2663 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2665 if(size != sizeof(ULONG))
2666 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2668 FIXME("%08x\n", *(ULONG*)buf);
2669 return ERROR_SUCCESS;
2672 return ERROR_INTERNET_INVALID_OPTION;
2675 /***********************************************************************
2676 * InternetSetOptionW (WININET.@)
2678 * Sets an options on the specified handle
2680 * RETURNS
2681 * TRUE on success
2682 * FALSE on failure
2685 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2686 LPVOID lpBuffer, DWORD dwBufferLength)
2688 object_header_t *lpwhh;
2689 BOOL ret = TRUE;
2690 DWORD res;
2692 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2694 lpwhh = (object_header_t*) get_handle_object( hInternet );
2695 if(lpwhh)
2696 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2697 else
2698 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2700 if(res != ERROR_INTERNET_INVALID_OPTION) {
2701 if(lpwhh)
2702 WININET_Release(lpwhh);
2704 if(res != ERROR_SUCCESS)
2705 SetLastError(res);
2707 return res == ERROR_SUCCESS;
2710 switch (dwOption)
2712 case INTERNET_OPTION_HTTP_VERSION:
2714 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2715 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2717 break;
2718 case INTERNET_OPTION_ERROR_MASK:
2720 if(!lpwhh) {
2721 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2722 return FALSE;
2723 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2724 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2725 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2726 SetLastError(ERROR_INVALID_PARAMETER);
2727 ret = FALSE;
2728 } else if(dwBufferLength != sizeof(ULONG)) {
2729 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2730 ret = FALSE;
2731 } else
2732 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2733 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2735 break;
2736 case INTERNET_OPTION_PROXY:
2738 INTERNET_PROXY_INFOW *info = lpBuffer;
2740 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2742 SetLastError(ERROR_INVALID_PARAMETER);
2743 return FALSE;
2745 if (!hInternet)
2747 EnterCriticalSection( &WININET_cs );
2748 free_global_proxy();
2749 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2750 if (global_proxy)
2752 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2754 global_proxy->proxyEnabled = 1;
2755 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2756 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2758 else
2760 global_proxy->proxyEnabled = 0;
2761 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2764 LeaveCriticalSection( &WININET_cs );
2766 else
2768 /* In general, each type of object should handle
2769 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2770 * get silently dropped.
2772 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2773 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2774 ret = FALSE;
2776 break;
2778 case INTERNET_OPTION_CODEPAGE:
2780 ULONG codepage = *(ULONG *)lpBuffer;
2781 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2783 break;
2784 case INTERNET_OPTION_REQUEST_PRIORITY:
2786 ULONG priority = *(ULONG *)lpBuffer;
2787 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2789 break;
2790 case INTERNET_OPTION_CONNECT_TIMEOUT:
2792 ULONG connecttimeout = *(ULONG *)lpBuffer;
2793 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2795 break;
2796 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2798 ULONG receivetimeout = *(ULONG *)lpBuffer;
2799 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2801 break;
2802 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2803 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2804 break;
2805 case INTERNET_OPTION_END_BROWSER_SESSION:
2806 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: semi-stub\n");
2807 free_cookie();
2808 break;
2809 case INTERNET_OPTION_CONNECTED_STATE:
2810 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2811 break;
2812 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2813 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2814 break;
2815 case INTERNET_OPTION_SEND_TIMEOUT:
2816 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2817 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2819 ULONG timeout = *(ULONG *)lpBuffer;
2820 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2821 break;
2823 case INTERNET_OPTION_CONNECT_RETRIES:
2825 ULONG retries = *(ULONG *)lpBuffer;
2826 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2827 break;
2829 case INTERNET_OPTION_CONTEXT_VALUE:
2831 if (!lpwhh)
2833 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2834 return FALSE;
2836 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2838 SetLastError(ERROR_INVALID_PARAMETER);
2839 ret = FALSE;
2841 else
2842 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2843 break;
2845 case INTERNET_OPTION_SECURITY_FLAGS:
2846 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2847 break;
2848 case INTERNET_OPTION_DISABLE_AUTODIAL:
2849 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2850 break;
2851 case INTERNET_OPTION_HTTP_DECODING:
2852 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2853 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2854 ret = FALSE;
2855 break;
2856 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2857 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2858 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2859 ret = FALSE;
2860 break;
2861 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2862 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2863 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2864 ret = FALSE;
2865 break;
2866 case INTERNET_OPTION_CODEPAGE_PATH:
2867 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2868 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2869 ret = FALSE;
2870 break;
2871 case INTERNET_OPTION_CODEPAGE_EXTRA:
2872 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2873 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2874 ret = FALSE;
2875 break;
2876 case INTERNET_OPTION_IDN:
2877 FIXME("INTERNET_OPTION_IDN; STUB\n");
2878 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2879 ret = FALSE;
2880 break;
2881 case INTERNET_OPTION_POLICY:
2882 SetLastError(ERROR_INVALID_PARAMETER);
2883 ret = FALSE;
2884 break;
2885 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2886 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2887 LONG res;
2888 unsigned int i;
2889 proxyinfo_t pi;
2891 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2893 for (i = 0; i < con->dwOptionCount; i++) {
2894 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2896 switch (option->dwOption) {
2897 case INTERNET_PER_CONN_PROXY_SERVER:
2898 heap_free(pi.proxy);
2899 pi.proxy = heap_strdupW(option->Value.pszValue);
2900 break;
2902 case INTERNET_PER_CONN_FLAGS:
2903 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2904 pi.proxyEnabled = 1;
2905 else
2907 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2908 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2909 pi.proxyEnabled = 0;
2911 break;
2913 case INTERNET_PER_CONN_PROXY_BYPASS:
2914 heap_free(pi.proxyBypass);
2915 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2916 break;
2918 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2919 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2920 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2921 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2922 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2923 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2924 FIXME("Unhandled dwOption %d\n", option->dwOption);
2925 break;
2927 default:
2928 FIXME("Unknown dwOption %d\n", option->dwOption);
2929 SetLastError(ERROR_INVALID_PARAMETER);
2930 break;
2934 if ((res = INTERNET_SaveProxySettings(&pi)))
2935 SetLastError(res);
2937 FreeProxyInfo(&pi);
2939 ret = (res == ERROR_SUCCESS);
2940 break;
2942 default:
2943 FIXME("Option %d STUB\n",dwOption);
2944 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2945 ret = FALSE;
2946 break;
2949 if(lpwhh)
2950 WININET_Release( lpwhh );
2952 return ret;
2956 /***********************************************************************
2957 * InternetSetOptionA (WININET.@)
2959 * Sets an options on the specified handle.
2961 * RETURNS
2962 * TRUE on success
2963 * FALSE on failure
2966 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2967 LPVOID lpBuffer, DWORD dwBufferLength)
2969 LPVOID wbuffer;
2970 DWORD wlen;
2971 BOOL r;
2973 switch( dwOption )
2975 case INTERNET_OPTION_PROXY:
2977 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2978 LPINTERNET_PROXY_INFOW piw;
2979 DWORD proxlen, prbylen;
2980 LPWSTR prox, prby;
2982 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2983 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2984 wlen = sizeof(*piw) + proxlen + prbylen;
2985 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2986 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2987 piw->dwAccessType = pi->dwAccessType;
2988 prox = (LPWSTR) &piw[1];
2989 prby = &prox[proxlen+1];
2990 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2991 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2992 piw->lpszProxy = prox;
2993 piw->lpszProxyBypass = prby;
2995 break;
2996 case INTERNET_OPTION_USER_AGENT:
2997 case INTERNET_OPTION_USERNAME:
2998 case INTERNET_OPTION_PASSWORD:
2999 case INTERNET_OPTION_PROXY_USERNAME:
3000 case INTERNET_OPTION_PROXY_PASSWORD:
3001 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3002 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3003 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3004 break;
3005 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3006 unsigned int i;
3007 INTERNET_PER_CONN_OPTION_LISTW *listW;
3008 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3009 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3010 wbuffer = heap_alloc(wlen);
3011 listW = wbuffer;
3013 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3014 if (listA->pszConnection)
3016 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3017 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3018 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3020 else
3021 listW->pszConnection = NULL;
3022 listW->dwOptionCount = listA->dwOptionCount;
3023 listW->dwOptionError = listA->dwOptionError;
3024 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3026 for (i = 0; i < listA->dwOptionCount; ++i) {
3027 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3028 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3030 optW->dwOption = optA->dwOption;
3032 switch (optA->dwOption) {
3033 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3034 case INTERNET_PER_CONN_PROXY_BYPASS:
3035 case INTERNET_PER_CONN_PROXY_SERVER:
3036 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3037 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3038 if (optA->Value.pszValue)
3040 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3041 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3042 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3044 else
3045 optW->Value.pszValue = NULL;
3046 break;
3047 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3048 case INTERNET_PER_CONN_FLAGS:
3049 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3050 optW->Value.dwValue = optA->Value.dwValue;
3051 break;
3052 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3053 optW->Value.ftValue = optA->Value.ftValue;
3054 break;
3055 default:
3056 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3057 optW->Value.dwValue = optA->Value.dwValue;
3058 break;
3062 break;
3063 default:
3064 wbuffer = lpBuffer;
3065 wlen = dwBufferLength;
3068 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3070 if( lpBuffer != wbuffer )
3072 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3074 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3075 unsigned int i;
3076 for (i = 0; i < list->dwOptionCount; ++i) {
3077 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3078 switch (opt->dwOption) {
3079 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3080 case INTERNET_PER_CONN_PROXY_BYPASS:
3081 case INTERNET_PER_CONN_PROXY_SERVER:
3082 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3083 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3084 heap_free( opt->Value.pszValue );
3085 break;
3086 default:
3087 break;
3090 heap_free( list->pOptions );
3092 heap_free( wbuffer );
3095 return r;
3099 /***********************************************************************
3100 * InternetSetOptionExA (WININET.@)
3102 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3103 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3105 FIXME("Flags %08x ignored\n", dwFlags);
3106 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3109 /***********************************************************************
3110 * InternetSetOptionExW (WININET.@)
3112 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3113 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3115 FIXME("Flags %08x ignored\n", dwFlags);
3116 if( dwFlags & ~ISO_VALID_FLAGS )
3118 SetLastError( ERROR_INVALID_PARAMETER );
3119 return FALSE;
3121 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3124 static const WCHAR WININET_wkday[7][4] =
3125 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3126 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3127 static const WCHAR WININET_month[12][4] =
3128 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3129 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3130 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3132 /***********************************************************************
3133 * InternetTimeFromSystemTimeA (WININET.@)
3135 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3137 BOOL ret;
3138 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3140 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3142 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3144 SetLastError(ERROR_INVALID_PARAMETER);
3145 return FALSE;
3148 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3150 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3151 return FALSE;
3154 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3155 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3157 return ret;
3160 /***********************************************************************
3161 * InternetTimeFromSystemTimeW (WININET.@)
3163 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3165 static const WCHAR date[] =
3166 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3167 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3169 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3171 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3173 SetLastError(ERROR_INVALID_PARAMETER);
3174 return FALSE;
3177 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3179 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3180 return FALSE;
3183 sprintfW( string, date,
3184 WININET_wkday[time->wDayOfWeek],
3185 time->wDay,
3186 WININET_month[time->wMonth - 1],
3187 time->wYear,
3188 time->wHour,
3189 time->wMinute,
3190 time->wSecond );
3192 return TRUE;
3195 /***********************************************************************
3196 * InternetTimeToSystemTimeA (WININET.@)
3198 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3200 BOOL ret = FALSE;
3201 WCHAR *stringW;
3203 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3205 stringW = heap_strdupAtoW(string);
3206 if (stringW)
3208 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3209 heap_free( stringW );
3211 return ret;
3214 /***********************************************************************
3215 * InternetTimeToSystemTimeW (WININET.@)
3217 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3219 unsigned int i;
3220 const WCHAR *s = string;
3221 WCHAR *end;
3223 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3225 if (!string || !time) return FALSE;
3227 /* Windows does this too */
3228 GetSystemTime( time );
3230 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3231 * a SYSTEMTIME structure.
3234 while (*s && !isalphaW( *s )) s++;
3235 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3236 time->wDayOfWeek = 7;
3238 for (i = 0; i < 7; i++)
3240 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3241 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3242 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3244 time->wDayOfWeek = i;
3245 break;
3249 if (time->wDayOfWeek > 6) return TRUE;
3250 while (*s && !isdigitW( *s )) s++;
3251 time->wDay = strtolW( s, &end, 10 );
3252 s = end;
3254 while (*s && !isalphaW( *s )) s++;
3255 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3256 time->wMonth = 0;
3258 for (i = 0; i < 12; i++)
3260 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3261 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3262 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3264 time->wMonth = i + 1;
3265 break;
3268 if (time->wMonth == 0) return TRUE;
3270 while (*s && !isdigitW( *s )) s++;
3271 if (*s == '\0') return TRUE;
3272 time->wYear = strtolW( s, &end, 10 );
3273 s = end;
3275 while (*s && !isdigitW( *s )) s++;
3276 if (*s == '\0') return TRUE;
3277 time->wHour = strtolW( s, &end, 10 );
3278 s = end;
3280 while (*s && !isdigitW( *s )) s++;
3281 if (*s == '\0') return TRUE;
3282 time->wMinute = strtolW( s, &end, 10 );
3283 s = end;
3285 while (*s && !isdigitW( *s )) s++;
3286 if (*s == '\0') return TRUE;
3287 time->wSecond = strtolW( s, &end, 10 );
3288 s = end;
3290 time->wMilliseconds = 0;
3291 return TRUE;
3294 /***********************************************************************
3295 * InternetCheckConnectionW (WININET.@)
3297 * Pings a requested host to check internet connection
3299 * RETURNS
3300 * TRUE on success and FALSE on failure. If a failure then
3301 * ERROR_NOT_CONNECTED is placed into GetLastError
3304 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3307 * this is a kludge which runs the resident ping program and reads the output.
3309 * Anyone have a better idea?
3312 BOOL rc = FALSE;
3313 static const CHAR ping[] = "ping -c 1 ";
3314 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3315 WCHAR *host;
3316 DWORD len, host_len;
3317 INTERNET_PORT port;
3318 int status = -1;
3320 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3323 * Crack or set the Address
3325 if (lpszUrl == NULL)
3328 * According to the doc we are supposed to use the ip for the next
3329 * server in the WnInet internal server database. I have
3330 * no idea what that is or how to get it.
3332 * So someone needs to implement this.
3334 FIXME("Unimplemented with URL of NULL\n");
3335 return TRUE;
3337 else
3339 URL_COMPONENTSW components = {sizeof(components)};
3341 components.dwHostNameLength = 1;
3343 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3344 goto End;
3346 host = components.lpszHostName;
3347 host_len = components.dwHostNameLength;
3348 port = components.nPort;
3349 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3352 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3354 struct sockaddr_storage saddr;
3355 int sa_len = sizeof(saddr);
3356 WCHAR *host_z;
3357 int fd;
3358 BOOL b;
3360 host_z = heap_strndupW(host, host_len);
3361 if (!host_z)
3362 return FALSE;
3364 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3365 heap_free(host_z);
3366 if(!b)
3367 goto End;
3368 init_winsock();
3369 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3370 if (fd != -1)
3372 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3373 rc = TRUE;
3374 closesocket(fd);
3377 else
3380 * Build our ping command
3382 char *command;
3384 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3385 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3386 strcpy(command, ping);
3387 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3388 strcpy(command+sizeof(ping)-1+len, redirect);
3390 TRACE("Ping command is : %s\n",command);
3392 status = system(command);
3393 heap_free( command );
3395 TRACE("Ping returned a code of %i\n",status);
3397 /* Ping return code of 0 indicates success */
3398 if (status == 0)
3399 rc = TRUE;
3402 End:
3403 if (rc == FALSE)
3404 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3406 return rc;
3410 /***********************************************************************
3411 * InternetCheckConnectionA (WININET.@)
3413 * Pings a requested host to check internet connection
3415 * RETURNS
3416 * TRUE on success and FALSE on failure. If a failure then
3417 * ERROR_NOT_CONNECTED is placed into GetLastError
3420 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3422 WCHAR *url = NULL;
3423 BOOL rc;
3425 if(lpszUrl) {
3426 url = heap_strdupAtoW(lpszUrl);
3427 if(!url)
3428 return FALSE;
3431 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3433 heap_free(url);
3434 return rc;
3438 /**********************************************************
3439 * INTERNET_InternetOpenUrlW (internal)
3441 * Opens an URL
3443 * RETURNS
3444 * handle of connection or NULL on failure
3446 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3447 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3449 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3450 WCHAR *host, *user = NULL, *pass = NULL, *path;
3451 HINTERNET client = NULL, client1 = NULL;
3452 DWORD res;
3454 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3455 dwHeadersLength, dwFlags, dwContext);
3457 urlComponents.dwHostNameLength = 1;
3458 urlComponents.dwUserNameLength = 1;
3459 urlComponents.dwPasswordLength = 1;
3460 urlComponents.dwUrlPathLength = 1;
3461 urlComponents.dwExtraInfoLength = 1;
3462 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3463 return NULL;
3465 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP && urlComponents.dwExtraInfoLength) {
3466 assert(urlComponents.lpszUrlPath + urlComponents.dwUrlPathLength == urlComponents.lpszExtraInfo);
3467 urlComponents.dwUrlPathLength += urlComponents.dwExtraInfoLength;
3470 host = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
3471 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
3472 if(urlComponents.dwUserNameLength)
3473 user = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
3474 if(urlComponents.dwPasswordLength)
3475 pass = heap_strndupW(urlComponents.lpszPassword, urlComponents.dwPasswordLength);
3477 switch(urlComponents.nScheme) {
3478 case INTERNET_SCHEME_FTP:
3479 client = FTP_Connect(hIC, host, urlComponents.nPort,
3480 user, pass, dwFlags, dwContext, INET_OPENURL);
3481 if(client == NULL)
3482 break;
3483 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3484 if(client1 == NULL) {
3485 InternetCloseHandle(client);
3486 break;
3488 break;
3490 case INTERNET_SCHEME_HTTP:
3491 case INTERNET_SCHEME_HTTPS: {
3492 static const WCHAR szStars[] = { '*','/','*', 0 };
3493 LPCWSTR accept[2] = { szStars, NULL };
3495 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3497 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3498 res = HTTP_Connect(hIC, host, urlComponents.nPort,
3499 user, pass, dwFlags, dwContext, INET_OPENURL, &client);
3500 if(res != ERROR_SUCCESS) {
3501 INTERNET_SetLastError(res);
3502 break;
3505 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3506 if(client1 == NULL) {
3507 InternetCloseHandle(client);
3508 break;
3510 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3511 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3512 GetLastError() != ERROR_IO_PENDING) {
3513 InternetCloseHandle(client1);
3514 client1 = NULL;
3515 break;
3518 case INTERNET_SCHEME_GOPHER:
3519 /* gopher doesn't seem to be implemented in wine, but it's supposed
3520 * to be supported by InternetOpenUrlA. */
3521 default:
3522 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3523 break;
3526 TRACE(" %p <--\n", client1);
3528 heap_free(host);
3529 heap_free(path);
3530 heap_free(user);
3531 heap_free(pass);
3532 return client1;
3535 /**********************************************************
3536 * InternetOpenUrlW (WININET.@)
3538 * Opens an URL
3540 * RETURNS
3541 * handle of connection or NULL on failure
3543 typedef struct {
3544 task_header_t hdr;
3545 WCHAR *url;
3546 WCHAR *headers;
3547 DWORD headers_len;
3548 DWORD flags;
3549 DWORD_PTR context;
3550 } open_url_task_t;
3552 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3554 open_url_task_t *task = (open_url_task_t*)hdr;
3556 TRACE("%p\n", task->hdr.hdr);
3558 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3559 task->headers_len, task->flags, task->context);
3560 heap_free(task->url);
3561 heap_free(task->headers);
3564 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3565 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3567 HINTERNET ret = NULL;
3568 appinfo_t *hIC = NULL;
3570 if (TRACE_ON(wininet)) {
3571 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3572 dwHeadersLength, dwFlags, dwContext);
3573 TRACE(" flags :");
3574 dump_INTERNET_FLAGS(dwFlags);
3577 if (!lpszUrl)
3579 SetLastError(ERROR_INVALID_PARAMETER);
3580 goto lend;
3583 hIC = (appinfo_t*)get_handle_object( hInternet );
3584 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3585 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3586 goto lend;
3589 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3590 open_url_task_t *task;
3592 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3593 task->url = heap_strdupW(lpszUrl);
3594 task->headers = heap_strdupW(lpszHeaders);
3595 task->headers_len = dwHeadersLength;
3596 task->flags = dwFlags;
3597 task->context = dwContext;
3599 INTERNET_AsyncCall(&task->hdr);
3600 SetLastError(ERROR_IO_PENDING);
3601 } else {
3602 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3605 lend:
3606 if( hIC )
3607 WININET_Release( &hIC->hdr );
3608 TRACE(" %p <--\n", ret);
3610 return ret;
3613 /**********************************************************
3614 * InternetOpenUrlA (WININET.@)
3616 * Opens an URL
3618 * RETURNS
3619 * handle of connection or NULL on failure
3621 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3622 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3624 HINTERNET rc = NULL;
3625 LPWSTR szUrl = NULL;
3626 WCHAR *headers = NULL;
3628 TRACE("\n");
3630 if(lpszUrl) {
3631 szUrl = heap_strdupAtoW(lpszUrl);
3632 if(!szUrl)
3633 return NULL;
3636 if(lpszHeaders) {
3637 headers = heap_strndupAtoW(lpszHeaders, dwHeadersLength, &dwHeadersLength);
3638 if(!headers) {
3639 heap_free(szUrl);
3640 return NULL;
3644 rc = InternetOpenUrlW(hInternet, szUrl, headers, dwHeadersLength, dwFlags, dwContext);
3646 heap_free(szUrl);
3647 heap_free(headers);
3648 return rc;
3652 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3654 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3656 if (lpwite)
3658 lpwite->dwError = 0;
3659 lpwite->response[0] = '\0';
3662 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3664 heap_free(lpwite);
3665 return NULL;
3667 return lpwite;
3671 /***********************************************************************
3672 * INTERNET_SetLastError (internal)
3674 * Set last thread specific error
3676 * RETURNS
3679 void INTERNET_SetLastError(DWORD dwError)
3681 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3683 if (!lpwite)
3684 lpwite = INTERNET_AllocThreadError();
3686 SetLastError(dwError);
3687 if(lpwite)
3688 lpwite->dwError = dwError;
3692 /***********************************************************************
3693 * INTERNET_GetLastError (internal)
3695 * Get last thread specific error
3697 * RETURNS
3700 DWORD INTERNET_GetLastError(void)
3702 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3703 if (!lpwite) return 0;
3704 /* TlsGetValue clears last error, so set it again here */
3705 SetLastError(lpwite->dwError);
3706 return lpwite->dwError;
3710 /***********************************************************************
3711 * INTERNET_WorkerThreadFunc (internal)
3713 * Worker thread execution function
3715 * RETURNS
3718 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3720 task_header_t *task = lpvParam;
3722 TRACE("\n");
3724 task->proc(task);
3725 WININET_Release(task->hdr);
3726 heap_free(task);
3728 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3730 heap_free(TlsGetValue(g_dwTlsErrIndex));
3731 TlsSetValue(g_dwTlsErrIndex, NULL);
3733 return TRUE;
3736 void *alloc_async_task(object_header_t *hdr, async_task_proc_t proc, size_t size)
3738 task_header_t *task;
3740 task = heap_alloc(size);
3741 if(!task)
3742 return NULL;
3744 task->hdr = WININET_AddRef(hdr);
3745 task->proc = proc;
3746 return task;
3749 /***********************************************************************
3750 * INTERNET_AsyncCall (internal)
3752 * Retrieves work request from queue
3754 * RETURNS
3757 DWORD INTERNET_AsyncCall(task_header_t *task)
3759 BOOL bSuccess;
3761 TRACE("\n");
3763 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, task, WT_EXECUTELONGFUNCTION);
3764 if (!bSuccess)
3766 heap_free(task);
3767 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3769 return ERROR_SUCCESS;
3773 /***********************************************************************
3774 * INTERNET_GetResponseBuffer (internal)
3776 * RETURNS
3779 LPSTR INTERNET_GetResponseBuffer(void)
3781 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3782 if (!lpwite)
3783 lpwite = INTERNET_AllocThreadError();
3784 TRACE("\n");
3785 return lpwite->response;
3788 /**********************************************************
3789 * InternetQueryDataAvailable (WININET.@)
3791 * Determines how much data is available to be read.
3793 * RETURNS
3794 * TRUE on success, FALSE if an error occurred. If
3795 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3796 * no data is presently available, FALSE is returned with
3797 * the last error ERROR_IO_PENDING; a callback with status
3798 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3799 * data is available.
3801 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3802 LPDWORD lpdwNumberOfBytesAvailable,
3803 DWORD dwFlags, DWORD_PTR dwContext)
3805 object_header_t *hdr;
3806 DWORD res;
3808 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3810 hdr = get_handle_object( hFile );
3811 if (!hdr) {
3812 SetLastError(ERROR_INVALID_HANDLE);
3813 return FALSE;
3816 if(hdr->vtbl->QueryDataAvailable) {
3817 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3818 }else {
3819 WARN("wrong handle\n");
3820 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3823 WININET_Release(hdr);
3825 if(res != ERROR_SUCCESS)
3826 SetLastError(res);
3827 return res == ERROR_SUCCESS;
3830 DWORD create_req_file(const WCHAR *file_name, req_file_t **ret)
3832 req_file_t *req_file;
3834 req_file = heap_alloc_zero(sizeof(*req_file));
3835 if(!req_file)
3836 return ERROR_NOT_ENOUGH_MEMORY;
3838 req_file->ref = 1;
3840 req_file->file_name = heap_strdupW(file_name);
3841 if(!req_file->file_name) {
3842 heap_free(req_file);
3843 return ERROR_NOT_ENOUGH_MEMORY;
3846 req_file->file_handle = CreateFileW(req_file->file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
3847 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3848 if(req_file->file_handle == INVALID_HANDLE_VALUE) {
3849 req_file_release(req_file);
3850 return GetLastError();
3853 *ret = req_file;
3854 return ERROR_SUCCESS;
3857 void req_file_release(req_file_t *req_file)
3859 if(InterlockedDecrement(&req_file->ref))
3860 return;
3862 if(!req_file->is_committed)
3863 DeleteFileW(req_file->file_name);
3864 if(req_file->file_handle && req_file->file_handle != INVALID_HANDLE_VALUE)
3865 CloseHandle(req_file->file_handle);
3866 heap_free(req_file->file_name);
3867 heap_free(req_file->url);
3868 heap_free(req_file);
3871 /***********************************************************************
3872 * InternetLockRequestFile (WININET.@)
3874 BOOL WINAPI InternetLockRequestFile(HINTERNET hInternet, HANDLE *lphLockReqHandle)
3876 req_file_t *req_file = NULL;
3877 object_header_t *hdr;
3878 DWORD res;
3880 TRACE("(%p %p)\n", hInternet, lphLockReqHandle);
3882 hdr = get_handle_object(hInternet);
3883 if (!hdr) {
3884 SetLastError(ERROR_INVALID_HANDLE);
3885 return FALSE;
3888 if(hdr->vtbl->LockRequestFile) {
3889 res = hdr->vtbl->LockRequestFile(hdr, &req_file);
3890 }else {
3891 WARN("wrong handle\n");
3892 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3895 WININET_Release(hdr);
3897 *lphLockReqHandle = req_file;
3898 if(res != ERROR_SUCCESS)
3899 SetLastError(res);
3900 return res == ERROR_SUCCESS;
3903 BOOL WINAPI InternetUnlockRequestFile(HANDLE hLockHandle)
3905 TRACE("(%p)\n", hLockHandle);
3907 req_file_release(hLockHandle);
3908 return TRUE;
3912 /***********************************************************************
3913 * InternetAutodial (WININET.@)
3915 * On windows this function is supposed to dial the default internet
3916 * connection. We don't want to have Wine dial out to the internet so
3917 * we return TRUE by default. It might be nice to check if we are connected.
3919 * RETURNS
3920 * TRUE on success
3921 * FALSE on failure
3924 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3926 FIXME("STUB\n");
3928 /* Tell that we are connected to the internet. */
3929 return TRUE;
3932 /***********************************************************************
3933 * InternetAutodialHangup (WININET.@)
3935 * Hangs up a connection made with InternetAutodial
3937 * PARAM
3938 * dwReserved
3939 * RETURNS
3940 * TRUE on success
3941 * FALSE on failure
3944 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3946 FIXME("STUB\n");
3948 /* we didn't dial, we don't disconnect */
3949 return TRUE;
3952 /***********************************************************************
3953 * InternetCombineUrlA (WININET.@)
3955 * Combine a base URL with a relative URL
3957 * RETURNS
3958 * TRUE on success
3959 * FALSE on failure
3963 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3964 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3965 DWORD dwFlags)
3967 HRESULT hr=S_OK;
3969 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3971 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3972 dwFlags ^= ICU_NO_ENCODE;
3973 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3975 return (hr==S_OK);
3978 /***********************************************************************
3979 * InternetCombineUrlW (WININET.@)
3981 * Combine a base URL with a relative URL
3983 * RETURNS
3984 * TRUE on success
3985 * FALSE on failure
3989 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3990 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3991 DWORD dwFlags)
3993 HRESULT hr=S_OK;
3995 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3997 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3998 dwFlags ^= ICU_NO_ENCODE;
3999 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4001 return (hr==S_OK);
4004 /* max port num is 65535 => 5 digits */
4005 #define MAX_WORD_DIGITS 5
4007 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
4008 (url)->dw##component##Length : strlenW((url)->lpsz##component))
4009 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
4010 (url)->dw##component##Length : strlen((url)->lpsz##component))
4012 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
4014 if ((nScheme == INTERNET_SCHEME_HTTP) &&
4015 (nPort == INTERNET_DEFAULT_HTTP_PORT))
4016 return TRUE;
4017 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
4018 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
4019 return TRUE;
4020 if ((nScheme == INTERNET_SCHEME_FTP) &&
4021 (nPort == INTERNET_DEFAULT_FTP_PORT))
4022 return TRUE;
4023 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
4024 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
4025 return TRUE;
4027 if (nPort == INTERNET_INVALID_PORT_NUMBER)
4028 return TRUE;
4030 return FALSE;
4033 /* opaque urls do not fit into the standard url hierarchy and don't have
4034 * two following slashes */
4035 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
4037 return (nScheme != INTERNET_SCHEME_FTP) &&
4038 (nScheme != INTERNET_SCHEME_GOPHER) &&
4039 (nScheme != INTERNET_SCHEME_HTTP) &&
4040 (nScheme != INTERNET_SCHEME_HTTPS) &&
4041 (nScheme != INTERNET_SCHEME_FILE);
4044 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
4046 int index;
4047 if (scheme < INTERNET_SCHEME_FIRST)
4048 return NULL;
4049 index = scheme - INTERNET_SCHEME_FIRST;
4050 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
4051 return NULL;
4052 return (LPCWSTR)url_schemes[index];
4055 /* we can calculate using ansi strings because we're just
4056 * calculating string length, not size
4058 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
4059 LPDWORD lpdwUrlLength)
4061 INTERNET_SCHEME nScheme;
4063 *lpdwUrlLength = 0;
4065 if (lpUrlComponents->lpszScheme)
4067 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4068 *lpdwUrlLength += dwLen;
4069 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4071 else
4073 LPCWSTR scheme;
4075 nScheme = lpUrlComponents->nScheme;
4077 if (nScheme == INTERNET_SCHEME_DEFAULT)
4078 nScheme = INTERNET_SCHEME_HTTP;
4079 scheme = INTERNET_GetSchemeString(nScheme);
4080 *lpdwUrlLength += strlenW(scheme);
4083 (*lpdwUrlLength)++; /* ':' */
4084 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4085 *lpdwUrlLength += strlen("//");
4087 if (lpUrlComponents->lpszUserName)
4089 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4090 *lpdwUrlLength += strlen("@");
4092 else
4094 if (lpUrlComponents->lpszPassword)
4096 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4097 return FALSE;
4101 if (lpUrlComponents->lpszPassword)
4103 *lpdwUrlLength += strlen(":");
4104 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4107 if (lpUrlComponents->lpszHostName)
4109 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4111 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4113 char szPort[MAX_WORD_DIGITS+1];
4115 *lpdwUrlLength += sprintf(szPort, "%d", lpUrlComponents->nPort);
4116 *lpdwUrlLength += strlen(":");
4119 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4120 (*lpdwUrlLength)++; /* '/' */
4123 if (lpUrlComponents->lpszUrlPath)
4124 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4126 if (lpUrlComponents->lpszExtraInfo)
4127 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4129 return TRUE;
4132 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4134 INT len;
4136 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4138 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4139 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4140 urlCompW->nScheme = lpUrlComponents->nScheme;
4141 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4142 urlCompW->nPort = lpUrlComponents->nPort;
4143 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4144 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4145 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4146 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4148 if (lpUrlComponents->lpszScheme)
4150 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4151 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4152 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4153 -1, urlCompW->lpszScheme, len);
4156 if (lpUrlComponents->lpszHostName)
4158 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4159 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4160 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4161 -1, urlCompW->lpszHostName, len);
4164 if (lpUrlComponents->lpszUserName)
4166 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4167 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4168 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4169 -1, urlCompW->lpszUserName, len);
4172 if (lpUrlComponents->lpszPassword)
4174 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4175 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4176 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4177 -1, urlCompW->lpszPassword, len);
4180 if (lpUrlComponents->lpszUrlPath)
4182 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4183 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4184 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4185 -1, urlCompW->lpszUrlPath, len);
4188 if (lpUrlComponents->lpszExtraInfo)
4190 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4191 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4192 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4193 -1, urlCompW->lpszExtraInfo, len);
4197 /***********************************************************************
4198 * InternetCreateUrlA (WININET.@)
4200 * See InternetCreateUrlW.
4202 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4203 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4205 BOOL ret;
4206 LPWSTR urlW = NULL;
4207 URL_COMPONENTSW urlCompW;
4209 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4211 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4213 SetLastError(ERROR_INVALID_PARAMETER);
4214 return FALSE;
4217 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4219 if (lpszUrl)
4220 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4222 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4224 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4225 *lpdwUrlLength /= sizeof(WCHAR);
4227 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4228 * minus one, so add one to leave room for NULL terminator
4230 if (ret)
4231 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4233 heap_free(urlCompW.lpszScheme);
4234 heap_free(urlCompW.lpszHostName);
4235 heap_free(urlCompW.lpszUserName);
4236 heap_free(urlCompW.lpszPassword);
4237 heap_free(urlCompW.lpszUrlPath);
4238 heap_free(urlCompW.lpszExtraInfo);
4239 heap_free(urlW);
4240 return ret;
4243 /***********************************************************************
4244 * InternetCreateUrlW (WININET.@)
4246 * Creates a URL from its component parts.
4248 * PARAMS
4249 * lpUrlComponents [I] URL Components.
4250 * dwFlags [I] Flags. See notes.
4251 * lpszUrl [I] Buffer in which to store the created URL.
4252 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4253 * lpszUrl in characters. On output, the number of bytes
4254 * required to store the URL including terminator.
4256 * NOTES
4258 * The dwFlags parameter can be zero or more of the following:
4259 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4261 * RETURNS
4262 * TRUE on success
4263 * FALSE on failure
4266 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4267 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4269 DWORD dwLen;
4270 INTERNET_SCHEME nScheme;
4272 static const WCHAR slashSlashW[] = {'/','/'};
4273 static const WCHAR fmtW[] = {'%','u',0};
4275 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4277 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4279 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4280 return FALSE;
4283 if (!calc_url_length(lpUrlComponents, &dwLen))
4284 return FALSE;
4286 if (!lpszUrl || *lpdwUrlLength < dwLen)
4288 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4289 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4290 return FALSE;
4293 *lpdwUrlLength = dwLen;
4294 lpszUrl[0] = 0x00;
4296 dwLen = 0;
4298 if (lpUrlComponents->lpszScheme)
4300 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4301 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4302 lpszUrl += dwLen;
4304 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4306 else
4308 LPCWSTR scheme;
4309 nScheme = lpUrlComponents->nScheme;
4311 if (nScheme == INTERNET_SCHEME_DEFAULT)
4312 nScheme = INTERNET_SCHEME_HTTP;
4314 scheme = INTERNET_GetSchemeString(nScheme);
4315 dwLen = strlenW(scheme);
4316 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4317 lpszUrl += dwLen;
4320 /* all schemes are followed by at least a colon */
4321 *lpszUrl = ':';
4322 lpszUrl++;
4324 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4326 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4327 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4330 if (lpUrlComponents->lpszUserName)
4332 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4333 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4334 lpszUrl += dwLen;
4336 if (lpUrlComponents->lpszPassword)
4338 *lpszUrl = ':';
4339 lpszUrl++;
4341 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4342 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4343 lpszUrl += dwLen;
4346 *lpszUrl = '@';
4347 lpszUrl++;
4350 if (lpUrlComponents->lpszHostName)
4352 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4353 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4354 lpszUrl += dwLen;
4356 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4358 *lpszUrl = ':';
4359 lpszUrl++;
4360 lpszUrl += sprintfW(lpszUrl, fmtW, lpUrlComponents->nPort);
4363 /* add slash between hostname and path if necessary */
4364 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4366 *lpszUrl = '/';
4367 lpszUrl++;
4371 if (lpUrlComponents->lpszUrlPath)
4373 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4374 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4375 lpszUrl += dwLen;
4378 if (lpUrlComponents->lpszExtraInfo)
4380 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4381 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4382 lpszUrl += dwLen;
4385 *lpszUrl = '\0';
4387 return TRUE;
4390 /***********************************************************************
4391 * InternetConfirmZoneCrossingA (WININET.@)
4394 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4396 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4397 return ERROR_SUCCESS;
4400 /***********************************************************************
4401 * InternetConfirmZoneCrossingW (WININET.@)
4404 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4406 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4407 return ERROR_SUCCESS;
4410 static DWORD zone_preference = 3;
4412 /***********************************************************************
4413 * PrivacySetZonePreferenceW (WININET.@)
4415 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4417 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4419 zone_preference = template;
4420 return 0;
4423 /***********************************************************************
4424 * PrivacyGetZonePreferenceW (WININET.@)
4426 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4427 LPWSTR preference, LPDWORD length )
4429 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4431 if (template) *template = zone_preference;
4432 return 0;
4435 /***********************************************************************
4436 * InternetGetSecurityInfoByURLA (WININET.@)
4438 BOOL WINAPI InternetGetSecurityInfoByURLA(LPSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4440 WCHAR *url;
4441 BOOL res;
4443 TRACE("(%s %p %p)\n", debugstr_a(lpszURL), ppCertChain, pdwSecureFlags);
4445 url = heap_strdupAtoW(lpszURL);
4446 if(!url)
4447 return FALSE;
4449 res = InternetGetSecurityInfoByURLW(url, ppCertChain, pdwSecureFlags);
4450 heap_free(url);
4451 return res;
4454 /***********************************************************************
4455 * InternetGetSecurityInfoByURLW (WININET.@)
4457 BOOL WINAPI InternetGetSecurityInfoByURLW(LPCWSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4459 URL_COMPONENTSW url = {sizeof(url)};
4460 server_t *server;
4461 BOOL res;
4463 TRACE("(%s %p %p)\n", debugstr_w(lpszURL), ppCertChain, pdwSecureFlags);
4465 url.dwHostNameLength = 1;
4466 res = InternetCrackUrlW(lpszURL, 0, 0, &url);
4467 if(!res || url.nScheme != INTERNET_SCHEME_HTTPS) {
4468 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4469 return FALSE;
4472 server = get_server(substr(url.lpszHostName, url.dwHostNameLength), url.nPort, TRUE, FALSE);
4473 if(!server) {
4474 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4475 return FALSE;
4478 if(server->cert_chain) {
4479 const CERT_CHAIN_CONTEXT *chain_dup;
4481 chain_dup = CertDuplicateCertificateChain(server->cert_chain);
4482 if(chain_dup) {
4483 *ppCertChain = chain_dup;
4484 *pdwSecureFlags = server->security_flags & _SECURITY_ERROR_FLAGS_MASK;
4485 }else {
4486 res = FALSE;
4488 }else {
4489 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4490 res = FALSE;
4493 server_release(server);
4494 return res;
4497 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4498 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4500 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4501 lpdwConnection, dwReserved);
4502 return ERROR_SUCCESS;
4505 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4506 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4508 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4509 lpdwConnection, dwReserved);
4510 return ERROR_SUCCESS;
4513 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4515 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4516 return TRUE;
4519 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4521 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4522 return TRUE;
4525 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4527 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4528 return ERROR_SUCCESS;
4531 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4532 PBYTE pbHexHash )
4534 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4535 debugstr_w(pwszTarget), pbHexHash);
4536 return FALSE;
4539 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4541 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4542 return FALSE;
4545 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4547 FIXME("(%p, %08lx) stub\n", a, b);
4548 return FALSE;
4551 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4553 FIXME("%p: stub\n", parent);
4554 return 0;