4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define MAXHOSTNAME 100 /* from http.c */
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
52 #include "wine/debug.h"
54 #define NO_SHLWAPI_STREAM
57 #include "wine/exception.h"
62 #include "wine/unicode.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
66 #define MAX_IDLE_WORKER 1000*60*1
67 #define MAX_WORKER_THREADS 10
68 #define RESPONSE_TIMEOUT 30
70 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
71 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
73 /* filter for page-fault exceptions */
74 static WINE_EXCEPTION_FILTER(page_fault)
76 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
77 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
78 return EXCEPTION_EXECUTE_HANDLER;
79 return EXCEPTION_CONTINUE_SEARCH;
85 CHAR response[MAX_REPLY_LEN];
86 } WITHREADERROR, *LPWITHREADERROR;
88 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
89 VOID INTERNET_ExecuteWork();
91 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
93 DWORD dwNumIdleThreads;
95 HANDLE hEventArray[2];
96 #define hQuitEvent hEventArray[0]
97 #define hWorkEvent hEventArray[1]
98 CRITICAL_SECTION csQueue;
99 LPWORKREQUEST lpHeadWorkQueue;
100 LPWORKREQUEST lpWorkQueueTail;
102 /***********************************************************************
103 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
106 * hinstDLL [I] handle to the DLL's instance
108 * lpvReserved [I] reserved, must be NULL
115 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
117 TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
120 case DLL_PROCESS_ATTACH:
122 g_dwTlsErrIndex = TlsAlloc();
124 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
127 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
128 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
129 InitializeCriticalSection(&csQueue);
132 dwNumIdleThreads = 0;
135 case DLL_THREAD_ATTACH:
137 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
141 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
145 case DLL_THREAD_DETACH:
146 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
148 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
150 HeapFree(GetProcessHeap(), 0, lpwite);
154 case DLL_PROCESS_DETACH:
156 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
158 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
159 TlsFree(g_dwTlsErrIndex);
162 SetEvent(hQuitEvent);
164 CloseHandle(hQuitEvent);
165 CloseHandle(hWorkEvent);
166 DeleteCriticalSection(&csQueue);
174 /***********************************************************************
175 * InternetInitializeAutoProxyDll (WININET.@)
177 * Setup the internal proxy
186 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
189 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
193 /***********************************************************************
194 * DetectAutoProxyUrl (WININET.@)
196 * Auto detect the proxy url
202 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
203 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
206 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
211 /***********************************************************************
212 * InternetOpenA (WININET.@)
214 * Per-application initialization of wininet
217 * HINTERNET on success
221 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
222 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
224 LPWININETAPPINFOA lpwai = NULL;
226 TRACE("(%s, %li, %s, %s, %li)\n", debugstr_a(lpszAgent), dwAccessType,
227 debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
229 /* Clear any error information */
230 INTERNET_SetLastError(0);
232 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
234 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
237 memset(lpwai, 0, sizeof(WININETAPPINFOA));
238 lpwai->hdr.htype = WH_HINIT;
239 lpwai->hdr.lpwhparent = NULL;
240 lpwai->hdr.dwFlags = dwFlags;
241 if (NULL != lpszAgent)
243 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
244 strcpy( lpwai->lpszAgent, lpszAgent );
246 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
249 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", &key))
251 DWORD keytype, len, enabled;
252 RegQueryValueExA(key, "ProxyEnable", NULL, NULL, (BYTE*)&enabled, NULL);
255 if(!RegQueryValueExA(key, "ProxyServer", NULL, &keytype, NULL, &len) && len && keytype == REG_SZ)
257 lpwai->lpszProxy=HeapAlloc( GetProcessHeap(), 0, len+1 );
258 RegQueryValueExA(key, "ProxyServer", NULL, &keytype, (BYTE*)lpwai->lpszProxy, &len);
259 TRACE("Proxy = %s\n", lpwai->lpszProxy);
260 dwAccessType = INTERNET_OPEN_TYPE_PROXY;
265 TRACE("Proxy is not enabled.\n");
270 else if (NULL != lpszProxy)
272 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
273 strcpy( lpwai->lpszProxy, lpszProxy );
275 if (NULL != lpszProxyBypass)
277 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
278 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
280 lpwai->dwAccessType = dwAccessType;
283 TRACE("returning %p\n", (HINTERNET)lpwai);
284 return (HINTERNET)lpwai;
288 /***********************************************************************
289 * InternetOpenW (WININET.@)
291 * Per-application initialization of wininet
294 * HINTERNET on success
298 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
299 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
301 HINTERNET rc = (HINTERNET)NULL;
302 INT lenAgent = lstrlenW(lpszAgent)+1;
303 INT lenProxy = lstrlenW(lpszProxy)+1;
304 INT lenBypass = lstrlenW(lpszProxyBypass)+1;
305 CHAR *szAgent = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenAgent*sizeof(CHAR));
306 CHAR *szProxy = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenProxy*sizeof(CHAR));
307 CHAR *szBypass = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenBypass*sizeof(CHAR));
309 if (!szAgent || !szProxy || !szBypass)
317 return (HINTERNET)NULL;
320 WideCharToMultiByte(CP_ACP, -1, lpszAgent, -1, szAgent, lenAgent,
322 WideCharToMultiByte(CP_ACP, -1, lpszProxy, -1, szProxy, lenProxy,
324 WideCharToMultiByte(CP_ACP, -1, lpszProxyBypass, -1, szBypass, lenBypass,
327 rc = InternetOpenA(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
329 HeapFree(GetProcessHeap(), 0, szAgent);
330 HeapFree(GetProcessHeap(), 0, szProxy);
331 HeapFree(GetProcessHeap(), 0, szBypass);
336 /***********************************************************************
337 * InternetGetLastResponseInfoA (WININET.@)
339 * Return last wininet error description on the calling thread
342 * TRUE on success of writting to buffer
346 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
347 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
349 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
353 *lpdwError = lpwite->dwError;
356 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
357 *lpdwBufferLength = strlen(lpszBuffer);
360 *lpdwBufferLength = 0;
366 /***********************************************************************
367 * InternetGetConnectedState (WININET.@)
369 * Return connected state
373 * if lpdwStatus is not null, return the status (off line,
374 * modem, lan...) in it.
375 * FALSE if not connected
377 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
380 FIXME("always returning LAN connection.\n");
381 *lpdwStatus = INTERNET_CONNECTION_LAN;
386 /***********************************************************************
387 * InternetGetConnectedStateEx (WININET.@)
389 * Return connected state
393 * if lpdwStatus is not null, return the status (off line,
394 * modem, lan...) in it.
395 * FALSE if not connected
397 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
398 DWORD dwNameLen, DWORD dwReserved)
405 FIXME("always returning LAN connection.\n");
406 *lpdwStatus = INTERNET_CONNECTION_LAN;
411 /***********************************************************************
412 * InternetConnectA (WININET.@)
414 * Open a ftp, gopher or http session
417 * HINTERNET a session handle on success
421 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
422 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
423 LPCSTR lpszUserName, LPCSTR lpszPassword,
424 DWORD dwService, DWORD dwFlags, DWORD dwContext)
426 HINTERNET rc = (HINTERNET) NULL;
428 TRACE("(%p, %s, %i, %s, %s, %li, %li, %li)\n", hInternet, debugstr_a(lpszServerName),
429 nServerPort, debugstr_a(lpszUserName), debugstr_a(lpszPassword),
430 dwService, dwFlags, dwContext);
432 /* Clear any error information */
433 INTERNET_SetLastError(0);
437 case INTERNET_SERVICE_FTP:
438 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
439 lpszUserName, lpszPassword, dwFlags, dwContext);
442 case INTERNET_SERVICE_HTTP:
443 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
444 lpszUserName, lpszPassword, dwFlags, dwContext);
447 case INTERNET_SERVICE_GOPHER:
452 TRACE("returning %p\n", rc);
457 /***********************************************************************
458 * InternetConnectW (WININET.@)
460 * Open a ftp, gopher or http session
463 * HINTERNET a session handle on success
467 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
468 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
469 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
470 DWORD dwService, DWORD dwFlags, DWORD dwContext)
472 HINTERNET rc = (HINTERNET)NULL;
476 CHAR *szServerName = NULL;
477 CHAR *szUserName = NULL;
478 CHAR *szPassword = NULL;
482 lenServer = lstrlenW(lpszServerName)+1;
483 szServerName = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenServer*sizeof(CHAR));
484 WideCharToMultiByte(CP_ACP, -1, lpszServerName, -1, szServerName, lenServer,
489 lenUser = lstrlenW(lpszUserName)+1;
490 szUserName = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenUser*sizeof(CHAR));
491 WideCharToMultiByte(CP_ACP, -1, lpszUserName, -1, szUserName, lenUser,
496 lenPass = lstrlenW(lpszPassword)+1;
497 szPassword = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenPass*sizeof(CHAR));
498 WideCharToMultiByte(CP_ACP, -1, lpszPassword, -1, szPassword, lenPass,
503 rc = InternetConnectA(hInternet, szServerName, nServerPort,
504 szUserName, szPassword, dwService, dwFlags, dwContext);
506 if (szServerName) HeapFree(GetProcessHeap(), 0, szServerName);
507 if (szUserName) HeapFree(GetProcessHeap(), 0, szUserName);
508 if (szPassword) HeapFree(GetProcessHeap(), 0, szPassword);
513 /***********************************************************************
514 * InternetFindNextFileA (WININET.@)
516 * Continues a file search from a previous call to FindFirstFile
523 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
525 LPWININETAPPINFOA hIC = NULL;
526 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
530 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
532 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
536 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
537 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
539 WORKREQUEST workRequest;
541 workRequest.asyncall = INTERNETFINDNEXTA;
542 workRequest.HFTPSESSION = (DWORD)hFind;
543 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
545 return INTERNET_AsyncCall(&workRequest);
549 return INTERNET_FindNextFileA(hFind, lpvFindData);
553 /***********************************************************************
554 * INTERNET_FindNextFileA (Internal)
556 * Continues a file search from a previous call to FindFirstFile
563 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
565 BOOL bSuccess = TRUE;
566 LPWININETAPPINFOA hIC = NULL;
567 LPWIN32_FIND_DATAA lpFindFileData;
568 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
572 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
574 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
578 /* Clear any error information */
579 INTERNET_SetLastError(0);
581 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
583 FIXME("Only FTP find next supported\n");
584 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
588 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
590 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
591 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
593 if (lpwh->index >= lpwh->size)
595 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
600 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
603 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
607 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
608 if (hIC->lpfnStatusCB)
610 INTERNET_ASYNC_RESULT iar;
612 iar.dwResult = (DWORD)bSuccess;
613 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
614 INTERNET_GetLastError();
616 SendAsyncCallback(hIC, hFind, lpwh->hdr.dwContext,
617 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
618 sizeof(INTERNET_ASYNC_RESULT));
625 /***********************************************************************
626 * INTERNET_CloseHandle (internal)
628 * Close internet handle
634 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
638 SendAsyncCallback(lpwai, lpwai, lpwai->hdr.dwContext,
639 INTERNET_STATUS_HANDLE_CLOSING, lpwai,
642 if (lpwai->lpszAgent)
643 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
645 if (lpwai->lpszProxy)
646 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
648 if (lpwai->lpszProxyBypass)
649 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
651 HeapFree(GetProcessHeap(), 0, lpwai);
655 /***********************************************************************
656 * InternetCloseHandle (WININET.@)
658 * Generic close handle function
665 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
668 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
670 TRACE("%p\n",hInternet);
675 /* Clear any error information */
676 INTERNET_SetLastError(0);
682 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
686 case WH_HHTTPSESSION:
687 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
692 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
697 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
701 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
705 retval = FTP_CloseFileTransferHandle((LPWININETFILE) lpwh);
711 } __EXCEPT(page_fault) {
712 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
721 /***********************************************************************
722 * ConvertUrlComponentValue (Internal)
724 * Helper function for InternetCrackUrlW
727 void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
728 LPWSTR lpwszComponent, DWORD dwwComponentLen,
732 if (*dwComponentLen != 0)
734 int nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
735 if (*lppszComponent == NULL)
737 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
738 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
739 *dwComponentLen = nASCIILength;
743 INT ncpylen = min((*dwComponentLen)-1, nASCIILength);
744 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
745 (*lppszComponent)[ncpylen]=0;
746 *dwComponentLen = ncpylen;
752 /***********************************************************************
753 * InternetCrackUrlA (WININET.@)
755 * Break up URL into its components
757 * TODO: Handle dwFlags
764 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
765 LPURL_COMPONENTSA lpUrlComponents)
771 dwUrlLength=strlen(lpszUrl);
772 lpwszUrl=HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(dwUrlLength+1));
773 memset(lpwszUrl,0,sizeof(WCHAR)*(dwUrlLength+1));
774 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,dwUrlLength+1);
775 memset(&UCW,0,sizeof(UCW));
776 if(lpUrlComponents->dwHostNameLength!=0)
777 UCW.dwHostNameLength=1;
778 if(lpUrlComponents->dwUserNameLength!=0)
779 UCW.dwUserNameLength=1;
780 if(lpUrlComponents->dwPasswordLength!=0)
781 UCW.dwPasswordLength=1;
782 if(lpUrlComponents->dwUrlPathLength!=0)
783 UCW.dwUrlPathLength=1;
784 if(lpUrlComponents->dwSchemeLength!=0)
785 UCW.dwSchemeLength=1;
786 if(lpUrlComponents->dwExtraInfoLength!=0)
787 UCW.dwExtraInfoLength=1;
788 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
790 HeapFree(GetProcessHeap(), 0, lpwszUrl);
793 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
794 UCW.lpszHostName, UCW.dwHostNameLength,
796 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
797 UCW.lpszUserName, UCW.dwUserNameLength,
799 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
800 UCW.lpszPassword, UCW.dwPasswordLength,
802 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
803 UCW.lpszUrlPath, UCW.dwUrlPathLength,
805 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
806 UCW.lpszScheme, UCW.dwSchemeLength,
808 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
809 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
811 lpUrlComponents->nScheme=UCW.nScheme;
812 lpUrlComponents->nPort=UCW.nPort;
813 HeapFree(GetProcessHeap(), 0, lpwszUrl);
815 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
816 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
817 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
818 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
819 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
824 /***********************************************************************
825 * GetInternetSchemeW (internal)
831 * INTERNET_SCHEME_UNKNOWN on failure
834 INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, INT nMaxCmp)
836 INTERNET_SCHEME iScheme=INTERNET_SCHEME_UNKNOWN;
837 WCHAR lpszFtp[]={'f','t','p',0};
838 WCHAR lpszGopher[]={'g','o','p','h','e','r',0};
839 WCHAR lpszHttp[]={'h','t','t','p',0};
840 WCHAR lpszHttps[]={'h','t','t','p','s',0};
841 WCHAR lpszFile[]={'f','i','l','e',0};
842 WCHAR lpszNews[]={'n','e','w','s',0};
843 WCHAR lpszMailto[]={'m','a','i','l','t','o',0};
844 WCHAR lpszRes[]={'r','e','s',0};
845 WCHAR* tempBuffer=NULL;
848 return INTERNET_SCHEME_UNKNOWN;
850 tempBuffer=malloc(nMaxCmp+1);
851 strncpyW(tempBuffer,lpszScheme,nMaxCmp);
852 tempBuffer[nMaxCmp]=0;
854 if (nMaxCmp==strlenW(lpszFtp) && !strncmpW(lpszFtp, tempBuffer, nMaxCmp))
855 iScheme=INTERNET_SCHEME_FTP;
856 else if (nMaxCmp==strlenW(lpszGopher) && !strncmpW(lpszGopher, tempBuffer, nMaxCmp))
857 iScheme=INTERNET_SCHEME_GOPHER;
858 else if (nMaxCmp==strlenW(lpszHttp) && !strncmpW(lpszHttp, tempBuffer, nMaxCmp))
859 iScheme=INTERNET_SCHEME_HTTP;
860 else if (nMaxCmp==strlenW(lpszHttps) && !strncmpW(lpszHttps, tempBuffer, nMaxCmp))
861 iScheme=INTERNET_SCHEME_HTTPS;
862 else if (nMaxCmp==strlenW(lpszFile) && !strncmpW(lpszFile, tempBuffer, nMaxCmp))
863 iScheme=INTERNET_SCHEME_FILE;
864 else if (nMaxCmp==strlenW(lpszNews) && !strncmpW(lpszNews, tempBuffer, nMaxCmp))
865 iScheme=INTERNET_SCHEME_NEWS;
866 else if (nMaxCmp==strlenW(lpszMailto) && !strncmpW(lpszMailto, tempBuffer, nMaxCmp))
867 iScheme=INTERNET_SCHEME_MAILTO;
868 else if (nMaxCmp==strlenW(lpszRes) && !strncmpW(lpszRes, tempBuffer, nMaxCmp))
869 iScheme=INTERNET_SCHEME_RES;
874 /***********************************************************************
875 * SetUrlComponentValueW (Internal)
877 * Helper function for InternetCrackUrlW
884 BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, INT len)
886 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
888 if (*dwComponentLen != 0 || *lppszComponent == NULL)
890 if (*lppszComponent == NULL)
892 *lppszComponent = (LPWSTR)lpszStart;
893 *dwComponentLen = len;
897 INT ncpylen = min((*dwComponentLen)-1, len);
898 strncpyW(*lppszComponent, lpszStart, ncpylen);
899 (*lppszComponent)[ncpylen] = '\0';
900 *dwComponentLen = ncpylen;
907 /***********************************************************************
908 * InternetCrackUrlW (WININET.@)
910 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
911 LPURL_COMPONENTSW lpUC)
915 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
918 LPWSTR lpszParam = NULL;
919 BOOL bIsAbsolute = FALSE;
920 LPWSTR lpszap = (WCHAR*)lpszUrl;
921 LPWSTR lpszcp = NULL;
922 WCHAR lpszSeparators[3]={';','?',0};
923 WCHAR lpszSlash[2]={'/',0};
925 dwUrlLength=strlenW(lpszUrl);
929 /* Determine if the URI is absolute. */
930 while (*lpszap != '\0')
932 if (isalnumW(*lpszap))
937 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
944 lpszcp = (LPWSTR)lpszUrl; /* Relative url */
951 lpszParam = strpbrkW(lpszap, lpszSeparators);
952 if (lpszParam != NULL)
954 if (!SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
955 lpszParam, dwUrlLength-(lpszParam-lpszUrl)))
961 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
964 WCHAR wszAbout[]={'a','b','o','u','t',':',0};
966 /* Get scheme first. */
967 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
968 if (!SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
969 lpszUrl, lpszcp - lpszUrl))
972 /* Eat ':' in protocol. */
975 /* if the scheme is "about", there is no host */
976 if(strncmpW(wszAbout,lpszUrl, lpszcp - lpszUrl)==0)
978 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
979 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
980 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
985 /* Skip over slashes. */
997 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1001 lpszNetLoc = min(lpszNetLoc, lpszParam);
1003 lpszNetLoc = lpszParam;
1005 else if (!lpszNetLoc)
1006 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1014 /* [<user>[<:password>]@]<host>[:<port>] */
1015 /* First find the user and password if they exist */
1017 lpszHost = strchrW(lpszcp, '@');
1018 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1020 /* username and password not specified. */
1021 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1022 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1024 else /* Parse out username and password */
1026 LPWSTR lpszUser = lpszcp;
1027 LPWSTR lpszPasswd = lpszHost;
1029 while (lpszcp < lpszHost)
1032 lpszPasswd = lpszcp;
1037 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1038 lpszUser, lpszPasswd - lpszUser);
1040 if (lpszPasswd != lpszHost)
1042 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1043 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1044 lpszHost - lpszPasswd);
1046 lpszcp++; /* Advance to beginning of host */
1049 /* Parse <host><:port> */
1052 lpszPort = lpszNetLoc;
1054 /* special case for res:// URLs: there is no port here, so the host is the
1055 entire string up to the first '/' */
1056 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1058 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1059 lpszHost, lpszPort - lpszHost);
1065 while (lpszcp < lpszNetLoc)
1073 /* If the scheme is "file" and the host is just one letter, it's not a host */
1074 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1077 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1083 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1084 lpszHost, lpszPort - lpszHost);
1085 if (lpszPort != lpszNetLoc)
1086 lpUC->nPort = atoiW(++lpszPort);
1095 /* Here lpszcp points to:
1097 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1098 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1100 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1104 /* Only truncate the parameter list if it's already been saved
1105 * in lpUC->lpszExtraInfo.
1107 if (lpszParam && lpUC->dwExtraInfoLength)
1108 len = lpszParam - lpszcp;
1111 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1112 * newlines if necessary.
1114 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1115 if (lpsznewline != NULL)
1116 len = lpsznewline - lpszcp;
1118 len = dwUrlLength-(lpszcp-lpszUrl);
1121 if (!SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1127 lpUC->dwUrlPathLength = 0;
1130 TRACE("%s: host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1131 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1132 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1133 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1138 /***********************************************************************
1139 * InternetAttemptConnect (WININET.@)
1141 * Attempt to make a connection to the internet
1144 * ERROR_SUCCESS on success
1145 * Error value on failure
1148 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1151 return ERROR_SUCCESS;
1155 /***********************************************************************
1156 * InternetCanonicalizeUrlA (WININET.@)
1158 * Escape unsafe characters and spaces
1165 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1166 LPDWORD lpdwBufferLength, DWORD dwFlags)
1169 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
1170 lpdwBufferLength, dwFlags);
1172 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1173 dwFlags ^= ICU_NO_ENCODE;
1175 dwFlags |= 0x80000000; /* Don't know what this means */
1177 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1179 return (hr == S_OK) ? TRUE : FALSE;
1182 /***********************************************************************
1183 * InternetCanonicalizeUrlW (WININET.@)
1185 * Escape unsafe characters and spaces
1192 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1193 LPDWORD lpdwBufferLength, DWORD dwFlags)
1196 TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
1197 lpdwBufferLength, dwFlags);
1199 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1200 dwFlags ^= ICU_NO_ENCODE;
1202 dwFlags |= 0x80000000; /* Don't know what this means */
1204 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1206 return (hr == S_OK) ? TRUE : FALSE;
1210 /***********************************************************************
1211 * InternetSetStatusCallbackA (WININET.@)
1213 * Sets up a callback function which is called as progress is made
1214 * during an operation.
1217 * Previous callback or NULL on success
1218 * INTERNET_INVALID_STATUS_CALLBACK on failure
1221 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1222 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1224 INTERNET_STATUS_CALLBACK retVal;
1225 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
1227 TRACE("0x%08lx\n", (ULONG)hInternet);
1228 if (lpwai->hdr.htype != WH_HINIT)
1229 return INTERNET_INVALID_STATUS_CALLBACK;
1231 retVal = lpwai->lpfnStatusCB;
1232 lpwai->lpfnStatusCB = lpfnIntCB;
1238 /***********************************************************************
1239 * InternetWriteFile (WININET.@)
1241 * Write data to an open internet file
1248 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
1249 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1251 BOOL retval = FALSE;
1253 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
1259 switch (lpwh->htype)
1262 FIXME("This shouldn't be here! We don't support this kind"
1263 " of connection anymore. Must use NETCON functions,"
1264 " especially if using SSL\n");
1265 nSocket = ((LPWININETHTTPREQA)hFile)->netConnection.socketFD;
1269 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
1278 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
1279 retval = (res >= 0);
1280 *lpdwNumOfBytesWritten = retval ? res : 0;
1287 /***********************************************************************
1288 * InternetReadFile (WININET.@)
1290 * Read data from an open internet file
1297 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1298 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
1300 BOOL retval = FALSE;
1302 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
1309 /* FIXME: this should use NETCON functions! */
1310 switch (lpwh->htype)
1313 if (!NETCON_recv(&((LPWININETHTTPREQA)hFile)->netConnection, lpBuffer,
1314 dwNumOfBytesToRead, 0, (int *)dwNumOfBytesRead))
1316 *dwNumOfBytesRead = 0;
1317 retval = TRUE; /* Under windows, it seems to return 0 even if nothing was read... */
1324 /* FIXME: FTP should use NETCON_ stuff */
1325 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
1328 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, 0);
1329 retval = (res >= 0);
1330 *dwNumOfBytesRead = retval ? res : 0;
1341 /***********************************************************************
1342 * InternetReadFileExA (WININET.@)
1344 * Read data from an open internet file
1351 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffer,
1352 DWORD dwFlags, DWORD dwContext)
1358 /***********************************************************************
1359 * InternetReadFileExW (WININET.@)
1361 * Read data from an open internet file
1368 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1369 DWORD dwFlags, DWORD dwContext)
1373 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1377 /***********************************************************************
1378 * INET_QueryOptionHelper (internal)
1380 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1381 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1383 LPWININETHANDLEHEADER lpwhh;
1384 BOOL bSuccess = FALSE;
1386 TRACE("(%p, 0x%08lx, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1388 if (NULL == hInternet)
1390 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1394 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1398 case INTERNET_OPTION_HANDLE_TYPE:
1400 ULONG type = lpwhh->htype;
1401 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
1403 if (*lpdwBufferLength < sizeof(ULONG))
1404 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1407 memcpy(lpBuffer, &type, sizeof(ULONG));
1408 *lpdwBufferLength = sizeof(ULONG);
1414 case INTERNET_OPTION_REQUEST_FLAGS:
1417 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
1418 if (*lpdwBufferLength < sizeof(ULONG))
1419 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1422 memcpy(lpBuffer, &flags, sizeof(ULONG));
1423 *lpdwBufferLength = sizeof(ULONG);
1429 case INTERNET_OPTION_URL:
1430 case INTERNET_OPTION_DATAFILE_NAME:
1432 ULONG type = lpwhh->htype;
1433 if (type == WH_HHTTPREQ)
1435 LPWININETHTTPREQA lpreq = hInternet;
1438 sprintf(url,"http://%s%s",lpreq->lpszHostName,lpreq->lpszPath);
1439 TRACE("INTERNET_OPTION_URL: %s\n",url);
1440 if (*lpdwBufferLength < strlen(url)+1)
1441 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1446 *lpdwBufferLength=MultiByteToWideChar(CP_ACP,0,url,-1,lpBuffer,*lpdwBufferLength);
1450 memcpy(lpBuffer, url, strlen(url)+1);
1451 *lpdwBufferLength = strlen(url)+1;
1458 case INTERNET_OPTION_HTTP_VERSION:
1461 * Presently hardcoded to 1.1
1463 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1464 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1470 FIXME("Stub! %ld \n",dwOption);
1477 /***********************************************************************
1478 * InternetQueryOptionW (WININET.@)
1480 * Queries an options on the specified handle
1487 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
1488 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1490 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
1493 /***********************************************************************
1494 * InternetQueryOptionA (WININET.@)
1496 * Queries an options on the specified handle
1503 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
1504 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1506 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
1510 /***********************************************************************
1511 * InternetSetOptionW (WININET.@)
1513 * Sets an options on the specified handle
1520 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
1521 LPVOID lpBuffer, DWORD dwBufferLength)
1523 LPWININETHANDLEHEADER lpwhh;
1525 TRACE("0x%08lx\n", dwOption);
1527 if (NULL == hInternet)
1529 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1533 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1537 case INTERNET_OPTION_HTTP_VERSION:
1539 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
1540 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%ld,%ld): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
1543 case INTERNET_OPTION_ERROR_MASK:
1545 unsigned long flags=*(unsigned long*)lpBuffer;
1546 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
1549 case INTERNET_OPTION_CODEPAGE:
1551 unsigned long codepage=*(unsigned long*)lpBuffer;
1552 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
1555 case INTERNET_OPTION_REQUEST_PRIORITY:
1557 unsigned long priority=*(unsigned long*)lpBuffer;
1558 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
1562 FIXME("Option %ld STUB\n",dwOption);
1563 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1571 /***********************************************************************
1572 * InternetSetOptionA (WININET.@)
1574 * Sets an options on the specified handle.
1581 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
1582 LPVOID lpBuffer, DWORD dwBufferLength)
1590 case INTERNET_OPTION_PROXY:
1592 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
1593 LPINTERNET_PROXY_INFOW piw;
1594 DWORD proxlen, prbylen;
1597 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
1598 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
1599 wlen = sizeof(*piw) + proxlen + prbylen;
1600 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
1601 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
1602 piw->dwAccessType = pi->dwAccessType;
1603 prox = (LPWSTR) &piw[1];
1604 prby = &prox[proxlen+1];
1605 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
1606 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
1607 piw->lpszProxy = prox;
1608 piw->lpszProxyBypass = prby;
1611 case INTERNET_OPTION_USER_AGENT:
1612 case INTERNET_OPTION_USERNAME:
1613 case INTERNET_OPTION_PASSWORD:
1614 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
1616 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
1617 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
1622 wlen = dwBufferLength;
1625 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
1627 if( lpBuffer != wbuffer )
1628 HeapFree( GetProcessHeap(), 0, wbuffer );
1634 /***********************************************************************
1635 * InternetSetOptionExA (WININET.@)
1637 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
1638 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
1640 FIXME("Flags %08lx ignored\n", dwFlags);
1641 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
1644 /***********************************************************************
1645 * InternetSetOptionExW (WININET.@)
1647 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
1648 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
1650 FIXME("Flags %08lx ignored\n", dwFlags);
1651 if( dwFlags & ~ISO_VALID_FLAGS )
1653 SetLastError( ERROR_INVALID_PARAMETER );
1656 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
1660 /***********************************************************************
1661 * InternetCheckConnectionA (WININET.@)
1663 * Pings a requested host to check internet connection
1666 * TRUE on success and FALSE on failure. If a failure then
1667 * ERROR_NOT_CONNECTED is placesd into GetLastError
1670 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
1673 * this is a kludge which runs the resident ping program and reads the output.
1675 * Anyone have a better idea?
1686 * Crack or set the Address
1688 if (lpszUrl == NULL)
1691 * According to the doc we are supost to use the ip for the next
1692 * server in the WnInet internal server database. I have
1693 * no idea what that is or how to get it.
1695 * So someone needs to implement this.
1697 FIXME("Unimplemented with URL of NULL\n");
1702 URL_COMPONENTSA componets;
1704 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1705 componets.lpszHostName = (LPSTR)&host;
1706 componets.dwHostNameLength = 1024;
1708 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1711 TRACE("host name : %s\n",componets.lpszHostName);
1715 * Build our ping command
1717 strcpy(command,"ping -w 1 ");
1718 strcat(command,host);
1719 strcat(command," >/dev/null 2>/dev/null");
1721 TRACE("Ping command is : %s\n",command);
1723 status = system(command);
1725 TRACE("Ping returned a code of %i \n",status);
1727 /* Ping return code of 0 indicates success */
1734 SetLastError(ERROR_NOT_CONNECTED);
1740 /***********************************************************************
1741 * InternetCheckConnectionW (WININET.@)
1743 * Pings a requested host to check internet connection
1746 * TRUE on success and FALSE on failure. If a failure then
1747 * ERROR_NOT_CONNECTED is placed into GetLastError
1750 BOOL WINAPI InternetCheckConnectionW(LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
1756 len = lstrlenW(lpszUrl)+1;
1757 if (!(szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
1759 WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, len, NULL, NULL);
1760 rc = InternetCheckConnectionA((LPCSTR)szUrl, dwFlags, dwReserved);
1761 HeapFree(GetProcessHeap(), 0, szUrl);
1767 /**********************************************************
1768 * InternetOpenUrlA (WININET.@)
1773 * handle of connection or NULL on failure
1775 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
1776 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1778 URL_COMPONENTSA urlComponents;
1779 char protocol[32], hostName[MAXHOSTNAME], userName[1024];
1780 char password[1024], path[2048], extra[1024];
1781 HINTERNET client = NULL, client1 = NULL;
1783 TRACE("(%p, %s, %s, %08lx, %08lx, %08lx\n", hInternet, debugstr_a(lpszUrl), debugstr_a(lpszHeaders),
1784 dwHeadersLength, dwFlags, dwContext);
1786 urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
1787 urlComponents.lpszScheme = protocol;
1788 urlComponents.dwSchemeLength = 32;
1789 urlComponents.lpszHostName = hostName;
1790 urlComponents.dwHostNameLength = MAXHOSTNAME;
1791 urlComponents.lpszUserName = userName;
1792 urlComponents.dwUserNameLength = 1024;
1793 urlComponents.lpszPassword = password;
1794 urlComponents.dwPasswordLength = 1024;
1795 urlComponents.lpszUrlPath = path;
1796 urlComponents.dwUrlPathLength = 2048;
1797 urlComponents.lpszExtraInfo = extra;
1798 urlComponents.dwExtraInfoLength = 1024;
1799 if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
1801 switch(urlComponents.nScheme) {
1802 case INTERNET_SCHEME_FTP:
1803 if(urlComponents.nPort == 0)
1804 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
1805 client = InternetConnectA(hInternet, hostName, urlComponents.nPort,
1806 userName, password, INTERNET_SERVICE_FTP, dwFlags, dwContext);
1807 return FtpOpenFileA(client, path, GENERIC_READ, dwFlags, dwContext);
1808 case INTERNET_SCHEME_HTTP:
1809 case INTERNET_SCHEME_HTTPS:
1811 LPCSTR accept[2] = { "*/*", NULL };
1812 char *hostreq=(char*)malloc(strlen(hostName)+9);
1813 sprintf(hostreq, "Host: %s\r\n", hostName);
1814 if(urlComponents.nPort == 0) {
1815 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
1816 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1818 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1820 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName,
1821 password, INTERNET_SERVICE_HTTP, dwFlags, dwContext);
1824 client1 = HttpOpenRequestA(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
1825 if(client1 == NULL) {
1826 InternetCloseHandle(client);
1829 HttpAddRequestHeadersA(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
1830 if(!HttpSendRequestA(client1, NULL, 0, NULL, 0)) {
1831 InternetCloseHandle(client1);
1832 InternetCloseHandle(client);
1837 case INTERNET_SCHEME_GOPHER:
1838 /* gopher doesn't seem to be implemented in wine, but it's supposed
1839 * to be supported by InternetOpenUrlA. */
1846 /**********************************************************
1847 * InternetOpenUrlW (WININET.@)
1852 * handle of connection or NULL on failure
1854 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
1855 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1857 HINTERNET rc = (HINTERNET)NULL;
1859 INT lenUrl = lstrlenW(lpszUrl)+1;
1860 INT lenHeaders = lstrlenW(lpszHeaders)+1;
1861 CHAR *szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(CHAR));
1862 CHAR *szHeaders = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(CHAR));
1864 if (!szUrl || !szHeaders)
1867 HeapFree(GetProcessHeap(), 0, szUrl);
1869 HeapFree(GetProcessHeap(), 0, szHeaders);
1870 return (HINTERNET)NULL;
1873 WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, lenUrl,
1875 WideCharToMultiByte(CP_ACP, -1, lpszHeaders, -1, szHeaders, lenHeaders,
1878 rc = InternetOpenUrlA(hInternet, szUrl, szHeaders,
1879 dwHeadersLength, dwFlags, dwContext);
1881 HeapFree(GetProcessHeap(), 0, szUrl);
1882 HeapFree(GetProcessHeap(), 0, szHeaders);
1888 /***********************************************************************
1889 * INTERNET_SetLastError (internal)
1891 * Set last thread specific error
1896 void INTERNET_SetLastError(DWORD dwError)
1898 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1900 SetLastError(dwError);
1902 lpwite->dwError = dwError;
1906 /***********************************************************************
1907 * INTERNET_GetLastError (internal)
1909 * Get last thread specific error
1914 DWORD INTERNET_GetLastError()
1916 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1917 return lpwite->dwError;
1921 /***********************************************************************
1922 * INTERNET_WorkerThreadFunc (internal)
1924 * Worker thread execution function
1929 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1936 INTERNET_ExecuteWork();
1939 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1941 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1942 INTERNET_ExecuteWork();
1946 InterlockedIncrement(&dwNumIdleThreads);
1949 InterlockedDecrement(&dwNumIdleThreads);
1950 InterlockedDecrement(&dwNumThreads);
1951 TRACE("Worker thread exiting\n");
1956 /***********************************************************************
1957 * INTERNET_InsertWorkRequest (internal)
1959 * Insert work request into queue
1964 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1966 BOOL bSuccess = FALSE;
1967 LPWORKREQUEST lpNewRequest;
1971 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1974 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1975 lpNewRequest->prev = NULL;
1977 EnterCriticalSection(&csQueue);
1979 lpNewRequest->next = lpWorkQueueTail;
1980 if (lpWorkQueueTail)
1981 lpWorkQueueTail->prev = lpNewRequest;
1982 lpWorkQueueTail = lpNewRequest;
1983 if (!lpHeadWorkQueue)
1984 lpHeadWorkQueue = lpWorkQueueTail;
1986 LeaveCriticalSection(&csQueue);
1989 InterlockedIncrement(&dwNumJobs);
1996 /***********************************************************************
1997 * INTERNET_GetWorkRequest (internal)
1999 * Retrieves work request from queue
2004 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
2006 BOOL bSuccess = FALSE;
2007 LPWORKREQUEST lpRequest = NULL;
2011 EnterCriticalSection(&csQueue);
2013 if (lpHeadWorkQueue)
2015 lpRequest = lpHeadWorkQueue;
2016 lpHeadWorkQueue = lpHeadWorkQueue->prev;
2017 if (lpRequest == lpWorkQueueTail)
2018 lpWorkQueueTail = lpHeadWorkQueue;
2021 LeaveCriticalSection(&csQueue);
2025 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
2026 HeapFree(GetProcessHeap(), 0, lpRequest);
2028 InterlockedDecrement(&dwNumJobs);
2035 /***********************************************************************
2036 * INTERNET_AsyncCall (internal)
2038 * Retrieves work request from queue
2043 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
2047 BOOL bSuccess = FALSE;
2051 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
2053 InterlockedIncrement(&dwNumIdleThreads);
2055 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
2056 !(hThread = CreateThread(NULL, 0,
2057 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
2059 InterlockedDecrement(&dwNumThreads);
2060 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
2064 TRACE("Created new thread\n");
2068 INTERNET_InsertWorkRequest(lpWorkRequest);
2069 SetEvent(hWorkEvent);
2077 /***********************************************************************
2078 * INTERNET_ExecuteWork (internal)
2083 VOID INTERNET_ExecuteWork()
2085 WORKREQUEST workRequest;
2089 if (INTERNET_GetWorkRequest(&workRequest))
2091 TRACE("Got work %d\n", workRequest.asyncall);
2092 switch (workRequest.asyncall)
2095 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
2096 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
2097 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
2098 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
2101 case FTPSETCURRENTDIRECTORYA:
2102 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
2103 (LPCSTR)workRequest.LPSZDIRECTORY);
2104 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
2107 case FTPCREATEDIRECTORYA:
2108 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
2109 (LPCSTR)workRequest.LPSZDIRECTORY);
2110 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
2113 case FTPFINDFIRSTFILEA:
2114 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
2115 (LPCSTR)workRequest.LPSZSEARCHFILE,
2116 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
2117 workRequest.DWCONTEXT);
2118 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
2121 case FTPGETCURRENTDIRECTORYA:
2122 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
2123 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
2127 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
2128 (LPCSTR)workRequest.LPSZFILENAME,
2129 workRequest.FDWACCESS,
2130 workRequest.DWFLAGS,
2131 workRequest.DWCONTEXT);
2132 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
2136 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
2137 (LPCSTR)workRequest.LPSZREMOTEFILE,
2138 (LPCSTR)workRequest.LPSZNEWFILE,
2139 (BOOL)workRequest.FFAILIFEXISTS,
2140 workRequest.DWLOCALFLAGSATTRIBUTE,
2141 workRequest.DWFLAGS,
2142 workRequest.DWCONTEXT);
2143 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
2144 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
2147 case FTPDELETEFILEA:
2148 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
2149 (LPCSTR)workRequest.LPSZFILENAME);
2150 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
2153 case FTPREMOVEDIRECTORYA:
2154 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
2155 (LPCSTR)workRequest.LPSZDIRECTORY);
2156 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
2159 case FTPRENAMEFILEA:
2160 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
2161 (LPCSTR)workRequest.LPSZSRCFILE,
2162 (LPCSTR)workRequest.LPSZDESTFILE);
2163 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
2164 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
2167 case INTERNETFINDNEXTA:
2168 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
2169 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
2172 case HTTPSENDREQUESTA:
2173 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
2174 (LPCSTR)workRequest.LPSZHEADER,
2175 workRequest.DWHEADERLENGTH,
2176 (LPVOID)workRequest.LPOPTIONAL,
2177 workRequest.DWOPTIONALLENGTH);
2178 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
2181 case HTTPOPENREQUESTA:
2182 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
2183 (LPCSTR)workRequest.LPSZVERB,
2184 (LPCSTR)workRequest.LPSZOBJECTNAME,
2185 (LPCSTR)workRequest.LPSZVERSION,
2186 (LPCSTR)workRequest.LPSZREFERRER,
2187 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
2188 workRequest.DWFLAGS,
2189 workRequest.DWCONTEXT);
2190 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
2191 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
2192 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
2193 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
2197 SendAsyncCallbackInt((LPWININETAPPINFOA)workRequest.param1,
2198 (HINTERNET)workRequest.param2, workRequest.param3,
2199 workRequest.param4, (LPVOID)workRequest.param5,
2200 workRequest.param6);
2207 /***********************************************************************
2208 * INTERNET_GetResponseBuffer
2213 LPSTR INTERNET_GetResponseBuffer()
2215 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2217 return lpwite->response;
2220 /***********************************************************************
2221 * INTERNET_GetNextLine (internal)
2223 * Parse next line in directory string listing
2226 * Pointer to beginning of next line
2231 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
2235 BOOL bSuccess = FALSE;
2241 FD_SET(nSocket, &infd);
2242 tv.tv_sec=RESPONSE_TIMEOUT;
2245 while (nRecv < *dwBuffer)
2247 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
2249 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
2251 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2255 if (lpszBuffer[nRecv] == '\n')
2260 if (lpszBuffer[nRecv] != '\r')
2265 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
2273 lpszBuffer[nRecv] = '\0';
2274 *dwBuffer = nRecv - 1;
2275 TRACE(":%d %s\n", nRecv, lpszBuffer);
2284 /***********************************************************************
2287 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
2288 LPDWORD lpdwNumberOfBytesAvailble,
2289 DWORD dwFlags, DWORD dwConext)
2291 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hFile;
2298 SetLastError(ERROR_NO_MORE_FILES);
2302 TRACE("--> %p %i\n",lpwhr,lpwhr->hdr.htype);
2304 switch (lpwhr->hdr.htype)
2307 if (!NETCON_recv(&((LPWININETHTTPREQA)hFile)->netConnection, buffer,
2308 4048, MSG_PEEK, (int *)lpdwNumberOfBytesAvailble))
2310 SetLastError(ERROR_NO_MORE_FILES);
2318 FIXME("unsupported file type\n");
2322 TRACE("<-- %i\n",retval);
2327 /***********************************************************************
2330 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
2337 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
2344 /***********************************************************************
2347 * On windows this function is supposed to dial the default internet
2348 * connection. We don't want to have Wine dial out to the internet so
2349 * we return TRUE by default. It might be nice to check if we are connected.
2356 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
2360 /* Tell that we are connected to the internet. */
2364 /***********************************************************************
2365 * InternetAutodialHangup
2367 * Hangs up an connection made with InternetAutodial
2376 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
2380 /* we didn't dial, we don't disconnect */
2384 /***********************************************************************
2386 * InternetCombineUrlA
2388 * Combine a base URL with a relative URL
2396 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
2397 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
2401 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2402 dwFlags ^= ICU_NO_ENCODE;
2403 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
2408 /***********************************************************************
2410 * InternetCombineUrlW
2412 * Combine a base URL with a relative URL
2420 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
2421 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
2425 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2426 dwFlags ^= ICU_NO_ENCODE;
2427 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
2432 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
2433 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
2439 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
2440 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)