gdi32: Reimplement Ellipse in paths to avoid calling imprecise arc helper functions.
[wine.git] / dlls / wininet / internet.c
blobd3d6a38657bf7a9682f17920a2b6d4c024dcf726
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
31 #ifdef HAVE_CORESERVICES_CORESERVICES_H
32 #define GetCurrentThread MacGetCurrentThread
33 #define LoadResource MacLoadResource
34 #include <CoreServices/CoreServices.h>
35 #undef GetCurrentThread
36 #undef LoadResource
37 #undef DPRINTF
38 #endif
40 #include "winsock2.h"
41 #include "ws2ipdef.h"
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <assert.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winuser.h"
54 #include "wininet.h"
55 #include "winnls.h"
56 #include "wine/debug.h"
57 #include "winerror.h"
58 #define NO_SHLWAPI_STREAM
59 #include "shlwapi.h"
61 #include "wine/exception.h"
63 #include "internet.h"
64 #include "resource.h"
66 #include "wine/unicode.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
70 typedef struct
72 DWORD dwError;
73 CHAR response[MAX_REPLY_LEN];
74 } WITHREADERROR, *LPWITHREADERROR;
76 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
77 HMODULE WININET_hModule;
79 static CRITICAL_SECTION WININET_cs;
80 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
82 0, 0, &WININET_cs,
83 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
84 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
86 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
88 static object_header_t **handle_table;
89 static UINT_PTR next_handle;
90 static UINT_PTR handle_table_size;
92 typedef struct
94 DWORD proxyEnabled;
95 LPWSTR proxy;
96 LPWSTR proxyBypass;
97 LPWSTR proxyUsername;
98 LPWSTR proxyPassword;
99 } proxyinfo_t;
101 static ULONG max_conns = 2, max_1_0_conns = 4;
102 static ULONG connect_timeout = 60000;
104 static const WCHAR szInternetSettings[] =
105 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
106 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
107 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
108 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
109 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
110 static const WCHAR szProxyOverride[] = { 'P','r','o','x','y','O','v','e','r','r','i','d','e', 0 };
112 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
114 UINT_PTR handle = 0, num;
115 object_header_t *ret;
116 object_header_t **p;
117 BOOL res = TRUE;
119 ret = heap_alloc_zero(size);
120 if(!ret)
121 return NULL;
123 list_init(&ret->children);
125 EnterCriticalSection( &WININET_cs );
127 if(!handle_table_size) {
128 num = 16;
129 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
130 if(p) {
131 handle_table = p;
132 handle_table_size = num;
133 next_handle = 1;
134 }else {
135 res = FALSE;
137 }else if(next_handle == handle_table_size) {
138 num = handle_table_size * 2;
139 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
140 if(p) {
141 handle_table = p;
142 handle_table_size = num;
143 }else {
144 res = FALSE;
148 if(res) {
149 handle = next_handle;
150 if(handle_table[handle])
151 ERR("handle isn't free but should be\n");
152 handle_table[handle] = ret;
153 ret->valid_handle = TRUE;
155 while(next_handle < handle_table_size && handle_table[next_handle])
156 next_handle++;
159 LeaveCriticalSection( &WININET_cs );
161 if(!res) {
162 heap_free(ret);
163 return NULL;
166 ret->vtbl = vtbl;
167 ret->refs = 1;
168 ret->hInternet = (HINTERNET)handle;
170 if(parent) {
171 ret->lpfnStatusCB = parent->lpfnStatusCB;
172 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
175 return ret;
178 object_header_t *WININET_AddRef( object_header_t *info )
180 ULONG refs = InterlockedIncrement(&info->refs);
181 TRACE("%p -> refcount = %d\n", info, refs );
182 return info;
185 object_header_t *get_handle_object( HINTERNET hinternet )
187 object_header_t *info = NULL;
188 UINT_PTR handle = (UINT_PTR) hinternet;
190 EnterCriticalSection( &WININET_cs );
192 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
193 info = WININET_AddRef(handle_table[handle]);
195 LeaveCriticalSection( &WININET_cs );
197 TRACE("handle %ld -> %p\n", handle, info);
199 return info;
202 static void invalidate_handle(object_header_t *info)
204 object_header_t *child, *next;
206 if(!info->valid_handle)
207 return;
208 info->valid_handle = FALSE;
210 /* Free all children as native does */
211 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
213 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
214 invalidate_handle( child );
217 WININET_Release(info);
220 BOOL WININET_Release( object_header_t *info )
222 ULONG refs = InterlockedDecrement(&info->refs);
223 TRACE( "object %p refcount = %d\n", info, refs );
224 if( !refs )
226 invalidate_handle(info);
227 if ( info->vtbl->CloseConnection )
229 TRACE( "closing connection %p\n", info);
230 info->vtbl->CloseConnection( info );
232 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
233 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
234 || !(info->dwInternalFlags & INET_OPENURL))
236 INTERNET_SendCallback(info, info->dwContext,
237 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
238 sizeof(HINTERNET));
240 TRACE( "destroying object %p\n", info);
241 if ( info->htype != WH_HINIT )
242 list_remove( &info->entry );
243 info->vtbl->Destroy( info );
245 if(info->hInternet) {
246 UINT_PTR handle = (UINT_PTR)info->hInternet;
248 EnterCriticalSection( &WININET_cs );
250 handle_table[handle] = NULL;
251 if(next_handle > handle)
252 next_handle = handle;
254 LeaveCriticalSection( &WININET_cs );
257 heap_free(info);
259 return TRUE;
262 /***********************************************************************
263 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
265 * PARAMS
266 * hinstDLL [I] handle to the DLL's instance
267 * fdwReason [I]
268 * lpvReserved [I] reserved, must be NULL
270 * RETURNS
271 * Success: TRUE
272 * Failure: FALSE
275 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
277 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
279 switch (fdwReason) {
280 case DLL_PROCESS_ATTACH:
282 g_dwTlsErrIndex = TlsAlloc();
284 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
285 return FALSE;
287 if(!init_urlcache())
289 TlsFree(g_dwTlsErrIndex);
290 return FALSE;
293 WININET_hModule = hinstDLL;
294 break;
296 case DLL_THREAD_ATTACH:
297 break;
299 case DLL_THREAD_DETACH:
300 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
302 heap_free(TlsGetValue(g_dwTlsErrIndex));
304 break;
306 case DLL_PROCESS_DETACH:
307 if (lpvReserved) break;
308 collect_connections(COLLECT_CLEANUP);
309 NETCON_unload();
310 free_urlcache();
311 free_cookie();
313 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
315 heap_free(TlsGetValue(g_dwTlsErrIndex));
316 TlsFree(g_dwTlsErrIndex);
318 break;
320 return TRUE;
323 /***********************************************************************
324 * DllInstall (WININET.@)
326 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
328 FIXME("(%x %s): stub\n", bInstall, debugstr_w(cmdline));
329 return S_OK;
332 /***********************************************************************
333 * INTERNET_SaveProxySettings
335 * Stores the proxy settings given by lpwai into the registry
337 * RETURNS
338 * ERROR_SUCCESS if no error, or error code on fail
340 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
342 HKEY key;
343 LONG ret;
345 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
346 return ret;
348 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
350 RegCloseKey( key );
351 return ret;
354 if (lpwpi->proxy)
356 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
358 RegCloseKey( key );
359 return ret;
362 else
364 if ((ret = RegDeleteValueW( key, szProxyServer )) && ret != ERROR_FILE_NOT_FOUND)
366 RegCloseKey( key );
367 return ret;
371 RegCloseKey(key);
372 return ERROR_SUCCESS;
375 /***********************************************************************
376 * INTERNET_FindProxyForProtocol
378 * Searches the proxy string for a proxy of the given protocol.
379 * Returns the found proxy, or the default proxy if none of the given
380 * protocol is found.
382 * PARAMETERS
383 * szProxy [In] proxy string to search
384 * proto [In] protocol to search for, e.g. "http"
385 * foundProxy [Out] found proxy
386 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
388 * RETURNS
389 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
390 * *foundProxyLen is set to the required size in WCHARs, including the
391 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
393 WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto)
395 WCHAR *ret = NULL;
396 const WCHAR *ptr;
398 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
400 /* First, look for the specified protocol (proto=scheme://host:port) */
401 for (ptr = szProxy; ptr && *ptr; )
403 LPCWSTR end, equal;
405 if (!(end = strchrW(ptr, ' ')))
406 end = ptr + strlenW(ptr);
407 if ((equal = strchrW(ptr, '=')) && equal < end &&
408 equal - ptr == strlenW(proto) &&
409 !strncmpiW(proto, ptr, strlenW(proto)))
411 ret = heap_strndupW(equal + 1, end - equal - 1);
412 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
413 return ret;
415 if (*end == ' ')
416 ptr = end + 1;
417 else
418 ptr = end;
421 /* It wasn't found: look for no protocol */
422 for (ptr = szProxy; ptr && *ptr; )
424 LPCWSTR end;
426 if (!(end = strchrW(ptr, ' ')))
427 end = ptr + strlenW(ptr);
428 if (!strchrW(ptr, '='))
430 ret = heap_strndupW(ptr, end - ptr);
431 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
432 return ret;
434 if (*end == ' ')
435 ptr = end + 1;
436 else
437 ptr = end;
440 return NULL;
443 /***********************************************************************
444 * InternetInitializeAutoProxyDll (WININET.@)
446 * Setup the internal proxy
448 * PARAMETERS
449 * dwReserved
451 * RETURNS
452 * FALSE on failure
455 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
457 FIXME("STUB\n");
458 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
459 return FALSE;
462 /***********************************************************************
463 * DetectAutoProxyUrl (WININET.@)
465 * Auto detect the proxy url
467 * RETURNS
468 * FALSE on failure
471 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
472 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
474 FIXME("STUB\n");
475 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
476 return FALSE;
479 static void FreeProxyInfo( proxyinfo_t *lpwpi )
481 heap_free(lpwpi->proxy);
482 heap_free(lpwpi->proxyBypass);
483 heap_free(lpwpi->proxyUsername);
484 heap_free(lpwpi->proxyPassword);
487 static proxyinfo_t *global_proxy;
489 static void free_global_proxy( void )
491 EnterCriticalSection( &WININET_cs );
492 if (global_proxy)
494 FreeProxyInfo( global_proxy );
495 heap_free( global_proxy );
497 LeaveCriticalSection( &WININET_cs );
500 static BOOL parse_proxy_url( proxyinfo_t *info, const WCHAR *url )
502 static const WCHAR fmt[] = {'%','.','*','s',':','%','u',0};
503 URL_COMPONENTSW uc = {sizeof(uc)};
505 uc.dwHostNameLength = 1;
506 uc.dwUserNameLength = 1;
507 uc.dwPasswordLength = 1;
509 if (!InternetCrackUrlW( url, 0, 0, &uc )) return FALSE;
510 if (!uc.dwHostNameLength)
512 if (!(info->proxy = heap_strdupW( url ))) return FALSE;
513 info->proxyUsername = NULL;
514 info->proxyPassword = NULL;
515 return TRUE;
517 if (!(info->proxy = heap_alloc( (uc.dwHostNameLength + 12) * sizeof(WCHAR) ))) return FALSE;
518 sprintfW( info->proxy, fmt, uc.dwHostNameLength, uc.lpszHostName, uc.nPort );
520 if (!uc.dwUserNameLength) info->proxyUsername = NULL;
521 else if (!(info->proxyUsername = heap_strndupW( uc.lpszUserName, uc.dwUserNameLength )))
523 heap_free( info->proxy );
524 return FALSE;
526 if (!uc.dwPasswordLength) info->proxyPassword = NULL;
527 else if (!(info->proxyPassword = heap_strndupW( uc.lpszPassword, uc.dwPasswordLength )))
529 heap_free( info->proxyUsername );
530 heap_free( info->proxy );
531 return FALSE;
533 return TRUE;
536 /***********************************************************************
537 * INTERNET_LoadProxySettings
539 * Loads proxy information from process-wide global settings, the registry,
540 * or the environment into lpwpi.
542 * The caller should call FreeProxyInfo when done with lpwpi.
544 * FIXME:
545 * The proxy may be specified in the form 'http=proxy.my.org'
546 * Presumably that means there can be ftp=ftpproxy.my.org too.
548 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
550 HKEY key;
551 DWORD type, len;
552 LPCSTR envproxy;
553 LONG ret;
555 memset( lpwpi, 0, sizeof(*lpwpi) );
557 EnterCriticalSection( &WININET_cs );
558 if (global_proxy)
560 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
561 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
562 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
564 LeaveCriticalSection( &WININET_cs );
566 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
568 FreeProxyInfo( lpwpi );
569 return ret;
572 len = sizeof(DWORD);
573 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
575 lpwpi->proxyEnabled = 0;
576 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
578 FreeProxyInfo( lpwpi );
579 RegCloseKey( key );
580 return ret;
584 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
586 /* figure out how much memory the proxy setting takes */
587 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
589 LPWSTR szProxy, p;
590 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
592 if (!(szProxy = heap_alloc(len)))
594 RegCloseKey( key );
595 FreeProxyInfo( lpwpi );
596 return ERROR_OUTOFMEMORY;
598 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
600 /* find the http proxy, and strip away everything else */
601 p = strstrW( szProxy, szHttp );
602 if (p)
604 p += lstrlenW( szHttp );
605 lstrcpyW( szProxy, p );
607 p = strchrW( szProxy, ';' );
608 if (p) *p = 0;
610 FreeProxyInfo( lpwpi );
611 lpwpi->proxy = szProxy;
612 lpwpi->proxyBypass = NULL;
614 TRACE("http proxy (from registry) = %s\n", debugstr_w(lpwpi->proxy));
616 else
618 TRACE("No proxy server settings in registry.\n");
619 FreeProxyInfo( lpwpi );
620 lpwpi->proxy = NULL;
621 lpwpi->proxyBypass = NULL;
624 else if (envproxy)
626 WCHAR *envproxyW;
628 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
629 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
631 RegCloseKey( key );
632 return ERROR_OUTOFMEMORY;
634 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
636 FreeProxyInfo( lpwpi );
637 if (parse_proxy_url( lpwpi, envproxyW ))
639 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
640 lpwpi->proxyEnabled = 1;
641 lpwpi->proxyBypass = NULL;
643 else
645 WARN("failed to parse http_proxy value %s\n", debugstr_w(envproxyW));
646 lpwpi->proxyEnabled = 0;
647 lpwpi->proxy = NULL;
648 lpwpi->proxyBypass = NULL;
650 heap_free( envproxyW );
653 if (lpwpi->proxyEnabled)
655 TRACE("Proxy is enabled.\n");
657 if (!(envproxy = getenv( "no_proxy" )))
659 /* figure out how much memory the proxy setting takes */
660 if (!RegQueryValueExW( key, szProxyOverride, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
662 LPWSTR szProxy;
664 if (!(szProxy = heap_alloc(len)))
666 RegCloseKey( key );
667 return ERROR_OUTOFMEMORY;
669 RegQueryValueExW( key, szProxyOverride, NULL, &type, (BYTE*)szProxy, &len );
671 heap_free( lpwpi->proxyBypass );
672 lpwpi->proxyBypass = szProxy;
674 TRACE("http proxy bypass (from registry) = %s\n", debugstr_w(lpwpi->proxyBypass));
676 else
678 heap_free( lpwpi->proxyBypass );
679 lpwpi->proxyBypass = NULL;
681 TRACE("No proxy bypass server settings in registry.\n");
684 else
686 WCHAR *envproxyW;
688 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
689 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
691 RegCloseKey( key );
692 return ERROR_OUTOFMEMORY;
694 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
696 heap_free( lpwpi->proxyBypass );
697 lpwpi->proxyBypass = envproxyW;
699 TRACE("http proxy bypass (from environment) = %s\n", debugstr_w(lpwpi->proxyBypass));
702 else TRACE("Proxy is disabled.\n");
704 RegCloseKey( key );
705 return ERROR_SUCCESS;
708 /***********************************************************************
709 * INTERNET_ConfigureProxy
711 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
713 proxyinfo_t wpi;
715 if (INTERNET_LoadProxySettings( &wpi ))
716 return FALSE;
718 if (wpi.proxyEnabled)
720 TRACE("http proxy = %s bypass = %s\n", debugstr_w(wpi.proxy), debugstr_w(wpi.proxyBypass));
722 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
723 lpwai->proxy = wpi.proxy;
724 lpwai->proxyBypass = wpi.proxyBypass;
725 lpwai->proxyUsername = wpi.proxyUsername;
726 lpwai->proxyPassword = wpi.proxyPassword;
727 return TRUE;
730 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
731 FreeProxyInfo(&wpi);
732 return FALSE;
735 /***********************************************************************
736 * dump_INTERNET_FLAGS
738 * Helper function to TRACE the internet flags.
740 * RETURNS
741 * None
744 static void dump_INTERNET_FLAGS(DWORD dwFlags)
746 #define FE(x) { x, #x }
747 static const wininet_flag_info flag[] = {
748 FE(INTERNET_FLAG_RELOAD),
749 FE(INTERNET_FLAG_RAW_DATA),
750 FE(INTERNET_FLAG_EXISTING_CONNECT),
751 FE(INTERNET_FLAG_ASYNC),
752 FE(INTERNET_FLAG_PASSIVE),
753 FE(INTERNET_FLAG_NO_CACHE_WRITE),
754 FE(INTERNET_FLAG_MAKE_PERSISTENT),
755 FE(INTERNET_FLAG_FROM_CACHE),
756 FE(INTERNET_FLAG_SECURE),
757 FE(INTERNET_FLAG_KEEP_CONNECTION),
758 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
759 FE(INTERNET_FLAG_READ_PREFETCH),
760 FE(INTERNET_FLAG_NO_COOKIES),
761 FE(INTERNET_FLAG_NO_AUTH),
762 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
763 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
764 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
765 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
766 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
767 FE(INTERNET_FLAG_RESYNCHRONIZE),
768 FE(INTERNET_FLAG_HYPERLINK),
769 FE(INTERNET_FLAG_NO_UI),
770 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
771 FE(INTERNET_FLAG_CACHE_ASYNC),
772 FE(INTERNET_FLAG_FORMS_SUBMIT),
773 FE(INTERNET_FLAG_NEED_FILE),
774 FE(INTERNET_FLAG_TRANSFER_ASCII),
775 FE(INTERNET_FLAG_TRANSFER_BINARY)
777 #undef FE
778 unsigned int i;
780 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
781 if (flag[i].val & dwFlags) {
782 TRACE(" %s", flag[i].name);
783 dwFlags &= ~flag[i].val;
786 if (dwFlags)
787 TRACE(" Unknown flags (%08x)\n", dwFlags);
788 else
789 TRACE("\n");
792 /***********************************************************************
793 * INTERNET_CloseHandle (internal)
795 * Close internet handle
798 static VOID APPINFO_Destroy(object_header_t *hdr)
800 appinfo_t *lpwai = (appinfo_t*)hdr;
802 TRACE("%p\n",lpwai);
804 heap_free(lpwai->agent);
805 heap_free(lpwai->proxy);
806 heap_free(lpwai->proxyBypass);
807 heap_free(lpwai->proxyUsername);
808 heap_free(lpwai->proxyPassword);
811 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
813 appinfo_t *ai = (appinfo_t*)hdr;
815 switch(option) {
816 case INTERNET_OPTION_HANDLE_TYPE:
817 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
819 if (*size < sizeof(ULONG))
820 return ERROR_INSUFFICIENT_BUFFER;
822 *size = sizeof(DWORD);
823 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
824 return ERROR_SUCCESS;
826 case INTERNET_OPTION_USER_AGENT: {
827 DWORD bufsize;
829 TRACE("INTERNET_OPTION_USER_AGENT\n");
831 bufsize = *size;
833 if (unicode) {
834 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
836 *size = (len + 1) * sizeof(WCHAR);
837 if(!buffer || bufsize < *size)
838 return ERROR_INSUFFICIENT_BUFFER;
840 if (ai->agent)
841 strcpyW(buffer, ai->agent);
842 else
843 *(WCHAR *)buffer = 0;
844 /* If the buffer is copied, the returned length doesn't include
845 * the NULL terminator.
847 *size = len;
848 }else {
849 if (ai->agent)
850 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
851 else
852 *size = 1;
853 if(!buffer || bufsize < *size)
854 return ERROR_INSUFFICIENT_BUFFER;
856 if (ai->agent)
857 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
858 else
859 *(char *)buffer = 0;
860 /* If the buffer is copied, the returned length doesn't include
861 * the NULL terminator.
863 *size -= 1;
866 return ERROR_SUCCESS;
869 case INTERNET_OPTION_PROXY:
870 if(!size) return ERROR_INVALID_PARAMETER;
871 if (unicode) {
872 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
873 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
874 LPWSTR proxy, proxy_bypass;
876 if (ai->proxy)
877 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
878 if (ai->proxyBypass)
879 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
880 if (!pi || *size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
882 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
883 return ERROR_INSUFFICIENT_BUFFER;
885 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
886 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
888 pi->dwAccessType = ai->accessType;
889 pi->lpszProxy = NULL;
890 pi->lpszProxyBypass = NULL;
891 if (ai->proxy) {
892 lstrcpyW(proxy, ai->proxy);
893 pi->lpszProxy = proxy;
896 if (ai->proxyBypass) {
897 lstrcpyW(proxy_bypass, ai->proxyBypass);
898 pi->lpszProxyBypass = proxy_bypass;
901 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
902 return ERROR_SUCCESS;
903 }else {
904 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
905 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
906 LPSTR proxy, proxy_bypass;
908 if (ai->proxy)
909 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
910 if (ai->proxyBypass)
911 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
912 NULL, 0, NULL, NULL);
913 if (!pi || *size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
915 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
916 return ERROR_INSUFFICIENT_BUFFER;
918 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
919 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
921 pi->dwAccessType = ai->accessType;
922 pi->lpszProxy = NULL;
923 pi->lpszProxyBypass = NULL;
924 if (ai->proxy) {
925 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
926 pi->lpszProxy = proxy;
929 if (ai->proxyBypass) {
930 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
931 proxyBypassBytesRequired, NULL, NULL);
932 pi->lpszProxyBypass = proxy_bypass;
935 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
936 return ERROR_SUCCESS;
939 case INTERNET_OPTION_CONNECT_TIMEOUT:
940 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
942 if (*size < sizeof(ULONG))
943 return ERROR_INSUFFICIENT_BUFFER;
945 *(ULONG*)buffer = ai->connect_timeout;
946 *size = sizeof(ULONG);
948 return ERROR_SUCCESS;
951 return INET_QueryOption(hdr, option, buffer, size, unicode);
954 static DWORD APPINFO_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
956 appinfo_t *ai = (appinfo_t*)hdr;
958 switch(option) {
959 case INTERNET_OPTION_CONNECT_TIMEOUT:
960 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
962 if(size != sizeof(connect_timeout))
963 return ERROR_INTERNET_BAD_OPTION_LENGTH;
964 if(!*(ULONG*)buf)
965 return ERROR_BAD_ARGUMENTS;
967 ai->connect_timeout = *(ULONG*)buf;
968 return ERROR_SUCCESS;
969 case INTERNET_OPTION_USER_AGENT:
970 heap_free(ai->agent);
971 if (!(ai->agent = heap_strdupW(buf))) return ERROR_OUTOFMEMORY;
972 return ERROR_SUCCESS;
975 return INET_SetOption(hdr, option, buf, size);
978 static const object_vtbl_t APPINFOVtbl = {
979 APPINFO_Destroy,
980 NULL,
981 APPINFO_QueryOption,
982 APPINFO_SetOption,
983 NULL,
984 NULL,
985 NULL,
986 NULL
990 /***********************************************************************
991 * InternetOpenW (WININET.@)
993 * Per-application initialization of wininet
995 * RETURNS
996 * HINTERNET on success
997 * NULL on failure
1000 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
1001 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
1003 appinfo_t *lpwai = NULL;
1005 if (TRACE_ON(wininet)) {
1006 #define FE(x) { x, #x }
1007 static const wininet_flag_info access_type[] = {
1008 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1009 FE(INTERNET_OPEN_TYPE_DIRECT),
1010 FE(INTERNET_OPEN_TYPE_PROXY),
1011 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1013 #undef FE
1014 DWORD i;
1015 const char *access_type_str = "Unknown";
1017 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1018 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1019 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1020 if (access_type[i].val == dwAccessType) {
1021 access_type_str = access_type[i].name;
1022 break;
1025 TRACE(" access type : %s\n", access_type_str);
1026 TRACE(" flags :");
1027 dump_INTERNET_FLAGS(dwFlags);
1030 /* Clear any error information */
1031 INTERNET_SetLastError(0);
1033 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1034 SetLastError(ERROR_INVALID_PARAMETER);
1035 return NULL;
1038 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1039 if (!lpwai) {
1040 SetLastError(ERROR_OUTOFMEMORY);
1041 return NULL;
1044 lpwai->hdr.htype = WH_HINIT;
1045 lpwai->hdr.dwFlags = dwFlags;
1046 lpwai->accessType = dwAccessType;
1047 lpwai->proxyUsername = NULL;
1048 lpwai->proxyPassword = NULL;
1049 lpwai->connect_timeout = connect_timeout;
1051 lpwai->agent = heap_strdupW(lpszAgent);
1052 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1053 INTERNET_ConfigureProxy( lpwai );
1054 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1055 lpwai->proxy = heap_strdupW(lpszProxy);
1056 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1059 TRACE("returning %p\n", lpwai);
1061 return lpwai->hdr.hInternet;
1065 /***********************************************************************
1066 * InternetOpenA (WININET.@)
1068 * Per-application initialization of wininet
1070 * RETURNS
1071 * HINTERNET on success
1072 * NULL on failure
1075 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1076 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1078 WCHAR *szAgent, *szProxy, *szBypass;
1079 HINTERNET rc;
1081 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1082 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1084 szAgent = heap_strdupAtoW(lpszAgent);
1085 szProxy = heap_strdupAtoW(lpszProxy);
1086 szBypass = heap_strdupAtoW(lpszProxyBypass);
1088 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1090 heap_free(szAgent);
1091 heap_free(szProxy);
1092 heap_free(szBypass);
1093 return rc;
1096 /***********************************************************************
1097 * InternetGetLastResponseInfoA (WININET.@)
1099 * Return last wininet error description on the calling thread
1101 * RETURNS
1102 * TRUE on success of writing to buffer
1103 * FALSE on failure
1106 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1107 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1109 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1111 TRACE("\n");
1113 if (lpwite)
1115 *lpdwError = lpwite->dwError;
1116 if (lpwite->dwError)
1118 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1119 *lpdwBufferLength = strlen(lpszBuffer);
1121 else
1122 *lpdwBufferLength = 0;
1124 else
1126 *lpdwError = 0;
1127 *lpdwBufferLength = 0;
1130 return TRUE;
1133 /***********************************************************************
1134 * InternetGetLastResponseInfoW (WININET.@)
1136 * Return last wininet error description on the calling thread
1138 * RETURNS
1139 * TRUE on success of writing to buffer
1140 * FALSE on failure
1143 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1144 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1146 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1148 TRACE("\n");
1150 if (lpwite)
1152 *lpdwError = lpwite->dwError;
1153 if (lpwite->dwError)
1155 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1156 *lpdwBufferLength = lstrlenW(lpszBuffer);
1158 else
1159 *lpdwBufferLength = 0;
1161 else
1163 *lpdwError = 0;
1164 *lpdwBufferLength = 0;
1167 return TRUE;
1170 /***********************************************************************
1171 * InternetGetConnectedState (WININET.@)
1173 * Return connected state
1175 * RETURNS
1176 * TRUE if connected
1177 * if lpdwStatus is not null, return the status (off line,
1178 * modem, lan...) in it.
1179 * FALSE if not connected
1181 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1183 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1185 if (lpdwStatus) {
1186 WARN("always returning LAN connection.\n");
1187 *lpdwStatus = INTERNET_CONNECTION_LAN;
1189 return TRUE;
1193 /***********************************************************************
1194 * InternetGetConnectedStateExW (WININET.@)
1196 * Return connected state
1198 * PARAMS
1200 * lpdwStatus [O] Flags specifying the status of the internet connection.
1201 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1202 * dwNameLen [I] Size of the buffer, in characters.
1203 * dwReserved [I] Reserved. Must be set to 0.
1205 * RETURNS
1206 * TRUE if connected
1207 * if lpdwStatus is not null, return the status (off line,
1208 * modem, lan...) in it.
1209 * FALSE if not connected
1211 * NOTES
1212 * If the system has no available network connections, an empty string is
1213 * stored in lpszConnectionName. If there is a LAN connection, a localized
1214 * "LAN Connection" string is stored. Presumably, if only a dial-up
1215 * connection is available then the name of the dial-up connection is
1216 * returned. Why any application, other than the "Internet Settings" CPL,
1217 * would want to use this function instead of the simpler InternetGetConnectedStateW
1218 * function is beyond me.
1220 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1221 DWORD dwNameLen, DWORD dwReserved)
1223 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1225 /* Must be zero */
1226 if(dwReserved)
1227 return FALSE;
1229 if (lpdwStatus) {
1230 WARN("always returning LAN connection.\n");
1231 *lpdwStatus = INTERNET_CONNECTION_LAN;
1234 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1235 * the resource, avoid it as we must not change the buffer in this case */
1236 if(lpszConnectionName && dwNameLen) {
1237 *lpszConnectionName = '\0';
1238 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1241 return TRUE;
1245 /***********************************************************************
1246 * InternetGetConnectedStateExA (WININET.@)
1248 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1249 DWORD dwNameLen, DWORD dwReserved)
1251 LPWSTR lpwszConnectionName = NULL;
1252 BOOL rc;
1254 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1256 if (lpszConnectionName && dwNameLen > 0)
1257 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1259 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1260 dwReserved);
1261 if (rc && lpwszConnectionName)
1262 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1263 dwNameLen, NULL, NULL);
1265 heap_free(lpwszConnectionName);
1266 return rc;
1270 /***********************************************************************
1271 * InternetConnectW (WININET.@)
1273 * Open a ftp, gopher or http session
1275 * RETURNS
1276 * HINTERNET a session handle on success
1277 * NULL on failure
1280 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1281 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1282 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1283 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1285 appinfo_t *hIC;
1286 HINTERNET rc = NULL;
1287 DWORD res = ERROR_SUCCESS;
1289 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1290 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1292 if (!lpszServerName)
1294 SetLastError(ERROR_INVALID_PARAMETER);
1295 return NULL;
1298 hIC = (appinfo_t*)get_handle_object( hInternet );
1299 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1301 res = ERROR_INVALID_HANDLE;
1302 goto lend;
1305 switch (dwService)
1307 case INTERNET_SERVICE_FTP:
1308 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1309 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1310 if(!rc)
1311 res = INTERNET_GetLastError();
1312 break;
1314 case INTERNET_SERVICE_HTTP:
1315 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1316 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1317 break;
1319 case INTERNET_SERVICE_GOPHER:
1320 default:
1321 break;
1323 lend:
1324 if( hIC )
1325 WININET_Release( &hIC->hdr );
1327 TRACE("returning %p\n", rc);
1328 SetLastError(res);
1329 return rc;
1333 /***********************************************************************
1334 * InternetConnectA (WININET.@)
1336 * Open a ftp, gopher or http session
1338 * RETURNS
1339 * HINTERNET a session handle on success
1340 * NULL on failure
1343 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1344 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1345 LPCSTR lpszUserName, LPCSTR lpszPassword,
1346 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1348 HINTERNET rc = NULL;
1349 LPWSTR szServerName;
1350 LPWSTR szUserName;
1351 LPWSTR szPassword;
1353 szServerName = heap_strdupAtoW(lpszServerName);
1354 szUserName = heap_strdupAtoW(lpszUserName);
1355 szPassword = heap_strdupAtoW(lpszPassword);
1357 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1358 szUserName, szPassword, dwService, dwFlags, dwContext);
1360 heap_free(szServerName);
1361 heap_free(szUserName);
1362 heap_free(szPassword);
1363 return rc;
1367 /***********************************************************************
1368 * InternetFindNextFileA (WININET.@)
1370 * Continues a file search from a previous call to FindFirstFile
1372 * RETURNS
1373 * TRUE on success
1374 * FALSE on failure
1377 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1379 BOOL ret;
1380 WIN32_FIND_DATAW fd;
1382 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1383 if(lpvFindData)
1384 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1385 return ret;
1388 /***********************************************************************
1389 * InternetFindNextFileW (WININET.@)
1391 * Continues a file search from a previous call to FindFirstFile
1393 * RETURNS
1394 * TRUE on success
1395 * FALSE on failure
1398 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1400 object_header_t *hdr;
1401 DWORD res;
1403 TRACE("\n");
1405 hdr = get_handle_object(hFind);
1406 if(!hdr) {
1407 WARN("Invalid handle\n");
1408 SetLastError(ERROR_INVALID_HANDLE);
1409 return FALSE;
1412 if(hdr->vtbl->FindNextFileW) {
1413 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1414 }else {
1415 WARN("Handle doesn't support NextFile\n");
1416 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1419 WININET_Release(hdr);
1421 if(res != ERROR_SUCCESS)
1422 SetLastError(res);
1423 return res == ERROR_SUCCESS;
1426 /***********************************************************************
1427 * InternetCloseHandle (WININET.@)
1429 * Generic close handle function
1431 * RETURNS
1432 * TRUE on success
1433 * FALSE on failure
1436 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1438 object_header_t *obj;
1440 TRACE("%p\n", hInternet);
1442 obj = get_handle_object( hInternet );
1443 if (!obj) {
1444 SetLastError(ERROR_INVALID_HANDLE);
1445 return FALSE;
1448 invalidate_handle(obj);
1449 WININET_Release(obj);
1451 return TRUE;
1454 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1456 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1458 if (!*component_length)
1459 return TRUE;
1461 if (!*component) {
1462 *(const WCHAR**)component = value;
1463 *component_length = len;
1464 return TRUE;
1467 if (*component_length < len+1) {
1468 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1469 return FALSE;
1472 *component_length = len;
1473 if(len)
1474 memcpy(*component, value, len*sizeof(WCHAR));
1475 (*component)[len] = 0;
1476 return TRUE;
1479 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1480 const char *url_a)
1482 size_t size, ret_size = *ret_length;
1484 if (!*ret_length)
1485 return TRUE;
1486 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1488 if (!*comp) {
1489 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1490 *ret_length = size;
1491 return TRUE;
1494 if (size+1 > ret_size) {
1495 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1496 *ret_length = size+1;
1497 return FALSE;
1500 *ret_length = size;
1501 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1502 (*comp)[size] = 0;
1503 return TRUE;
1506 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1508 *len_w = len_a;
1510 if(!comp_a) {
1511 *comp_w = NULL;
1512 return TRUE;
1515 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1516 SetLastError(ERROR_OUTOFMEMORY);
1517 return FALSE;
1520 return TRUE;
1523 /***********************************************************************
1524 * InternetCrackUrlA (WININET.@)
1526 * See InternetCrackUrlW.
1528 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1530 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1531 URL_COMPONENTSW comp;
1532 WCHAR *url_w = NULL;
1533 BOOL ret;
1535 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1537 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1538 SetLastError(ERROR_INVALID_PARAMETER);
1539 return FALSE;
1542 comp.dwStructSize = sizeof(comp);
1544 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1545 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1546 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1547 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1548 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1549 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1550 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1551 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1552 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1553 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1554 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1555 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1557 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1558 SetLastError(ERROR_OUTOFMEMORY);
1559 ret = FALSE;
1562 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1563 ret_comp->nScheme = comp.nScheme;
1564 ret_comp->nPort = comp.nPort;
1566 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1567 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1568 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1569 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1570 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1571 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1572 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1573 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1574 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1575 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1576 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1577 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1579 if(ret)
1580 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1581 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1582 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1583 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1584 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1587 heap_free(host);
1588 heap_free(user);
1589 heap_free(pass);
1590 heap_free(path);
1591 heap_free(scheme);
1592 heap_free(extra);
1593 heap_free(url_w);
1594 return ret;
1597 static const WCHAR url_schemes[][7] =
1599 {'f','t','p',0},
1600 {'g','o','p','h','e','r',0},
1601 {'h','t','t','p',0},
1602 {'h','t','t','p','s',0},
1603 {'f','i','l','e',0},
1604 {'n','e','w','s',0},
1605 {'m','a','i','l','t','o',0},
1606 {'r','e','s',0},
1609 /***********************************************************************
1610 * GetInternetSchemeW (internal)
1612 * Get scheme of url
1614 * RETURNS
1615 * scheme on success
1616 * INTERNET_SCHEME_UNKNOWN on failure
1619 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1621 int i;
1623 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1625 if(lpszScheme==NULL)
1626 return INTERNET_SCHEME_UNKNOWN;
1628 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1629 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1630 return INTERNET_SCHEME_FIRST + i;
1632 return INTERNET_SCHEME_UNKNOWN;
1635 /***********************************************************************
1636 * InternetCrackUrlW (WININET.@)
1638 * Break up URL into its components
1640 * RETURNS
1641 * TRUE on success
1642 * FALSE on failure
1644 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1647 * RFC 1808
1648 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1651 LPCWSTR lpszParam = NULL;
1652 BOOL found_colon = FALSE;
1653 LPCWSTR lpszap;
1654 LPCWSTR lpszcp = NULL, lpszNetLoc;
1656 TRACE("(%s %u %x %p)\n",
1657 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1658 dwUrlLength, dwFlags, lpUC);
1660 if (!lpszUrl || !*lpszUrl || !lpUC)
1662 SetLastError(ERROR_INVALID_PARAMETER);
1663 return FALSE;
1665 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1667 if (dwFlags & ICU_DECODE)
1669 WCHAR *url_tmp;
1670 DWORD len = dwUrlLength + 1;
1671 BOOL ret;
1673 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1675 SetLastError(ERROR_OUTOFMEMORY);
1676 return FALSE;
1678 ret = InternetCanonicalizeUrlW(url_tmp, url_tmp, &len, ICU_DECODE | ICU_NO_ENCODE);
1679 if (ret)
1680 ret = InternetCrackUrlW(url_tmp, len, dwFlags & ~ICU_DECODE, lpUC);
1681 heap_free(url_tmp);
1682 return ret;
1684 lpszap = lpszUrl;
1686 /* Determine if the URI is absolute. */
1687 while (lpszap - lpszUrl < dwUrlLength)
1689 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1691 lpszap++;
1692 continue;
1694 if (*lpszap == ':')
1696 found_colon = TRUE;
1697 lpszcp = lpszap;
1699 else
1701 lpszcp = lpszUrl; /* Relative url */
1704 break;
1707 if(!found_colon){
1708 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1709 return FALSE;
1712 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1713 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1715 /* Parse <params> */
1716 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1717 if(!lpszParam)
1718 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1720 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1721 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1722 return FALSE;
1725 /* Get scheme first. */
1726 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1727 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1728 return FALSE;
1730 /* Eat ':' in protocol. */
1731 lpszcp++;
1733 /* double slash indicates the net_loc portion is present */
1734 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1736 lpszcp += 2;
1738 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1739 if (lpszParam)
1741 if (lpszNetLoc)
1742 lpszNetLoc = min(lpszNetLoc, lpszParam);
1743 else
1744 lpszNetLoc = lpszParam;
1746 else if (!lpszNetLoc)
1747 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1749 /* Parse net-loc */
1750 if (lpszNetLoc)
1752 LPCWSTR lpszHost;
1753 LPCWSTR lpszPort;
1755 /* [<user>[<:password>]@]<host>[:<port>] */
1756 /* First find the user and password if they exist */
1758 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1759 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1761 /* username and password not specified. */
1762 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1763 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1765 else /* Parse out username and password */
1767 LPCWSTR lpszUser = lpszcp;
1768 LPCWSTR lpszPasswd = lpszHost;
1770 while (lpszcp < lpszHost)
1772 if (*lpszcp == ':')
1773 lpszPasswd = lpszcp;
1775 lpszcp++;
1778 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1779 return FALSE;
1781 if (lpszPasswd != lpszHost)
1782 lpszPasswd++;
1783 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1784 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1785 return FALSE;
1787 lpszcp++; /* Advance to beginning of host */
1790 /* Parse <host><:port> */
1792 lpszHost = lpszcp;
1793 lpszPort = lpszNetLoc;
1795 /* special case for res:// URLs: there is no port here, so the host is the
1796 entire string up to the first '/' */
1797 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1799 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1800 return FALSE;
1801 lpszcp=lpszNetLoc;
1803 else
1805 while (lpszcp < lpszNetLoc)
1807 if (*lpszcp == ':')
1808 lpszPort = lpszcp;
1810 lpszcp++;
1813 /* If the scheme is "file" and the host is just one letter, it's not a host */
1814 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1816 lpszcp=lpszHost;
1817 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1819 else
1821 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1822 return FALSE;
1823 if (lpszPort != lpszNetLoc)
1824 lpUC->nPort = atoiW(++lpszPort);
1825 else switch (lpUC->nScheme)
1827 case INTERNET_SCHEME_HTTP:
1828 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1829 break;
1830 case INTERNET_SCHEME_HTTPS:
1831 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1832 break;
1833 case INTERNET_SCHEME_FTP:
1834 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1835 break;
1836 case INTERNET_SCHEME_GOPHER:
1837 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1838 break;
1839 default:
1840 break;
1846 else
1848 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1849 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1850 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1853 /* Here lpszcp points to:
1855 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1856 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1858 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1860 DWORD len;
1862 /* Only truncate the parameter list if it's already been saved
1863 * in lpUC->lpszExtraInfo.
1865 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1866 len = lpszParam - lpszcp;
1867 else
1869 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1870 * newlines if necessary.
1872 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1873 if (lpsznewline != NULL)
1874 len = lpsznewline - lpszcp;
1875 else
1876 len = dwUrlLength-(lpszcp-lpszUrl);
1878 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1879 lpUC->nScheme == INTERNET_SCHEME_FILE)
1881 WCHAR tmppath[MAX_PATH];
1882 if (*lpszcp == '/')
1884 len = MAX_PATH;
1885 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1887 else
1889 WCHAR *iter;
1890 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1891 tmppath[len] = '\0';
1893 iter = tmppath;
1894 while (*iter) {
1895 if (*iter == '/')
1896 *iter = '\\';
1897 ++iter;
1900 /* if ends in \. or \.. append a backslash */
1901 if (tmppath[len - 1] == '.' &&
1902 (tmppath[len - 2] == '\\' ||
1903 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1905 if (len < MAX_PATH - 1)
1907 tmppath[len] = '\\';
1908 tmppath[len+1] = '\0';
1909 ++len;
1912 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1913 return FALSE;
1915 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1916 return FALSE;
1918 else
1920 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1921 lpUC->lpszUrlPath[0] = 0;
1922 lpUC->dwUrlPathLength = 0;
1925 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1926 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1927 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1928 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1929 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1931 return TRUE;
1934 /***********************************************************************
1935 * InternetAttemptConnect (WININET.@)
1937 * Attempt to make a connection to the internet
1939 * RETURNS
1940 * ERROR_SUCCESS on success
1941 * Error value on failure
1944 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1946 FIXME("Stub\n");
1947 return ERROR_SUCCESS;
1951 /***********************************************************************
1952 * convert_url_canonicalization_flags
1954 * Helper for InternetCanonicalizeUrl
1956 * PARAMS
1957 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1959 * RETURNS
1960 * Flags suitable for UrlCanonicalize
1962 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1964 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1966 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1967 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1968 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1969 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1970 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1971 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1972 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1974 return dwUrlFlags;
1977 /***********************************************************************
1978 * InternetCanonicalizeUrlA (WININET.@)
1980 * Escape unsafe characters and spaces
1982 * RETURNS
1983 * TRUE on success
1984 * FALSE on failure
1987 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1988 LPDWORD lpdwBufferLength, DWORD dwFlags)
1990 HRESULT hr;
1992 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1993 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1995 dwFlags = convert_url_canonicalization_flags(dwFlags);
1996 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1997 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1998 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2000 return hr == S_OK;
2003 /***********************************************************************
2004 * InternetCanonicalizeUrlW (WININET.@)
2006 * Escape unsafe characters and spaces
2008 * RETURNS
2009 * TRUE on success
2010 * FALSE on failure
2013 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2014 LPDWORD lpdwBufferLength, DWORD dwFlags)
2016 HRESULT hr;
2018 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2019 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2021 dwFlags = convert_url_canonicalization_flags(dwFlags);
2022 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2023 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2024 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2026 return hr == S_OK;
2029 /* #################################################### */
2031 static INTERNET_STATUS_CALLBACK set_status_callback(
2032 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2034 INTERNET_STATUS_CALLBACK ret;
2036 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2037 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2039 ret = lpwh->lpfnStatusCB;
2040 lpwh->lpfnStatusCB = callback;
2042 return ret;
2045 /***********************************************************************
2046 * InternetSetStatusCallbackA (WININET.@)
2048 * Sets up a callback function which is called as progress is made
2049 * during an operation.
2051 * RETURNS
2052 * Previous callback or NULL on success
2053 * INTERNET_INVALID_STATUS_CALLBACK on failure
2056 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2057 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2059 INTERNET_STATUS_CALLBACK retVal;
2060 object_header_t *lpwh;
2062 TRACE("%p\n", hInternet);
2064 if (!(lpwh = get_handle_object(hInternet)))
2065 return INTERNET_INVALID_STATUS_CALLBACK;
2067 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2069 WININET_Release( lpwh );
2070 return retVal;
2073 /***********************************************************************
2074 * InternetSetStatusCallbackW (WININET.@)
2076 * Sets up a callback function which is called as progress is made
2077 * during an operation.
2079 * RETURNS
2080 * Previous callback or NULL on success
2081 * INTERNET_INVALID_STATUS_CALLBACK on failure
2084 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2085 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2087 INTERNET_STATUS_CALLBACK retVal;
2088 object_header_t *lpwh;
2090 TRACE("%p\n", hInternet);
2092 if (!(lpwh = get_handle_object(hInternet)))
2093 return INTERNET_INVALID_STATUS_CALLBACK;
2095 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2097 WININET_Release( lpwh );
2098 return retVal;
2101 /***********************************************************************
2102 * InternetSetFilePointer (WININET.@)
2104 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2105 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2107 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2108 return FALSE;
2111 /***********************************************************************
2112 * InternetWriteFile (WININET.@)
2114 * Write data to an open internet file
2116 * RETURNS
2117 * TRUE on success
2118 * FALSE on failure
2121 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2122 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2124 object_header_t *lpwh;
2125 BOOL res;
2127 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2129 lpwh = get_handle_object( hFile );
2130 if (!lpwh) {
2131 WARN("Invalid handle\n");
2132 SetLastError(ERROR_INVALID_HANDLE);
2133 return FALSE;
2136 if(lpwh->vtbl->WriteFile) {
2137 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2138 }else {
2139 WARN("No Writefile method.\n");
2140 res = ERROR_INVALID_HANDLE;
2143 WININET_Release( lpwh );
2145 if(res != ERROR_SUCCESS)
2146 SetLastError(res);
2147 return res == ERROR_SUCCESS;
2151 /***********************************************************************
2152 * InternetReadFile (WININET.@)
2154 * Read data from an open internet file
2156 * RETURNS
2157 * TRUE on success
2158 * FALSE on failure
2161 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2162 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2164 object_header_t *hdr;
2165 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2167 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2169 hdr = get_handle_object(hFile);
2170 if (!hdr) {
2171 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2172 return FALSE;
2175 if(hdr->vtbl->ReadFile)
2176 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2178 WININET_Release(hdr);
2180 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2181 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2183 if(res != ERROR_SUCCESS)
2184 SetLastError(res);
2185 return res == ERROR_SUCCESS;
2188 /***********************************************************************
2189 * InternetReadFileExA (WININET.@)
2191 * Read data from an open internet file
2193 * PARAMS
2194 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2195 * lpBuffersOut [I/O] Buffer.
2196 * dwFlags [I] Flags. See notes.
2197 * dwContext [I] Context for callbacks.
2199 * RETURNS
2200 * TRUE on success
2201 * FALSE on failure
2203 * NOTES
2204 * The parameter dwFlags include zero or more of the following flags:
2205 *|IRF_ASYNC - Makes the call asynchronous.
2206 *|IRF_SYNC - Makes the call synchronous.
2207 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2208 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2210 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2212 * SEE
2213 * InternetOpenUrlA(), HttpOpenRequestA()
2215 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2216 DWORD dwFlags, DWORD_PTR dwContext)
2218 object_header_t *hdr;
2219 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2221 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2223 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2224 SetLastError(ERROR_INVALID_PARAMETER);
2225 return FALSE;
2228 hdr = get_handle_object(hFile);
2229 if (!hdr) {
2230 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2231 return FALSE;
2234 if(hdr->vtbl->ReadFileEx)
2235 res = hdr->vtbl->ReadFileEx(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2236 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2238 WININET_Release(hdr);
2240 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2241 res, lpBuffersOut->dwBufferLength);
2243 if(res != ERROR_SUCCESS)
2244 SetLastError(res);
2245 return res == ERROR_SUCCESS;
2248 /***********************************************************************
2249 * InternetReadFileExW (WININET.@)
2250 * SEE
2251 * InternetReadFileExA()
2253 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2254 DWORD dwFlags, DWORD_PTR dwContext)
2256 object_header_t *hdr;
2257 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2259 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2261 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2262 SetLastError(ERROR_INVALID_PARAMETER);
2263 return FALSE;
2266 hdr = get_handle_object(hFile);
2267 if (!hdr) {
2268 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2269 return FALSE;
2272 if(hdr->vtbl->ReadFileEx)
2273 res = hdr->vtbl->ReadFileEx(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2274 dwFlags, dwContext);
2276 WININET_Release(hdr);
2278 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2279 res, lpBuffer->dwBufferLength);
2281 if(res != ERROR_SUCCESS)
2282 SetLastError(res);
2283 return res == ERROR_SUCCESS;
2286 static WCHAR *get_proxy_autoconfig_url(void)
2288 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2290 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2291 WCHAR *ret = NULL;
2292 SIZE_T len;
2293 const void *ref;
2295 if (!settings) return NULL;
2297 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2299 CFRelease( settings );
2300 return NULL;
2302 len = CFStringGetLength( ref );
2303 if (len)
2304 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2305 if (ret)
2307 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2308 ret[len] = 0;
2310 TRACE( "returning %s\n", debugstr_w(ret) );
2311 CFRelease( settings );
2312 return ret;
2313 #else
2314 FIXME( "no support on this platform\n" );
2315 return NULL;
2316 #endif
2319 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2321 /* FIXME: This function currently handles more options than it should. Options requiring
2322 * proper handles should be moved to proper functions */
2323 switch(option) {
2324 case INTERNET_OPTION_HTTP_VERSION:
2325 if (*size < sizeof(HTTP_VERSION_INFO))
2326 return ERROR_INSUFFICIENT_BUFFER;
2329 * Presently hardcoded to 1.1
2331 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2332 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2333 *size = sizeof(HTTP_VERSION_INFO);
2335 return ERROR_SUCCESS;
2337 case INTERNET_OPTION_CONNECTED_STATE:
2338 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2340 if (*size < sizeof(ULONG))
2341 return ERROR_INSUFFICIENT_BUFFER;
2343 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2344 *size = sizeof(ULONG);
2346 return ERROR_SUCCESS;
2348 case INTERNET_OPTION_PROXY: {
2349 appinfo_t ai;
2350 BOOL ret;
2352 TRACE("Getting global proxy info\n");
2353 memset(&ai, 0, sizeof(appinfo_t));
2354 INTERNET_ConfigureProxy(&ai);
2356 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2357 APPINFO_Destroy(&ai.hdr);
2358 return ret;
2361 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2362 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2364 if (*size < sizeof(ULONG))
2365 return ERROR_INSUFFICIENT_BUFFER;
2367 *(ULONG*)buffer = max_conns;
2368 *size = sizeof(ULONG);
2370 return ERROR_SUCCESS;
2372 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2373 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2375 if (*size < sizeof(ULONG))
2376 return ERROR_INSUFFICIENT_BUFFER;
2378 *(ULONG*)buffer = max_1_0_conns;
2379 *size = sizeof(ULONG);
2381 return ERROR_SUCCESS;
2383 case INTERNET_OPTION_SECURITY_FLAGS:
2384 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2385 return ERROR_SUCCESS;
2387 case INTERNET_OPTION_VERSION: {
2388 static const INTERNET_VERSION_INFO info = { 1, 2 };
2390 TRACE("INTERNET_OPTION_VERSION\n");
2392 if (*size < sizeof(INTERNET_VERSION_INFO))
2393 return ERROR_INSUFFICIENT_BUFFER;
2395 memcpy(buffer, &info, sizeof(info));
2396 *size = sizeof(info);
2398 return ERROR_SUCCESS;
2401 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2402 WCHAR *url;
2403 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2404 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2405 DWORD res = ERROR_SUCCESS, i;
2406 proxyinfo_t pi;
2407 LONG ret;
2409 TRACE("Getting global proxy info\n");
2410 if((ret = INTERNET_LoadProxySettings(&pi)))
2411 return ret;
2413 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2415 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2416 FreeProxyInfo(&pi);
2417 return ERROR_INSUFFICIENT_BUFFER;
2420 url = get_proxy_autoconfig_url();
2422 for (i = 0; i < con->dwOptionCount; i++) {
2423 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2424 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2426 switch (optionW->dwOption) {
2427 case INTERNET_PER_CONN_FLAGS:
2428 if(pi.proxyEnabled)
2429 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2430 else
2431 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2432 if (url)
2433 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2434 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2435 break;
2437 case INTERNET_PER_CONN_PROXY_SERVER:
2438 if (unicode)
2439 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2440 else
2441 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2442 break;
2444 case INTERNET_PER_CONN_PROXY_BYPASS:
2445 if (unicode)
2446 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2447 else
2448 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2449 break;
2451 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2452 if (!url)
2453 optionW->Value.pszValue = NULL;
2454 else if (unicode)
2455 optionW->Value.pszValue = heap_strdupW(url);
2456 else
2457 optionA->Value.pszValue = heap_strdupWtoA(url);
2458 break;
2460 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2461 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2462 break;
2464 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2465 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2466 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2467 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2468 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2469 memset(&optionW->Value, 0, sizeof(optionW->Value));
2470 break;
2472 default:
2473 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2474 res = ERROR_INVALID_PARAMETER;
2475 break;
2478 heap_free(url);
2479 FreeProxyInfo(&pi);
2481 return res;
2483 case INTERNET_OPTION_REQUEST_FLAGS:
2484 case INTERNET_OPTION_USER_AGENT:
2485 *size = 0;
2486 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2487 case INTERNET_OPTION_POLICY:
2488 return ERROR_INVALID_PARAMETER;
2489 case INTERNET_OPTION_CONNECT_TIMEOUT:
2490 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2492 if (*size < sizeof(ULONG))
2493 return ERROR_INSUFFICIENT_BUFFER;
2495 *(ULONG*)buffer = connect_timeout;
2496 *size = sizeof(ULONG);
2498 return ERROR_SUCCESS;
2501 FIXME("Stub for %d\n", option);
2502 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2505 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2507 switch(option) {
2508 case INTERNET_OPTION_CONTEXT_VALUE:
2509 if (!size)
2510 return ERROR_INVALID_PARAMETER;
2512 if (*size < sizeof(DWORD_PTR)) {
2513 *size = sizeof(DWORD_PTR);
2514 return ERROR_INSUFFICIENT_BUFFER;
2516 if (!buffer)
2517 return ERROR_INVALID_PARAMETER;
2519 *(DWORD_PTR *)buffer = hdr->dwContext;
2520 *size = sizeof(DWORD_PTR);
2521 return ERROR_SUCCESS;
2523 case INTERNET_OPTION_REQUEST_FLAGS:
2524 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2525 *size = sizeof(DWORD);
2526 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2528 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2529 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2530 WARN("Called on global option %u\n", option);
2531 return ERROR_INTERNET_INVALID_OPERATION;
2534 /* FIXME: we shouldn't call it here */
2535 return query_global_option(option, buffer, size, unicode);
2538 /***********************************************************************
2539 * InternetQueryOptionW (WININET.@)
2541 * Queries an options on the specified handle
2543 * RETURNS
2544 * TRUE on success
2545 * FALSE on failure
2548 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2549 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2551 object_header_t *hdr;
2552 DWORD res = ERROR_INVALID_HANDLE;
2554 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2556 if(hInternet) {
2557 hdr = get_handle_object(hInternet);
2558 if (hdr) {
2559 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2560 WININET_Release(hdr);
2562 }else {
2563 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2566 if(res != ERROR_SUCCESS)
2567 SetLastError(res);
2568 return res == ERROR_SUCCESS;
2571 /***********************************************************************
2572 * InternetQueryOptionA (WININET.@)
2574 * Queries an options on the specified handle
2576 * RETURNS
2577 * TRUE on success
2578 * FALSE on failure
2581 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2582 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2584 object_header_t *hdr;
2585 DWORD res = ERROR_INVALID_HANDLE;
2587 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2589 if(hInternet) {
2590 hdr = get_handle_object(hInternet);
2591 if (hdr) {
2592 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2593 WININET_Release(hdr);
2595 }else {
2596 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2599 if(res != ERROR_SUCCESS)
2600 SetLastError(res);
2601 return res == ERROR_SUCCESS;
2604 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2606 switch(option) {
2607 case INTERNET_OPTION_CALLBACK:
2608 WARN("Not settable option %u\n", option);
2609 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2610 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2611 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2612 WARN("Called on global option %u\n", option);
2613 return ERROR_INTERNET_INVALID_OPERATION;
2616 return ERROR_INTERNET_INVALID_OPTION;
2619 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2621 switch(option) {
2622 case INTERNET_OPTION_CALLBACK:
2623 WARN("Not global option %u\n", option);
2624 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2626 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2627 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2629 if(size != sizeof(max_conns))
2630 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2631 if(!*(ULONG*)buf)
2632 return ERROR_BAD_ARGUMENTS;
2634 max_conns = *(ULONG*)buf;
2635 return ERROR_SUCCESS;
2637 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2638 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2640 if(size != sizeof(max_1_0_conns))
2641 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2642 if(!*(ULONG*)buf)
2643 return ERROR_BAD_ARGUMENTS;
2645 max_1_0_conns = *(ULONG*)buf;
2646 return ERROR_SUCCESS;
2648 case INTERNET_OPTION_CONNECT_TIMEOUT:
2649 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2651 if(size != sizeof(connect_timeout))
2652 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2653 if(!*(ULONG*)buf)
2654 return ERROR_BAD_ARGUMENTS;
2656 connect_timeout = *(ULONG*)buf;
2657 return ERROR_SUCCESS;
2659 case INTERNET_OPTION_SETTINGS_CHANGED:
2660 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2661 collect_connections(COLLECT_CONNECTIONS);
2662 return ERROR_SUCCESS;
2664 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2665 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2667 if(size != sizeof(ULONG))
2668 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2670 FIXME("%08x\n", *(ULONG*)buf);
2671 return ERROR_SUCCESS;
2674 return ERROR_INTERNET_INVALID_OPTION;
2677 /***********************************************************************
2678 * InternetSetOptionW (WININET.@)
2680 * Sets an options on the specified handle
2682 * RETURNS
2683 * TRUE on success
2684 * FALSE on failure
2687 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2688 LPVOID lpBuffer, DWORD dwBufferLength)
2690 object_header_t *lpwhh;
2691 BOOL ret = TRUE;
2692 DWORD res;
2694 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2696 lpwhh = (object_header_t*) get_handle_object( hInternet );
2697 if(lpwhh)
2698 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2699 else
2700 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2702 if(res != ERROR_INTERNET_INVALID_OPTION) {
2703 if(lpwhh)
2704 WININET_Release(lpwhh);
2706 if(res != ERROR_SUCCESS)
2707 SetLastError(res);
2709 return res == ERROR_SUCCESS;
2712 switch (dwOption)
2714 case INTERNET_OPTION_HTTP_VERSION:
2716 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2717 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2719 break;
2720 case INTERNET_OPTION_ERROR_MASK:
2722 if(!lpwhh) {
2723 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2724 return FALSE;
2725 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2726 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2727 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2728 SetLastError(ERROR_INVALID_PARAMETER);
2729 ret = FALSE;
2730 } else if(dwBufferLength != sizeof(ULONG)) {
2731 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2732 ret = FALSE;
2733 } else
2734 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2735 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2737 break;
2738 case INTERNET_OPTION_PROXY:
2740 INTERNET_PROXY_INFOW *info = lpBuffer;
2742 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2744 SetLastError(ERROR_INVALID_PARAMETER);
2745 return FALSE;
2747 if (!hInternet)
2749 EnterCriticalSection( &WININET_cs );
2750 free_global_proxy();
2751 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2752 if (global_proxy)
2754 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2756 global_proxy->proxyEnabled = 1;
2757 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2758 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2760 else
2762 global_proxy->proxyEnabled = 0;
2763 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2766 LeaveCriticalSection( &WININET_cs );
2768 else
2770 /* In general, each type of object should handle
2771 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2772 * get silently dropped.
2774 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2775 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2776 ret = FALSE;
2778 break;
2780 case INTERNET_OPTION_CODEPAGE:
2782 ULONG codepage = *(ULONG *)lpBuffer;
2783 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2785 break;
2786 case INTERNET_OPTION_REQUEST_PRIORITY:
2788 ULONG priority = *(ULONG *)lpBuffer;
2789 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2791 break;
2792 case INTERNET_OPTION_CONNECT_TIMEOUT:
2794 ULONG connecttimeout = *(ULONG *)lpBuffer;
2795 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2797 break;
2798 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2800 ULONG receivetimeout = *(ULONG *)lpBuffer;
2801 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2803 break;
2804 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2805 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2806 break;
2807 case INTERNET_OPTION_END_BROWSER_SESSION:
2808 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2809 break;
2810 case INTERNET_OPTION_CONNECTED_STATE:
2811 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2812 break;
2813 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2814 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2815 break;
2816 case INTERNET_OPTION_SEND_TIMEOUT:
2817 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2818 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2820 ULONG timeout = *(ULONG *)lpBuffer;
2821 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2822 break;
2824 case INTERNET_OPTION_CONNECT_RETRIES:
2826 ULONG retries = *(ULONG *)lpBuffer;
2827 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2828 break;
2830 case INTERNET_OPTION_CONTEXT_VALUE:
2832 if (!lpwhh)
2834 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2835 return FALSE;
2837 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2839 SetLastError(ERROR_INVALID_PARAMETER);
2840 ret = FALSE;
2842 else
2843 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2844 break;
2846 case INTERNET_OPTION_SECURITY_FLAGS:
2847 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2848 break;
2849 case INTERNET_OPTION_DISABLE_AUTODIAL:
2850 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2851 break;
2852 case INTERNET_OPTION_HTTP_DECODING:
2853 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2854 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2855 ret = FALSE;
2856 break;
2857 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2858 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2859 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2860 ret = FALSE;
2861 break;
2862 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2863 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2864 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2865 ret = FALSE;
2866 break;
2867 case INTERNET_OPTION_CODEPAGE_PATH:
2868 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2869 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2870 ret = FALSE;
2871 break;
2872 case INTERNET_OPTION_CODEPAGE_EXTRA:
2873 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2874 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2875 ret = FALSE;
2876 break;
2877 case INTERNET_OPTION_IDN:
2878 FIXME("INTERNET_OPTION_IDN; STUB\n");
2879 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2880 ret = FALSE;
2881 break;
2882 case INTERNET_OPTION_POLICY:
2883 SetLastError(ERROR_INVALID_PARAMETER);
2884 ret = FALSE;
2885 break;
2886 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2887 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2888 LONG res;
2889 unsigned int i;
2890 proxyinfo_t pi;
2892 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2894 for (i = 0; i < con->dwOptionCount; i++) {
2895 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2897 switch (option->dwOption) {
2898 case INTERNET_PER_CONN_PROXY_SERVER:
2899 heap_free(pi.proxy);
2900 pi.proxy = heap_strdupW(option->Value.pszValue);
2901 break;
2903 case INTERNET_PER_CONN_FLAGS:
2904 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2905 pi.proxyEnabled = 1;
2906 else
2908 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2909 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2910 pi.proxyEnabled = 0;
2912 break;
2914 case INTERNET_PER_CONN_PROXY_BYPASS:
2915 heap_free(pi.proxyBypass);
2916 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2917 break;
2919 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2920 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2921 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2922 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2923 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2924 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2925 FIXME("Unhandled dwOption %d\n", option->dwOption);
2926 break;
2928 default:
2929 FIXME("Unknown dwOption %d\n", option->dwOption);
2930 SetLastError(ERROR_INVALID_PARAMETER);
2931 break;
2935 if ((res = INTERNET_SaveProxySettings(&pi)))
2936 SetLastError(res);
2938 FreeProxyInfo(&pi);
2940 ret = (res == ERROR_SUCCESS);
2941 break;
2943 default:
2944 FIXME("Option %d STUB\n",dwOption);
2945 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2946 ret = FALSE;
2947 break;
2950 if(lpwhh)
2951 WININET_Release( lpwhh );
2953 return ret;
2957 /***********************************************************************
2958 * InternetSetOptionA (WININET.@)
2960 * Sets an options on the specified handle.
2962 * RETURNS
2963 * TRUE on success
2964 * FALSE on failure
2967 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2968 LPVOID lpBuffer, DWORD dwBufferLength)
2970 LPVOID wbuffer;
2971 DWORD wlen;
2972 BOOL r;
2974 switch( dwOption )
2976 case INTERNET_OPTION_PROXY:
2978 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2979 LPINTERNET_PROXY_INFOW piw;
2980 DWORD proxlen, prbylen;
2981 LPWSTR prox, prby;
2983 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2984 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2985 wlen = sizeof(*piw) + proxlen + prbylen;
2986 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2987 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2988 piw->dwAccessType = pi->dwAccessType;
2989 prox = (LPWSTR) &piw[1];
2990 prby = &prox[proxlen+1];
2991 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2992 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2993 piw->lpszProxy = prox;
2994 piw->lpszProxyBypass = prby;
2996 break;
2997 case INTERNET_OPTION_USER_AGENT:
2998 case INTERNET_OPTION_USERNAME:
2999 case INTERNET_OPTION_PASSWORD:
3000 case INTERNET_OPTION_PROXY_USERNAME:
3001 case INTERNET_OPTION_PROXY_PASSWORD:
3002 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3003 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3004 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3005 break;
3006 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3007 unsigned int i;
3008 INTERNET_PER_CONN_OPTION_LISTW *listW;
3009 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3010 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3011 wbuffer = heap_alloc(wlen);
3012 listW = wbuffer;
3014 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3015 if (listA->pszConnection)
3017 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3018 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3019 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3021 else
3022 listW->pszConnection = NULL;
3023 listW->dwOptionCount = listA->dwOptionCount;
3024 listW->dwOptionError = listA->dwOptionError;
3025 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3027 for (i = 0; i < listA->dwOptionCount; ++i) {
3028 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3029 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3031 optW->dwOption = optA->dwOption;
3033 switch (optA->dwOption) {
3034 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3035 case INTERNET_PER_CONN_PROXY_BYPASS:
3036 case INTERNET_PER_CONN_PROXY_SERVER:
3037 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3038 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3039 if (optA->Value.pszValue)
3041 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3042 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3043 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3045 else
3046 optW->Value.pszValue = NULL;
3047 break;
3048 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3049 case INTERNET_PER_CONN_FLAGS:
3050 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3051 optW->Value.dwValue = optA->Value.dwValue;
3052 break;
3053 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3054 optW->Value.ftValue = optA->Value.ftValue;
3055 break;
3056 default:
3057 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3058 optW->Value.dwValue = optA->Value.dwValue;
3059 break;
3063 break;
3064 default:
3065 wbuffer = lpBuffer;
3066 wlen = dwBufferLength;
3069 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3071 if( lpBuffer != wbuffer )
3073 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3075 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3076 unsigned int i;
3077 for (i = 0; i < list->dwOptionCount; ++i) {
3078 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3079 switch (opt->dwOption) {
3080 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3081 case INTERNET_PER_CONN_PROXY_BYPASS:
3082 case INTERNET_PER_CONN_PROXY_SERVER:
3083 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3084 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3085 heap_free( opt->Value.pszValue );
3086 break;
3087 default:
3088 break;
3091 heap_free( list->pOptions );
3093 heap_free( wbuffer );
3096 return r;
3100 /***********************************************************************
3101 * InternetSetOptionExA (WININET.@)
3103 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3104 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3106 FIXME("Flags %08x ignored\n", dwFlags);
3107 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3110 /***********************************************************************
3111 * InternetSetOptionExW (WININET.@)
3113 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3114 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3116 FIXME("Flags %08x ignored\n", dwFlags);
3117 if( dwFlags & ~ISO_VALID_FLAGS )
3119 SetLastError( ERROR_INVALID_PARAMETER );
3120 return FALSE;
3122 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3125 static const WCHAR WININET_wkday[7][4] =
3126 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3127 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3128 static const WCHAR WININET_month[12][4] =
3129 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3130 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3131 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3133 /***********************************************************************
3134 * InternetTimeFromSystemTimeA (WININET.@)
3136 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3138 BOOL ret;
3139 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3141 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3143 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3145 SetLastError(ERROR_INVALID_PARAMETER);
3146 return FALSE;
3149 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3151 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3152 return FALSE;
3155 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3156 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3158 return ret;
3161 /***********************************************************************
3162 * InternetTimeFromSystemTimeW (WININET.@)
3164 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3166 static const WCHAR date[] =
3167 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3168 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3170 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3172 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3174 SetLastError(ERROR_INVALID_PARAMETER);
3175 return FALSE;
3178 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3180 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3181 return FALSE;
3184 sprintfW( string, date,
3185 WININET_wkday[time->wDayOfWeek],
3186 time->wDay,
3187 WININET_month[time->wMonth - 1],
3188 time->wYear,
3189 time->wHour,
3190 time->wMinute,
3191 time->wSecond );
3193 return TRUE;
3196 /***********************************************************************
3197 * InternetTimeToSystemTimeA (WININET.@)
3199 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3201 BOOL ret = FALSE;
3202 WCHAR *stringW;
3204 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3206 stringW = heap_strdupAtoW(string);
3207 if (stringW)
3209 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3210 heap_free( stringW );
3212 return ret;
3215 /***********************************************************************
3216 * InternetTimeToSystemTimeW (WININET.@)
3218 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3220 unsigned int i;
3221 const WCHAR *s = string;
3222 WCHAR *end;
3224 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3226 if (!string || !time) return FALSE;
3228 /* Windows does this too */
3229 GetSystemTime( time );
3231 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3232 * a SYSTEMTIME structure.
3235 while (*s && !isalphaW( *s )) s++;
3236 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3237 time->wDayOfWeek = 7;
3239 for (i = 0; i < 7; i++)
3241 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3242 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3243 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3245 time->wDayOfWeek = i;
3246 break;
3250 if (time->wDayOfWeek > 6) return TRUE;
3251 while (*s && !isdigitW( *s )) s++;
3252 time->wDay = strtolW( s, &end, 10 );
3253 s = end;
3255 while (*s && !isalphaW( *s )) s++;
3256 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3257 time->wMonth = 0;
3259 for (i = 0; i < 12; i++)
3261 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3262 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3263 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3265 time->wMonth = i + 1;
3266 break;
3269 if (time->wMonth == 0) return TRUE;
3271 while (*s && !isdigitW( *s )) s++;
3272 if (*s == '\0') return TRUE;
3273 time->wYear = strtolW( s, &end, 10 );
3274 s = end;
3276 while (*s && !isdigitW( *s )) s++;
3277 if (*s == '\0') return TRUE;
3278 time->wHour = strtolW( s, &end, 10 );
3279 s = end;
3281 while (*s && !isdigitW( *s )) s++;
3282 if (*s == '\0') return TRUE;
3283 time->wMinute = strtolW( s, &end, 10 );
3284 s = end;
3286 while (*s && !isdigitW( *s )) s++;
3287 if (*s == '\0') return TRUE;
3288 time->wSecond = strtolW( s, &end, 10 );
3289 s = end;
3291 time->wMilliseconds = 0;
3292 return TRUE;
3295 /***********************************************************************
3296 * InternetCheckConnectionW (WININET.@)
3298 * Pings a requested host to check internet connection
3300 * RETURNS
3301 * TRUE on success and FALSE on failure. If a failure then
3302 * ERROR_NOT_CONNECTED is placed into GetLastError
3305 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3308 * this is a kludge which runs the resident ping program and reads the output.
3310 * Anyone have a better idea?
3313 BOOL rc = FALSE;
3314 static const CHAR ping[] = "ping -c 1 ";
3315 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3316 WCHAR *host;
3317 DWORD len, host_len;
3318 INTERNET_PORT port;
3319 int status = -1;
3321 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3324 * Crack or set the Address
3326 if (lpszUrl == NULL)
3329 * According to the doc we are supposed to use the ip for the next
3330 * server in the WnInet internal server database. I have
3331 * no idea what that is or how to get it.
3333 * So someone needs to implement this.
3335 FIXME("Unimplemented with URL of NULL\n");
3336 return TRUE;
3338 else
3340 URL_COMPONENTSW components = {sizeof(components)};
3342 components.dwHostNameLength = 1;
3344 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3345 goto End;
3347 host = components.lpszHostName;
3348 host_len = components.dwHostNameLength;
3349 port = components.nPort;
3350 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3353 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3355 struct sockaddr_storage saddr;
3356 int sa_len = sizeof(saddr);
3357 WCHAR *host_z;
3358 int fd;
3359 BOOL b;
3361 host_z = heap_strndupW(host, host_len);
3362 if (!host_z)
3363 return FALSE;
3365 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3366 heap_free(host_z);
3367 if(!b)
3368 goto End;
3369 init_winsock();
3370 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3371 if (fd != -1)
3373 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3374 rc = TRUE;
3375 closesocket(fd);
3378 else
3381 * Build our ping command
3383 char *command;
3385 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3386 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3387 strcpy(command, ping);
3388 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3389 strcpy(command+sizeof(ping)-1+len, redirect);
3391 TRACE("Ping command is : %s\n",command);
3393 status = system(command);
3394 heap_free( command );
3396 TRACE("Ping returned a code of %i\n",status);
3398 /* Ping return code of 0 indicates success */
3399 if (status == 0)
3400 rc = TRUE;
3403 End:
3404 if (rc == FALSE)
3405 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3407 return rc;
3411 /***********************************************************************
3412 * InternetCheckConnectionA (WININET.@)
3414 * Pings a requested host to check internet connection
3416 * RETURNS
3417 * TRUE on success and FALSE on failure. If a failure then
3418 * ERROR_NOT_CONNECTED is placed into GetLastError
3421 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3423 WCHAR *url = NULL;
3424 BOOL rc;
3426 if(lpszUrl) {
3427 url = heap_strdupAtoW(lpszUrl);
3428 if(!url)
3429 return FALSE;
3432 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3434 heap_free(url);
3435 return rc;
3439 /**********************************************************
3440 * INTERNET_InternetOpenUrlW (internal)
3442 * Opens an URL
3444 * RETURNS
3445 * handle of connection or NULL on failure
3447 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3448 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3450 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3451 WCHAR *host, *user = NULL, *pass = NULL, *path;
3452 HINTERNET client = NULL, client1 = NULL;
3453 DWORD res;
3455 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3456 dwHeadersLength, dwFlags, dwContext);
3458 urlComponents.dwHostNameLength = 1;
3459 urlComponents.dwUserNameLength = 1;
3460 urlComponents.dwPasswordLength = 1;
3461 urlComponents.dwUrlPathLength = 1;
3462 urlComponents.dwExtraInfoLength = 1;
3463 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3464 return NULL;
3466 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP && urlComponents.dwExtraInfoLength) {
3467 assert(urlComponents.lpszUrlPath + urlComponents.dwUrlPathLength == urlComponents.lpszExtraInfo);
3468 urlComponents.dwUrlPathLength += urlComponents.dwExtraInfoLength;
3471 host = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
3472 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
3473 if(urlComponents.dwUserNameLength)
3474 user = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
3475 if(urlComponents.dwPasswordLength)
3476 pass = heap_strndupW(urlComponents.lpszPassword, urlComponents.dwPasswordLength);
3478 switch(urlComponents.nScheme) {
3479 case INTERNET_SCHEME_FTP:
3480 client = FTP_Connect(hIC, host, urlComponents.nPort,
3481 user, pass, dwFlags, dwContext, INET_OPENURL);
3482 if(client == NULL)
3483 break;
3484 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3485 if(client1 == NULL) {
3486 InternetCloseHandle(client);
3487 break;
3489 break;
3491 case INTERNET_SCHEME_HTTP:
3492 case INTERNET_SCHEME_HTTPS: {
3493 static const WCHAR szStars[] = { '*','/','*', 0 };
3494 LPCWSTR accept[2] = { szStars, NULL };
3496 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3498 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3499 res = HTTP_Connect(hIC, host, urlComponents.nPort,
3500 user, pass, dwFlags, dwContext, INET_OPENURL, &client);
3501 if(res != ERROR_SUCCESS) {
3502 INTERNET_SetLastError(res);
3503 break;
3506 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3507 if(client1 == NULL) {
3508 InternetCloseHandle(client);
3509 break;
3511 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3512 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3513 GetLastError() != ERROR_IO_PENDING) {
3514 InternetCloseHandle(client1);
3515 client1 = NULL;
3516 break;
3519 case INTERNET_SCHEME_GOPHER:
3520 /* gopher doesn't seem to be implemented in wine, but it's supposed
3521 * to be supported by InternetOpenUrlA. */
3522 default:
3523 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3524 break;
3527 TRACE(" %p <--\n", client1);
3529 heap_free(host);
3530 heap_free(path);
3531 heap_free(user);
3532 heap_free(pass);
3533 return client1;
3536 /**********************************************************
3537 * InternetOpenUrlW (WININET.@)
3539 * Opens an URL
3541 * RETURNS
3542 * handle of connection or NULL on failure
3544 typedef struct {
3545 task_header_t hdr;
3546 WCHAR *url;
3547 WCHAR *headers;
3548 DWORD headers_len;
3549 DWORD flags;
3550 DWORD_PTR context;
3551 } open_url_task_t;
3553 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3555 open_url_task_t *task = (open_url_task_t*)hdr;
3557 TRACE("%p\n", task->hdr.hdr);
3559 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3560 task->headers_len, task->flags, task->context);
3561 heap_free(task->url);
3562 heap_free(task->headers);
3565 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3566 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3568 HINTERNET ret = NULL;
3569 appinfo_t *hIC = NULL;
3571 if (TRACE_ON(wininet)) {
3572 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3573 dwHeadersLength, dwFlags, dwContext);
3574 TRACE(" flags :");
3575 dump_INTERNET_FLAGS(dwFlags);
3578 if (!lpszUrl)
3580 SetLastError(ERROR_INVALID_PARAMETER);
3581 goto lend;
3584 hIC = (appinfo_t*)get_handle_object( hInternet );
3585 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3586 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3587 goto lend;
3590 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3591 open_url_task_t *task;
3593 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3594 task->url = heap_strdupW(lpszUrl);
3595 task->headers = heap_strdupW(lpszHeaders);
3596 task->headers_len = dwHeadersLength;
3597 task->flags = dwFlags;
3598 task->context = dwContext;
3600 INTERNET_AsyncCall(&task->hdr);
3601 SetLastError(ERROR_IO_PENDING);
3602 } else {
3603 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3606 lend:
3607 if( hIC )
3608 WININET_Release( &hIC->hdr );
3609 TRACE(" %p <--\n", ret);
3611 return ret;
3614 /**********************************************************
3615 * InternetOpenUrlA (WININET.@)
3617 * Opens an URL
3619 * RETURNS
3620 * handle of connection or NULL on failure
3622 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3623 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3625 HINTERNET rc = NULL;
3626 LPWSTR szUrl = NULL;
3627 WCHAR *headers = NULL;
3629 TRACE("\n");
3631 if(lpszUrl) {
3632 szUrl = heap_strdupAtoW(lpszUrl);
3633 if(!szUrl)
3634 return NULL;
3637 if(lpszHeaders) {
3638 headers = heap_strndupAtoW(lpszHeaders, dwHeadersLength, &dwHeadersLength);
3639 if(!headers) {
3640 heap_free(szUrl);
3641 return NULL;
3645 rc = InternetOpenUrlW(hInternet, szUrl, headers, dwHeadersLength, dwFlags, dwContext);
3647 heap_free(szUrl);
3648 heap_free(headers);
3649 return rc;
3653 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3655 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3657 if (lpwite)
3659 lpwite->dwError = 0;
3660 lpwite->response[0] = '\0';
3663 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3665 heap_free(lpwite);
3666 return NULL;
3668 return lpwite;
3672 /***********************************************************************
3673 * INTERNET_SetLastError (internal)
3675 * Set last thread specific error
3677 * RETURNS
3680 void INTERNET_SetLastError(DWORD dwError)
3682 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3684 if (!lpwite)
3685 lpwite = INTERNET_AllocThreadError();
3687 SetLastError(dwError);
3688 if(lpwite)
3689 lpwite->dwError = dwError;
3693 /***********************************************************************
3694 * INTERNET_GetLastError (internal)
3696 * Get last thread specific error
3698 * RETURNS
3701 DWORD INTERNET_GetLastError(void)
3703 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3704 if (!lpwite) return 0;
3705 /* TlsGetValue clears last error, so set it again here */
3706 SetLastError(lpwite->dwError);
3707 return lpwite->dwError;
3711 /***********************************************************************
3712 * INTERNET_WorkerThreadFunc (internal)
3714 * Worker thread execution function
3716 * RETURNS
3719 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3721 task_header_t *task = lpvParam;
3723 TRACE("\n");
3725 task->proc(task);
3726 WININET_Release(task->hdr);
3727 heap_free(task);
3729 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3731 heap_free(TlsGetValue(g_dwTlsErrIndex));
3732 TlsSetValue(g_dwTlsErrIndex, NULL);
3734 return TRUE;
3737 void *alloc_async_task(object_header_t *hdr, async_task_proc_t proc, size_t size)
3739 task_header_t *task;
3741 task = heap_alloc(size);
3742 if(!task)
3743 return NULL;
3745 task->hdr = WININET_AddRef(hdr);
3746 task->proc = proc;
3747 return task;
3750 /***********************************************************************
3751 * INTERNET_AsyncCall (internal)
3753 * Retrieves work request from queue
3755 * RETURNS
3758 DWORD INTERNET_AsyncCall(task_header_t *task)
3760 BOOL bSuccess;
3762 TRACE("\n");
3764 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, task, WT_EXECUTELONGFUNCTION);
3765 if (!bSuccess)
3767 heap_free(task);
3768 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3770 return ERROR_SUCCESS;
3774 /***********************************************************************
3775 * INTERNET_GetResponseBuffer (internal)
3777 * RETURNS
3780 LPSTR INTERNET_GetResponseBuffer(void)
3782 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3783 if (!lpwite)
3784 lpwite = INTERNET_AllocThreadError();
3785 TRACE("\n");
3786 return lpwite->response;
3789 /**********************************************************
3790 * InternetQueryDataAvailable (WININET.@)
3792 * Determines how much data is available to be read.
3794 * RETURNS
3795 * TRUE on success, FALSE if an error occurred. If
3796 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3797 * no data is presently available, FALSE is returned with
3798 * the last error ERROR_IO_PENDING; a callback with status
3799 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3800 * data is available.
3802 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3803 LPDWORD lpdwNumberOfBytesAvailable,
3804 DWORD dwFlags, DWORD_PTR dwContext)
3806 object_header_t *hdr;
3807 DWORD res;
3809 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3811 hdr = get_handle_object( hFile );
3812 if (!hdr) {
3813 SetLastError(ERROR_INVALID_HANDLE);
3814 return FALSE;
3817 if(hdr->vtbl->QueryDataAvailable) {
3818 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3819 }else {
3820 WARN("wrong handle\n");
3821 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3824 WININET_Release(hdr);
3826 if(res != ERROR_SUCCESS)
3827 SetLastError(res);
3828 return res == ERROR_SUCCESS;
3831 DWORD create_req_file(const WCHAR *file_name, req_file_t **ret)
3833 req_file_t *req_file;
3835 req_file = heap_alloc_zero(sizeof(*req_file));
3836 if(!req_file)
3837 return ERROR_NOT_ENOUGH_MEMORY;
3839 req_file->ref = 1;
3841 req_file->file_name = heap_strdupW(file_name);
3842 if(!req_file->file_name) {
3843 heap_free(req_file);
3844 return ERROR_NOT_ENOUGH_MEMORY;
3847 req_file->file_handle = CreateFileW(req_file->file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
3848 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3849 if(req_file->file_handle == INVALID_HANDLE_VALUE) {
3850 req_file_release(req_file);
3851 return GetLastError();
3854 *ret = req_file;
3855 return ERROR_SUCCESS;
3858 void req_file_release(req_file_t *req_file)
3860 if(InterlockedDecrement(&req_file->ref))
3861 return;
3863 if(!req_file->is_committed)
3864 DeleteFileW(req_file->file_name);
3865 if(req_file->file_handle && req_file->file_handle != INVALID_HANDLE_VALUE)
3866 CloseHandle(req_file->file_handle);
3867 heap_free(req_file->file_name);
3868 heap_free(req_file->url);
3869 heap_free(req_file);
3872 /***********************************************************************
3873 * InternetLockRequestFile (WININET.@)
3875 BOOL WINAPI InternetLockRequestFile(HINTERNET hInternet, HANDLE *lphLockReqHandle)
3877 req_file_t *req_file = NULL;
3878 object_header_t *hdr;
3879 DWORD res;
3881 TRACE("(%p %p)\n", hInternet, lphLockReqHandle);
3883 hdr = get_handle_object(hInternet);
3884 if (!hdr) {
3885 SetLastError(ERROR_INVALID_HANDLE);
3886 return FALSE;
3889 if(hdr->vtbl->LockRequestFile) {
3890 res = hdr->vtbl->LockRequestFile(hdr, &req_file);
3891 }else {
3892 WARN("wrong handle\n");
3893 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3896 WININET_Release(hdr);
3898 *lphLockReqHandle = req_file;
3899 if(res != ERROR_SUCCESS)
3900 SetLastError(res);
3901 return res == ERROR_SUCCESS;
3904 BOOL WINAPI InternetUnlockRequestFile(HANDLE hLockHandle)
3906 TRACE("(%p)\n", hLockHandle);
3908 req_file_release(hLockHandle);
3909 return TRUE;
3913 /***********************************************************************
3914 * InternetAutodial (WININET.@)
3916 * On windows this function is supposed to dial the default internet
3917 * connection. We don't want to have Wine dial out to the internet so
3918 * we return TRUE by default. It might be nice to check if we are connected.
3920 * RETURNS
3921 * TRUE on success
3922 * FALSE on failure
3925 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3927 FIXME("STUB\n");
3929 /* Tell that we are connected to the internet. */
3930 return TRUE;
3933 /***********************************************************************
3934 * InternetAutodialHangup (WININET.@)
3936 * Hangs up a connection made with InternetAutodial
3938 * PARAM
3939 * dwReserved
3940 * RETURNS
3941 * TRUE on success
3942 * FALSE on failure
3945 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3947 FIXME("STUB\n");
3949 /* we didn't dial, we don't disconnect */
3950 return TRUE;
3953 /***********************************************************************
3954 * InternetCombineUrlA (WININET.@)
3956 * Combine a base URL with a relative URL
3958 * RETURNS
3959 * TRUE on success
3960 * FALSE on failure
3964 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3965 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3966 DWORD dwFlags)
3968 HRESULT hr=S_OK;
3970 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3972 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3973 dwFlags ^= ICU_NO_ENCODE;
3974 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3976 return (hr==S_OK);
3979 /***********************************************************************
3980 * InternetCombineUrlW (WININET.@)
3982 * Combine a base URL with a relative URL
3984 * RETURNS
3985 * TRUE on success
3986 * FALSE on failure
3990 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3991 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3992 DWORD dwFlags)
3994 HRESULT hr=S_OK;
3996 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3998 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3999 dwFlags ^= ICU_NO_ENCODE;
4000 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4002 return (hr==S_OK);
4005 /* max port num is 65535 => 5 digits */
4006 #define MAX_WORD_DIGITS 5
4008 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
4009 (url)->dw##component##Length : strlenW((url)->lpsz##component))
4010 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
4011 (url)->dw##component##Length : strlen((url)->lpsz##component))
4013 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
4015 if ((nScheme == INTERNET_SCHEME_HTTP) &&
4016 (nPort == INTERNET_DEFAULT_HTTP_PORT))
4017 return TRUE;
4018 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
4019 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
4020 return TRUE;
4021 if ((nScheme == INTERNET_SCHEME_FTP) &&
4022 (nPort == INTERNET_DEFAULT_FTP_PORT))
4023 return TRUE;
4024 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
4025 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
4026 return TRUE;
4028 if (nPort == INTERNET_INVALID_PORT_NUMBER)
4029 return TRUE;
4031 return FALSE;
4034 /* opaque urls do not fit into the standard url hierarchy and don't have
4035 * two following slashes */
4036 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
4038 return (nScheme != INTERNET_SCHEME_FTP) &&
4039 (nScheme != INTERNET_SCHEME_GOPHER) &&
4040 (nScheme != INTERNET_SCHEME_HTTP) &&
4041 (nScheme != INTERNET_SCHEME_HTTPS) &&
4042 (nScheme != INTERNET_SCHEME_FILE);
4045 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
4047 int index;
4048 if (scheme < INTERNET_SCHEME_FIRST)
4049 return NULL;
4050 index = scheme - INTERNET_SCHEME_FIRST;
4051 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
4052 return NULL;
4053 return (LPCWSTR)url_schemes[index];
4056 /* we can calculate using ansi strings because we're just
4057 * calculating string length, not size
4059 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
4060 LPDWORD lpdwUrlLength)
4062 INTERNET_SCHEME nScheme;
4064 *lpdwUrlLength = 0;
4066 if (lpUrlComponents->lpszScheme)
4068 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4069 *lpdwUrlLength += dwLen;
4070 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4072 else
4074 LPCWSTR scheme;
4076 nScheme = lpUrlComponents->nScheme;
4078 if (nScheme == INTERNET_SCHEME_DEFAULT)
4079 nScheme = INTERNET_SCHEME_HTTP;
4080 scheme = INTERNET_GetSchemeString(nScheme);
4081 *lpdwUrlLength += strlenW(scheme);
4084 (*lpdwUrlLength)++; /* ':' */
4085 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4086 *lpdwUrlLength += strlen("//");
4088 if (lpUrlComponents->lpszUserName)
4090 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4091 *lpdwUrlLength += strlen("@");
4093 else
4095 if (lpUrlComponents->lpszPassword)
4097 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4098 return FALSE;
4102 if (lpUrlComponents->lpszPassword)
4104 *lpdwUrlLength += strlen(":");
4105 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4108 if (lpUrlComponents->lpszHostName)
4110 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4112 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4114 char szPort[MAX_WORD_DIGITS+1];
4116 sprintf(szPort, "%d", lpUrlComponents->nPort);
4117 *lpdwUrlLength += strlen(szPort);
4118 *lpdwUrlLength += strlen(":");
4121 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4122 (*lpdwUrlLength)++; /* '/' */
4125 if (lpUrlComponents->lpszUrlPath)
4126 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4128 if (lpUrlComponents->lpszExtraInfo)
4129 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4131 return TRUE;
4134 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4136 INT len;
4138 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4140 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4141 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4142 urlCompW->nScheme = lpUrlComponents->nScheme;
4143 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4144 urlCompW->nPort = lpUrlComponents->nPort;
4145 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4146 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4147 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4148 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4150 if (lpUrlComponents->lpszScheme)
4152 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4153 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4154 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4155 -1, urlCompW->lpszScheme, len);
4158 if (lpUrlComponents->lpszHostName)
4160 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4161 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4162 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4163 -1, urlCompW->lpszHostName, len);
4166 if (lpUrlComponents->lpszUserName)
4168 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4169 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4170 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4171 -1, urlCompW->lpszUserName, len);
4174 if (lpUrlComponents->lpszPassword)
4176 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4177 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4178 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4179 -1, urlCompW->lpszPassword, len);
4182 if (lpUrlComponents->lpszUrlPath)
4184 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4185 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4186 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4187 -1, urlCompW->lpszUrlPath, len);
4190 if (lpUrlComponents->lpszExtraInfo)
4192 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4193 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4194 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4195 -1, urlCompW->lpszExtraInfo, len);
4199 /***********************************************************************
4200 * InternetCreateUrlA (WININET.@)
4202 * See InternetCreateUrlW.
4204 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4205 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4207 BOOL ret;
4208 LPWSTR urlW = NULL;
4209 URL_COMPONENTSW urlCompW;
4211 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4213 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4215 SetLastError(ERROR_INVALID_PARAMETER);
4216 return FALSE;
4219 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4221 if (lpszUrl)
4222 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4224 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4226 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4227 *lpdwUrlLength /= sizeof(WCHAR);
4229 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4230 * minus one, so add one to leave room for NULL terminator
4232 if (ret)
4233 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4235 heap_free(urlCompW.lpszScheme);
4236 heap_free(urlCompW.lpszHostName);
4237 heap_free(urlCompW.lpszUserName);
4238 heap_free(urlCompW.lpszPassword);
4239 heap_free(urlCompW.lpszUrlPath);
4240 heap_free(urlCompW.lpszExtraInfo);
4241 heap_free(urlW);
4242 return ret;
4245 /***********************************************************************
4246 * InternetCreateUrlW (WININET.@)
4248 * Creates a URL from its component parts.
4250 * PARAMS
4251 * lpUrlComponents [I] URL Components.
4252 * dwFlags [I] Flags. See notes.
4253 * lpszUrl [I] Buffer in which to store the created URL.
4254 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4255 * lpszUrl in characters. On output, the number of bytes
4256 * required to store the URL including terminator.
4258 * NOTES
4260 * The dwFlags parameter can be zero or more of the following:
4261 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4263 * RETURNS
4264 * TRUE on success
4265 * FALSE on failure
4268 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4269 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4271 DWORD dwLen;
4272 INTERNET_SCHEME nScheme;
4274 static const WCHAR slashSlashW[] = {'/','/'};
4275 static const WCHAR fmtW[] = {'%','u',0};
4277 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4279 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4281 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4282 return FALSE;
4285 if (!calc_url_length(lpUrlComponents, &dwLen))
4286 return FALSE;
4288 if (!lpszUrl || *lpdwUrlLength < dwLen)
4290 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4291 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4292 return FALSE;
4295 *lpdwUrlLength = dwLen;
4296 lpszUrl[0] = 0x00;
4298 dwLen = 0;
4300 if (lpUrlComponents->lpszScheme)
4302 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4303 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4304 lpszUrl += dwLen;
4306 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4308 else
4310 LPCWSTR scheme;
4311 nScheme = lpUrlComponents->nScheme;
4313 if (nScheme == INTERNET_SCHEME_DEFAULT)
4314 nScheme = INTERNET_SCHEME_HTTP;
4316 scheme = INTERNET_GetSchemeString(nScheme);
4317 dwLen = strlenW(scheme);
4318 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4319 lpszUrl += dwLen;
4322 /* all schemes are followed by at least a colon */
4323 *lpszUrl = ':';
4324 lpszUrl++;
4326 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4328 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4329 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4332 if (lpUrlComponents->lpszUserName)
4334 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4335 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4336 lpszUrl += dwLen;
4338 if (lpUrlComponents->lpszPassword)
4340 *lpszUrl = ':';
4341 lpszUrl++;
4343 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4344 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4345 lpszUrl += dwLen;
4348 *lpszUrl = '@';
4349 lpszUrl++;
4352 if (lpUrlComponents->lpszHostName)
4354 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4355 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4356 lpszUrl += dwLen;
4358 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4360 WCHAR szPort[MAX_WORD_DIGITS+1];
4362 sprintfW(szPort, fmtW, lpUrlComponents->nPort);
4363 *lpszUrl = ':';
4364 lpszUrl++;
4365 dwLen = strlenW(szPort);
4366 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4367 lpszUrl += dwLen;
4370 /* add slash between hostname and path if necessary */
4371 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4373 *lpszUrl = '/';
4374 lpszUrl++;
4378 if (lpUrlComponents->lpszUrlPath)
4380 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4381 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4382 lpszUrl += dwLen;
4385 if (lpUrlComponents->lpszExtraInfo)
4387 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4388 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4389 lpszUrl += dwLen;
4392 *lpszUrl = '\0';
4394 return TRUE;
4397 /***********************************************************************
4398 * InternetConfirmZoneCrossingA (WININET.@)
4401 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4403 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4404 return ERROR_SUCCESS;
4407 /***********************************************************************
4408 * InternetConfirmZoneCrossingW (WININET.@)
4411 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4413 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4414 return ERROR_SUCCESS;
4417 static DWORD zone_preference = 3;
4419 /***********************************************************************
4420 * PrivacySetZonePreferenceW (WININET.@)
4422 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4424 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4426 zone_preference = template;
4427 return 0;
4430 /***********************************************************************
4431 * PrivacyGetZonePreferenceW (WININET.@)
4433 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4434 LPWSTR preference, LPDWORD length )
4436 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4438 if (template) *template = zone_preference;
4439 return 0;
4442 /***********************************************************************
4443 * InternetGetSecurityInfoByURLA (WININET.@)
4445 BOOL WINAPI InternetGetSecurityInfoByURLA(LPSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4447 WCHAR *url;
4448 BOOL res;
4450 TRACE("(%s %p %p)\n", debugstr_a(lpszURL), ppCertChain, pdwSecureFlags);
4452 url = heap_strdupAtoW(lpszURL);
4453 if(!url)
4454 return FALSE;
4456 res = InternetGetSecurityInfoByURLW(url, ppCertChain, pdwSecureFlags);
4457 heap_free(url);
4458 return res;
4461 /***********************************************************************
4462 * InternetGetSecurityInfoByURLW (WININET.@)
4464 BOOL WINAPI InternetGetSecurityInfoByURLW(LPCWSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4466 URL_COMPONENTSW url = {sizeof(url)};
4467 server_t *server;
4468 BOOL res;
4470 TRACE("(%s %p %p)\n", debugstr_w(lpszURL), ppCertChain, pdwSecureFlags);
4472 url.dwHostNameLength = 1;
4473 res = InternetCrackUrlW(lpszURL, 0, 0, &url);
4474 if(!res || url.nScheme != INTERNET_SCHEME_HTTPS) {
4475 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4476 return FALSE;
4479 server = get_server(substr(url.lpszHostName, url.dwHostNameLength), url.nPort, TRUE, FALSE);
4480 if(!server) {
4481 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4482 return FALSE;
4485 if(server->cert_chain) {
4486 const CERT_CHAIN_CONTEXT *chain_dup;
4488 chain_dup = CertDuplicateCertificateChain(server->cert_chain);
4489 if(chain_dup) {
4490 *ppCertChain = chain_dup;
4491 *pdwSecureFlags = server->security_flags & _SECURITY_ERROR_FLAGS_MASK;
4492 }else {
4493 res = FALSE;
4495 }else {
4496 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4497 res = FALSE;
4500 server_release(server);
4501 return res;
4504 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4505 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4507 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4508 lpdwConnection, dwReserved);
4509 return ERROR_SUCCESS;
4512 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4513 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4515 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4516 lpdwConnection, dwReserved);
4517 return ERROR_SUCCESS;
4520 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4522 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4523 return TRUE;
4526 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4528 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4529 return TRUE;
4532 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4534 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4535 return ERROR_SUCCESS;
4538 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4539 PBYTE pbHexHash )
4541 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4542 debugstr_w(pwszTarget), pbHexHash);
4543 return FALSE;
4546 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4548 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4549 return FALSE;
4552 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4554 FIXME("(%p, %08lx) stub\n", a, b);
4555 return FALSE;
4558 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4560 FIXME("%p: stub\n", parent);
4561 return 0;