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