server: Create the initial thread as a separate request.
[wine.git] / dlls / wininet / internet.c
blobb407e851afd38840af79caa2cba3cb36d5e6de8d
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;
973 case INTERNET_OPTION_REFRESH:
974 FIXME("INTERNET_OPTION_REFRESH\n");
975 return ERROR_SUCCESS;
978 return INET_SetOption(hdr, option, buf, size);
981 static const object_vtbl_t APPINFOVtbl = {
982 APPINFO_Destroy,
983 NULL,
984 APPINFO_QueryOption,
985 APPINFO_SetOption,
986 NULL,
987 NULL,
988 NULL
992 /***********************************************************************
993 * InternetOpenW (WININET.@)
995 * Per-application initialization of wininet
997 * RETURNS
998 * HINTERNET on success
999 * NULL on failure
1002 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
1003 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
1005 appinfo_t *lpwai = NULL;
1007 if (TRACE_ON(wininet)) {
1008 #define FE(x) { x, #x }
1009 static const wininet_flag_info access_type[] = {
1010 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1011 FE(INTERNET_OPEN_TYPE_DIRECT),
1012 FE(INTERNET_OPEN_TYPE_PROXY),
1013 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1015 #undef FE
1016 DWORD i;
1017 const char *access_type_str = "Unknown";
1019 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1020 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1021 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1022 if (access_type[i].val == dwAccessType) {
1023 access_type_str = access_type[i].name;
1024 break;
1027 TRACE(" access type : %s\n", access_type_str);
1028 TRACE(" flags :");
1029 dump_INTERNET_FLAGS(dwFlags);
1032 /* Clear any error information */
1033 INTERNET_SetLastError(0);
1035 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1036 SetLastError(ERROR_INVALID_PARAMETER);
1037 return NULL;
1040 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1041 if (!lpwai) {
1042 SetLastError(ERROR_OUTOFMEMORY);
1043 return NULL;
1046 lpwai->hdr.htype = WH_HINIT;
1047 lpwai->hdr.dwFlags = dwFlags;
1048 lpwai->accessType = dwAccessType;
1049 lpwai->proxyUsername = NULL;
1050 lpwai->proxyPassword = NULL;
1051 lpwai->connect_timeout = connect_timeout;
1053 lpwai->agent = heap_strdupW(lpszAgent);
1054 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1055 INTERNET_ConfigureProxy( lpwai );
1056 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1057 lpwai->proxy = heap_strdupW(lpszProxy);
1058 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1061 TRACE("returning %p\n", lpwai);
1063 return lpwai->hdr.hInternet;
1067 /***********************************************************************
1068 * InternetOpenA (WININET.@)
1070 * Per-application initialization of wininet
1072 * RETURNS
1073 * HINTERNET on success
1074 * NULL on failure
1077 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1078 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1080 WCHAR *szAgent, *szProxy, *szBypass;
1081 HINTERNET rc;
1083 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1084 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1086 szAgent = heap_strdupAtoW(lpszAgent);
1087 szProxy = heap_strdupAtoW(lpszProxy);
1088 szBypass = heap_strdupAtoW(lpszProxyBypass);
1090 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1092 heap_free(szAgent);
1093 heap_free(szProxy);
1094 heap_free(szBypass);
1095 return rc;
1098 /***********************************************************************
1099 * InternetGetLastResponseInfoA (WININET.@)
1101 * Return last wininet error description on the calling thread
1103 * RETURNS
1104 * TRUE on success of writing to buffer
1105 * FALSE on failure
1108 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1109 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1111 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1113 TRACE("\n");
1115 if (lpwite)
1117 *lpdwError = lpwite->dwError;
1118 if (lpwite->dwError)
1120 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1121 *lpdwBufferLength = strlen(lpszBuffer);
1123 else
1124 *lpdwBufferLength = 0;
1126 else
1128 *lpdwError = 0;
1129 *lpdwBufferLength = 0;
1132 return TRUE;
1135 /***********************************************************************
1136 * InternetGetLastResponseInfoW (WININET.@)
1138 * Return last wininet error description on the calling thread
1140 * RETURNS
1141 * TRUE on success of writing to buffer
1142 * FALSE on failure
1145 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1146 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1148 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1150 TRACE("\n");
1152 if (lpwite)
1154 *lpdwError = lpwite->dwError;
1155 if (lpwite->dwError)
1157 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1158 *lpdwBufferLength = lstrlenW(lpszBuffer);
1160 else
1161 *lpdwBufferLength = 0;
1163 else
1165 *lpdwError = 0;
1166 *lpdwBufferLength = 0;
1169 return TRUE;
1172 /***********************************************************************
1173 * InternetGetConnectedState (WININET.@)
1175 * Return connected state
1177 * RETURNS
1178 * TRUE if connected
1179 * if lpdwStatus is not null, return the status (off line,
1180 * modem, lan...) in it.
1181 * FALSE if not connected
1183 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1185 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1187 return InternetGetConnectedStateExW(lpdwStatus, NULL, 0, dwReserved);
1191 /***********************************************************************
1192 * InternetGetConnectedStateExW (WININET.@)
1194 * Return connected state
1196 * PARAMS
1198 * lpdwStatus [O] Flags specifying the status of the internet connection.
1199 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1200 * dwNameLen [I] Size of the buffer, in characters.
1201 * dwReserved [I] Reserved. Must be set to 0.
1203 * RETURNS
1204 * TRUE if connected
1205 * if lpdwStatus is not null, return the status (off line,
1206 * modem, lan...) in it.
1207 * FALSE if not connected
1209 * NOTES
1210 * If the system has no available network connections, an empty string is
1211 * stored in lpszConnectionName. If there is a LAN connection, a localized
1212 * "LAN Connection" string is stored. Presumably, if only a dial-up
1213 * connection is available then the name of the dial-up connection is
1214 * returned. Why any application, other than the "Internet Settings" CPL,
1215 * would want to use this function instead of the simpler InternetGetConnectedStateW
1216 * function is beyond me.
1218 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1219 DWORD dwNameLen, DWORD dwReserved)
1221 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1223 /* Must be zero */
1224 if(dwReserved)
1225 return FALSE;
1227 if (lpdwStatus) {
1228 WARN("always returning LAN connection.\n");
1229 *lpdwStatus = INTERNET_CONNECTION_LAN;
1232 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1233 * the resource, avoid it as we must not change the buffer in this case */
1234 if(lpszConnectionName && dwNameLen) {
1235 *lpszConnectionName = '\0';
1236 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1239 return TRUE;
1243 /***********************************************************************
1244 * InternetGetConnectedStateExA (WININET.@)
1246 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1247 DWORD dwNameLen, DWORD dwReserved)
1249 LPWSTR lpwszConnectionName = NULL;
1250 BOOL rc;
1252 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1254 if (lpszConnectionName && dwNameLen > 0)
1255 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1257 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1258 dwReserved);
1259 if (rc && lpwszConnectionName)
1260 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1261 dwNameLen, NULL, NULL);
1263 heap_free(lpwszConnectionName);
1264 return rc;
1268 /***********************************************************************
1269 * InternetConnectW (WININET.@)
1271 * Open a ftp, gopher or http session
1273 * RETURNS
1274 * HINTERNET a session handle on success
1275 * NULL on failure
1278 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1279 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1280 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1281 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1283 appinfo_t *hIC;
1284 HINTERNET rc = NULL;
1285 DWORD res = ERROR_SUCCESS;
1287 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1288 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1290 if (!lpszServerName)
1292 SetLastError(ERROR_INVALID_PARAMETER);
1293 return NULL;
1296 hIC = (appinfo_t*)get_handle_object( hInternet );
1297 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1299 res = ERROR_INVALID_HANDLE;
1300 goto lend;
1303 switch (dwService)
1305 case INTERNET_SERVICE_FTP:
1306 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1307 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1308 if(!rc)
1309 res = INTERNET_GetLastError();
1310 break;
1312 case INTERNET_SERVICE_HTTP:
1313 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1314 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1315 break;
1317 case INTERNET_SERVICE_GOPHER:
1318 default:
1319 break;
1321 lend:
1322 if( hIC )
1323 WININET_Release( &hIC->hdr );
1325 TRACE("returning %p\n", rc);
1326 SetLastError(res);
1327 return rc;
1331 /***********************************************************************
1332 * InternetConnectA (WININET.@)
1334 * Open a ftp, gopher or http session
1336 * RETURNS
1337 * HINTERNET a session handle on success
1338 * NULL on failure
1341 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1342 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1343 LPCSTR lpszUserName, LPCSTR lpszPassword,
1344 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1346 HINTERNET rc = NULL;
1347 LPWSTR szServerName;
1348 LPWSTR szUserName;
1349 LPWSTR szPassword;
1351 szServerName = heap_strdupAtoW(lpszServerName);
1352 szUserName = heap_strdupAtoW(lpszUserName);
1353 szPassword = heap_strdupAtoW(lpszPassword);
1355 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1356 szUserName, szPassword, dwService, dwFlags, dwContext);
1358 heap_free(szServerName);
1359 heap_free(szUserName);
1360 heap_free(szPassword);
1361 return rc;
1365 /***********************************************************************
1366 * InternetFindNextFileA (WININET.@)
1368 * Continues a file search from a previous call to FindFirstFile
1370 * RETURNS
1371 * TRUE on success
1372 * FALSE on failure
1375 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1377 BOOL ret;
1378 WIN32_FIND_DATAW fd;
1380 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1381 if(lpvFindData)
1382 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1383 return ret;
1386 /***********************************************************************
1387 * InternetFindNextFileW (WININET.@)
1389 * Continues a file search from a previous call to FindFirstFile
1391 * RETURNS
1392 * TRUE on success
1393 * FALSE on failure
1396 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1398 object_header_t *hdr;
1399 DWORD res;
1401 TRACE("\n");
1403 hdr = get_handle_object(hFind);
1404 if(!hdr) {
1405 WARN("Invalid handle\n");
1406 SetLastError(ERROR_INVALID_HANDLE);
1407 return FALSE;
1410 if(hdr->vtbl->FindNextFileW) {
1411 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1412 }else {
1413 WARN("Handle doesn't support NextFile\n");
1414 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1417 WININET_Release(hdr);
1419 if(res != ERROR_SUCCESS)
1420 SetLastError(res);
1421 return res == ERROR_SUCCESS;
1424 /***********************************************************************
1425 * InternetCloseHandle (WININET.@)
1427 * Generic close handle function
1429 * RETURNS
1430 * TRUE on success
1431 * FALSE on failure
1434 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1436 object_header_t *obj;
1438 TRACE("%p\n", hInternet);
1440 obj = get_handle_object( hInternet );
1441 if (!obj) {
1442 SetLastError(ERROR_INVALID_HANDLE);
1443 return FALSE;
1446 invalidate_handle(obj);
1447 WININET_Release(obj);
1449 return TRUE;
1452 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1454 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1456 if (!*component_length)
1457 return TRUE;
1459 if (!*component) {
1460 *(const WCHAR**)component = value;
1461 *component_length = len;
1462 return TRUE;
1465 if (*component_length < len+1) {
1466 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1467 return FALSE;
1470 *component_length = len;
1471 if(len)
1472 memcpy(*component, value, len*sizeof(WCHAR));
1473 (*component)[len] = 0;
1474 return TRUE;
1477 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1478 const char *url_a)
1480 size_t size, ret_size = *ret_length;
1482 if (!*ret_length)
1483 return TRUE;
1484 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1486 if (!*comp) {
1487 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1488 *ret_length = size;
1489 return TRUE;
1492 if (size+1 > ret_size) {
1493 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1494 *ret_length = size+1;
1495 return FALSE;
1498 *ret_length = size;
1499 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1500 (*comp)[size] = 0;
1501 return TRUE;
1504 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1506 *len_w = len_a;
1508 if(!comp_a) {
1509 *comp_w = NULL;
1510 return TRUE;
1513 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1514 SetLastError(ERROR_OUTOFMEMORY);
1515 return FALSE;
1518 return TRUE;
1521 /***********************************************************************
1522 * InternetCrackUrlA (WININET.@)
1524 * See InternetCrackUrlW.
1526 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1528 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1529 URL_COMPONENTSW comp;
1530 WCHAR *url_w = NULL;
1531 BOOL ret;
1533 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1535 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1536 SetLastError(ERROR_INVALID_PARAMETER);
1537 return FALSE;
1540 comp.dwStructSize = sizeof(comp);
1542 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1543 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1544 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1545 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1546 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1547 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1548 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1549 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1550 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1551 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1552 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1553 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1555 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1556 SetLastError(ERROR_OUTOFMEMORY);
1557 ret = FALSE;
1560 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1561 ret_comp->nScheme = comp.nScheme;
1562 ret_comp->nPort = comp.nPort;
1564 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1565 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1566 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1567 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1568 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1569 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1570 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1571 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1572 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1573 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1574 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1575 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1577 if(ret)
1578 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1579 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1580 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1581 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1582 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1585 heap_free(host);
1586 heap_free(user);
1587 heap_free(pass);
1588 heap_free(path);
1589 heap_free(scheme);
1590 heap_free(extra);
1591 heap_free(url_w);
1592 return ret;
1595 static const WCHAR url_schemes[][7] =
1597 {'f','t','p',0},
1598 {'g','o','p','h','e','r',0},
1599 {'h','t','t','p',0},
1600 {'h','t','t','p','s',0},
1601 {'f','i','l','e',0},
1602 {'n','e','w','s',0},
1603 {'m','a','i','l','t','o',0},
1604 {'r','e','s',0},
1607 /***********************************************************************
1608 * GetInternetSchemeW (internal)
1610 * Get scheme of url
1612 * RETURNS
1613 * scheme on success
1614 * INTERNET_SCHEME_UNKNOWN on failure
1617 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1619 int i;
1621 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1623 if(lpszScheme==NULL)
1624 return INTERNET_SCHEME_UNKNOWN;
1626 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1627 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1628 return INTERNET_SCHEME_FIRST + i;
1630 return INTERNET_SCHEME_UNKNOWN;
1633 /***********************************************************************
1634 * InternetCrackUrlW (WININET.@)
1636 * Break up URL into its components
1638 * RETURNS
1639 * TRUE on success
1640 * FALSE on failure
1642 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1645 * RFC 1808
1646 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1649 LPCWSTR lpszParam = NULL;
1650 BOOL found_colon = FALSE;
1651 LPCWSTR lpszap;
1652 LPCWSTR lpszcp = NULL, lpszNetLoc;
1654 TRACE("(%s %u %x %p)\n",
1655 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1656 dwUrlLength, dwFlags, lpUC);
1658 if (!lpszUrl || !*lpszUrl || !lpUC)
1660 SetLastError(ERROR_INVALID_PARAMETER);
1661 return FALSE;
1663 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1665 if (dwFlags & ICU_DECODE)
1667 WCHAR *url_tmp, *buffer;
1668 DWORD len = dwUrlLength + 1;
1669 BOOL ret;
1671 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1673 SetLastError(ERROR_OUTOFMEMORY);
1674 return FALSE;
1677 buffer = url_tmp;
1678 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1679 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1681 buffer = heap_alloc(len * sizeof(WCHAR));
1682 if (!buffer)
1684 SetLastError(ERROR_OUTOFMEMORY);
1685 heap_free(url_tmp);
1686 return FALSE;
1688 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1690 if (ret)
1691 ret = InternetCrackUrlW(buffer, len, dwFlags & ~ICU_DECODE, lpUC);
1693 if (buffer != url_tmp) heap_free(buffer);
1694 heap_free(url_tmp);
1695 return ret;
1697 lpszap = lpszUrl;
1699 /* Determine if the URI is absolute. */
1700 while (lpszap - lpszUrl < dwUrlLength)
1702 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1704 lpszap++;
1705 continue;
1707 if (*lpszap == ':')
1709 found_colon = TRUE;
1710 lpszcp = lpszap;
1712 else
1714 lpszcp = lpszUrl; /* Relative url */
1717 break;
1720 if(!found_colon){
1721 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1722 return FALSE;
1725 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1726 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1728 /* Parse <params> */
1729 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1730 if(!lpszParam)
1731 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1733 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1734 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1735 return FALSE;
1738 /* Get scheme first. */
1739 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1740 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1741 return FALSE;
1743 /* Eat ':' in protocol. */
1744 lpszcp++;
1746 /* double slash indicates the net_loc portion is present */
1747 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1749 lpszcp += 2;
1751 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1752 if (lpszParam)
1754 if (lpszNetLoc)
1755 lpszNetLoc = min(lpszNetLoc, lpszParam);
1756 else
1757 lpszNetLoc = lpszParam;
1759 else if (!lpszNetLoc)
1760 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1762 /* Parse net-loc */
1763 if (lpszNetLoc)
1765 LPCWSTR lpszHost;
1766 LPCWSTR lpszPort;
1768 /* [<user>[<:password>]@]<host>[:<port>] */
1769 /* First find the user and password if they exist */
1771 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1772 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1774 /* username and password not specified. */
1775 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1776 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1778 else /* Parse out username and password */
1780 LPCWSTR lpszUser = lpszcp;
1781 LPCWSTR lpszPasswd = lpszHost;
1783 while (lpszcp < lpszHost)
1785 if (*lpszcp == ':')
1786 lpszPasswd = lpszcp;
1788 lpszcp++;
1791 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1792 return FALSE;
1794 if (lpszPasswd != lpszHost)
1795 lpszPasswd++;
1796 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1797 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1798 return FALSE;
1800 lpszcp++; /* Advance to beginning of host */
1803 /* Parse <host><:port> */
1805 lpszHost = lpszcp;
1806 lpszPort = lpszNetLoc;
1808 /* special case for res:// URLs: there is no port here, so the host is the
1809 entire string up to the first '/' */
1810 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1812 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1813 return FALSE;
1814 lpszcp=lpszNetLoc;
1816 else
1818 while (lpszcp < lpszNetLoc)
1820 if (*lpszcp == ':')
1821 lpszPort = lpszcp;
1823 lpszcp++;
1826 /* If the scheme is "file" and the host is just one letter, it's not a host */
1827 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1829 lpszcp=lpszHost;
1830 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1832 else
1834 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1835 return FALSE;
1836 if (lpszPort != lpszNetLoc)
1837 lpUC->nPort = atoiW(++lpszPort);
1838 else switch (lpUC->nScheme)
1840 case INTERNET_SCHEME_HTTP:
1841 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1842 break;
1843 case INTERNET_SCHEME_HTTPS:
1844 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1845 break;
1846 case INTERNET_SCHEME_FTP:
1847 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1848 break;
1849 case INTERNET_SCHEME_GOPHER:
1850 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1851 break;
1852 default:
1853 break;
1859 else
1861 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1862 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1863 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1866 /* Here lpszcp points to:
1868 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1869 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1871 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1873 DWORD len;
1875 /* Only truncate the parameter list if it's already been saved
1876 * in lpUC->lpszExtraInfo.
1878 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1879 len = lpszParam - lpszcp;
1880 else
1882 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1883 * newlines if necessary.
1885 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1886 if (lpsznewline != NULL)
1887 len = lpsznewline - lpszcp;
1888 else
1889 len = dwUrlLength-(lpszcp-lpszUrl);
1891 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1892 lpUC->nScheme == INTERNET_SCHEME_FILE)
1894 WCHAR tmppath[MAX_PATH];
1895 if (*lpszcp == '/')
1897 len = MAX_PATH;
1898 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1900 else
1902 WCHAR *iter;
1903 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1904 tmppath[len] = '\0';
1906 iter = tmppath;
1907 while (*iter) {
1908 if (*iter == '/')
1909 *iter = '\\';
1910 ++iter;
1913 /* if ends in \. or \.. append a backslash */
1914 if (tmppath[len - 1] == '.' &&
1915 (tmppath[len - 2] == '\\' ||
1916 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1918 if (len < MAX_PATH - 1)
1920 tmppath[len] = '\\';
1921 tmppath[len+1] = '\0';
1922 ++len;
1925 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1926 return FALSE;
1928 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1929 return FALSE;
1931 else
1933 set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, 0);
1936 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1937 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1938 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1939 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1940 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1942 return TRUE;
1945 /***********************************************************************
1946 * InternetAttemptConnect (WININET.@)
1948 * Attempt to make a connection to the internet
1950 * RETURNS
1951 * ERROR_SUCCESS on success
1952 * Error value on failure
1955 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1957 FIXME("Stub\n");
1958 return ERROR_SUCCESS;
1962 /***********************************************************************
1963 * convert_url_canonicalization_flags
1965 * Helper for InternetCanonicalizeUrl
1967 * PARAMS
1968 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1970 * RETURNS
1971 * Flags suitable for UrlCanonicalize
1973 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1975 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1977 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1978 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1979 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1980 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1981 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1982 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1983 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1985 return dwUrlFlags;
1988 /***********************************************************************
1989 * InternetCanonicalizeUrlA (WININET.@)
1991 * Escape unsafe characters and spaces
1993 * RETURNS
1994 * TRUE on success
1995 * FALSE on failure
1998 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1999 LPDWORD lpdwBufferLength, DWORD dwFlags)
2001 HRESULT hr;
2003 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
2004 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2006 dwFlags = convert_url_canonicalization_flags(dwFlags);
2007 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2008 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2009 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2011 return hr == S_OK;
2014 /***********************************************************************
2015 * InternetCanonicalizeUrlW (WININET.@)
2017 * Escape unsafe characters and spaces
2019 * RETURNS
2020 * TRUE on success
2021 * FALSE on failure
2024 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2025 LPDWORD lpdwBufferLength, DWORD dwFlags)
2027 HRESULT hr;
2029 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2030 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2032 dwFlags = convert_url_canonicalization_flags(dwFlags);
2033 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2034 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2035 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2037 return hr == S_OK;
2040 /* #################################################### */
2042 static INTERNET_STATUS_CALLBACK set_status_callback(
2043 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2045 INTERNET_STATUS_CALLBACK ret;
2047 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2048 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2050 ret = lpwh->lpfnStatusCB;
2051 lpwh->lpfnStatusCB = callback;
2053 return ret;
2056 /***********************************************************************
2057 * InternetSetStatusCallbackA (WININET.@)
2059 * Sets up a callback function which is called as progress is made
2060 * during an operation.
2062 * RETURNS
2063 * Previous callback or NULL on success
2064 * INTERNET_INVALID_STATUS_CALLBACK on failure
2067 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2068 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2070 INTERNET_STATUS_CALLBACK retVal;
2071 object_header_t *lpwh;
2073 TRACE("%p\n", hInternet);
2075 if (!(lpwh = get_handle_object(hInternet)))
2076 return INTERNET_INVALID_STATUS_CALLBACK;
2078 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2080 WININET_Release( lpwh );
2081 return retVal;
2084 /***********************************************************************
2085 * InternetSetStatusCallbackW (WININET.@)
2087 * Sets up a callback function which is called as progress is made
2088 * during an operation.
2090 * RETURNS
2091 * Previous callback or NULL on success
2092 * INTERNET_INVALID_STATUS_CALLBACK on failure
2095 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2096 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2098 INTERNET_STATUS_CALLBACK retVal;
2099 object_header_t *lpwh;
2101 TRACE("%p\n", hInternet);
2103 if (!(lpwh = get_handle_object(hInternet)))
2104 return INTERNET_INVALID_STATUS_CALLBACK;
2106 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2108 WININET_Release( lpwh );
2109 return retVal;
2112 /***********************************************************************
2113 * InternetSetFilePointer (WININET.@)
2115 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2116 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2118 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2119 return FALSE;
2122 /***********************************************************************
2123 * InternetWriteFile (WININET.@)
2125 * Write data to an open internet file
2127 * RETURNS
2128 * TRUE on success
2129 * FALSE on failure
2132 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2133 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2135 object_header_t *lpwh;
2136 BOOL res;
2138 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2140 lpwh = get_handle_object( hFile );
2141 if (!lpwh) {
2142 WARN("Invalid handle\n");
2143 SetLastError(ERROR_INVALID_HANDLE);
2144 return FALSE;
2147 if(lpwh->vtbl->WriteFile) {
2148 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2149 }else {
2150 WARN("No Writefile method.\n");
2151 res = ERROR_INVALID_HANDLE;
2154 WININET_Release( lpwh );
2156 if(res != ERROR_SUCCESS)
2157 SetLastError(res);
2158 return res == ERROR_SUCCESS;
2162 /***********************************************************************
2163 * InternetReadFile (WININET.@)
2165 * Read data from an open internet file
2167 * RETURNS
2168 * TRUE on success
2169 * FALSE on failure
2172 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2173 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2175 object_header_t *hdr;
2176 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2178 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2180 hdr = get_handle_object(hFile);
2181 if (!hdr) {
2182 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2183 return FALSE;
2186 if(hdr->vtbl->ReadFile) {
2187 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, 0, 0);
2188 if(res == ERROR_IO_PENDING)
2189 *pdwNumOfBytesRead = 0;
2192 WININET_Release(hdr);
2194 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2195 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2197 if(res != ERROR_SUCCESS)
2198 SetLastError(res);
2199 return res == ERROR_SUCCESS;
2202 /***********************************************************************
2203 * InternetReadFileExA (WININET.@)
2205 * Read data from an open internet file
2207 * PARAMS
2208 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2209 * lpBuffersOut [I/O] Buffer.
2210 * dwFlags [I] Flags. See notes.
2211 * dwContext [I] Context for callbacks.
2213 * RETURNS
2214 * TRUE on success
2215 * FALSE on failure
2217 * NOTES
2218 * The parameter dwFlags include zero or more of the following flags:
2219 *|IRF_ASYNC - Makes the call asynchronous.
2220 *|IRF_SYNC - Makes the call synchronous.
2221 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2222 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2224 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2226 * SEE
2227 * InternetOpenUrlA(), HttpOpenRequestA()
2229 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2230 DWORD dwFlags, DWORD_PTR dwContext)
2232 object_header_t *hdr;
2233 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2235 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2237 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2238 SetLastError(ERROR_INVALID_PARAMETER);
2239 return FALSE;
2242 hdr = get_handle_object(hFile);
2243 if (!hdr) {
2244 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2245 return FALSE;
2248 if(hdr->vtbl->ReadFile)
2249 res = hdr->vtbl->ReadFile(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2250 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2252 WININET_Release(hdr);
2254 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2255 res, lpBuffersOut->dwBufferLength);
2257 if(res != ERROR_SUCCESS)
2258 SetLastError(res);
2259 return res == ERROR_SUCCESS;
2262 /***********************************************************************
2263 * InternetReadFileExW (WININET.@)
2264 * SEE
2265 * InternetReadFileExA()
2267 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2268 DWORD dwFlags, DWORD_PTR dwContext)
2270 object_header_t *hdr;
2271 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2273 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2275 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2276 SetLastError(ERROR_INVALID_PARAMETER);
2277 return FALSE;
2280 hdr = get_handle_object(hFile);
2281 if (!hdr) {
2282 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2283 return FALSE;
2286 if(hdr->vtbl->ReadFile)
2287 res = hdr->vtbl->ReadFile(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2288 dwFlags, dwContext);
2290 WININET_Release(hdr);
2292 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2293 res, lpBuffer->dwBufferLength);
2295 if(res != ERROR_SUCCESS)
2296 SetLastError(res);
2297 return res == ERROR_SUCCESS;
2300 static WCHAR *get_proxy_autoconfig_url(void)
2302 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2304 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2305 WCHAR *ret = NULL;
2306 SIZE_T len;
2307 const void *ref;
2309 if (!settings) return NULL;
2311 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2313 CFRelease( settings );
2314 return NULL;
2316 len = CFStringGetLength( ref );
2317 if (len)
2318 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2319 if (ret)
2321 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2322 ret[len] = 0;
2324 TRACE( "returning %s\n", debugstr_w(ret) );
2325 CFRelease( settings );
2326 return ret;
2327 #else
2328 FIXME( "no support on this platform\n" );
2329 return NULL;
2330 #endif
2333 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2335 /* FIXME: This function currently handles more options than it should. Options requiring
2336 * proper handles should be moved to proper functions */
2337 switch(option) {
2338 case INTERNET_OPTION_HTTP_VERSION:
2339 if (*size < sizeof(HTTP_VERSION_INFO))
2340 return ERROR_INSUFFICIENT_BUFFER;
2343 * Presently hardcoded to 1.1
2345 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2346 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2347 *size = sizeof(HTTP_VERSION_INFO);
2349 return ERROR_SUCCESS;
2351 case INTERNET_OPTION_CONNECTED_STATE:
2352 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2354 if (*size < sizeof(ULONG))
2355 return ERROR_INSUFFICIENT_BUFFER;
2357 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2358 *size = sizeof(ULONG);
2360 return ERROR_SUCCESS;
2362 case INTERNET_OPTION_PROXY: {
2363 appinfo_t ai;
2364 BOOL ret;
2366 TRACE("Getting global proxy info\n");
2367 memset(&ai, 0, sizeof(appinfo_t));
2368 INTERNET_ConfigureProxy(&ai);
2370 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2371 APPINFO_Destroy(&ai.hdr);
2372 return ret;
2375 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2376 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2378 if (*size < sizeof(ULONG))
2379 return ERROR_INSUFFICIENT_BUFFER;
2381 *(ULONG*)buffer = max_conns;
2382 *size = sizeof(ULONG);
2384 return ERROR_SUCCESS;
2386 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2387 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2389 if (*size < sizeof(ULONG))
2390 return ERROR_INSUFFICIENT_BUFFER;
2392 *(ULONG*)buffer = max_1_0_conns;
2393 *size = sizeof(ULONG);
2395 return ERROR_SUCCESS;
2397 case INTERNET_OPTION_SECURITY_FLAGS:
2398 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2399 return ERROR_SUCCESS;
2401 case INTERNET_OPTION_VERSION: {
2402 static const INTERNET_VERSION_INFO info = { 1, 2 };
2404 TRACE("INTERNET_OPTION_VERSION\n");
2406 if (*size < sizeof(INTERNET_VERSION_INFO))
2407 return ERROR_INSUFFICIENT_BUFFER;
2409 memcpy(buffer, &info, sizeof(info));
2410 *size = sizeof(info);
2412 return ERROR_SUCCESS;
2415 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2416 WCHAR *url;
2417 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2418 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2419 DWORD res = ERROR_SUCCESS, i;
2420 proxyinfo_t pi;
2421 LONG ret;
2423 TRACE("Getting global proxy info\n");
2424 if((ret = INTERNET_LoadProxySettings(&pi)))
2425 return ret;
2427 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2429 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2430 FreeProxyInfo(&pi);
2431 return ERROR_INSUFFICIENT_BUFFER;
2434 url = get_proxy_autoconfig_url();
2436 for (i = 0; i < con->dwOptionCount; i++) {
2437 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2438 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2440 switch (optionW->dwOption) {
2441 case INTERNET_PER_CONN_FLAGS:
2442 if(pi.proxyEnabled)
2443 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2444 else
2445 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2446 if (url)
2447 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2448 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2449 break;
2451 case INTERNET_PER_CONN_PROXY_SERVER:
2452 if (unicode)
2453 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2454 else
2455 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2456 break;
2458 case INTERNET_PER_CONN_PROXY_BYPASS:
2459 if (unicode)
2460 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2461 else
2462 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2463 break;
2465 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2466 if (!url)
2467 optionW->Value.pszValue = NULL;
2468 else if (unicode)
2469 optionW->Value.pszValue = heap_strdupW(url);
2470 else
2471 optionA->Value.pszValue = heap_strdupWtoA(url);
2472 break;
2474 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2475 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2476 break;
2478 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2479 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2480 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2481 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2482 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2483 memset(&optionW->Value, 0, sizeof(optionW->Value));
2484 break;
2486 default:
2487 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2488 res = ERROR_INVALID_PARAMETER;
2489 break;
2492 heap_free(url);
2493 FreeProxyInfo(&pi);
2495 return res;
2497 case INTERNET_OPTION_REQUEST_FLAGS:
2498 case INTERNET_OPTION_USER_AGENT:
2499 *size = 0;
2500 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2501 case INTERNET_OPTION_POLICY:
2502 return ERROR_INVALID_PARAMETER;
2503 case INTERNET_OPTION_CONNECT_TIMEOUT:
2504 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2506 if (*size < sizeof(ULONG))
2507 return ERROR_INSUFFICIENT_BUFFER;
2509 *(ULONG*)buffer = connect_timeout;
2510 *size = sizeof(ULONG);
2512 return ERROR_SUCCESS;
2515 FIXME("Stub for %d\n", option);
2516 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2519 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2521 switch(option) {
2522 case INTERNET_OPTION_CONTEXT_VALUE:
2523 if (!size)
2524 return ERROR_INVALID_PARAMETER;
2526 if (*size < sizeof(DWORD_PTR)) {
2527 *size = sizeof(DWORD_PTR);
2528 return ERROR_INSUFFICIENT_BUFFER;
2530 if (!buffer)
2531 return ERROR_INVALID_PARAMETER;
2533 *(DWORD_PTR *)buffer = hdr->dwContext;
2534 *size = sizeof(DWORD_PTR);
2535 return ERROR_SUCCESS;
2537 case INTERNET_OPTION_REQUEST_FLAGS:
2538 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2539 *size = sizeof(DWORD);
2540 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2542 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2543 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2544 WARN("Called on global option %u\n", option);
2545 return ERROR_INTERNET_INVALID_OPERATION;
2548 /* FIXME: we shouldn't call it here */
2549 return query_global_option(option, buffer, size, unicode);
2552 /***********************************************************************
2553 * InternetQueryOptionW (WININET.@)
2555 * Queries an options on the specified handle
2557 * RETURNS
2558 * TRUE on success
2559 * FALSE on failure
2562 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2563 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2565 object_header_t *hdr;
2566 DWORD res = ERROR_INVALID_HANDLE;
2568 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2570 if(hInternet) {
2571 hdr = get_handle_object(hInternet);
2572 if (hdr) {
2573 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2574 WININET_Release(hdr);
2576 }else {
2577 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2580 if(res != ERROR_SUCCESS)
2581 SetLastError(res);
2582 return res == ERROR_SUCCESS;
2585 /***********************************************************************
2586 * InternetQueryOptionA (WININET.@)
2588 * Queries an options on the specified handle
2590 * RETURNS
2591 * TRUE on success
2592 * FALSE on failure
2595 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2596 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2598 object_header_t *hdr;
2599 DWORD res = ERROR_INVALID_HANDLE;
2601 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2603 if(hInternet) {
2604 hdr = get_handle_object(hInternet);
2605 if (hdr) {
2606 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2607 WININET_Release(hdr);
2609 }else {
2610 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2613 if(res != ERROR_SUCCESS)
2614 SetLastError(res);
2615 return res == ERROR_SUCCESS;
2618 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2620 switch(option) {
2621 case INTERNET_OPTION_SETTINGS_CHANGED:
2622 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2623 collect_connections(COLLECT_CONNECTIONS);
2624 return ERROR_SUCCESS;
2625 case INTERNET_OPTION_CALLBACK:
2626 WARN("Not settable option %u\n", option);
2627 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2628 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2629 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2630 WARN("Called on global option %u\n", option);
2631 return ERROR_INTERNET_INVALID_OPERATION;
2632 case INTERNET_OPTION_REFRESH:
2633 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2636 return ERROR_INTERNET_INVALID_OPTION;
2639 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2641 switch(option) {
2642 case INTERNET_OPTION_CALLBACK:
2643 WARN("Not global option %u\n", option);
2644 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2646 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2647 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2649 if(size != sizeof(max_conns))
2650 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2651 if(!*(ULONG*)buf)
2652 return ERROR_BAD_ARGUMENTS;
2654 max_conns = *(ULONG*)buf;
2655 return ERROR_SUCCESS;
2657 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2658 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2660 if(size != sizeof(max_1_0_conns))
2661 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2662 if(!*(ULONG*)buf)
2663 return ERROR_BAD_ARGUMENTS;
2665 max_1_0_conns = *(ULONG*)buf;
2666 return ERROR_SUCCESS;
2668 case INTERNET_OPTION_CONNECT_TIMEOUT:
2669 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2671 if(size != sizeof(connect_timeout))
2672 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2673 if(!*(ULONG*)buf)
2674 return ERROR_BAD_ARGUMENTS;
2676 connect_timeout = *(ULONG*)buf;
2677 return ERROR_SUCCESS;
2679 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2680 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2682 if(size != sizeof(ULONG))
2683 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2685 FIXME("%08x\n", *(ULONG*)buf);
2686 return ERROR_SUCCESS;
2689 return INET_SetOption(NULL, option, buf, size);
2692 /***********************************************************************
2693 * InternetSetOptionW (WININET.@)
2695 * Sets an options on the specified handle
2697 * RETURNS
2698 * TRUE on success
2699 * FALSE on failure
2702 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2703 LPVOID lpBuffer, DWORD dwBufferLength)
2705 object_header_t *lpwhh;
2706 BOOL ret = TRUE;
2707 DWORD res;
2709 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2711 lpwhh = (object_header_t*) get_handle_object( hInternet );
2712 if(lpwhh)
2713 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2714 else
2715 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2717 if(res != ERROR_INTERNET_INVALID_OPTION) {
2718 if(lpwhh)
2719 WININET_Release(lpwhh);
2721 if(res != ERROR_SUCCESS)
2722 SetLastError(res);
2724 return res == ERROR_SUCCESS;
2727 switch (dwOption)
2729 case INTERNET_OPTION_HTTP_VERSION:
2731 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2732 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2734 break;
2735 case INTERNET_OPTION_ERROR_MASK:
2737 if(!lpwhh) {
2738 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2739 return FALSE;
2740 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2741 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2742 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2743 SetLastError(ERROR_INVALID_PARAMETER);
2744 ret = FALSE;
2745 } else if(dwBufferLength != sizeof(ULONG)) {
2746 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2747 ret = FALSE;
2748 } else
2749 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2750 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2752 break;
2753 case INTERNET_OPTION_PROXY:
2755 INTERNET_PROXY_INFOW *info = lpBuffer;
2757 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2759 SetLastError(ERROR_INVALID_PARAMETER);
2760 return FALSE;
2762 if (!hInternet)
2764 EnterCriticalSection( &WININET_cs );
2765 free_global_proxy();
2766 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2767 if (global_proxy)
2769 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2771 global_proxy->proxyEnabled = 1;
2772 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2773 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2775 else
2777 global_proxy->proxyEnabled = 0;
2778 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2781 LeaveCriticalSection( &WININET_cs );
2783 else
2785 /* In general, each type of object should handle
2786 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2787 * get silently dropped.
2789 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2790 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2791 ret = FALSE;
2793 break;
2795 case INTERNET_OPTION_CODEPAGE:
2797 ULONG codepage = *(ULONG *)lpBuffer;
2798 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2800 break;
2801 case INTERNET_OPTION_REQUEST_PRIORITY:
2803 ULONG priority = *(ULONG *)lpBuffer;
2804 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2806 break;
2807 case INTERNET_OPTION_CONNECT_TIMEOUT:
2809 ULONG connecttimeout = *(ULONG *)lpBuffer;
2810 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2812 break;
2813 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2815 ULONG receivetimeout = *(ULONG *)lpBuffer;
2816 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2818 break;
2819 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2820 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2821 break;
2822 case INTERNET_OPTION_END_BROWSER_SESSION:
2823 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: semi-stub\n");
2824 free_cookie();
2825 break;
2826 case INTERNET_OPTION_CONNECTED_STATE:
2827 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2828 break;
2829 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2830 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2831 break;
2832 case INTERNET_OPTION_SEND_TIMEOUT:
2833 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2834 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2836 ULONG timeout = *(ULONG *)lpBuffer;
2837 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2838 break;
2840 case INTERNET_OPTION_CONNECT_RETRIES:
2842 ULONG retries = *(ULONG *)lpBuffer;
2843 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2844 break;
2846 case INTERNET_OPTION_CONTEXT_VALUE:
2848 if (!lpwhh)
2850 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2851 return FALSE;
2853 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2855 SetLastError(ERROR_INVALID_PARAMETER);
2856 ret = FALSE;
2858 else
2859 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2860 break;
2862 case INTERNET_OPTION_SECURITY_FLAGS:
2863 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2864 break;
2865 case INTERNET_OPTION_DISABLE_AUTODIAL:
2866 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2867 break;
2868 case INTERNET_OPTION_HTTP_DECODING:
2869 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2870 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2871 ret = FALSE;
2872 break;
2873 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2874 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2875 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2876 ret = FALSE;
2877 break;
2878 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2879 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2880 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2881 ret = FALSE;
2882 break;
2883 case INTERNET_OPTION_CODEPAGE_PATH:
2884 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2885 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2886 ret = FALSE;
2887 break;
2888 case INTERNET_OPTION_CODEPAGE_EXTRA:
2889 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2890 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2891 ret = FALSE;
2892 break;
2893 case INTERNET_OPTION_IDN:
2894 FIXME("INTERNET_OPTION_IDN; STUB\n");
2895 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2896 ret = FALSE;
2897 break;
2898 case INTERNET_OPTION_POLICY:
2899 SetLastError(ERROR_INVALID_PARAMETER);
2900 ret = FALSE;
2901 break;
2902 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2903 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2904 LONG res;
2905 unsigned int i;
2906 proxyinfo_t pi;
2908 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2910 for (i = 0; i < con->dwOptionCount; i++) {
2911 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2913 switch (option->dwOption) {
2914 case INTERNET_PER_CONN_PROXY_SERVER:
2915 heap_free(pi.proxy);
2916 pi.proxy = heap_strdupW(option->Value.pszValue);
2917 break;
2919 case INTERNET_PER_CONN_FLAGS:
2920 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2921 pi.proxyEnabled = 1;
2922 else
2924 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2925 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2926 pi.proxyEnabled = 0;
2928 break;
2930 case INTERNET_PER_CONN_PROXY_BYPASS:
2931 heap_free(pi.proxyBypass);
2932 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2933 break;
2935 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2936 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2937 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2938 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2939 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2940 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2941 FIXME("Unhandled dwOption %d\n", option->dwOption);
2942 break;
2944 default:
2945 FIXME("Unknown dwOption %d\n", option->dwOption);
2946 SetLastError(ERROR_INVALID_PARAMETER);
2947 break;
2951 if ((res = INTERNET_SaveProxySettings(&pi)))
2952 SetLastError(res);
2954 FreeProxyInfo(&pi);
2956 ret = (res == ERROR_SUCCESS);
2957 break;
2959 default:
2960 FIXME("Option %d STUB\n",dwOption);
2961 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2962 ret = FALSE;
2963 break;
2966 if(lpwhh)
2967 WININET_Release( lpwhh );
2969 return ret;
2973 /***********************************************************************
2974 * InternetSetOptionA (WININET.@)
2976 * Sets an options on the specified handle.
2978 * RETURNS
2979 * TRUE on success
2980 * FALSE on failure
2983 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2984 LPVOID lpBuffer, DWORD dwBufferLength)
2986 LPVOID wbuffer;
2987 DWORD wlen;
2988 BOOL r;
2990 switch( dwOption )
2992 case INTERNET_OPTION_PROXY:
2994 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2995 LPINTERNET_PROXY_INFOW piw;
2996 DWORD proxlen, prbylen;
2997 LPWSTR prox, prby;
2999 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
3000 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
3001 wlen = sizeof(*piw) + proxlen + prbylen;
3002 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
3003 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
3004 piw->dwAccessType = pi->dwAccessType;
3005 prox = (LPWSTR) &piw[1];
3006 prby = &prox[proxlen+1];
3007 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
3008 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
3009 piw->lpszProxy = prox;
3010 piw->lpszProxyBypass = prby;
3012 break;
3013 case INTERNET_OPTION_USER_AGENT:
3014 case INTERNET_OPTION_USERNAME:
3015 case INTERNET_OPTION_PASSWORD:
3016 case INTERNET_OPTION_PROXY_USERNAME:
3017 case INTERNET_OPTION_PROXY_PASSWORD:
3018 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3019 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3020 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3021 break;
3022 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3023 unsigned int i;
3024 INTERNET_PER_CONN_OPTION_LISTW *listW;
3025 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3026 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3027 wbuffer = heap_alloc(wlen);
3028 listW = wbuffer;
3030 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3031 if (listA->pszConnection)
3033 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3034 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3035 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3037 else
3038 listW->pszConnection = NULL;
3039 listW->dwOptionCount = listA->dwOptionCount;
3040 listW->dwOptionError = listA->dwOptionError;
3041 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3043 for (i = 0; i < listA->dwOptionCount; ++i) {
3044 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3045 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3047 optW->dwOption = optA->dwOption;
3049 switch (optA->dwOption) {
3050 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3051 case INTERNET_PER_CONN_PROXY_BYPASS:
3052 case INTERNET_PER_CONN_PROXY_SERVER:
3053 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3054 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3055 if (optA->Value.pszValue)
3057 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3058 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3059 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3061 else
3062 optW->Value.pszValue = NULL;
3063 break;
3064 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3065 case INTERNET_PER_CONN_FLAGS:
3066 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3067 optW->Value.dwValue = optA->Value.dwValue;
3068 break;
3069 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3070 optW->Value.ftValue = optA->Value.ftValue;
3071 break;
3072 default:
3073 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3074 optW->Value.dwValue = optA->Value.dwValue;
3075 break;
3079 break;
3080 default:
3081 wbuffer = lpBuffer;
3082 wlen = dwBufferLength;
3085 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3087 if( lpBuffer != wbuffer )
3089 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3091 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3092 unsigned int i;
3093 for (i = 0; i < list->dwOptionCount; ++i) {
3094 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3095 switch (opt->dwOption) {
3096 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3097 case INTERNET_PER_CONN_PROXY_BYPASS:
3098 case INTERNET_PER_CONN_PROXY_SERVER:
3099 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3100 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3101 heap_free( opt->Value.pszValue );
3102 break;
3103 default:
3104 break;
3107 heap_free( list->pOptions );
3109 heap_free( wbuffer );
3112 return r;
3116 /***********************************************************************
3117 * InternetSetOptionExA (WININET.@)
3119 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3120 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3122 FIXME("Flags %08x ignored\n", dwFlags);
3123 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3126 /***********************************************************************
3127 * InternetSetOptionExW (WININET.@)
3129 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3130 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3132 FIXME("Flags %08x ignored\n", dwFlags);
3133 if( dwFlags & ~ISO_VALID_FLAGS )
3135 SetLastError( ERROR_INVALID_PARAMETER );
3136 return FALSE;
3138 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3141 static const WCHAR WININET_wkday[7][4] =
3142 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3143 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3144 static const WCHAR WININET_month[12][4] =
3145 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3146 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3147 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3149 /***********************************************************************
3150 * InternetTimeFromSystemTimeA (WININET.@)
3152 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3154 BOOL ret;
3155 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3157 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3159 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3161 SetLastError(ERROR_INVALID_PARAMETER);
3162 return FALSE;
3165 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3167 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3168 return FALSE;
3171 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3172 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3174 return ret;
3177 /***********************************************************************
3178 * InternetTimeFromSystemTimeW (WININET.@)
3180 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3182 static const WCHAR date[] =
3183 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3184 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3186 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3188 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3190 SetLastError(ERROR_INVALID_PARAMETER);
3191 return FALSE;
3194 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3196 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3197 return FALSE;
3200 sprintfW( string, date,
3201 WININET_wkday[time->wDayOfWeek],
3202 time->wDay,
3203 WININET_month[time->wMonth - 1],
3204 time->wYear,
3205 time->wHour,
3206 time->wMinute,
3207 time->wSecond );
3209 return TRUE;
3212 /***********************************************************************
3213 * InternetTimeToSystemTimeA (WININET.@)
3215 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3217 BOOL ret = FALSE;
3218 WCHAR *stringW;
3220 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3222 stringW = heap_strdupAtoW(string);
3223 if (stringW)
3225 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3226 heap_free( stringW );
3228 return ret;
3231 /***********************************************************************
3232 * InternetTimeToSystemTimeW (WININET.@)
3234 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3236 unsigned int i;
3237 const WCHAR *s = string;
3238 WCHAR *end;
3240 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3242 if (!string || !time) return FALSE;
3244 /* Windows does this too */
3245 GetSystemTime( time );
3247 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3248 * a SYSTEMTIME structure.
3251 while (*s && !isalphaW( *s )) s++;
3252 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3253 time->wDayOfWeek = 7;
3255 for (i = 0; i < 7; i++)
3257 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3258 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3259 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3261 time->wDayOfWeek = i;
3262 break;
3266 if (time->wDayOfWeek > 6) return TRUE;
3267 while (*s && !isdigitW( *s )) s++;
3268 time->wDay = strtolW( s, &end, 10 );
3269 s = end;
3271 while (*s && !isalphaW( *s )) s++;
3272 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3273 time->wMonth = 0;
3275 for (i = 0; i < 12; i++)
3277 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3278 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3279 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3281 time->wMonth = i + 1;
3282 break;
3285 if (time->wMonth == 0) return TRUE;
3287 while (*s && !isdigitW( *s )) s++;
3288 if (*s == '\0') return TRUE;
3289 time->wYear = strtolW( s, &end, 10 );
3290 s = end;
3292 while (*s && !isdigitW( *s )) s++;
3293 if (*s == '\0') return TRUE;
3294 time->wHour = strtolW( s, &end, 10 );
3295 s = end;
3297 while (*s && !isdigitW( *s )) s++;
3298 if (*s == '\0') return TRUE;
3299 time->wMinute = strtolW( s, &end, 10 );
3300 s = end;
3302 while (*s && !isdigitW( *s )) s++;
3303 if (*s == '\0') return TRUE;
3304 time->wSecond = strtolW( s, &end, 10 );
3305 s = end;
3307 time->wMilliseconds = 0;
3308 return TRUE;
3311 /***********************************************************************
3312 * InternetCheckConnectionW (WININET.@)
3314 * Pings a requested host to check internet connection
3316 * RETURNS
3317 * TRUE on success and FALSE on failure. If a failure then
3318 * ERROR_NOT_CONNECTED is placed into GetLastError
3321 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3324 * this is a kludge which runs the resident ping program and reads the output.
3326 * Anyone have a better idea?
3329 BOOL rc = FALSE;
3330 static const CHAR ping[] = "ping -c 1 ";
3331 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3332 WCHAR *host;
3333 DWORD len, host_len;
3334 INTERNET_PORT port;
3335 int status = -1;
3337 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3340 * Crack or set the Address
3342 if (lpszUrl == NULL)
3345 * According to the doc we are supposed to use the ip for the next
3346 * server in the WnInet internal server database. I have
3347 * no idea what that is or how to get it.
3349 * So someone needs to implement this.
3351 FIXME("Unimplemented with URL of NULL\n");
3352 return TRUE;
3354 else
3356 URL_COMPONENTSW components = {sizeof(components)};
3358 components.dwHostNameLength = 1;
3360 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3361 goto End;
3363 host = components.lpszHostName;
3364 host_len = components.dwHostNameLength;
3365 port = components.nPort;
3366 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3369 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3371 struct sockaddr_storage saddr;
3372 int sa_len = sizeof(saddr);
3373 WCHAR *host_z;
3374 int fd;
3375 BOOL b;
3377 host_z = heap_strndupW(host, host_len);
3378 if (!host_z)
3379 return FALSE;
3381 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3382 heap_free(host_z);
3383 if(!b)
3384 goto End;
3385 init_winsock();
3386 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3387 if (fd != -1)
3389 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3390 rc = TRUE;
3391 closesocket(fd);
3394 else
3397 * Build our ping command
3399 char *command;
3401 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3402 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3403 strcpy(command, ping);
3404 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3405 strcpy(command+sizeof(ping)-1+len, redirect);
3407 TRACE("Ping command is : %s\n",command);
3409 status = system(command);
3410 heap_free( command );
3412 TRACE("Ping returned a code of %i\n",status);
3414 /* Ping return code of 0 indicates success */
3415 if (status == 0)
3416 rc = TRUE;
3419 End:
3420 if (rc == FALSE)
3421 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3423 return rc;
3427 /***********************************************************************
3428 * InternetCheckConnectionA (WININET.@)
3430 * Pings a requested host to check internet connection
3432 * RETURNS
3433 * TRUE on success and FALSE on failure. If a failure then
3434 * ERROR_NOT_CONNECTED is placed into GetLastError
3437 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3439 WCHAR *url = NULL;
3440 BOOL rc;
3442 if(lpszUrl) {
3443 url = heap_strdupAtoW(lpszUrl);
3444 if(!url)
3445 return FALSE;
3448 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3450 heap_free(url);
3451 return rc;
3455 /**********************************************************
3456 * INTERNET_InternetOpenUrlW (internal)
3458 * Opens an URL
3460 * RETURNS
3461 * handle of connection or NULL on failure
3463 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3464 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3466 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3467 WCHAR *host, *user = NULL, *pass = NULL, *path;
3468 HINTERNET client = NULL, client1 = NULL;
3469 DWORD res;
3471 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3472 dwHeadersLength, dwFlags, dwContext);
3474 urlComponents.dwHostNameLength = 1;
3475 urlComponents.dwUserNameLength = 1;
3476 urlComponents.dwPasswordLength = 1;
3477 urlComponents.dwUrlPathLength = 1;
3478 urlComponents.dwExtraInfoLength = 1;
3479 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3480 return NULL;
3482 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
3483 urlComponents.dwExtraInfoLength)
3485 assert(urlComponents.lpszUrlPath + urlComponents.dwUrlPathLength == urlComponents.lpszExtraInfo);
3486 urlComponents.dwUrlPathLength += urlComponents.dwExtraInfoLength;
3489 host = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
3490 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
3491 if(urlComponents.dwUserNameLength)
3492 user = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
3493 if(urlComponents.dwPasswordLength)
3494 pass = heap_strndupW(urlComponents.lpszPassword, urlComponents.dwPasswordLength);
3496 switch(urlComponents.nScheme) {
3497 case INTERNET_SCHEME_FTP:
3498 client = FTP_Connect(hIC, host, urlComponents.nPort,
3499 user, pass, dwFlags, dwContext, INET_OPENURL);
3500 if(client == NULL)
3501 break;
3502 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3503 if(client1 == NULL) {
3504 InternetCloseHandle(client);
3505 break;
3507 break;
3509 case INTERNET_SCHEME_HTTP:
3510 case INTERNET_SCHEME_HTTPS: {
3511 static const WCHAR szStars[] = { '*','/','*', 0 };
3512 LPCWSTR accept[2] = { szStars, NULL };
3514 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3516 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3517 res = HTTP_Connect(hIC, host, urlComponents.nPort,
3518 user, pass, dwFlags, dwContext, INET_OPENURL, &client);
3519 if(res != ERROR_SUCCESS) {
3520 INTERNET_SetLastError(res);
3521 break;
3524 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3525 if(client1 == NULL) {
3526 InternetCloseHandle(client);
3527 break;
3529 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3530 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3531 GetLastError() != ERROR_IO_PENDING) {
3532 InternetCloseHandle(client1);
3533 client1 = NULL;
3534 break;
3537 case INTERNET_SCHEME_GOPHER:
3538 /* gopher doesn't seem to be implemented in wine, but it's supposed
3539 * to be supported by InternetOpenUrlA. */
3540 default:
3541 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3542 break;
3545 TRACE(" %p <--\n", client1);
3547 heap_free(host);
3548 heap_free(path);
3549 heap_free(user);
3550 heap_free(pass);
3551 return client1;
3554 /**********************************************************
3555 * InternetOpenUrlW (WININET.@)
3557 * Opens an URL
3559 * RETURNS
3560 * handle of connection or NULL on failure
3562 typedef struct {
3563 task_header_t hdr;
3564 WCHAR *url;
3565 WCHAR *headers;
3566 DWORD headers_len;
3567 DWORD flags;
3568 DWORD_PTR context;
3569 } open_url_task_t;
3571 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3573 open_url_task_t *task = (open_url_task_t*)hdr;
3575 TRACE("%p\n", task->hdr.hdr);
3577 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3578 task->headers_len, task->flags, task->context);
3579 heap_free(task->url);
3580 heap_free(task->headers);
3583 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3584 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3586 HINTERNET ret = NULL;
3587 appinfo_t *hIC = NULL;
3589 if (TRACE_ON(wininet)) {
3590 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3591 dwHeadersLength, dwFlags, dwContext);
3592 TRACE(" flags :");
3593 dump_INTERNET_FLAGS(dwFlags);
3596 if (!lpszUrl)
3598 SetLastError(ERROR_INVALID_PARAMETER);
3599 goto lend;
3602 hIC = (appinfo_t*)get_handle_object( hInternet );
3603 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3604 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3605 goto lend;
3608 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3609 open_url_task_t *task;
3611 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3612 task->url = heap_strdupW(lpszUrl);
3613 task->headers = heap_strdupW(lpszHeaders);
3614 task->headers_len = dwHeadersLength;
3615 task->flags = dwFlags;
3616 task->context = dwContext;
3618 INTERNET_AsyncCall(&task->hdr);
3619 SetLastError(ERROR_IO_PENDING);
3620 } else {
3621 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3624 lend:
3625 if( hIC )
3626 WININET_Release( &hIC->hdr );
3627 TRACE(" %p <--\n", ret);
3629 return ret;
3632 /**********************************************************
3633 * InternetOpenUrlA (WININET.@)
3635 * Opens an URL
3637 * RETURNS
3638 * handle of connection or NULL on failure
3640 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3641 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3643 HINTERNET rc = NULL;
3644 LPWSTR szUrl = NULL;
3645 WCHAR *headers = NULL;
3647 TRACE("\n");
3649 if(lpszUrl) {
3650 szUrl = heap_strdupAtoW(lpszUrl);
3651 if(!szUrl)
3652 return NULL;
3655 if(lpszHeaders) {
3656 headers = heap_strndupAtoW(lpszHeaders, dwHeadersLength, &dwHeadersLength);
3657 if(!headers) {
3658 heap_free(szUrl);
3659 return NULL;
3663 rc = InternetOpenUrlW(hInternet, szUrl, headers, dwHeadersLength, dwFlags, dwContext);
3665 heap_free(szUrl);
3666 heap_free(headers);
3667 return rc;
3671 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3673 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3675 if (lpwite)
3677 lpwite->dwError = 0;
3678 lpwite->response[0] = '\0';
3681 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3683 heap_free(lpwite);
3684 return NULL;
3686 return lpwite;
3690 /***********************************************************************
3691 * INTERNET_SetLastError (internal)
3693 * Set last thread specific error
3695 * RETURNS
3698 void INTERNET_SetLastError(DWORD dwError)
3700 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3702 if (!lpwite)
3703 lpwite = INTERNET_AllocThreadError();
3705 SetLastError(dwError);
3706 if(lpwite)
3707 lpwite->dwError = dwError;
3711 /***********************************************************************
3712 * INTERNET_GetLastError (internal)
3714 * Get last thread specific error
3716 * RETURNS
3719 DWORD INTERNET_GetLastError(void)
3721 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3722 if (!lpwite) return 0;
3723 /* TlsGetValue clears last error, so set it again here */
3724 SetLastError(lpwite->dwError);
3725 return lpwite->dwError;
3729 /***********************************************************************
3730 * INTERNET_WorkerThreadFunc (internal)
3732 * Worker thread execution function
3734 * RETURNS
3737 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3739 task_header_t *task = lpvParam;
3741 TRACE("\n");
3743 task->proc(task);
3744 WININET_Release(task->hdr);
3745 heap_free(task);
3747 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3749 heap_free(TlsGetValue(g_dwTlsErrIndex));
3750 TlsSetValue(g_dwTlsErrIndex, NULL);
3752 return TRUE;
3755 void *alloc_async_task(object_header_t *hdr, async_task_proc_t proc, size_t size)
3757 task_header_t *task;
3759 task = heap_alloc(size);
3760 if(!task)
3761 return NULL;
3763 task->hdr = WININET_AddRef(hdr);
3764 task->proc = proc;
3765 return task;
3768 /***********************************************************************
3769 * INTERNET_AsyncCall (internal)
3771 * Retrieves work request from queue
3773 * RETURNS
3776 DWORD INTERNET_AsyncCall(task_header_t *task)
3778 BOOL bSuccess;
3780 TRACE("\n");
3782 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, task, WT_EXECUTELONGFUNCTION);
3783 if (!bSuccess)
3785 heap_free(task);
3786 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3788 return ERROR_SUCCESS;
3792 /***********************************************************************
3793 * INTERNET_GetResponseBuffer (internal)
3795 * RETURNS
3798 LPSTR INTERNET_GetResponseBuffer(void)
3800 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3801 if (!lpwite)
3802 lpwite = INTERNET_AllocThreadError();
3803 TRACE("\n");
3804 return lpwite->response;
3807 /**********************************************************
3808 * InternetQueryDataAvailable (WININET.@)
3810 * Determines how much data is available to be read.
3812 * RETURNS
3813 * TRUE on success, FALSE if an error occurred. If
3814 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3815 * no data is presently available, FALSE is returned with
3816 * the last error ERROR_IO_PENDING; a callback with status
3817 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3818 * data is available.
3820 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3821 LPDWORD lpdwNumberOfBytesAvailable,
3822 DWORD dwFlags, DWORD_PTR dwContext)
3824 object_header_t *hdr;
3825 DWORD res;
3827 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3829 hdr = get_handle_object( hFile );
3830 if (!hdr) {
3831 SetLastError(ERROR_INVALID_HANDLE);
3832 return FALSE;
3835 if(hdr->vtbl->QueryDataAvailable) {
3836 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3837 }else {
3838 WARN("wrong handle\n");
3839 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3842 WININET_Release(hdr);
3844 if(res != ERROR_SUCCESS)
3845 SetLastError(res);
3846 return res == ERROR_SUCCESS;
3849 DWORD create_req_file(const WCHAR *file_name, req_file_t **ret)
3851 req_file_t *req_file;
3853 req_file = heap_alloc_zero(sizeof(*req_file));
3854 if(!req_file)
3855 return ERROR_NOT_ENOUGH_MEMORY;
3857 req_file->ref = 1;
3859 req_file->file_name = heap_strdupW(file_name);
3860 if(!req_file->file_name) {
3861 heap_free(req_file);
3862 return ERROR_NOT_ENOUGH_MEMORY;
3865 req_file->file_handle = CreateFileW(req_file->file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
3866 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3867 if(req_file->file_handle == INVALID_HANDLE_VALUE) {
3868 req_file_release(req_file);
3869 return GetLastError();
3872 *ret = req_file;
3873 return ERROR_SUCCESS;
3876 void req_file_release(req_file_t *req_file)
3878 if(InterlockedDecrement(&req_file->ref))
3879 return;
3881 if(!req_file->is_committed)
3882 DeleteFileW(req_file->file_name);
3883 if(req_file->file_handle && req_file->file_handle != INVALID_HANDLE_VALUE)
3884 CloseHandle(req_file->file_handle);
3885 heap_free(req_file->file_name);
3886 heap_free(req_file->url);
3887 heap_free(req_file);
3890 /***********************************************************************
3891 * InternetLockRequestFile (WININET.@)
3893 BOOL WINAPI InternetLockRequestFile(HINTERNET hInternet, HANDLE *lphLockReqHandle)
3895 req_file_t *req_file = NULL;
3896 object_header_t *hdr;
3897 DWORD res;
3899 TRACE("(%p %p)\n", hInternet, lphLockReqHandle);
3901 hdr = get_handle_object(hInternet);
3902 if (!hdr) {
3903 SetLastError(ERROR_INVALID_HANDLE);
3904 return FALSE;
3907 if(hdr->vtbl->LockRequestFile) {
3908 res = hdr->vtbl->LockRequestFile(hdr, &req_file);
3909 }else {
3910 WARN("wrong handle\n");
3911 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3914 WININET_Release(hdr);
3916 *lphLockReqHandle = req_file;
3917 if(res != ERROR_SUCCESS)
3918 SetLastError(res);
3919 return res == ERROR_SUCCESS;
3922 BOOL WINAPI InternetUnlockRequestFile(HANDLE hLockHandle)
3924 TRACE("(%p)\n", hLockHandle);
3926 req_file_release(hLockHandle);
3927 return TRUE;
3931 /***********************************************************************
3932 * InternetAutodial (WININET.@)
3934 * On windows this function is supposed to dial the default internet
3935 * connection. We don't want to have Wine dial out to the internet so
3936 * we return TRUE by default. It might be nice to check if we are connected.
3938 * RETURNS
3939 * TRUE on success
3940 * FALSE on failure
3943 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3945 FIXME("STUB\n");
3947 /* Tell that we are connected to the internet. */
3948 return TRUE;
3951 /***********************************************************************
3952 * InternetAutodialHangup (WININET.@)
3954 * Hangs up a connection made with InternetAutodial
3956 * PARAM
3957 * dwReserved
3958 * RETURNS
3959 * TRUE on success
3960 * FALSE on failure
3963 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3965 FIXME("STUB\n");
3967 /* we didn't dial, we don't disconnect */
3968 return TRUE;
3971 /***********************************************************************
3972 * InternetCombineUrlA (WININET.@)
3974 * Combine a base URL with a relative URL
3976 * RETURNS
3977 * TRUE on success
3978 * FALSE on failure
3982 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3983 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3984 DWORD dwFlags)
3986 HRESULT hr=S_OK;
3988 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3990 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3991 dwFlags ^= ICU_NO_ENCODE;
3992 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3994 return (hr==S_OK);
3997 /***********************************************************************
3998 * InternetCombineUrlW (WININET.@)
4000 * Combine a base URL with a relative URL
4002 * RETURNS
4003 * TRUE on success
4004 * FALSE on failure
4008 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
4009 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
4010 DWORD dwFlags)
4012 HRESULT hr=S_OK;
4014 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
4016 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
4017 dwFlags ^= ICU_NO_ENCODE;
4018 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4020 return (hr==S_OK);
4023 /* max port num is 65535 => 5 digits */
4024 #define MAX_WORD_DIGITS 5
4026 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
4027 (url)->dw##component##Length : strlenW((url)->lpsz##component))
4028 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
4029 (url)->dw##component##Length : strlen((url)->lpsz##component))
4031 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
4033 if ((nScheme == INTERNET_SCHEME_HTTP) &&
4034 (nPort == INTERNET_DEFAULT_HTTP_PORT))
4035 return TRUE;
4036 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
4037 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
4038 return TRUE;
4039 if ((nScheme == INTERNET_SCHEME_FTP) &&
4040 (nPort == INTERNET_DEFAULT_FTP_PORT))
4041 return TRUE;
4042 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
4043 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
4044 return TRUE;
4046 if (nPort == INTERNET_INVALID_PORT_NUMBER)
4047 return TRUE;
4049 return FALSE;
4052 /* opaque urls do not fit into the standard url hierarchy and don't have
4053 * two following slashes */
4054 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
4056 return (nScheme != INTERNET_SCHEME_FTP) &&
4057 (nScheme != INTERNET_SCHEME_GOPHER) &&
4058 (nScheme != INTERNET_SCHEME_HTTP) &&
4059 (nScheme != INTERNET_SCHEME_HTTPS) &&
4060 (nScheme != INTERNET_SCHEME_FILE);
4063 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
4065 int index;
4066 if (scheme < INTERNET_SCHEME_FIRST)
4067 return NULL;
4068 index = scheme - INTERNET_SCHEME_FIRST;
4069 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
4070 return NULL;
4071 return (LPCWSTR)url_schemes[index];
4074 /* we can calculate using ansi strings because we're just
4075 * calculating string length, not size
4077 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
4078 LPDWORD lpdwUrlLength)
4080 INTERNET_SCHEME nScheme;
4082 *lpdwUrlLength = 0;
4084 if (lpUrlComponents->lpszScheme)
4086 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4087 *lpdwUrlLength += dwLen;
4088 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4090 else
4092 LPCWSTR scheme;
4094 nScheme = lpUrlComponents->nScheme;
4096 if (nScheme == INTERNET_SCHEME_DEFAULT)
4097 nScheme = INTERNET_SCHEME_HTTP;
4098 scheme = INTERNET_GetSchemeString(nScheme);
4099 *lpdwUrlLength += strlenW(scheme);
4102 (*lpdwUrlLength)++; /* ':' */
4103 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4104 *lpdwUrlLength += strlen("//");
4106 if (lpUrlComponents->lpszUserName)
4108 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4109 *lpdwUrlLength += strlen("@");
4111 else
4113 if (lpUrlComponents->lpszPassword)
4115 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4116 return FALSE;
4120 if (lpUrlComponents->lpszPassword)
4122 *lpdwUrlLength += strlen(":");
4123 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4126 if (lpUrlComponents->lpszHostName)
4128 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4130 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4132 char szPort[MAX_WORD_DIGITS+1];
4134 *lpdwUrlLength += sprintf(szPort, "%d", lpUrlComponents->nPort);
4135 *lpdwUrlLength += strlen(":");
4138 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4139 (*lpdwUrlLength)++; /* '/' */
4142 if (lpUrlComponents->lpszUrlPath)
4143 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4145 if (lpUrlComponents->lpszExtraInfo)
4146 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4148 return TRUE;
4151 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4153 INT len;
4155 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4157 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4158 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4159 urlCompW->nScheme = lpUrlComponents->nScheme;
4160 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4161 urlCompW->nPort = lpUrlComponents->nPort;
4162 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4163 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4164 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4165 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4167 if (lpUrlComponents->lpszScheme)
4169 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4170 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4171 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4172 -1, urlCompW->lpszScheme, len);
4175 if (lpUrlComponents->lpszHostName)
4177 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4178 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4179 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4180 -1, urlCompW->lpszHostName, len);
4183 if (lpUrlComponents->lpszUserName)
4185 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4186 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4187 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4188 -1, urlCompW->lpszUserName, len);
4191 if (lpUrlComponents->lpszPassword)
4193 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4194 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4195 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4196 -1, urlCompW->lpszPassword, len);
4199 if (lpUrlComponents->lpszUrlPath)
4201 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4202 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4203 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4204 -1, urlCompW->lpszUrlPath, len);
4207 if (lpUrlComponents->lpszExtraInfo)
4209 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4210 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4211 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4212 -1, urlCompW->lpszExtraInfo, len);
4216 /***********************************************************************
4217 * InternetCreateUrlA (WININET.@)
4219 * See InternetCreateUrlW.
4221 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4222 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4224 BOOL ret;
4225 LPWSTR urlW = NULL;
4226 URL_COMPONENTSW urlCompW;
4228 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4230 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4232 SetLastError(ERROR_INVALID_PARAMETER);
4233 return FALSE;
4236 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4238 if (lpszUrl)
4239 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4241 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4243 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4244 *lpdwUrlLength /= sizeof(WCHAR);
4246 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4247 * minus one, so add one to leave room for NULL terminator
4249 if (ret)
4250 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4252 heap_free(urlCompW.lpszScheme);
4253 heap_free(urlCompW.lpszHostName);
4254 heap_free(urlCompW.lpszUserName);
4255 heap_free(urlCompW.lpszPassword);
4256 heap_free(urlCompW.lpszUrlPath);
4257 heap_free(urlCompW.lpszExtraInfo);
4258 heap_free(urlW);
4259 return ret;
4262 /***********************************************************************
4263 * InternetCreateUrlW (WININET.@)
4265 * Creates a URL from its component parts.
4267 * PARAMS
4268 * lpUrlComponents [I] URL Components.
4269 * dwFlags [I] Flags. See notes.
4270 * lpszUrl [I] Buffer in which to store the created URL.
4271 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4272 * lpszUrl in characters. On output, the number of bytes
4273 * required to store the URL including terminator.
4275 * NOTES
4277 * The dwFlags parameter can be zero or more of the following:
4278 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4280 * RETURNS
4281 * TRUE on success
4282 * FALSE on failure
4285 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4286 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4288 DWORD dwLen;
4289 INTERNET_SCHEME nScheme;
4291 static const WCHAR slashSlashW[] = {'/','/'};
4292 static const WCHAR fmtW[] = {'%','u',0};
4294 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4296 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4298 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4299 return FALSE;
4302 if (!calc_url_length(lpUrlComponents, &dwLen))
4303 return FALSE;
4305 if (!lpszUrl || *lpdwUrlLength < dwLen)
4307 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4308 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4309 return FALSE;
4312 *lpdwUrlLength = dwLen;
4313 lpszUrl[0] = 0x00;
4315 dwLen = 0;
4317 if (lpUrlComponents->lpszScheme)
4319 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4320 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4321 lpszUrl += dwLen;
4323 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4325 else
4327 LPCWSTR scheme;
4328 nScheme = lpUrlComponents->nScheme;
4330 if (nScheme == INTERNET_SCHEME_DEFAULT)
4331 nScheme = INTERNET_SCHEME_HTTP;
4333 scheme = INTERNET_GetSchemeString(nScheme);
4334 dwLen = strlenW(scheme);
4335 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4336 lpszUrl += dwLen;
4339 /* all schemes are followed by at least a colon */
4340 *lpszUrl = ':';
4341 lpszUrl++;
4343 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4345 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4346 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4349 if (lpUrlComponents->lpszUserName)
4351 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4352 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4353 lpszUrl += dwLen;
4355 if (lpUrlComponents->lpszPassword)
4357 *lpszUrl = ':';
4358 lpszUrl++;
4360 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4361 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4362 lpszUrl += dwLen;
4365 *lpszUrl = '@';
4366 lpszUrl++;
4369 if (lpUrlComponents->lpszHostName)
4371 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4372 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4373 lpszUrl += dwLen;
4375 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4377 *lpszUrl = ':';
4378 lpszUrl++;
4379 lpszUrl += sprintfW(lpszUrl, fmtW, lpUrlComponents->nPort);
4382 /* add slash between hostname and path if necessary */
4383 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4385 *lpszUrl = '/';
4386 lpszUrl++;
4390 if (lpUrlComponents->lpszUrlPath)
4392 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4393 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4394 lpszUrl += dwLen;
4397 if (lpUrlComponents->lpszExtraInfo)
4399 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4400 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4401 lpszUrl += dwLen;
4404 *lpszUrl = '\0';
4406 return TRUE;
4409 /***********************************************************************
4410 * InternetConfirmZoneCrossingA (WININET.@)
4413 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4415 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4416 return ERROR_SUCCESS;
4419 /***********************************************************************
4420 * InternetConfirmZoneCrossingW (WININET.@)
4423 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4425 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4426 return ERROR_SUCCESS;
4429 static DWORD zone_preference = 3;
4431 /***********************************************************************
4432 * PrivacySetZonePreferenceW (WININET.@)
4434 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4436 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4438 zone_preference = template;
4439 return 0;
4442 /***********************************************************************
4443 * PrivacyGetZonePreferenceW (WININET.@)
4445 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4446 LPWSTR preference, LPDWORD length )
4448 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4450 if (template) *template = zone_preference;
4451 return 0;
4454 /***********************************************************************
4455 * InternetGetSecurityInfoByURLA (WININET.@)
4457 BOOL WINAPI InternetGetSecurityInfoByURLA(LPSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4459 WCHAR *url;
4460 BOOL res;
4462 TRACE("(%s %p %p)\n", debugstr_a(lpszURL), ppCertChain, pdwSecureFlags);
4464 url = heap_strdupAtoW(lpszURL);
4465 if(!url)
4466 return FALSE;
4468 res = InternetGetSecurityInfoByURLW(url, ppCertChain, pdwSecureFlags);
4469 heap_free(url);
4470 return res;
4473 /***********************************************************************
4474 * InternetGetSecurityInfoByURLW (WININET.@)
4476 BOOL WINAPI InternetGetSecurityInfoByURLW(LPCWSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4478 URL_COMPONENTSW url = {sizeof(url)};
4479 server_t *server;
4480 BOOL res;
4482 TRACE("(%s %p %p)\n", debugstr_w(lpszURL), ppCertChain, pdwSecureFlags);
4484 if (!ppCertChain && !pdwSecureFlags) {
4485 SetLastError(ERROR_INVALID_PARAMETER);
4486 return FALSE;
4489 url.dwHostNameLength = 1;
4490 res = InternetCrackUrlW(lpszURL, 0, 0, &url);
4491 if(!res || url.nScheme != INTERNET_SCHEME_HTTPS) {
4492 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4493 return FALSE;
4496 server = get_server(substr(url.lpszHostName, url.dwHostNameLength), url.nPort, TRUE, FALSE);
4497 if(!server) {
4498 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4499 return FALSE;
4502 if(server->cert_chain) {
4503 if(pdwSecureFlags)
4504 *pdwSecureFlags = server->security_flags & _SECURITY_ERROR_FLAGS_MASK;
4506 if(ppCertChain && !(*ppCertChain = CertDuplicateCertificateChain(server->cert_chain)))
4507 res = FALSE;
4508 }else {
4509 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4510 res = FALSE;
4513 server_release(server);
4514 return res;
4517 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4518 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4520 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4521 lpdwConnection, dwReserved);
4522 return ERROR_SUCCESS;
4525 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4526 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4528 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4529 lpdwConnection, dwReserved);
4530 return ERROR_SUCCESS;
4533 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4535 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4536 return TRUE;
4539 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4541 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4542 return TRUE;
4545 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4547 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4548 return ERROR_SUCCESS;
4551 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4552 PBYTE pbHexHash )
4554 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4555 debugstr_w(pwszTarget), pbHexHash);
4556 return FALSE;
4559 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4561 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4562 return FALSE;
4565 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4567 FIXME("(%p, %08lx) stub\n", a, b);
4568 return FALSE;
4571 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4573 FIXME("%p: stub\n", parent);
4574 return 0;