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