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