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
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.
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.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
58 #include "wine/debug.h"
60 #define NO_SHLWAPI_STREAM
63 #include "wine/exception.h"
69 #include "wine/unicode.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
73 #define MAX_IDLE_WORKER 1000*60*1
74 #define MAX_WORKER_THREADS 10
75 #define RESPONSE_TIMEOUT 30
77 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
78 (LPWININETAPPINFOW)(((LPWININETFTPSESSIONW)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
84 CHAR response[MAX_REPLY_LEN];
85 } WITHREADERROR, *LPWITHREADERROR;
87 static VOID INTERNET_CloseHandle(LPWININETHANDLEHEADER hdr);
88 BOOL WINAPI INTERNET_FindNextFileW(LPWININETFINDNEXTW lpwh, LPVOID lpvFindData);
89 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
90 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext);
91 static VOID INTERNET_ExecuteWork();
93 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
95 DWORD dwNumIdleThreads;
97 HANDLE hEventArray[2];
98 #define hQuitEvent hEventArray[0]
99 #define hWorkEvent hEventArray[1]
100 CRITICAL_SECTION csQueue;
101 LPWORKREQUEST lpHeadWorkQueue;
102 LPWORKREQUEST lpWorkQueueTail;
103 HMODULE WININET_hModule;
105 extern void URLCacheContainers_CreateDefaults();
106 extern void URLCacheContainers_DeleteAll();
108 #define HANDLE_CHUNK_SIZE 0x10
110 static CRITICAL_SECTION WININET_cs;
111 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
114 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
115 0, 0, { 0, (DWORD)(__FILE__ ": WININET_cs") }
117 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
119 static LPWININETHANDLEHEADER *WININET_Handles;
120 static UINT WININET_dwNextHandle;
121 static UINT WININET_dwMaxHandles;
123 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
125 LPWININETHANDLEHEADER *p;
126 UINT handle = 0, num;
128 EnterCriticalSection( &WININET_cs );
129 if( !WININET_dwMaxHandles )
131 num = HANDLE_CHUNK_SIZE;
132 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
137 WININET_dwMaxHandles = num;
139 if( WININET_dwMaxHandles == WININET_dwNextHandle )
141 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
142 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
143 WININET_Handles, sizeof (UINT)* num);
147 WININET_dwMaxHandles = num;
150 handle = WININET_dwNextHandle;
151 if( WININET_Handles[handle] )
152 ERR("handle isn't free but should be\n");
153 WININET_Handles[handle] = WININET_AddRef( info );
155 while( WININET_Handles[WININET_dwNextHandle] &&
156 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
157 WININET_dwNextHandle++;
160 LeaveCriticalSection( &WININET_cs );
162 return (HINTERNET) (handle+1);
165 HINTERNET WININET_FindHandle( LPWININETHANDLEHEADER info )
169 EnterCriticalSection( &WININET_cs );
170 for( i=0; i<WININET_dwMaxHandles; i++ )
172 if( info == WININET_Handles[i] )
174 WININET_AddRef( info );
179 LeaveCriticalSection( &WININET_cs );
181 return (HINTERNET) handle;
184 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
187 TRACE("%p -> refcount = %ld\n", info, info->dwRefCount );
191 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
193 LPWININETHANDLEHEADER info = NULL;
194 UINT handle = (UINT) hinternet;
196 EnterCriticalSection( &WININET_cs );
198 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
199 WININET_Handles[handle-1] )
200 info = WININET_AddRef( WININET_Handles[handle-1] );
202 LeaveCriticalSection( &WININET_cs );
204 TRACE("handle %d -> %p\n", handle, info);
209 BOOL WININET_Release( LPWININETHANDLEHEADER info )
212 TRACE( "object %p refcount = %ld\n", info, info->dwRefCount );
213 if( !info->dwRefCount )
215 TRACE( "destroying object %p\n", info);
216 info->destroy( info );
221 BOOL WININET_FreeHandle( HINTERNET hinternet )
224 UINT handle = (UINT) hinternet;
225 LPWININETHANDLEHEADER info = NULL;
227 EnterCriticalSection( &WININET_cs );
229 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
232 if( WININET_Handles[handle] )
234 info = WININET_Handles[handle];
235 TRACE( "destroying handle %d for object %p\n", handle+1, info);
236 WININET_Handles[handle] = NULL;
238 if( WININET_dwNextHandle > handle )
239 WININET_dwNextHandle = handle;
243 LeaveCriticalSection( &WININET_cs );
246 WININET_Release( info );
251 /***********************************************************************
252 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
255 * hinstDLL [I] handle to the DLL's instance
257 * lpvReserved [I] reserved, must be NULL
264 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
266 TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
269 case DLL_PROCESS_ATTACH:
271 g_dwTlsErrIndex = TlsAlloc();
273 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
276 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
277 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
278 InitializeCriticalSection(&csQueue);
280 URLCacheContainers_CreateDefaults();
283 dwNumIdleThreads = 0;
286 WININET_hModule = (HMODULE)hinstDLL;
288 case DLL_THREAD_ATTACH:
290 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
294 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
298 case DLL_THREAD_DETACH:
299 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
301 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
303 HeapFree(GetProcessHeap(), 0, lpwite);
307 case DLL_PROCESS_DETACH:
309 URLCacheContainers_DeleteAll();
311 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
313 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
314 TlsFree(g_dwTlsErrIndex);
317 SetEvent(hQuitEvent);
319 CloseHandle(hQuitEvent);
320 CloseHandle(hWorkEvent);
321 DeleteCriticalSection(&csQueue);
329 /***********************************************************************
330 * InternetInitializeAutoProxyDll (WININET.@)
332 * Setup the internal proxy
341 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
344 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
348 /***********************************************************************
349 * DetectAutoProxyUrl (WININET.@)
351 * Auto detect the proxy url
357 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
358 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
361 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
366 /***********************************************************************
367 * INTERNET_ConfigureProxyFromReg
370 * The proxy may be specified in the form 'http=proxy.my.org'
371 * Presumably that means there can be ftp=ftpproxy.my.org too.
373 static BOOL INTERNET_ConfigureProxyFromReg( LPWININETAPPINFOW lpwai )
376 DWORD r, keytype, len, enabled;
377 LPSTR lpszInternetSettings =
378 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
379 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
381 r = RegOpenKeyA(HKEY_CURRENT_USER, lpszInternetSettings, &key);
382 if ( r != ERROR_SUCCESS )
385 len = sizeof enabled;
386 r = RegQueryValueExA( key, "ProxyEnable", NULL, &keytype,
387 (BYTE*)&enabled, &len);
388 if( (r == ERROR_SUCCESS) && enabled )
390 TRACE("Proxy is enabled.\n");
392 /* figure out how much memory the proxy setting takes */
393 r = RegQueryValueExW( key, szProxyServer, NULL, &keytype,
395 if( (r == ERROR_SUCCESS) && len && (keytype == REG_SZ) )
398 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
400 szProxy=HeapAlloc( GetProcessHeap(), 0, len );
401 RegQueryValueExW( key, szProxyServer, NULL, &keytype,
402 (BYTE*)szProxy, &len);
404 /* find the http proxy, and strip away everything else */
405 p = strstrW( szProxy, szHttp );
408 p += lstrlenW(szHttp);
409 lstrcpyW( szProxy, p );
411 p = strchrW( szProxy, ' ' );
415 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
416 lpwai->lpszProxy = szProxy;
418 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
421 ERR("Couldn't read proxy server settings.\n");
424 TRACE("Proxy is not enabled.\n");
430 /***********************************************************************
431 * InternetOpenW (WININET.@)
433 * Per-application initialization of wininet
436 * HINTERNET on success
440 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
441 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
443 LPWININETAPPINFOW lpwai = NULL;
444 HINTERNET handle = NULL;
446 if (TRACE_ON(wininet)) {
447 #define FE(x) { x, #x }
448 static const wininet_flag_info access_type[] = {
449 FE(INTERNET_OPEN_TYPE_PRECONFIG),
450 FE(INTERNET_OPEN_TYPE_DIRECT),
451 FE(INTERNET_OPEN_TYPE_PROXY),
452 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
454 static const wininet_flag_info flag[] = {
455 FE(INTERNET_FLAG_ASYNC),
456 FE(INTERNET_FLAG_FROM_CACHE),
457 FE(INTERNET_FLAG_OFFLINE)
461 const char *access_type_str = "Unknown";
462 DWORD flag_val = dwFlags;
464 TRACE("(%s, %li, %s, %s, %li)\n", debugstr_w(lpszAgent), dwAccessType,
465 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
466 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
467 if (access_type[i].val == dwAccessType) {
468 access_type_str = access_type[i].name;
472 TRACE(" access type : %s\n", access_type_str);
474 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
475 if (flag[i].val & flag_val) {
476 DPRINTF(" %s", flag[i].name);
477 flag_val &= ~flag[i].val;
480 if (flag_val) DPRINTF(" Unknown flags (%08lx)", flag_val);
484 /* Clear any error information */
485 INTERNET_SetLastError(0);
487 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOW));
490 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
494 memset(lpwai, 0, sizeof(WININETAPPINFOW));
495 lpwai->hdr.htype = WH_HINIT;
496 lpwai->hdr.lpwhparent = NULL;
497 lpwai->hdr.dwFlags = dwFlags;
498 lpwai->hdr.dwRefCount = 1;
499 lpwai->hdr.destroy = INTERNET_CloseHandle;
500 lpwai->dwAccessType = dwAccessType;
501 lpwai->lpszProxyUsername = NULL;
502 lpwai->lpszProxyPassword = NULL;
504 handle = WININET_AllocHandle( &lpwai->hdr );
507 HeapFree( GetProcessHeap(), 0, lpwai );
508 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
512 if (NULL != lpszAgent)
514 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
515 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
516 if (lpwai->lpszAgent)
517 lstrcpyW( lpwai->lpszAgent, lpszAgent );
519 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
520 INTERNET_ConfigureProxyFromReg( lpwai );
521 else if (NULL != lpszProxy)
523 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
524 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
525 if (lpwai->lpszProxy)
526 lstrcpyW( lpwai->lpszProxy, lpszProxy );
529 if (NULL != lpszProxyBypass)
531 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
532 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
533 if (lpwai->lpszProxyBypass)
534 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
539 WININET_Release( &lpwai->hdr );
541 TRACE("returning %p\n", lpwai);
547 /***********************************************************************
548 * InternetOpenA (WININET.@)
550 * Per-application initialization of wininet
553 * HINTERNET on success
557 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
558 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
560 HINTERNET rc = (HINTERNET)NULL;
562 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
564 TRACE("(%s, 0x%08lx, %s, %s, 0x%08lx)\n", debugstr_a(lpszAgent),
565 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
569 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
570 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
571 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
576 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
577 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
578 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
581 if( lpszProxyBypass )
583 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
584 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
585 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
588 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
591 HeapFree(GetProcessHeap(), 0, szAgent);
593 HeapFree(GetProcessHeap(), 0, szProxy);
595 HeapFree(GetProcessHeap(), 0, szBypass);
600 /***********************************************************************
601 * InternetGetLastResponseInfoA (WININET.@)
603 * Return last wininet error description on the calling thread
606 * TRUE on success of writing to buffer
610 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
611 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
613 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
617 *lpdwError = lpwite->dwError;
620 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
621 *lpdwBufferLength = strlen(lpszBuffer);
624 *lpdwBufferLength = 0;
630 /***********************************************************************
631 * InternetGetConnectedState (WININET.@)
633 * Return connected state
637 * if lpdwStatus is not null, return the status (off line,
638 * modem, lan...) in it.
639 * FALSE if not connected
641 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
643 TRACE("(%p, 0x%08lx)\n", lpdwStatus, dwReserved);
646 FIXME("always returning LAN connection.\n");
647 *lpdwStatus = INTERNET_CONNECTION_LAN;
653 /***********************************************************************
654 * InternetGetConnectedStateExW (WININET.@)
656 * Return connected state
660 * lpdwStatus [O] Flags specifying the status of the internet connection.
661 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
662 * dwNameLen [I] Size of the buffer, in characters.
663 * dwReserved [I] Reserved. Must be set to 0.
667 * if lpdwStatus is not null, return the status (off line,
668 * modem, lan...) in it.
669 * FALSE if not connected
672 * If the system has no available network connections, an empty string is
673 * stored in lpszConnectionName. If there is a LAN connection, a localized
674 * "LAN Connection" string is stored. Presumably, if only a dial-up
675 * connection is available then the name of the dial-up connection is
676 * returned. Why any application, other than the "Internet Settings" CPL,
677 * would want to use this function instead of the simpler InternetGetConnectedStateW
678 * function is beyond me.
680 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
681 DWORD dwNameLen, DWORD dwReserved)
683 TRACE("(%p, %p, %ld, 0x%08lx)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
690 FIXME("always returning LAN connection.\n");
691 *lpdwStatus = INTERNET_CONNECTION_LAN;
693 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
697 /***********************************************************************
698 * InternetGetConnectedStateExA (WININET.@)
700 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
701 DWORD dwNameLen, DWORD dwReserved)
703 LPWSTR lpwszConnectionName = NULL;
706 TRACE("(%p, %p, %ld, 0x%08lx)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
708 if (lpszConnectionName && dwNameLen > 0)
709 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
711 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
713 if (rc && lpwszConnectionName)
715 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
716 dwNameLen, NULL, NULL);
718 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
725 /***********************************************************************
726 * InternetConnectW (WININET.@)
728 * Open a ftp, gopher or http session
731 * HINTERNET a session handle on success
735 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
736 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
737 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
738 DWORD dwService, DWORD dwFlags, DWORD dwContext)
740 LPWININETAPPINFOW hIC;
741 HINTERNET rc = (HINTERNET) NULL;
743 TRACE("(%p, %s, %i, %s, %s, %li, %li, %li)\n", hInternet, debugstr_w(lpszServerName),
744 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
745 dwService, dwFlags, dwContext);
747 /* Clear any error information */
748 INTERNET_SetLastError(0);
749 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
750 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
755 case INTERNET_SERVICE_FTP:
756 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
757 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
760 case INTERNET_SERVICE_HTTP:
761 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
762 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
765 case INTERNET_SERVICE_GOPHER:
771 WININET_Release( &hIC->hdr );
773 TRACE("returning %p\n", rc);
778 /***********************************************************************
779 * InternetConnectA (WININET.@)
781 * Open a ftp, gopher or http session
784 * HINTERNET a session handle on success
788 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
789 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
790 LPCSTR lpszUserName, LPCSTR lpszPassword,
791 DWORD dwService, DWORD dwFlags, DWORD dwContext)
793 HINTERNET rc = (HINTERNET)NULL;
795 LPWSTR szServerName = NULL;
796 LPWSTR szUserName = NULL;
797 LPWSTR szPassword = NULL;
801 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
802 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
803 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
807 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
808 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
809 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
813 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
814 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
815 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
819 rc = InternetConnectW(hInternet, szServerName, nServerPort,
820 szUserName, szPassword, dwService, dwFlags, dwContext);
822 if (szServerName) HeapFree(GetProcessHeap(), 0, szServerName);
823 if (szUserName) HeapFree(GetProcessHeap(), 0, szUserName);
824 if (szPassword) HeapFree(GetProcessHeap(), 0, szPassword);
829 /***********************************************************************
830 * InternetFindNextFileA (WININET.@)
832 * Continues a file search from a previous call to FindFirstFile
839 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
844 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
846 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
850 /***********************************************************************
851 * InternetFindNextFileW (WININET.@)
853 * Continues a file search from a previous call to FindFirstFile
860 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
862 LPWININETAPPINFOW hIC = NULL;
863 LPWININETFINDNEXTW lpwh;
864 BOOL bSuccess = FALSE;
868 lpwh = (LPWININETFINDNEXTW) WININET_GetObject( hFind );
869 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
871 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
875 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
876 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
878 WORKREQUEST workRequest;
879 struct WORKREQ_INTERNETFINDNEXTW *req;
881 workRequest.asyncall = INTERNETFINDNEXTW;
882 workRequest.hdr = WININET_AddRef( &lpwh->hdr );
883 req = &workRequest.u.InternetFindNextW;
884 req->lpFindFileData = lpvFindData;
886 bSuccess = INTERNET_AsyncCall(&workRequest);
890 bSuccess = INTERNET_FindNextFileW(lpwh, lpvFindData);
894 WININET_Release( &lpwh->hdr );
898 /***********************************************************************
899 * INTERNET_FindNextFileW (Internal)
901 * Continues a file search from a previous call to FindFirstFile
908 BOOL WINAPI INTERNET_FindNextFileW(LPWININETFINDNEXTW lpwh, LPVOID lpvFindData)
910 BOOL bSuccess = TRUE;
911 LPWIN32_FIND_DATAW lpFindFileData;
915 assert (lpwh->hdr.htype == WH_HFINDNEXT);
917 /* Clear any error information */
918 INTERNET_SetLastError(0);
920 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
922 FIXME("Only FTP find next supported\n");
923 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
927 TRACE("index(%ld) size(%ld)\n", lpwh->index, lpwh->size);
929 lpFindFileData = (LPWIN32_FIND_DATAW) lpvFindData;
930 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
932 if (lpwh->index >= lpwh->size)
934 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
939 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
942 TRACE("\nName: %s\nSize: %ld\n", debugstr_w(lpFindFileData->cFileName), lpFindFileData->nFileSizeLow);
946 if (lpwh->hdr.dwFlags & INTERNET_FLAG_ASYNC && lpwh->hdr.lpfnStatusCB)
948 INTERNET_ASYNC_RESULT iar;
950 iar.dwResult = (DWORD)bSuccess;
951 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
952 INTERNET_GetLastError();
954 SendAsyncCallback(&lpwh->hdr, lpwh->hdr.dwContext,
955 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
956 sizeof(INTERNET_ASYNC_RESULT));
963 /***********************************************************************
964 * INTERNET_CloseHandle (internal)
966 * Close internet handle
972 static VOID INTERNET_CloseHandle(LPWININETHANDLEHEADER hdr)
974 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
978 if (lpwai->lpszAgent)
979 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
981 if (lpwai->lpszProxy)
982 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
984 if (lpwai->lpszProxyBypass)
985 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
987 if (lpwai->lpszProxyUsername)
988 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
990 if (lpwai->lpszProxyPassword)
991 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
993 HeapFree(GetProcessHeap(), 0, lpwai);
997 /***********************************************************************
998 * InternetCloseHandle (WININET.@)
1000 * Generic close handle function
1007 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1009 LPWININETHANDLEHEADER lpwh;
1011 TRACE("%p\n",hInternet);
1013 lpwh = WININET_GetObject( hInternet );
1016 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1020 SendAsyncCallback(lpwh, lpwh->dwContext,
1021 INTERNET_STATUS_HANDLE_CLOSING, &hInternet,
1022 sizeof(HINTERNET*));
1024 if( lpwh->lpwhparent )
1025 WININET_Release( lpwh->lpwhparent );
1026 WININET_FreeHandle( hInternet );
1027 WININET_Release( lpwh );
1033 /***********************************************************************
1034 * ConvertUrlComponentValue (Internal)
1036 * Helper function for InternetCrackUrlW
1039 void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1040 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1044 if (*dwComponentLen != 0)
1046 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1047 if (*lppszComponent == NULL)
1049 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1050 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1051 *dwComponentLen = nASCIILength;
1055 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1056 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1057 (*lppszComponent)[ncpylen]=0;
1058 *dwComponentLen = ncpylen;
1064 /***********************************************************************
1065 * InternetCrackUrlA (WININET.@)
1067 * Break up URL into its components
1069 * TODO: Handle dwFlags
1076 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1077 LPURL_COMPONENTSA lpUrlComponents)
1080 URL_COMPONENTSW UCW;
1085 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1086 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1087 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1089 memset(&UCW,0,sizeof(UCW));
1090 if(lpUrlComponents->dwHostNameLength!=0)
1091 UCW.dwHostNameLength=1;
1092 if(lpUrlComponents->dwUserNameLength!=0)
1093 UCW.dwUserNameLength=1;
1094 if(lpUrlComponents->dwPasswordLength!=0)
1095 UCW.dwPasswordLength=1;
1096 if(lpUrlComponents->dwUrlPathLength!=0)
1097 UCW.dwUrlPathLength=1;
1098 if(lpUrlComponents->dwSchemeLength!=0)
1099 UCW.dwSchemeLength=1;
1100 if(lpUrlComponents->dwExtraInfoLength!=0)
1101 UCW.dwExtraInfoLength=1;
1102 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1104 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1107 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1108 UCW.lpszHostName, UCW.dwHostNameLength,
1110 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1111 UCW.lpszUserName, UCW.dwUserNameLength,
1113 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1114 UCW.lpszPassword, UCW.dwPasswordLength,
1116 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1117 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1119 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1120 UCW.lpszScheme, UCW.dwSchemeLength,
1122 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1123 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1125 lpUrlComponents->nScheme=UCW.nScheme;
1126 lpUrlComponents->nPort=UCW.nPort;
1127 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1129 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1130 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1131 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1132 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1133 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1138 /***********************************************************************
1139 * GetInternetSchemeW (internal)
1145 * INTERNET_SCHEME_UNKNOWN on failure
1148 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1150 INTERNET_SCHEME iScheme=INTERNET_SCHEME_UNKNOWN;
1151 static const WCHAR lpszFtp[]={'f','t','p',0};
1152 static const WCHAR lpszGopher[]={'g','o','p','h','e','r',0};
1153 static const WCHAR lpszHttp[]={'h','t','t','p',0};
1154 static const WCHAR lpszHttps[]={'h','t','t','p','s',0};
1155 static const WCHAR lpszFile[]={'f','i','l','e',0};
1156 static const WCHAR lpszNews[]={'n','e','w','s',0};
1157 static const WCHAR lpszMailto[]={'m','a','i','l','t','o',0};
1158 static const WCHAR lpszRes[]={'r','e','s',0};
1159 WCHAR* tempBuffer=NULL;
1161 if(lpszScheme==NULL)
1162 return INTERNET_SCHEME_UNKNOWN;
1164 tempBuffer=HeapAlloc(GetProcessHeap(),0,(nMaxCmp+1)*sizeof(WCHAR));
1165 strncpyW(tempBuffer,lpszScheme,nMaxCmp);
1166 tempBuffer[nMaxCmp]=0;
1167 strlwrW(tempBuffer);
1168 if (nMaxCmp==strlenW(lpszFtp) && !strncmpW(lpszFtp, tempBuffer, nMaxCmp))
1169 iScheme=INTERNET_SCHEME_FTP;
1170 else if (nMaxCmp==strlenW(lpszGopher) && !strncmpW(lpszGopher, tempBuffer, nMaxCmp))
1171 iScheme=INTERNET_SCHEME_GOPHER;
1172 else if (nMaxCmp==strlenW(lpszHttp) && !strncmpW(lpszHttp, tempBuffer, nMaxCmp))
1173 iScheme=INTERNET_SCHEME_HTTP;
1174 else if (nMaxCmp==strlenW(lpszHttps) && !strncmpW(lpszHttps, tempBuffer, nMaxCmp))
1175 iScheme=INTERNET_SCHEME_HTTPS;
1176 else if (nMaxCmp==strlenW(lpszFile) && !strncmpW(lpszFile, tempBuffer, nMaxCmp))
1177 iScheme=INTERNET_SCHEME_FILE;
1178 else if (nMaxCmp==strlenW(lpszNews) && !strncmpW(lpszNews, tempBuffer, nMaxCmp))
1179 iScheme=INTERNET_SCHEME_NEWS;
1180 else if (nMaxCmp==strlenW(lpszMailto) && !strncmpW(lpszMailto, tempBuffer, nMaxCmp))
1181 iScheme=INTERNET_SCHEME_MAILTO;
1182 else if (nMaxCmp==strlenW(lpszRes) && !strncmpW(lpszRes, tempBuffer, nMaxCmp))
1183 iScheme=INTERNET_SCHEME_RES;
1184 HeapFree(GetProcessHeap(),0,tempBuffer);
1188 /***********************************************************************
1189 * SetUrlComponentValueW (Internal)
1191 * Helper function for InternetCrackUrlW
1198 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1200 TRACE("%s (%ld)\n", debugstr_wn(lpszStart,len), len);
1202 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1205 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1207 if (*lppszComponent == NULL)
1209 *lppszComponent = (LPWSTR)lpszStart;
1210 *dwComponentLen = len;
1214 DWORD ncpylen = min((*dwComponentLen)-1, len);
1215 strncpyW(*lppszComponent, lpszStart, ncpylen);
1216 (*lppszComponent)[ncpylen] = '\0';
1217 *dwComponentLen = ncpylen;
1224 /***********************************************************************
1225 * InternetCrackUrlW (WININET.@)
1227 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1228 LPURL_COMPONENTSW lpUC)
1232 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1235 LPCWSTR lpszParam = NULL;
1236 BOOL bIsAbsolute = FALSE;
1237 LPCWSTR lpszap = lpszUrl;
1238 LPCWSTR lpszcp = NULL;
1239 const WCHAR lpszSeparators[3]={';','?',0};
1240 const WCHAR lpszSlash[2]={'/',0};
1242 dwUrlLength=strlenW(lpszUrl);
1246 /* Determine if the URI is absolute. */
1247 while (*lpszap != '\0')
1249 if (isalnumW(*lpszap))
1254 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1261 lpszcp = lpszUrl; /* Relative url */
1267 /* Parse <params> */
1268 lpszParam = strpbrkW(lpszap, lpszSeparators);
1269 if (lpszParam != NULL)
1271 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1272 lpszParam, dwUrlLength-(lpszParam-lpszUrl));
1275 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1278 static const WCHAR wszAbout[]={'a','b','o','u','t',':',0};
1280 /* Get scheme first. */
1281 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1282 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1283 lpszUrl, lpszcp - lpszUrl);
1285 /* Eat ':' in protocol. */
1288 /* if the scheme is "about", there is no host */
1289 if(strncmpW(wszAbout,lpszUrl, lpszcp - lpszUrl)==0)
1291 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1292 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1293 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1298 /* Skip over slashes. */
1310 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1314 lpszNetLoc = min(lpszNetLoc, lpszParam);
1316 lpszNetLoc = lpszParam;
1318 else if (!lpszNetLoc)
1319 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1327 /* [<user>[<:password>]@]<host>[:<port>] */
1328 /* First find the user and password if they exist */
1330 lpszHost = strchrW(lpszcp, '@');
1331 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1333 /* username and password not specified. */
1334 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1335 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1337 else /* Parse out username and password */
1339 LPCWSTR lpszUser = lpszcp;
1340 LPCWSTR lpszPasswd = lpszHost;
1342 while (lpszcp < lpszHost)
1345 lpszPasswd = lpszcp;
1350 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1351 lpszUser, lpszPasswd - lpszUser);
1353 if (lpszPasswd != lpszHost)
1355 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1356 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1357 lpszHost - lpszPasswd);
1359 lpszcp++; /* Advance to beginning of host */
1362 /* Parse <host><:port> */
1365 lpszPort = lpszNetLoc;
1367 /* special case for res:// URLs: there is no port here, so the host is the
1368 entire string up to the first '/' */
1369 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1371 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1372 lpszHost, lpszPort - lpszHost);
1378 while (lpszcp < lpszNetLoc)
1386 /* If the scheme is "file" and the host is just one letter, it's not a host */
1387 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1390 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1396 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1397 lpszHost, lpszPort - lpszHost);
1398 if (lpszPort != lpszNetLoc)
1399 lpUC->nPort = atoiW(++lpszPort);
1408 /* Here lpszcp points to:
1410 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1411 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1413 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1417 /* Only truncate the parameter list if it's already been saved
1418 * in lpUC->lpszExtraInfo.
1420 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1421 len = lpszParam - lpszcp;
1424 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1425 * newlines if necessary.
1427 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1428 if (lpsznewline != NULL)
1429 len = lpsznewline - lpszcp;
1431 len = dwUrlLength-(lpszcp-lpszUrl);
1434 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1439 lpUC->dwUrlPathLength = 0;
1442 TRACE("%s: host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1443 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1444 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1445 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1450 /***********************************************************************
1451 * InternetAttemptConnect (WININET.@)
1453 * Attempt to make a connection to the internet
1456 * ERROR_SUCCESS on success
1457 * Error value on failure
1460 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1463 return ERROR_SUCCESS;
1467 /***********************************************************************
1468 * InternetCanonicalizeUrlA (WININET.@)
1470 * Escape unsafe characters and spaces
1477 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1478 LPDWORD lpdwBufferLength, DWORD dwFlags)
1481 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
1482 lpdwBufferLength, dwFlags);
1484 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1485 dwFlags ^= ICU_NO_ENCODE;
1487 dwFlags |= 0x80000000; /* Don't know what this means */
1489 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1491 return (hr == S_OK) ? TRUE : FALSE;
1494 /***********************************************************************
1495 * InternetCanonicalizeUrlW (WININET.@)
1497 * Escape unsafe characters and spaces
1504 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1505 LPDWORD lpdwBufferLength, DWORD dwFlags)
1508 TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
1509 lpdwBufferLength, dwFlags);
1511 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1512 dwFlags ^= ICU_NO_ENCODE;
1514 dwFlags |= 0x80000000; /* Don't know what this means */
1516 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1518 return (hr == S_OK) ? TRUE : FALSE;
1522 /***********************************************************************
1523 * InternetSetStatusCallbackA (WININET.@)
1525 * Sets up a callback function which is called as progress is made
1526 * during an operation.
1529 * Previous callback or NULL on success
1530 * INTERNET_INVALID_STATUS_CALLBACK on failure
1533 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1534 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1536 INTERNET_STATUS_CALLBACK retVal;
1537 LPWININETHANDLEHEADER lpwh;
1539 TRACE("0x%08lx\n", (ULONG)hInternet);
1541 lpwh = WININET_GetObject(hInternet);
1543 return INTERNET_INVALID_STATUS_CALLBACK;
1545 lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1546 retVal = lpwh->lpfnStatusCB;
1547 lpwh->lpfnStatusCB = lpfnIntCB;
1549 WININET_Release( lpwh );
1554 /***********************************************************************
1555 * InternetSetStatusCallbackW (WININET.@)
1557 * Sets up a callback function which is called as progress is made
1558 * during an operation.
1561 * Previous callback or NULL on success
1562 * INTERNET_INVALID_STATUS_CALLBACK on failure
1565 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1566 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1568 INTERNET_STATUS_CALLBACK retVal;
1569 LPWININETHANDLEHEADER lpwh;
1571 TRACE("0x%08lx\n", (ULONG)hInternet);
1573 lpwh = WININET_GetObject(hInternet);
1575 return INTERNET_INVALID_STATUS_CALLBACK;
1577 lpwh->dwInternalFlags |= INET_CALLBACKW;
1578 retVal = lpwh->lpfnStatusCB;
1579 lpwh->lpfnStatusCB = lpfnIntCB;
1581 WININET_Release( lpwh );
1586 /***********************************************************************
1587 * InternetSetFilePointer (WININET.@)
1589 DWORD WINAPI InternetSetFilePointer(HINTERNET f1, LONG f2, PVOID f3, DWORD f4, DWORD f5)
1595 /***********************************************************************
1596 * InternetWriteFile (WININET.@)
1598 * Write data to an open internet file
1605 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
1606 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1608 BOOL retval = FALSE;
1610 LPWININETHANDLEHEADER lpwh;
1613 lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1617 switch (lpwh->htype)
1620 FIXME("This shouldn't be here! We don't support this kind"
1621 " of connection anymore. Must use NETCON functions,"
1622 " especially if using SSL\n");
1623 nSocket = ((LPWININETHTTPREQW)lpwh)->netConnection.socketFD;
1627 nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1636 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
1637 retval = (res >= 0);
1638 *lpdwNumOfBytesWritten = retval ? res : 0;
1640 WININET_Release( lpwh );
1646 /***********************************************************************
1647 * InternetReadFile (WININET.@)
1649 * Read data from an open internet file
1656 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1657 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
1659 BOOL retval = FALSE;
1661 LPWININETHANDLEHEADER lpwh;
1663 TRACE("%p %p %ld %p\n", hFile, lpBuffer, dwNumOfBytesToRead, dwNumOfBytesRead);
1665 lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1669 /* FIXME: this should use NETCON functions! */
1670 switch (lpwh->htype)
1673 if (!NETCON_recv(&((LPWININETHTTPREQW)lpwh)->netConnection, lpBuffer,
1674 dwNumOfBytesToRead, MSG_WAITALL, (int *)dwNumOfBytesRead))
1676 *dwNumOfBytesRead = 0;
1677 retval = TRUE; /* Under windows, it seems to return 0 even if nothing was read... */
1684 /* FIXME: FTP should use NETCON_ stuff */
1685 nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1688 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, MSG_WAITALL);
1689 retval = (res >= 0);
1690 *dwNumOfBytesRead = retval ? res : 0;
1697 WININET_Release( lpwh );
1699 TRACE("-- %s (bytes read: %ld)\n", retval ? "TRUE": "FALSE", dwNumOfBytesRead ? *dwNumOfBytesRead : -1);
1703 /***********************************************************************
1704 * InternetReadFileExA (WININET.@)
1706 * Read data from an open internet file
1713 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffer,
1714 DWORD dwFlags, DWORD dwContext)
1720 /***********************************************************************
1721 * InternetReadFileExW (WININET.@)
1723 * Read data from an open internet file
1730 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1731 DWORD dwFlags, DWORD dwContext)
1735 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1739 /***********************************************************************
1740 * INET_QueryOptionHelper (internal)
1742 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1743 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1745 LPWININETHANDLEHEADER lpwhh;
1746 BOOL bSuccess = FALSE;
1748 TRACE("(%p, 0x%08lx, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1750 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
1754 case INTERNET_OPTION_HANDLE_TYPE:
1760 WARN("Invalid hInternet handle\n");
1761 SetLastError(ERROR_INVALID_HANDLE);
1765 type = lpwhh->htype;
1767 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
1769 if (*lpdwBufferLength < sizeof(ULONG))
1770 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1773 memcpy(lpBuffer, &type, sizeof(ULONG));
1774 *lpdwBufferLength = sizeof(ULONG);
1780 case INTERNET_OPTION_REQUEST_FLAGS:
1783 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
1784 if (*lpdwBufferLength < sizeof(ULONG))
1785 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1788 memcpy(lpBuffer, &flags, sizeof(ULONG));
1789 *lpdwBufferLength = sizeof(ULONG);
1795 case INTERNET_OPTION_URL:
1796 case INTERNET_OPTION_DATAFILE_NAME:
1800 WARN("Invalid hInternet handle\n");
1801 SetLastError(ERROR_INVALID_HANDLE);
1804 if (lpwhh->htype == WH_HHTTPREQ)
1806 LPWININETHTTPREQW lpreq = (LPWININETHTTPREQW) lpwhh;
1808 static const WCHAR szFmt[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1810 sprintfW(url,szFmt,lpreq->StdHeaders[HTTP_QUERY_HOST].lpszValue,lpreq->lpszPath);
1811 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1812 if (*lpdwBufferLength < strlenW(url)+1)
1813 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1818 *lpdwBufferLength=WideCharToMultiByte(CP_ACP,0,url,-1,lpBuffer,*lpdwBufferLength,NULL,NULL);
1822 strcpyW(lpBuffer, url);
1823 *lpdwBufferLength = strlenW(url)+1;
1830 case INTERNET_OPTION_HTTP_VERSION:
1833 * Presently hardcoded to 1.1
1835 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1836 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1840 case INTERNET_OPTION_CONNECTED_STATE:
1842 INTERNET_CONNECTED_INFO * pCi = (INTERNET_CONNECTED_INFO *)lpBuffer;
1843 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
1844 pCi->dwConnectedState = INTERNET_STATE_CONNECTED;
1849 case INTERNET_OPTION_SECURITY_FLAGS:
1850 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
1854 FIXME("Stub! %ld \n",dwOption);
1858 WININET_Release( lpwhh );
1863 /***********************************************************************
1864 * InternetQueryOptionW (WININET.@)
1866 * Queries an options on the specified handle
1873 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
1874 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1876 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
1879 /***********************************************************************
1880 * InternetQueryOptionA (WININET.@)
1882 * Queries an options on the specified handle
1889 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
1890 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1892 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
1896 /***********************************************************************
1897 * InternetSetOptionW (WININET.@)
1899 * Sets an options on the specified handle
1906 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
1907 LPVOID lpBuffer, DWORD dwBufferLength)
1909 LPWININETHANDLEHEADER lpwhh;
1912 TRACE("0x%08lx\n", dwOption);
1914 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
1920 case INTERNET_OPTION_HTTP_VERSION:
1922 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
1923 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%ld,%ld): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
1926 case INTERNET_OPTION_ERROR_MASK:
1928 unsigned long flags=*(unsigned long*)lpBuffer;
1929 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
1932 case INTERNET_OPTION_CODEPAGE:
1934 unsigned long codepage=*(unsigned long*)lpBuffer;
1935 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
1938 case INTERNET_OPTION_REQUEST_PRIORITY:
1940 unsigned long priority=*(unsigned long*)lpBuffer;
1941 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
1944 case INTERNET_OPTION_CONNECT_TIMEOUT:
1946 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
1947 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
1950 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
1952 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
1953 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
1956 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
1957 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
1959 case INTERNET_OPTION_END_BROWSER_SESSION:
1960 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
1962 case INTERNET_OPTION_CONNECTED_STATE:
1963 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
1966 FIXME("Option %ld STUB\n",dwOption);
1967 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1971 WININET_Release( lpwhh );
1977 /***********************************************************************
1978 * InternetSetOptionA (WININET.@)
1980 * Sets an options on the specified handle.
1987 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
1988 LPVOID lpBuffer, DWORD dwBufferLength)
1996 case INTERNET_OPTION_PROXY:
1998 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
1999 LPINTERNET_PROXY_INFOW piw;
2000 DWORD proxlen, prbylen;
2003 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2004 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2005 wlen = sizeof(*piw) + proxlen + prbylen;
2006 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2007 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2008 piw->dwAccessType = pi->dwAccessType;
2009 prox = (LPWSTR) &piw[1];
2010 prby = &prox[proxlen+1];
2011 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2012 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2013 piw->lpszProxy = prox;
2014 piw->lpszProxyBypass = prby;
2017 case INTERNET_OPTION_USER_AGENT:
2018 case INTERNET_OPTION_USERNAME:
2019 case INTERNET_OPTION_PASSWORD:
2020 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2022 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2023 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2028 wlen = dwBufferLength;
2031 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2033 if( lpBuffer != wbuffer )
2034 HeapFree( GetProcessHeap(), 0, wbuffer );
2040 /***********************************************************************
2041 * InternetSetOptionExA (WININET.@)
2043 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2044 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2046 FIXME("Flags %08lx ignored\n", dwFlags);
2047 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2050 /***********************************************************************
2051 * InternetSetOptionExW (WININET.@)
2053 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2054 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2056 FIXME("Flags %08lx ignored\n", dwFlags);
2057 if( dwFlags & ~ISO_VALID_FLAGS )
2059 SetLastError( ERROR_INVALID_PARAMETER );
2062 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2066 /***********************************************************************
2067 * InternetCheckConnectionA (WININET.@)
2069 * Pings a requested host to check internet connection
2072 * TRUE on success and FALSE on failure. If a failure then
2073 * ERROR_NOT_CONNECTED is placesd into GetLastError
2076 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2079 * this is a kludge which runs the resident ping program and reads the output.
2081 * Anyone have a better idea?
2092 * Crack or set the Address
2094 if (lpszUrl == NULL)
2097 * According to the doc we are supost to use the ip for the next
2098 * server in the WnInet internal server database. I have
2099 * no idea what that is or how to get it.
2101 * So someone needs to implement this.
2103 FIXME("Unimplemented with URL of NULL\n");
2108 URL_COMPONENTSA componets;
2110 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
2111 componets.lpszHostName = (LPSTR)&host;
2112 componets.dwHostNameLength = 1024;
2114 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
2117 TRACE("host name : %s\n",componets.lpszHostName);
2121 * Build our ping command
2123 strcpy(command,"ping -w 1 ");
2124 strcat(command,host);
2125 strcat(command," >/dev/null 2>/dev/null");
2127 TRACE("Ping command is : %s\n",command);
2129 status = system(command);
2131 TRACE("Ping returned a code of %i \n",status);
2133 /* Ping return code of 0 indicates success */
2140 SetLastError(ERROR_NOT_CONNECTED);
2146 /***********************************************************************
2147 * InternetCheckConnectionW (WININET.@)
2149 * Pings a requested host to check internet connection
2152 * TRUE on success and FALSE on failure. If a failure then
2153 * ERROR_NOT_CONNECTED is placed into GetLastError
2156 BOOL WINAPI InternetCheckConnectionW(LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2162 len = WideCharToMultiByte(CP_ACP, 0, lpszUrl, -1, NULL, 0, NULL, NULL);
2163 if (!(szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
2165 WideCharToMultiByte(CP_ACP, 0, lpszUrl, -1, szUrl, len, NULL, NULL);
2166 rc = InternetCheckConnectionA((LPCSTR)szUrl, dwFlags, dwReserved);
2167 HeapFree(GetProcessHeap(), 0, szUrl);
2173 /**********************************************************
2174 * INTERNET_InternetOpenUrlW (internal)
2179 * handle of connection or NULL on failure
2181 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2182 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2184 URL_COMPONENTSW urlComponents;
2185 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2186 WCHAR password[1024], path[2048], extra[1024];
2187 HINTERNET client = NULL, client1 = NULL;
2189 TRACE("(%p, %s, %s, %08lx, %08lx, %08lx\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2190 dwHeadersLength, dwFlags, dwContext);
2192 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2193 urlComponents.lpszScheme = protocol;
2194 urlComponents.dwSchemeLength = 32;
2195 urlComponents.lpszHostName = hostName;
2196 urlComponents.dwHostNameLength = MAXHOSTNAME;
2197 urlComponents.lpszUserName = userName;
2198 urlComponents.dwUserNameLength = 1024;
2199 urlComponents.lpszPassword = password;
2200 urlComponents.dwPasswordLength = 1024;
2201 urlComponents.lpszUrlPath = path;
2202 urlComponents.dwUrlPathLength = 2048;
2203 urlComponents.lpszExtraInfo = extra;
2204 urlComponents.dwExtraInfoLength = 1024;
2205 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2207 switch(urlComponents.nScheme) {
2208 case INTERNET_SCHEME_FTP:
2209 if(urlComponents.nPort == 0)
2210 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2211 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2212 userName, password, dwFlags, dwContext, INET_OPENURL);
2215 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2216 if(client1 == NULL) {
2217 InternetCloseHandle(client);
2222 case INTERNET_SCHEME_HTTP:
2223 case INTERNET_SCHEME_HTTPS: {
2224 static const WCHAR szStars[] = { '*','/','*', 0 };
2225 LPCWSTR accept[2] = { szStars, NULL };
2226 if(urlComponents.nPort == 0) {
2227 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2228 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2230 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2232 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2233 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2234 userName, password, dwFlags, dwContext, INET_OPENURL);
2237 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2238 if(client1 == NULL) {
2239 InternetCloseHandle(client);
2242 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2243 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0)) {
2244 InternetCloseHandle(client1);
2249 case INTERNET_SCHEME_GOPHER:
2250 /* gopher doesn't seem to be implemented in wine, but it's supposed
2251 * to be supported by InternetOpenUrlA. */
2256 TRACE(" %p <--\n", client1);
2261 /**********************************************************
2262 * InternetOpenUrlW (WININET.@)
2267 * handle of connection or NULL on failure
2269 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2270 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2272 HINTERNET ret = NULL;
2273 LPWININETAPPINFOW hIC = NULL;
2275 TRACE("(%p, %s, %s, %08lx, %08lx, %08lx\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2276 dwHeadersLength, dwFlags, dwContext);
2278 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2279 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2280 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2284 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2285 WORKREQUEST workRequest;
2286 struct WORKREQ_INTERNETOPENURLW *req;
2288 workRequest.asyncall = INTERNETOPENURLW;
2289 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2290 req = &workRequest.u.InternetOpenUrlW;
2292 req->lpszUrl = WININET_strdupW(lpszUrl);
2296 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2298 req->lpszHeaders = 0;
2299 req->dwHeadersLength = dwHeadersLength;
2300 req->dwFlags = dwFlags;
2301 req->dwContext = dwContext;
2303 INTERNET_AsyncCall(&workRequest);
2305 * This is from windows.
2307 SetLastError(ERROR_IO_PENDING);
2309 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2314 WININET_Release( &hIC->hdr );
2315 TRACE(" %p <--\n", ret);
2320 /**********************************************************
2321 * InternetOpenUrlA (WININET.@)
2326 * handle of connection or NULL on failure
2328 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2329 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2331 HINTERNET rc = (HINTERNET)NULL;
2335 LPWSTR szUrl = NULL;
2336 LPWSTR szHeaders = NULL;
2341 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2342 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2344 return (HINTERNET)NULL;
2345 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2349 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2350 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2352 if(szUrl) HeapFree(GetProcessHeap(), 0, szUrl);
2353 return (HINTERNET)NULL;
2355 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2358 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2359 lenHeaders, dwFlags, dwContext);
2361 if(szUrl) HeapFree(GetProcessHeap(), 0, szUrl);
2362 if(szHeaders) HeapFree(GetProcessHeap(), 0, szHeaders);
2368 /***********************************************************************
2369 * INTERNET_SetLastError (internal)
2371 * Set last thread specific error
2376 void INTERNET_SetLastError(DWORD dwError)
2378 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2380 SetLastError(dwError);
2382 lpwite->dwError = dwError;
2386 /***********************************************************************
2387 * INTERNET_GetLastError (internal)
2389 * Get last thread specific error
2394 DWORD INTERNET_GetLastError()
2396 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2397 return lpwite->dwError;
2401 /***********************************************************************
2402 * INTERNET_WorkerThreadFunc (internal)
2404 * Worker thread execution function
2409 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
2416 INTERNET_ExecuteWork();
2419 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
2421 if (dwWaitRes == WAIT_OBJECT_0 + 1)
2422 INTERNET_ExecuteWork();
2426 InterlockedIncrement(&dwNumIdleThreads);
2429 InterlockedDecrement(&dwNumIdleThreads);
2430 InterlockedDecrement(&dwNumThreads);
2431 TRACE("Worker thread exiting\n");
2436 /***********************************************************************
2437 * INTERNET_InsertWorkRequest (internal)
2439 * Insert work request into queue
2444 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
2446 BOOL bSuccess = FALSE;
2447 LPWORKREQUEST lpNewRequest;
2451 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
2454 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
2455 lpNewRequest->prev = NULL;
2457 EnterCriticalSection(&csQueue);
2459 lpNewRequest->next = lpWorkQueueTail;
2460 if (lpWorkQueueTail)
2461 lpWorkQueueTail->prev = lpNewRequest;
2462 lpWorkQueueTail = lpNewRequest;
2463 if (!lpHeadWorkQueue)
2464 lpHeadWorkQueue = lpWorkQueueTail;
2466 LeaveCriticalSection(&csQueue);
2469 InterlockedIncrement(&dwNumJobs);
2476 /***********************************************************************
2477 * INTERNET_GetWorkRequest (internal)
2479 * Retrieves work request from queue
2484 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
2486 BOOL bSuccess = FALSE;
2487 LPWORKREQUEST lpRequest = NULL;
2491 EnterCriticalSection(&csQueue);
2493 if (lpHeadWorkQueue)
2495 lpRequest = lpHeadWorkQueue;
2496 lpHeadWorkQueue = lpHeadWorkQueue->prev;
2497 if (lpRequest == lpWorkQueueTail)
2498 lpWorkQueueTail = lpHeadWorkQueue;
2501 LeaveCriticalSection(&csQueue);
2505 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
2506 HeapFree(GetProcessHeap(), 0, lpRequest);
2508 InterlockedDecrement(&dwNumJobs);
2515 /***********************************************************************
2516 * INTERNET_AsyncCall (internal)
2518 * Retrieves work request from queue
2523 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
2527 BOOL bSuccess = FALSE;
2531 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
2533 InterlockedIncrement(&dwNumIdleThreads);
2535 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
2536 !(hThread = CreateThread(NULL, 0,
2537 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
2539 InterlockedDecrement(&dwNumThreads);
2540 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
2544 TRACE("Created new thread\n");
2548 INTERNET_InsertWorkRequest(lpWorkRequest);
2549 SetEvent(hWorkEvent);
2557 /***********************************************************************
2558 * INTERNET_ExecuteWork (internal)
2563 static VOID INTERNET_ExecuteWork()
2565 WORKREQUEST workRequest;
2569 if (!INTERNET_GetWorkRequest(&workRequest))
2572 switch (workRequest.asyncall)
2576 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
2577 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2579 TRACE("FTPPUTFILEW %p\n", lpwfs);
2581 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
2582 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
2584 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
2585 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
2589 case FTPSETCURRENTDIRECTORYW:
2591 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
2592 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2594 TRACE("FTPSETCURRENTDIRECTORYW %p\n", lpwfs);
2596 req = &workRequest.u.FtpSetCurrentDirectoryW;
2597 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
2598 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
2602 case FTPCREATEDIRECTORYW:
2604 struct WORKREQ_FTPCREATEDIRECTORYW *req;
2605 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2607 TRACE("FTPCREATEDIRECTORYW %p\n", lpwfs);
2609 req = &workRequest.u.FtpCreateDirectoryW;
2610 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
2611 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
2615 case FTPFINDFIRSTFILEW:
2617 struct WORKREQ_FTPFINDFIRSTFILEW *req;
2618 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2620 TRACE("FTPFINDFIRSTFILEW %p\n", lpwfs);
2622 req = &workRequest.u.FtpFindFirstFileW;
2623 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
2624 req->lpFindFileData, req->dwFlags, req->dwContext);
2625 if (req->lpszSearchFile != NULL)
2626 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
2630 case FTPGETCURRENTDIRECTORYW:
2632 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
2633 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2635 TRACE("FTPGETCURRENTDIRECTORYW %p\n", lpwfs);
2637 req = &workRequest.u.FtpGetCurrentDirectoryW;
2638 FTP_FtpGetCurrentDirectoryW(lpwfs,
2639 req->lpszDirectory, req->lpdwDirectory);
2645 struct WORKREQ_FTPOPENFILEW *req = &workRequest.u.FtpOpenFileW;
2646 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2648 TRACE("FTPOPENFILEW %p\n", lpwfs);
2650 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
2651 req->dwAccess, req->dwFlags, req->dwContext);
2652 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
2658 struct WORKREQ_FTPGETFILEW *req = &workRequest.u.FtpGetFileW;
2659 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2661 TRACE("FTPGETFILEW %p\n", lpwfs);
2663 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
2664 req->lpszNewFile, req->fFailIfExists,
2665 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
2666 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
2667 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
2671 case FTPDELETEFILEW:
2673 struct WORKREQ_FTPDELETEFILEW *req = &workRequest.u.FtpDeleteFileW;
2674 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2676 TRACE("FTPDELETEFILEW %p\n", lpwfs);
2678 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
2679 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
2683 case FTPREMOVEDIRECTORYW:
2685 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
2686 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2688 TRACE("FTPREMOVEDIRECTORYW %p\n", lpwfs);
2690 req = &workRequest.u.FtpRemoveDirectoryW;
2691 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
2692 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
2696 case FTPRENAMEFILEW:
2698 struct WORKREQ_FTPRENAMEFILEW *req = &workRequest.u.FtpRenameFileW;
2699 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
2701 TRACE("FTPRENAMEFILEW %p\n", lpwfs);
2703 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
2704 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
2705 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
2709 case INTERNETFINDNEXTW:
2711 struct WORKREQ_INTERNETFINDNEXTW *req;
2712 LPWININETFINDNEXTW lpwh = (LPWININETFINDNEXTW) workRequest.hdr;
2714 TRACE("INTERNETFINDNEXTW %p\n", lpwh);
2716 req = &workRequest.u.InternetFindNextW;
2717 INTERNET_FindNextFileW(lpwh, req->lpFindFileData);
2721 case HTTPSENDREQUESTW:
2723 struct WORKREQ_HTTPSENDREQUESTW *req = &workRequest.u.HttpSendRequestW;
2724 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest.hdr;
2726 TRACE("HTTPSENDREQUESTW %p\n", lpwhr);
2728 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
2729 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength);
2731 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
2735 case HTTPOPENREQUESTW:
2737 struct WORKREQ_HTTPOPENREQUESTW *req = &workRequest.u.HttpOpenRequestW;
2738 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) workRequest.hdr;
2740 TRACE("HTTPOPENREQUESTW %p\n", lpwhs);
2742 HTTP_HttpOpenRequestW(lpwhs, req->lpszVerb,
2743 req->lpszObjectName, req->lpszVersion, req->lpszReferrer,
2744 req->lpszAcceptTypes, req->dwFlags, req->dwContext);
2746 HeapFree(GetProcessHeap(), 0, req->lpszVerb);
2747 HeapFree(GetProcessHeap(), 0, req->lpszObjectName);
2748 HeapFree(GetProcessHeap(), 0, req->lpszVersion);
2749 HeapFree(GetProcessHeap(), 0, req->lpszReferrer);
2755 struct WORKREQ_SENDCALLBACK *req = &workRequest.u.SendCallback;
2757 TRACE("SENDCALLBACK %p\n", workRequest.hdr);
2759 SendSyncCallback(workRequest.hdr,
2760 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
2761 req->dwStatusInfoLength);
2765 case INTERNETOPENURLW:
2767 struct WORKREQ_INTERNETOPENURLW *req = &workRequest.u.InternetOpenUrlW;
2768 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest.hdr;
2770 TRACE("INTERNETOPENURLW %p\n", hIC);
2772 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2773 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2774 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2775 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2779 WININET_Release( workRequest.hdr );
2783 /***********************************************************************
2784 * INTERNET_GetResponseBuffer
2789 LPSTR INTERNET_GetResponseBuffer()
2791 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2793 return lpwite->response;
2796 /***********************************************************************
2797 * INTERNET_GetNextLine (internal)
2799 * Parse next line in directory string listing
2802 * Pointer to beginning of next line
2807 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
2811 BOOL bSuccess = FALSE;
2813 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
2818 FD_SET(nSocket, &infd);
2819 tv.tv_sec=RESPONSE_TIMEOUT;
2822 while (nRecv < MAX_REPLY_LEN)
2824 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
2826 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
2828 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2832 if (lpszBuffer[nRecv] == '\n')
2837 if (lpszBuffer[nRecv] != '\r')
2842 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
2850 lpszBuffer[nRecv] = '\0';
2852 TRACE(":%d %s\n", nRecv, lpszBuffer);
2861 /***********************************************************************
2864 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
2865 LPDWORD lpdwNumberOfBytesAvailble,
2866 DWORD dwFlags, DWORD dwConext)
2868 LPWININETHTTPREQW lpwhr;
2872 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hFile );
2875 SetLastError(ERROR_NO_MORE_FILES);
2879 TRACE("--> %p %i\n",lpwhr,lpwhr->hdr.htype);
2881 switch (lpwhr->hdr.htype)
2884 if (!NETCON_recv(&lpwhr->netConnection, buffer,
2885 4048, MSG_PEEK, (int *)lpdwNumberOfBytesAvailble))
2887 SetLastError(ERROR_NO_MORE_FILES);
2895 FIXME("unsupported file type\n");
2898 WININET_Release( &lpwhr->hdr );
2900 TRACE("<-- %i\n",retval);
2905 /***********************************************************************
2908 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
2915 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
2922 /***********************************************************************
2925 * On windows this function is supposed to dial the default internet
2926 * connection. We don't want to have Wine dial out to the internet so
2927 * we return TRUE by default. It might be nice to check if we are connected.
2934 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
2938 /* Tell that we are connected to the internet. */
2942 /***********************************************************************
2943 * InternetAutodialHangup
2945 * Hangs up an connection made with InternetAutodial
2954 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
2958 /* we didn't dial, we don't disconnect */
2962 /***********************************************************************
2964 * InternetCombineUrlA
2966 * Combine a base URL with a relative URL
2974 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
2975 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
2980 TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
2982 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2983 dwFlags ^= ICU_NO_ENCODE;
2984 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
2989 /***********************************************************************
2991 * InternetCombineUrlW
2993 * Combine a base URL with a relative URL
3001 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3002 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3007 TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3009 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3010 dwFlags ^= ICU_NO_ENCODE;
3011 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3016 /***********************************************************************
3018 * InternetCreateUrlA
3025 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3026 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3032 /***********************************************************************
3034 * InternetCreateUrlW
3041 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3042 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)