4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MAXHOSTNAME 100 /* from http.c */
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
48 #include "wine/debug.h"
50 #define NO_SHLWAPI_STREAM
53 #include "wine/exception.h"
54 #include "msvcrt/excpt.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
60 #define MAX_IDLE_WORKER 1000*60*1
61 #define MAX_WORKER_THREADS 10
62 #define RESPONSE_TIMEOUT 30
64 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
65 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
67 /* filter for page-fault exceptions */
68 static WINE_EXCEPTION_FILTER(page_fault)
70 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
71 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
72 return EXCEPTION_EXECUTE_HANDLER;
73 return EXCEPTION_CONTINUE_SEARCH;
79 CHAR response[MAX_REPLY_LEN];
80 } WITHREADERROR, *LPWITHREADERROR;
82 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
83 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
84 VOID INTERNET_ExecuteWork();
86 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
88 DWORD dwNumIdleThreads;
89 HANDLE hEventArray[2];
90 #define hQuitEvent hEventArray[0]
91 #define hWorkEvent hEventArray[1]
92 CRITICAL_SECTION csQueue;
93 LPWORKREQUEST lpHeadWorkQueue;
94 LPWORKREQUEST lpWorkQueueTail;
96 /***********************************************************************
97 * WININET_LibMain [Internal] Initializes the internal 'WININET.DLL'.
100 * hinstDLL [I] handle to the DLL's instance
102 * lpvReserved [I] reserved, must be NULL
110 WININET_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
112 TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
115 case DLL_PROCESS_ATTACH:
117 g_dwTlsErrIndex = TlsAlloc();
119 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
122 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
123 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
124 InitializeCriticalSection(&csQueue);
127 dwNumIdleThreads = 0;
129 case DLL_THREAD_ATTACH:
131 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
135 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
139 case DLL_THREAD_DETACH:
140 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
142 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
144 HeapFree(GetProcessHeap(), 0, lpwite);
148 case DLL_PROCESS_DETACH:
150 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
152 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
153 TlsFree(g_dwTlsErrIndex);
156 SetEvent(hQuitEvent);
158 CloseHandle(hQuitEvent);
159 CloseHandle(hWorkEvent);
160 DeleteCriticalSection(&csQueue);
168 /***********************************************************************
169 * InternetOpenA (WININET.@)
171 * Per-application initialization of wininet
174 * HINTERNET on success
178 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent,
179 DWORD dwAccessType, LPCSTR lpszProxy,
180 LPCSTR lpszProxyBypass, DWORD dwFlags)
182 LPWININETAPPINFOA lpwai = NULL;
186 /* Clear any error information */
187 INTERNET_SetLastError(0);
189 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
191 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
194 memset(lpwai, 0, sizeof(WININETAPPINFOA));
195 lpwai->hdr.htype = WH_HINIT;
196 lpwai->hdr.lpwhparent = NULL;
197 lpwai->hdr.dwFlags = dwFlags;
198 if (NULL != lpszAgent)
200 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
201 strcpy( lpwai->lpszAgent, lpszAgent );
203 if (NULL != lpszProxy)
205 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
206 strcpy( lpwai->lpszProxy, lpszProxy );
208 if (NULL != lpszProxyBypass)
210 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
211 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
213 lpwai->dwAccessType = dwAccessType;
216 return (HINTERNET)lpwai;
220 /***********************************************************************
221 * InternetGetLastResponseInfoA (WININET.@)
223 * Return last wininet error description on the calling thread
226 * TRUE on success of writting to buffer
230 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
231 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
233 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
237 *lpdwError = lpwite->dwError;
240 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
241 *lpdwBufferLength = strlen(lpszBuffer);
244 *lpdwBufferLength = 0;
250 /***********************************************************************
251 * InternetGetConnectedState (WININET.@)
253 * Return connected state
257 * if lpdwStatus is not null, return the status (off line,
258 * modem, lan...) in it.
259 * FALSE if not connected
261 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
264 FIXME("always returning LAN connection.\n");
265 *lpdwStatus = INTERNET_CONNECTION_LAN;
271 /***********************************************************************
272 * InternetConnectA (WININET.@)
274 * Open a ftp, gopher or http session
277 * HINTERNET a session handle on success
281 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
282 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
283 LPCSTR lpszUserName, LPCSTR lpszPassword,
284 DWORD dwService, DWORD dwFlags, DWORD dwContext)
286 HINTERNET rc = (HINTERNET) NULL;
288 TRACE("ServerPort %i\n",nServerPort);
290 /* Clear any error information */
291 INTERNET_SetLastError(0);
295 case INTERNET_SERVICE_FTP:
296 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
297 lpszUserName, lpszPassword, dwFlags, dwContext);
300 case INTERNET_SERVICE_HTTP:
301 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
302 lpszUserName, lpszPassword, dwFlags, dwContext);
305 case INTERNET_SERVICE_GOPHER:
313 /***********************************************************************
314 * InternetFindNextFileA (WININET.@)
316 * Continues a file search from a previous call to FindFirstFile
323 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
325 LPWININETAPPINFOA hIC = NULL;
326 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
330 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
332 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
336 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
337 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
339 WORKREQUEST workRequest;
341 workRequest.asyncall = INTERNETFINDNEXTA;
342 workRequest.HFTPSESSION = (DWORD)hFind;
343 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
345 return INTERNET_AsyncCall(&workRequest);
349 return INTERNET_FindNextFileA(hFind, lpvFindData);
353 /***********************************************************************
354 * INTERNET_FindNextFileA (Internal)
356 * Continues a file search from a previous call to FindFirstFile
363 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
365 BOOL bSuccess = TRUE;
366 LPWININETAPPINFOA hIC = NULL;
367 LPWIN32_FIND_DATAA lpFindFileData;
368 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
372 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
374 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
378 /* Clear any error information */
379 INTERNET_SetLastError(0);
381 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
383 FIXME("Only FTP find next supported\n");
384 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
388 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
390 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
391 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
393 if (lpwh->index >= lpwh->size)
395 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
400 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
403 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
407 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
408 if (hIC->lpfnStatusCB)
410 INTERNET_ASYNC_RESULT iar;
412 iar.dwResult = (DWORD)bSuccess;
413 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
414 INTERNET_GetLastError();
416 SendAsyncCallback(hIC, hFind, lpwh->hdr.dwContext,
417 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
418 sizeof(INTERNET_ASYNC_RESULT));
425 /***********************************************************************
426 * INTERNET_CloseHandle (internal)
428 * Close internet handle
434 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
438 SendAsyncCallback(lpwai, lpwai, lpwai->hdr.dwContext,
439 INTERNET_STATUS_HANDLE_CLOSING, lpwai,
442 if (lpwai->lpszAgent)
443 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
445 if (lpwai->lpszProxy)
446 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
448 if (lpwai->lpszProxyBypass)
449 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
451 HeapFree(GetProcessHeap(), 0, lpwai);
455 /***********************************************************************
456 * InternetCloseHandle (WININET.@)
458 * Generic close handle function
465 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
468 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
470 TRACE("%p\n",hInternet);
475 /* Clear any error information */
476 INTERNET_SetLastError(0);
482 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
486 case WH_HHTTPSESSION:
487 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
492 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
497 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
501 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
507 } __EXCEPT(page_fault) {
508 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
517 /***********************************************************************
518 * SetUrlComponentValue (Internal)
520 * Helper function for InternetCrackUrlA
527 BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen, LPCSTR lpszStart, INT len)
529 TRACE("%s (%d)\n", lpszStart, len);
531 if (*dwComponentLen != 0)
533 if (*lppszComponent == NULL)
535 *lppszComponent = (LPSTR)lpszStart;
536 *dwComponentLen = len;
540 INT ncpylen = min((*dwComponentLen)-1, len);
541 strncpy(*lppszComponent, lpszStart, ncpylen);
542 (*lppszComponent)[ncpylen] = '\0';
543 *dwComponentLen = ncpylen;
551 /***********************************************************************
552 * InternetCrackUrlA (WININET.@)
554 * Break up URL into its components
556 * TODO: Handle dwFlags
563 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
564 LPURL_COMPONENTSA lpUrlComponents)
568 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
571 LPSTR lpszParam = NULL;
572 BOOL bIsAbsolute = FALSE;
573 LPSTR lpszap = (char*)lpszUrl;
578 /* Determine if the URI is absolute. */
579 while (*lpszap != '\0')
581 if (isalnum(*lpszap))
586 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
593 lpszcp = (LPSTR)lpszUrl; /* Relative url */
600 lpszParam = strpbrk(lpszap, ";?");
601 if (lpszParam != NULL)
603 if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
604 &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
610 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
614 /* Get scheme first. */
615 lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
616 if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
617 &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
620 /* Eat ':' in protocol. */
623 /* Skip over slashes. */
635 lpszNetLoc = strpbrk(lpszcp, "/");
639 lpszNetLoc = min(lpszNetLoc, lpszParam);
641 lpszNetLoc = lpszParam;
643 else if (!lpszNetLoc)
644 lpszNetLoc = lpszcp + strlen(lpszcp);
652 /* [<user>[<:password>]@]<host>[:<port>] */
653 /* First find the user and password if they exist */
655 lpszHost = strchr(lpszcp, '@');
656 if (lpszHost == NULL || lpszHost > lpszNetLoc)
658 /* username and password not specified. */
659 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
660 &lpUrlComponents->dwUserNameLength, NULL, 0);
661 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
662 &lpUrlComponents->dwPasswordLength, NULL, 0);
664 else /* Parse out username and password */
666 LPSTR lpszUser = lpszcp;
667 LPSTR lpszPasswd = lpszHost;
669 while (lpszcp < lpszHost)
677 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
678 &lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);
680 if (lpszPasswd != lpszHost)
682 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
683 &lpUrlComponents->dwPasswordLength,
684 lpszPasswd == lpszHost ? NULL : lpszPasswd,
685 lpszHost - lpszPasswd);
687 lpszcp++; /* Advance to beginning of host */
690 /* Parse <host><:port> */
693 lpszPort = lpszNetLoc;
695 while (lpszcp < lpszNetLoc)
703 SetUrlComponentValue(&lpUrlComponents->lpszHostName,
704 &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
706 if (lpszPort != lpszNetLoc)
707 lpUrlComponents->nPort = atoi(++lpszPort);
709 lpUrlComponents->nPort = 0;
713 /* Here lpszcp points to:
715 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
716 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
718 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
722 /* Only truncate the parameter list if it's already been saved
723 * in lpUrlComponents->lpszExtraInfo.
725 if (lpszParam && lpUrlComponents->dwExtraInfoLength)
726 len = lpszParam - lpszcp;
729 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
730 * newlines if necessary.
732 LPSTR lpsznewline = strchr (lpszcp, '\n');
733 if (lpsznewline != NULL)
734 len = lpsznewline - lpszcp;
736 len = strlen(lpszcp);
739 if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
740 &lpUrlComponents->dwUrlPathLength, lpszcp, len))
745 lpUrlComponents->dwUrlPathLength = 0;
748 TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
749 lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);
755 /***********************************************************************
756 * GetUrlCacheEntryInfoA (WININET.@)
759 BOOL WINAPI GetUrlCacheEntryInfoA(LPCSTR lpszUrl,
760 LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntry,
761 LPDWORD lpCacheEntrySize)
767 /***********************************************************************
768 * CommitUrlCacheEntryA (WININET.@)
771 BOOL WINAPI CommitUrlCacheEntryA(LPCSTR lpszUrl, LPCSTR lpszLocalName,
772 FILETIME ExpireTime, FILETIME lastModified, DWORD cacheEntryType,
773 LPBYTE lpHeaderInfo, DWORD headerSize, LPCSTR fileExtension,
780 /***********************************************************************
781 * InternetAttemptConnect (WININET.@)
783 * Attempt to make a connection to the internet
786 * ERROR_SUCCESS on success
787 * Error value on failure
790 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
793 return ERROR_SUCCESS;
797 /***********************************************************************
798 * InternetCanonicalizeUrlA (WININET.@)
800 * Escape unsafe characters and spaces
807 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
808 LPDWORD lpdwBufferLength, DWORD dwFlags)
811 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
812 lpdwBufferLength, dwFlags);
814 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
815 dwFlags ^= ICU_NO_ENCODE;
817 dwFlags |= 0x80000000; /* Don't know what this means */
819 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
821 return (hr == S_OK) ? TRUE : FALSE;
824 /***********************************************************************
825 * InternetSetStatusCallback (WININET.@)
827 * Sets up a callback function which is called as progress is made
828 * during an operation.
831 * Previous callback or NULL on success
832 * INTERNET_INVALID_STATUS_CALLBACK on failure
835 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
836 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
838 INTERNET_STATUS_CALLBACK retVal;
839 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
841 TRACE("0x%08lx\n", (ULONG)hInternet);
842 if (lpwai->hdr.htype != WH_HINIT)
843 return INTERNET_INVALID_STATUS_CALLBACK;
845 retVal = lpwai->lpfnStatusCB;
846 lpwai->lpfnStatusCB = lpfnIntCB;
852 /***********************************************************************
853 * InternetWriteFile (WININET.@)
855 * Write data to an open internet file
862 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
863 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
867 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
876 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
880 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
889 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
891 *lpdwNumOfBytesWritten = retval ? res : 0;
898 /***********************************************************************
899 * InternetReadFile (WININET.@)
901 * Read data from an open internet file
908 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
909 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
913 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
923 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
927 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
936 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, 0);
938 *dwNumOfBytesRead = retval ? res : 0;
944 /***********************************************************************
945 * InternetQueryOptionA (WININET.@)
947 * Queries an options on the specified handle
954 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
955 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
957 LPWININETHANDLEHEADER lpwhh;
958 BOOL bSuccess = FALSE;
960 TRACE("0x%08lx\n", dwOption);
962 if (NULL == hInternet)
964 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
968 lpwhh = (LPWININETHANDLEHEADER) hInternet;
972 case INTERNET_OPTION_HANDLE_TYPE:
974 ULONG type = lpwhh->htype;
975 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
977 if (*lpdwBufferLength < sizeof(ULONG))
978 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
981 memcpy(lpBuffer, &type, sizeof(ULONG));
982 *lpdwBufferLength = sizeof(ULONG);
988 case INTERNET_OPTION_REQUEST_FLAGS:
991 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
992 if (*lpdwBufferLength < sizeof(ULONG))
993 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
996 memcpy(lpBuffer, &flags, sizeof(ULONG));
997 *lpdwBufferLength = sizeof(ULONG);
1003 case INTERNET_OPTION_URL:
1004 case INTERNET_OPTION_DATAFILE_NAME:
1006 ULONG type = lpwhh->htype;
1007 if (type == WH_HHTTPREQ)
1009 LPWININETHTTPREQA lpreq = hInternet;
1012 sprintf(url,"http://%s%s",lpreq->lpszHostName,lpreq->lpszPath);
1013 TRACE("INTERNET_OPTION_URL: %s\n",url);
1014 if (*lpdwBufferLength < strlen(url)+1)
1015 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1018 memcpy(lpBuffer, url, strlen(url)+1);
1019 *lpdwBufferLength = strlen(url)+1;
1025 case INTERNET_OPTION_HTTP_VERSION:
1028 * Presently hardcoded to 1.1
1030 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1031 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1037 FIXME("Stub! %ld \n",dwOption);
1045 /***********************************************************************
1046 * InternetSetOptionW (WININET.@)
1048 * Sets an options on the specified handle
1055 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
1056 LPVOID lpBuffer, DWORD dwBufferLength)
1058 LPWININETHANDLEHEADER lpwhh;
1059 BOOL bSuccess = FALSE;
1061 TRACE("0x%08lx\n", dwOption);
1063 if (NULL == hInternet)
1065 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1069 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1074 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1083 /***********************************************************************
1084 * InternetSetOptionA (WININET.@)
1086 * Sets an options on the specified handle.
1093 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
1094 LPVOID lpBuffer, DWORD dwBufferLength)
1096 /* FIXME!!! implement if lpBuffer is a string, dwBufferLength is
1098 return InternetSetOptionW(hInternet,dwOption, lpBuffer,
1103 /***********************************************************************
1104 * InternetGetCookieA (WININET.@)
1106 * Retrieve cookie from the specified url
1113 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1114 LPSTR lpCookieData, LPDWORD lpdwSize)
1116 FIXME("(%s,%s,%p), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),
1121 /***********************************************************************
1122 * InternetSetCookieA (WININET.@)
1124 * Sets cookie for the specified url
1131 BOOL WINAPI InternetSetCookieA(
1132 LPCSTR lpszUrl, LPCSTR lpszCookieName, LPCSTR lpCookieData
1134 FIXME("(%s,%s,%s), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),debugstr_a(lpCookieData));
1138 /***********************************************************************
1139 * GetInternetScheme (internal)
1145 * INTERNET_SCHEME_UNKNOWN on failure
1148 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
1151 if(lpszScheme==NULL)
1152 return INTERNET_SCHEME_UNKNOWN;
1154 if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
1155 return INTERNET_SCHEME_FTP;
1156 else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
1157 return INTERNET_SCHEME_GOPHER;
1158 else if (!strncasecmp("http", lpszScheme, nMaxCmp))
1159 return INTERNET_SCHEME_HTTP;
1160 else if (!strncasecmp("https", lpszScheme, nMaxCmp))
1161 return INTERNET_SCHEME_HTTPS;
1162 else if (!strncasecmp("file", lpszScheme, nMaxCmp))
1163 return INTERNET_SCHEME_FILE;
1164 else if (!strncasecmp("news", lpszScheme, nMaxCmp))
1165 return INTERNET_SCHEME_NEWS;
1166 else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
1167 return INTERNET_SCHEME_MAILTO;
1169 return INTERNET_SCHEME_UNKNOWN;
1172 /***********************************************************************
1173 * InternetCheckConnectionA (WININET.@)
1175 * Pings a requested host to check internet connection
1179 * TRUE on success and FALSE on failure. if a failures then
1180 * ERROR_NOT_CONNECTED is places into GetLastError
1183 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
1186 * this is a kludge which runs the resident ping program and reads the output.
1188 * Anyone have a better idea?
1199 * Crack or set the Address
1201 if (lpszUrl == NULL)
1204 * According to the doc we are supost to use the ip for the next
1205 * server in the WnInet internal server database. I have
1206 * no idea what that is or how to get it.
1208 * So someone needs to implement this.
1210 FIXME("Unimplemented with URL of NULL\n");
1215 URL_COMPONENTSA componets;
1217 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1218 componets.lpszHostName = (LPSTR)&host;
1219 componets.dwHostNameLength = 1024;
1221 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1224 TRACE("host name : %s\n",componets.lpszHostName);
1228 * Build our ping command
1230 strcpy(command,"ping -w 1 ");
1231 strcat(command,host);
1232 strcat(command," >/dev/null 2>/dev/null");
1234 TRACE("Ping command is : %s\n",command);
1236 status = system(command);
1238 TRACE("Ping returned a code of %i \n",status);
1240 /* Ping return code of 0 indicates success */
1247 SetLastError(ERROR_NOT_CONNECTED);
1252 /**********************************************************
1253 * InternetOpenUrlA (WININET.@)
1258 * handle of connection or NULL on failure
1260 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl, LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1262 URL_COMPONENTSA urlComponents;
1263 char protocol[32], hostName[MAXHOSTNAME], userName[1024], password[1024], path[2048], extra[1024];
1264 HINTERNET client = NULL, client1 = NULL;
1265 urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
1266 urlComponents.lpszScheme = protocol;
1267 urlComponents.dwSchemeLength = 32;
1268 urlComponents.lpszHostName = hostName;
1269 urlComponents.dwHostNameLength = MAXHOSTNAME;
1270 urlComponents.lpszUserName = userName;
1271 urlComponents.dwUserNameLength = 1024;
1272 urlComponents.lpszPassword = password;
1273 urlComponents.dwPasswordLength = 1024;
1274 urlComponents.lpszUrlPath = path;
1275 urlComponents.dwUrlPathLength = 2048;
1276 urlComponents.lpszExtraInfo = extra;
1277 urlComponents.dwExtraInfoLength = 1024;
1278 if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
1280 switch(urlComponents.nScheme) {
1281 case INTERNET_SCHEME_FTP:
1282 if(urlComponents.nPort == 0)
1283 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
1284 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName, password, INTERNET_SERVICE_FTP, dwFlags, dwContext);
1285 return FtpOpenFileA(client, path, GENERIC_READ, dwFlags, dwContext);
1287 case INTERNET_SCHEME_HTTP:
1288 case INTERNET_SCHEME_HTTPS:
1290 LPCSTR accept[2] = { "*/*", NULL };
1291 char *hostreq=(char*)malloc(strlen(hostName)+9);
1292 sprintf(hostreq, "Host: %s\r\n", hostName);
1293 if(urlComponents.nPort == 0) {
1294 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
1295 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1297 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1299 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName, password, INTERNET_SERVICE_HTTP, dwFlags, dwContext);
1302 client1 = HttpOpenRequestA(hInternet, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
1303 if(client1 == NULL) {
1304 InternetCloseHandle(client);
1307 HttpAddRequestHeadersA(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
1308 HttpAddRequestHeadersA(client1, hostreq, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1309 if(!HttpSendRequestA(client1, NULL, 0, NULL, 0)) {
1310 InternetCloseHandle(client1);
1311 InternetCloseHandle(client);
1317 case INTERNET_SCHEME_GOPHER:
1318 /* gopher doesn't seem to be implemented in wine, but it's supposed
1319 * to be supported by InternetOpenUrlA. */
1324 InternetCloseHandle(client);
1328 /***********************************************************************
1329 * INTERNET_SetLastError (internal)
1331 * Set last thread specific error
1336 void INTERNET_SetLastError(DWORD dwError)
1338 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1340 SetLastError(dwError);
1341 lpwite->dwError = dwError;
1345 /***********************************************************************
1346 * INTERNET_GetLastError (internal)
1348 * Get last thread specific error
1353 DWORD INTERNET_GetLastError()
1355 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1356 return lpwite->dwError;
1360 /***********************************************************************
1361 * INTERNET_WorkerThreadFunc (internal)
1363 * Worker thread execution function
1368 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1374 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1376 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1377 INTERNET_ExecuteWork();
1381 InterlockedIncrement(&dwNumIdleThreads);
1384 InterlockedDecrement(&dwNumIdleThreads);
1385 InterlockedDecrement(&dwNumThreads);
1386 TRACE("Worker thread exiting\n");
1391 /***********************************************************************
1392 * INTERNET_InsertWorkRequest (internal)
1394 * Insert work request into queue
1399 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1401 BOOL bSuccess = FALSE;
1402 LPWORKREQUEST lpNewRequest;
1406 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1409 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1410 lpNewRequest->prev = NULL;
1412 EnterCriticalSection(&csQueue);
1414 lpNewRequest->next = lpWorkQueueTail;
1415 if (lpWorkQueueTail)
1416 lpWorkQueueTail->prev = lpNewRequest;
1417 lpWorkQueueTail = lpNewRequest;
1418 if (!lpHeadWorkQueue)
1419 lpHeadWorkQueue = lpWorkQueueTail;
1421 LeaveCriticalSection(&csQueue);
1430 /***********************************************************************
1431 * INTERNET_GetWorkRequest (internal)
1433 * Retrieves work request from queue
1438 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
1440 BOOL bSuccess = FALSE;
1441 LPWORKREQUEST lpRequest = NULL;
1445 EnterCriticalSection(&csQueue);
1447 if (lpHeadWorkQueue)
1449 lpRequest = lpHeadWorkQueue;
1450 lpHeadWorkQueue = lpHeadWorkQueue->prev;
1451 if (lpRequest == lpWorkQueueTail)
1452 lpWorkQueueTail = lpHeadWorkQueue;
1455 LeaveCriticalSection(&csQueue);
1459 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
1460 HeapFree(GetProcessHeap(), 0, lpRequest);
1468 /***********************************************************************
1469 * INTERNET_AsyncCall (internal)
1471 * Retrieves work request from queue
1476 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
1480 BOOL bSuccess = FALSE;
1484 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
1486 InterlockedIncrement(&dwNumIdleThreads);
1488 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
1489 !(hThread = CreateThread(NULL, 0,
1490 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
1492 InterlockedDecrement(&dwNumThreads);
1493 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
1497 TRACE("Created new thread\n");
1501 INTERNET_InsertWorkRequest(lpWorkRequest);
1502 SetEvent(hWorkEvent);
1510 /***********************************************************************
1511 * INTERNET_ExecuteWork (internal)
1516 VOID INTERNET_ExecuteWork()
1518 WORKREQUEST workRequest;
1522 if (INTERNET_GetWorkRequest(&workRequest))
1524 switch (workRequest.asyncall)
1527 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
1528 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
1529 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
1530 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
1533 case FTPSETCURRENTDIRECTORYA:
1534 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1535 (LPCSTR)workRequest.LPSZDIRECTORY);
1536 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1539 case FTPCREATEDIRECTORYA:
1540 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1541 (LPCSTR)workRequest.LPSZDIRECTORY);
1542 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1545 case FTPFINDFIRSTFILEA:
1546 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
1547 (LPCSTR)workRequest.LPSZSEARCHFILE,
1548 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
1549 workRequest.DWCONTEXT);
1550 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
1553 case FTPGETCURRENTDIRECTORYA:
1554 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1555 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
1559 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
1560 (LPCSTR)workRequest.LPSZFILENAME,
1561 workRequest.FDWACCESS,
1562 workRequest.DWFLAGS,
1563 workRequest.DWCONTEXT);
1564 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1568 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
1569 (LPCSTR)workRequest.LPSZREMOTEFILE,
1570 (LPCSTR)workRequest.LPSZNEWFILE,
1571 (BOOL)workRequest.FFAILIFEXISTS,
1572 workRequest.DWLOCALFLAGSATTRIBUTE,
1573 workRequest.DWFLAGS,
1574 workRequest.DWCONTEXT);
1575 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
1576 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
1579 case FTPDELETEFILEA:
1580 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
1581 (LPCSTR)workRequest.LPSZFILENAME);
1582 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1585 case FTPREMOVEDIRECTORYA:
1586 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1587 (LPCSTR)workRequest.LPSZDIRECTORY);
1588 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1591 case FTPRENAMEFILEA:
1592 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
1593 (LPCSTR)workRequest.LPSZSRCFILE,
1594 (LPCSTR)workRequest.LPSZDESTFILE);
1595 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
1596 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
1599 case INTERNETFINDNEXTA:
1600 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
1601 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
1604 case HTTPSENDREQUESTA:
1605 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
1606 (LPCSTR)workRequest.LPSZHEADER,
1607 workRequest.DWHEADERLENGTH,
1608 (LPVOID)workRequest.LPOPTIONAL,
1609 workRequest.DWOPTIONALLENGTH);
1610 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
1613 case HTTPOPENREQUESTA:
1614 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
1615 (LPCSTR)workRequest.LPSZVERB,
1616 (LPCSTR)workRequest.LPSZOBJECTNAME,
1617 (LPCSTR)workRequest.LPSZVERSION,
1618 (LPCSTR)workRequest.LPSZREFERRER,
1619 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
1620 workRequest.DWFLAGS,
1621 workRequest.DWCONTEXT);
1622 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
1623 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
1624 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
1625 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
1629 SendAsyncCallbackInt((LPWININETAPPINFOA)workRequest.param1,
1630 (HINTERNET)workRequest.param2, workRequest.param3,
1631 workRequest.param4, (LPVOID)workRequest.param5,
1632 workRequest.param6);
1639 /***********************************************************************
1640 * INTERNET_GetResponseBuffer
1645 LPSTR INTERNET_GetResponseBuffer()
1647 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1649 return lpwite->response;
1653 /***********************************************************************
1654 * INTERNET_GetNextLine (internal)
1656 * Parse next line in directory string listing
1659 * Pointer to beginning of next line
1664 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
1668 BOOL bSuccess = FALSE;
1674 FD_SET(nSocket, &infd);
1675 tv.tv_sec=RESPONSE_TIMEOUT;
1678 while (nRecv < *dwBuffer)
1680 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
1682 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
1684 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1688 if (lpszBuffer[nRecv] == '\n')
1693 if (lpszBuffer[nRecv] != '\r')
1698 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
1706 lpszBuffer[nRecv] = '\0';
1707 *dwBuffer = nRecv - 1;
1708 TRACE(":%d %s\n", nRecv, lpszBuffer);
1717 /***********************************************************************
1720 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
1721 LPDWORD lpdwNumberOfBytesAvailble,
1722 DWORD dwFlags, DWORD dwConext)
1724 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hFile;
1731 SetLastError(ERROR_NO_MORE_FILES);
1735 TRACE("--> %p %i %i\n",lpwhr,lpwhr->hdr.htype,lpwhr->nSocketFD);
1737 switch (lpwhr->hdr.htype)
1740 nSocket = lpwhr->nSocketFD;
1751 retval = recv(nSocket,buffer,4048,MSG_PEEK);
1755 SetLastError(ERROR_NO_MORE_FILES);
1758 if (lpdwNumberOfBytesAvailble)
1760 (*lpdwNumberOfBytesAvailble) = retval;
1763 TRACE("<-- %i\n",retval);
1768 /***********************************************************************
1771 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
1778 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
1785 /***********************************************************************
1788 * On windows this function is supposed to dial the default internet
1789 * connection. We don't want to have Wine dial out to the internet so
1790 * we return TRUE by default. It might be nice to check if we are connected.
1798 BOOL WINAPI InternetAutoDial(DWORD dwFlags, HWND hwndParent)
1802 /* Tell that we are connected to the internet. */