mshtml: Populate dynamic properties table in get_dynamic_data.
[wine] / dlls / wininet / internet.c
1 /*
2  * Wininet
3  *
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
9  *
10  * Ulrich Czekalla
11  * Aric Stewart
12  * David Hammerton
13  *
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.
18  *
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.
23  *
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
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #define MAXHOSTNAME 100 /* from http.c */
33
34 #if defined(__MINGW32__) || defined (_MSC_VER)
35 #include <ws2tcpip.h>
36 #endif
37
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48 #ifdef HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #include <stdlib.h>
55 #include <ctype.h>
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #include <assert.h>
60
61 #include "windef.h"
62 #include "winbase.h"
63 #include "winreg.h"
64 #include "winuser.h"
65 #include "wininet.h"
66 #include "winineti.h"
67 #include "winnls.h"
68 #include "wine/debug.h"
69 #include "winerror.h"
70 #define NO_SHLWAPI_STREAM
71 #include "shlwapi.h"
72
73 #include "wine/exception.h"
74
75 #include "internet.h"
76 #include "resource.h"
77
78 #include "wine/unicode.h"
79
80 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
81
82 #define RESPONSE_TIMEOUT        30
83
84 typedef struct
85 {
86     DWORD  dwError;
87     CHAR   response[MAX_REPLY_LEN];
88 } WITHREADERROR, *LPWITHREADERROR;
89
90 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
91 HMODULE WININET_hModule;
92
93 static CRITICAL_SECTION WININET_cs;
94 static CRITICAL_SECTION_DEBUG WININET_cs_debug = 
95 {
96     0, 0, &WININET_cs,
97     { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
98       0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
99 };
100 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
101
102 static object_header_t **handle_table;
103 static UINT_PTR next_handle;
104 static UINT_PTR handle_table_size;
105
106 typedef struct
107 {
108     DWORD  proxyEnabled;
109     LPWSTR proxy;
110     LPWSTR proxyBypass;
111 } proxyinfo_t;
112
113 static const WCHAR szInternetSettings[] =
114     { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
115       'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
116       'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
117 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
118 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
119
120 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
121 {
122     UINT_PTR handle = 0, num;
123     object_header_t *ret;
124     object_header_t **p;
125     BOOL res = TRUE;
126
127     ret = heap_alloc_zero(size);
128     if(!ret)
129         return NULL;
130
131     list_init(&ret->children);
132
133     EnterCriticalSection( &WININET_cs );
134
135     if(!handle_table_size) {
136         num = 16;
137         p = heap_alloc_zero(sizeof(handle_table[0]) * num);
138         if(p) {
139             handle_table = p;
140             handle_table_size = num;
141             next_handle = 1;
142         }else {
143             res = FALSE;
144         }
145     }else if(next_handle == handle_table_size) {
146         num = handle_table_size * 2;
147         p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
148         if(p) {
149             handle_table = p;
150             handle_table_size = num;
151         }else {
152             res = FALSE;
153         }
154     }
155
156     if(res) {
157         handle = next_handle;
158         if(handle_table[handle])
159             ERR("handle isn't free but should be\n");
160         handle_table[handle] = ret;
161         ret->valid_handle = TRUE;
162
163         while(handle_table[next_handle] && next_handle < handle_table_size)
164             next_handle++;
165     }
166
167     LeaveCriticalSection( &WININET_cs );
168
169     if(!res) {
170         heap_free(ret);
171         return NULL;
172     }
173
174     ret->vtbl = vtbl;
175     ret->refs = 1;
176     ret->hInternet = (HINTERNET)handle;
177
178     if(parent) {
179         ret->lpfnStatusCB = parent->lpfnStatusCB;
180         ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
181     }
182
183     return ret;
184 }
185
186 object_header_t *WININET_AddRef( object_header_t *info )
187 {
188     ULONG refs = InterlockedIncrement(&info->refs);
189     TRACE("%p -> refcount = %d\n", info, refs );
190     return info;
191 }
192
193 object_header_t *get_handle_object( HINTERNET hinternet )
194 {
195     object_header_t *info = NULL;
196     UINT_PTR handle = (UINT_PTR) hinternet;
197
198     EnterCriticalSection( &WININET_cs );
199
200     if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
201         info = WININET_AddRef(handle_table[handle]);
202
203     LeaveCriticalSection( &WININET_cs );
204
205     TRACE("handle %ld -> %p\n", handle, info);
206
207     return info;
208 }
209
210 static void invalidate_handle(object_header_t *info)
211 {
212     object_header_t *child, *next;
213
214     if(!info->valid_handle)
215         return;
216     info->valid_handle = FALSE;
217
218     /* Free all children as native does */
219     LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
220     {
221         TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
222         invalidate_handle( child );
223     }
224
225     WININET_Release(info);
226 }
227
228 BOOL WININET_Release( object_header_t *info )
229 {
230     ULONG refs = InterlockedDecrement(&info->refs);
231     TRACE( "object %p refcount = %d\n", info, refs );
232     if( !refs )
233     {
234         invalidate_handle(info);
235         if ( info->vtbl->CloseConnection )
236         {
237             TRACE( "closing connection %p\n", info);
238             info->vtbl->CloseConnection( info );
239         }
240         /* Don't send a callback if this is a session handle created with InternetOpenUrl */
241         if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
242             || !(info->dwInternalFlags & INET_OPENURL))
243         {
244             INTERNET_SendCallback(info, info->dwContext,
245                                   INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
246                                   sizeof(HINTERNET));
247         }
248         TRACE( "destroying object %p\n", info);
249         if ( info->htype != WH_HINIT )
250             list_remove( &info->entry );
251         info->vtbl->Destroy( info );
252
253         if(info->hInternet) {
254             UINT_PTR handle = (UINT_PTR)info->hInternet;
255
256             EnterCriticalSection( &WININET_cs );
257
258             handle_table[handle] = NULL;
259             if(next_handle > handle)
260                 next_handle = handle;
261
262             LeaveCriticalSection( &WININET_cs );
263         }
264
265         heap_free(info);
266     }
267     return TRUE;
268 }
269
270 /***********************************************************************
271  * DllMain [Internal] Initializes the internal 'WININET.DLL'.
272  *
273  * PARAMS
274  *     hinstDLL    [I] handle to the DLL's instance
275  *     fdwReason   [I]
276  *     lpvReserved [I] reserved, must be NULL
277  *
278  * RETURNS
279  *     Success: TRUE
280  *     Failure: FALSE
281  */
282
283 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
284 {
285     TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
286
287     switch (fdwReason) {
288         case DLL_PROCESS_ATTACH:
289
290             g_dwTlsErrIndex = TlsAlloc();
291
292             if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
293                 return FALSE;
294
295             URLCacheContainers_CreateDefaults();
296
297             WININET_hModule = hinstDLL;
298
299         case DLL_THREAD_ATTACH:
300             break;
301
302         case DLL_THREAD_DETACH:
303             if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
304             {
305                 heap_free(TlsGetValue(g_dwTlsErrIndex));
306             }
307             break;
308
309         case DLL_PROCESS_DETACH:
310             collect_connections(TRUE);
311             NETCON_unload();
312             URLCacheContainers_DeleteAll();
313
314             if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
315             {
316                 heap_free(TlsGetValue(g_dwTlsErrIndex));
317                 TlsFree(g_dwTlsErrIndex);
318             }
319             break;
320     }
321     return TRUE;
322 }
323
324 /***********************************************************************
325  *           INTERNET_SaveProxySettings
326  *
327  * Stores the proxy settings given by lpwai into the registry
328  *
329  * RETURNS
330  *     ERROR_SUCCESS if no error, or error code on fail
331  */
332 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
333 {
334     HKEY key;
335     LONG ret;
336
337     if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
338         return ret;
339
340     if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
341     {
342         RegCloseKey( key );
343         return ret;
344     }
345
346     if (lpwpi->proxy)
347     {
348         if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
349         {
350             RegCloseKey( key );
351             return ret;
352         }
353     }
354     else
355     {
356         if ((ret = RegDeleteValueW( key, szProxyServer )))
357         {
358             RegCloseKey( key );
359             return ret;
360         }
361     }
362
363     RegCloseKey(key);
364     return ERROR_SUCCESS;
365 }
366
367 /***********************************************************************
368  *           INTERNET_FindProxyForProtocol
369  *
370  * Searches the proxy string for a proxy of the given protocol.
371  * Returns the found proxy, or the default proxy if none of the given
372  * protocol is found.
373  *
374  * PARAMETERS
375  *     szProxy       [In]     proxy string to search
376  *     proto         [In]     protocol to search for, e.g. "http"
377  *     foundProxy    [Out]    found proxy
378  *     foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
379  *
380  * RETURNS
381  *     TRUE if a proxy is found, FALSE if not.  If foundProxy is too short,
382  *     *foundProxyLen is set to the required size in WCHARs, including the
383  *     NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
384  */
385 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen)
386 {
387     LPCWSTR ptr;
388     BOOL ret = FALSE;
389
390     TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
391
392     /* First, look for the specified protocol (proto=scheme://host:port) */
393     for (ptr = szProxy; !ret && ptr && *ptr; )
394     {
395         LPCWSTR end, equal;
396
397         if (!(end = strchrW(ptr, ' ')))
398             end = ptr + strlenW(ptr);
399         if ((equal = strchrW(ptr, '=')) && equal < end &&
400              equal - ptr == strlenW(proto) &&
401              !strncmpiW(proto, ptr, strlenW(proto)))
402         {
403             if (end - equal > *foundProxyLen)
404             {
405                 WARN("buffer too short for %s\n",
406                      debugstr_wn(equal + 1, end - equal - 1));
407                 *foundProxyLen = end - equal;
408                 SetLastError(ERROR_INSUFFICIENT_BUFFER);
409             }
410             else
411             {
412                 memcpy(foundProxy, equal + 1, (end - equal) * sizeof(WCHAR));
413                 foundProxy[end - equal] = 0;
414                 ret = TRUE;
415             }
416         }
417         if (*end == ' ')
418             ptr = end + 1;
419         else
420             ptr = end;
421     }
422     if (!ret)
423     {
424         /* It wasn't found: look for no protocol */
425         for (ptr = szProxy; !ret && ptr && *ptr; )
426         {
427             LPCWSTR end, equal;
428
429             if (!(end = strchrW(ptr, ' ')))
430                 end = ptr + strlenW(ptr);
431             if (!(equal = strchrW(ptr, '=')))
432             {
433                 if (end - ptr + 1 > *foundProxyLen)
434                 {
435                     WARN("buffer too short for %s\n",
436                          debugstr_wn(ptr, end - ptr));
437                     *foundProxyLen = end - ptr + 1;
438                     SetLastError(ERROR_INSUFFICIENT_BUFFER);
439                 }
440                 else
441                 {
442                     memcpy(foundProxy, ptr, (end - ptr) * sizeof(WCHAR));
443                     foundProxy[end - ptr] = 0;
444                     ret = TRUE;
445                 }
446             }
447             if (*end == ' ')
448                 ptr = end + 1;
449             else
450                 ptr = end;
451         }
452     }
453     if (ret)
454         TRACE("found proxy for %s: %s\n", debugstr_w(proto),
455               debugstr_w(foundProxy));
456     return ret;
457 }
458
459 /***********************************************************************
460  *           InternetInitializeAutoProxyDll   (WININET.@)
461  *
462  * Setup the internal proxy
463  *
464  * PARAMETERS
465  *     dwReserved
466  *
467  * RETURNS
468  *     FALSE on failure
469  *
470  */
471 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
472 {
473     FIXME("STUB\n");
474     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
475     return FALSE;
476 }
477
478 /***********************************************************************
479  *           DetectAutoProxyUrl   (WININET.@)
480  *
481  * Auto detect the proxy url
482  *
483  * RETURNS
484  *     FALSE on failure
485  *
486  */
487 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
488         DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
489 {
490     FIXME("STUB\n");
491     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
492     return FALSE;
493 }
494
495 static void FreeProxyInfo( proxyinfo_t *lpwpi )
496 {
497     heap_free(lpwpi->proxy);
498     heap_free(lpwpi->proxyBypass);
499 }
500
501 static proxyinfo_t *global_proxy;
502
503 static void free_global_proxy( void )
504 {
505     EnterCriticalSection( &WININET_cs );
506     if (global_proxy)
507     {
508         FreeProxyInfo( global_proxy );
509         heap_free( global_proxy );
510     }
511     LeaveCriticalSection( &WININET_cs );
512 }
513
514 /***********************************************************************
515  *          INTERNET_LoadProxySettings
516  *
517  * Loads proxy information from process-wide global settings, the registry,
518  * or the environment into lpwpi.
519  *
520  * The caller should call FreeProxyInfo when done with lpwpi.
521  *
522  * FIXME:
523  * The proxy may be specified in the form 'http=proxy.my.org'
524  * Presumably that means there can be ftp=ftpproxy.my.org too.
525  */
526 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
527 {
528     HKEY key;
529     DWORD type, len;
530     LPCSTR envproxy;
531     LONG ret;
532
533     EnterCriticalSection( &WININET_cs );
534     if (global_proxy)
535     {
536         lpwpi->proxyEnabled = global_proxy->proxyEnabled;
537         lpwpi->proxy = heap_strdupW( global_proxy->proxy );
538         lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
539     }
540     LeaveCriticalSection( &WININET_cs );
541
542     if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
543         return ret;
544
545     len = sizeof(DWORD);
546     if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
547     {
548         lpwpi->proxyEnabled = 0;
549         if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
550         {
551             RegCloseKey( key );
552             return ret;
553         }
554     }
555
556     if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
557     {
558         TRACE("Proxy is enabled.\n");
559
560         /* figure out how much memory the proxy setting takes */
561         if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
562         {
563             LPWSTR szProxy, p;
564             static const WCHAR szHttp[] = {'h','t','t','p','=',0};
565
566             if (!(szProxy = heap_alloc(len)))
567             {
568                 RegCloseKey( key );
569                 return ERROR_OUTOFMEMORY;
570             }
571             RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
572
573             /* find the http proxy, and strip away everything else */
574             p = strstrW( szProxy, szHttp );
575             if (p)
576             {
577                 p += lstrlenW( szHttp );
578                 lstrcpyW( szProxy, p );
579             }
580             p = strchrW( szProxy, ' ' );
581             if (p) *p = 0;
582
583             lpwpi->proxy = szProxy;
584
585             TRACE("http proxy = %s\n", debugstr_w(lpwpi->proxy));
586         }
587         else
588         {
589             TRACE("No proxy server settings in registry.\n");
590             lpwpi->proxy = NULL;
591         }
592     }
593     else if (envproxy)
594     {
595         WCHAR *envproxyW;
596
597         len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
598         if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
599             return ERROR_OUTOFMEMORY;
600         MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
601
602         lpwpi->proxyEnabled = 1;
603         lpwpi->proxy = envproxyW;
604
605         TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
606     }
607     RegCloseKey( key );
608
609     lpwpi->proxyBypass = NULL;
610
611     return ERROR_SUCCESS;
612 }
613
614 /***********************************************************************
615  *           INTERNET_ConfigureProxy
616  */
617 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
618 {
619     proxyinfo_t wpi;
620
621     if (INTERNET_LoadProxySettings( &wpi ))
622         return FALSE;
623
624     if (wpi.proxyEnabled)
625     {
626         lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
627         lpwai->proxy = wpi.proxy;
628         return TRUE;
629     }
630
631     lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
632     return FALSE;
633 }
634
635 /***********************************************************************
636  *           dump_INTERNET_FLAGS
637  *
638  * Helper function to TRACE the internet flags.
639  *
640  * RETURNS
641  *    None
642  *
643  */
644 static void dump_INTERNET_FLAGS(DWORD dwFlags) 
645 {
646 #define FE(x) { x, #x }
647     static const wininet_flag_info flag[] = {
648         FE(INTERNET_FLAG_RELOAD),
649         FE(INTERNET_FLAG_RAW_DATA),
650         FE(INTERNET_FLAG_EXISTING_CONNECT),
651         FE(INTERNET_FLAG_ASYNC),
652         FE(INTERNET_FLAG_PASSIVE),
653         FE(INTERNET_FLAG_NO_CACHE_WRITE),
654         FE(INTERNET_FLAG_MAKE_PERSISTENT),
655         FE(INTERNET_FLAG_FROM_CACHE),
656         FE(INTERNET_FLAG_SECURE),
657         FE(INTERNET_FLAG_KEEP_CONNECTION),
658         FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
659         FE(INTERNET_FLAG_READ_PREFETCH),
660         FE(INTERNET_FLAG_NO_COOKIES),
661         FE(INTERNET_FLAG_NO_AUTH),
662         FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
663         FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
664         FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
665         FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
666         FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
667         FE(INTERNET_FLAG_RESYNCHRONIZE),
668         FE(INTERNET_FLAG_HYPERLINK),
669         FE(INTERNET_FLAG_NO_UI),
670         FE(INTERNET_FLAG_PRAGMA_NOCACHE),
671         FE(INTERNET_FLAG_CACHE_ASYNC),
672         FE(INTERNET_FLAG_FORMS_SUBMIT),
673         FE(INTERNET_FLAG_NEED_FILE),
674         FE(INTERNET_FLAG_TRANSFER_ASCII),
675         FE(INTERNET_FLAG_TRANSFER_BINARY)
676     };
677 #undef FE
678     unsigned int i;
679
680     for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
681         if (flag[i].val & dwFlags) {
682             TRACE(" %s", flag[i].name);
683             dwFlags &= ~flag[i].val;
684         }
685     }   
686     if (dwFlags)
687         TRACE(" Unknown flags (%08x)\n", dwFlags);
688     else
689         TRACE("\n");
690 }
691
692 /***********************************************************************
693  *           INTERNET_CloseHandle (internal)
694  *
695  * Close internet handle
696  *
697  */
698 static VOID APPINFO_Destroy(object_header_t *hdr)
699 {
700     appinfo_t *lpwai = (appinfo_t*)hdr;
701
702     TRACE("%p\n",lpwai);
703
704     heap_free(lpwai->agent);
705     heap_free(lpwai->proxy);
706     heap_free(lpwai->proxyBypass);
707     heap_free(lpwai->proxyUsername);
708     heap_free(lpwai->proxyPassword);
709 }
710
711 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
712 {
713     appinfo_t *ai = (appinfo_t*)hdr;
714
715     switch(option) {
716     case INTERNET_OPTION_HANDLE_TYPE:
717         TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
718
719         if (*size < sizeof(ULONG))
720             return ERROR_INSUFFICIENT_BUFFER;
721
722         *size = sizeof(DWORD);
723         *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
724         return ERROR_SUCCESS;
725
726     case INTERNET_OPTION_USER_AGENT: {
727         DWORD bufsize;
728
729         TRACE("INTERNET_OPTION_USER_AGENT\n");
730
731         bufsize = *size;
732
733         if (unicode) {
734             DWORD len = ai->agent ? strlenW(ai->agent) : 0;
735
736             *size = (len + 1) * sizeof(WCHAR);
737             if(!buffer || bufsize < *size)
738                 return ERROR_INSUFFICIENT_BUFFER;
739
740             if (ai->agent)
741                 strcpyW(buffer, ai->agent);
742             else
743                 *(WCHAR *)buffer = 0;
744             /* If the buffer is copied, the returned length doesn't include
745              * the NULL terminator.
746              */
747             *size = len * sizeof(WCHAR);
748         }else {
749             if (ai->agent)
750                 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
751             else
752                 *size = 1;
753             if(!buffer || bufsize < *size)
754                 return ERROR_INSUFFICIENT_BUFFER;
755
756             if (ai->agent)
757                 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
758             else
759                 *(char *)buffer = 0;
760             /* If the buffer is copied, the returned length doesn't include
761              * the NULL terminator.
762              */
763             *size -= 1;
764         }
765
766         return ERROR_SUCCESS;
767     }
768
769     case INTERNET_OPTION_PROXY:
770         if (unicode) {
771             INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
772             DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
773             LPWSTR proxy, proxy_bypass;
774
775             if (ai->proxy)
776                 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
777             if (ai->proxyBypass)
778                 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
779             if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
780             {
781                 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
782                 return ERROR_INSUFFICIENT_BUFFER;
783             }
784             proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
785             proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
786
787             pi->dwAccessType = ai->accessType;
788             pi->lpszProxy = NULL;
789             pi->lpszProxyBypass = NULL;
790             if (ai->proxy) {
791                 lstrcpyW(proxy, ai->proxy);
792                 pi->lpszProxy = proxy;
793             }
794
795             if (ai->proxyBypass) {
796                 lstrcpyW(proxy_bypass, ai->proxyBypass);
797                 pi->lpszProxyBypass = proxy_bypass;
798             }
799
800             *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
801             return ERROR_SUCCESS;
802         }else {
803             INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
804             DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
805             LPSTR proxy, proxy_bypass;
806
807             if (ai->proxy)
808                 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
809             if (ai->proxyBypass)
810                 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
811                         NULL, 0, NULL, NULL);
812             if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
813             {
814                 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
815                 return ERROR_INSUFFICIENT_BUFFER;
816             }
817             proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
818             proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
819
820             pi->dwAccessType = ai->accessType;
821             pi->lpszProxy = NULL;
822             pi->lpszProxyBypass = NULL;
823             if (ai->proxy) {
824                 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
825                 pi->lpszProxy = proxy;
826             }
827
828             if (ai->proxyBypass) {
829                 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
830                         proxyBypassBytesRequired, NULL, NULL);
831                 pi->lpszProxyBypass = proxy_bypass;
832             }
833
834             *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
835             return ERROR_SUCCESS;
836         }
837     }
838
839     return INET_QueryOption(hdr, option, buffer, size, unicode);
840 }
841
842 static const object_vtbl_t APPINFOVtbl = {
843     APPINFO_Destroy,
844     NULL,
845     APPINFO_QueryOption,
846     NULL,
847     NULL,
848     NULL,
849     NULL,
850     NULL,
851     NULL
852 };
853
854
855 /***********************************************************************
856  *           InternetOpenW   (WININET.@)
857  *
858  * Per-application initialization of wininet
859  *
860  * RETURNS
861  *    HINTERNET on success
862  *    NULL on failure
863  *
864  */
865 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
866     LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
867 {
868     appinfo_t *lpwai = NULL;
869
870     if (TRACE_ON(wininet)) {
871 #define FE(x) { x, #x }
872         static const wininet_flag_info access_type[] = {
873             FE(INTERNET_OPEN_TYPE_PRECONFIG),
874             FE(INTERNET_OPEN_TYPE_DIRECT),
875             FE(INTERNET_OPEN_TYPE_PROXY),
876             FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
877         };
878 #undef FE
879         DWORD i;
880         const char *access_type_str = "Unknown";
881         
882         TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
883               debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
884         for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
885             if (access_type[i].val == dwAccessType) {
886                 access_type_str = access_type[i].name;
887                 break;
888             }
889         }
890         TRACE("  access type : %s\n", access_type_str);
891         TRACE("  flags       :");
892         dump_INTERNET_FLAGS(dwFlags);
893     }
894
895     /* Clear any error information */
896     INTERNET_SetLastError(0);
897
898     lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
899     if (!lpwai) {
900         SetLastError(ERROR_OUTOFMEMORY);
901         return NULL;
902     }
903
904     lpwai->hdr.htype = WH_HINIT;
905     lpwai->hdr.dwFlags = dwFlags;
906     lpwai->accessType = dwAccessType;
907     lpwai->proxyUsername = NULL;
908     lpwai->proxyPassword = NULL;
909
910     lpwai->agent = heap_strdupW(lpszAgent);
911     if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
912         INTERNET_ConfigureProxy( lpwai );
913     else
914         lpwai->proxy = heap_strdupW(lpszProxy);
915     lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
916
917     TRACE("returning %p\n", lpwai);
918
919     return lpwai->hdr.hInternet;
920 }
921
922
923 /***********************************************************************
924  *           InternetOpenA   (WININET.@)
925  *
926  * Per-application initialization of wininet
927  *
928  * RETURNS
929  *    HINTERNET on success
930  *    NULL on failure
931  *
932  */
933 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
934     LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
935 {
936     WCHAR *szAgent, *szProxy, *szBypass;
937     HINTERNET rc;
938
939     TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
940        dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
941
942     szAgent = heap_strdupAtoW(lpszAgent);
943     szProxy = heap_strdupAtoW(lpszProxy);
944     szBypass = heap_strdupAtoW(lpszProxyBypass);
945
946     rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
947
948     heap_free(szAgent);
949     heap_free(szProxy);
950     heap_free(szBypass);
951     return rc;
952 }
953
954 /***********************************************************************
955  *           InternetGetLastResponseInfoA (WININET.@)
956  *
957  * Return last wininet error description on the calling thread
958  *
959  * RETURNS
960  *    TRUE on success of writing to buffer
961  *    FALSE on failure
962  *
963  */
964 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
965     LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
966 {
967     LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
968
969     TRACE("\n");
970
971     if (lpwite)
972     {
973         *lpdwError = lpwite->dwError;
974         if (lpwite->dwError)
975         {
976             memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
977             *lpdwBufferLength = strlen(lpszBuffer);
978         }
979         else
980             *lpdwBufferLength = 0;
981     }
982     else
983     {
984         *lpdwError = 0;
985         *lpdwBufferLength = 0;
986     }
987
988     return TRUE;
989 }
990
991 /***********************************************************************
992  *           InternetGetLastResponseInfoW (WININET.@)
993  *
994  * Return last wininet error description on the calling thread
995  *
996  * RETURNS
997  *    TRUE on success of writing to buffer
998  *    FALSE on failure
999  *
1000  */
1001 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1002     LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1003 {
1004     LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1005
1006     TRACE("\n");
1007
1008     if (lpwite)
1009     {
1010         *lpdwError = lpwite->dwError;
1011         if (lpwite->dwError)
1012         {
1013             memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1014             *lpdwBufferLength = lstrlenW(lpszBuffer);
1015         }
1016         else
1017             *lpdwBufferLength = 0;
1018     }
1019     else
1020     {
1021         *lpdwError = 0;
1022         *lpdwBufferLength = 0;
1023     }
1024
1025     return TRUE;
1026 }
1027
1028 /***********************************************************************
1029  *           InternetGetConnectedState (WININET.@)
1030  *
1031  * Return connected state
1032  *
1033  * RETURNS
1034  *    TRUE if connected
1035  *    if lpdwStatus is not null, return the status (off line,
1036  *    modem, lan...) in it.
1037  *    FALSE if not connected
1038  */
1039 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1040 {
1041     TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1042
1043     if (lpdwStatus) {
1044         WARN("always returning LAN connection.\n");
1045         *lpdwStatus = INTERNET_CONNECTION_LAN;
1046     }
1047     return TRUE;
1048 }
1049
1050
1051 /***********************************************************************
1052  *           InternetGetConnectedStateExW (WININET.@)
1053  *
1054  * Return connected state
1055  *
1056  * PARAMS
1057  *
1058  * lpdwStatus         [O] Flags specifying the status of the internet connection.
1059  * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1060  * dwNameLen          [I] Size of the buffer, in characters.
1061  * dwReserved         [I] Reserved. Must be set to 0.
1062  *
1063  * RETURNS
1064  *    TRUE if connected
1065  *    if lpdwStatus is not null, return the status (off line,
1066  *    modem, lan...) in it.
1067  *    FALSE if not connected
1068  *
1069  * NOTES
1070  *   If the system has no available network connections, an empty string is
1071  *   stored in lpszConnectionName. If there is a LAN connection, a localized
1072  *   "LAN Connection" string is stored. Presumably, if only a dial-up
1073  *   connection is available then the name of the dial-up connection is
1074  *   returned. Why any application, other than the "Internet Settings" CPL,
1075  *   would want to use this function instead of the simpler InternetGetConnectedStateW
1076  *   function is beyond me.
1077  */
1078 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1079                                          DWORD dwNameLen, DWORD dwReserved)
1080 {
1081     TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1082
1083     /* Must be zero */
1084     if(dwReserved)
1085         return FALSE;
1086
1087     if (lpdwStatus) {
1088         WARN("always returning LAN connection.\n");
1089         *lpdwStatus = INTERNET_CONNECTION_LAN;
1090     }
1091     return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1092 }
1093
1094
1095 /***********************************************************************
1096  *           InternetGetConnectedStateExA (WININET.@)
1097  */
1098 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1099                                          DWORD dwNameLen, DWORD dwReserved)
1100 {
1101     LPWSTR lpwszConnectionName = NULL;
1102     BOOL rc;
1103
1104     TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1105
1106     if (lpszConnectionName && dwNameLen > 0)
1107         lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1108
1109     rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1110                                       dwReserved);
1111     if (rc && lpwszConnectionName)
1112     {
1113         WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1114                             dwNameLen, NULL, NULL);
1115         heap_free(lpwszConnectionName);
1116     }
1117     return rc;
1118 }
1119
1120
1121 /***********************************************************************
1122  *           InternetConnectW (WININET.@)
1123  *
1124  * Open a ftp, gopher or http session
1125  *
1126  * RETURNS
1127  *    HINTERNET a session handle on success
1128  *    NULL on failure
1129  *
1130  */
1131 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1132     LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1133     LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1134     DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1135 {
1136     appinfo_t *hIC;
1137     HINTERNET rc = NULL;
1138     DWORD res = ERROR_SUCCESS;
1139
1140     TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
1141           nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
1142           dwService, dwFlags, dwContext);
1143
1144     if (!lpszServerName)
1145     {
1146         SetLastError(ERROR_INVALID_PARAMETER);
1147         return NULL;
1148     }
1149
1150     hIC = (appinfo_t*)get_handle_object( hInternet );
1151     if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1152     {
1153         res = ERROR_INVALID_HANDLE;
1154         goto lend;
1155     }
1156
1157     switch (dwService)
1158     {
1159         case INTERNET_SERVICE_FTP:
1160             rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1161             lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1162             if(!rc)
1163                 res = INTERNET_GetLastError();
1164             break;
1165
1166         case INTERNET_SERVICE_HTTP:
1167             res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1168                     lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1169             break;
1170
1171         case INTERNET_SERVICE_GOPHER:
1172         default:
1173             break;
1174     }
1175 lend:
1176     if( hIC )
1177         WININET_Release( &hIC->hdr );
1178
1179     TRACE("returning %p\n", rc);
1180     SetLastError(res);
1181     return rc;
1182 }
1183
1184
1185 /***********************************************************************
1186  *           InternetConnectA (WININET.@)
1187  *
1188  * Open a ftp, gopher or http session
1189  *
1190  * RETURNS
1191  *    HINTERNET a session handle on success
1192  *    NULL on failure
1193  *
1194  */
1195 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1196     LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1197     LPCSTR lpszUserName, LPCSTR lpszPassword,
1198     DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1199 {
1200     HINTERNET rc = NULL;
1201     LPWSTR szServerName;
1202     LPWSTR szUserName;
1203     LPWSTR szPassword;
1204
1205     szServerName = heap_strdupAtoW(lpszServerName);
1206     szUserName = heap_strdupAtoW(lpszUserName);
1207     szPassword = heap_strdupAtoW(lpszPassword);
1208
1209     rc = InternetConnectW(hInternet, szServerName, nServerPort,
1210         szUserName, szPassword, dwService, dwFlags, dwContext);
1211
1212     heap_free(szServerName);
1213     heap_free(szUserName);
1214     heap_free(szPassword);
1215     return rc;
1216 }
1217
1218
1219 /***********************************************************************
1220  *           InternetFindNextFileA (WININET.@)
1221  *
1222  * Continues a file search from a previous call to FindFirstFile
1223  *
1224  * RETURNS
1225  *    TRUE on success
1226  *    FALSE on failure
1227  *
1228  */
1229 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1230 {
1231     BOOL ret;
1232     WIN32_FIND_DATAW fd;
1233     
1234     ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1235     if(lpvFindData)
1236         WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1237     return ret;
1238 }
1239
1240 /***********************************************************************
1241  *           InternetFindNextFileW (WININET.@)
1242  *
1243  * Continues a file search from a previous call to FindFirstFile
1244  *
1245  * RETURNS
1246  *    TRUE on success
1247  *    FALSE on failure
1248  *
1249  */
1250 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1251 {
1252     object_header_t *hdr;
1253     DWORD res;
1254
1255     TRACE("\n");
1256
1257     hdr = get_handle_object(hFind);
1258     if(!hdr) {
1259         WARN("Invalid handle\n");
1260         SetLastError(ERROR_INVALID_HANDLE);
1261         return FALSE;
1262     }
1263
1264     if(hdr->vtbl->FindNextFileW) {
1265         res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1266     }else {
1267         WARN("Handle doesn't support NextFile\n");
1268         res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1269     }
1270
1271     WININET_Release(hdr);
1272
1273     if(res != ERROR_SUCCESS)
1274         SetLastError(res);
1275     return res == ERROR_SUCCESS;
1276 }
1277
1278 /***********************************************************************
1279  *           InternetCloseHandle (WININET.@)
1280  *
1281  * Generic close handle function
1282  *
1283  * RETURNS
1284  *    TRUE on success
1285  *    FALSE on failure
1286  *
1287  */
1288 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1289 {
1290     object_header_t *obj;
1291     
1292     TRACE("%p\n", hInternet);
1293
1294     obj = get_handle_object( hInternet );
1295     if (!obj) {
1296         SetLastError(ERROR_INVALID_HANDLE);
1297         return FALSE;
1298     }
1299
1300     invalidate_handle(obj);
1301     WININET_Release(obj);
1302
1303     return TRUE;
1304 }
1305
1306
1307 /***********************************************************************
1308  *           ConvertUrlComponentValue (Internal)
1309  *
1310  * Helper function for InternetCrackUrlA
1311  *
1312  */
1313 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1314                                      LPWSTR lpwszComponent, DWORD dwwComponentLen,
1315                                      LPCSTR lpszStart, LPCWSTR lpwszStart)
1316 {
1317     TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1318     if (*dwComponentLen != 0)
1319     {
1320         DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1321         if (*lppszComponent == NULL)
1322         {
1323             if (lpwszComponent)
1324             {
1325                 int offset = WideCharToMultiByte(CP_ACP, 0, lpwszStart, lpwszComponent-lpwszStart, NULL, 0, NULL, NULL);
1326                 *lppszComponent = (LPSTR)lpszStart + offset;
1327             }
1328             else
1329                 *lppszComponent = NULL;
1330
1331             *dwComponentLen = nASCIILength;
1332         }
1333         else
1334         {
1335             DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1336             WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1337             (*lppszComponent)[ncpylen]=0;
1338             *dwComponentLen = ncpylen;
1339         }
1340     }
1341 }
1342
1343
1344 /***********************************************************************
1345  *           InternetCrackUrlA (WININET.@)
1346  *
1347  * See InternetCrackUrlW.
1348  */
1349 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1350     LPURL_COMPONENTSA lpUrlComponents)
1351 {
1352   DWORD nLength;
1353   URL_COMPONENTSW UCW;
1354   BOOL ret = FALSE;
1355   WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1356         *scheme = NULL, *extra = NULL;
1357
1358   TRACE("(%s %u %x %p)\n",
1359         lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1360         dwUrlLength, dwFlags, lpUrlComponents);
1361
1362   if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1363           lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1364   {
1365       INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1366       return FALSE;
1367   }
1368
1369   if(dwUrlLength<=0)
1370       dwUrlLength=-1;
1371   nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1372
1373   /* if dwUrlLength=-1 then nLength includes null but length to 
1374        InternetCrackUrlW should not include it                  */
1375   if (dwUrlLength == -1) nLength--;
1376
1377   lpwszUrl = heap_alloc((nLength + 1) * sizeof(WCHAR));
1378   MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength + 1);
1379   lpwszUrl[nLength] = '\0';
1380
1381   memset(&UCW,0,sizeof(UCW));
1382   UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1383   if (lpUrlComponents->dwHostNameLength)
1384   {
1385     UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1386     if (lpUrlComponents->lpszHostName)
1387     {
1388       hostname = heap_alloc(UCW.dwHostNameLength * sizeof(WCHAR));
1389       UCW.lpszHostName = hostname;
1390     }
1391   }
1392   if (lpUrlComponents->dwUserNameLength)
1393   {
1394     UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1395     if (lpUrlComponents->lpszUserName)
1396     {
1397       username = heap_alloc(UCW.dwUserNameLength * sizeof(WCHAR));
1398       UCW.lpszUserName = username;
1399     }
1400   }
1401   if (lpUrlComponents->dwPasswordLength)
1402   {
1403     UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1404     if (lpUrlComponents->lpszPassword)
1405     {
1406       password = heap_alloc(UCW.dwPasswordLength * sizeof(WCHAR));
1407       UCW.lpszPassword = password;
1408     }
1409   }
1410   if (lpUrlComponents->dwUrlPathLength)
1411   {
1412     UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1413     if (lpUrlComponents->lpszUrlPath)
1414     {
1415       path = heap_alloc(UCW.dwUrlPathLength * sizeof(WCHAR));
1416       UCW.lpszUrlPath = path;
1417     }
1418   }
1419   if (lpUrlComponents->dwSchemeLength)
1420   {
1421     UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1422     if (lpUrlComponents->lpszScheme)
1423     {
1424       scheme = heap_alloc(UCW.dwSchemeLength * sizeof(WCHAR));
1425       UCW.lpszScheme = scheme;
1426     }
1427   }
1428   if (lpUrlComponents->dwExtraInfoLength)
1429   {
1430     UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1431     if (lpUrlComponents->lpszExtraInfo)
1432     {
1433       extra = heap_alloc(UCW.dwExtraInfoLength * sizeof(WCHAR));
1434       UCW.lpszExtraInfo = extra;
1435     }
1436   }
1437   if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1438   {
1439     ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1440                              UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1441     ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1442                              UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1443     ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1444                              UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1445     ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1446                              UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1447     ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1448                              UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1449     ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1450                              UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1451
1452     lpUrlComponents->nScheme = UCW.nScheme;
1453     lpUrlComponents->nPort = UCW.nPort;
1454
1455     TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(lpszUrl),
1456           debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1457           debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1458           debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1459           debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1460   }
1461   heap_free(lpwszUrl);
1462   heap_free(hostname);
1463   heap_free(username);
1464   heap_free(password);
1465   heap_free(path);
1466   heap_free(scheme);
1467   heap_free(extra);
1468   return ret;
1469 }
1470
1471 static const WCHAR url_schemes[][7] =
1472 {
1473     {'f','t','p',0},
1474     {'g','o','p','h','e','r',0},
1475     {'h','t','t','p',0},
1476     {'h','t','t','p','s',0},
1477     {'f','i','l','e',0},
1478     {'n','e','w','s',0},
1479     {'m','a','i','l','t','o',0},
1480     {'r','e','s',0},
1481 };
1482
1483 /***********************************************************************
1484  *           GetInternetSchemeW (internal)
1485  *
1486  * Get scheme of url
1487  *
1488  * RETURNS
1489  *    scheme on success
1490  *    INTERNET_SCHEME_UNKNOWN on failure
1491  *
1492  */
1493 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1494 {
1495     int i;
1496
1497     TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1498
1499     if(lpszScheme==NULL)
1500         return INTERNET_SCHEME_UNKNOWN;
1501
1502     for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1503         if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1504             return INTERNET_SCHEME_FIRST + i;
1505
1506     return INTERNET_SCHEME_UNKNOWN;
1507 }
1508
1509 /***********************************************************************
1510  *           SetUrlComponentValueW (Internal)
1511  *
1512  * Helper function for InternetCrackUrlW
1513  *
1514  * PARAMS
1515  *     lppszComponent [O] Holds the returned string
1516  *     dwComponentLen [I] Holds the size of lppszComponent
1517  *                    [O] Holds the length of the string in lppszComponent without '\0'
1518  *     lpszStart      [I] Holds the string to copy from
1519  *     len            [I] Holds the length of lpszStart without '\0'
1520  *
1521  * RETURNS
1522  *    TRUE on success
1523  *    FALSE on failure
1524  *
1525  */
1526 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1527 {
1528     TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1529
1530     if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1531         return FALSE;
1532
1533     if (*dwComponentLen != 0 || *lppszComponent == NULL)
1534     {
1535         if (*lppszComponent == NULL)
1536         {
1537             *lppszComponent = (LPWSTR)lpszStart;
1538             *dwComponentLen = len;
1539         }
1540         else
1541         {
1542             DWORD ncpylen = min((*dwComponentLen)-1, len);
1543             memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1544             (*lppszComponent)[ncpylen] = '\0';
1545             *dwComponentLen = ncpylen;
1546         }
1547     }
1548
1549     return TRUE;
1550 }
1551
1552 /***********************************************************************
1553  *           InternetCrackUrlW   (WININET.@)
1554  *
1555  * Break up URL into its components
1556  *
1557  * RETURNS
1558  *    TRUE on success
1559  *    FALSE on failure
1560  */
1561 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1562                               LPURL_COMPONENTSW lpUC)
1563 {
1564   /*
1565    * RFC 1808
1566    * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1567    *
1568    */
1569     LPCWSTR lpszParam    = NULL;
1570     BOOL  bIsAbsolute = FALSE;
1571     LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1572     LPCWSTR lpszcp = NULL;
1573     LPWSTR  lpszUrl_decode = NULL;
1574     DWORD dwUrlLength = dwUrlLength_orig;
1575
1576     TRACE("(%s %u %x %p)\n",
1577           lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1578           dwUrlLength, dwFlags, lpUC);
1579
1580     if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1581     {
1582         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1583         return FALSE;
1584     }
1585     if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1586
1587     if (dwFlags & ICU_DECODE)
1588     {
1589         WCHAR *url_tmp;
1590         DWORD len = dwUrlLength + 1;
1591
1592         if (!(url_tmp = heap_alloc(len * sizeof(WCHAR))))
1593         {
1594             INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1595             return FALSE;
1596         }
1597         memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1598         url_tmp[dwUrlLength] = 0;
1599         if (!(lpszUrl_decode = heap_alloc(len * sizeof(WCHAR))))
1600         {
1601             heap_free(url_tmp);
1602             INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1603             return FALSE;
1604         }
1605         if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1606         {
1607             dwUrlLength = len;
1608             lpszUrl = lpszUrl_decode;
1609         }
1610         heap_free(url_tmp);
1611     }
1612     lpszap = lpszUrl;
1613     
1614     /* Determine if the URI is absolute. */
1615     while (lpszap - lpszUrl < dwUrlLength)
1616     {
1617         if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1618         {
1619             lpszap++;
1620             continue;
1621         }
1622         if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1623         {
1624             bIsAbsolute = TRUE;
1625             lpszcp = lpszap;
1626         }
1627         else
1628         {
1629             lpszcp = lpszUrl; /* Relative url */
1630         }
1631
1632         break;
1633     }
1634
1635     lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1636     lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1637
1638     /* Parse <params> */
1639     lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl));
1640     if(!lpszParam)
1641         lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1642     if(!lpszParam)
1643         lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1644
1645     SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1646                           lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1647
1648     if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1649     {
1650         LPCWSTR lpszNetLoc;
1651
1652         /* Get scheme first. */
1653         lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1654         SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1655                                    lpszUrl, lpszcp - lpszUrl);
1656
1657         /* Eat ':' in protocol. */
1658         lpszcp++;
1659
1660         /* double slash indicates the net_loc portion is present */
1661         if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1662         {
1663             lpszcp += 2;
1664
1665             lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1666             if (lpszParam)
1667             {
1668                 if (lpszNetLoc)
1669                     lpszNetLoc = min(lpszNetLoc, lpszParam);
1670                 else
1671                     lpszNetLoc = lpszParam;
1672             }
1673             else if (!lpszNetLoc)
1674                 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1675
1676             /* Parse net-loc */
1677             if (lpszNetLoc)
1678             {
1679                 LPCWSTR lpszHost;
1680                 LPCWSTR lpszPort;
1681
1682                 /* [<user>[<:password>]@]<host>[:<port>] */
1683                 /* First find the user and password if they exist */
1684
1685                 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1686                 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1687                 {
1688                     /* username and password not specified. */
1689                     SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1690                     SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1691                 }
1692                 else /* Parse out username and password */
1693                 {
1694                     LPCWSTR lpszUser = lpszcp;
1695                     LPCWSTR lpszPasswd = lpszHost;
1696
1697                     while (lpszcp < lpszHost)
1698                     {
1699                         if (*lpszcp == ':')
1700                             lpszPasswd = lpszcp;
1701
1702                         lpszcp++;
1703                     }
1704
1705                     SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1706                                           lpszUser, lpszPasswd - lpszUser);
1707
1708                     if (lpszPasswd != lpszHost)
1709                         lpszPasswd++;
1710                     SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1711                                           lpszPasswd == lpszHost ? NULL : lpszPasswd,
1712                                           lpszHost - lpszPasswd);
1713
1714                     lpszcp++; /* Advance to beginning of host */
1715                 }
1716
1717                 /* Parse <host><:port> */
1718
1719                 lpszHost = lpszcp;
1720                 lpszPort = lpszNetLoc;
1721
1722                 /* special case for res:// URLs: there is no port here, so the host is the
1723                    entire string up to the first '/' */
1724                 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1725                 {
1726                     SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1727                                           lpszHost, lpszPort - lpszHost);
1728                     lpszcp=lpszNetLoc;
1729                 }
1730                 else
1731                 {
1732                     while (lpszcp < lpszNetLoc)
1733                     {
1734                         if (*lpszcp == ':')
1735                             lpszPort = lpszcp;
1736
1737                         lpszcp++;
1738                     }
1739
1740                     /* If the scheme is "file" and the host is just one letter, it's not a host */
1741                     if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1742                     {
1743                         lpszcp=lpszHost;
1744                         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1745                                               NULL, 0);
1746                     }
1747                     else
1748                     {
1749                         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1750                                               lpszHost, lpszPort - lpszHost);
1751                         if (lpszPort != lpszNetLoc)
1752                             lpUC->nPort = atoiW(++lpszPort);
1753                         else switch (lpUC->nScheme)
1754                         {
1755                         case INTERNET_SCHEME_HTTP:
1756                             lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1757                             break;
1758                         case INTERNET_SCHEME_HTTPS:
1759                             lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1760                             break;
1761                         case INTERNET_SCHEME_FTP:
1762                             lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1763                             break;
1764                         case INTERNET_SCHEME_GOPHER:
1765                             lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1766                             break;
1767                         default:
1768                             break;
1769                         }
1770                     }
1771                 }
1772             }
1773         }
1774         else
1775         {
1776             SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1777             SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1778             SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1779         }
1780     }
1781     else
1782     {
1783         SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1784         SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1785         SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1786         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1787     }
1788
1789     /* Here lpszcp points to:
1790      *
1791      * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1792      *                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1793      */
1794     if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1795     {
1796         DWORD len;
1797
1798         /* Only truncate the parameter list if it's already been saved
1799          * in lpUC->lpszExtraInfo.
1800          */
1801         if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1802             len = lpszParam - lpszcp;
1803         else
1804         {
1805             /* Leave the parameter list in lpszUrlPath.  Strip off any trailing
1806              * newlines if necessary.
1807              */
1808             LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1809             if (lpsznewline != NULL)
1810                 len = lpsznewline - lpszcp;
1811             else
1812                 len = dwUrlLength-(lpszcp-lpszUrl);
1813         }
1814         if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1815                 lpUC->nScheme == INTERNET_SCHEME_FILE)
1816         {
1817             WCHAR tmppath[MAX_PATH];
1818             if (*lpszcp == '/')
1819             {
1820                 len = MAX_PATH;
1821                 PathCreateFromUrlW(lpszUrl_orig, tmppath, &len, 0);
1822             }
1823             else
1824             {
1825                 WCHAR *iter;
1826                 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1827                 tmppath[len] = '\0';
1828
1829                 iter = tmppath;
1830                 while (*iter) {
1831                     if (*iter == '/')
1832                         *iter = '\\';
1833                     ++iter;
1834                 }
1835             }
1836             /* if ends in \. or \.. append a backslash */
1837             if (tmppath[len - 1] == '.' &&
1838                     (tmppath[len - 2] == '\\' ||
1839                      (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1840             {
1841                 if (len < MAX_PATH - 1)
1842                 {
1843                     tmppath[len] = '\\';
1844                     tmppath[len+1] = '\0';
1845                     ++len;
1846                 }
1847             }
1848             SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1849                                        tmppath, len);
1850         }
1851         else
1852             SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1853                                        lpszcp, len);
1854     }
1855     else
1856     {
1857         if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1858             lpUC->lpszUrlPath[0] = 0;
1859         lpUC->dwUrlPathLength = 0;
1860     }
1861
1862     TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1863              debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1864              debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1865              debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1866              debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1867
1868     heap_free( lpszUrl_decode );
1869     return TRUE;
1870 }
1871
1872 /***********************************************************************
1873  *           InternetAttemptConnect (WININET.@)
1874  *
1875  * Attempt to make a connection to the internet
1876  *
1877  * RETURNS
1878  *    ERROR_SUCCESS on success
1879  *    Error value   on failure
1880  *
1881  */
1882 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1883 {
1884     FIXME("Stub\n");
1885     return ERROR_SUCCESS;
1886 }
1887
1888
1889 /***********************************************************************
1890  *           InternetCanonicalizeUrlA (WININET.@)
1891  *
1892  * Escape unsafe characters and spaces
1893  *
1894  * RETURNS
1895  *    TRUE on success
1896  *    FALSE on failure
1897  *
1898  */
1899 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1900         LPDWORD lpdwBufferLength, DWORD dwFlags)
1901 {
1902     HRESULT hr;
1903     DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1904
1905     TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1906         lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1907
1908     if(dwFlags & ICU_DECODE)
1909     {
1910         dwURLFlags |= URL_UNESCAPE;
1911         dwFlags &= ~ICU_DECODE;
1912     }
1913
1914     if(dwFlags & ICU_ESCAPE)
1915     {
1916         dwURLFlags |= URL_UNESCAPE;
1917         dwFlags &= ~ICU_ESCAPE;
1918     }
1919
1920     if(dwFlags & ICU_BROWSER_MODE)
1921     {
1922         dwURLFlags |= URL_BROWSER_MODE;
1923         dwFlags &= ~ICU_BROWSER_MODE;
1924     }
1925
1926     if(dwFlags & ICU_NO_ENCODE)
1927     {
1928         /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1929         dwURLFlags ^= URL_ESCAPE_UNSAFE;
1930         dwFlags &= ~ICU_NO_ENCODE;
1931     }
1932
1933     if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1934
1935     hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1936     if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1937     if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1938
1939     return (hr == S_OK) ? TRUE : FALSE;
1940 }
1941
1942 /***********************************************************************
1943  *           InternetCanonicalizeUrlW (WININET.@)
1944  *
1945  * Escape unsafe characters and spaces
1946  *
1947  * RETURNS
1948  *    TRUE on success
1949  *    FALSE on failure
1950  *
1951  */
1952 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1953     LPDWORD lpdwBufferLength, DWORD dwFlags)
1954 {
1955     HRESULT hr;
1956     DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1957
1958     TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1959           lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1960
1961     if(dwFlags & ICU_DECODE)
1962     {
1963         dwURLFlags |= URL_UNESCAPE;
1964         dwFlags &= ~ICU_DECODE;
1965     }
1966
1967     if(dwFlags & ICU_ESCAPE)
1968     {
1969         dwURLFlags |= URL_UNESCAPE;
1970         dwFlags &= ~ICU_ESCAPE;
1971     }
1972
1973     if(dwFlags & ICU_BROWSER_MODE)
1974     {
1975         dwURLFlags |= URL_BROWSER_MODE;
1976         dwFlags &= ~ICU_BROWSER_MODE;
1977     }
1978
1979     if(dwFlags & ICU_NO_ENCODE)
1980     {
1981         /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1982         dwURLFlags ^= URL_ESCAPE_UNSAFE;
1983         dwFlags &= ~ICU_NO_ENCODE;
1984     }
1985
1986     if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1987
1988     hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1989     if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1990     if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1991
1992     return (hr == S_OK) ? TRUE : FALSE;
1993 }
1994
1995 /* #################################################### */
1996
1997 static INTERNET_STATUS_CALLBACK set_status_callback(
1998     object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1999 {
2000     INTERNET_STATUS_CALLBACK ret;
2001
2002     if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2003     else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2004
2005     ret = lpwh->lpfnStatusCB;
2006     lpwh->lpfnStatusCB = callback;
2007
2008     return ret;
2009 }
2010
2011 /***********************************************************************
2012  *           InternetSetStatusCallbackA (WININET.@)
2013  *
2014  * Sets up a callback function which is called as progress is made
2015  * during an operation.
2016  *
2017  * RETURNS
2018  *    Previous callback or NULL         on success
2019  *    INTERNET_INVALID_STATUS_CALLBACK  on failure
2020  *
2021  */
2022 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2023         HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2024 {
2025     INTERNET_STATUS_CALLBACK retVal;
2026     object_header_t *lpwh;
2027
2028     TRACE("%p\n", hInternet);
2029
2030     if (!(lpwh = get_handle_object(hInternet)))
2031         return INTERNET_INVALID_STATUS_CALLBACK;
2032
2033     retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2034
2035     WININET_Release( lpwh );
2036     return retVal;
2037 }
2038
2039 /***********************************************************************
2040  *           InternetSetStatusCallbackW (WININET.@)
2041  *
2042  * Sets up a callback function which is called as progress is made
2043  * during an operation.
2044  *
2045  * RETURNS
2046  *    Previous callback or NULL         on success
2047  *    INTERNET_INVALID_STATUS_CALLBACK  on failure
2048  *
2049  */
2050 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2051         HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2052 {
2053     INTERNET_STATUS_CALLBACK retVal;
2054     object_header_t *lpwh;
2055
2056     TRACE("%p\n", hInternet);
2057
2058     if (!(lpwh = get_handle_object(hInternet)))
2059         return INTERNET_INVALID_STATUS_CALLBACK;
2060
2061     retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2062
2063     WININET_Release( lpwh );
2064     return retVal;
2065 }
2066
2067 /***********************************************************************
2068  *           InternetSetFilePointer (WININET.@)
2069  */
2070 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2071     PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2072 {
2073     FIXME("stub\n");
2074     return FALSE;
2075 }
2076
2077 /***********************************************************************
2078  *           InternetWriteFile (WININET.@)
2079  *
2080  * Write data to an open internet file
2081  *
2082  * RETURNS
2083  *    TRUE  on success
2084  *    FALSE on failure
2085  *
2086  */
2087 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2088         DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2089 {
2090     object_header_t *lpwh;
2091     BOOL res;
2092
2093     TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2094
2095     lpwh = get_handle_object( hFile );
2096     if (!lpwh) {
2097         WARN("Invalid handle\n");
2098         SetLastError(ERROR_INVALID_HANDLE);
2099         return FALSE;
2100     }
2101
2102     if(lpwh->vtbl->WriteFile) {
2103         res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2104     }else {
2105         WARN("No Writefile method.\n");
2106         res = ERROR_INVALID_HANDLE;
2107     }
2108
2109     WININET_Release( lpwh );
2110
2111     if(res != ERROR_SUCCESS)
2112         SetLastError(res);
2113     return res == ERROR_SUCCESS;
2114 }
2115
2116
2117 /***********************************************************************
2118  *           InternetReadFile (WININET.@)
2119  *
2120  * Read data from an open internet file
2121  *
2122  * RETURNS
2123  *    TRUE  on success
2124  *    FALSE on failure
2125  *
2126  */
2127 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2128         DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2129 {
2130     object_header_t *hdr;
2131     DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2132
2133     TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2134
2135     hdr = get_handle_object(hFile);
2136     if (!hdr) {
2137         INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2138         return FALSE;
2139     }
2140
2141     if(hdr->vtbl->ReadFile)
2142         res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2143
2144     WININET_Release(hdr);
2145
2146     TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2147           pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2148
2149     if(res != ERROR_SUCCESS)
2150         SetLastError(res);
2151     return res == ERROR_SUCCESS;
2152 }
2153
2154 /***********************************************************************
2155  *           InternetReadFileExA (WININET.@)
2156  *
2157  * Read data from an open internet file
2158  *
2159  * PARAMS
2160  *  hFile         [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2161  *  lpBuffersOut  [I/O] Buffer.
2162  *  dwFlags       [I] Flags. See notes.
2163  *  dwContext     [I] Context for callbacks.
2164  *
2165  * RETURNS
2166  *    TRUE  on success
2167  *    FALSE on failure
2168  *
2169  * NOTES
2170  *  The parameter dwFlags include zero or more of the following flags:
2171  *|IRF_ASYNC - Makes the call asynchronous.
2172  *|IRF_SYNC - Makes the call synchronous.
2173  *|IRF_USE_CONTEXT - Forces dwContext to be used.
2174  *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2175  *
2176  * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2177  *
2178  * SEE
2179  *  InternetOpenUrlA(), HttpOpenRequestA()
2180  */
2181 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2182         DWORD dwFlags, DWORD_PTR dwContext)
2183 {
2184     object_header_t *hdr;
2185     DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2186
2187     TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2188
2189     hdr = get_handle_object(hFile);
2190     if (!hdr) {
2191         INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2192         return FALSE;
2193     }
2194
2195     if(hdr->vtbl->ReadFileExA)
2196         res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
2197
2198     WININET_Release(hdr);
2199
2200     TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2201           res, lpBuffersOut->dwBufferLength);
2202
2203     if(res != ERROR_SUCCESS)
2204         SetLastError(res);
2205     return res == ERROR_SUCCESS;
2206 }
2207
2208 /***********************************************************************
2209  *           InternetReadFileExW (WININET.@)
2210  * SEE
2211  *  InternetReadFileExA()
2212  */
2213 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2214         DWORD dwFlags, DWORD_PTR dwContext)
2215 {
2216     object_header_t *hdr;
2217     DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2218
2219     TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2220
2221     hdr = get_handle_object(hFile);
2222     if (!hdr) {
2223         INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2224         return FALSE;
2225     }
2226
2227     if(hdr->vtbl->ReadFileExW)
2228         res = hdr->vtbl->ReadFileExW(hdr, lpBuffer, dwFlags, dwContext);
2229
2230     WININET_Release(hdr);
2231
2232     TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2233           res, lpBuffer->dwBufferLength);
2234
2235     if(res != ERROR_SUCCESS)
2236         SetLastError(res);
2237     return res == ERROR_SUCCESS;
2238 }
2239
2240 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2241 {
2242     static BOOL warn = TRUE;
2243
2244     switch(option) {
2245     case INTERNET_OPTION_REQUEST_FLAGS:
2246         TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
2247
2248         if (*size < sizeof(ULONG))
2249             return ERROR_INSUFFICIENT_BUFFER;
2250
2251         *(ULONG*)buffer = 4;
2252         *size = sizeof(ULONG);
2253
2254         return ERROR_SUCCESS;
2255
2256     case INTERNET_OPTION_HTTP_VERSION:
2257         if (*size < sizeof(HTTP_VERSION_INFO))
2258             return ERROR_INSUFFICIENT_BUFFER;
2259
2260         /*
2261          * Presently hardcoded to 1.1
2262          */
2263         ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2264         ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2265         *size = sizeof(HTTP_VERSION_INFO);
2266
2267         return ERROR_SUCCESS;
2268
2269     case INTERNET_OPTION_CONNECTED_STATE:
2270         if (warn) {
2271             FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2272             warn = FALSE;
2273         }
2274         if (*size < sizeof(ULONG))
2275             return ERROR_INSUFFICIENT_BUFFER;
2276
2277         *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2278         *size = sizeof(ULONG);
2279
2280         return ERROR_SUCCESS;
2281
2282     case INTERNET_OPTION_PROXY: {
2283         appinfo_t ai;
2284         BOOL ret;
2285
2286         TRACE("Getting global proxy info\n");
2287         memset(&ai, 0, sizeof(appinfo_t));
2288         INTERNET_ConfigureProxy(&ai);
2289
2290         ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2291         APPINFO_Destroy(&ai.hdr);
2292         return ret;
2293     }
2294
2295     case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2296         TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2297
2298         if (*size < sizeof(ULONG))
2299             return ERROR_INSUFFICIENT_BUFFER;
2300
2301         *(ULONG*)buffer = 2;
2302         *size = sizeof(ULONG);
2303
2304         return ERROR_SUCCESS;
2305
2306     case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2307             TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2308
2309             if (*size < sizeof(ULONG))
2310                 return ERROR_INSUFFICIENT_BUFFER;
2311
2312             *(ULONG*)buffer = 4;
2313             *size = sizeof(ULONG);
2314
2315             return ERROR_SUCCESS;
2316
2317     case INTERNET_OPTION_SECURITY_FLAGS:
2318         FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2319         return ERROR_SUCCESS;
2320
2321     case INTERNET_OPTION_VERSION: {
2322         static const INTERNET_VERSION_INFO info = { 1, 2 };
2323
2324         TRACE("INTERNET_OPTION_VERSION\n");
2325
2326         if (*size < sizeof(INTERNET_VERSION_INFO))
2327             return ERROR_INSUFFICIENT_BUFFER;
2328
2329         memcpy(buffer, &info, sizeof(info));
2330         *size = sizeof(info);
2331
2332         return ERROR_SUCCESS;
2333     }
2334
2335     case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2336         INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2337         INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2338         DWORD res = ERROR_SUCCESS, i;
2339         proxyinfo_t pi;
2340         LONG ret;
2341
2342         TRACE("Getting global proxy info\n");
2343         if((ret = INTERNET_LoadProxySettings(&pi)))
2344             return ret;
2345
2346         FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2347
2348         if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2349             FreeProxyInfo(&pi);
2350             return ERROR_INSUFFICIENT_BUFFER;
2351         }
2352
2353         for (i = 0; i < con->dwOptionCount; i++) {
2354             INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2355             INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2356
2357             switch (optionW->dwOption) {
2358             case INTERNET_PER_CONN_FLAGS:
2359                 if(pi.proxyEnabled)
2360                     optionW->Value.dwValue = PROXY_TYPE_PROXY;
2361                 else
2362                     optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2363                 break;
2364
2365             case INTERNET_PER_CONN_PROXY_SERVER:
2366                 if (unicode)
2367                     optionW->Value.pszValue = heap_strdupW(pi.proxy);
2368                 else
2369                     optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2370                 break;
2371
2372             case INTERNET_PER_CONN_PROXY_BYPASS:
2373                 if (unicode)
2374                     optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2375                 else
2376                     optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2377                 break;
2378
2379             case INTERNET_PER_CONN_AUTOCONFIG_URL:
2380             case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2381             case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2382             case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2383             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2384             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2385                 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2386                 memset(&optionW->Value, 0, sizeof(optionW->Value));
2387                 break;
2388
2389             default:
2390                 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2391                 res = ERROR_INVALID_PARAMETER;
2392                 break;
2393             }
2394         }
2395         FreeProxyInfo(&pi);
2396
2397         return res;
2398     }
2399     case INTERNET_OPTION_USER_AGENT:
2400         return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2401     case INTERNET_OPTION_POLICY:
2402         return ERROR_INVALID_PARAMETER;
2403     case INTERNET_OPTION_CONTEXT_VALUE:
2404     {
2405         if (!hdr)
2406             return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2407         if (!size)
2408             return ERROR_INVALID_PARAMETER;
2409
2410         if (*size < sizeof(DWORD_PTR))
2411         {
2412             *size = sizeof(DWORD_PTR);
2413             return ERROR_INSUFFICIENT_BUFFER;
2414         }
2415         if (!buffer)
2416             return ERROR_INVALID_PARAMETER;
2417
2418         *(DWORD_PTR *)buffer = hdr->dwContext;
2419         *size = sizeof(DWORD_PTR);
2420         return ERROR_SUCCESS;
2421     }
2422     }
2423
2424     FIXME("Stub for %d\n", option);
2425     return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2426 }
2427
2428 /***********************************************************************
2429  *           InternetQueryOptionW (WININET.@)
2430  *
2431  * Queries an options on the specified handle
2432  *
2433  * RETURNS
2434  *    TRUE  on success
2435  *    FALSE on failure
2436  *
2437  */
2438 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2439                                  LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2440 {
2441     object_header_t *hdr;
2442     DWORD res = ERROR_INVALID_HANDLE;
2443
2444     TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2445
2446     if(hInternet) {
2447         hdr = get_handle_object(hInternet);
2448         if (hdr) {
2449             res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2450             WININET_Release(hdr);
2451         }
2452     }else {
2453         res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2454     }
2455
2456     if(res != ERROR_SUCCESS)
2457         SetLastError(res);
2458     return res == ERROR_SUCCESS;
2459 }
2460
2461 /***********************************************************************
2462  *           InternetQueryOptionA (WININET.@)
2463  *
2464  * Queries an options on the specified handle
2465  *
2466  * RETURNS
2467  *    TRUE  on success
2468  *    FALSE on failure
2469  *
2470  */
2471 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2472                                  LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2473 {
2474     object_header_t *hdr;
2475     DWORD res = ERROR_INVALID_HANDLE;
2476
2477     TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2478
2479     if(hInternet) {
2480         hdr = get_handle_object(hInternet);
2481         if (hdr) {
2482             res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2483             WININET_Release(hdr);
2484         }
2485     }else {
2486         res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2487     }
2488
2489     if(res != ERROR_SUCCESS)
2490         SetLastError(res);
2491     return res == ERROR_SUCCESS;
2492 }
2493
2494
2495 /***********************************************************************
2496  *           InternetSetOptionW (WININET.@)
2497  *
2498  * Sets an options on the specified handle
2499  *
2500  * RETURNS
2501  *    TRUE  on success
2502  *    FALSE on failure
2503  *
2504  */
2505 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2506                            LPVOID lpBuffer, DWORD dwBufferLength)
2507 {
2508     object_header_t *lpwhh;
2509     BOOL ret = TRUE;
2510
2511     TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2512
2513     lpwhh = (object_header_t*) get_handle_object( hInternet );
2514     if(lpwhh && lpwhh->vtbl->SetOption) {
2515         DWORD res;
2516
2517         res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2518         if(res != ERROR_INTERNET_INVALID_OPTION) {
2519             WININET_Release( lpwhh );
2520
2521             if(res != ERROR_SUCCESS)
2522                 SetLastError(res);
2523
2524             return res == ERROR_SUCCESS;
2525         }
2526     }
2527
2528     switch (dwOption)
2529     {
2530     case INTERNET_OPTION_CALLBACK:
2531       {
2532         if (!lpwhh)
2533         {
2534             SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2535             return FALSE;
2536         }
2537         WININET_Release(lpwhh);
2538         SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2539         return FALSE;
2540       }
2541     case INTERNET_OPTION_HTTP_VERSION:
2542       {
2543         HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2544         FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2545       }
2546       break;
2547     case INTERNET_OPTION_ERROR_MASK:
2548       {
2549         if(!lpwhh) {
2550             SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2551             return FALSE;
2552         } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2553                         INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2554                         INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2555             SetLastError(ERROR_INVALID_PARAMETER);
2556             ret = FALSE;
2557         } else if(dwBufferLength != sizeof(ULONG)) {
2558             SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2559             ret = FALSE;
2560         } else
2561             lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2562       }
2563       break;
2564     case INTERNET_OPTION_PROXY:
2565     {
2566         INTERNET_PROXY_INFOW *info = lpBuffer;
2567
2568         if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2569         {
2570             SetLastError(ERROR_INVALID_PARAMETER);
2571             return FALSE;
2572         }
2573         if (!hInternet)
2574         {
2575             EnterCriticalSection( &WININET_cs );
2576             free_global_proxy();
2577             global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2578             if (global_proxy)
2579             {
2580                 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2581                 {
2582                     global_proxy->proxyEnabled = 1;
2583                     global_proxy->proxy = heap_strdupW( info->lpszProxy );
2584                     global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2585                 }
2586                 else
2587                 {
2588                     global_proxy->proxyEnabled = 0;
2589                     global_proxy->proxy = global_proxy->proxyBypass = NULL;
2590                 }
2591             }
2592             LeaveCriticalSection( &WININET_cs );
2593         }
2594         else
2595         {
2596             /* In general, each type of object should handle
2597              * INTERNET_OPTION_PROXY directly.  This FIXME ensures it doesn't
2598              * get silently dropped.
2599              */
2600             FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2601             SetLastError(ERROR_INTERNET_INVALID_OPTION);
2602             ret = FALSE;
2603         }
2604         break;
2605     }
2606     case INTERNET_OPTION_CODEPAGE:
2607       {
2608         ULONG codepage = *(ULONG *)lpBuffer;
2609         FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2610       }
2611       break;
2612     case INTERNET_OPTION_REQUEST_PRIORITY:
2613       {
2614         ULONG priority = *(ULONG *)lpBuffer;
2615         FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2616       }
2617       break;
2618     case INTERNET_OPTION_CONNECT_TIMEOUT:
2619       {
2620         ULONG connecttimeout = *(ULONG *)lpBuffer;
2621         FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2622       }
2623       break;
2624     case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2625       {
2626         ULONG receivetimeout = *(ULONG *)lpBuffer;
2627         FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2628       }
2629       break;
2630     case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2631       {
2632         ULONG conns = *(ULONG *)lpBuffer;
2633         FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2634       }
2635       break;
2636     case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2637       {
2638         ULONG conns = *(ULONG *)lpBuffer;
2639         FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2640       }
2641       break;
2642     case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2643         FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2644         break;
2645     case INTERNET_OPTION_END_BROWSER_SESSION:
2646         FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2647         break;
2648     case INTERNET_OPTION_CONNECTED_STATE:
2649         FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2650         break;
2651     case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2652         TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2653         break;
2654     case INTERNET_OPTION_SEND_TIMEOUT:
2655     case INTERNET_OPTION_RECEIVE_TIMEOUT:
2656     case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2657     {
2658         ULONG timeout = *(ULONG *)lpBuffer;
2659         FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2660         break;
2661     }
2662     case INTERNET_OPTION_CONNECT_RETRIES:
2663     {
2664         ULONG retries = *(ULONG *)lpBuffer;
2665         FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2666         break;
2667     }
2668     case INTERNET_OPTION_CONTEXT_VALUE:
2669     {
2670         if (!lpwhh)
2671         {
2672             SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2673             return FALSE;
2674         }
2675         if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2676         {
2677             SetLastError(ERROR_INVALID_PARAMETER);
2678             ret = FALSE;
2679         }
2680         else
2681             lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2682         break;
2683     }
2684     case INTERNET_OPTION_SECURITY_FLAGS:
2685          FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2686          break;
2687     case INTERNET_OPTION_DISABLE_AUTODIAL:
2688          FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2689          break;
2690     case INTERNET_OPTION_HTTP_DECODING:
2691         FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2692         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2693         ret = FALSE;
2694         break;
2695     case INTERNET_OPTION_COOKIES_3RD_PARTY:
2696         FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2697         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2698         ret = FALSE;
2699         break;
2700     case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2701         FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2702         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2703         ret = FALSE;
2704         break;
2705     case INTERNET_OPTION_CODEPAGE_PATH:
2706         FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2707         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2708         ret = FALSE;
2709         break;
2710     case INTERNET_OPTION_CODEPAGE_EXTRA:
2711         FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2712         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2713         ret = FALSE;
2714         break;
2715     case INTERNET_OPTION_IDN:
2716         FIXME("INTERNET_OPTION_IDN; STUB\n");
2717         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2718         ret = FALSE;
2719         break;
2720     case INTERNET_OPTION_POLICY:
2721         SetLastError(ERROR_INVALID_PARAMETER);
2722         ret = FALSE;
2723         break;
2724     case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2725         INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2726         LONG res;
2727         int i;
2728         proxyinfo_t pi;
2729
2730         INTERNET_LoadProxySettings(&pi);
2731
2732         for (i = 0; i < con->dwOptionCount; i++) {
2733             INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2734
2735             switch (option->dwOption) {
2736             case INTERNET_PER_CONN_PROXY_SERVER:
2737                 heap_free(pi.proxy);
2738                 pi.proxy = heap_strdupW(option->Value.pszValue);
2739                 break;
2740
2741             case INTERNET_PER_CONN_FLAGS:
2742                 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2743                     pi.proxyEnabled = 1;
2744                 else
2745                 {
2746                     if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2747                         FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2748                     pi.proxyEnabled = 0;
2749                 }
2750                 break;
2751
2752             case INTERNET_PER_CONN_AUTOCONFIG_URL:
2753             case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2754             case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2755             case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2756             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2757             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2758             case INTERNET_PER_CONN_PROXY_BYPASS:
2759                 FIXME("Unhandled dwOption %d\n", option->dwOption);
2760                 break;
2761
2762             default:
2763                 FIXME("Unknown dwOption %d\n", option->dwOption);
2764                 SetLastError(ERROR_INVALID_PARAMETER);
2765                 break;
2766             }
2767         }
2768
2769         if ((res = INTERNET_SaveProxySettings(&pi)))
2770             SetLastError(res);
2771
2772         FreeProxyInfo(&pi);
2773
2774         ret = (res == ERROR_SUCCESS);
2775         break;
2776         }
2777     default:
2778         FIXME("Option %d STUB\n",dwOption);
2779         SetLastError(ERROR_INTERNET_INVALID_OPTION);
2780         ret = FALSE;
2781         break;
2782     }
2783
2784     if(lpwhh)
2785         WININET_Release( lpwhh );
2786
2787     return ret;
2788 }
2789
2790
2791 /***********************************************************************
2792  *           InternetSetOptionA (WININET.@)
2793  *
2794  * Sets an options on the specified handle.
2795  *
2796  * RETURNS
2797  *    TRUE  on success
2798  *    FALSE on failure
2799  *
2800  */
2801 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2802                            LPVOID lpBuffer, DWORD dwBufferLength)
2803 {
2804     LPVOID wbuffer;
2805     DWORD wlen;
2806     BOOL r;
2807
2808     switch( dwOption )
2809     {
2810     case INTERNET_OPTION_CALLBACK:
2811         {
2812         object_header_t *lpwh;
2813
2814         if (!(lpwh = get_handle_object(hInternet)))
2815         {
2816             INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2817             return FALSE;
2818         }
2819         WININET_Release(lpwh);
2820         INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2821         return FALSE;
2822         }
2823     case INTERNET_OPTION_PROXY:
2824         {
2825         LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2826         LPINTERNET_PROXY_INFOW piw;
2827         DWORD proxlen, prbylen;
2828         LPWSTR prox, prby;
2829
2830         proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2831         prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2832         wlen = sizeof(*piw) + proxlen + prbylen;
2833         wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2834         piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2835         piw->dwAccessType = pi->dwAccessType;
2836         prox = (LPWSTR) &piw[1];
2837         prby = &prox[proxlen+1];
2838         MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2839         MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2840         piw->lpszProxy = prox;
2841         piw->lpszProxyBypass = prby;
2842         }
2843         break;
2844     case INTERNET_OPTION_USER_AGENT:
2845     case INTERNET_OPTION_USERNAME:
2846     case INTERNET_OPTION_PASSWORD:
2847         wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2848                                    NULL, 0 );
2849         wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2850         MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2851                                    wbuffer, wlen );
2852         break;
2853     case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2854         int i;
2855         INTERNET_PER_CONN_OPTION_LISTW *listW;
2856         INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
2857         wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2858         wbuffer = heap_alloc(wlen);
2859         listW = wbuffer;
2860
2861         listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2862         if (listA->pszConnection)
2863         {
2864             wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
2865             listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
2866             MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
2867         }
2868         else
2869             listW->pszConnection = NULL;
2870         listW->dwOptionCount = listA->dwOptionCount;
2871         listW->dwOptionError = listA->dwOptionError;
2872         listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
2873
2874         for (i = 0; i < listA->dwOptionCount; ++i) {
2875             INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
2876             INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
2877
2878             optW->dwOption = optA->dwOption;
2879
2880             switch (optA->dwOption) {
2881             case INTERNET_PER_CONN_AUTOCONFIG_URL:
2882             case INTERNET_PER_CONN_PROXY_BYPASS:
2883             case INTERNET_PER_CONN_PROXY_SERVER:
2884             case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2885             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2886                 if (optA->Value.pszValue)
2887                 {
2888                     wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
2889                     optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
2890                     MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
2891                 }
2892                 else
2893                     optW->Value.pszValue = NULL;
2894                 break;
2895             case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2896             case INTERNET_PER_CONN_FLAGS:
2897             case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2898                 optW->Value.dwValue = optA->Value.dwValue;
2899                 break;
2900             case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2901                 optW->Value.ftValue = optA->Value.ftValue;
2902                 break;
2903             default:
2904                 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
2905                 optW->Value.dwValue = optA->Value.dwValue;
2906                 break;
2907             }
2908         }
2909         }
2910         break;
2911     default:
2912         wbuffer = lpBuffer;
2913         wlen = dwBufferLength;
2914     }
2915
2916     r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2917
2918     if( lpBuffer != wbuffer )
2919     {
2920         if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
2921         {
2922             INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
2923             int i;
2924             for (i = 0; i < list->dwOptionCount; ++i) {
2925                 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
2926                 switch (opt->dwOption) {
2927                 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2928                 case INTERNET_PER_CONN_PROXY_BYPASS:
2929                 case INTERNET_PER_CONN_PROXY_SERVER:
2930                 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2931                 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2932                     heap_free( opt->Value.pszValue );
2933                     break;
2934                 default:
2935                     break;
2936                 }
2937             }
2938             heap_free( list->pOptions );
2939         }
2940         heap_free( wbuffer );
2941     }
2942
2943     return r;
2944 }
2945
2946
2947 /***********************************************************************
2948  *           InternetSetOptionExA (WININET.@)
2949  */
2950 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2951                            LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2952 {
2953     FIXME("Flags %08x ignored\n", dwFlags);
2954     return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2955 }
2956
2957 /***********************************************************************
2958  *           InternetSetOptionExW (WININET.@)
2959  */
2960 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2961                            LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2962 {
2963     FIXME("Flags %08x ignored\n", dwFlags);
2964     if( dwFlags & ~ISO_VALID_FLAGS )
2965     {
2966         SetLastError( ERROR_INVALID_PARAMETER );
2967         return FALSE;
2968     }
2969     return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2970 }
2971
2972 static const WCHAR WININET_wkday[7][4] =
2973     { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2974       { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2975 static const WCHAR WININET_month[12][4] =
2976     { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2977       { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2978       { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2979
2980 /***********************************************************************
2981  *           InternetTimeFromSystemTimeA (WININET.@)
2982  */
2983 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2984 {
2985     BOOL ret;
2986     WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2987
2988     TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2989
2990     if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2991     {
2992         SetLastError(ERROR_INVALID_PARAMETER);
2993         return FALSE;
2994     }
2995
2996     if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2997     {
2998         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2999         return FALSE;
3000     }
3001
3002     ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3003     if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3004
3005     return ret;
3006 }
3007
3008 /***********************************************************************
3009  *           InternetTimeFromSystemTimeW (WININET.@)
3010  */
3011 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3012 {
3013     static const WCHAR date[] =
3014         { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3015           '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3016
3017     TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3018
3019     if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3020     {
3021         SetLastError(ERROR_INVALID_PARAMETER);
3022         return FALSE;
3023     }
3024
3025     if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3026     {
3027         SetLastError(ERROR_INSUFFICIENT_BUFFER);
3028         return FALSE;
3029     }
3030
3031     sprintfW( string, date,
3032               WININET_wkday[time->wDayOfWeek],
3033               time->wDay,
3034               WININET_month[time->wMonth - 1],
3035               time->wYear,
3036               time->wHour,
3037               time->wMinute,
3038               time->wSecond );
3039
3040     return TRUE;
3041 }
3042
3043 /***********************************************************************
3044  *           InternetTimeToSystemTimeA (WININET.@)
3045  */
3046 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3047 {
3048     BOOL ret = FALSE;
3049     WCHAR *stringW;
3050
3051     TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3052
3053     stringW = heap_strdupAtoW(string);
3054     if (stringW)
3055     {
3056         ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3057         heap_free( stringW );
3058     }
3059     return ret;
3060 }
3061
3062 /***********************************************************************
3063  *           InternetTimeToSystemTimeW (WININET.@)
3064  */
3065 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3066 {
3067     unsigned int i;
3068     const WCHAR *s = string;
3069     WCHAR       *end;
3070
3071     TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3072
3073     if (!string || !time) return FALSE;
3074
3075     /* Windows does this too */
3076     GetSystemTime( time );
3077
3078     /*  Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3079      *  a SYSTEMTIME structure.
3080      */
3081
3082     while (*s && !isalphaW( *s )) s++;
3083     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3084     time->wDayOfWeek = 7;
3085
3086     for (i = 0; i < 7; i++)
3087     {
3088         if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3089             toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3090             toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3091         {
3092             time->wDayOfWeek = i;
3093             break;
3094         }
3095     }
3096
3097     if (time->wDayOfWeek > 6) return TRUE;
3098     while (*s && !isdigitW( *s )) s++;
3099     time->wDay = strtolW( s, &end, 10 );
3100     s = end;
3101
3102     while (*s && !isalphaW( *s )) s++;
3103     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3104     time->wMonth = 0;
3105
3106     for (i = 0; i < 12; i++)
3107     {
3108         if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3109             toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3110             toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3111         {
3112             time->wMonth = i + 1;
3113             break;
3114         }
3115     }
3116     if (time->wMonth == 0) return TRUE;
3117
3118     while (*s && !isdigitW( *s )) s++;
3119     if (*s == '\0') return TRUE;
3120     time->wYear = strtolW( s, &end, 10 );
3121     s = end;
3122
3123     while (*s && !isdigitW( *s )) s++;
3124     if (*s == '\0') return TRUE;
3125     time->wHour = strtolW( s, &end, 10 );
3126     s = end;
3127
3128     while (*s && !isdigitW( *s )) s++;
3129     if (*s == '\0') return TRUE;
3130     time->wMinute = strtolW( s, &end, 10 );
3131     s = end;
3132
3133     while (*s && !isdigitW( *s )) s++;
3134     if (*s == '\0') return TRUE;
3135     time->wSecond = strtolW( s, &end, 10 );
3136     s = end;
3137
3138     time->wMilliseconds = 0;
3139     return TRUE;
3140 }
3141
3142 /***********************************************************************
3143  *      InternetCheckConnectionW (WININET.@)
3144  *
3145  * Pings a requested host to check internet connection
3146  *
3147  * RETURNS
3148  *   TRUE on success and FALSE on failure. If a failure then
3149  *   ERROR_NOT_CONNECTED is placed into GetLastError
3150  *
3151  */
3152 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3153 {
3154 /*
3155  * this is a kludge which runs the resident ping program and reads the output.
3156  *
3157  * Anyone have a better idea?
3158  */
3159
3160   BOOL   rc = FALSE;
3161   static const CHAR ping[] = "ping -c 1 ";
3162   static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3163   CHAR *command = NULL;
3164   WCHAR hostW[1024];
3165   DWORD len;
3166   INTERNET_PORT port;
3167   int status = -1;
3168
3169   FIXME("\n");
3170
3171   /*
3172    * Crack or set the Address
3173    */
3174   if (lpszUrl == NULL)
3175   {
3176      /*
3177       * According to the doc we are supposed to use the ip for the next
3178       * server in the WnInet internal server database. I have
3179       * no idea what that is or how to get it.
3180       *
3181       * So someone needs to implement this.
3182       */
3183      FIXME("Unimplemented with URL of NULL\n");
3184      return TRUE;
3185   }
3186   else
3187   {
3188      URL_COMPONENTSW components;
3189
3190      ZeroMemory(&components,sizeof(URL_COMPONENTSW));
3191      components.lpszHostName = (LPWSTR)hostW;
3192      components.dwHostNameLength = 1024;
3193
3194      if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3195        goto End;
3196
3197      TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
3198      port = components.nPort;
3199      TRACE("port: %d\n", port);
3200   }
3201
3202   if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3203   {
3204       struct sockaddr_storage saddr;
3205       socklen_t sa_len = sizeof(saddr);
3206       int fd;
3207
3208       if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
3209           goto End;
3210       fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3211       if (fd != -1)
3212       {
3213           if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3214               rc = TRUE;
3215           close(fd);
3216       }
3217   }
3218   else
3219   {
3220       /*
3221        * Build our ping command
3222        */
3223       len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
3224       command = heap_alloc(strlen(ping)+len+strlen(redirect));
3225       strcpy(command,ping);
3226       WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
3227       strcat(command,redirect);
3228
3229       TRACE("Ping command is : %s\n",command);
3230
3231       status = system(command);
3232
3233       TRACE("Ping returned a code of %i\n",status);
3234
3235       /* Ping return code of 0 indicates success */
3236       if (status == 0)
3237          rc = TRUE;
3238   }
3239
3240 End:
3241   heap_free( command );
3242   if (rc == FALSE)
3243     INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3244
3245   return rc;
3246 }
3247
3248
3249 /***********************************************************************
3250  *      InternetCheckConnectionA (WININET.@)
3251  *
3252  * Pings a requested host to check internet connection
3253  *
3254  * RETURNS
3255  *   TRUE on success and FALSE on failure. If a failure then
3256  *   ERROR_NOT_CONNECTED is placed into GetLastError
3257  *
3258  */
3259 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3260 {
3261     WCHAR *url = NULL;
3262     BOOL rc;
3263
3264     if(lpszUrl) {
3265         url = heap_strdupAtoW(lpszUrl);
3266         if(!url)
3267             return FALSE;
3268     }
3269
3270     rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3271
3272     heap_free(url);
3273     return rc;
3274 }
3275
3276
3277 /**********************************************************
3278  *      INTERNET_InternetOpenUrlW (internal)
3279  *
3280  * Opens an URL
3281  *
3282  * RETURNS
3283  *   handle of connection or NULL on failure
3284  */
3285 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3286     LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3287 {
3288     URL_COMPONENTSW urlComponents;
3289     WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
3290     WCHAR password[1024], path[2048], extra[1024];
3291     HINTERNET client = NULL, client1 = NULL;
3292     DWORD res;
3293     
3294     TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3295           dwHeadersLength, dwFlags, dwContext);
3296     
3297     urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3298     urlComponents.lpszScheme = protocol;
3299     urlComponents.dwSchemeLength = 32;
3300     urlComponents.lpszHostName = hostName;
3301     urlComponents.dwHostNameLength = MAXHOSTNAME;
3302     urlComponents.lpszUserName = userName;
3303     urlComponents.dwUserNameLength = 1024;
3304     urlComponents.lpszPassword = password;
3305     urlComponents.dwPasswordLength = 1024;
3306     urlComponents.lpszUrlPath = path;
3307     urlComponents.dwUrlPathLength = 2048;
3308     urlComponents.lpszExtraInfo = extra;
3309     urlComponents.dwExtraInfoLength = 1024;
3310     if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3311         return NULL;
3312     switch(urlComponents.nScheme) {
3313     case INTERNET_SCHEME_FTP:
3314         if(urlComponents.nPort == 0)
3315             urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
3316         client = FTP_Connect(hIC, hostName, urlComponents.nPort,
3317                              userName, password, dwFlags, dwContext, INET_OPENURL);
3318         if(client == NULL)
3319             break;
3320         client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3321         if(client1 == NULL) {
3322             InternetCloseHandle(client);
3323             break;
3324         }
3325         break;
3326         
3327     case INTERNET_SCHEME_HTTP:
3328     case INTERNET_SCHEME_HTTPS: {
3329         static const WCHAR szStars[] = { '*','/','*', 0 };
3330         LPCWSTR accept[2] = { szStars, NULL };
3331         if(urlComponents.nPort == 0) {
3332             if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
3333                 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3334             else
3335                 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3336         }
3337         if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3338
3339         /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3340         res = HTTP_Connect(hIC, hostName, urlComponents.nPort,
3341                            userName, password, dwFlags, dwContext, INET_OPENURL, &client);
3342         if(res != ERROR_SUCCESS) {
3343             INTERNET_SetLastError(res);
3344             break;
3345         }
3346
3347         if (urlComponents.dwExtraInfoLength) {
3348                 WCHAR *path_extra;
3349                 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
3350
3351                 if (!(path_extra = heap_alloc(len * sizeof(WCHAR))))
3352                 {
3353                         InternetCloseHandle(client);
3354                         break;
3355                 }
3356                 strcpyW(path_extra, urlComponents.lpszUrlPath);
3357                 strcatW(path_extra, urlComponents.lpszExtraInfo);
3358                 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
3359                 heap_free(path_extra);
3360         }
3361         else
3362                 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3363
3364         if(client1 == NULL) {
3365             InternetCloseHandle(client);
3366             break;
3367         }
3368         HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3369         if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3370             GetLastError() != ERROR_IO_PENDING) {
3371             InternetCloseHandle(client1);
3372             client1 = NULL;
3373             break;
3374         }
3375     }
3376     case INTERNET_SCHEME_GOPHER:
3377         /* gopher doesn't seem to be implemented in wine, but it's supposed
3378          * to be supported by InternetOpenUrlA. */
3379     default:
3380         SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3381         break;
3382     }
3383
3384     TRACE(" %p <--\n", client1);
3385     
3386     return client1;
3387 }
3388
3389 /**********************************************************
3390  *      InternetOpenUrlW (WININET.@)
3391  *
3392  * Opens an URL
3393  *
3394  * RETURNS
3395  *   handle of connection or NULL on failure
3396  */
3397 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
3398 {
3399     struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
3400     appinfo_t *hIC = (appinfo_t*) workRequest->hdr;
3401
3402     TRACE("%p\n", hIC);
3403
3404     INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
3405                               req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
3406     heap_free(req->lpszUrl);
3407     heap_free(req->lpszHeaders);
3408 }
3409
3410 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3411     LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3412 {
3413     HINTERNET ret = NULL;
3414     appinfo_t *hIC = NULL;
3415
3416     if (TRACE_ON(wininet)) {
3417         TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3418               dwHeadersLength, dwFlags, dwContext);
3419         TRACE("  flags :");
3420         dump_INTERNET_FLAGS(dwFlags);
3421     }
3422
3423     if (!lpszUrl)
3424     {
3425         SetLastError(ERROR_INVALID_PARAMETER);
3426         goto lend;
3427     }
3428
3429     hIC = (appinfo_t*)get_handle_object( hInternet );
3430     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT) {
3431         SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3432         goto lend;
3433     }
3434     
3435     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3436         WORKREQUEST workRequest;
3437         struct WORKREQ_INTERNETOPENURLW *req;
3438
3439         workRequest.asyncproc = AsyncInternetOpenUrlProc;
3440         workRequest.hdr = WININET_AddRef( &hIC->hdr );
3441         req = &workRequest.u.InternetOpenUrlW;
3442         req->lpszUrl = heap_strdupW(lpszUrl);
3443         req->lpszHeaders = heap_strdupW(lpszHeaders);
3444         req->dwHeadersLength = dwHeadersLength;
3445         req->dwFlags = dwFlags;
3446         req->dwContext = dwContext;
3447         
3448         INTERNET_AsyncCall(&workRequest);
3449         /*
3450          * This is from windows.
3451          */
3452         SetLastError(ERROR_IO_PENDING);
3453     } else {
3454         ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3455     }
3456     
3457   lend:
3458     if( hIC )
3459         WININET_Release( &hIC->hdr );
3460     TRACE(" %p <--\n", ret);
3461     
3462     return ret;
3463 }
3464
3465 /**********************************************************
3466  *      InternetOpenUrlA (WININET.@)
3467  *
3468  * Opens an URL
3469  *
3470  * RETURNS
3471  *   handle of connection or NULL on failure
3472  */
3473 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3474     LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3475 {
3476     HINTERNET rc = NULL;
3477     DWORD lenHeaders = 0;
3478     LPWSTR szUrl = NULL;
3479     LPWSTR szHeaders = NULL;
3480
3481     TRACE("\n");
3482
3483     if(lpszUrl) {
3484         szUrl = heap_strdupAtoW(lpszUrl);
3485         if(!szUrl)
3486             return NULL;
3487     }
3488
3489     if(lpszHeaders) {
3490         lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3491         szHeaders = heap_alloc(lenHeaders*sizeof(WCHAR));
3492         if(!szHeaders) {
3493             heap_free(szUrl);
3494             return NULL;
3495         }
3496         MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3497     }
3498     
3499     rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3500         lenHeaders, dwFlags, dwContext);
3501
3502     heap_free(szUrl);
3503     heap_free(szHeaders);
3504     return rc;
3505 }
3506
3507
3508 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3509 {
3510     LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3511
3512     if (lpwite)
3513     {
3514         lpwite->dwError = 0;
3515         lpwite->response[0] = '\0';
3516     }
3517
3518     if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3519     {
3520         heap_free(lpwite);
3521         return NULL;
3522     }
3523     return lpwite;
3524 }
3525
3526
3527 /***********************************************************************
3528  *           INTERNET_SetLastError (internal)
3529  *
3530  * Set last thread specific error
3531  *
3532  * RETURNS
3533  *
3534  */
3535 void INTERNET_SetLastError(DWORD dwError)
3536 {
3537     LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3538
3539     if (!lpwite)
3540         lpwite = INTERNET_AllocThreadError();
3541
3542     SetLastError(dwError);
3543     if(lpwite)
3544         lpwite->dwError = dwError;
3545 }
3546
3547
3548 /***********************************************************************
3549  *           INTERNET_GetLastError (internal)
3550  *
3551  * Get last thread specific error
3552  *
3553  * RETURNS
3554  *
3555  */
3556 DWORD INTERNET_GetLastError(void)
3557 {
3558     LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3559     if (!lpwite) return 0;
3560     /* TlsGetValue clears last error, so set it again here */
3561     SetLastError(lpwite->dwError);
3562     return lpwite->dwError;
3563 }
3564
3565
3566 /***********************************************************************
3567  *           INTERNET_WorkerThreadFunc (internal)
3568  *
3569  * Worker thread execution function
3570  *
3571  * RETURNS
3572  *
3573  */
3574 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3575 {
3576     LPWORKREQUEST lpRequest = lpvParam;
3577     WORKREQUEST workRequest;
3578
3579     TRACE("\n");
3580
3581     workRequest = *lpRequest;
3582     heap_free(lpRequest);
3583
3584     workRequest.asyncproc(&workRequest);
3585     WININET_Release( workRequest.hdr );
3586
3587     if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3588     {
3589         heap_free(TlsGetValue(g_dwTlsErrIndex));
3590         TlsSetValue(g_dwTlsErrIndex, NULL);
3591     }
3592     return TRUE;
3593 }
3594
3595
3596 /***********************************************************************
3597  *           INTERNET_AsyncCall (internal)
3598  *
3599  * Retrieves work request from queue
3600  *
3601  * RETURNS
3602  *
3603  */
3604 DWORD INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3605 {
3606     BOOL bSuccess;
3607     LPWORKREQUEST lpNewRequest;
3608
3609     TRACE("\n");
3610
3611     lpNewRequest = heap_alloc(sizeof(WORKREQUEST));
3612     if (!lpNewRequest)
3613         return ERROR_OUTOFMEMORY;
3614
3615     *lpNewRequest = *lpWorkRequest;
3616
3617     bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3618     if (!bSuccess)
3619     {
3620         heap_free(lpNewRequest);
3621         return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3622     }
3623     return ERROR_SUCCESS;
3624 }
3625
3626
3627 /***********************************************************************
3628  *          INTERNET_GetResponseBuffer  (internal)
3629  *
3630  * RETURNS
3631  *
3632  */
3633 LPSTR INTERNET_GetResponseBuffer(void)
3634 {
3635     LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3636     if (!lpwite)
3637         lpwite = INTERNET_AllocThreadError();
3638     TRACE("\n");
3639     return lpwite->response;
3640 }
3641
3642 /***********************************************************************
3643  *           INTERNET_GetNextLine  (internal)
3644  *
3645  * Parse next line in directory string listing
3646  *
3647  * RETURNS
3648  *   Pointer to beginning of next line
3649  *   NULL on failure
3650  *
3651  */
3652
3653 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3654 {
3655     struct pollfd pfd;
3656     BOOL bSuccess = FALSE;
3657     INT nRecv = 0;
3658     LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3659
3660     TRACE("\n");
3661
3662     pfd.fd = nSocket;
3663     pfd.events = POLLIN;
3664
3665     while (nRecv < MAX_REPLY_LEN)
3666     {
3667         if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3668         {
3669             if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3670             {
3671                 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3672                 goto lend;
3673             }
3674
3675             if (lpszBuffer[nRecv] == '\n')
3676             {
3677                 bSuccess = TRUE;
3678                 break;
3679             }
3680             if (lpszBuffer[nRecv] != '\r')
3681                 nRecv++;
3682         }
3683         else
3684         {
3685             INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3686             goto lend;
3687         }
3688     }
3689
3690 lend:
3691     if (bSuccess)
3692     {
3693         lpszBuffer[nRecv] = '\0';
3694         *dwLen = nRecv - 1;
3695         TRACE(":%d %s\n", nRecv, lpszBuffer);
3696         return lpszBuffer;
3697     }
3698     else
3699     {
3700         return NULL;
3701     }
3702 }
3703
3704 /**********************************************************
3705  *      InternetQueryDataAvailable (WININET.@)
3706  *
3707  * Determines how much data is available to be read.
3708  *
3709  * RETURNS
3710  *   TRUE on success, FALSE if an error occurred. If
3711  *   INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3712  *   no data is presently available, FALSE is returned with
3713  *   the last error ERROR_IO_PENDING; a callback with status
3714  *   INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3715  *   data is available.
3716  */
3717 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3718                                 LPDWORD lpdwNumberOfBytesAvailble,
3719                                 DWORD dwFlags, DWORD_PTR dwContext)
3720 {
3721     object_header_t *hdr;
3722     DWORD res;
3723
3724     TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3725
3726     hdr = get_handle_object( hFile );
3727     if (!hdr) {
3728         SetLastError(ERROR_INVALID_HANDLE);
3729         return FALSE;
3730     }
3731
3732     if(hdr->vtbl->QueryDataAvailable) {
3733         res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3734     }else {
3735         WARN("wrong handle\n");
3736         res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3737     }
3738
3739     WININET_Release(hdr);
3740
3741     if(res != ERROR_SUCCESS)
3742         SetLastError(res);
3743     return res == ERROR_SUCCESS;
3744 }
3745
3746
3747 /***********************************************************************
3748  *      InternetLockRequestFile (WININET.@)
3749  */
3750 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3751 *lphLockReqHandle)
3752 {
3753     FIXME("STUB\n");
3754     return FALSE;
3755 }
3756
3757 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3758 {
3759     FIXME("STUB\n");
3760     return FALSE;
3761 }
3762
3763
3764 /***********************************************************************
3765  *      InternetAutodial (WININET.@)
3766  *
3767  * On windows this function is supposed to dial the default internet
3768  * connection. We don't want to have Wine dial out to the internet so
3769  * we return TRUE by default. It might be nice to check if we are connected.
3770  *
3771  * RETURNS
3772  *   TRUE on success
3773  *   FALSE on failure
3774  *
3775  */
3776 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3777 {
3778     FIXME("STUB\n");
3779
3780     /* Tell that we are connected to the internet. */
3781     return TRUE;
3782 }
3783
3784 /***********************************************************************
3785  *      InternetAutodialHangup (WININET.@)
3786  *
3787  * Hangs up a connection made with InternetAutodial
3788  *
3789  * PARAM
3790  *    dwReserved
3791  * RETURNS
3792  *   TRUE on success
3793  *   FALSE on failure
3794  *
3795  */
3796 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3797 {
3798     FIXME("STUB\n");
3799
3800     /* we didn't dial, we don't disconnect */
3801     return TRUE;
3802 }
3803
3804 /***********************************************************************
3805  *      InternetCombineUrlA (WININET.@)
3806  *
3807  * Combine a base URL with a relative URL
3808  *
3809  * RETURNS
3810  *   TRUE on success
3811  *   FALSE on failure
3812  *
3813  */
3814
3815 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3816                                 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3817                                 DWORD dwFlags)
3818 {
3819     HRESULT hr=S_OK;
3820
3821     TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3822
3823     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3824     dwFlags ^= ICU_NO_ENCODE;
3825     hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3826
3827     return (hr==S_OK);
3828 }
3829
3830 /***********************************************************************
3831  *      InternetCombineUrlW (WININET.@)
3832  *
3833  * Combine a base URL with a relative URL
3834  *
3835  * RETURNS
3836  *   TRUE on success
3837  *   FALSE on failure
3838  *
3839  */
3840
3841 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3842                                 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3843                                 DWORD dwFlags)
3844 {
3845     HRESULT hr=S_OK;
3846
3847     TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3848
3849     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3850     dwFlags ^= ICU_NO_ENCODE;
3851     hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3852
3853     return (hr==S_OK);
3854 }
3855
3856 /* max port num is 65535 => 5 digits */
3857 #define MAX_WORD_DIGITS 5
3858
3859 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3860     (url)->dw##component##Length : strlenW((url)->lpsz##component))
3861 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3862     (url)->dw##component##Length : strlen((url)->lpsz##component))
3863
3864 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3865 {
3866     if ((nScheme == INTERNET_SCHEME_HTTP) &&
3867         (nPort == INTERNET_DEFAULT_HTTP_PORT))
3868         return TRUE;
3869     if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3870         (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3871         return TRUE;
3872     if ((nScheme == INTERNET_SCHEME_FTP) &&
3873         (nPort == INTERNET_DEFAULT_FTP_PORT))
3874         return TRUE;
3875     if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3876         (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3877         return TRUE;
3878
3879     if (nPort == INTERNET_INVALID_PORT_NUMBER)
3880         return TRUE;
3881
3882     return FALSE;
3883 }
3884
3885 /* opaque urls do not fit into the standard url hierarchy and don't have
3886  * two following slashes */
3887 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3888 {
3889     return (nScheme != INTERNET_SCHEME_FTP) &&
3890            (nScheme != INTERNET_SCHEME_GOPHER) &&
3891            (nScheme != INTERNET_SCHEME_HTTP) &&
3892            (nScheme != INTERNET_SCHEME_HTTPS) &&
3893            (nScheme != INTERNET_SCHEME_FILE);
3894 }
3895
3896 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3897 {
3898     int index;
3899     if (scheme < INTERNET_SCHEME_FIRST)
3900         return NULL;
3901     index = scheme - INTERNET_SCHEME_FIRST;
3902     if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3903         return NULL;
3904     return (LPCWSTR)url_schemes[index];
3905 }
3906
3907 /* we can calculate using ansi strings because we're just
3908  * calculating string length, not size
3909  */
3910 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3911                             LPDWORD lpdwUrlLength)
3912 {
3913     INTERNET_SCHEME nScheme;
3914
3915     *lpdwUrlLength = 0;
3916
3917     if (lpUrlComponents->lpszScheme)
3918     {
3919         DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3920         *lpdwUrlLength += dwLen;
3921         nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3922     }
3923     else
3924     {
3925         LPCWSTR scheme;
3926
3927         nScheme = lpUrlComponents->nScheme;
3928
3929         if (nScheme == INTERNET_SCHEME_DEFAULT)
3930             nScheme = INTERNET_SCHEME_HTTP;
3931         scheme = INTERNET_GetSchemeString(nScheme);
3932         *lpdwUrlLength += strlenW(scheme);
3933     }
3934
3935     (*lpdwUrlLength)++; /* ':' */
3936     if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3937         *lpdwUrlLength += strlen("//");
3938
3939     if (lpUrlComponents->lpszUserName)
3940     {
3941         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3942         *lpdwUrlLength += strlen("@");
3943     }
3944     else
3945     {
3946         if (lpUrlComponents->lpszPassword)
3947         {
3948             INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3949             return FALSE;
3950         }
3951     }
3952
3953     if (lpUrlComponents->lpszPassword)
3954     {
3955         *lpdwUrlLength += strlen(":");
3956         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3957     }
3958
3959     if (lpUrlComponents->lpszHostName)
3960     {
3961         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3962
3963         if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3964         {
3965             char szPort[MAX_WORD_DIGITS+1];
3966
3967             sprintf(szPort, "%d", lpUrlComponents->nPort);
3968             *lpdwUrlLength += strlen(szPort);
3969             *lpdwUrlLength += strlen(":");
3970         }
3971
3972         if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3973             (*lpdwUrlLength)++; /* '/' */
3974     }
3975
3976     if (lpUrlComponents->lpszUrlPath)
3977         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3978
3979     if (lpUrlComponents->lpszExtraInfo)
3980         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3981
3982     return TRUE;
3983 }
3984
3985 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3986 {
3987     INT len;
3988
3989     ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3990
3991     urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3992     urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3993     urlCompW->nScheme = lpUrlComponents->nScheme;
3994     urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3995     urlCompW->nPort = lpUrlComponents->nPort;
3996     urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3997     urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3998     urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3999     urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4000
4001     if (lpUrlComponents->lpszScheme)
4002     {
4003         len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4004         urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4005         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4006                             -1, urlCompW->lpszScheme, len);
4007     }
4008
4009     if (lpUrlComponents->lpszHostName)
4010     {
4011         len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4012         urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4013         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4014                             -1, urlCompW->lpszHostName, len);
4015     }
4016
4017     if (lpUrlComponents->lpszUserName)
4018     {
4019         len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4020         urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4021         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4022                             -1, urlCompW->lpszUserName, len);
4023     }
4024
4025     if (lpUrlComponents->lpszPassword)
4026     {
4027         len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4028         urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4029         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4030                             -1, urlCompW->lpszPassword, len);
4031     }
4032
4033     if (lpUrlComponents->lpszUrlPath)
4034     {
4035         len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4036         urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4037         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4038                             -1, urlCompW->lpszUrlPath, len);
4039     }
4040
4041     if (lpUrlComponents->lpszExtraInfo)
4042     {
4043         len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4044         urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4045         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4046                             -1, urlCompW->lpszExtraInfo, len);
4047     }
4048 }
4049
4050 /***********************************************************************
4051  *      InternetCreateUrlA (WININET.@)
4052  *
4053  * See InternetCreateUrlW.
4054  */
4055 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4056                                LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4057 {
4058     BOOL ret;
4059     LPWSTR urlW = NULL;
4060     URL_COMPONENTSW urlCompW;
4061
4062     TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4063
4064     if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4065     {
4066         SetLastError(ERROR_INVALID_PARAMETER);
4067         return FALSE;
4068     }
4069
4070     convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4071
4072     if (lpszUrl)
4073         urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4074
4075     ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4076
4077     if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4078         *lpdwUrlLength /= sizeof(WCHAR);
4079
4080     /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4081     * minus one, so add one to leave room for NULL terminator
4082     */
4083     if (ret)
4084         WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4085
4086     heap_free(urlCompW.lpszScheme);
4087     heap_free(urlCompW.lpszHostName);
4088     heap_free(urlCompW.lpszUserName);
4089     heap_free(urlCompW.lpszPassword);
4090     heap_free(urlCompW.lpszUrlPath);
4091     heap_free(urlCompW.lpszExtraInfo);
4092     heap_free(urlW);
4093     return ret;
4094 }
4095
4096 /***********************************************************************
4097  *      InternetCreateUrlW (WININET.@)
4098  *
4099  * Creates a URL from its component parts.
4100  *
4101  * PARAMS
4102  *  lpUrlComponents [I] URL Components.
4103  *  dwFlags         [I] Flags. See notes.
4104  *  lpszUrl         [I] Buffer in which to store the created URL.
4105  *  lpdwUrlLength   [I/O] On input, the length of the buffer pointed to by
4106  *                        lpszUrl in characters. On output, the number of bytes
4107  *                        required to store the URL including terminator.
4108  *
4109  * NOTES
4110  *
4111  * The dwFlags parameter can be zero or more of the following:
4112  *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4113  *
4114  * RETURNS
4115  *   TRUE on success
4116  *   FALSE on failure
4117  *
4118  */
4119 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4120                                LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4121 {
4122     DWORD dwLen;
4123     INTERNET_SCHEME nScheme;
4124
4125     static const WCHAR slashSlashW[] = {'/','/'};
4126     static const WCHAR fmtW[] = {'%','u',0};
4127
4128     TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4129
4130     if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4131     {
4132         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4133         return FALSE;
4134     }
4135
4136     if (!calc_url_length(lpUrlComponents, &dwLen))
4137         return FALSE;
4138
4139     if (!lpszUrl || *lpdwUrlLength < dwLen)
4140     {
4141         *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4142         INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4143         return FALSE;
4144     }
4145
4146     *lpdwUrlLength = dwLen;
4147     lpszUrl[0] = 0x00;
4148
4149     dwLen = 0;
4150
4151     if (lpUrlComponents->lpszScheme)
4152     {
4153         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4154         memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4155         lpszUrl += dwLen;
4156
4157         nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4158     }
4159     else
4160     {
4161         LPCWSTR scheme;
4162         nScheme = lpUrlComponents->nScheme;
4163
4164         if (nScheme == INTERNET_SCHEME_DEFAULT)
4165             nScheme = INTERNET_SCHEME_HTTP;
4166
4167         scheme = INTERNET_GetSchemeString(nScheme);
4168         dwLen = strlenW(scheme);
4169         memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4170         lpszUrl += dwLen;
4171     }
4172
4173     /* all schemes are followed by at least a colon */
4174     *lpszUrl = ':';
4175     lpszUrl++;
4176
4177     if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4178     {
4179         memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4180         lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4181     }
4182
4183     if (lpUrlComponents->lpszUserName)
4184     {
4185         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4186         memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4187         lpszUrl += dwLen;
4188
4189         if (lpUrlComponents->lpszPassword)
4190         {
4191             *lpszUrl = ':';
4192             lpszUrl++;
4193
4194             dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4195             memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4196             lpszUrl += dwLen;
4197         }
4198
4199         *lpszUrl = '@';
4200         lpszUrl++;
4201     }
4202
4203     if (lpUrlComponents->lpszHostName)
4204     {
4205         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4206         memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4207         lpszUrl += dwLen;
4208
4209         if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4210         {
4211             WCHAR szPort[MAX_WORD_DIGITS+1];
4212
4213             sprintfW(szPort, fmtW, lpUrlComponents->nPort);
4214             *lpszUrl = ':';
4215             lpszUrl++;
4216             dwLen = strlenW(szPort);
4217             memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4218             lpszUrl += dwLen;
4219         }
4220
4221         /* add slash between hostname and path if necessary */
4222         if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4223         {
4224             *lpszUrl = '/';
4225             lpszUrl++;
4226         }
4227     }
4228
4229     if (lpUrlComponents->lpszUrlPath)
4230     {
4231         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4232         memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4233         lpszUrl += dwLen;
4234     }
4235
4236     if (lpUrlComponents->lpszExtraInfo)
4237     {
4238         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4239         memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4240         lpszUrl += dwLen;
4241     }
4242
4243     *lpszUrl = '\0';
4244
4245     return TRUE;
4246 }
4247
4248 /***********************************************************************
4249  *      InternetConfirmZoneCrossingA (WININET.@)
4250  *
4251  */
4252 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4253 {
4254     FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4255     return ERROR_SUCCESS;
4256 }
4257
4258 /***********************************************************************
4259  *      InternetConfirmZoneCrossingW (WININET.@)
4260  *
4261  */
4262 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4263 {
4264     FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4265     return ERROR_SUCCESS;
4266 }
4267
4268 static DWORD zone_preference = 3;
4269
4270 /***********************************************************************
4271  *      PrivacySetZonePreferenceW (WININET.@)
4272  */
4273 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4274 {
4275     FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4276
4277     zone_preference = template;
4278     return 0;
4279 }
4280
4281 /***********************************************************************
4282  *      PrivacyGetZonePreferenceW (WININET.@)
4283  */
4284 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4285                                         LPWSTR preference, LPDWORD length )
4286 {
4287     FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4288
4289     if (template) *template = zone_preference;
4290     return 0;
4291 }
4292
4293 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4294                             DWORD_PTR* lpdwConnection, DWORD dwReserved )
4295 {
4296     FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4297           lpdwConnection, dwReserved);
4298     return ERROR_SUCCESS;
4299 }
4300
4301 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4302                             DWORD_PTR* lpdwConnection, DWORD dwReserved )
4303 {
4304     FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4305           lpdwConnection, dwReserved);
4306     return ERROR_SUCCESS;
4307 }
4308
4309 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4310 {
4311     FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4312     return TRUE;
4313 }
4314
4315 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4316 {
4317     FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4318     return TRUE;
4319 }
4320
4321 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4322 {
4323     FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4324     return ERROR_SUCCESS;
4325 }
4326
4327 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4328                               PBYTE pbHexHash )
4329 {
4330     FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4331           debugstr_w(pwszTarget), pbHexHash);
4332     return FALSE;
4333 }
4334
4335 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4336 {
4337     FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4338     return FALSE;
4339 }
4340
4341 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4342 {
4343     FIXME("(%p, %08lx) stub\n", a, b);
4344     return 0;
4345 }