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>
44 #include "wine/debug.h"
46 #define NO_SHLWAPI_STREAM
49 #include "wine/exception.h"
50 #include "msvcrt/excpt.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
56 #define MAX_IDLE_WORKER 1000*60*1
57 #define MAX_WORKER_THREADS 10
58 #define RESPONSE_TIMEOUT 30
60 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
61 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
63 /* filter for page-fault exceptions */
64 static WINE_EXCEPTION_FILTER(page_fault)
66 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
67 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
68 return EXCEPTION_EXECUTE_HANDLER;
69 return EXCEPTION_CONTINUE_SEARCH;
75 CHAR response[MAX_REPLY_LEN];
76 } WITHREADERROR, *LPWITHREADERROR;
78 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
79 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
80 VOID INTERNET_ExecuteWork();
82 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
84 DWORD dwNumIdleThreads;
85 HANDLE hEventArray[2];
86 #define hQuitEvent hEventArray[0]
87 #define hWorkEvent hEventArray[1]
88 CRITICAL_SECTION csQueue;
89 LPWORKREQUEST lpHeadWorkQueue;
90 LPWORKREQUEST lpWorkQueueTail;
92 /***********************************************************************
93 * WININET_LibMain [Internal] Initializes the internal 'WININET.DLL'.
96 * hinstDLL [I] handle to the DLL's instance
98 * lpvReserved [I] reserved, must be NULL
106 WININET_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
108 TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
111 case DLL_PROCESS_ATTACH:
113 g_dwTlsErrIndex = TlsAlloc();
115 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
118 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
119 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
120 InitializeCriticalSection(&csQueue);
123 dwNumIdleThreads = 0;
125 case DLL_THREAD_ATTACH:
127 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
131 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
135 case DLL_THREAD_DETACH:
136 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
138 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
140 HeapFree(GetProcessHeap(), 0, lpwite);
144 case DLL_PROCESS_DETACH:
146 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
148 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
149 TlsFree(g_dwTlsErrIndex);
152 SetEvent(hQuitEvent);
154 CloseHandle(hQuitEvent);
155 CloseHandle(hWorkEvent);
156 DeleteCriticalSection(&csQueue);
164 /***********************************************************************
165 * InternetOpenA (WININET.@)
167 * Per-application initialization of wininet
170 * HINTERNET on success
174 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent,
175 DWORD dwAccessType, LPCSTR lpszProxy,
176 LPCSTR lpszProxyBypass, DWORD dwFlags)
178 LPWININETAPPINFOA lpwai = NULL;
182 /* Clear any error information */
183 INTERNET_SetLastError(0);
185 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
187 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
190 memset(lpwai, 0, sizeof(WININETAPPINFOA));
191 lpwai->hdr.htype = WH_HINIT;
192 lpwai->hdr.lpwhparent = NULL;
193 lpwai->hdr.dwFlags = dwFlags;
194 if (NULL != lpszAgent)
196 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
197 strcpy( lpwai->lpszAgent, lpszAgent );
199 if (NULL != lpszProxy)
201 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
202 strcpy( lpwai->lpszProxy, lpszProxy );
204 if (NULL != lpszProxyBypass)
206 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
207 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
209 lpwai->dwAccessType = dwAccessType;
212 return (HINTERNET)lpwai;
216 /***********************************************************************
217 * InternetGetLastResponseInfoA (WININET.@)
219 * Return last wininet error description on the calling thread
222 * TRUE on success of writting to buffer
226 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
227 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
229 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
233 *lpdwError = lpwite->dwError;
236 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
237 *lpdwBufferLength = strlen(lpszBuffer);
240 *lpdwBufferLength = 0;
246 /***********************************************************************
247 * InternetGetConnectedState (WININET.@)
249 * Return connected state
253 * if lpdwStatus is not null, return the status (off line,
254 * modem, lan...) in it.
255 * FALSE if not connected
257 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
260 FIXME("always returning LAN connection.\n");
261 *lpdwStatus = INTERNET_CONNECTION_LAN;
267 /***********************************************************************
268 * InternetConnectA (WININET.@)
270 * Open a ftp, gopher or http session
273 * HINTERNET a session handle on success
277 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
278 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
279 LPCSTR lpszUserName, LPCSTR lpszPassword,
280 DWORD dwService, DWORD dwFlags, DWORD dwContext)
282 HINTERNET rc = (HINTERNET) NULL;
284 TRACE("ServerPort %i\n",nServerPort);
286 /* Clear any error information */
287 INTERNET_SetLastError(0);
291 case INTERNET_SERVICE_FTP:
292 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
293 lpszUserName, lpszPassword, dwFlags, dwContext);
296 case INTERNET_SERVICE_HTTP:
297 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
298 lpszUserName, lpszPassword, dwFlags, dwContext);
301 case INTERNET_SERVICE_GOPHER:
309 /***********************************************************************
310 * InternetFindNextFileA (WININET.@)
312 * Continues a file search from a previous call to FindFirstFile
319 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
321 LPWININETAPPINFOA hIC = NULL;
322 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
326 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
328 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
332 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
333 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
335 WORKREQUEST workRequest;
337 workRequest.asyncall = INTERNETFINDNEXTA;
338 workRequest.HFTPSESSION = (DWORD)hFind;
339 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
341 return INTERNET_AsyncCall(&workRequest);
345 return INTERNET_FindNextFileA(hFind, lpvFindData);
349 /***********************************************************************
350 * INTERNET_FindNextFileA (Internal)
352 * Continues a file search from a previous call to FindFirstFile
359 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
361 BOOL bSuccess = TRUE;
362 LPWININETAPPINFOA hIC = NULL;
363 LPWIN32_FIND_DATAA lpFindFileData;
364 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
368 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
370 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
374 /* Clear any error information */
375 INTERNET_SetLastError(0);
377 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
379 FIXME("Only FTP find next supported\n");
380 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
384 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
386 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
387 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
389 if (lpwh->index >= lpwh->size)
391 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
396 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
399 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
403 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
404 if (hIC->lpfnStatusCB)
406 INTERNET_ASYNC_RESULT iar;
408 iar.dwResult = (DWORD)bSuccess;
409 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
410 INTERNET_GetLastError();
412 SendAsyncCallback(hIC, hFind, lpwh->hdr.dwContext,
413 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
414 sizeof(INTERNET_ASYNC_RESULT));
421 /***********************************************************************
422 * INTERNET_CloseHandle (internal)
424 * Close internet handle
430 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
434 SendAsyncCallback(lpwai, lpwai, lpwai->hdr.dwContext,
435 INTERNET_STATUS_HANDLE_CLOSING, lpwai,
438 if (lpwai->lpszAgent)
439 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
441 if (lpwai->lpszProxy)
442 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
444 if (lpwai->lpszProxyBypass)
445 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
447 HeapFree(GetProcessHeap(), 0, lpwai);
451 /***********************************************************************
452 * InternetCloseHandle (WININET.@)
454 * Generic close handle function
461 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
464 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
466 TRACE("%p\n",hInternet);
471 /* Clear any error information */
472 INTERNET_SetLastError(0);
478 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
482 case WH_HHTTPSESSION:
483 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
488 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
493 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
497 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
503 } __EXCEPT(page_fault) {
504 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
513 /***********************************************************************
514 * SetUrlComponentValue (Internal)
516 * Helper function for InternetCrackUrlA
523 BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen, LPCSTR lpszStart, INT len)
525 TRACE("%s (%d)\n", lpszStart, len);
527 if (*dwComponentLen != 0)
529 if (*lppszComponent == NULL)
531 *lppszComponent = (LPSTR)lpszStart;
532 *dwComponentLen = len;
536 INT ncpylen = min((*dwComponentLen)-1, len);
537 strncpy(*lppszComponent, lpszStart, ncpylen);
538 (*lppszComponent)[ncpylen] = '\0';
539 *dwComponentLen = ncpylen;
547 /***********************************************************************
548 * InternetCrackUrlA (WININET.@)
550 * Break up URL into its components
552 * TODO: Handle dwFlags
559 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
560 LPURL_COMPONENTSA lpUrlComponents)
564 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
567 LPSTR lpszParam = NULL;
568 BOOL bIsAbsolute = FALSE;
569 LPSTR lpszap = (char*)lpszUrl;
574 /* Determine if the URI is absolute. */
575 while (*lpszap != '\0')
577 if (isalnum(*lpszap))
582 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
589 lpszcp = (LPSTR)lpszUrl; /* Relative url */
596 lpszParam = strpbrk(lpszap, ";?");
597 if (lpszParam != NULL)
599 if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
600 &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
606 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
610 /* Get scheme first. */
611 lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
612 if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
613 &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
616 /* Eat ':' in protocol. */
619 /* Skip over slashes. */
631 lpszNetLoc = strpbrk(lpszcp, "/");
635 lpszNetLoc = min(lpszNetLoc, lpszParam);
637 lpszNetLoc = lpszParam;
639 else if (!lpszNetLoc)
640 lpszNetLoc = lpszcp + strlen(lpszcp);
648 /* [<user>[<:password>]@]<host>[:<port>] */
649 /* First find the user and password if they exist */
651 lpszHost = strchr(lpszcp, '@');
652 if (lpszHost == NULL || lpszHost > lpszNetLoc)
654 /* username and password not specified. */
655 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
656 &lpUrlComponents->dwUserNameLength, NULL, 0);
657 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
658 &lpUrlComponents->dwPasswordLength, NULL, 0);
660 else /* Parse out username and password */
662 LPSTR lpszUser = lpszcp;
663 LPSTR lpszPasswd = lpszHost;
665 while (lpszcp < lpszHost)
673 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
674 &lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);
676 if (lpszPasswd != lpszHost)
678 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
679 &lpUrlComponents->dwPasswordLength,
680 lpszPasswd == lpszHost ? NULL : lpszPasswd,
681 lpszHost - lpszPasswd);
683 lpszcp++; /* Advance to beginning of host */
686 /* Parse <host><:port> */
689 lpszPort = lpszNetLoc;
691 while (lpszcp < lpszNetLoc)
699 SetUrlComponentValue(&lpUrlComponents->lpszHostName,
700 &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
702 if (lpszPort != lpszNetLoc)
703 lpUrlComponents->nPort = atoi(++lpszPort);
705 lpUrlComponents->nPort = 0;
709 /* Here lpszcp points to:
711 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
712 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
714 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
718 /* Only truncate the parameter list if it's already been saved
719 * in lpUrlComponents->lpszExtraInfo.
721 if (lpszParam && lpUrlComponents->dwExtraInfoLength)
722 len = lpszParam - lpszcp;
725 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
726 * newlines if necessary.
728 LPSTR lpsznewline = strchr (lpszcp, '\n');
729 if (lpsznewline != NULL)
730 len = lpsznewline - lpszcp;
732 len = strlen(lpszcp);
735 if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
736 &lpUrlComponents->dwUrlPathLength, lpszcp, len))
741 lpUrlComponents->dwUrlPathLength = 0;
744 TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
745 lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);
751 /***********************************************************************
752 * GetUrlCacheEntryInfoA (WININET.@)
755 BOOL WINAPI GetUrlCacheEntryInfoA(LPCSTR lpszUrl,
756 LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntry,
757 LPDWORD lpCacheEntrySize)
763 /***********************************************************************
764 * CommitUrlCacheEntryA (WININET.@)
767 BOOL WINAPI CommitUrlCacheEntryA(LPCSTR lpszUrl, LPCSTR lpszLocalName,
768 FILETIME ExpireTime, FILETIME lastModified, DWORD cacheEntryType,
769 LPBYTE lpHeaderInfo, DWORD headerSize, LPCSTR fileExtension,
776 /***********************************************************************
777 * InternetAttemptConnect (WININET.@)
779 * Attempt to make a connection to the internet
782 * ERROR_SUCCESS on success
783 * Error value on failure
786 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
789 return ERROR_SUCCESS;
793 /***********************************************************************
794 * InternetCanonicalizeUrlA (WININET.@)
796 * Escape unsafe characters and spaces
803 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
804 LPDWORD lpdwBufferLength, DWORD dwFlags)
807 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
808 lpdwBufferLength, dwFlags);
810 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
811 dwFlags ^= ICU_NO_ENCODE;
813 dwFlags |= 0x80000000; /* Don't know what this means */
815 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
817 return (hr == S_OK) ? TRUE : FALSE;
820 /***********************************************************************
821 * InternetSetStatusCallback (WININET.@)
823 * Sets up a callback function which is called as progress is made
824 * during an operation.
827 * Previous callback or NULL on success
828 * INTERNET_INVALID_STATUS_CALLBACK on failure
831 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
832 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
834 INTERNET_STATUS_CALLBACK retVal;
835 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
837 TRACE("0x%08lx\n", (ULONG)hInternet);
838 if (lpwai->hdr.htype != WH_HINIT)
839 return INTERNET_INVALID_STATUS_CALLBACK;
841 retVal = lpwai->lpfnStatusCB;
842 lpwai->lpfnStatusCB = lpfnIntCB;
848 /***********************************************************************
849 * InternetWriteFile (WININET.@)
851 * Write data to an open internet file
858 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
859 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
863 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
872 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
876 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
885 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
887 *lpdwNumOfBytesWritten = retval ? res : 0;
894 /***********************************************************************
895 * InternetReadFile (WININET.@)
897 * Read data from an open internet file
904 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
905 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
909 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
919 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
923 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
932 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, 0);
934 *dwNumOfBytesRead = retval ? res : 0;
940 /***********************************************************************
941 * InternetQueryOptionA (WININET.@)
943 * Queries an options on the specified handle
950 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
951 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
953 LPWININETHANDLEHEADER lpwhh;
954 BOOL bSuccess = FALSE;
956 TRACE("0x%08lx\n", dwOption);
958 if (NULL == hInternet)
960 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
964 lpwhh = (LPWININETHANDLEHEADER) hInternet;
968 case INTERNET_OPTION_HANDLE_TYPE:
970 ULONG type = lpwhh->htype;
971 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
973 if (*lpdwBufferLength < sizeof(ULONG))
974 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
977 memcpy(lpBuffer, &type, sizeof(ULONG));
978 *lpdwBufferLength = sizeof(ULONG);
984 case INTERNET_OPTION_REQUEST_FLAGS:
987 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
988 if (*lpdwBufferLength < sizeof(ULONG))
989 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
992 memcpy(lpBuffer, &flags, sizeof(ULONG));
993 *lpdwBufferLength = sizeof(ULONG);
999 case INTERNET_OPTION_URL:
1000 case INTERNET_OPTION_DATAFILE_NAME:
1002 ULONG type = lpwhh->htype;
1003 if (type == WH_HHTTPREQ)
1005 LPWININETHTTPREQA lpreq = hInternet;
1008 sprintf(url,"http://%s%s",lpreq->lpszHostName,lpreq->lpszPath);
1009 TRACE("INTERNET_OPTION_URL: %s\n",url);
1010 if (*lpdwBufferLength < strlen(url)+1)
1011 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1014 memcpy(lpBuffer, url, strlen(url)+1);
1015 *lpdwBufferLength = strlen(url)+1;
1021 case INTERNET_OPTION_HTTP_VERSION:
1024 * Presently hardcoded to 1.1
1026 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1027 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1033 FIXME("Stub! %ld \n",dwOption);
1041 /***********************************************************************
1042 * InternetSetOptionW (WININET.@)
1044 * Sets an options on the specified handle
1051 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
1052 LPVOID lpBuffer, DWORD dwBufferLength)
1054 LPWININETHANDLEHEADER lpwhh;
1055 BOOL bSuccess = FALSE;
1057 TRACE("0x%08lx\n", dwOption);
1059 if (NULL == hInternet)
1061 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1065 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1070 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1079 /***********************************************************************
1080 * InternetSetOptionA (WININET.@)
1082 * Sets an options on the specified handle.
1089 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
1090 LPVOID lpBuffer, DWORD dwBufferLength)
1092 /* FIXME!!! implement if lpBuffer is a string, dwBufferLength is
1094 return InternetSetOptionW(hInternet,dwOption, lpBuffer,
1099 /***********************************************************************
1100 * InternetGetCookieA (WININET.@)
1102 * Retrieve cookie from the specified url
1109 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1110 LPSTR lpCookieData, LPDWORD lpdwSize)
1112 FIXME("(%s,%s,%p), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),
1117 /***********************************************************************
1118 * InternetSetCookieA (WININET.@)
1120 * Sets cookie for the specified url
1127 BOOL WINAPI InternetSetCookieA(
1128 LPCSTR lpszUrl, LPCSTR lpszCookieName, LPCSTR lpCookieData
1130 FIXME("(%s,%s,%s), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),debugstr_a(lpCookieData));
1134 /***********************************************************************
1135 * GetInternetScheme (internal)
1141 * INTERNET_SCHEME_UNKNOWN on failure
1144 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
1147 if(lpszScheme==NULL)
1148 return INTERNET_SCHEME_UNKNOWN;
1150 if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
1151 return INTERNET_SCHEME_FTP;
1152 else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
1153 return INTERNET_SCHEME_GOPHER;
1154 else if (!strncasecmp("http", lpszScheme, nMaxCmp))
1155 return INTERNET_SCHEME_HTTP;
1156 else if (!strncasecmp("https", lpszScheme, nMaxCmp))
1157 return INTERNET_SCHEME_HTTPS;
1158 else if (!strncasecmp("file", lpszScheme, nMaxCmp))
1159 return INTERNET_SCHEME_FILE;
1160 else if (!strncasecmp("news", lpszScheme, nMaxCmp))
1161 return INTERNET_SCHEME_NEWS;
1162 else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
1163 return INTERNET_SCHEME_MAILTO;
1165 return INTERNET_SCHEME_UNKNOWN;
1168 /***********************************************************************
1169 * InternetCheckConnectionA (WININET.@)
1171 * Pings a requested host to check internet connection
1175 * TRUE on success and FALSE on failure. if a failures then
1176 * ERROR_NOT_CONNECTED is places into GetLastError
1179 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
1182 * this is a kludge which runs the resident ping program and reads the output.
1184 * Anyone have a better idea?
1195 * Crack or set the Address
1197 if (lpszUrl == NULL)
1200 * According to the doc we are supost to use the ip for the next
1201 * server in the WnInet internal server database. I have
1202 * no idea what that is or how to get it.
1204 * So someone needs to implement this.
1206 FIXME("Unimplemented with URL of NULL\n");
1211 URL_COMPONENTSA componets;
1213 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1214 componets.lpszHostName = (LPSTR)&host;
1215 componets.dwHostNameLength = 1024;
1217 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1220 TRACE("host name : %s\n",componets.lpszHostName);
1224 * Build our ping command
1226 strcpy(command,"ping -w 1 ");
1227 strcat(command,host);
1228 strcat(command," >/dev/null 2>/dev/null");
1230 TRACE("Ping command is : %s\n",command);
1232 status = system(command);
1234 TRACE("Ping returned a code of %i \n",status);
1236 /* Ping return code of 0 indicates success */
1243 SetLastError(ERROR_NOT_CONNECTED);
1248 /**********************************************************
1249 * InternetOpenUrlA (WININET.@)
1254 * handle of connection or NULL on failure
1256 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl, LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1258 URL_COMPONENTSA urlComponents;
1259 char protocol[32], hostName[MAXHOSTNAME], userName[1024], password[1024], path[2048], extra[1024];
1260 HINTERNET client = NULL, client1 = NULL;
1261 urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
1262 urlComponents.lpszScheme = protocol;
1263 urlComponents.dwSchemeLength = 32;
1264 urlComponents.lpszHostName = hostName;
1265 urlComponents.dwHostNameLength = MAXHOSTNAME;
1266 urlComponents.lpszUserName = userName;
1267 urlComponents.dwUserNameLength = 1024;
1268 urlComponents.lpszPassword = password;
1269 urlComponents.dwPasswordLength = 1024;
1270 urlComponents.lpszUrlPath = path;
1271 urlComponents.dwUrlPathLength = 2048;
1272 urlComponents.lpszExtraInfo = extra;
1273 urlComponents.dwExtraInfoLength = 1024;
1274 if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
1276 switch(urlComponents.nScheme) {
1277 case INTERNET_SCHEME_FTP:
1278 if(urlComponents.nPort == 0)
1279 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
1280 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName, password, INTERNET_SERVICE_FTP, dwFlags, dwContext);
1281 return FtpOpenFileA(client, path, GENERIC_READ, dwFlags, dwContext);
1283 case INTERNET_SCHEME_HTTP:
1284 case INTERNET_SCHEME_HTTPS:
1286 LPCSTR accept[2] = { "*/*", NULL };
1287 char *hostreq=(char*)malloc(strlen(hostName)+9);
1288 sprintf(hostreq, "Host: %s\r\n", hostName);
1289 if(urlComponents.nPort == 0) {
1290 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
1291 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1293 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1295 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName, password, INTERNET_SERVICE_HTTP, dwFlags, dwContext);
1298 client1 = HttpOpenRequestA(hInternet, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
1299 if(client1 == NULL) {
1300 InternetCloseHandle(client);
1303 HttpAddRequestHeadersA(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
1304 HttpAddRequestHeadersA(client1, hostreq, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1305 if(!HttpSendRequestA(client1, NULL, 0, NULL, 0)) {
1306 InternetCloseHandle(client1);
1307 InternetCloseHandle(client);
1313 case INTERNET_SCHEME_GOPHER:
1314 /* gopher doesn't seem to be implemented in wine, but it's supposed
1315 * to be supported by InternetOpenUrlA. */
1320 InternetCloseHandle(client);
1324 /***********************************************************************
1325 * INTERNET_SetLastError (internal)
1327 * Set last thread specific error
1332 void INTERNET_SetLastError(DWORD dwError)
1334 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1336 SetLastError(dwError);
1337 lpwite->dwError = dwError;
1341 /***********************************************************************
1342 * INTERNET_GetLastError (internal)
1344 * Get last thread specific error
1349 DWORD INTERNET_GetLastError()
1351 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1352 return lpwite->dwError;
1356 /***********************************************************************
1357 * INTERNET_WorkerThreadFunc (internal)
1359 * Worker thread execution function
1364 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1370 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1372 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1373 INTERNET_ExecuteWork();
1377 InterlockedIncrement(&dwNumIdleThreads);
1380 InterlockedDecrement(&dwNumIdleThreads);
1381 InterlockedDecrement(&dwNumThreads);
1382 TRACE("Worker thread exiting\n");
1387 /***********************************************************************
1388 * INTERNET_InsertWorkRequest (internal)
1390 * Insert work request into queue
1395 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1397 BOOL bSuccess = FALSE;
1398 LPWORKREQUEST lpNewRequest;
1402 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1405 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1406 lpNewRequest->prev = NULL;
1408 EnterCriticalSection(&csQueue);
1410 lpNewRequest->next = lpWorkQueueTail;
1411 if (lpWorkQueueTail)
1412 lpWorkQueueTail->prev = lpNewRequest;
1413 lpWorkQueueTail = lpNewRequest;
1414 if (!lpHeadWorkQueue)
1415 lpHeadWorkQueue = lpWorkQueueTail;
1417 LeaveCriticalSection(&csQueue);
1426 /***********************************************************************
1427 * INTERNET_GetWorkRequest (internal)
1429 * Retrieves work request from queue
1434 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
1436 BOOL bSuccess = FALSE;
1437 LPWORKREQUEST lpRequest = NULL;
1441 EnterCriticalSection(&csQueue);
1443 if (lpHeadWorkQueue)
1445 lpRequest = lpHeadWorkQueue;
1446 lpHeadWorkQueue = lpHeadWorkQueue->prev;
1447 if (lpRequest == lpWorkQueueTail)
1448 lpWorkQueueTail = lpHeadWorkQueue;
1451 LeaveCriticalSection(&csQueue);
1455 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
1456 HeapFree(GetProcessHeap(), 0, lpRequest);
1464 /***********************************************************************
1465 * INTERNET_AsyncCall (internal)
1467 * Retrieves work request from queue
1472 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
1476 BOOL bSuccess = FALSE;
1480 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
1482 InterlockedIncrement(&dwNumIdleThreads);
1484 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
1485 !(hThread = CreateThread(NULL, 0,
1486 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
1488 InterlockedDecrement(&dwNumThreads);
1489 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
1493 TRACE("Created new thread\n");
1497 INTERNET_InsertWorkRequest(lpWorkRequest);
1498 SetEvent(hWorkEvent);
1506 /***********************************************************************
1507 * INTERNET_ExecuteWork (internal)
1512 VOID INTERNET_ExecuteWork()
1514 WORKREQUEST workRequest;
1518 if (INTERNET_GetWorkRequest(&workRequest))
1520 switch (workRequest.asyncall)
1523 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
1524 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
1525 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
1526 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
1529 case FTPSETCURRENTDIRECTORYA:
1530 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1531 (LPCSTR)workRequest.LPSZDIRECTORY);
1532 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1535 case FTPCREATEDIRECTORYA:
1536 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1537 (LPCSTR)workRequest.LPSZDIRECTORY);
1538 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1541 case FTPFINDFIRSTFILEA:
1542 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
1543 (LPCSTR)workRequest.LPSZSEARCHFILE,
1544 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
1545 workRequest.DWCONTEXT);
1546 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
1549 case FTPGETCURRENTDIRECTORYA:
1550 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1551 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
1555 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
1556 (LPCSTR)workRequest.LPSZFILENAME,
1557 workRequest.FDWACCESS,
1558 workRequest.DWFLAGS,
1559 workRequest.DWCONTEXT);
1560 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1564 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
1565 (LPCSTR)workRequest.LPSZREMOTEFILE,
1566 (LPCSTR)workRequest.LPSZNEWFILE,
1567 (BOOL)workRequest.FFAILIFEXISTS,
1568 workRequest.DWLOCALFLAGSATTRIBUTE,
1569 workRequest.DWFLAGS,
1570 workRequest.DWCONTEXT);
1571 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
1572 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
1575 case FTPDELETEFILEA:
1576 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
1577 (LPCSTR)workRequest.LPSZFILENAME);
1578 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1581 case FTPREMOVEDIRECTORYA:
1582 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1583 (LPCSTR)workRequest.LPSZDIRECTORY);
1584 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1587 case FTPRENAMEFILEA:
1588 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
1589 (LPCSTR)workRequest.LPSZSRCFILE,
1590 (LPCSTR)workRequest.LPSZDESTFILE);
1591 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
1592 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
1595 case INTERNETFINDNEXTA:
1596 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
1597 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
1600 case HTTPSENDREQUESTA:
1601 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
1602 (LPCSTR)workRequest.LPSZHEADER,
1603 workRequest.DWHEADERLENGTH,
1604 (LPVOID)workRequest.LPOPTIONAL,
1605 workRequest.DWOPTIONALLENGTH);
1606 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
1609 case HTTPOPENREQUESTA:
1610 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
1611 (LPCSTR)workRequest.LPSZVERB,
1612 (LPCSTR)workRequest.LPSZOBJECTNAME,
1613 (LPCSTR)workRequest.LPSZVERSION,
1614 (LPCSTR)workRequest.LPSZREFERRER,
1615 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
1616 workRequest.DWFLAGS,
1617 workRequest.DWCONTEXT);
1618 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
1619 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
1620 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
1621 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
1625 SendAsyncCallbackInt((LPWININETAPPINFOA)workRequest.param1,
1626 (HINTERNET)workRequest.param2, workRequest.param3,
1627 workRequest.param4, (LPVOID)workRequest.param5,
1628 workRequest.param6);
1635 /***********************************************************************
1636 * INTERNET_GetResponseBuffer
1641 LPSTR INTERNET_GetResponseBuffer()
1643 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1645 return lpwite->response;
1649 /***********************************************************************
1650 * INTERNET_GetNextLine (internal)
1652 * Parse next line in directory string listing
1655 * Pointer to beginning of next line
1660 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
1664 BOOL bSuccess = FALSE;
1670 FD_SET(nSocket, &infd);
1671 tv.tv_sec=RESPONSE_TIMEOUT;
1674 while (nRecv < *dwBuffer)
1676 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
1678 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
1680 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1684 if (lpszBuffer[nRecv] == '\n')
1689 if (lpszBuffer[nRecv] != '\r')
1694 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
1702 lpszBuffer[nRecv] = '\0';
1703 *dwBuffer = nRecv - 1;
1704 TRACE(":%d %s\n", nRecv, lpszBuffer);
1713 /***********************************************************************
1716 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
1717 LPDWORD lpdwNumberOfBytesAvailble,
1718 DWORD dwFlags, DWORD dwConext)
1720 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hFile;
1727 SetLastError(ERROR_NO_MORE_FILES);
1731 TRACE("--> %p %i %i\n",lpwhr,lpwhr->hdr.htype,lpwhr->nSocketFD);
1733 switch (lpwhr->hdr.htype)
1736 nSocket = lpwhr->nSocketFD;
1747 retval = recv(nSocket,buffer,4048,MSG_PEEK);
1751 SetLastError(ERROR_NO_MORE_FILES);
1754 if (lpdwNumberOfBytesAvailble)
1756 (*lpdwNumberOfBytesAvailble) = retval;
1759 TRACE("<-- %i\n",retval);
1764 /***********************************************************************
1767 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
1774 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
1781 /***********************************************************************
1784 * On windows this function is supposed to dial the default internet
1785 * connection. We don't want to have Wine dial out to the internet so
1786 * we return TRUE by default. It might be nice to check if we are connected.
1794 BOOL WINAPI InternetAutoDial(DWORD dwFlags, HWND hwndParent)
1798 /* Tell that we are connected to the internet. */