d2d1/tests: Fix stroke style object leak (Valgrind).
[wine.git] / dlls / wininet / internet.c
blob1f1bbbd6d4cdcc403f516a1d636620653f90086a
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
989 /***********************************************************************
990 * InternetOpenW (WININET.@)
992 * Per-application initialization of wininet
994 * RETURNS
995 * HINTERNET on success
996 * NULL on failure
999 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
1000 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
1002 appinfo_t *lpwai = NULL;
1004 if (TRACE_ON(wininet)) {
1005 #define FE(x) { x, #x }
1006 static const wininet_flag_info access_type[] = {
1007 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1008 FE(INTERNET_OPEN_TYPE_DIRECT),
1009 FE(INTERNET_OPEN_TYPE_PROXY),
1010 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1012 #undef FE
1013 DWORD i;
1014 const char *access_type_str = "Unknown";
1016 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1017 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1018 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1019 if (access_type[i].val == dwAccessType) {
1020 access_type_str = access_type[i].name;
1021 break;
1024 TRACE(" access type : %s\n", access_type_str);
1025 TRACE(" flags :");
1026 dump_INTERNET_FLAGS(dwFlags);
1029 /* Clear any error information */
1030 INTERNET_SetLastError(0);
1032 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1033 SetLastError(ERROR_INVALID_PARAMETER);
1034 return NULL;
1037 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1038 if (!lpwai) {
1039 SetLastError(ERROR_OUTOFMEMORY);
1040 return NULL;
1043 lpwai->hdr.htype = WH_HINIT;
1044 lpwai->hdr.dwFlags = dwFlags;
1045 lpwai->accessType = dwAccessType;
1046 lpwai->proxyUsername = NULL;
1047 lpwai->proxyPassword = NULL;
1048 lpwai->connect_timeout = connect_timeout;
1050 lpwai->agent = heap_strdupW(lpszAgent);
1051 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1052 INTERNET_ConfigureProxy( lpwai );
1053 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1054 lpwai->proxy = heap_strdupW(lpszProxy);
1055 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1058 TRACE("returning %p\n", lpwai);
1060 return lpwai->hdr.hInternet;
1064 /***********************************************************************
1065 * InternetOpenA (WININET.@)
1067 * Per-application initialization of wininet
1069 * RETURNS
1070 * HINTERNET on success
1071 * NULL on failure
1074 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1075 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1077 WCHAR *szAgent, *szProxy, *szBypass;
1078 HINTERNET rc;
1080 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1081 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1083 szAgent = heap_strdupAtoW(lpszAgent);
1084 szProxy = heap_strdupAtoW(lpszProxy);
1085 szBypass = heap_strdupAtoW(lpszProxyBypass);
1087 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1089 heap_free(szAgent);
1090 heap_free(szProxy);
1091 heap_free(szBypass);
1092 return rc;
1095 /***********************************************************************
1096 * InternetGetLastResponseInfoA (WININET.@)
1098 * Return last wininet error description on the calling thread
1100 * RETURNS
1101 * TRUE on success of writing to buffer
1102 * FALSE on failure
1105 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1106 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1108 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1110 TRACE("\n");
1112 if (lpwite)
1114 *lpdwError = lpwite->dwError;
1115 if (lpwite->dwError)
1117 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1118 *lpdwBufferLength = strlen(lpszBuffer);
1120 else
1121 *lpdwBufferLength = 0;
1123 else
1125 *lpdwError = 0;
1126 *lpdwBufferLength = 0;
1129 return TRUE;
1132 /***********************************************************************
1133 * InternetGetLastResponseInfoW (WININET.@)
1135 * Return last wininet error description on the calling thread
1137 * RETURNS
1138 * TRUE on success of writing to buffer
1139 * FALSE on failure
1142 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1143 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1145 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1147 TRACE("\n");
1149 if (lpwite)
1151 *lpdwError = lpwite->dwError;
1152 if (lpwite->dwError)
1154 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1155 *lpdwBufferLength = lstrlenW(lpszBuffer);
1157 else
1158 *lpdwBufferLength = 0;
1160 else
1162 *lpdwError = 0;
1163 *lpdwBufferLength = 0;
1166 return TRUE;
1169 /***********************************************************************
1170 * InternetGetConnectedState (WININET.@)
1172 * Return connected state
1174 * RETURNS
1175 * TRUE if connected
1176 * if lpdwStatus is not null, return the status (off line,
1177 * modem, lan...) in it.
1178 * FALSE if not connected
1180 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1182 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1184 return InternetGetConnectedStateExW(lpdwStatus, NULL, 0, dwReserved);
1188 /***********************************************************************
1189 * InternetGetConnectedStateExW (WININET.@)
1191 * Return connected state
1193 * PARAMS
1195 * lpdwStatus [O] Flags specifying the status of the internet connection.
1196 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1197 * dwNameLen [I] Size of the buffer, in characters.
1198 * dwReserved [I] Reserved. Must be set to 0.
1200 * RETURNS
1201 * TRUE if connected
1202 * if lpdwStatus is not null, return the status (off line,
1203 * modem, lan...) in it.
1204 * FALSE if not connected
1206 * NOTES
1207 * If the system has no available network connections, an empty string is
1208 * stored in lpszConnectionName. If there is a LAN connection, a localized
1209 * "LAN Connection" string is stored. Presumably, if only a dial-up
1210 * connection is available then the name of the dial-up connection is
1211 * returned. Why any application, other than the "Internet Settings" CPL,
1212 * would want to use this function instead of the simpler InternetGetConnectedStateW
1213 * function is beyond me.
1215 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1216 DWORD dwNameLen, DWORD dwReserved)
1218 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1220 /* Must be zero */
1221 if(dwReserved)
1222 return FALSE;
1224 if (lpdwStatus) {
1225 WARN("always returning LAN connection.\n");
1226 *lpdwStatus = INTERNET_CONNECTION_LAN;
1229 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1230 * the resource, avoid it as we must not change the buffer in this case */
1231 if(lpszConnectionName && dwNameLen) {
1232 *lpszConnectionName = '\0';
1233 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1236 return TRUE;
1240 /***********************************************************************
1241 * InternetGetConnectedStateExA (WININET.@)
1243 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1244 DWORD dwNameLen, DWORD dwReserved)
1246 LPWSTR lpwszConnectionName = NULL;
1247 BOOL rc;
1249 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1251 if (lpszConnectionName && dwNameLen > 0)
1252 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1254 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1255 dwReserved);
1256 if (rc && lpwszConnectionName)
1257 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1258 dwNameLen, NULL, NULL);
1260 heap_free(lpwszConnectionName);
1261 return rc;
1265 /***********************************************************************
1266 * InternetConnectW (WININET.@)
1268 * Open a ftp, gopher or http session
1270 * RETURNS
1271 * HINTERNET a session handle on success
1272 * NULL on failure
1275 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1276 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1277 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1278 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1280 appinfo_t *hIC;
1281 HINTERNET rc = NULL;
1282 DWORD res = ERROR_SUCCESS;
1284 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1285 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1287 if (!lpszServerName)
1289 SetLastError(ERROR_INVALID_PARAMETER);
1290 return NULL;
1293 hIC = (appinfo_t*)get_handle_object( hInternet );
1294 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1296 res = ERROR_INVALID_HANDLE;
1297 goto lend;
1300 switch (dwService)
1302 case INTERNET_SERVICE_FTP:
1303 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1304 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1305 if(!rc)
1306 res = INTERNET_GetLastError();
1307 break;
1309 case INTERNET_SERVICE_HTTP:
1310 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1311 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1312 break;
1314 case INTERNET_SERVICE_GOPHER:
1315 default:
1316 break;
1318 lend:
1319 if( hIC )
1320 WININET_Release( &hIC->hdr );
1322 TRACE("returning %p\n", rc);
1323 SetLastError(res);
1324 return rc;
1328 /***********************************************************************
1329 * InternetConnectA (WININET.@)
1331 * Open a ftp, gopher or http session
1333 * RETURNS
1334 * HINTERNET a session handle on success
1335 * NULL on failure
1338 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1339 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1340 LPCSTR lpszUserName, LPCSTR lpszPassword,
1341 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1343 HINTERNET rc = NULL;
1344 LPWSTR szServerName;
1345 LPWSTR szUserName;
1346 LPWSTR szPassword;
1348 szServerName = heap_strdupAtoW(lpszServerName);
1349 szUserName = heap_strdupAtoW(lpszUserName);
1350 szPassword = heap_strdupAtoW(lpszPassword);
1352 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1353 szUserName, szPassword, dwService, dwFlags, dwContext);
1355 heap_free(szServerName);
1356 heap_free(szUserName);
1357 heap_free(szPassword);
1358 return rc;
1362 /***********************************************************************
1363 * InternetFindNextFileA (WININET.@)
1365 * Continues a file search from a previous call to FindFirstFile
1367 * RETURNS
1368 * TRUE on success
1369 * FALSE on failure
1372 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1374 BOOL ret;
1375 WIN32_FIND_DATAW fd;
1377 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1378 if(lpvFindData)
1379 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1380 return ret;
1383 /***********************************************************************
1384 * InternetFindNextFileW (WININET.@)
1386 * Continues a file search from a previous call to FindFirstFile
1388 * RETURNS
1389 * TRUE on success
1390 * FALSE on failure
1393 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1395 object_header_t *hdr;
1396 DWORD res;
1398 TRACE("\n");
1400 hdr = get_handle_object(hFind);
1401 if(!hdr) {
1402 WARN("Invalid handle\n");
1403 SetLastError(ERROR_INVALID_HANDLE);
1404 return FALSE;
1407 if(hdr->vtbl->FindNextFileW) {
1408 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1409 }else {
1410 WARN("Handle doesn't support NextFile\n");
1411 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1414 WININET_Release(hdr);
1416 if(res != ERROR_SUCCESS)
1417 SetLastError(res);
1418 return res == ERROR_SUCCESS;
1421 /***********************************************************************
1422 * InternetCloseHandle (WININET.@)
1424 * Generic close handle function
1426 * RETURNS
1427 * TRUE on success
1428 * FALSE on failure
1431 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1433 object_header_t *obj;
1435 TRACE("%p\n", hInternet);
1437 obj = get_handle_object( hInternet );
1438 if (!obj) {
1439 SetLastError(ERROR_INVALID_HANDLE);
1440 return FALSE;
1443 invalidate_handle(obj);
1444 WININET_Release(obj);
1446 return TRUE;
1449 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1451 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1453 if (!*component_length)
1454 return TRUE;
1456 if (!*component) {
1457 *(const WCHAR**)component = value;
1458 *component_length = len;
1459 return TRUE;
1462 if (*component_length < len+1) {
1463 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1464 return FALSE;
1467 *component_length = len;
1468 if(len)
1469 memcpy(*component, value, len*sizeof(WCHAR));
1470 (*component)[len] = 0;
1471 return TRUE;
1474 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1475 const char *url_a)
1477 size_t size, ret_size = *ret_length;
1479 if (!*ret_length)
1480 return TRUE;
1481 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1483 if (!*comp) {
1484 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1485 *ret_length = size;
1486 return TRUE;
1489 if (size+1 > ret_size) {
1490 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1491 *ret_length = size+1;
1492 return FALSE;
1495 *ret_length = size;
1496 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1497 (*comp)[size] = 0;
1498 return TRUE;
1501 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1503 *len_w = len_a;
1505 if(!comp_a) {
1506 *comp_w = NULL;
1507 return TRUE;
1510 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1511 SetLastError(ERROR_OUTOFMEMORY);
1512 return FALSE;
1515 return TRUE;
1518 /***********************************************************************
1519 * InternetCrackUrlA (WININET.@)
1521 * See InternetCrackUrlW.
1523 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1525 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1526 URL_COMPONENTSW comp;
1527 WCHAR *url_w = NULL;
1528 BOOL ret;
1530 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1532 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1533 SetLastError(ERROR_INVALID_PARAMETER);
1534 return FALSE;
1537 comp.dwStructSize = sizeof(comp);
1539 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1540 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1541 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1542 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1543 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1544 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1545 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1546 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1547 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1548 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1549 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1550 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1552 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1553 SetLastError(ERROR_OUTOFMEMORY);
1554 ret = FALSE;
1557 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1558 ret_comp->nScheme = comp.nScheme;
1559 ret_comp->nPort = comp.nPort;
1561 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1562 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1563 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1564 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1565 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1566 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1567 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1568 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1569 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1570 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1571 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1572 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1574 if(ret)
1575 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1576 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1577 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1578 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1579 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1582 heap_free(host);
1583 heap_free(user);
1584 heap_free(pass);
1585 heap_free(path);
1586 heap_free(scheme);
1587 heap_free(extra);
1588 heap_free(url_w);
1589 return ret;
1592 static const WCHAR url_schemes[][7] =
1594 {'f','t','p',0},
1595 {'g','o','p','h','e','r',0},
1596 {'h','t','t','p',0},
1597 {'h','t','t','p','s',0},
1598 {'f','i','l','e',0},
1599 {'n','e','w','s',0},
1600 {'m','a','i','l','t','o',0},
1601 {'r','e','s',0},
1604 /***********************************************************************
1605 * GetInternetSchemeW (internal)
1607 * Get scheme of url
1609 * RETURNS
1610 * scheme on success
1611 * INTERNET_SCHEME_UNKNOWN on failure
1614 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1616 int i;
1618 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1620 if(lpszScheme==NULL)
1621 return INTERNET_SCHEME_UNKNOWN;
1623 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1624 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1625 return INTERNET_SCHEME_FIRST + i;
1627 return INTERNET_SCHEME_UNKNOWN;
1630 /***********************************************************************
1631 * InternetCrackUrlW (WININET.@)
1633 * Break up URL into its components
1635 * RETURNS
1636 * TRUE on success
1637 * FALSE on failure
1639 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1642 * RFC 1808
1643 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1646 LPCWSTR lpszParam = NULL;
1647 BOOL found_colon = FALSE;
1648 LPCWSTR lpszap;
1649 LPCWSTR lpszcp = NULL, lpszNetLoc;
1651 TRACE("(%s %u %x %p)\n",
1652 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1653 dwUrlLength, dwFlags, lpUC);
1655 if (!lpszUrl || !*lpszUrl || !lpUC)
1657 SetLastError(ERROR_INVALID_PARAMETER);
1658 return FALSE;
1660 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1662 if (dwFlags & ICU_DECODE)
1664 WCHAR *url_tmp;
1665 DWORD len = dwUrlLength + 1;
1666 BOOL ret;
1668 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1670 SetLastError(ERROR_OUTOFMEMORY);
1671 return FALSE;
1673 ret = InternetCanonicalizeUrlW(url_tmp, url_tmp, &len, ICU_DECODE | ICU_NO_ENCODE);
1674 if (ret)
1675 ret = InternetCrackUrlW(url_tmp, len, dwFlags & ~ICU_DECODE, lpUC);
1676 heap_free(url_tmp);
1677 return ret;
1679 lpszap = lpszUrl;
1681 /* Determine if the URI is absolute. */
1682 while (lpszap - lpszUrl < dwUrlLength)
1684 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1686 lpszap++;
1687 continue;
1689 if (*lpszap == ':')
1691 found_colon = TRUE;
1692 lpszcp = lpszap;
1694 else
1696 lpszcp = lpszUrl; /* Relative url */
1699 break;
1702 if(!found_colon){
1703 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1704 return FALSE;
1707 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1708 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1710 /* Parse <params> */
1711 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1712 if(!lpszParam)
1713 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1715 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1716 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1717 return FALSE;
1720 /* Get scheme first. */
1721 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1722 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1723 return FALSE;
1725 /* Eat ':' in protocol. */
1726 lpszcp++;
1728 /* double slash indicates the net_loc portion is present */
1729 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1731 lpszcp += 2;
1733 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1734 if (lpszParam)
1736 if (lpszNetLoc)
1737 lpszNetLoc = min(lpszNetLoc, lpszParam);
1738 else
1739 lpszNetLoc = lpszParam;
1741 else if (!lpszNetLoc)
1742 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1744 /* Parse net-loc */
1745 if (lpszNetLoc)
1747 LPCWSTR lpszHost;
1748 LPCWSTR lpszPort;
1750 /* [<user>[<:password>]@]<host>[:<port>] */
1751 /* First find the user and password if they exist */
1753 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1754 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1756 /* username and password not specified. */
1757 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1758 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1760 else /* Parse out username and password */
1762 LPCWSTR lpszUser = lpszcp;
1763 LPCWSTR lpszPasswd = lpszHost;
1765 while (lpszcp < lpszHost)
1767 if (*lpszcp == ':')
1768 lpszPasswd = lpszcp;
1770 lpszcp++;
1773 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1774 return FALSE;
1776 if (lpszPasswd != lpszHost)
1777 lpszPasswd++;
1778 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1779 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1780 return FALSE;
1782 lpszcp++; /* Advance to beginning of host */
1785 /* Parse <host><:port> */
1787 lpszHost = lpszcp;
1788 lpszPort = lpszNetLoc;
1790 /* special case for res:// URLs: there is no port here, so the host is the
1791 entire string up to the first '/' */
1792 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1794 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1795 return FALSE;
1796 lpszcp=lpszNetLoc;
1798 else
1800 while (lpszcp < lpszNetLoc)
1802 if (*lpszcp == ':')
1803 lpszPort = lpszcp;
1805 lpszcp++;
1808 /* If the scheme is "file" and the host is just one letter, it's not a host */
1809 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1811 lpszcp=lpszHost;
1812 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1814 else
1816 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1817 return FALSE;
1818 if (lpszPort != lpszNetLoc)
1819 lpUC->nPort = atoiW(++lpszPort);
1820 else switch (lpUC->nScheme)
1822 case INTERNET_SCHEME_HTTP:
1823 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1824 break;
1825 case INTERNET_SCHEME_HTTPS:
1826 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1827 break;
1828 case INTERNET_SCHEME_FTP:
1829 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1830 break;
1831 case INTERNET_SCHEME_GOPHER:
1832 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1833 break;
1834 default:
1835 break;
1841 else
1843 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1844 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1845 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1848 /* Here lpszcp points to:
1850 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1851 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1853 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1855 DWORD len;
1857 /* Only truncate the parameter list if it's already been saved
1858 * in lpUC->lpszExtraInfo.
1860 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1861 len = lpszParam - lpszcp;
1862 else
1864 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1865 * newlines if necessary.
1867 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1868 if (lpsznewline != NULL)
1869 len = lpsznewline - lpszcp;
1870 else
1871 len = dwUrlLength-(lpszcp-lpszUrl);
1873 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1874 lpUC->nScheme == INTERNET_SCHEME_FILE)
1876 WCHAR tmppath[MAX_PATH];
1877 if (*lpszcp == '/')
1879 len = MAX_PATH;
1880 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1882 else
1884 WCHAR *iter;
1885 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1886 tmppath[len] = '\0';
1888 iter = tmppath;
1889 while (*iter) {
1890 if (*iter == '/')
1891 *iter = '\\';
1892 ++iter;
1895 /* if ends in \. or \.. append a backslash */
1896 if (tmppath[len - 1] == '.' &&
1897 (tmppath[len - 2] == '\\' ||
1898 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1900 if (len < MAX_PATH - 1)
1902 tmppath[len] = '\\';
1903 tmppath[len+1] = '\0';
1904 ++len;
1907 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1908 return FALSE;
1910 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1911 return FALSE;
1913 else
1915 set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, 0);
1918 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1919 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1920 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1921 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1922 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1924 return TRUE;
1927 /***********************************************************************
1928 * InternetAttemptConnect (WININET.@)
1930 * Attempt to make a connection to the internet
1932 * RETURNS
1933 * ERROR_SUCCESS on success
1934 * Error value on failure
1937 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1939 FIXME("Stub\n");
1940 return ERROR_SUCCESS;
1944 /***********************************************************************
1945 * convert_url_canonicalization_flags
1947 * Helper for InternetCanonicalizeUrl
1949 * PARAMS
1950 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1952 * RETURNS
1953 * Flags suitable for UrlCanonicalize
1955 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1957 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1959 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1960 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1961 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1962 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1963 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1964 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1965 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1967 return dwUrlFlags;
1970 /***********************************************************************
1971 * InternetCanonicalizeUrlA (WININET.@)
1973 * Escape unsafe characters and spaces
1975 * RETURNS
1976 * TRUE on success
1977 * FALSE on failure
1980 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1981 LPDWORD lpdwBufferLength, DWORD dwFlags)
1983 HRESULT hr;
1985 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1986 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1988 dwFlags = convert_url_canonicalization_flags(dwFlags);
1989 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1990 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1991 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1993 return hr == S_OK;
1996 /***********************************************************************
1997 * InternetCanonicalizeUrlW (WININET.@)
1999 * Escape unsafe characters and spaces
2001 * RETURNS
2002 * TRUE on success
2003 * FALSE on failure
2006 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2007 LPDWORD lpdwBufferLength, DWORD dwFlags)
2009 HRESULT hr;
2011 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2012 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2014 dwFlags = convert_url_canonicalization_flags(dwFlags);
2015 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2016 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2017 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2019 return hr == S_OK;
2022 /* #################################################### */
2024 static INTERNET_STATUS_CALLBACK set_status_callback(
2025 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2027 INTERNET_STATUS_CALLBACK ret;
2029 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2030 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2032 ret = lpwh->lpfnStatusCB;
2033 lpwh->lpfnStatusCB = callback;
2035 return ret;
2038 /***********************************************************************
2039 * InternetSetStatusCallbackA (WININET.@)
2041 * Sets up a callback function which is called as progress is made
2042 * during an operation.
2044 * RETURNS
2045 * Previous callback or NULL on success
2046 * INTERNET_INVALID_STATUS_CALLBACK on failure
2049 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2050 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2052 INTERNET_STATUS_CALLBACK retVal;
2053 object_header_t *lpwh;
2055 TRACE("%p\n", hInternet);
2057 if (!(lpwh = get_handle_object(hInternet)))
2058 return INTERNET_INVALID_STATUS_CALLBACK;
2060 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2062 WININET_Release( lpwh );
2063 return retVal;
2066 /***********************************************************************
2067 * InternetSetStatusCallbackW (WININET.@)
2069 * Sets up a callback function which is called as progress is made
2070 * during an operation.
2072 * RETURNS
2073 * Previous callback or NULL on success
2074 * INTERNET_INVALID_STATUS_CALLBACK on failure
2077 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2078 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2080 INTERNET_STATUS_CALLBACK retVal;
2081 object_header_t *lpwh;
2083 TRACE("%p\n", hInternet);
2085 if (!(lpwh = get_handle_object(hInternet)))
2086 return INTERNET_INVALID_STATUS_CALLBACK;
2088 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2090 WININET_Release( lpwh );
2091 return retVal;
2094 /***********************************************************************
2095 * InternetSetFilePointer (WININET.@)
2097 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2098 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2100 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2101 return FALSE;
2104 /***********************************************************************
2105 * InternetWriteFile (WININET.@)
2107 * Write data to an open internet file
2109 * RETURNS
2110 * TRUE on success
2111 * FALSE on failure
2114 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2115 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2117 object_header_t *lpwh;
2118 BOOL res;
2120 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2122 lpwh = get_handle_object( hFile );
2123 if (!lpwh) {
2124 WARN("Invalid handle\n");
2125 SetLastError(ERROR_INVALID_HANDLE);
2126 return FALSE;
2129 if(lpwh->vtbl->WriteFile) {
2130 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2131 }else {
2132 WARN("No Writefile method.\n");
2133 res = ERROR_INVALID_HANDLE;
2136 WININET_Release( lpwh );
2138 if(res != ERROR_SUCCESS)
2139 SetLastError(res);
2140 return res == ERROR_SUCCESS;
2144 /***********************************************************************
2145 * InternetReadFile (WININET.@)
2147 * Read data from an open internet file
2149 * RETURNS
2150 * TRUE on success
2151 * FALSE on failure
2154 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2155 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2157 object_header_t *hdr;
2158 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2160 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2162 hdr = get_handle_object(hFile);
2163 if (!hdr) {
2164 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2165 return FALSE;
2168 if(hdr->vtbl->ReadFile) {
2169 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, 0, 0);
2170 if(res == ERROR_IO_PENDING)
2171 *pdwNumOfBytesRead = 0;
2174 WININET_Release(hdr);
2176 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2177 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2179 if(res != ERROR_SUCCESS)
2180 SetLastError(res);
2181 return res == ERROR_SUCCESS;
2184 /***********************************************************************
2185 * InternetReadFileExA (WININET.@)
2187 * Read data from an open internet file
2189 * PARAMS
2190 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2191 * lpBuffersOut [I/O] Buffer.
2192 * dwFlags [I] Flags. See notes.
2193 * dwContext [I] Context for callbacks.
2195 * RETURNS
2196 * TRUE on success
2197 * FALSE on failure
2199 * NOTES
2200 * The parameter dwFlags include zero or more of the following flags:
2201 *|IRF_ASYNC - Makes the call asynchronous.
2202 *|IRF_SYNC - Makes the call synchronous.
2203 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2204 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2206 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2208 * SEE
2209 * InternetOpenUrlA(), HttpOpenRequestA()
2211 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2212 DWORD dwFlags, DWORD_PTR dwContext)
2214 object_header_t *hdr;
2215 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2217 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2219 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2220 SetLastError(ERROR_INVALID_PARAMETER);
2221 return FALSE;
2224 hdr = get_handle_object(hFile);
2225 if (!hdr) {
2226 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2227 return FALSE;
2230 if(hdr->vtbl->ReadFile)
2231 res = hdr->vtbl->ReadFile(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2232 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2234 WININET_Release(hdr);
2236 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2237 res, lpBuffersOut->dwBufferLength);
2239 if(res != ERROR_SUCCESS)
2240 SetLastError(res);
2241 return res == ERROR_SUCCESS;
2244 /***********************************************************************
2245 * InternetReadFileExW (WININET.@)
2246 * SEE
2247 * InternetReadFileExA()
2249 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2250 DWORD dwFlags, DWORD_PTR dwContext)
2252 object_header_t *hdr;
2253 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2255 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2257 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2258 SetLastError(ERROR_INVALID_PARAMETER);
2259 return FALSE;
2262 hdr = get_handle_object(hFile);
2263 if (!hdr) {
2264 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2265 return FALSE;
2268 if(hdr->vtbl->ReadFile)
2269 res = hdr->vtbl->ReadFile(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2270 dwFlags, dwContext);
2272 WININET_Release(hdr);
2274 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2275 res, lpBuffer->dwBufferLength);
2277 if(res != ERROR_SUCCESS)
2278 SetLastError(res);
2279 return res == ERROR_SUCCESS;
2282 static WCHAR *get_proxy_autoconfig_url(void)
2284 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2286 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2287 WCHAR *ret = NULL;
2288 SIZE_T len;
2289 const void *ref;
2291 if (!settings) return NULL;
2293 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2295 CFRelease( settings );
2296 return NULL;
2298 len = CFStringGetLength( ref );
2299 if (len)
2300 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2301 if (ret)
2303 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2304 ret[len] = 0;
2306 TRACE( "returning %s\n", debugstr_w(ret) );
2307 CFRelease( settings );
2308 return ret;
2309 #else
2310 FIXME( "no support on this platform\n" );
2311 return NULL;
2312 #endif
2315 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2317 /* FIXME: This function currently handles more options than it should. Options requiring
2318 * proper handles should be moved to proper functions */
2319 switch(option) {
2320 case INTERNET_OPTION_HTTP_VERSION:
2321 if (*size < sizeof(HTTP_VERSION_INFO))
2322 return ERROR_INSUFFICIENT_BUFFER;
2325 * Presently hardcoded to 1.1
2327 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2328 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2329 *size = sizeof(HTTP_VERSION_INFO);
2331 return ERROR_SUCCESS;
2333 case INTERNET_OPTION_CONNECTED_STATE:
2334 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2336 if (*size < sizeof(ULONG))
2337 return ERROR_INSUFFICIENT_BUFFER;
2339 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2340 *size = sizeof(ULONG);
2342 return ERROR_SUCCESS;
2344 case INTERNET_OPTION_PROXY: {
2345 appinfo_t ai;
2346 BOOL ret;
2348 TRACE("Getting global proxy info\n");
2349 memset(&ai, 0, sizeof(appinfo_t));
2350 INTERNET_ConfigureProxy(&ai);
2352 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2353 APPINFO_Destroy(&ai.hdr);
2354 return ret;
2357 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2358 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2360 if (*size < sizeof(ULONG))
2361 return ERROR_INSUFFICIENT_BUFFER;
2363 *(ULONG*)buffer = max_conns;
2364 *size = sizeof(ULONG);
2366 return ERROR_SUCCESS;
2368 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2369 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2371 if (*size < sizeof(ULONG))
2372 return ERROR_INSUFFICIENT_BUFFER;
2374 *(ULONG*)buffer = max_1_0_conns;
2375 *size = sizeof(ULONG);
2377 return ERROR_SUCCESS;
2379 case INTERNET_OPTION_SECURITY_FLAGS:
2380 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2381 return ERROR_SUCCESS;
2383 case INTERNET_OPTION_VERSION: {
2384 static const INTERNET_VERSION_INFO info = { 1, 2 };
2386 TRACE("INTERNET_OPTION_VERSION\n");
2388 if (*size < sizeof(INTERNET_VERSION_INFO))
2389 return ERROR_INSUFFICIENT_BUFFER;
2391 memcpy(buffer, &info, sizeof(info));
2392 *size = sizeof(info);
2394 return ERROR_SUCCESS;
2397 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2398 WCHAR *url;
2399 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2400 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2401 DWORD res = ERROR_SUCCESS, i;
2402 proxyinfo_t pi;
2403 LONG ret;
2405 TRACE("Getting global proxy info\n");
2406 if((ret = INTERNET_LoadProxySettings(&pi)))
2407 return ret;
2409 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2411 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2412 FreeProxyInfo(&pi);
2413 return ERROR_INSUFFICIENT_BUFFER;
2416 url = get_proxy_autoconfig_url();
2418 for (i = 0; i < con->dwOptionCount; i++) {
2419 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2420 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2422 switch (optionW->dwOption) {
2423 case INTERNET_PER_CONN_FLAGS:
2424 if(pi.proxyEnabled)
2425 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2426 else
2427 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2428 if (url)
2429 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2430 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2431 break;
2433 case INTERNET_PER_CONN_PROXY_SERVER:
2434 if (unicode)
2435 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2436 else
2437 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2438 break;
2440 case INTERNET_PER_CONN_PROXY_BYPASS:
2441 if (unicode)
2442 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2443 else
2444 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2445 break;
2447 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2448 if (!url)
2449 optionW->Value.pszValue = NULL;
2450 else if (unicode)
2451 optionW->Value.pszValue = heap_strdupW(url);
2452 else
2453 optionA->Value.pszValue = heap_strdupWtoA(url);
2454 break;
2456 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2457 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2458 break;
2460 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2461 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2462 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2463 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2464 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2465 memset(&optionW->Value, 0, sizeof(optionW->Value));
2466 break;
2468 default:
2469 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2470 res = ERROR_INVALID_PARAMETER;
2471 break;
2474 heap_free(url);
2475 FreeProxyInfo(&pi);
2477 return res;
2479 case INTERNET_OPTION_REQUEST_FLAGS:
2480 case INTERNET_OPTION_USER_AGENT:
2481 *size = 0;
2482 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2483 case INTERNET_OPTION_POLICY:
2484 return ERROR_INVALID_PARAMETER;
2485 case INTERNET_OPTION_CONNECT_TIMEOUT:
2486 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2488 if (*size < sizeof(ULONG))
2489 return ERROR_INSUFFICIENT_BUFFER;
2491 *(ULONG*)buffer = connect_timeout;
2492 *size = sizeof(ULONG);
2494 return ERROR_SUCCESS;
2497 FIXME("Stub for %d\n", option);
2498 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2501 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2503 switch(option) {
2504 case INTERNET_OPTION_CONTEXT_VALUE:
2505 if (!size)
2506 return ERROR_INVALID_PARAMETER;
2508 if (*size < sizeof(DWORD_PTR)) {
2509 *size = sizeof(DWORD_PTR);
2510 return ERROR_INSUFFICIENT_BUFFER;
2512 if (!buffer)
2513 return ERROR_INVALID_PARAMETER;
2515 *(DWORD_PTR *)buffer = hdr->dwContext;
2516 *size = sizeof(DWORD_PTR);
2517 return ERROR_SUCCESS;
2519 case INTERNET_OPTION_REQUEST_FLAGS:
2520 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2521 *size = sizeof(DWORD);
2522 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2524 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2525 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2526 WARN("Called on global option %u\n", option);
2527 return ERROR_INTERNET_INVALID_OPERATION;
2530 /* FIXME: we shouldn't call it here */
2531 return query_global_option(option, buffer, size, unicode);
2534 /***********************************************************************
2535 * InternetQueryOptionW (WININET.@)
2537 * Queries an options on the specified handle
2539 * RETURNS
2540 * TRUE on success
2541 * FALSE on failure
2544 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2545 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2547 object_header_t *hdr;
2548 DWORD res = ERROR_INVALID_HANDLE;
2550 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2552 if(hInternet) {
2553 hdr = get_handle_object(hInternet);
2554 if (hdr) {
2555 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2556 WININET_Release(hdr);
2558 }else {
2559 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2562 if(res != ERROR_SUCCESS)
2563 SetLastError(res);
2564 return res == ERROR_SUCCESS;
2567 /***********************************************************************
2568 * InternetQueryOptionA (WININET.@)
2570 * Queries an options on the specified handle
2572 * RETURNS
2573 * TRUE on success
2574 * FALSE on failure
2577 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2578 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2580 object_header_t *hdr;
2581 DWORD res = ERROR_INVALID_HANDLE;
2583 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2585 if(hInternet) {
2586 hdr = get_handle_object(hInternet);
2587 if (hdr) {
2588 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2589 WININET_Release(hdr);
2591 }else {
2592 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2595 if(res != ERROR_SUCCESS)
2596 SetLastError(res);
2597 return res == ERROR_SUCCESS;
2600 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2602 switch(option) {
2603 case INTERNET_OPTION_CALLBACK:
2604 WARN("Not settable option %u\n", option);
2605 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2606 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2607 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2608 WARN("Called on global option %u\n", option);
2609 return ERROR_INTERNET_INVALID_OPERATION;
2612 return ERROR_INTERNET_INVALID_OPTION;
2615 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2617 switch(option) {
2618 case INTERNET_OPTION_CALLBACK:
2619 WARN("Not global option %u\n", option);
2620 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2622 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2623 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2625 if(size != sizeof(max_conns))
2626 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2627 if(!*(ULONG*)buf)
2628 return ERROR_BAD_ARGUMENTS;
2630 max_conns = *(ULONG*)buf;
2631 return ERROR_SUCCESS;
2633 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2634 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2636 if(size != sizeof(max_1_0_conns))
2637 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2638 if(!*(ULONG*)buf)
2639 return ERROR_BAD_ARGUMENTS;
2641 max_1_0_conns = *(ULONG*)buf;
2642 return ERROR_SUCCESS;
2644 case INTERNET_OPTION_CONNECT_TIMEOUT:
2645 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2647 if(size != sizeof(connect_timeout))
2648 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2649 if(!*(ULONG*)buf)
2650 return ERROR_BAD_ARGUMENTS;
2652 connect_timeout = *(ULONG*)buf;
2653 return ERROR_SUCCESS;
2655 case INTERNET_OPTION_SETTINGS_CHANGED:
2656 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2657 collect_connections(COLLECT_CONNECTIONS);
2658 return ERROR_SUCCESS;
2660 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2661 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2663 if(size != sizeof(ULONG))
2664 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2666 FIXME("%08x\n", *(ULONG*)buf);
2667 return ERROR_SUCCESS;
2670 return ERROR_INTERNET_INVALID_OPTION;
2673 /***********************************************************************
2674 * InternetSetOptionW (WININET.@)
2676 * Sets an options on the specified handle
2678 * RETURNS
2679 * TRUE on success
2680 * FALSE on failure
2683 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2684 LPVOID lpBuffer, DWORD dwBufferLength)
2686 object_header_t *lpwhh;
2687 BOOL ret = TRUE;
2688 DWORD res;
2690 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2692 lpwhh = (object_header_t*) get_handle_object( hInternet );
2693 if(lpwhh)
2694 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2695 else
2696 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2698 if(res != ERROR_INTERNET_INVALID_OPTION) {
2699 if(lpwhh)
2700 WININET_Release(lpwhh);
2702 if(res != ERROR_SUCCESS)
2703 SetLastError(res);
2705 return res == ERROR_SUCCESS;
2708 switch (dwOption)
2710 case INTERNET_OPTION_HTTP_VERSION:
2712 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2713 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2715 break;
2716 case INTERNET_OPTION_ERROR_MASK:
2718 if(!lpwhh) {
2719 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2720 return FALSE;
2721 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2722 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2723 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2724 SetLastError(ERROR_INVALID_PARAMETER);
2725 ret = FALSE;
2726 } else if(dwBufferLength != sizeof(ULONG)) {
2727 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2728 ret = FALSE;
2729 } else
2730 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2731 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2733 break;
2734 case INTERNET_OPTION_PROXY:
2736 INTERNET_PROXY_INFOW *info = lpBuffer;
2738 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2740 SetLastError(ERROR_INVALID_PARAMETER);
2741 return FALSE;
2743 if (!hInternet)
2745 EnterCriticalSection( &WININET_cs );
2746 free_global_proxy();
2747 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2748 if (global_proxy)
2750 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2752 global_proxy->proxyEnabled = 1;
2753 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2754 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2756 else
2758 global_proxy->proxyEnabled = 0;
2759 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2762 LeaveCriticalSection( &WININET_cs );
2764 else
2766 /* In general, each type of object should handle
2767 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2768 * get silently dropped.
2770 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2771 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2772 ret = FALSE;
2774 break;
2776 case INTERNET_OPTION_CODEPAGE:
2778 ULONG codepage = *(ULONG *)lpBuffer;
2779 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2781 break;
2782 case INTERNET_OPTION_REQUEST_PRIORITY:
2784 ULONG priority = *(ULONG *)lpBuffer;
2785 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2787 break;
2788 case INTERNET_OPTION_CONNECT_TIMEOUT:
2790 ULONG connecttimeout = *(ULONG *)lpBuffer;
2791 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2793 break;
2794 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2796 ULONG receivetimeout = *(ULONG *)lpBuffer;
2797 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2799 break;
2800 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2801 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2802 break;
2803 case INTERNET_OPTION_END_BROWSER_SESSION:
2804 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: semi-stub\n");
2805 free_cookie();
2806 break;
2807 case INTERNET_OPTION_CONNECTED_STATE:
2808 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2809 break;
2810 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2811 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2812 break;
2813 case INTERNET_OPTION_SEND_TIMEOUT:
2814 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2815 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2817 ULONG timeout = *(ULONG *)lpBuffer;
2818 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2819 break;
2821 case INTERNET_OPTION_CONNECT_RETRIES:
2823 ULONG retries = *(ULONG *)lpBuffer;
2824 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2825 break;
2827 case INTERNET_OPTION_CONTEXT_VALUE:
2829 if (!lpwhh)
2831 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2832 return FALSE;
2834 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2836 SetLastError(ERROR_INVALID_PARAMETER);
2837 ret = FALSE;
2839 else
2840 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2841 break;
2843 case INTERNET_OPTION_SECURITY_FLAGS:
2844 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2845 break;
2846 case INTERNET_OPTION_DISABLE_AUTODIAL:
2847 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2848 break;
2849 case INTERNET_OPTION_HTTP_DECODING:
2850 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2851 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2852 ret = FALSE;
2853 break;
2854 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2855 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2856 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2857 ret = FALSE;
2858 break;
2859 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2860 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2861 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2862 ret = FALSE;
2863 break;
2864 case INTERNET_OPTION_CODEPAGE_PATH:
2865 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2866 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2867 ret = FALSE;
2868 break;
2869 case INTERNET_OPTION_CODEPAGE_EXTRA:
2870 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2871 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2872 ret = FALSE;
2873 break;
2874 case INTERNET_OPTION_IDN:
2875 FIXME("INTERNET_OPTION_IDN; STUB\n");
2876 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2877 ret = FALSE;
2878 break;
2879 case INTERNET_OPTION_POLICY:
2880 SetLastError(ERROR_INVALID_PARAMETER);
2881 ret = FALSE;
2882 break;
2883 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2884 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2885 LONG res;
2886 unsigned int i;
2887 proxyinfo_t pi;
2889 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2891 for (i = 0; i < con->dwOptionCount; i++) {
2892 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2894 switch (option->dwOption) {
2895 case INTERNET_PER_CONN_PROXY_SERVER:
2896 heap_free(pi.proxy);
2897 pi.proxy = heap_strdupW(option->Value.pszValue);
2898 break;
2900 case INTERNET_PER_CONN_FLAGS:
2901 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2902 pi.proxyEnabled = 1;
2903 else
2905 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2906 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2907 pi.proxyEnabled = 0;
2909 break;
2911 case INTERNET_PER_CONN_PROXY_BYPASS:
2912 heap_free(pi.proxyBypass);
2913 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2914 break;
2916 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2917 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2918 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2919 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2920 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2921 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2922 FIXME("Unhandled dwOption %d\n", option->dwOption);
2923 break;
2925 default:
2926 FIXME("Unknown dwOption %d\n", option->dwOption);
2927 SetLastError(ERROR_INVALID_PARAMETER);
2928 break;
2932 if ((res = INTERNET_SaveProxySettings(&pi)))
2933 SetLastError(res);
2935 FreeProxyInfo(&pi);
2937 ret = (res == ERROR_SUCCESS);
2938 break;
2940 default:
2941 FIXME("Option %d STUB\n",dwOption);
2942 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2943 ret = FALSE;
2944 break;
2947 if(lpwhh)
2948 WININET_Release( lpwhh );
2950 return ret;
2954 /***********************************************************************
2955 * InternetSetOptionA (WININET.@)
2957 * Sets an options on the specified handle.
2959 * RETURNS
2960 * TRUE on success
2961 * FALSE on failure
2964 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2965 LPVOID lpBuffer, DWORD dwBufferLength)
2967 LPVOID wbuffer;
2968 DWORD wlen;
2969 BOOL r;
2971 switch( dwOption )
2973 case INTERNET_OPTION_PROXY:
2975 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2976 LPINTERNET_PROXY_INFOW piw;
2977 DWORD proxlen, prbylen;
2978 LPWSTR prox, prby;
2980 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2981 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2982 wlen = sizeof(*piw) + proxlen + prbylen;
2983 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2984 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2985 piw->dwAccessType = pi->dwAccessType;
2986 prox = (LPWSTR) &piw[1];
2987 prby = &prox[proxlen+1];
2988 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2989 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2990 piw->lpszProxy = prox;
2991 piw->lpszProxyBypass = prby;
2993 break;
2994 case INTERNET_OPTION_USER_AGENT:
2995 case INTERNET_OPTION_USERNAME:
2996 case INTERNET_OPTION_PASSWORD:
2997 case INTERNET_OPTION_PROXY_USERNAME:
2998 case INTERNET_OPTION_PROXY_PASSWORD:
2999 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3000 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3001 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3002 break;
3003 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3004 unsigned int i;
3005 INTERNET_PER_CONN_OPTION_LISTW *listW;
3006 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3007 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3008 wbuffer = heap_alloc(wlen);
3009 listW = wbuffer;
3011 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3012 if (listA->pszConnection)
3014 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3015 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3016 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3018 else
3019 listW->pszConnection = NULL;
3020 listW->dwOptionCount = listA->dwOptionCount;
3021 listW->dwOptionError = listA->dwOptionError;
3022 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3024 for (i = 0; i < listA->dwOptionCount; ++i) {
3025 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3026 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3028 optW->dwOption = optA->dwOption;
3030 switch (optA->dwOption) {
3031 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3032 case INTERNET_PER_CONN_PROXY_BYPASS:
3033 case INTERNET_PER_CONN_PROXY_SERVER:
3034 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3035 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3036 if (optA->Value.pszValue)
3038 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3039 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3040 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3042 else
3043 optW->Value.pszValue = NULL;
3044 break;
3045 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3046 case INTERNET_PER_CONN_FLAGS:
3047 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3048 optW->Value.dwValue = optA->Value.dwValue;
3049 break;
3050 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3051 optW->Value.ftValue = optA->Value.ftValue;
3052 break;
3053 default:
3054 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3055 optW->Value.dwValue = optA->Value.dwValue;
3056 break;
3060 break;
3061 default:
3062 wbuffer = lpBuffer;
3063 wlen = dwBufferLength;
3066 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3068 if( lpBuffer != wbuffer )
3070 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3072 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3073 unsigned int i;
3074 for (i = 0; i < list->dwOptionCount; ++i) {
3075 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3076 switch (opt->dwOption) {
3077 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3078 case INTERNET_PER_CONN_PROXY_BYPASS:
3079 case INTERNET_PER_CONN_PROXY_SERVER:
3080 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3081 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3082 heap_free( opt->Value.pszValue );
3083 break;
3084 default:
3085 break;
3088 heap_free( list->pOptions );
3090 heap_free( wbuffer );
3093 return r;
3097 /***********************************************************************
3098 * InternetSetOptionExA (WININET.@)
3100 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3101 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3103 FIXME("Flags %08x ignored\n", dwFlags);
3104 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3107 /***********************************************************************
3108 * InternetSetOptionExW (WININET.@)
3110 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3111 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3113 FIXME("Flags %08x ignored\n", dwFlags);
3114 if( dwFlags & ~ISO_VALID_FLAGS )
3116 SetLastError( ERROR_INVALID_PARAMETER );
3117 return FALSE;
3119 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3122 static const WCHAR WININET_wkday[7][4] =
3123 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3124 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3125 static const WCHAR WININET_month[12][4] =
3126 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3127 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3128 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3130 /***********************************************************************
3131 * InternetTimeFromSystemTimeA (WININET.@)
3133 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3135 BOOL ret;
3136 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3138 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3140 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3142 SetLastError(ERROR_INVALID_PARAMETER);
3143 return FALSE;
3146 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3148 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3149 return FALSE;
3152 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3153 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3155 return ret;
3158 /***********************************************************************
3159 * InternetTimeFromSystemTimeW (WININET.@)
3161 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3163 static const WCHAR date[] =
3164 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3165 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3167 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3169 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3171 SetLastError(ERROR_INVALID_PARAMETER);
3172 return FALSE;
3175 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3177 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3178 return FALSE;
3181 sprintfW( string, date,
3182 WININET_wkday[time->wDayOfWeek],
3183 time->wDay,
3184 WININET_month[time->wMonth - 1],
3185 time->wYear,
3186 time->wHour,
3187 time->wMinute,
3188 time->wSecond );
3190 return TRUE;
3193 /***********************************************************************
3194 * InternetTimeToSystemTimeA (WININET.@)
3196 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3198 BOOL ret = FALSE;
3199 WCHAR *stringW;
3201 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3203 stringW = heap_strdupAtoW(string);
3204 if (stringW)
3206 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3207 heap_free( stringW );
3209 return ret;
3212 /***********************************************************************
3213 * InternetTimeToSystemTimeW (WININET.@)
3215 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3217 unsigned int i;
3218 const WCHAR *s = string;
3219 WCHAR *end;
3221 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3223 if (!string || !time) return FALSE;
3225 /* Windows does this too */
3226 GetSystemTime( time );
3228 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3229 * a SYSTEMTIME structure.
3232 while (*s && !isalphaW( *s )) s++;
3233 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3234 time->wDayOfWeek = 7;
3236 for (i = 0; i < 7; i++)
3238 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3239 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3240 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3242 time->wDayOfWeek = i;
3243 break;
3247 if (time->wDayOfWeek > 6) return TRUE;
3248 while (*s && !isdigitW( *s )) s++;
3249 time->wDay = strtolW( s, &end, 10 );
3250 s = end;
3252 while (*s && !isalphaW( *s )) s++;
3253 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3254 time->wMonth = 0;
3256 for (i = 0; i < 12; i++)
3258 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3259 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3260 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3262 time->wMonth = i + 1;
3263 break;
3266 if (time->wMonth == 0) return TRUE;
3268 while (*s && !isdigitW( *s )) s++;
3269 if (*s == '\0') return TRUE;
3270 time->wYear = strtolW( s, &end, 10 );
3271 s = end;
3273 while (*s && !isdigitW( *s )) s++;
3274 if (*s == '\0') return TRUE;
3275 time->wHour = strtolW( s, &end, 10 );
3276 s = end;
3278 while (*s && !isdigitW( *s )) s++;
3279 if (*s == '\0') return TRUE;
3280 time->wMinute = strtolW( s, &end, 10 );
3281 s = end;
3283 while (*s && !isdigitW( *s )) s++;
3284 if (*s == '\0') return TRUE;
3285 time->wSecond = strtolW( s, &end, 10 );
3286 s = end;
3288 time->wMilliseconds = 0;
3289 return TRUE;
3292 /***********************************************************************
3293 * InternetCheckConnectionW (WININET.@)
3295 * Pings a requested host to check internet connection
3297 * RETURNS
3298 * TRUE on success and FALSE on failure. If a failure then
3299 * ERROR_NOT_CONNECTED is placed into GetLastError
3302 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3305 * this is a kludge which runs the resident ping program and reads the output.
3307 * Anyone have a better idea?
3310 BOOL rc = FALSE;
3311 static const CHAR ping[] = "ping -c 1 ";
3312 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3313 WCHAR *host;
3314 DWORD len, host_len;
3315 INTERNET_PORT port;
3316 int status = -1;
3318 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3321 * Crack or set the Address
3323 if (lpszUrl == NULL)
3326 * According to the doc we are supposed to use the ip for the next
3327 * server in the WnInet internal server database. I have
3328 * no idea what that is or how to get it.
3330 * So someone needs to implement this.
3332 FIXME("Unimplemented with URL of NULL\n");
3333 return TRUE;
3335 else
3337 URL_COMPONENTSW components = {sizeof(components)};
3339 components.dwHostNameLength = 1;
3341 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3342 goto End;
3344 host = components.lpszHostName;
3345 host_len = components.dwHostNameLength;
3346 port = components.nPort;
3347 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3350 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3352 struct sockaddr_storage saddr;
3353 int sa_len = sizeof(saddr);
3354 WCHAR *host_z;
3355 int fd;
3356 BOOL b;
3358 host_z = heap_strndupW(host, host_len);
3359 if (!host_z)
3360 return FALSE;
3362 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3363 heap_free(host_z);
3364 if(!b)
3365 goto End;
3366 init_winsock();
3367 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3368 if (fd != -1)
3370 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3371 rc = TRUE;
3372 closesocket(fd);
3375 else
3378 * Build our ping command
3380 char *command;
3382 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3383 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3384 strcpy(command, ping);
3385 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3386 strcpy(command+sizeof(ping)-1+len, redirect);
3388 TRACE("Ping command is : %s\n",command);
3390 status = system(command);
3391 heap_free( command );
3393 TRACE("Ping returned a code of %i\n",status);
3395 /* Ping return code of 0 indicates success */
3396 if (status == 0)
3397 rc = TRUE;
3400 End:
3401 if (rc == FALSE)
3402 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3404 return rc;
3408 /***********************************************************************
3409 * InternetCheckConnectionA (WININET.@)
3411 * Pings a requested host to check internet connection
3413 * RETURNS
3414 * TRUE on success and FALSE on failure. If a failure then
3415 * ERROR_NOT_CONNECTED is placed into GetLastError
3418 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3420 WCHAR *url = NULL;
3421 BOOL rc;
3423 if(lpszUrl) {
3424 url = heap_strdupAtoW(lpszUrl);
3425 if(!url)
3426 return FALSE;
3429 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3431 heap_free(url);
3432 return rc;
3436 /**********************************************************
3437 * INTERNET_InternetOpenUrlW (internal)
3439 * Opens an URL
3441 * RETURNS
3442 * handle of connection or NULL on failure
3444 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3445 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3447 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3448 WCHAR *host, *user = NULL, *pass = NULL, *path;
3449 HINTERNET client = NULL, client1 = NULL;
3450 DWORD res;
3452 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3453 dwHeadersLength, dwFlags, dwContext);
3455 urlComponents.dwHostNameLength = 1;
3456 urlComponents.dwUserNameLength = 1;
3457 urlComponents.dwPasswordLength = 1;
3458 urlComponents.dwUrlPathLength = 1;
3459 urlComponents.dwExtraInfoLength = 1;
3460 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3461 return NULL;
3463 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
3464 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;