4 * Copyright 1999 Corel Corporation
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_SOCKET_H
15 # include <sys/socket.h>
26 #include "debugtools.h"
28 #define NO_SHLWAPI_STREAM
33 DEFAULT_DEBUG_CHANNEL(wininet);
35 #define MAX_IDLE_WORKER 1000*60*1
36 #define MAX_WORKER_THREADS 10
37 #define RESPONSE_TIMEOUT 30
39 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
40 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
45 CHAR response[MAX_REPLY_LEN];
46 } WITHREADERROR, *LPWITHREADERROR;
48 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
49 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
50 VOID INTERNET_ExecuteWork();
52 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
54 DWORD dwNumIdleThreads;
55 HANDLE hEventArray[2];
56 #define hQuitEvent hEventArray[0]
57 #define hWorkEvent hEventArray[1]
58 CRITICAL_SECTION csQueue;
59 LPWORKREQUEST lpHeadWorkQueue;
60 LPWORKREQUEST lpWorkQueueTail;
62 /***********************************************************************
63 * WININET_LibMain [Internal] Initializes the internal 'WININET.DLL'.
66 * hinstDLL [I] handle to the DLL's instance
68 * lpvReserved [I] reserved, must be NULL
76 WININET_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
78 TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
81 case DLL_PROCESS_ATTACH:
83 g_dwTlsErrIndex = TlsAlloc();
85 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
88 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
89 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
90 InitializeCriticalSection(&csQueue);
95 case DLL_THREAD_ATTACH:
97 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
101 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
105 case DLL_THREAD_DETACH:
106 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
108 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
110 HeapFree(GetProcessHeap(), 0, lpwite);
114 case DLL_PROCESS_DETACH:
116 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
118 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
119 TlsFree(g_dwTlsErrIndex);
122 SetEvent(hQuitEvent);
124 CloseHandle(hQuitEvent);
125 CloseHandle(hWorkEvent);
126 DeleteCriticalSection(&csQueue);
134 /***********************************************************************
135 * InternetOpenA (WININET.@)
137 * Per-application initialization of wininet
140 * HINTERNET on success
144 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent,
145 DWORD dwAccessType, LPCSTR lpszProxy,
146 LPCSTR lpszProxyBypass, DWORD dwFlags)
148 LPWININETAPPINFOA lpwai = NULL;
152 /* Clear any error information */
153 INTERNET_SetLastError(0);
155 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
157 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
160 memset(lpwai, 0, sizeof(WININETAPPINFOA));
161 lpwai->hdr.htype = WH_HINIT;
162 lpwai->hdr.lpwhparent = NULL;
163 lpwai->hdr.dwFlags = dwFlags;
164 if (NULL != lpszAgent)
166 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
167 strcpy( lpwai->lpszAgent, lpszAgent );
169 if (NULL != lpszProxy)
171 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
172 strcpy( lpwai->lpszProxy, lpszProxy );
174 if (NULL != lpszProxyBypass)
176 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
177 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
179 lpwai->dwAccessType = dwAccessType;
182 return (HINTERNET)lpwai;
186 /***********************************************************************
187 * InternetGetLastResponseInfoA (WININET.@)
189 * Return last wininet error description on the calling thread
192 * TRUE on success of writting to buffer
196 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
197 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
199 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
203 *lpdwError = lpwite->dwError;
206 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
207 *lpdwBufferLength = strlen(lpszBuffer);
210 *lpdwBufferLength = 0;
216 /***********************************************************************
217 * InternetGetConnectedState (WININET.@)
219 * Return connected state
223 * if lpdwStatus is not null, return the status (off line,
224 * modem, lan...) in it.
225 * FALSE if not connected
227 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
230 FIXME("always returning LAN connection.\n");
231 *lpdwStatus = INTERNET_CONNECTION_LAN;
237 /***********************************************************************
238 * InternetConnectA (WININET.@)
240 * Open a ftp, gopher or http session
243 * HINTERNET a session handle on success
247 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
248 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
249 LPCSTR lpszUserName, LPCSTR lpszPassword,
250 DWORD dwService, DWORD dwFlags, DWORD dwContext)
252 HINTERNET rc = (HINTERNET) NULL;
256 /* Clear any error information */
257 INTERNET_SetLastError(0);
261 case INTERNET_SERVICE_FTP:
262 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
263 lpszUserName, lpszPassword, dwFlags, dwContext);
266 case INTERNET_SERVICE_HTTP:
267 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
268 lpszUserName, lpszPassword, dwFlags, dwContext);
271 case INTERNET_SERVICE_GOPHER:
279 /***********************************************************************
280 * InternetFindNextFileA (WININET.@)
282 * Continues a file search from a previous call to FindFirstFile
289 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
291 LPWININETAPPINFOA hIC = NULL;
292 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
296 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
298 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
302 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
303 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
305 WORKREQUEST workRequest;
307 workRequest.asyncall = INTERNETFINDNEXTA;
308 workRequest.HFTPSESSION = (DWORD)hFind;
309 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
311 return INTERNET_AsyncCall(&workRequest);
315 return INTERNET_FindNextFileA(hFind, lpvFindData);
319 /***********************************************************************
320 * INTERNET_FindNextFileA (Internal)
322 * Continues a file search from a previous call to FindFirstFile
329 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
331 BOOL bSuccess = TRUE;
332 LPWININETAPPINFOA hIC = NULL;
333 LPWIN32_FIND_DATAA lpFindFileData;
334 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
338 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
340 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
344 /* Clear any error information */
345 INTERNET_SetLastError(0);
347 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
349 FIXME("Only FTP find next supported\n");
350 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
354 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
356 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
357 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
359 if (lpwh->index >= lpwh->size)
361 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
366 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
369 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
373 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
374 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC && hIC->lpfnStatusCB)
376 INTERNET_ASYNC_RESULT iar;
378 iar.dwResult = (DWORD)bSuccess;
379 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
381 hIC->lpfnStatusCB(hFind, lpwh->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
382 &iar, sizeof(INTERNET_ASYNC_RESULT));
389 /***********************************************************************
390 * INTERNET_CloseHandle (internal)
392 * Close internet handle
398 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
400 if (lpwai->lpszAgent)
401 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
403 if (lpwai->lpszProxy)
404 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
406 if (lpwai->lpszProxyBypass)
407 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
409 HeapFree(GetProcessHeap(), 0, lpwai);
413 /***********************************************************************
414 * InternetCloseHandle (WININET.@)
416 * Generic close handle function
423 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
426 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
432 /* Clear any error information */
433 INTERNET_SetLastError(0);
438 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
442 case WH_HHTTPSESSION:
443 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
448 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
453 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
457 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
468 /***********************************************************************
469 * SetUrlComponentValue (Internal)
471 * Helper function for InternetCrackUrlA
478 BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen, LPCSTR lpszStart, INT len)
480 TRACE("%s (%d)\n", lpszStart, len);
482 if (*dwComponentLen != 0)
484 if (*lppszComponent == NULL)
486 *lppszComponent = (LPSTR)lpszStart;
487 *dwComponentLen = len;
491 INT ncpylen = min((*dwComponentLen)-1, len);
492 strncpy(*lppszComponent, lpszStart, ncpylen);
493 (*lppszComponent)[ncpylen] = '\0';
494 *dwComponentLen = ncpylen;
502 /***********************************************************************
503 * InternetCrackUrlA (WININET.@)
505 * Break up URL into its components
507 * TODO: Hadnle dwFlags
514 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
515 LPURL_COMPONENTSA lpUrlComponents)
519 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
522 LPSTR lpszParam = NULL;
523 BOOL bIsAbsolute = FALSE;
524 LPSTR lpszap = (char*)lpszUrl;
529 /* Determine if the URI is absolute. */
530 while (*lpszap != '\0')
532 if (isalnum(*lpszap))
537 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
544 lpszcp = (LPSTR)lpszUrl; /* Relative url */
551 lpszParam = strpbrk(lpszap, ";?");
552 if (lpszParam != NULL)
554 if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
555 &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
561 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
565 /* Get scheme first. */
566 lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
567 if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
568 &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
571 /* Eat ':' in protocol. */
574 /* Skip over slashes. */
586 lpszNetLoc = strpbrk(lpszcp, "/");
590 lpszNetLoc = min(lpszNetLoc, lpszParam);
592 lpszNetLoc = lpszParam;
594 else if (!lpszNetLoc)
595 lpszNetLoc = lpszcp + strlen(lpszcp);
603 /* [<user>[<:password>]@]<host>[:<port>] */
604 /* First find the user and password if they exist */
606 lpszHost = strchr(lpszcp, '@');
607 if (lpszHost == NULL || lpszHost > lpszNetLoc)
609 /* username and password not specified. */
610 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
611 &lpUrlComponents->dwUserNameLength, NULL, 0);
612 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
613 &lpUrlComponents->dwPasswordLength, NULL, 0);
615 else /* Parse out username and password */
617 LPSTR lpszUser = lpszcp;
618 LPSTR lpszPasswd = lpszHost;
620 while (lpszcp < lpszHost)
628 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
629 &lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);
631 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
632 &lpUrlComponents->dwPasswordLength,
633 lpszPasswd == lpszHost ? NULL : ++lpszPasswd,
634 lpszHost - lpszPasswd);
636 lpszcp++; /* Advance to beginning of host */
639 /* Parse <host><:port> */
642 lpszPort = lpszNetLoc;
644 while (lpszcp < lpszNetLoc)
652 SetUrlComponentValue(&lpUrlComponents->lpszHostName,
653 &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
655 if (lpszPort != lpszNetLoc)
656 lpUrlComponents->nPort = atoi(++lpszPort);
660 /* Here lpszcp points to:
662 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
663 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
665 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
669 /* Only truncate the parameter list if it's already been saved
670 * in lpUrlComponents->lpszExtraInfo.
672 if (lpszParam && lpUrlComponents->dwExtraInfoLength)
673 len = lpszParam - lpszcp;
676 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
677 * newlines if necessary.
679 LPSTR lpsznewline = strchr (lpszcp, '\n');
680 if (lpsznewline != NULL)
681 len = lpsznewline - lpszcp;
683 len = strlen(lpszcp);
686 if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
687 &lpUrlComponents->dwUrlPathLength, lpszcp, len))
692 lpUrlComponents->dwUrlPathLength = 0;
695 TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
696 lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);
702 /***********************************************************************
703 * GetUrlCacheEntryInfoA (WININET.@)
706 BOOL WINAPI GetUrlCacheEntryInfoA(LPCSTR lpszUrl,
707 LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntry,
708 LPDWORD lpCacheEntrySize)
714 /***********************************************************************
715 * CommitUrlCacheEntryA (WININET.@)
718 BOOL WINAPI CommitUrlCacheEntryA(LPCSTR lpszUrl, LPCSTR lpszLocalName,
719 FILETIME ExpireTime, FILETIME lastModified, DWORD cacheEntryType,
720 LPBYTE lpHeaderInfo, DWORD headerSize, LPCSTR fileExtension,
727 /***********************************************************************
728 * InternetAttemptConnect (WININET.@)
730 * Attempt to make a connection to the internet
733 * ERROR_SUCCESS on success
734 * Error value on failure
737 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
740 return ERROR_SUCCESS;
744 /***********************************************************************
745 * InternetCanonicalizeUrlA (WININET.@)
747 * Escape unsafe characters and spaces
754 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
755 LPDWORD lpdwBufferLength, DWORD dwFlags)
758 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
759 lpdwBufferLength, dwFlags);
761 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
762 dwFlags ^= ICU_NO_ENCODE;
764 dwFlags |= 0x80000000; /* Don't know what this means */
766 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
768 return (hr == S_OK) ? TRUE : FALSE;
771 /***********************************************************************
772 * InternetSetStatusCallback (WININET.@)
774 * Sets up a callback function which is called as progress is made
775 * during an operation.
778 * Previous callback or NULL on success
779 * INTERNET_INVALID_STATUS_CALLBACK on failure
782 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
783 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
785 INTERNET_STATUS_CALLBACK retVal;
786 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
788 TRACE("0x%08lx\n", (ULONG)hInternet);
789 if (lpwai->hdr.htype != WH_HINIT)
790 return INTERNET_INVALID_STATUS_CALLBACK;
792 retVal = lpwai->lpfnStatusCB;
793 lpwai->lpfnStatusCB = lpfnIntCB;
799 /***********************************************************************
800 * InternetWriteFile (WININET.@)
802 * Write data to an open internet file
809 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
810 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
814 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
823 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
827 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
836 *lpdwNumOfBytesWritten = INTERNET_WriteDataToStream(nSocket, lpBuffer, dwNumOfBytesToWrite);
837 if (*lpdwNumOfBytesWritten < 0)
838 *lpdwNumOfBytesWritten = 0;
847 /***********************************************************************
848 * InternetReadFile (WININET.@)
850 * Read data from an open internet file
857 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
858 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
862 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
871 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
875 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
884 *dwNumOfBytesRead = INTERNET_ReadDataFromStream(nSocket, lpBuffer, dwNumOfBytesToRead);
885 if (*dwNumOfBytesRead < 0)
886 *dwNumOfBytesRead = 0;
895 /***********************************************************************
896 * InternetQueryOptionA (WININET.@)
898 * Queries an options on the specified handle
905 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
906 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
908 LPWININETHANDLEHEADER lpwhh;
909 BOOL bSuccess = FALSE;
911 TRACE("0x%08lx\n", dwOption);
913 if (NULL == hInternet)
915 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
919 lpwhh = (LPWININETHANDLEHEADER) hInternet;
923 case INTERNET_OPTION_HANDLE_TYPE:
925 ULONG type = lpwhh->htype;
926 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
928 if (*lpdwBufferLength < sizeof(ULONG))
929 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
932 memcpy(lpBuffer, &type, sizeof(ULONG));
933 *lpdwBufferLength = sizeof(ULONG);
949 /***********************************************************************
950 * InternetGetCookieA (WININET.@)
952 * Retrieve cookie from the specified url
959 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
960 LPSTR lpCookieData, LPDWORD lpdwSize)
962 FIXME("(%s,%s,%p), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),
967 /***********************************************************************
968 * InternetSetCookieA (WININET.@)
970 * Sets cookie for the specified url
977 BOOL WINAPI InternetSetCookieA(
978 LPCSTR lpszUrl, LPCSTR lpszCookieName, LPCSTR lpCookieData
980 FIXME("(%s,%s,%s), stub!\n",debugstr_a(lpszUrl),debugstr_a(lpszCookieName),debugstr_a(lpCookieData));
984 /***********************************************************************
985 * GetInternetScheme (internal)
991 * INTERNET_SCHEME_UNKNOWN on failure
994 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
997 return INTERNET_SCHEME_UNKNOWN;
999 if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
1000 return INTERNET_SCHEME_FTP;
1001 else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
1002 return INTERNET_SCHEME_GOPHER;
1003 else if (!strncasecmp("http", lpszScheme, nMaxCmp))
1004 return INTERNET_SCHEME_HTTP;
1005 else if (!strncasecmp("https", lpszScheme, nMaxCmp))
1006 return INTERNET_SCHEME_HTTPS;
1007 else if (!strncasecmp("file", lpszScheme, nMaxCmp))
1008 return INTERNET_SCHEME_FILE;
1009 else if (!strncasecmp("news", lpszScheme, nMaxCmp))
1010 return INTERNET_SCHEME_NEWS;
1011 else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
1012 return INTERNET_SCHEME_MAILTO;
1014 return INTERNET_SCHEME_UNKNOWN;
1017 /***********************************************************************
1018 * InternetCheckConnectionA (WININET.@)
1020 * Pings a requested host to check internet connection
1024 * TRUE on success and FALSE on failure. if a failures then
1025 * ERROR_NOT_CONNECTED is places into GetLastError
1028 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
1031 * this is a kludge which runs the resident ping program and reads the output.
1033 * Anyone have a better idea?
1042 * Crack or set the Address
1044 if (lpszUrl == NULL)
1047 * According to the doc we are supost to use the ip for the next
1048 * server in the WnInet internal server database. I have
1049 * no idea what that is or how to get it.
1051 * So someone needs to implement this.
1053 FIXME("Unimplemented with URL of NULL\n");
1058 URL_COMPONENTSA componets;
1060 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1061 componets.lpszHostName = (LPSTR)&host;
1062 componets.dwHostNameLength = 1024;
1064 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1067 TRACE("host name : %s\n",componets.lpszHostName);
1071 * Build our ping command
1073 strcpy(command,"ping -w 1 ");
1074 strcat(command,host);
1075 strcat(command," >/dev/null 2>/dev/null");
1077 TRACE("Ping command is : %s\n",command);
1079 status = system(command);
1081 TRACE("Ping returned a code of %i \n",status);
1083 /* Ping return code of 0 indicates success */
1090 SetLastError(ERROR_NOT_CONNECTED);
1097 /***********************************************************************
1098 * INTERNET_WriteDataToStream (internal)
1100 * Send data to server
1104 * number of characters sent on success
1107 int INTERNET_WriteDataToStream(int nDataSocket, LPCVOID Buffer, DWORD BytesToWrite)
1109 if (nDataSocket == -1)
1112 return send(nDataSocket, Buffer, BytesToWrite, 0);
1116 /***********************************************************************
1117 * INTERNET_ReadDataFromStream (internal)
1119 * Read data from http server
1123 * number of characters sent on success
1126 int INTERNET_ReadDataFromStream(int nDataSocket, LPVOID Buffer, DWORD BytesToRead)
1128 if (nDataSocket == -1)
1131 return recv(nDataSocket, Buffer, BytesToRead, 0);
1135 /***********************************************************************
1136 * INTERNET_SetLastError (internal)
1138 * Set last thread specific error
1143 void INTERNET_SetLastError(DWORD dwError)
1145 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1147 SetLastError(dwError);
1148 lpwite->dwError = dwError;
1152 /***********************************************************************
1153 * INTERNET_GetLastError (internal)
1155 * Get last thread specific error
1160 DWORD INTERNET_GetLastError()
1162 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1163 return lpwite->dwError;
1167 /***********************************************************************
1168 * INTERNET_WorkerThreadFunc (internal)
1170 * Worker thread execution function
1175 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1181 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1183 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1184 INTERNET_ExecuteWork();
1188 InterlockedIncrement(&dwNumIdleThreads);
1191 InterlockedDecrement(&dwNumIdleThreads);
1192 InterlockedDecrement(&dwNumThreads);
1193 TRACE("Worker thread exiting\n");
1198 /***********************************************************************
1199 * INTERNET_InsertWorkRequest (internal)
1201 * Insert work request into queue
1206 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1208 BOOL bSuccess = FALSE;
1209 LPWORKREQUEST lpNewRequest;
1213 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1216 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1217 lpNewRequest->prev = NULL;
1219 EnterCriticalSection(&csQueue);
1221 lpNewRequest->next = lpWorkQueueTail;
1222 if (lpWorkQueueTail)
1223 lpWorkQueueTail->prev = lpNewRequest;
1224 lpWorkQueueTail = lpNewRequest;
1225 if (!lpHeadWorkQueue)
1226 lpHeadWorkQueue = lpWorkQueueTail;
1228 LeaveCriticalSection(&csQueue);
1237 /***********************************************************************
1238 * INTERNET_GetWorkRequest (internal)
1240 * Retrieves work request from queue
1245 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
1247 BOOL bSuccess = FALSE;
1248 LPWORKREQUEST lpRequest = NULL;
1252 EnterCriticalSection(&csQueue);
1254 if (lpHeadWorkQueue)
1256 lpRequest = lpHeadWorkQueue;
1257 lpHeadWorkQueue = lpHeadWorkQueue->prev;
1258 if (lpRequest == lpWorkQueueTail)
1259 lpWorkQueueTail = lpHeadWorkQueue;
1262 LeaveCriticalSection(&csQueue);
1266 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
1267 HeapFree(GetProcessHeap(), 0, lpRequest);
1275 /***********************************************************************
1276 * INTERNET_AsyncCall (internal)
1278 * Retrieves work request from queue
1283 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
1287 BOOL bSuccess = FALSE;
1291 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
1293 InterlockedIncrement(&dwNumIdleThreads);
1295 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
1296 !(hThread = CreateThread(NULL, 0,
1297 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
1299 InterlockedDecrement(&dwNumThreads);
1300 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
1304 TRACE("Created new thread\n");
1308 INTERNET_InsertWorkRequest(lpWorkRequest);
1309 SetEvent(hWorkEvent);
1317 /***********************************************************************
1318 * INTERNET_ExecuteWork (internal)
1323 VOID INTERNET_ExecuteWork()
1325 WORKREQUEST workRequest;
1329 if (INTERNET_GetWorkRequest(&workRequest))
1331 switch (workRequest.asyncall)
1334 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
1335 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
1336 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
1337 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
1340 case FTPSETCURRENTDIRECTORYA:
1341 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1342 (LPCSTR)workRequest.LPSZDIRECTORY);
1343 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1346 case FTPCREATEDIRECTORYA:
1347 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1348 (LPCSTR)workRequest.LPSZDIRECTORY);
1349 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1352 case FTPFINDFIRSTFILEA:
1353 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
1354 (LPCSTR)workRequest.LPSZSEARCHFILE,
1355 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
1356 workRequest.DWCONTEXT);
1357 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
1360 case FTPGETCURRENTDIRECTORYA:
1361 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1362 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
1366 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
1367 (LPCSTR)workRequest.LPSZFILENAME,
1368 workRequest.FDWACCESS,
1369 workRequest.DWFLAGS,
1370 workRequest.DWCONTEXT);
1371 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1375 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
1376 (LPCSTR)workRequest.LPSZREMOTEFILE,
1377 (LPCSTR)workRequest.LPSZNEWFILE,
1378 (BOOL)workRequest.FFAILIFEXISTS,
1379 workRequest.DWLOCALFLAGSATTRIBUTE,
1380 workRequest.DWFLAGS,
1381 workRequest.DWCONTEXT);
1382 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
1383 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
1386 case FTPDELETEFILEA:
1387 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
1388 (LPCSTR)workRequest.LPSZFILENAME);
1389 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1392 case FTPREMOVEDIRECTORYA:
1393 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1394 (LPCSTR)workRequest.LPSZDIRECTORY);
1395 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1398 case FTPRENAMEFILEA:
1399 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
1400 (LPCSTR)workRequest.LPSZSRCFILE,
1401 (LPCSTR)workRequest.LPSZDESTFILE);
1402 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
1403 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
1406 case INTERNETFINDNEXTA:
1407 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
1408 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
1411 case HTTPSENDREQUESTA:
1412 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
1413 (LPCSTR)workRequest.LPSZHEADER,
1414 workRequest.DWHEADERLENGTH,
1415 (LPVOID)workRequest.LPOPTIONAL,
1416 workRequest.DWOPTIONALLENGTH);
1417 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
1420 case HTTPOPENREQUESTA:
1421 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
1422 (LPCSTR)workRequest.LPSZVERB,
1423 (LPCSTR)workRequest.LPSZOBJECTNAME,
1424 (LPCSTR)workRequest.LPSZVERSION,
1425 (LPCSTR)workRequest.LPSZREFERRER,
1426 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
1427 workRequest.DWFLAGS,
1428 workRequest.DWCONTEXT);
1429 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
1430 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
1431 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
1432 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
1440 /***********************************************************************
1441 * INTERNET_GetResponseBuffer
1446 LPSTR INTERNET_GetResponseBuffer()
1448 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1449 return lpwite->response;
1453 /***********************************************************************
1454 * INTERNET_GetNextLine (internal)
1456 * Parse next line in directory string listing
1459 * Pointer to beginning of next line
1464 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
1468 BOOL bSuccess = FALSE;
1474 FD_SET(nSocket, &infd);
1475 tv.tv_sec=RESPONSE_TIMEOUT;
1478 while (nRecv < *dwBuffer)
1480 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
1482 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
1484 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1488 if (lpszBuffer[nRecv] == '\n')
1493 if (lpszBuffer[nRecv] != '\r')
1498 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
1506 lpszBuffer[nRecv] = '\0';
1507 *dwBuffer = nRecv - 1;
1508 TRACE(":%d %s\n", nRecv, lpszBuffer);