dinput8: DirectInput8Create rewrite.
[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
1327     if (!lpszUrl_orig || !*lpszUrl_orig)
1328     {
1329         SetLastError(ERROR_INVALID_PARAMETER);
1330         return FALSE;
1331     }
1332
1333     if (dwFlags & ICU_DECODE)
1334     {
1335         lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0,  dwUrlLength * sizeof (WCHAR) );
1336         if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1337         {
1338             lpszUrl =  lpszUrl_decode;
1339         }
1340     }
1341     lpszap = lpszUrl;
1342     
1343     /* Determine if the URI is absolute. */
1344     while (*lpszap != '\0')
1345     {
1346         if (isalnumW(*lpszap))
1347         {
1348             lpszap++;
1349             continue;
1350         }
1351         if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1352         {
1353             bIsAbsolute = TRUE;
1354             lpszcp = lpszap;
1355         }
1356         else
1357         {
1358             lpszcp = lpszUrl; /* Relative url */
1359         }
1360
1361         break;
1362     }
1363
1364     lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1365     lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1366
1367     /* Parse <params> */
1368     lpszParam = strpbrkW(lpszap, lpszSeparators);
1369     SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1370                           lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1371
1372     if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1373     {
1374         LPCWSTR lpszNetLoc;
1375
1376         /* Get scheme first. */
1377         lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1378         SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1379                                    lpszUrl, lpszcp - lpszUrl);
1380
1381         /* Eat ':' in protocol. */
1382         lpszcp++;
1383
1384         /* double slash indicates the net_loc portion is present */
1385         if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1386         {
1387             lpszcp += 2;
1388
1389             lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1390             if (lpszParam)
1391             {
1392                 if (lpszNetLoc)
1393                     lpszNetLoc = min(lpszNetLoc, lpszParam);
1394                 else
1395                     lpszNetLoc = lpszParam;
1396             }
1397             else if (!lpszNetLoc)
1398                 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1399
1400             /* Parse net-loc */
1401             if (lpszNetLoc)
1402             {
1403                 LPCWSTR lpszHost;
1404                 LPCWSTR lpszPort;
1405
1406                 /* [<user>[<:password>]@]<host>[:<port>] */
1407                 /* First find the user and password if they exist */
1408
1409                 lpszHost = strchrW(lpszcp, '@');
1410                 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1411                 {
1412                     /* username and password not specified. */
1413                     SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1414                     SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1415                 }
1416                 else /* Parse out username and password */
1417                 {
1418                     LPCWSTR lpszUser = lpszcp;
1419                     LPCWSTR lpszPasswd = lpszHost;
1420
1421                     while (lpszcp < lpszHost)
1422                     {
1423                         if (*lpszcp == ':')
1424                             lpszPasswd = lpszcp;
1425
1426                         lpszcp++;
1427                     }
1428
1429                     SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1430                                           lpszUser, lpszPasswd - lpszUser);
1431
1432                     if (lpszPasswd != lpszHost)
1433                         lpszPasswd++;
1434                     SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1435                                           lpszPasswd == lpszHost ? NULL : lpszPasswd,
1436                                           lpszHost - lpszPasswd);
1437
1438                     lpszcp++; /* Advance to beginning of host */
1439                 }
1440
1441                 /* Parse <host><:port> */
1442
1443                 lpszHost = lpszcp;
1444                 lpszPort = lpszNetLoc;
1445
1446                 /* special case for res:// URLs: there is no port here, so the host is the
1447                    entire string up to the first '/' */
1448                 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1449                 {
1450                     SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1451                                           lpszHost, lpszPort - lpszHost);
1452                     lpszcp=lpszNetLoc;
1453                 }
1454                 else
1455                 {
1456                     while (lpszcp < lpszNetLoc)
1457                     {
1458                         if (*lpszcp == ':')
1459                             lpszPort = lpszcp;
1460
1461                         lpszcp++;
1462                     }
1463
1464                     /* If the scheme is "file" and the host is just one letter, it's not a host */
1465                     if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1466                     {
1467                         lpszcp=lpszHost;
1468                         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1469                                               NULL, 0);
1470                     }
1471                     else
1472                     {
1473                         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1474                                               lpszHost, lpszPort - lpszHost);
1475                         if (lpszPort != lpszNetLoc)
1476                             lpUC->nPort = atoiW(++lpszPort);
1477                         else switch (lpUC->nScheme)
1478                         {
1479                         case INTERNET_SCHEME_HTTP:
1480                             lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1481                             break;
1482                         case INTERNET_SCHEME_HTTPS:
1483                             lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1484                             break;
1485                         case INTERNET_SCHEME_FTP:
1486                             lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1487                             break;
1488                         case INTERNET_SCHEME_GOPHER:
1489                             lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1490                             break;
1491                         default:
1492                             break;
1493                         }
1494                     }
1495                 }
1496             }
1497         }
1498         else
1499         {
1500             SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1501             SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1502             SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1503         }
1504     }
1505     else
1506     {
1507         SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1508         SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1509         SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1510         SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1511     }
1512
1513     /* Here lpszcp points to:
1514      *
1515      * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1516      *                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1517      */
1518     if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1519     {
1520         INT len;
1521
1522         /* Only truncate the parameter list if it's already been saved
1523          * in lpUC->lpszExtraInfo.
1524          */
1525         if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1526             len = lpszParam - lpszcp;
1527         else
1528         {
1529             /* Leave the parameter list in lpszUrlPath.  Strip off any trailing
1530              * newlines if necessary.
1531              */
1532             LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1533             if (lpsznewline != NULL)
1534                 len = lpsznewline - lpszcp;
1535             else
1536                 len = dwUrlLength-(lpszcp-lpszUrl);
1537         }
1538         SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1539                                    lpszcp, len);
1540     }
1541     else
1542     {
1543         lpUC->dwUrlPathLength = 0;
1544     }
1545
1546     TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1547              debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1548              debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1549              debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1550              debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1551
1552     HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1553     return TRUE;
1554 }
1555
1556 /***********************************************************************
1557  *           InternetAttemptConnect (WININET.@)
1558  *
1559  * Attempt to make a connection to the internet
1560  *
1561  * RETURNS
1562  *    ERROR_SUCCESS on success
1563  *    Error value   on failure
1564  *
1565  */
1566 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1567 {
1568     FIXME("Stub\n");
1569     return ERROR_SUCCESS;
1570 }
1571
1572
1573 /***********************************************************************
1574  *           InternetCanonicalizeUrlA (WININET.@)
1575  *
1576  * Escape unsafe characters and spaces
1577  *
1578  * RETURNS
1579  *    TRUE on success
1580  *    FALSE on failure
1581  *
1582  */
1583 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1584         LPDWORD lpdwBufferLength, DWORD dwFlags)
1585 {
1586     HRESULT hr;
1587     DWORD dwURLFlags= 0x80000000; /* Don't know what this means */
1588     if(dwFlags & ICU_DECODE)
1589     {
1590         dwURLFlags |= URL_UNESCAPE;
1591         dwFlags &= ~ICU_DECODE;
1592     }
1593
1594     if(dwFlags & ICU_ESCAPE)
1595     {
1596         dwURLFlags |= URL_UNESCAPE;
1597         dwFlags &= ~ICU_ESCAPE;
1598     }
1599     if(dwFlags & ICU_BROWSER_MODE)
1600     {
1601         dwURLFlags |= URL_BROWSER_MODE;
1602         dwFlags &= ~ICU_BROWSER_MODE;
1603     }
1604     if(dwFlags)
1605         FIXME("Unhandled flags 0x%08lx\n", dwFlags);
1606     TRACE("%s %p %p %08lx\n", debugstr_a(lpszUrl), lpszBuffer,
1607         lpdwBufferLength, dwURLFlags);
1608
1609     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1610     dwFlags ^= ICU_NO_ENCODE;
1611
1612     hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1613
1614     return (hr == S_OK) ? TRUE : FALSE;
1615 }
1616
1617 /***********************************************************************
1618  *           InternetCanonicalizeUrlW (WININET.@)
1619  *
1620  * Escape unsafe characters and spaces
1621  *
1622  * RETURNS
1623  *    TRUE on success
1624  *    FALSE on failure
1625  *
1626  */
1627 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1628     LPDWORD lpdwBufferLength, DWORD dwFlags)
1629 {
1630     HRESULT hr;
1631     DWORD dwURLFlags= 0x80000000; /* Don't know what this means */
1632     if(dwFlags & ICU_DECODE)
1633     {
1634         dwURLFlags |= URL_UNESCAPE;
1635         dwFlags &= ~ICU_DECODE;
1636     }
1637
1638     if(dwFlags & ICU_ESCAPE)
1639     {
1640         dwURLFlags |= URL_UNESCAPE;
1641         dwFlags &= ~ICU_ESCAPE;
1642     }
1643     if(dwFlags & ICU_BROWSER_MODE)
1644     {
1645         dwURLFlags |= URL_BROWSER_MODE;
1646         dwFlags &= ~ICU_BROWSER_MODE;
1647     }
1648     if(dwFlags)
1649         FIXME("Unhandled flags 0x%08lx\n", dwFlags);
1650     TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
1651         lpdwBufferLength, dwURLFlags);
1652
1653     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1654     dwFlags ^= ICU_NO_ENCODE;
1655
1656     hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1657
1658     return (hr == S_OK) ? TRUE : FALSE;
1659 }
1660
1661
1662 /***********************************************************************
1663  *           InternetSetStatusCallbackA (WININET.@)
1664  *
1665  * Sets up a callback function which is called as progress is made
1666  * during an operation.
1667  *
1668  * RETURNS
1669  *    Previous callback or NULL         on success
1670  *    INTERNET_INVALID_STATUS_CALLBACK  on failure
1671  *
1672  */
1673 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1674         HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1675 {
1676     INTERNET_STATUS_CALLBACK retVal;
1677     LPWININETHANDLEHEADER lpwh;
1678
1679     TRACE("0x%08lx\n", (ULONG)hInternet);
1680     
1681     lpwh = WININET_GetObject(hInternet);
1682     if (!lpwh)
1683         return INTERNET_INVALID_STATUS_CALLBACK;
1684
1685     lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1686     retVal = lpwh->lpfnStatusCB;
1687     lpwh->lpfnStatusCB = lpfnIntCB;
1688
1689     WININET_Release( lpwh );
1690
1691     return retVal;
1692 }
1693
1694 /***********************************************************************
1695  *           InternetSetStatusCallbackW (WININET.@)
1696  *
1697  * Sets up a callback function which is called as progress is made
1698  * during an operation.
1699  *
1700  * RETURNS
1701  *    Previous callback or NULL         on success
1702  *    INTERNET_INVALID_STATUS_CALLBACK  on failure
1703  *
1704  */
1705 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1706         HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1707 {
1708     INTERNET_STATUS_CALLBACK retVal;
1709     LPWININETHANDLEHEADER lpwh;
1710
1711     TRACE("0x%08lx\n", (ULONG)hInternet);
1712     
1713     lpwh = WININET_GetObject(hInternet);
1714     if (!lpwh)
1715         return INTERNET_INVALID_STATUS_CALLBACK;
1716
1717     lpwh->dwInternalFlags |= INET_CALLBACKW;
1718     retVal = lpwh->lpfnStatusCB;
1719     lpwh->lpfnStatusCB = lpfnIntCB;
1720
1721     WININET_Release( lpwh );
1722
1723     return retVal;
1724 }
1725
1726 /***********************************************************************
1727  *           InternetSetFilePointer (WININET.@)
1728  */
1729 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1730     PVOID pReserved, DWORD dwMoveContext, DWORD dwContext)
1731 {
1732     FIXME("stub\n");
1733     return FALSE;
1734 }
1735
1736 /***********************************************************************
1737  *           InternetWriteFile (WININET.@)
1738  *
1739  * Write data to an open internet file
1740  *
1741  * RETURNS
1742  *    TRUE  on success
1743  *    FALSE on failure
1744  *
1745  */
1746 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
1747         DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1748 {
1749     BOOL retval = FALSE;
1750     int nSocket = -1;
1751     LPWININETHANDLEHEADER lpwh;
1752
1753     TRACE("\n");
1754     lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1755     if (NULL == lpwh)
1756         return FALSE;
1757
1758     switch (lpwh->htype)
1759     {
1760         case WH_HHTTPREQ:
1761             {
1762                 LPWININETHTTPREQW lpwhr;
1763                 lpwhr = (LPWININETHTTPREQW)lpwh;
1764
1765                 TRACE("HTTPREQ %li\n",dwNumOfBytesToWrite);
1766                 retval = NETCON_send(&lpwhr->netConnection, lpBuffer, 
1767                         dwNumOfBytesToWrite, 0, (LPINT)lpdwNumOfBytesWritten);
1768
1769                 WININET_Release( lpwh );
1770                 return retval;
1771             }
1772             break;
1773
1774         case WH_HFILE:
1775             nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1776             break;
1777
1778         default:
1779             break;
1780     }
1781
1782     if (nSocket != -1)
1783     {
1784         int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
1785         retval = (res >= 0);
1786         *lpdwNumOfBytesWritten = retval ? res : 0;
1787     }
1788     WININET_Release( lpwh );
1789
1790     return retval;
1791 }
1792
1793
1794 static BOOL INTERNET_ReadFile(LPWININETHANDLEHEADER lpwh, LPVOID lpBuffer,
1795                               DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead,
1796                               BOOL bWait, BOOL bSendCompletionStatus)
1797 {
1798     BOOL retval = FALSE;
1799     int nSocket = -1;
1800
1801     /* FIXME: this should use NETCON functions! */
1802     switch (lpwh->htype)
1803     {
1804         case WH_HHTTPREQ:
1805             if (!NETCON_recv(&((LPWININETHTTPREQW)lpwh)->netConnection, lpBuffer,
1806                              dwNumOfBytesToRead, bWait ? MSG_WAITALL : 0, (int *)pdwNumOfBytesRead))
1807             {
1808                 *pdwNumOfBytesRead = 0;
1809                 retval = TRUE; /* Under windows, it seems to return 0 even if nothing was read... */
1810             }
1811             else
1812                 retval = TRUE;
1813             break;
1814
1815         case WH_HFILE:
1816             /* FIXME: FTP should use NETCON_ stuff */
1817             nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1818             if (nSocket != -1)
1819             {
1820                 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, bWait ? MSG_WAITALL : 0);
1821                 retval = (res >= 0);
1822                 *pdwNumOfBytesRead = retval ? res : 0;
1823             }
1824             break;
1825
1826         default:
1827             break;
1828     }
1829
1830     if (bSendCompletionStatus)
1831     {
1832         INTERNET_ASYNC_RESULT iar;
1833
1834         iar.dwResult = retval;
1835         iar.dwError = iar.dwError = retval ? ERROR_SUCCESS :
1836                                              INTERNET_GetLastError();
1837
1838         INTERNET_SendCallback(lpwh, lpwh->dwContext,
1839                               INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1840                               sizeof(INTERNET_ASYNC_RESULT));
1841     }
1842     return retval;
1843 }
1844
1845 /***********************************************************************
1846  *           InternetReadFile (WININET.@)
1847  *
1848  * Read data from an open internet file
1849  *
1850  * RETURNS
1851  *    TRUE  on success
1852  *    FALSE on failure
1853  *
1854  */
1855 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1856         DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1857 {
1858     LPWININETHANDLEHEADER lpwh;
1859     BOOL retval;
1860
1861     TRACE("%p %p %ld %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1862
1863     lpwh = WININET_GetObject( hFile );
1864     if (!lpwh)
1865     {
1866         SetLastError(ERROR_INVALID_HANDLE);
1867         return FALSE;
1868     }
1869
1870     retval = INTERNET_ReadFile(lpwh, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, TRUE, FALSE);
1871     WININET_Release( lpwh );
1872
1873     TRACE("-- %s (bytes read: %ld)\n", retval ? "TRUE": "FALSE", pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1874     return retval;
1875 }
1876
1877 /***********************************************************************
1878  *           InternetReadFileExA (WININET.@)
1879  *
1880  * Read data from an open internet file
1881  *
1882  * PARAMS
1883  *  hFile         [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1884  *  lpBuffersOut  [I/O] Buffer.
1885  *  dwFlags       [I] Flags. See notes.
1886  *  dwContext     [I] Context for callbacks.
1887  *
1888  * RETURNS
1889  *    TRUE  on success
1890  *    FALSE on failure
1891  *
1892  * NOTES
1893  *  The parameter dwFlags include zero or more of the following flags:
1894  *|IRF_ASYNC - Makes the call asynchronous.
1895  *|IRF_SYNC - Makes the call synchronous.
1896  *|IRF_USE_CONTEXT - Forces dwContext to be used.
1897  *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1898  *
1899  * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1900  *
1901  * SEE
1902  *  InternetOpenUrlA(), HttpOpenRequestA()
1903  */
1904 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1905         DWORD dwFlags, DWORD dwContext)
1906 {
1907     BOOL retval = FALSE;
1908     LPWININETHANDLEHEADER lpwh;
1909
1910     TRACE("(%p %p 0x%lx 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1911
1912     if (dwFlags & ~(IRF_ASYNC|IRF_NO_WAIT))
1913         FIXME("these dwFlags aren't implemented: 0x%lx\n", dwFlags & ~(IRF_ASYNC|IRF_NO_WAIT));
1914
1915     if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut))
1916     {
1917         SetLastError(ERROR_INVALID_PARAMETER);
1918         return FALSE;
1919     }
1920
1921     lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1922     if (!lpwh)
1923     {
1924         SetLastError(ERROR_INVALID_HANDLE);
1925         return FALSE;
1926     }
1927
1928     /* FIXME: native only does it asynchronously if the amount of data
1929      * requested isn't available. See NtReadFile. */
1930     /* FIXME: IRF_ASYNC may not be the right thing to test here;
1931      * hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC is probably better, but
1932      * we should implement the above first */
1933     if (dwFlags & IRF_ASYNC)
1934     {
1935         WORKREQUEST workRequest;
1936         struct WORKREQ_INTERNETREADFILEEXA *req;
1937
1938         workRequest.asyncall = INTERNETREADFILEEXA;
1939         workRequest.hdr = WININET_AddRef( lpwh );
1940         req = &workRequest.u.InternetReadFileExA;
1941         req->lpBuffersOut = lpBuffersOut;
1942
1943         retval = INTERNET_AsyncCall(&workRequest);
1944         if (!retval) return FALSE;
1945
1946         SetLastError(ERROR_IO_PENDING);
1947         return FALSE;
1948     }
1949
1950     retval = INTERNET_ReadFile(lpwh, lpBuffersOut->lpvBuffer,
1951         lpBuffersOut->dwBufferLength, &lpBuffersOut->dwBufferLength,
1952         !(dwFlags & IRF_NO_WAIT), FALSE);
1953
1954     WININET_Release( lpwh );
1955
1956     TRACE("-- %s (bytes read: %ld)\n", retval ? "TRUE": "FALSE", lpBuffersOut->dwBufferLength);
1957     return retval;
1958 }
1959
1960 /***********************************************************************
1961  *           InternetReadFileExW (WININET.@)
1962  *
1963  * Read data from an open internet file.
1964  *
1965  * PARAMS
1966  *  hFile         [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1967  *  lpBuffersOut  [I/O] Buffer.
1968  *  dwFlags       [I] Flags.
1969  *  dwContext     [I] Context for callbacks.
1970  *
1971  * RETURNS
1972  *    FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1973  *
1974  * NOTES
1975  *  Not implemented in Wine or native either (as of IE6 SP2).
1976  *
1977  */
1978 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1979         DWORD dwFlags, DWORD dwContext)
1980 {
1981   ERR("(%p, %p, 0x%lx, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1982
1983   INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1984   return FALSE;
1985 }
1986
1987 /***********************************************************************
1988  *           INET_QueryOptionHelper (internal)
1989  */
1990 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1991                                    LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1992 {
1993     LPWININETHANDLEHEADER lpwhh;
1994     BOOL bSuccess = FALSE;
1995
1996     TRACE("(%p, 0x%08lx, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1997
1998     lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
1999     if (!lpwhh)
2000     {
2001         SetLastError(ERROR_INVALID_PARAMETER);
2002         return FALSE;
2003     }
2004
2005     switch (dwOption)
2006     {
2007         case INTERNET_OPTION_HANDLE_TYPE:
2008         {
2009             ULONG type;
2010
2011             if (!lpwhh)
2012             {
2013                 WARN("Invalid hInternet handle\n");
2014                 SetLastError(ERROR_INVALID_HANDLE);
2015                 return FALSE;
2016             }
2017
2018             type = lpwhh->htype;
2019
2020             TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
2021
2022             if (*lpdwBufferLength < sizeof(ULONG))
2023                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2024             else
2025             {
2026                 memcpy(lpBuffer, &type, sizeof(ULONG));
2027                 bSuccess = TRUE;
2028             }
2029             *lpdwBufferLength = sizeof(ULONG);
2030             break;
2031         }
2032
2033         case INTERNET_OPTION_REQUEST_FLAGS:
2034         {
2035             ULONG flags = 4;
2036             TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
2037             if (*lpdwBufferLength < sizeof(ULONG))
2038                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2039             else
2040             {
2041                 memcpy(lpBuffer, &flags, sizeof(ULONG));
2042                 bSuccess = TRUE;
2043             }
2044             *lpdwBufferLength = sizeof(ULONG);
2045             break;
2046         }
2047
2048         case INTERNET_OPTION_URL:
2049         case INTERNET_OPTION_DATAFILE_NAME:
2050         {
2051             if (!lpwhh)
2052             {
2053                 WARN("Invalid hInternet handle\n");
2054                 SetLastError(ERROR_INVALID_HANDLE);
2055                 return FALSE;
2056             }
2057             if (lpwhh->htype == WH_HHTTPREQ)
2058             {
2059                 LPWININETHTTPREQW lpreq = (LPWININETHTTPREQW) lpwhh;
2060                 WCHAR url[1023];
2061                 static const WCHAR szFmt[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2062                 static const WCHAR szHost[] = {'H','o','s','t',0};
2063                 DWORD sizeRequired;
2064                 LPHTTPHEADERW Host;
2065
2066                 Host = HTTP_GetHeader(lpreq,szHost);
2067                 sprintfW(url,szFmt,Host->lpszValue,lpreq->lpszPath);
2068                 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
2069                 if(!bIsUnicode)
2070                 {
2071                     sizeRequired = WideCharToMultiByte(CP_ACP,0,url,-1,
2072                      lpBuffer,*lpdwBufferLength,NULL,NULL);
2073                     if (sizeRequired > *lpdwBufferLength)
2074                         INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2075                     else
2076                         bSuccess = TRUE;
2077                     *lpdwBufferLength = sizeRequired;
2078                 }
2079                 else
2080                 {
2081                     sizeRequired = (lstrlenW(url)+1) * sizeof(WCHAR);
2082                     if (*lpdwBufferLength < sizeRequired)
2083                         INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2084                     else
2085                     {
2086                         strcpyW(lpBuffer, url);
2087                         bSuccess = TRUE;
2088                     }
2089                     *lpdwBufferLength = sizeRequired;
2090                 }
2091             }
2092             break;
2093         }
2094         case INTERNET_OPTION_HTTP_VERSION:
2095         {
2096             if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
2097                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2098             else
2099             {
2100                 /*
2101                  * Presently hardcoded to 1.1
2102                  */
2103                 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
2104                 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
2105                 bSuccess = TRUE;
2106             }
2107             *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
2108             break;
2109         }
2110        case INTERNET_OPTION_CONNECTED_STATE:
2111        {
2112             DWORD *pdwConnectedState = (DWORD *)lpBuffer;
2113             FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2114
2115             if (*lpdwBufferLength < sizeof(*pdwConnectedState))
2116                  INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2117             else
2118             {
2119                 *pdwConnectedState = INTERNET_STATE_CONNECTED;
2120                 bSuccess = TRUE;
2121             }
2122             *lpdwBufferLength = sizeof(*pdwConnectedState);
2123             break;
2124         }
2125         case INTERNET_OPTION_PROXY:
2126         {
2127             LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
2128             WININETAPPINFOW wai;
2129
2130             if (lpwai == NULL)
2131             {
2132                 TRACE("Getting global proxy info\n");
2133                 memset(&wai, 0, sizeof(WININETAPPINFOW));
2134                 INTERNET_ConfigureProxyFromReg( &wai );
2135                 lpwai = &wai;
2136             }
2137
2138             if (bIsUnicode)
2139             {
2140                 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
2141                 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
2142
2143                 if (lpwai->lpszProxy)
2144                     proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
2145                      sizeof(WCHAR);
2146                 if (lpwai->lpszProxyBypass)
2147                     proxyBypassBytesRequired =
2148                      (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
2149                 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
2150                  proxyBytesRequired + proxyBypassBytesRequired)
2151                     INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2152                 else
2153                 {
2154                     pPI->dwAccessType = lpwai->dwAccessType;
2155                     if (lpwai->lpszProxy)
2156                     {
2157                         pPI->lpszProxy = (LPWSTR)((LPBYTE)lpBuffer +
2158                                                   sizeof(INTERNET_PROXY_INFOW));
2159                         lstrcpyW((LPWSTR)pPI->lpszProxy, lpwai->lpszProxy);
2160                     }
2161                     else
2162                     {
2163                         pPI->lpszProxy = (LPWSTR)((LPBYTE)lpBuffer +
2164                                                   sizeof(INTERNET_PROXY_INFOW));
2165                         *((LPWSTR)(pPI->lpszProxy)) = 0;
2166                     }
2167
2168                     if (lpwai->lpszProxyBypass)
2169                     {
2170                         pPI->lpszProxyBypass = (LPWSTR)((LPBYTE)lpBuffer +
2171                                                         sizeof(INTERNET_PROXY_INFOW) +
2172                                                         proxyBytesRequired);
2173                         lstrcpyW((LPWSTR)pPI->lpszProxyBypass,
2174                          lpwai->lpszProxyBypass);
2175                     }
2176                     else
2177                     {
2178                         pPI->lpszProxyBypass = (LPWSTR)((LPBYTE)lpBuffer +
2179                                                         sizeof(INTERNET_PROXY_INFOW) +
2180                                                         proxyBytesRequired);
2181                         *((LPWSTR)(pPI->lpszProxyBypass)) = 0;
2182                     }
2183                     bSuccess = TRUE;
2184                 }
2185                 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
2186                  proxyBytesRequired + proxyBypassBytesRequired;
2187             }
2188             else
2189             {
2190                 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
2191                 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
2192
2193                 if (lpwai->lpszProxy)
2194                     proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2195                      lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
2196                 if (lpwai->lpszProxyBypass)
2197                     proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2198                      lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
2199                 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
2200                  proxyBytesRequired + proxyBypassBytesRequired)
2201                     INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2202                 else
2203                 {
2204                     pPI->dwAccessType = lpwai->dwAccessType;
2205                     if (lpwai->lpszProxy)
2206                     {
2207                         pPI->lpszProxy = (LPSTR)((LPBYTE)lpBuffer +
2208                                                  sizeof(INTERNET_PROXY_INFOA));
2209                         WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
2210                          (LPSTR)pPI->lpszProxy, proxyBytesRequired, NULL, NULL);
2211                     }
2212                     else
2213                     {
2214                         pPI->lpszProxy = (LPSTR)((LPBYTE)lpBuffer +
2215                                                  sizeof(INTERNET_PROXY_INFOA));
2216                         *((LPSTR)(pPI->lpszProxy)) = '\0';
2217                     }
2218                     
2219                     if (lpwai->lpszProxyBypass)
2220                     {
2221                         pPI->lpszProxyBypass = (LPSTR)((LPBYTE)lpBuffer +
2222                          sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
2223                         WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
2224                          -1, (LPSTR)pPI->lpszProxyBypass,
2225                          proxyBypassBytesRequired,
2226                          NULL, NULL);
2227                     }
2228                     else
2229                     {
2230                         pPI->lpszProxyBypass = (LPSTR)((LPBYTE)lpBuffer +
2231                                                        sizeof(INTERNET_PROXY_INFOA) +
2232                                                        proxyBytesRequired);
2233                         *((LPSTR)(pPI->lpszProxyBypass)) = '\0';
2234                     }
2235                     bSuccess = TRUE;
2236                 }
2237                 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2238                  proxyBytesRequired + proxyBypassBytesRequired;
2239             }
2240             break;
2241         }
2242         case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2243         {
2244             ULONG conn = 2;
2245             TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %ld\n", conn);
2246             if (*lpdwBufferLength < sizeof(ULONG))
2247                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2248             else
2249             {
2250                 memcpy(lpBuffer, &conn, sizeof(ULONG));
2251                 bSuccess = TRUE;
2252             }
2253             *lpdwBufferLength = sizeof(ULONG);
2254             break;
2255         }
2256         case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2257         {
2258             ULONG conn = 4;
2259             TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %ld\n", conn);
2260             if (*lpdwBufferLength < sizeof(ULONG))
2261                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2262             else
2263             {
2264                 memcpy(lpBuffer, &conn, sizeof(ULONG));
2265                 bSuccess = TRUE;
2266             }
2267             *lpdwBufferLength = sizeof(ULONG);
2268             break;
2269         }
2270         case INTERNET_OPTION_SECURITY_FLAGS:
2271             FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2272             break;
2273
2274         case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT:
2275             if (*lpdwBufferLength < sizeof(INTERNET_CERTIFICATE_INFOW))
2276             {
2277                 *lpdwBufferLength = sizeof(INTERNET_CERTIFICATE_INFOW);
2278                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2279             }
2280             else if (lpwhh->htype == WH_HHTTPREQ)
2281             {
2282                 LPWININETHTTPREQW lpwhr;
2283                 PCCERT_CONTEXT context;
2284
2285                 lpwhr = (LPWININETHTTPREQW)lpwhh;
2286                 context = (PCCERT_CONTEXT)NETCON_GetCert(&(lpwhr->netConnection));
2287                 if (context)
2288                 {
2289                     LPINTERNET_CERTIFICATE_INFOW info = (LPINTERNET_CERTIFICATE_INFOW)lpBuffer;
2290                     DWORD strLen;
2291
2292                     memset(info,0,sizeof(INTERNET_CERTIFICATE_INFOW));
2293                     info->ftExpiry = context->pCertInfo->NotAfter;
2294                     info->ftStart = context->pCertInfo->NotBefore;
2295                     if (bIsUnicode)
2296                     {
2297                         strLen = CertNameToStrW(context->dwCertEncodingType,
2298                          &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2299                          NULL, 0);
2300                         info->lpszSubjectInfo = LocalAlloc(0,
2301                          strLen * sizeof(WCHAR));
2302                         if (info->lpszSubjectInfo)
2303                             CertNameToStrW(context->dwCertEncodingType,
2304                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2305                              info->lpszSubjectInfo, strLen);
2306                         strLen = CertNameToStrW(context->dwCertEncodingType,
2307                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2308                          NULL, 0);
2309                         info->lpszIssuerInfo = LocalAlloc(0,
2310                          strLen * sizeof(WCHAR));
2311                         if (info->lpszIssuerInfo)
2312                             CertNameToStrW(context->dwCertEncodingType,
2313                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2314                              info->lpszIssuerInfo, strLen);
2315                     }
2316                     else
2317                     {
2318                         LPINTERNET_CERTIFICATE_INFOA infoA =
2319                          (LPINTERNET_CERTIFICATE_INFOA)info;
2320
2321                         strLen = CertNameToStrA(context->dwCertEncodingType,
2322                          &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2323                          NULL, 0);
2324                         infoA->lpszSubjectInfo = LocalAlloc(0, strLen);
2325                         if (infoA->lpszSubjectInfo)
2326                             CertNameToStrA(context->dwCertEncodingType,
2327                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2328                              infoA->lpszSubjectInfo, strLen);
2329                         strLen = CertNameToStrA(context->dwCertEncodingType,
2330                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2331                          NULL, 0);
2332                         infoA->lpszIssuerInfo = LocalAlloc(0, strLen);
2333                         if (infoA->lpszIssuerInfo)
2334                             CertNameToStrA(context->dwCertEncodingType,
2335                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2336                              infoA->lpszIssuerInfo, strLen);
2337                     }
2338                     /*
2339                      * Contrary to MSDN, these do not appear to be set.
2340                      * lpszProtocolName
2341                      * lpszSignatureAlgName
2342                      * lpszEncryptionAlgName
2343                      * dwKeySize
2344                      */
2345                     CertFreeCertificateContext(context);
2346                     bSuccess = TRUE;
2347                 }
2348             }
2349             break;
2350         default:
2351             FIXME("Stub! %ld\n", dwOption);
2352             break;
2353     }
2354     if (lpwhh)
2355         WININET_Release( lpwhh );
2356
2357     return bSuccess;
2358 }
2359
2360 /***********************************************************************
2361  *           InternetQueryOptionW (WININET.@)
2362  *
2363  * Queries an options on the specified handle
2364  *
2365  * RETURNS
2366  *    TRUE  on success
2367  *    FALSE on failure
2368  *
2369  */
2370 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2371                                  LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2372 {
2373     return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2374 }
2375
2376 /***********************************************************************
2377  *           InternetQueryOptionA (WININET.@)
2378  *
2379  * Queries an options on the specified handle
2380  *
2381  * RETURNS
2382  *    TRUE  on success
2383  *    FALSE on failure
2384  *
2385  */
2386 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2387                                  LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2388 {
2389     return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2390 }
2391
2392
2393 /***********************************************************************
2394  *           InternetSetOptionW (WININET.@)
2395  *
2396  * Sets an options on the specified handle
2397  *
2398  * RETURNS
2399  *    TRUE  on success
2400  *    FALSE on failure
2401  *
2402  */
2403 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2404                            LPVOID lpBuffer, DWORD dwBufferLength)
2405 {
2406     LPWININETHANDLEHEADER lpwhh;
2407     BOOL ret = TRUE;
2408
2409     TRACE("0x%08lx\n", dwOption);
2410
2411     lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2412     if( !lpwhh )
2413         return FALSE;
2414
2415     switch (dwOption)
2416     {
2417     case INTERNET_OPTION_HTTP_VERSION:
2418       {
2419         HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2420         FIXME("Option INTERNET_OPTION_HTTP_VERSION(%ld,%ld): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2421       }
2422       break;
2423     case INTERNET_OPTION_ERROR_MASK:
2424       {
2425         unsigned long flags=*(unsigned long*)lpBuffer;
2426         FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2427       }
2428       break;
2429     case INTERNET_OPTION_CODEPAGE:
2430       {
2431         unsigned long codepage=*(unsigned long*)lpBuffer;
2432         FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2433       }
2434       break;
2435     case INTERNET_OPTION_REQUEST_PRIORITY:
2436       {
2437         unsigned long priority=*(unsigned long*)lpBuffer;
2438         FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2439       }
2440       break;
2441     case INTERNET_OPTION_CONNECT_TIMEOUT:
2442       {
2443         unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2444         FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2445       }
2446       break;
2447     case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2448       {
2449         unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2450         FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2451       }
2452       break;
2453     case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2454       {
2455         unsigned long conns=*(unsigned long*)lpBuffer;
2456         FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2457       }
2458       break;
2459     case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2460       {
2461         unsigned long conns=*(unsigned long*)lpBuffer;
2462         FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2463       }
2464       break;
2465     case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2466         FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2467         break;
2468     case INTERNET_OPTION_END_BROWSER_SESSION:
2469         FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2470         break;
2471     case INTERNET_OPTION_CONNECTED_STATE:
2472         FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2473         break;
2474     case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2475         TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2476         break;
2477     case INTERNET_OPTION_SEND_TIMEOUT:
2478     case INTERNET_OPTION_RECEIVE_TIMEOUT:
2479         TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
2480         if (dwBufferLength == sizeof(DWORD))
2481         {
2482             if (lpwhh->htype == WH_HHTTPREQ)
2483                 ret = NETCON_set_timeout(
2484                     &((LPWININETHTTPREQW)lpwhh)->netConnection,
2485                     dwOption == INTERNET_OPTION_SEND_TIMEOUT,
2486                     *(DWORD *)lpBuffer);
2487             else
2488             {
2489                 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT not supported on protocol %d\n",
2490                       lpwhh->htype);
2491                 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2492                 ret = FALSE;
2493             }
2494         }
2495         else
2496         {
2497             INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2498             ret = FALSE;
2499         }
2500         break;
2501     case INTERNET_OPTION_CONNECT_RETRIES:
2502         FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2503         break;
2504     case INTERNET_OPTION_CONTEXT_VALUE:
2505          FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2506          break;
2507     default:
2508         FIXME("Option %ld STUB\n",dwOption);
2509         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2510         ret = FALSE;
2511         break;
2512     }
2513     WININET_Release( lpwhh );
2514
2515     return ret;
2516 }
2517
2518
2519 /***********************************************************************
2520  *           InternetSetOptionA (WININET.@)
2521  *
2522  * Sets an options on the specified handle.
2523  *
2524  * RETURNS
2525  *    TRUE  on success
2526  *    FALSE on failure
2527  *
2528  */
2529 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2530                            LPVOID lpBuffer, DWORD dwBufferLength)
2531 {
2532     LPVOID wbuffer;
2533     DWORD wlen;
2534     BOOL r;
2535
2536     switch( dwOption )
2537     {
2538     case INTERNET_OPTION_PROXY:
2539         {
2540         LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2541         LPINTERNET_PROXY_INFOW piw;
2542         DWORD proxlen, prbylen;
2543         LPWSTR prox, prby;
2544
2545         proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2546         prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2547         wlen = sizeof(*piw) + proxlen + prbylen;
2548         wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2549         piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2550         piw->dwAccessType = pi->dwAccessType;
2551         prox = (LPWSTR) &piw[1];
2552         prby = &prox[proxlen+1];
2553         MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2554         MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2555         piw->lpszProxy = prox;
2556         piw->lpszProxyBypass = prby;
2557         }
2558         break;
2559     case INTERNET_OPTION_USER_AGENT:
2560     case INTERNET_OPTION_USERNAME:
2561     case INTERNET_OPTION_PASSWORD:
2562         wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2563                                    NULL, 0 );
2564         wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2565         MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2566                                    wbuffer, wlen );
2567         break;
2568     default:
2569         wbuffer = lpBuffer;
2570         wlen = dwBufferLength;
2571     }
2572
2573     r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2574
2575     if( lpBuffer != wbuffer )
2576         HeapFree( GetProcessHeap(), 0, wbuffer );
2577
2578     return r;
2579 }
2580
2581
2582 /***********************************************************************
2583  *           InternetSetOptionExA (WININET.@)
2584  */
2585 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2586                            LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2587 {
2588     FIXME("Flags %08lx ignored\n", dwFlags);
2589     return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2590 }
2591
2592 /***********************************************************************
2593  *           InternetSetOptionExW (WININET.@)
2594  */
2595 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2596                            LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2597 {
2598     FIXME("Flags %08lx ignored\n", dwFlags);
2599     if( dwFlags & ~ISO_VALID_FLAGS )
2600     {
2601         SetLastError( ERROR_INVALID_PARAMETER );
2602         return FALSE;
2603     }
2604     return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2605 }
2606
2607 static const WCHAR WININET_wkday[7][4] =
2608     { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2609       { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2610 static const WCHAR WININET_month[12][4] =
2611     { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2612       { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2613       { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2614
2615 /***********************************************************************
2616  *           InternetTimeFromSystemTimeA (WININET.@)
2617  */
2618 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2619 {
2620     BOOL ret;
2621     WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2622
2623     TRACE( "%p 0x%08lx %p 0x%08lx\n", time, format, string, size );
2624
2625     ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2626     if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2627
2628     return ret;
2629 }
2630
2631 /***********************************************************************
2632  *           InternetTimeFromSystemTimeW (WININET.@)
2633  */
2634 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2635 {
2636     static const WCHAR date[] =
2637         { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2638           '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2639
2640     TRACE( "%p 0x%08lx %p 0x%08lx\n", time, format, string, size );
2641
2642     if (!time || !string) return FALSE;
2643
2644     if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2645         return FALSE;
2646
2647     sprintfW( string, date,
2648               WININET_wkday[time->wDayOfWeek],
2649               time->wDay,
2650               WININET_month[time->wMonth - 1],
2651               time->wYear,
2652               time->wHour,
2653               time->wMinute,
2654               time->wSecond );
2655
2656     return TRUE;
2657 }
2658
2659 /***********************************************************************
2660  *           InternetTimeToSystemTimeA (WININET.@)
2661  */
2662 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2663 {
2664     BOOL ret = FALSE;
2665     WCHAR *stringW;
2666     int len;
2667
2668     TRACE( "%s %p 0x%08lx\n", debugstr_a(string), time, reserved );
2669
2670     len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2671     stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2672
2673     if (stringW)
2674     {
2675         MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2676         ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2677         HeapFree( GetProcessHeap(), 0, stringW );
2678     }
2679     return ret;
2680 }
2681
2682 /***********************************************************************
2683  *           InternetTimeToSystemTimeW (WININET.@)
2684  */
2685 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2686 {
2687     unsigned int i;
2688     WCHAR *s = (LPWSTR)string;
2689
2690     TRACE( "%s %p 0x%08lx\n", debugstr_w(string), time, reserved );
2691
2692     if (!string || !time) return FALSE;
2693
2694     /* Windows does this too */
2695     GetSystemTime( time );
2696
2697     /*  Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2698      *  a SYSTEMTIME structure.
2699      */
2700
2701     while (*s && !isalphaW( *s )) s++;
2702     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2703     time->wDayOfWeek = 7;
2704
2705     for (i = 0; i < 7; i++)
2706     {
2707         if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2708             toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2709             toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2710         {
2711             time->wDayOfWeek = i;
2712             break;
2713         }
2714     }
2715
2716     if (time->wDayOfWeek > 6) return TRUE;
2717     while (*s && !isdigitW( *s )) s++;
2718     time->wDay = strtolW( s, &s, 10 );
2719
2720     while (*s && !isalphaW( *s )) s++;
2721     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2722     time->wMonth = 0;
2723
2724     for (i = 0; i < 12; i++)
2725     {
2726         if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2727             toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2728             toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2729         {
2730             time->wMonth = i + 1;
2731             break;
2732         }
2733     }
2734     if (time->wMonth == 0) return TRUE;
2735
2736     while (*s && !isdigitW( *s )) s++;
2737     if (*s == '\0') return TRUE;
2738     time->wYear = strtolW( s, &s, 10 );
2739
2740     while (*s && !isdigitW( *s )) s++;
2741     if (*s == '\0') return TRUE;
2742     time->wHour = strtolW( s, &s, 10 );
2743
2744     while (*s && !isdigitW( *s )) s++;
2745     if (*s == '\0') return TRUE;
2746     time->wMinute = strtolW( s, &s, 10 );
2747
2748     while (*s && !isdigitW( *s )) s++;
2749     if (*s == '\0') return TRUE;
2750     time->wSecond = strtolW( s, &s, 10 );
2751
2752     time->wMilliseconds = 0;
2753     return TRUE;
2754 }
2755
2756 /***********************************************************************
2757  *      InternetCheckConnectionW (WININET.@)
2758  *
2759  * Pings a requested host to check internet connection
2760  *
2761  * RETURNS
2762  *   TRUE on success and FALSE on failure. If a failure then
2763  *   ERROR_NOT_CONNECTED is placed into GetLastError
2764  *
2765  */
2766 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2767 {
2768 /*
2769  * this is a kludge which runs the resident ping program and reads the output.
2770  *
2771  * Anyone have a better idea?
2772  */
2773
2774   BOOL   rc = FALSE;
2775   static const CHAR ping[] = "ping -w 1 ";
2776   static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2777   CHAR *command = NULL;
2778   WCHAR hostW[1024];
2779   DWORD len;
2780   int status = -1;
2781
2782   FIXME("\n");
2783
2784   /*
2785    * Crack or set the Address
2786    */
2787   if (lpszUrl == NULL)
2788   {
2789      /*
2790       * According to the doc we are supost to use the ip for the next
2791       * server in the WnInet internal server database. I have
2792       * no idea what that is or how to get it.
2793       *
2794       * So someone needs to implement this.
2795       */
2796      FIXME("Unimplemented with URL of NULL\n");
2797      return TRUE;
2798   }
2799   else
2800   {
2801      URL_COMPONENTSW components;
2802
2803      ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2804      components.lpszHostName = (LPWSTR)&hostW;
2805      components.dwHostNameLength = 1024;
2806
2807      if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2808        goto End;
2809
2810      TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2811   }
2812
2813   /*
2814    * Build our ping command
2815    */
2816   len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2817   command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2818   strcpy(command,ping);
2819   WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2820   strcat(command,redirect);
2821
2822   TRACE("Ping command is : %s\n",command);
2823
2824   status = system(command);
2825
2826   TRACE("Ping returned a code of %i\n",status);
2827
2828   /* Ping return code of 0 indicates success */
2829   if (status == 0)
2830      rc = TRUE;
2831
2832 End:
2833
2834   HeapFree( GetProcessHeap(), 0, command );
2835   if (rc == FALSE)
2836     SetLastError(ERROR_NOT_CONNECTED);
2837
2838   return rc;
2839 }
2840
2841
2842 /***********************************************************************
2843  *      InternetCheckConnectionA (WININET.@)
2844  *
2845  * Pings a requested host to check internet connection
2846  *
2847  * RETURNS
2848  *   TRUE on success and FALSE on failure. If a failure then
2849  *   ERROR_NOT_CONNECTED is placed into GetLastError
2850  *
2851  */
2852 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2853 {
2854     WCHAR *szUrl;
2855     INT len;
2856     BOOL rc;
2857
2858     len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2859     if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2860         return FALSE;
2861     MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2862     rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2863     HeapFree(GetProcessHeap(), 0, szUrl);
2864     
2865     return rc;
2866 }
2867
2868
2869 /**********************************************************
2870  *      INTERNET_InternetOpenUrlW (internal)
2871  *
2872  * Opens an URL
2873  *
2874  * RETURNS
2875  *   handle of connection or NULL on failure
2876  */
2877 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2878     LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2879 {
2880     URL_COMPONENTSW urlComponents;
2881     WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2882     WCHAR password[1024], path[2048], extra[1024];
2883     HINTERNET client = NULL, client1 = NULL;
2884     
2885     TRACE("(%p, %s, %s, %08lx, %08lx, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2886           dwHeadersLength, dwFlags, dwContext);
2887     
2888     urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2889     urlComponents.lpszScheme = protocol;
2890     urlComponents.dwSchemeLength = 32;
2891     urlComponents.lpszHostName = hostName;
2892     urlComponents.dwHostNameLength = MAXHOSTNAME;
2893     urlComponents.lpszUserName = userName;
2894     urlComponents.dwUserNameLength = 1024;
2895     urlComponents.lpszPassword = password;
2896     urlComponents.dwPasswordLength = 1024;
2897     urlComponents.lpszUrlPath = path;
2898     urlComponents.dwUrlPathLength = 2048;
2899     urlComponents.lpszExtraInfo = extra;
2900     urlComponents.dwExtraInfoLength = 1024;
2901     if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2902         return NULL;
2903     switch(urlComponents.nScheme) {
2904     case INTERNET_SCHEME_FTP:
2905         if(urlComponents.nPort == 0)
2906             urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2907         client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2908                              userName, password, dwFlags, dwContext, INET_OPENURL);
2909         if(client == NULL)
2910             break;
2911         client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2912         if(client1 == NULL) {
2913             InternetCloseHandle(client);
2914             break;
2915         }
2916         break;
2917         
2918     case INTERNET_SCHEME_HTTP:
2919     case INTERNET_SCHEME_HTTPS: {
2920         static const WCHAR szStars[] = { '*','/','*', 0 };
2921         LPCWSTR accept[2] = { szStars, NULL };
2922         if(urlComponents.nPort == 0) {
2923             if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2924                 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2925             else
2926                 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2927         }
2928         /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2929         client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2930                               userName, password, dwFlags, dwContext, INET_OPENURL);
2931         if(client == NULL)
2932             break;
2933         client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2934         if(client1 == NULL) {
2935             InternetCloseHandle(client);
2936             break;
2937         }
2938         HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2939         if (!HttpSendRequestW(client1, NULL, 0, NULL, 0)) {
2940             InternetCloseHandle(client1);
2941             client1 = NULL;
2942             break;
2943         }
2944     }
2945     case INTERNET_SCHEME_GOPHER:
2946         /* gopher doesn't seem to be implemented in wine, but it's supposed
2947          * to be supported by InternetOpenUrlA. */
2948     default:
2949         INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2950         break;
2951     }
2952
2953     TRACE(" %p <--\n", client1);
2954     
2955     return client1;
2956 }
2957
2958 /**********************************************************
2959  *      InternetOpenUrlW (WININET.@)
2960  *
2961  * Opens an URL
2962  *
2963  * RETURNS
2964  *   handle of connection or NULL on failure
2965  */
2966 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2967     LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2968 {
2969     HINTERNET ret = NULL;
2970     LPWININETAPPINFOW hIC = NULL;
2971
2972     if (TRACE_ON(wininet)) {
2973         TRACE("(%p, %s, %s, %08lx, %08lx, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2974               dwHeadersLength, dwFlags, dwContext);
2975         TRACE("  flags :");
2976         dump_INTERNET_FLAGS(dwFlags);
2977     }
2978
2979     if (!lpszUrl)
2980     {
2981         SetLastError(ERROR_INVALID_PARAMETER);
2982         goto lend;
2983     }
2984
2985     hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2986     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT) {
2987         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2988         goto lend;
2989     }
2990     
2991     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2992         WORKREQUEST workRequest;
2993         struct WORKREQ_INTERNETOPENURLW *req;
2994         
2995         workRequest.asyncall = INTERNETOPENURLW;
2996         workRequest.hdr = WININET_AddRef( &hIC->hdr );
2997         req = &workRequest.u.InternetOpenUrlW;
2998         req->lpszUrl = WININET_strdupW(lpszUrl);
2999         if (lpszHeaders)
3000             req->lpszHeaders = WININET_strdupW(lpszHeaders);
3001         else
3002             req->lpszHeaders = 0;
3003         req->dwHeadersLength = dwHeadersLength;
3004         req->dwFlags = dwFlags;
3005         req->dwContext = dwContext;
3006         
3007         INTERNET_AsyncCall(&workRequest);
3008         /*
3009          * This is from windows.
3010          */
3011         SetLastError(ERROR_IO_PENDING);
3012     } else {
3013         ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3014     }
3015     
3016   lend:
3017     if( hIC )
3018         WININET_Release( &hIC->hdr );
3019     TRACE(" %p <--\n", ret);
3020     
3021     return ret;
3022 }
3023
3024 /**********************************************************
3025  *      InternetOpenUrlA (WININET.@)
3026  *
3027  * Opens an URL
3028  *
3029  * RETURNS
3030  *   handle of connection or NULL on failure
3031  */
3032 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3033     LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
3034 {
3035     HINTERNET rc = (HINTERNET)NULL;
3036
3037     INT lenUrl;
3038     INT lenHeaders = 0;
3039     LPWSTR szUrl = NULL;
3040     LPWSTR szHeaders = NULL;
3041
3042     TRACE("\n");
3043
3044     if(lpszUrl) {
3045         lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
3046         szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
3047         if(!szUrl)
3048             return (HINTERNET)NULL;
3049         MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
3050     }
3051     
3052     if(lpszHeaders) {
3053         lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3054         szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
3055         if(!szHeaders) {
3056             HeapFree(GetProcessHeap(), 0, szUrl);
3057             return (HINTERNET)NULL;
3058         }
3059         MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3060     }
3061     
3062     rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3063         lenHeaders, dwFlags, dwContext);
3064
3065     HeapFree(GetProcessHeap(), 0, szUrl);
3066     HeapFree(GetProcessHeap(), 0, szHeaders);
3067
3068     return rc;
3069 }
3070
3071
3072 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3073 {
3074     LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
3075
3076     if (lpwite)
3077     {
3078         lpwite->dwError = 0;
3079         lpwite->response[0] = '\0';
3080     }
3081
3082     if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3083     {
3084         HeapFree(GetProcessHeap(), 0, lpwite);
3085         return NULL;
3086     }
3087
3088     return lpwite;
3089 }
3090
3091
3092 /***********************************************************************
3093  *           INTERNET_SetLastError (internal)
3094  *
3095  * Set last thread specific error
3096  *
3097  * RETURNS
3098  *
3099  */
3100 void INTERNET_SetLastError(DWORD dwError)
3101 {
3102     LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3103
3104     if (!lpwite)
3105         lpwite = INTERNET_AllocThreadError();
3106
3107     SetLastError(dwError);
3108     if(lpwite)
3109         lpwite->dwError = dwError;
3110 }
3111
3112
3113 /***********************************************************************
3114  *           INTERNET_GetLastError (internal)
3115  *
3116  * Get last thread specific error
3117  *
3118  * RETURNS
3119  *
3120  */
3121 DWORD INTERNET_GetLastError(void)
3122 {
3123     LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3124     if (!lpwite) return 0;
3125     /* TlsGetValue clears last error, so set it again here */
3126     SetLastError(lpwite->dwError);
3127     return lpwite->dwError;
3128 }
3129
3130
3131 /***********************************************************************
3132  *           INTERNET_WorkerThreadFunc (internal)
3133  *
3134  * Worker thread execution function
3135  *
3136  * RETURNS
3137  *
3138  */
3139 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3140 {
3141     DWORD dwWaitRes;
3142
3143     while (1)
3144     {
3145         if(dwNumJobs > 0) {
3146             INTERNET_ExecuteWork();
3147             continue;
3148         }
3149         dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
3150
3151         if (dwWaitRes == WAIT_OBJECT_0 + 1)
3152             INTERNET_ExecuteWork();
3153         else
3154             break;
3155
3156         InterlockedIncrement(&dwNumIdleThreads);
3157     }
3158
3159     InterlockedDecrement(&dwNumIdleThreads);
3160     InterlockedDecrement(&dwNumThreads);
3161     TRACE("Worker thread exiting\n");
3162     return TRUE;
3163 }
3164
3165
3166 /***********************************************************************
3167  *           INTERNET_InsertWorkRequest (internal)
3168  *
3169  * Insert work request into queue
3170  *
3171  * RETURNS
3172  *
3173  */
3174 static BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
3175 {
3176     BOOL bSuccess = FALSE;
3177     LPWORKREQUEST lpNewRequest;
3178
3179     TRACE("\n");
3180
3181     lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3182     if (lpNewRequest)
3183     {
3184         memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
3185         lpNewRequest->prev = NULL;
3186
3187         EnterCriticalSection(&csQueue);
3188
3189         lpNewRequest->next = lpWorkQueueTail;
3190         if (lpWorkQueueTail)
3191             lpWorkQueueTail->prev = lpNewRequest;
3192         lpWorkQueueTail = lpNewRequest;
3193         if (!lpHeadWorkQueue)
3194             lpHeadWorkQueue = lpWorkQueueTail;
3195
3196         LeaveCriticalSection(&csQueue);
3197
3198         bSuccess = TRUE;
3199         InterlockedIncrement(&dwNumJobs);
3200     }
3201
3202     return bSuccess;
3203 }
3204
3205
3206 /***********************************************************************
3207  *           INTERNET_GetWorkRequest (internal)
3208  *
3209  * Retrieves work request from queue
3210  *
3211  * RETURNS
3212  *
3213  */
3214 static BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
3215 {
3216     BOOL bSuccess = FALSE;
3217     LPWORKREQUEST lpRequest = NULL;
3218
3219     TRACE("\n");
3220
3221     EnterCriticalSection(&csQueue);
3222
3223     if (lpHeadWorkQueue)
3224     {
3225         lpRequest = lpHeadWorkQueue;
3226         lpHeadWorkQueue = lpHeadWorkQueue->prev;
3227         if (lpRequest == lpWorkQueueTail)
3228             lpWorkQueueTail = lpHeadWorkQueue;
3229     }
3230
3231     LeaveCriticalSection(&csQueue);
3232
3233     if (lpRequest)
3234     {
3235         memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
3236         HeapFree(GetProcessHeap(), 0, lpRequest);
3237         bSuccess = TRUE;
3238         InterlockedDecrement(&dwNumJobs);
3239     }
3240
3241     return bSuccess;
3242 }
3243
3244
3245 /***********************************************************************
3246  *           INTERNET_AsyncCall (internal)
3247  *
3248  * Retrieves work request from queue
3249  *
3250  * RETURNS
3251  *
3252  */
3253 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3254 {
3255     HANDLE hThread;
3256     DWORD dwTID;
3257     BOOL bSuccess = FALSE;
3258
3259     TRACE("\n");
3260
3261     if (InterlockedDecrement(&dwNumIdleThreads) < 0)
3262     {
3263         InterlockedIncrement(&dwNumIdleThreads);
3264
3265         if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
3266             !(hThread = CreateThread(NULL, 0,
3267             INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
3268         {
3269             InterlockedDecrement(&dwNumThreads);
3270             INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3271             goto lerror;
3272         }
3273
3274         TRACE("Created new thread\n");
3275     }
3276
3277     bSuccess = TRUE;
3278     INTERNET_InsertWorkRequest(lpWorkRequest);
3279     SetEvent(hWorkEvent);
3280
3281 lerror:
3282
3283     return bSuccess;
3284 }
3285
3286
3287 /***********************************************************************
3288  *           INTERNET_ExecuteWork (internal)
3289  *
3290  * RETURNS
3291  *
3292  */
3293 static VOID INTERNET_ExecuteWork(void)
3294 {
3295     WORKREQUEST workRequest;
3296
3297     TRACE("\n");
3298
3299     if (!INTERNET_GetWorkRequest(&workRequest))
3300         return;
3301
3302     switch (workRequest.asyncall)
3303     {
3304     case FTPPUTFILEW:
3305         {
3306         struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
3307         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3308
3309         TRACE("FTPPUTFILEW %p\n", lpwfs);
3310
3311         FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
3312                    req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
3313
3314         HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
3315         HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
3316         }
3317         break;
3318
3319     case FTPSETCURRENTDIRECTORYW:
3320         {
3321         struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
3322         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3323
3324         TRACE("FTPSETCURRENTDIRECTORYW %p\n", lpwfs);
3325
3326         req = &workRequest.u.FtpSetCurrentDirectoryW;
3327         FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
3328         HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3329         }
3330         break;
3331
3332     case FTPCREATEDIRECTORYW:
3333         {
3334         struct WORKREQ_FTPCREATEDIRECTORYW *req;
3335         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3336
3337         TRACE("FTPCREATEDIRECTORYW %p\n", lpwfs);
3338
3339         req = &workRequest.u.FtpCreateDirectoryW;
3340         FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
3341         HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3342         }
3343         break;
3344
3345     case FTPFINDFIRSTFILEW:
3346         {
3347         struct WORKREQ_FTPFINDFIRSTFILEW *req;
3348         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3349
3350         TRACE("FTPFINDFIRSTFILEW %p\n", lpwfs);
3351
3352         req = &workRequest.u.FtpFindFirstFileW;
3353         FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
3354            req->lpFindFileData, req->dwFlags, req->dwContext);
3355         HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
3356         }
3357         break;
3358
3359     case FTPGETCURRENTDIRECTORYW:
3360         {
3361         struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
3362         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3363
3364         TRACE("FTPGETCURRENTDIRECTORYW %p\n", lpwfs);
3365
3366         req = &workRequest.u.FtpGetCurrentDirectoryW;
3367         FTP_FtpGetCurrentDirectoryW(lpwfs,
3368                 req->lpszDirectory, req->lpdwDirectory);
3369         }
3370         break;
3371
3372     case FTPOPENFILEW:
3373         {
3374         struct WORKREQ_FTPOPENFILEW *req = &workRequest.u.FtpOpenFileW;
3375         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3376
3377         TRACE("FTPOPENFILEW %p\n", lpwfs);
3378
3379         FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
3380             req->dwAccess, req->dwFlags, req->dwContext);
3381         HeapFree(GetProcessHeap(), 0, req->lpszFilename);
3382         }
3383         break;
3384
3385     case FTPGETFILEW:
3386         {
3387         struct WORKREQ_FTPGETFILEW *req = &workRequest.u.FtpGetFileW;
3388         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3389
3390         TRACE("FTPGETFILEW %p\n", lpwfs);
3391
3392         FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
3393                  req->lpszNewFile, req->fFailIfExists,
3394                  req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
3395         HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
3396         HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
3397         }
3398         break;
3399
3400     case FTPDELETEFILEW:
3401         {
3402         struct WORKREQ_FTPDELETEFILEW *req = &workRequest.u.FtpDeleteFileW;
3403         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3404
3405         TRACE("FTPDELETEFILEW %p\n", lpwfs);
3406
3407         FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
3408         HeapFree(GetProcessHeap(), 0, req->lpszFilename);
3409         }
3410         break;
3411
3412     case FTPREMOVEDIRECTORYW:
3413         {
3414         struct WORKREQ_FTPREMOVEDIRECTORYW *req;
3415         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3416
3417         TRACE("FTPREMOVEDIRECTORYW %p\n", lpwfs);
3418
3419         req = &workRequest.u.FtpRemoveDirectoryW;
3420         FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
3421         HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3422         }
3423         break;
3424
3425     case FTPRENAMEFILEW:
3426         {
3427         struct WORKREQ_FTPRENAMEFILEW *req = &workRequest.u.FtpRenameFileW;
3428         LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3429
3430         TRACE("FTPRENAMEFILEW %p\n", lpwfs);
3431
3432         FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
3433         HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
3434         HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
3435         }
3436         break;
3437
3438     case INTERNETFINDNEXTW:
3439         {
3440         struct WORKREQ_INTERNETFINDNEXTW *req;
3441         LPWININETFINDNEXTW lpwh = (LPWININETFINDNEXTW) workRequest.hdr;
3442
3443         TRACE("INTERNETFINDNEXTW %p\n", lpwh);
3444
3445         req = &workRequest.u.InternetFindNextW;
3446         INTERNET_FindNextFileW(lpwh, req->lpFindFileData);
3447         }
3448         break;
3449
3450     case HTTPSENDREQUESTW:
3451         {
3452         struct WORKREQ_HTTPSENDREQUESTW *req = &workRequest.u.HttpSendRequestW;
3453         LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest.hdr;
3454
3455         TRACE("HTTPSENDREQUESTW %p\n", lpwhr);
3456
3457         HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
3458                 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
3459                 req->dwContentLength, req->bEndRequest);
3460
3461         HeapFree(GetProcessHeap(), 0, req->lpszHeader);
3462         }
3463         break;
3464
3465     case HTTPOPENREQUESTW:
3466         {
3467         struct WORKREQ_HTTPOPENREQUESTW *req = &workRequest.u.HttpOpenRequestW;
3468         LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) workRequest.hdr;
3469
3470         TRACE("HTTPOPENREQUESTW %p\n", lpwhs);
3471
3472         HTTP_HttpOpenRequestW(lpwhs, req->lpszVerb,
3473             req->lpszObjectName, req->lpszVersion, req->lpszReferrer,
3474             req->lpszAcceptTypes, req->dwFlags, req->dwContext);
3475
3476         HeapFree(GetProcessHeap(), 0, req->lpszVerb);
3477         HeapFree(GetProcessHeap(), 0, req->lpszObjectName);
3478         HeapFree(GetProcessHeap(), 0, req->lpszVersion);
3479         HeapFree(GetProcessHeap(), 0, req->lpszReferrer);
3480         }
3481         break;
3482
3483     case SENDCALLBACK:
3484         {
3485         struct WORKREQ_SENDCALLBACK *req = &workRequest.u.SendCallback;
3486
3487         TRACE("SENDCALLBACK %p\n", workRequest.hdr);
3488
3489         INTERNET_SendCallback(workRequest.hdr,
3490                          req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
3491                          req->dwStatusInfoLength);
3492
3493         /* And frees the copy of the status info */
3494         HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
3495         }
3496         break;
3497
3498     case INTERNETOPENURLW:
3499         {
3500         struct WORKREQ_INTERNETOPENURLW *req = &workRequest.u.InternetOpenUrlW;
3501         LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest.hdr;
3502         
3503         TRACE("INTERNETOPENURLW %p\n", hIC);
3504
3505         INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
3506                                   req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
3507         HeapFree(GetProcessHeap(), 0, req->lpszUrl);
3508         HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
3509         }
3510         break;
3511     case INTERNETREADFILEEXA:
3512         {
3513         struct WORKREQ_INTERNETREADFILEEXA *req = &workRequest.u.InternetReadFileExA;
3514
3515         TRACE("INTERNETREADFILEEXA %p\n", workRequest.hdr);
3516
3517         INTERNET_ReadFile(workRequest.hdr, req->lpBuffersOut->lpvBuffer,
3518             req->lpBuffersOut->dwBufferLength,
3519             &req->lpBuffersOut->dwBufferLength, TRUE, TRUE);
3520         }
3521         break;
3522     }
3523     WININET_Release( workRequest.hdr );
3524 }
3525
3526
3527 /***********************************************************************
3528  *          INTERNET_GetResponseBuffer  (internal)
3529  *
3530  * RETURNS
3531  *
3532  */
3533 LPSTR INTERNET_GetResponseBuffer(void)
3534 {
3535     LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3536     if (!lpwite)
3537         lpwite = INTERNET_AllocThreadError();
3538     TRACE("\n");
3539     return lpwite->response;
3540 }
3541
3542 /***********************************************************************
3543  *           INTERNET_GetNextLine  (internal)
3544  *
3545  * Parse next line in directory string listing
3546  *
3547  * RETURNS
3548  *   Pointer to beginning of next line
3549  *   NULL on failure
3550  *
3551  */
3552
3553 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3554 {
3555     struct timeval tv;
3556     fd_set infd;
3557     BOOL bSuccess = FALSE;
3558     INT nRecv = 0;
3559     LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3560
3561     TRACE("\n");
3562
3563     FD_ZERO(&infd);
3564     FD_SET(nSocket, &infd);
3565     tv.tv_sec=RESPONSE_TIMEOUT;
3566     tv.tv_usec=0;
3567
3568     while (nRecv < MAX_REPLY_LEN)
3569     {
3570         if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
3571         {
3572             if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3573             {
3574                 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3575                 goto lend;
3576             }
3577
3578             if (lpszBuffer[nRecv] == '\n')
3579             {
3580                 bSuccess = TRUE;
3581                 break;
3582             }
3583             if (lpszBuffer[nRecv] != '\r')
3584                 nRecv++;
3585         }
3586         else
3587         {
3588             INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3589             goto lend;
3590         }
3591     }
3592
3593 lend:
3594     if (bSuccess)
3595     {
3596         lpszBuffer[nRecv] = '\0';
3597         *dwLen = nRecv - 1;
3598         TRACE(":%d %s\n", nRecv, lpszBuffer);
3599         return lpszBuffer;
3600     }
3601     else
3602     {
3603         return NULL;
3604     }
3605 }
3606
3607 /**********************************************************
3608  *      InternetQueryDataAvailable (WININET.@)
3609  *
3610  * Determines how much data is available to be read.
3611  *
3612  * RETURNS
3613  *   If there is data available then TRUE, otherwise if there
3614  *   is not or an error occurred then FALSE. Use GetLastError() to
3615  *   check for ERROR_NO_MORE_FILES to see if it was the former.
3616  */
3617 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3618                                 LPDWORD lpdwNumberOfBytesAvailble,
3619                                 DWORD dwFlags, DWORD dwConext)
3620 {
3621     LPWININETHTTPREQW lpwhr;
3622     BOOL retval = FALSE;
3623     char buffer[4048];
3624
3625     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hFile );
3626     if (NULL == lpwhr)
3627     {
3628         SetLastError(ERROR_NO_MORE_FILES);
3629         return FALSE;
3630     }
3631
3632     TRACE("-->  %p %i\n",lpwhr,lpwhr->hdr.htype);
3633
3634     switch (lpwhr->hdr.htype)
3635     {
3636     case WH_HHTTPREQ:
3637         if (!NETCON_recv(&lpwhr->netConnection, buffer,
3638                          4048, MSG_PEEK, (int *)lpdwNumberOfBytesAvailble))
3639         {
3640             SetLastError(ERROR_NO_MORE_FILES);
3641             retval = FALSE;
3642         }
3643         else
3644             retval = TRUE;
3645         break;
3646
3647     default:
3648         FIXME("unsupported file type\n");
3649         break;
3650     }
3651     WININET_Release( &lpwhr->hdr );
3652
3653     TRACE("<-- %i\n",retval);
3654     return retval;
3655 }
3656
3657
3658 /***********************************************************************
3659  *      InternetLockRequestFile (WININET.@)
3660  */
3661 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3662 *lphLockReqHandle)
3663 {
3664     FIXME("STUB\n");
3665     return FALSE;
3666 }
3667
3668 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3669 {
3670     FIXME("STUB\n");
3671     return FALSE;
3672 }
3673
3674
3675 /***********************************************************************
3676  *      InternetAutodial (WININET.@)
3677  *
3678  * On windows this function is supposed to dial the default internet
3679  * connection. We don't want to have Wine dial out to the internet so
3680  * we return TRUE by default. It might be nice to check if we are connected.
3681  *
3682  * RETURNS
3683  *   TRUE on success
3684  *   FALSE on failure
3685  *
3686  */
3687 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3688 {
3689     FIXME("STUB\n");
3690
3691     /* Tell that we are connected to the internet. */
3692     return TRUE;
3693 }
3694
3695 /***********************************************************************
3696  *      InternetAutodialHangup (WININET.@)
3697  *
3698  * Hangs up a connection made with InternetAutodial
3699  *
3700  * PARAM
3701  *    dwReserved
3702  * RETURNS
3703  *   TRUE on success
3704  *   FALSE on failure
3705  *
3706  */
3707 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3708 {
3709     FIXME("STUB\n");
3710
3711     /* we didn't dial, we don't disconnect */
3712     return TRUE;
3713 }
3714
3715 /***********************************************************************
3716  *      InternetCombineUrlA (WININET.@)
3717  *
3718  * Combine a base URL with a relative URL
3719  *
3720  * RETURNS
3721  *   TRUE on success
3722  *   FALSE on failure
3723  *
3724  */
3725
3726 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3727                                 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3728                                 DWORD dwFlags)
3729 {
3730     HRESULT hr=S_OK;
3731
3732     TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3733
3734     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3735     dwFlags ^= ICU_NO_ENCODE;
3736     hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3737
3738     return (hr==S_OK);
3739 }
3740
3741 /***********************************************************************
3742  *      InternetCombineUrlW (WININET.@)
3743  *
3744  * Combine a base URL with a relative URL
3745  *
3746  * RETURNS
3747  *   TRUE on success
3748  *   FALSE on failure
3749  *
3750  */
3751
3752 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3753                                 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3754                                 DWORD dwFlags)
3755 {
3756     HRESULT hr=S_OK;
3757
3758     TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3759
3760     /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3761     dwFlags ^= ICU_NO_ENCODE;
3762     hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3763
3764     return (hr==S_OK);
3765 }
3766
3767 /* max port num is 65535 => 5 digits */
3768 #define MAX_WORD_DIGITS 5
3769
3770 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3771     (url)->dw##component##Length : strlenW((url)->lpsz##component))
3772 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3773     (url)->dw##component##Length : strlen((url)->lpsz##component))
3774
3775 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3776 {
3777     if ((nScheme == INTERNET_SCHEME_HTTP) &&
3778         (nPort == INTERNET_DEFAULT_HTTP_PORT))
3779         return TRUE;
3780     if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3781         (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3782         return TRUE;
3783     if ((nScheme == INTERNET_SCHEME_FTP) &&
3784         (nPort == INTERNET_DEFAULT_FTP_PORT))
3785         return TRUE;
3786     if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3787         (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3788         return TRUE;
3789
3790     if (nPort == INTERNET_INVALID_PORT_NUMBER)
3791         return TRUE;
3792
3793     return FALSE;
3794 }
3795
3796 /* opaque urls do not fit into the standard url hierarchy and don't have
3797  * two following slashes */
3798 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3799 {
3800     return (nScheme != INTERNET_SCHEME_FTP) &&
3801            (nScheme != INTERNET_SCHEME_GOPHER) &&
3802            (nScheme != INTERNET_SCHEME_HTTP) &&
3803            (nScheme != INTERNET_SCHEME_HTTPS) &&
3804            (nScheme != INTERNET_SCHEME_FILE);
3805 }
3806
3807 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3808 {
3809     int index;
3810     if (scheme < INTERNET_SCHEME_FIRST)
3811         return NULL;
3812     index = scheme - INTERNET_SCHEME_FIRST;
3813     if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3814         return NULL;
3815     return (LPCWSTR)&url_schemes[index];
3816 }
3817
3818 /* we can calculate using ansi strings because we're just
3819  * calculating string length, not size
3820  */
3821 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3822                             LPDWORD lpdwUrlLength)
3823 {
3824     INTERNET_SCHEME nScheme;
3825
3826     *lpdwUrlLength = 0;
3827
3828     if (lpUrlComponents->lpszScheme)
3829     {
3830         DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3831         *lpdwUrlLength += dwLen;
3832         nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3833     }
3834     else
3835     {
3836         LPCWSTR scheme;
3837
3838         nScheme = lpUrlComponents->nScheme;
3839
3840         if (nScheme == INTERNET_SCHEME_DEFAULT)
3841             nScheme = INTERNET_SCHEME_HTTP;
3842         scheme = INTERNET_GetSchemeString(nScheme);
3843         *lpdwUrlLength += strlenW(scheme);
3844     }
3845
3846     (*lpdwUrlLength)++; /* ':' */
3847     if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3848         *lpdwUrlLength += strlen("//");
3849
3850     if (lpUrlComponents->lpszUserName)
3851     {
3852         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3853         *lpdwUrlLength += strlen("@");
3854     }
3855     else
3856     {
3857         if (lpUrlComponents->lpszPassword)
3858         {
3859             SetLastError(ERROR_INVALID_PARAMETER);
3860             return FALSE;
3861         }
3862     }
3863
3864     if (lpUrlComponents->lpszPassword)
3865     {
3866         *lpdwUrlLength += strlen(":");
3867         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3868     }
3869
3870     if (lpUrlComponents->lpszHostName)
3871     {
3872         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3873
3874         if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3875         {
3876             char szPort[MAX_WORD_DIGITS+1];
3877
3878             sprintf(szPort, "%d", lpUrlComponents->nPort);
3879             *lpdwUrlLength += strlen(szPort);
3880             *lpdwUrlLength += strlen(":");
3881         }
3882
3883         if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3884             (*lpdwUrlLength)++; /* '/' */
3885     }
3886
3887     if (lpUrlComponents->lpszUrlPath)
3888         *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3889
3890     return TRUE;
3891 }
3892
3893 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3894 {
3895     INT len;
3896
3897     ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3898
3899     urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3900     urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3901     urlCompW->nScheme = lpUrlComponents->nScheme;
3902     urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3903     urlCompW->nPort = lpUrlComponents->nPort;
3904     urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3905     urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3906     urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3907     urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3908
3909     if (lpUrlComponents->lpszScheme)
3910     {
3911         len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3912         urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3913         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3914                             -1, urlCompW->lpszScheme, len);
3915     }
3916
3917     if (lpUrlComponents->lpszHostName)
3918     {
3919         len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3920         urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3921         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3922                             -1, urlCompW->lpszHostName, len);
3923     }
3924
3925     if (lpUrlComponents->lpszUserName)
3926     {
3927         len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3928         urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3929         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3930                             -1, urlCompW->lpszUserName, len);
3931     }
3932
3933     if (lpUrlComponents->lpszPassword)
3934     {
3935         len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3936         urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3937         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3938                             -1, urlCompW->lpszPassword, len);
3939     }
3940
3941     if (lpUrlComponents->lpszUrlPath)
3942     {
3943         len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3944         urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3945         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3946                             -1, urlCompW->lpszUrlPath, len);
3947     }
3948
3949     if (lpUrlComponents->lpszExtraInfo)
3950     {
3951         len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3952         urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3953         MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3954                             -1, urlCompW->lpszExtraInfo, len);
3955     }
3956 }
3957
3958 /***********************************************************************
3959  *      InternetCreateUrlA (WININET.@)
3960  *
3961  * See InternetCreateUrlW.
3962  */
3963 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3964                                LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3965 {
3966     BOOL ret;
3967     LPWSTR urlW = NULL;
3968     URL_COMPONENTSW urlCompW;
3969
3970     TRACE("(%p,%ld,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3971
3972     if (!lpUrlComponents)
3973         return FALSE;
3974
3975     if (lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3976     {
3977         SetLastError(ERROR_INVALID_PARAMETER);
3978         return FALSE;
3979     }
3980
3981     convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3982
3983     if (lpszUrl)
3984         urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3985
3986     ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3987
3988     if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3989         *lpdwUrlLength /= sizeof(WCHAR);
3990
3991     /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3992     * minus one, so add one to leave room for NULL terminator
3993     */
3994     if (ret)
3995         WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3996
3997     HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3998     HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3999     HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
4000     HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
4001     HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
4002     HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
4003     HeapFree(GetProcessHeap(), 0, urlW);
4004
4005     return ret;
4006 }
4007
4008 /***********************************************************************
4009  *      InternetCreateUrlW (WININET.@)
4010  *
4011  * Creates a URL from its component parts.
4012  *
4013  * PARAMS
4014  *  lpUrlComponents [I] URL Components.
4015  *  dwFlags         [I] Flags. See notes.
4016  *  lpszUrl         [I] Buffer in which to store the created URL.
4017  *  lpdwUrlLength   [I/O] On input, the length of the buffer pointed to by
4018  *                        lpszUrl in characters. On output, the number of bytes
4019  *                        required to store the URL including terminator.
4020  *
4021  * NOTES
4022  *
4023  * The dwFlags parameter can be zero or more of the following:
4024  *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4025  *
4026  * RETURNS
4027  *   TRUE on success
4028  *   FALSE on failure
4029  *
4030  */
4031 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4032                                LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4033 {
4034     DWORD dwLen;
4035     INTERNET_SCHEME nScheme;
4036
4037     static const WCHAR slashSlashW[] = {'/','/'};
4038     static const WCHAR percentD[] = {'%','d',0};
4039
4040     TRACE("(%p,%ld,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4041
4042     if (!lpUrlComponents)
4043         return FALSE;
4044
4045     if (lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4046     {
4047         SetLastError(ERROR_INVALID_PARAMETER);
4048         return FALSE;
4049     }
4050
4051     if (!calc_url_length(lpUrlComponents, &dwLen))
4052         return FALSE;
4053
4054     if (!lpszUrl || *lpdwUrlLength < dwLen)
4055     {
4056         *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4057         SetLastError(ERROR_INSUFFICIENT_BUFFER);
4058         return FALSE;
4059     }
4060
4061     *lpdwUrlLength = dwLen;
4062     lpszUrl[0] = 0x00;
4063
4064     dwLen = 0;
4065
4066     if (lpUrlComponents->lpszScheme)
4067     {
4068         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4069         memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4070         lpszUrl += dwLen;
4071
4072         nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4073     }
4074     else
4075     {
4076         LPCWSTR scheme;
4077         nScheme = lpUrlComponents->nScheme;
4078
4079         if (nScheme == INTERNET_SCHEME_DEFAULT)
4080             nScheme = INTERNET_SCHEME_HTTP;
4081
4082         scheme = INTERNET_GetSchemeString(nScheme);
4083         dwLen = strlenW(scheme);
4084         memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4085         lpszUrl += dwLen;
4086     }
4087
4088     /* all schemes are followed by at least a colon */
4089     *lpszUrl = ':';
4090     lpszUrl++;
4091
4092     if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4093     {
4094         memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4095         lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4096     }
4097
4098     if (lpUrlComponents->lpszUserName)
4099     {
4100         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4101         memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4102         lpszUrl += dwLen;
4103
4104         if (lpUrlComponents->lpszPassword)
4105         {
4106             *lpszUrl = ':';
4107             lpszUrl++;
4108
4109             dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4110             memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4111             lpszUrl += dwLen;
4112         }
4113
4114         *lpszUrl = '@';
4115         lpszUrl++;
4116     }
4117
4118     if (lpUrlComponents->lpszHostName)
4119     {
4120         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4121         memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4122         lpszUrl += dwLen;
4123
4124         if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4125         {
4126             WCHAR szPort[MAX_WORD_DIGITS+1];
4127
4128             sprintfW(szPort, percentD, lpUrlComponents->nPort);
4129             *lpszUrl = ':';
4130             lpszUrl++;
4131             dwLen = strlenW(szPort);
4132             memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4133             lpszUrl += dwLen;
4134         }
4135
4136         /* add slash between hostname and path if necessary */
4137         if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4138         {
4139             *lpszUrl = '/';
4140             lpszUrl++;
4141         }
4142     }
4143
4144
4145     if (lpUrlComponents->lpszUrlPath)
4146     {
4147         dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4148         memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4149         lpszUrl += dwLen;
4150     }
4151
4152     *lpszUrl = '\0';
4153
4154     return TRUE;
4155 }
4156
4157 /***********************************************************************
4158  *      InternetConfirmZoneCrossingA (WININET.@)
4159  *
4160  */
4161 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4162 {
4163     FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4164     return ERROR_SUCCESS;
4165 }
4166
4167 /***********************************************************************
4168  *      InternetConfirmZoneCrossingW (WININET.@)
4169  *
4170  */
4171 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4172 {
4173     FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4174     return ERROR_SUCCESS;
4175 }
4176
4177 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4178                             LPDWORD lpdwConnection, DWORD dwReserved )
4179 {
4180     FIXME("(%p, %p, 0x%08lx, %p, 0x%08lx) stub\n", hwndParent, lpszConnectoid, dwFlags,
4181           lpdwConnection, dwReserved);
4182     return ERROR_SUCCESS;
4183 }
4184
4185 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4186                             LPDWORD lpdwConnection, DWORD dwReserved )
4187 {
4188     FIXME("(%p, %p, 0x%08lx, %p, 0x%08lx) stub\n", hwndParent, lpszConnectoid, dwFlags,
4189           lpdwConnection, dwReserved);
4190     return ERROR_SUCCESS;
4191 }
4192
4193 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4194 {
4195     FIXME("(%s, %p, 0x%08lx) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4196     return TRUE;
4197 }
4198
4199 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4200 {
4201     FIXME("(%s, %p, 0x%08lx) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4202     return TRUE;
4203 }
4204
4205 DWORD WINAPI InternetHangUp( DWORD dwConnection, DWORD dwReserved )
4206 {
4207     FIXME("(0x%08lx, 0x%08lx) stub\n", dwConnection, dwReserved);
4208     return ERROR_SUCCESS;
4209 }
4210
4211 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4212                               PBYTE pbHexHash )
4213 {
4214     FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4215           debugstr_w(pwszTarget), pbHexHash);
4216     return FALSE;
4217 }
4218
4219 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4220 {
4221     FIXME("(%p, 0x%08lx) stub\n", hInternet, dwError);
4222     return FALSE;
4223 }