4 * Copyright 1999 Corel Corporation
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_SOCKET_H
15 # include <sys/socket.h>
25 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(wininet);
34 #define MAX_IDLE_WORKER 1000*60*1
35 #define MAX_WORKER_THREADS 10
36 #define RESPONSE_TIMEOUT 30
38 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
39 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
44 CHAR response[MAX_REPLY_LEN];
45 } WITHREADERROR, *LPWITHREADERROR;
47 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
48 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
49 VOID INTERNET_ExecuteWork();
51 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
53 DWORD dwNumIdleThreads;
54 HANDLE hEventArray[2];
55 #define hQuitEvent hEventArray[0]
56 #define hWorkEvent hEventArray[1]
57 CRITICAL_SECTION csQueue;
58 LPWORKREQUEST lpHeadWorkQueue;
59 LPWORKREQUEST lpWorkQueueTail;
61 /***********************************************************************
62 * WININET_LibMain [Internal] Initializes the internal 'WININET.DLL'.
65 * hinstDLL [I] handle to the DLL's instance
67 * lpvReserved [I] reserved, must be NULL
75 WININET_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
77 TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
80 case DLL_PROCESS_ATTACH:
82 g_dwTlsErrIndex = TlsAlloc();
84 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
87 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
88 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
89 InitializeCriticalSection(&csQueue);
94 case DLL_THREAD_ATTACH:
96 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
100 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
104 case DLL_THREAD_DETACH:
105 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
107 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
109 HeapFree(GetProcessHeap(), 0, lpwite);
113 case DLL_PROCESS_DETACH:
115 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
117 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
118 TlsFree(g_dwTlsErrIndex);
121 SetEvent(hQuitEvent);
123 CloseHandle(hQuitEvent);
124 CloseHandle(hWorkEvent);
125 DeleteCriticalSection(&csQueue);
133 /***********************************************************************
134 * InternetOpenA (WININET.@)
136 * Per-application initialization of wininet
139 * HINTERNET on success
143 INTERNETAPI HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent,
144 DWORD dwAccessType, LPCSTR lpszProxy,
145 LPCSTR lpszProxyBypass, DWORD dwFlags)
147 LPWININETAPPINFOA lpwai = NULL;
151 /* Clear any error information */
152 INTERNET_SetLastError(0);
154 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
156 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
159 memset(lpwai, 0, sizeof(WININETAPPINFOA));
160 lpwai->hdr.htype = WH_HINIT;
161 lpwai->hdr.lpwhparent = NULL;
162 lpwai->hdr.dwFlags = dwFlags;
163 if (NULL != lpszAgent)
165 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
166 strcpy( lpwai->lpszAgent, lpszAgent );
168 if (NULL != lpszProxy)
170 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
171 strcpy( lpwai->lpszProxy, lpszProxy );
173 if (NULL != lpszProxyBypass)
175 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
176 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
178 lpwai->dwAccessType = dwAccessType;
181 return (HINTERNET)lpwai;
185 /***********************************************************************
186 * InternetGetLastResponseInfoA (WININET.@)
188 * Return last wininet error description on the calling thread
191 * TRUE on success of writting to buffer
195 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
196 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
198 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
202 *lpdwError = lpwite->dwError;
205 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
206 *lpdwBufferLength = strlen(lpszBuffer);
209 *lpdwBufferLength = 0;
215 /***********************************************************************
216 * InternetGetConnectedState (WININET.@)
218 * Return connected state
222 * if lpdwStatus is not null, return the status (off line,
223 * modem, lan...) in it.
224 * FALSE if not connected
226 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
233 /***********************************************************************
234 * InternetConnectA (WININET.@)
236 * Open a ftp, gopher or http session
239 * HINTERNET a session handle on success
243 INTERNETAPI HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
244 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
245 LPCSTR lpszUserName, LPCSTR lpszPassword,
246 DWORD dwService, DWORD dwFlags, DWORD dwContext)
248 HINTERNET rc = (HINTERNET) NULL;
252 /* Clear any error information */
253 INTERNET_SetLastError(0);
257 case INTERNET_SERVICE_FTP:
258 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
259 lpszUserName, lpszPassword, dwFlags, dwContext);
262 case INTERNET_SERVICE_HTTP:
263 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
264 lpszUserName, lpszPassword, dwFlags, dwContext);
267 case INTERNET_SERVICE_GOPHER:
275 /***********************************************************************
276 * InternetFindNextFileA (WININET.@)
278 * Continues a file search from a previous call to FindFirstFile
285 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
287 LPWININETAPPINFOA hIC = NULL;
288 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
292 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
294 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
298 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
299 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
301 WORKREQUEST workRequest;
303 workRequest.asyncall = INTERNETFINDNEXTA;
304 workRequest.HFTPSESSION = (DWORD)hFind;
305 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
307 return INTERNET_AsyncCall(&workRequest);
311 return INTERNET_FindNextFileA(hFind, lpvFindData);
315 /***********************************************************************
316 * INTERNET_FindNextFileA (Internal)
318 * Continues a file search from a previous call to FindFirstFile
325 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
327 BOOL bSuccess = TRUE;
328 LPWININETAPPINFOA hIC = NULL;
329 LPWIN32_FIND_DATAA lpFindFileData;
330 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
334 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
336 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
340 /* Clear any error information */
341 INTERNET_SetLastError(0);
343 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
345 FIXME("Only FTP find next supported\n");
346 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
350 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
352 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
353 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
355 if (lpwh->index >= lpwh->size)
357 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
362 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
365 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
369 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
370 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC && hIC->lpfnStatusCB)
372 INTERNET_ASYNC_RESULT iar;
374 iar.dwResult = (DWORD)bSuccess;
375 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
377 hIC->lpfnStatusCB(hFind, lpwh->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
378 &iar, sizeof(INTERNET_ASYNC_RESULT));
385 /***********************************************************************
386 * INTERNET_CloseHandle (internal)
388 * Close internet handle
394 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
396 if (lpwai->lpszAgent)
397 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
399 if (lpwai->lpszProxy)
400 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
402 if (lpwai->lpszProxyBypass)
403 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
405 HeapFree(GetProcessHeap(), 0, lpwai);
409 /***********************************************************************
410 * InternetCloseHandle (WININET.@)
412 * Generic close handle function
419 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
422 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
428 /* Clear any error information */
429 INTERNET_SetLastError(0);
434 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
438 case WH_HHTTPSESSION:
439 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
444 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
449 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
453 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
464 /***********************************************************************
465 * SetUrlComponentValue (Internal)
467 * Helper function for InternetCrackUrlA
474 BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen, LPCSTR lpszStart, INT len)
476 TRACE("%s (%d)\n", lpszStart, len);
478 if (*dwComponentLen != 0)
480 if (*lppszComponent == NULL)
482 *lppszComponent = (LPSTR)lpszStart;
483 *dwComponentLen = len;
487 INT ncpylen = min((*dwComponentLen)-1, len);
488 strncpy(*lppszComponent, lpszStart, ncpylen);
489 (*lppszComponent)[ncpylen] = '\0';
490 *dwComponentLen = ncpylen;
498 /***********************************************************************
499 * InternetCrackUrlA (WININET.@)
501 * Break up URL into its components
503 * TODO: Hadnle dwFlags
510 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
511 LPURL_COMPONENTSA lpUrlComponents)
515 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
518 LPSTR lpszParam = NULL;
519 BOOL bIsAbsolute = FALSE;
520 LPSTR lpszap = (char*)lpszUrl;
525 /* Determine if the URI is absolute. */
526 while (*lpszap != '\0')
528 if (isalnum(*lpszap))
533 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
540 lpszcp = (LPSTR)lpszUrl; /* Relative url */
547 lpszParam = strpbrk(lpszap, ";?");
548 if (lpszParam != NULL)
550 if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
551 &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
557 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
561 /* Get scheme first. */
562 lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
563 if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
564 &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
567 /* Eat ':' in protocol. */
570 /* Skip over slashes. */
582 lpszNetLoc = strpbrk(lpszcp, "/");
586 lpszNetLoc = min(lpszNetLoc, lpszParam);
588 lpszNetLoc = lpszParam;
590 else if (!lpszNetLoc)
591 lpszNetLoc = lpszcp + strlen(lpszcp);
599 /* [<user>[<:password>]@]<host>[:<port>] */
600 /* First find the user and password if they exist */
602 lpszHost = strchr(lpszcp, '@');
603 if (lpszHost == NULL || lpszHost > lpszNetLoc)
605 /* username and password not specified. */
606 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
607 &lpUrlComponents->dwUserNameLength, NULL, 0);
608 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
609 &lpUrlComponents->dwPasswordLength, NULL, 0);
611 else /* Parse out username and password */
613 LPSTR lpszUser = lpszcp;
614 LPSTR lpszPasswd = lpszHost;
616 while (lpszcp < lpszHost)
624 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
625 &lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);
627 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
628 &lpUrlComponents->dwPasswordLength,
629 lpszPasswd == lpszHost ? NULL : ++lpszPasswd,
630 lpszHost - lpszPasswd);
632 lpszcp++; /* Advance to beginning of host */
635 /* Parse <host><:port> */
638 lpszPort = lpszNetLoc;
640 while (lpszcp < lpszNetLoc)
648 SetUrlComponentValue(&lpUrlComponents->lpszHostName,
649 &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
651 if (lpszPort != lpszNetLoc)
652 lpUrlComponents->nPort = atoi(++lpszPort);
656 /* Here lpszcp points to:
658 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
659 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
661 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
665 /* Only truncate the parameter list if it's already been saved
666 * in lpUrlComponents->lpszExtraInfo.
668 if (lpszParam && lpUrlComponents->dwExtraInfoLength)
669 len = lpszParam - lpszcp;
672 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
673 * newlines if necessary.
675 LPSTR lpsznewline = strchr (lpszcp, '\n');
676 if (lpsznewline != NULL)
677 len = lpsznewline - lpszcp;
679 len = strlen(lpszcp);
682 if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
683 &lpUrlComponents->dwUrlPathLength, lpszcp, len))
688 lpUrlComponents->dwUrlPathLength = 0;
691 TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
692 lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);
698 /***********************************************************************
699 * InternetAttemptConnect (WININET.@)
701 * Attempt to make a connection to the internet
704 * ERROR_SUCCESS on success
705 * Error value on failure
708 INTERNETAPI DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
711 return ERROR_SUCCESS;
715 /***********************************************************************
716 * InternetCanonicalizeUrlA (WININET.@)
718 * Escape unsafe characters and spaces
725 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
726 LPDWORD lpdwBufferLength, DWORD dwFlags)
729 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
730 lpdwBufferLength, dwFlags);
732 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
733 dwFlags ^= ICU_NO_ENCODE;
735 dwFlags |= 0x80000000; /* Don't know what this means */
737 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
739 return (hr == S_OK) ? TRUE : FALSE;
742 /***********************************************************************
743 * InternetSetStatusCallback (WININET.@)
745 * Sets up a callback function which is called as progress is made
746 * during an operation.
749 * Previous callback or NULL on success
750 * INTERNET_INVALID_STATUS_CALLBACK on failure
753 INTERNETAPI INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
754 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
756 INTERNET_STATUS_CALLBACK retVal;
757 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
759 TRACE("0x%08lx\n", (ULONG)hInternet);
760 if (lpwai->hdr.htype != WH_HINIT)
761 return INTERNET_INVALID_STATUS_CALLBACK;
763 retVal = lpwai->lpfnStatusCB;
764 lpwai->lpfnStatusCB = lpfnIntCB;
770 /***********************************************************************
771 * InternetWriteFile (WININET.@)
773 * Write data to an open internet file
780 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
781 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
784 int nSocket = INVALID_SOCKET;
785 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
794 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
798 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
805 if (INVALID_SOCKET != nSocket)
807 *lpdwNumOfBytesWritten = INTERNET_WriteDataToStream(nSocket, lpBuffer, dwNumOfBytesToWrite);
808 if (*lpdwNumOfBytesWritten < 0)
809 *lpdwNumOfBytesWritten = 0;
818 /***********************************************************************
819 * InternetReadFile (WININET.@)
821 * Read data from an open internet file
828 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
829 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
832 int nSocket = INVALID_SOCKET;
833 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
842 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
846 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
853 if (INVALID_SOCKET != nSocket)
855 *dwNumOfBytesRead = INTERNET_ReadDataFromStream(nSocket, lpBuffer, dwNumOfBytesToRead);
856 if (*dwNumOfBytesRead < 0)
857 *dwNumOfBytesRead = 0;
866 /***********************************************************************
867 * InternetQueryOptionA (WININET.@)
869 * Queries an options on the specified handle
876 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
877 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
879 LPWININETHANDLEHEADER lpwhh;
880 BOOL bSuccess = FALSE;
882 TRACE("0x%08lx\n", dwOption);
884 if (NULL == hInternet)
886 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
890 lpwhh = (LPWININETHANDLEHEADER) hInternet;
894 case INTERNET_OPTION_HANDLE_TYPE:
896 ULONG type = lpwhh->htype;
897 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
899 if (*lpdwBufferLength < sizeof(ULONG))
900 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
903 memcpy(lpBuffer, &type, sizeof(ULONG));
904 *lpdwBufferLength = sizeof(ULONG);
920 /***********************************************************************
921 * InternetGetCookieA (WININET.@)
923 * Retrieve cookie from the specified url
930 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
931 LPSTR lpCookieData, LPDWORD lpdwSize)
938 /***********************************************************************
939 * GetInternetScheme (internal)
945 * INTERNET_SCHEME_UNKNOWN on failure
948 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
951 return INTERNET_SCHEME_UNKNOWN;
953 if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
954 return INTERNET_SCHEME_FTP;
955 else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
956 return INTERNET_SCHEME_GOPHER;
957 else if (!strncasecmp("http", lpszScheme, nMaxCmp))
958 return INTERNET_SCHEME_HTTP;
959 else if (!strncasecmp("https", lpszScheme, nMaxCmp))
960 return INTERNET_SCHEME_HTTPS;
961 else if (!strncasecmp("file", lpszScheme, nMaxCmp))
962 return INTERNET_SCHEME_FILE;
963 else if (!strncasecmp("news", lpszScheme, nMaxCmp))
964 return INTERNET_SCHEME_NEWS;
965 else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
966 return INTERNET_SCHEME_MAILTO;
968 return INTERNET_SCHEME_UNKNOWN;
971 /***********************************************************************
972 * InternetCheckConnectionA (WININET.@)
974 * Pings a requested host to check internet connection
978 * TRUE on success and FALSE on failure. if a failures then
979 * ERROR_NOT_CONNECTED is places into GetLastError
982 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
985 * this is a kludge which runs the resident ping program and reads the output.
987 * Anyone have a better idea?
996 * Crack or set the Address
1001 * According to the doc we are supost to use the ip for the next
1002 * server in the WnInet internal server database. I have
1003 * no idea what that is or how to get it.
1005 * So someone needs to implement this.
1007 FIXME("Unimplemented with URL of NULL\n");
1012 URL_COMPONENTSA componets;
1014 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1015 componets.lpszHostName = (LPSTR)&host;
1016 componets.dwHostNameLength = 1024;
1018 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1021 TRACE("host name : %s\n",componets.lpszHostName);
1025 * Build our ping command
1027 strcpy(command,"ping -w 1 ");
1028 strcat(command,host);
1029 strcat(command," >/dev/null 2>/dev/null");
1031 TRACE("Ping command is : %s\n",command);
1033 status = system(command);
1035 TRACE("Ping returned a code of %i \n",status);
1037 /* Ping return code of 0 indicates success */
1044 SetLastError(ERROR_NOT_CONNECTED);
1051 /***********************************************************************
1052 * INTERNET_WriteDataToStream (internal)
1054 * Send data to server
1058 * number of characters sent on success
1061 int INTERNET_WriteDataToStream(int nDataSocket, LPCVOID Buffer, DWORD BytesToWrite)
1063 if (INVALID_SOCKET == nDataSocket)
1064 return SOCKET_ERROR;
1066 return send(nDataSocket, Buffer, BytesToWrite, 0);
1070 /***********************************************************************
1071 * INTERNET_ReadDataFromStream (internal)
1073 * Read data from http server
1077 * number of characters sent on success
1080 int INTERNET_ReadDataFromStream(int nDataSocket, LPVOID Buffer, DWORD BytesToRead)
1082 if (INVALID_SOCKET == nDataSocket)
1083 return SOCKET_ERROR;
1085 return recv(nDataSocket, Buffer, BytesToRead, 0);
1089 /***********************************************************************
1090 * INTERNET_SetLastError (internal)
1092 * Set last thread specific error
1097 void INTERNET_SetLastError(DWORD dwError)
1099 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1101 SetLastError(dwError);
1102 lpwite->dwError = dwError;
1106 /***********************************************************************
1107 * INTERNET_GetLastError (internal)
1109 * Get last thread specific error
1114 DWORD INTERNET_GetLastError()
1116 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1117 return lpwite->dwError;
1121 /***********************************************************************
1122 * INTERNET_WorkerThreadFunc (internal)
1124 * Worker thread execution function
1129 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1135 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1137 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1138 INTERNET_ExecuteWork();
1142 InterlockedIncrement(&dwNumIdleThreads);
1145 InterlockedDecrement(&dwNumIdleThreads);
1146 InterlockedDecrement(&dwNumThreads);
1147 TRACE("Worker thread exiting\n");
1152 /***********************************************************************
1153 * INTERNET_InsertWorkRequest (internal)
1155 * Insert work request into queue
1160 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1162 BOOL bSuccess = FALSE;
1163 LPWORKREQUEST lpNewRequest;
1167 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1170 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1171 lpNewRequest->prev = NULL;
1173 EnterCriticalSection(&csQueue);
1175 lpNewRequest->next = lpWorkQueueTail;
1176 if (lpWorkQueueTail)
1177 lpWorkQueueTail->prev = lpNewRequest;
1178 lpWorkQueueTail = lpNewRequest;
1179 if (!lpHeadWorkQueue)
1180 lpHeadWorkQueue = lpWorkQueueTail;
1182 LeaveCriticalSection(&csQueue);
1191 /***********************************************************************
1192 * INTERNET_GetWorkRequest (internal)
1194 * Retrieves work request from queue
1199 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
1201 BOOL bSuccess = FALSE;
1202 LPWORKREQUEST lpRequest = NULL;
1206 EnterCriticalSection(&csQueue);
1208 if (lpHeadWorkQueue)
1210 lpRequest = lpHeadWorkQueue;
1211 lpHeadWorkQueue = lpHeadWorkQueue->prev;
1212 if (lpRequest == lpWorkQueueTail)
1213 lpWorkQueueTail = lpHeadWorkQueue;
1216 LeaveCriticalSection(&csQueue);
1220 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
1221 HeapFree(GetProcessHeap(), 0, lpRequest);
1229 /***********************************************************************
1230 * INTERNET_AsyncCall (internal)
1232 * Retrieves work request from queue
1237 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
1241 BOOL bSuccess = FALSE;
1245 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
1247 InterlockedIncrement(&dwNumIdleThreads);
1249 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
1250 !(hThread = CreateThread(NULL, 0,
1251 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
1253 InterlockedDecrement(&dwNumThreads);
1254 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
1258 TRACE("Created new thread\n");
1262 INTERNET_InsertWorkRequest(lpWorkRequest);
1263 SetEvent(hWorkEvent);
1271 /***********************************************************************
1272 * INTERNET_ExecuteWork (internal)
1277 VOID INTERNET_ExecuteWork()
1279 WORKREQUEST workRequest;
1283 if (INTERNET_GetWorkRequest(&workRequest))
1285 switch (workRequest.asyncall)
1288 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
1289 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
1290 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
1291 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
1294 case FTPSETCURRENTDIRECTORYA:
1295 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1296 (LPCSTR)workRequest.LPSZDIRECTORY);
1297 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1300 case FTPCREATEDIRECTORYA:
1301 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1302 (LPCSTR)workRequest.LPSZDIRECTORY);
1303 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1306 case FTPFINDFIRSTFILEA:
1307 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
1308 (LPCSTR)workRequest.LPSZSEARCHFILE,
1309 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
1310 workRequest.DWCONTEXT);
1311 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
1314 case FTPGETCURRENTDIRECTORYA:
1315 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1316 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
1320 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
1321 (LPCSTR)workRequest.LPSZFILENAME,
1322 workRequest.FDWACCESS,
1323 workRequest.DWFLAGS,
1324 workRequest.DWCONTEXT);
1325 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1329 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
1330 (LPCSTR)workRequest.LPSZREMOTEFILE,
1331 (LPCSTR)workRequest.LPSZNEWFILE,
1332 (BOOL)workRequest.FFAILIFEXISTS,
1333 workRequest.DWLOCALFLAGSATTRIBUTE,
1334 workRequest.DWFLAGS,
1335 workRequest.DWCONTEXT);
1336 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
1337 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
1340 case FTPDELETEFILEA:
1341 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
1342 (LPCSTR)workRequest.LPSZFILENAME);
1343 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1346 case FTPREMOVEDIRECTORYA:
1347 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1348 (LPCSTR)workRequest.LPSZDIRECTORY);
1349 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1352 case FTPRENAMEFILEA:
1353 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
1354 (LPCSTR)workRequest.LPSZSRCFILE,
1355 (LPCSTR)workRequest.LPSZDESTFILE);
1356 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
1357 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
1360 case INTERNETFINDNEXTA:
1361 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
1362 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
1365 case HTTPSENDREQUESTA:
1366 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
1367 (LPCSTR)workRequest.LPSZHEADER,
1368 workRequest.DWHEADERLENGTH,
1369 (LPVOID)workRequest.LPOPTIONAL,
1370 workRequest.DWOPTIONALLENGTH);
1371 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
1374 case HTTPOPENREQUESTA:
1375 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
1376 (LPCSTR)workRequest.LPSZVERB,
1377 (LPCSTR)workRequest.LPSZOBJECTNAME,
1378 (LPCSTR)workRequest.LPSZVERSION,
1379 (LPCSTR)workRequest.LPSZREFERRER,
1380 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
1381 workRequest.DWFLAGS,
1382 workRequest.DWCONTEXT);
1383 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
1384 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
1385 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
1386 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
1394 /***********************************************************************
1395 * INTERNET_GetResponseBuffer
1400 LPSTR INTERNET_GetResponseBuffer()
1402 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1403 return lpwite->response;
1407 /***********************************************************************
1408 * INTERNET_GetNextLine (internal)
1410 * Parse next line in directory string listing
1413 * Pointer to begining of next line
1418 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
1422 BOOL bSuccess = FALSE;
1428 FD_SET(nSocket, &infd);
1429 tv.tv_sec=RESPONSE_TIMEOUT;
1432 while (nRecv < *dwBuffer)
1434 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
1436 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
1438 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1442 if (lpszBuffer[nRecv] == '\n')
1447 if (lpszBuffer[nRecv] != '\r')
1452 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
1460 lpszBuffer[nRecv] = '\0';
1461 *dwBuffer = nRecv - 1;
1462 TRACE(":%d %s\n", nRecv, lpszBuffer);