Add a counter to keep track of the number of outstanding async
[wine/wine-kai.git] / dlls / wininet / internet.c
blob2a31c3a2e84406546d9601466b7a00f58036cae3
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
7 * Ulrich Czekalla
8 * Aric Stewart
10 * Copyright 2002 Jaco Greeff
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config.h"
29 #define MAXHOSTNAME 100 /* from http.c */
31 #include <string.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
36 #endif
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include <stdlib.h>
41 #include <ctype.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winreg.h"
49 #include "wininet.h"
50 #include "winnls.h"
51 #include "wine/debug.h"
52 #include "winerror.h"
53 #define NO_SHLWAPI_STREAM
54 #include "shlwapi.h"
56 #include "wine/exception.h"
57 #include "msvcrt/excpt.h"
59 #include "internet.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
63 #define MAX_IDLE_WORKER 1000*60*1
64 #define MAX_WORKER_THREADS 10
65 #define RESPONSE_TIMEOUT 30
67 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
68 (LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
70 /* filter for page-fault exceptions */
71 static WINE_EXCEPTION_FILTER(page_fault)
73 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
74 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
75 return EXCEPTION_EXECUTE_HANDLER;
76 return EXCEPTION_CONTINUE_SEARCH;
79 typedef struct
81 DWORD dwError;
82 CHAR response[MAX_REPLY_LEN];
83 } WITHREADERROR, *LPWITHREADERROR;
85 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
86 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
87 VOID INTERNET_ExecuteWork();
89 DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
90 DWORD dwNumThreads;
91 DWORD dwNumIdleThreads;
92 DWORD dwNumJobs;
93 HANDLE hEventArray[2];
94 #define hQuitEvent hEventArray[0]
95 #define hWorkEvent hEventArray[1]
96 CRITICAL_SECTION csQueue;
97 LPWORKREQUEST lpHeadWorkQueue;
98 LPWORKREQUEST lpWorkQueueTail;
100 /***********************************************************************
101 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
103 * PARAMS
104 * hinstDLL [I] handle to the DLL's instance
105 * fdwReason [I]
106 * lpvReserved [I] reserved, must be NULL
108 * RETURNS
109 * Success: TRUE
110 * Failure: FALSE
113 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
115 TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
117 switch (fdwReason) {
118 case DLL_PROCESS_ATTACH:
120 g_dwTlsErrIndex = TlsAlloc();
122 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
123 return FALSE;
125 hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
126 hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
127 InitializeCriticalSection(&csQueue);
129 dwNumThreads = 0;
130 dwNumIdleThreads = 0;
131 dwNumJobs = 0;
133 case DLL_THREAD_ATTACH:
135 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
136 if (NULL == lpwite)
137 return FALSE;
139 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
141 break;
143 case DLL_THREAD_DETACH:
144 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
146 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
147 if (lpwite)
148 HeapFree(GetProcessHeap(), 0, lpwite);
150 break;
152 case DLL_PROCESS_DETACH:
154 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
156 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
157 TlsFree(g_dwTlsErrIndex);
160 SetEvent(hQuitEvent);
162 CloseHandle(hQuitEvent);
163 CloseHandle(hWorkEvent);
164 DeleteCriticalSection(&csQueue);
165 break;
168 return TRUE;
172 /***********************************************************************
173 * InternetInitializeAutoProxyDll (WININET.@)
175 * Setup the internal proxy
177 * PARAMETERS
178 * dwReserved
180 * RETURNS
181 * FALSE on failure
184 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
186 FIXME("STUB\n");
187 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
188 return FALSE;
192 /***********************************************************************
193 * InternetOpenA (WININET.@)
195 * Per-application initialization of wininet
197 * RETURNS
198 * HINTERNET on success
199 * NULL on failure
202 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
203 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
205 LPWININETAPPINFOA lpwai = NULL;
207 TRACE("\n");
209 /* Clear any error information */
210 INTERNET_SetLastError(0);
212 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
213 if (NULL == lpwai)
214 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
215 else
217 memset(lpwai, 0, sizeof(WININETAPPINFOA));
218 lpwai->hdr.htype = WH_HINIT;
219 lpwai->hdr.lpwhparent = NULL;
220 lpwai->hdr.dwFlags = dwFlags;
221 if (NULL != lpszAgent)
223 if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
224 strcpy( lpwai->lpszAgent, lpszAgent );
226 if (NULL != lpszProxy)
228 if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
229 strcpy( lpwai->lpszProxy, lpszProxy );
231 if (NULL != lpszProxyBypass)
233 if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
234 strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
236 lpwai->dwAccessType = dwAccessType;
239 return (HINTERNET)lpwai;
243 /***********************************************************************
244 * InternetOpenW (WININET.@)
246 * Per-application initialization of wininet
248 * RETURNS
249 * HINTERNET on success
250 * NULL on failure
253 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
254 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
256 HINTERNET rc = (HINTERNET)NULL;
257 INT lenAgent = lstrlenW(lpszAgent)+1;
258 INT lenProxy = lstrlenW(lpszProxy)+1;
259 INT lenBypass = lstrlenW(lpszProxyBypass)+1;
260 CHAR *szAgent = (CHAR *)malloc(lenAgent*sizeof(CHAR));
261 CHAR *szProxy = (CHAR *)malloc(lenProxy*sizeof(CHAR));
262 CHAR *szBypass = (CHAR *)malloc(lenBypass*sizeof(CHAR));
264 if (!szAgent || !szProxy || !szBypass)
266 if (szAgent)
267 free(szAgent);
268 if (szProxy)
269 free(szProxy);
270 if (szBypass)
271 free(szBypass);
272 return (HINTERNET)NULL;
275 WideCharToMultiByte(CP_ACP, -1, lpszAgent, -1, szAgent, lenAgent,
276 NULL, NULL);
277 WideCharToMultiByte(CP_ACP, -1, lpszProxy, -1, szProxy, lenProxy,
278 NULL, NULL);
279 WideCharToMultiByte(CP_ACP, -1, lpszProxyBypass, -1, szBypass, lenBypass,
280 NULL, NULL);
282 rc = InternetOpenA(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
284 free(szAgent);
285 free(szProxy);
286 free(szBypass);
288 return rc;
291 /***********************************************************************
292 * InternetGetLastResponseInfoA (WININET.@)
294 * Return last wininet error description on the calling thread
296 * RETURNS
297 * TRUE on success of writting to buffer
298 * FALSE on failure
301 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
302 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
304 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
306 TRACE("\n");
308 *lpdwError = lpwite->dwError;
309 if (lpwite->dwError)
311 strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
312 *lpdwBufferLength = strlen(lpszBuffer);
314 else
315 *lpdwBufferLength = 0;
317 return TRUE;
321 /***********************************************************************
322 * InternetGetConnectedState (WININET.@)
324 * Return connected state
326 * RETURNS
327 * TRUE if connected
328 * if lpdwStatus is not null, return the status (off line,
329 * modem, lan...) in it.
330 * FALSE if not connected
332 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
334 if (lpdwStatus) {
335 FIXME("always returning LAN connection.\n");
336 *lpdwStatus = INTERNET_CONNECTION_LAN;
338 return TRUE;
342 /***********************************************************************
343 * InternetConnectA (WININET.@)
345 * Open a ftp, gopher or http session
347 * RETURNS
348 * HINTERNET a session handle on success
349 * NULL on failure
352 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
353 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
354 LPCSTR lpszUserName, LPCSTR lpszPassword,
355 DWORD dwService, DWORD dwFlags, DWORD dwContext)
357 HINTERNET rc = (HINTERNET) NULL;
359 TRACE("ServerPort %i\n",nServerPort);
361 /* Clear any error information */
362 INTERNET_SetLastError(0);
364 switch (dwService)
366 case INTERNET_SERVICE_FTP:
367 rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
368 lpszUserName, lpszPassword, dwFlags, dwContext);
369 break;
371 case INTERNET_SERVICE_HTTP:
372 rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
373 lpszUserName, lpszPassword, dwFlags, dwContext);
374 break;
376 case INTERNET_SERVICE_GOPHER:
377 default:
378 break;
381 return rc;
385 /***********************************************************************
386 * InternetConnectW (WININET.@)
388 * Open a ftp, gopher or http session
390 * RETURNS
391 * HINTERNET a session handle on success
392 * NULL on failure
395 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
396 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
397 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
398 DWORD dwService, DWORD dwFlags, DWORD dwContext)
400 HINTERNET rc = (HINTERNET)NULL;
401 INT lenServer = lstrlenW(lpszServerName)+1;
402 INT lenUser = lstrlenW(lpszUserName)+1;
403 INT lenPass = lstrlenW(lpszPassword)+1;
404 CHAR *szServerName = (CHAR *)malloc(lenServer*sizeof(CHAR));
405 CHAR *szUserName = (CHAR *)malloc(lenUser*sizeof(CHAR));
406 CHAR *szPassword = (CHAR *)malloc(lenPass*sizeof(CHAR));
408 if (!szServerName || !szUserName || !szPassword)
410 if (szServerName)
411 free(szServerName);
412 if (szUserName)
413 free(szUserName);
414 if (szPassword)
415 free(szPassword);
416 return (HINTERNET)NULL;
419 WideCharToMultiByte(CP_ACP, -1, lpszServerName, -1, szServerName, lenServer,
420 NULL, NULL);
421 WideCharToMultiByte(CP_ACP, -1, lpszUserName, -1, szUserName, lenUser,
422 NULL, NULL);
423 WideCharToMultiByte(CP_ACP, -1, lpszPassword, -1, szPassword, lenPass,
424 NULL, NULL);
426 rc = InternetConnectA(hInternet, szServerName, nServerPort,
427 szUserName, szPassword, dwService, dwFlags, dwContext);
429 free(szServerName);
430 free(szUserName);
431 free(szPassword);
432 return rc;
436 /***********************************************************************
437 * InternetFindNextFileA (WININET.@)
439 * Continues a file search from a previous call to FindFirstFile
441 * RETURNS
442 * TRUE on success
443 * FALSE on failure
446 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
448 LPWININETAPPINFOA hIC = NULL;
449 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
451 TRACE("\n");
453 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
455 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
456 return FALSE;
459 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
460 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
462 WORKREQUEST workRequest;
464 workRequest.asyncall = INTERNETFINDNEXTA;
465 workRequest.HFTPSESSION = (DWORD)hFind;
466 workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;
468 return INTERNET_AsyncCall(&workRequest);
470 else
472 return INTERNET_FindNextFileA(hFind, lpvFindData);
476 /***********************************************************************
477 * INTERNET_FindNextFileA (Internal)
479 * Continues a file search from a previous call to FindFirstFile
481 * RETURNS
482 * TRUE on success
483 * FALSE on failure
486 BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
488 BOOL bSuccess = TRUE;
489 LPWININETAPPINFOA hIC = NULL;
490 LPWIN32_FIND_DATAA lpFindFileData;
491 LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;
493 TRACE("\n");
495 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
497 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
498 return FALSE;
501 /* Clear any error information */
502 INTERNET_SetLastError(0);
504 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
506 FIXME("Only FTP find next supported\n");
507 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
508 return FALSE;
511 TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);
513 lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
514 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
516 if (lpwh->index >= lpwh->size)
518 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
519 bSuccess = FALSE;
520 goto lend;
523 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
524 lpwh->index++;
526 TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);
528 lend:
530 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
531 if (hIC->lpfnStatusCB)
533 INTERNET_ASYNC_RESULT iar;
535 iar.dwResult = (DWORD)bSuccess;
536 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
537 INTERNET_GetLastError();
539 SendAsyncCallback(hIC, hFind, lpwh->hdr.dwContext,
540 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
541 sizeof(INTERNET_ASYNC_RESULT));
544 return bSuccess;
548 /***********************************************************************
549 * INTERNET_CloseHandle (internal)
551 * Close internet handle
553 * RETURNS
554 * Void
557 VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
559 TRACE("%p\n",lpwai);
561 SendAsyncCallback(lpwai, lpwai, lpwai->hdr.dwContext,
562 INTERNET_STATUS_HANDLE_CLOSING, lpwai,
563 sizeof(HINTERNET));
565 if (lpwai->lpszAgent)
566 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
568 if (lpwai->lpszProxy)
569 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
571 if (lpwai->lpszProxyBypass)
572 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
574 HeapFree(GetProcessHeap(), 0, lpwai);
578 /***********************************************************************
579 * InternetCloseHandle (WININET.@)
581 * Generic close handle function
583 * RETURNS
584 * TRUE on success
585 * FALSE on failure
588 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
590 BOOL retval;
591 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;
593 TRACE("%p\n",hInternet);
594 if (NULL == lpwh)
595 return FALSE;
597 __TRY {
598 /* Clear any error information */
599 INTERNET_SetLastError(0);
600 retval = FALSE;
602 switch (lpwh->htype)
604 case WH_HINIT:
605 INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
606 retval = TRUE;
607 break;
609 case WH_HHTTPSESSION:
610 HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
611 retval = TRUE;
612 break;
614 case WH_HHTTPREQ:
615 HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
616 retval = TRUE;
617 break;
619 case WH_HFTPSESSION:
620 retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
621 break;
623 case WH_HFINDNEXT:
624 retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
625 break;
627 default:
628 break;
630 } __EXCEPT(page_fault) {
631 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
632 return FALSE;
634 __ENDTRY
636 return retval;
640 /***********************************************************************
641 * SetUrlComponentValue (Internal)
643 * Helper function for InternetCrackUrlA
645 * RETURNS
646 * TRUE on success
647 * FALSE on failure
650 BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
651 LPCSTR lpszStart, INT len)
653 TRACE("%s (%d)\n", lpszStart, len);
655 if (*dwComponentLen != 0)
657 if (*lppszComponent == NULL)
659 *lppszComponent = (LPSTR)lpszStart;
660 *dwComponentLen = len;
662 else
664 INT ncpylen = min((*dwComponentLen)-1, len);
665 strncpy(*lppszComponent, lpszStart, ncpylen);
666 (*lppszComponent)[ncpylen] = '\0';
667 *dwComponentLen = ncpylen;
671 return TRUE;
675 /***********************************************************************
676 * InternetCrackUrlA (WININET.@)
678 * Break up URL into its components
680 * TODO: Handle dwFlags
682 * RETURNS
683 * TRUE on success
684 * FALSE on failure
687 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
688 LPURL_COMPONENTSA lpUrlComponents)
691 * RFC 1808
692 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
695 LPSTR lpszParam = NULL;
696 BOOL bIsAbsolute = FALSE;
697 LPSTR lpszap = (char*)lpszUrl;
698 LPSTR lpszcp = NULL;
700 TRACE("\n");
702 /* Determine if the URI is absolute. */
703 while (*lpszap != '\0')
705 if (isalnum(*lpszap))
707 lpszap++;
708 continue;
710 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
712 bIsAbsolute = TRUE;
713 lpszcp = lpszap;
715 else
717 lpszcp = (LPSTR)lpszUrl; /* Relative url */
720 break;
723 /* Parse <params> */
724 lpszParam = strpbrk(lpszap, ";?");
725 if (lpszParam != NULL)
727 if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
728 &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
730 return FALSE;
734 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
736 LPSTR lpszNetLoc;
738 /* Get scheme first. */
739 lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
740 if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
741 &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
742 return FALSE;
744 /* Eat ':' in protocol. */
745 lpszcp++;
747 /* Skip over slashes. */
748 if (*lpszcp == '/')
750 lpszcp++;
751 if (*lpszcp == '/')
753 lpszcp++;
754 if (*lpszcp == '/')
755 lpszcp++;
759 lpszNetLoc = strpbrk(lpszcp, "/");
760 if (lpszParam)
762 if (lpszNetLoc)
763 lpszNetLoc = min(lpszNetLoc, lpszParam);
764 else
765 lpszNetLoc = lpszParam;
767 else if (!lpszNetLoc)
768 lpszNetLoc = lpszcp + strlen(lpszcp);
770 /* Parse net-loc */
771 if (lpszNetLoc)
773 LPSTR lpszHost;
774 LPSTR lpszPort;
776 /* [<user>[<:password>]@]<host>[:<port>] */
777 /* First find the user and password if they exist */
779 lpszHost = strchr(lpszcp, '@');
780 if (lpszHost == NULL || lpszHost > lpszNetLoc)
782 /* username and password not specified. */
783 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
784 &lpUrlComponents->dwUserNameLength, NULL, 0);
785 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
786 &lpUrlComponents->dwPasswordLength, NULL, 0);
788 else /* Parse out username and password */
790 LPSTR lpszUser = lpszcp;
791 LPSTR lpszPasswd = lpszHost;
793 while (lpszcp < lpszHost)
795 if (*lpszcp == ':')
796 lpszPasswd = lpszcp;
798 lpszcp++;
801 SetUrlComponentValue(&lpUrlComponents->lpszUserName,
802 &lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);
804 if (lpszPasswd != lpszHost)
805 lpszPasswd++;
806 SetUrlComponentValue(&lpUrlComponents->lpszPassword,
807 &lpUrlComponents->dwPasswordLength,
808 lpszPasswd == lpszHost ? NULL : lpszPasswd,
809 lpszHost - lpszPasswd);
811 lpszcp++; /* Advance to beginning of host */
814 /* Parse <host><:port> */
816 lpszHost = lpszcp;
817 lpszPort = lpszNetLoc;
819 while (lpszcp < lpszNetLoc)
821 if (*lpszcp == ':')
822 lpszPort = lpszcp;
824 lpszcp++;
827 SetUrlComponentValue(&lpUrlComponents->lpszHostName,
828 &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
830 if (lpszPort != lpszNetLoc)
831 lpUrlComponents->nPort = atoi(++lpszPort);
832 else
833 lpUrlComponents->nPort = 0;
837 /* Here lpszcp points to:
839 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
840 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
842 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
844 INT len;
846 /* Only truncate the parameter list if it's already been saved
847 * in lpUrlComponents->lpszExtraInfo.
849 if (lpszParam && lpUrlComponents->dwExtraInfoLength)
850 len = lpszParam - lpszcp;
851 else
853 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
854 * newlines if necessary.
856 LPSTR lpsznewline = strchr (lpszcp, '\n');
857 if (lpsznewline != NULL)
858 len = lpsznewline - lpszcp;
859 else
860 len = strlen(lpszcp);
863 if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
864 &lpUrlComponents->dwUrlPathLength, lpszcp, len))
865 return FALSE;
867 else
869 lpUrlComponents->dwUrlPathLength = 0;
872 TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
873 lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);
875 return TRUE;
879 /***********************************************************************
880 * GetUrlCacheEntryInfoA (WININET.@)
883 BOOL WINAPI GetUrlCacheEntryInfoA(LPCSTR lpszUrl,
884 LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntry,
885 LPDWORD lpCacheEntrySize)
887 FIXME("stub\n");
888 return FALSE;
891 /***********************************************************************
892 * CommitUrlCacheEntryA (WININET.@)
895 BOOL WINAPI CommitUrlCacheEntryA(LPCSTR lpszUrl, LPCSTR lpszLocalName,
896 FILETIME ExpireTime, FILETIME lastModified, DWORD cacheEntryType,
897 LPBYTE lpHeaderInfo, DWORD headerSize, LPCSTR fileExtension,
898 DWORD originalUrl)
900 FIXME("stub\n");
901 return FALSE;
904 /***********************************************************************
905 * InternetAttemptConnect (WININET.@)
907 * Attempt to make a connection to the internet
909 * RETURNS
910 * ERROR_SUCCESS on success
911 * Error value on failure
914 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
916 FIXME("Stub\n");
917 return ERROR_SUCCESS;
921 /***********************************************************************
922 * InternetCanonicalizeUrlA (WININET.@)
924 * Escape unsafe characters and spaces
926 * RETURNS
927 * TRUE on success
928 * FALSE on failure
931 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
932 LPDWORD lpdwBufferLength, DWORD dwFlags)
934 HRESULT hr;
935 TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
936 lpdwBufferLength, dwFlags);
938 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
939 dwFlags ^= ICU_NO_ENCODE;
941 dwFlags |= 0x80000000; /* Don't know what this means */
943 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
945 return (hr == S_OK) ? TRUE : FALSE;
948 /***********************************************************************
949 * InternetCanonicalizeUrlW (WININET.@)
951 * Escape unsafe characters and spaces
953 * RETURNS
954 * TRUE on success
955 * FALSE on failure
958 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
959 LPDWORD lpdwBufferLength, DWORD dwFlags)
961 HRESULT hr;
962 TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
963 lpdwBufferLength, dwFlags);
965 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
966 dwFlags ^= ICU_NO_ENCODE;
968 dwFlags |= 0x80000000; /* Don't know what this means */
970 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
972 return (hr == S_OK) ? TRUE : FALSE;
976 /***********************************************************************
977 * InternetSetStatusCallback (WININET.@)
979 * Sets up a callback function which is called as progress is made
980 * during an operation.
982 * RETURNS
983 * Previous callback or NULL on success
984 * INTERNET_INVALID_STATUS_CALLBACK on failure
987 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
988 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
990 INTERNET_STATUS_CALLBACK retVal;
991 LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;
993 TRACE("0x%08lx\n", (ULONG)hInternet);
994 if (lpwai->hdr.htype != WH_HINIT)
995 return INTERNET_INVALID_STATUS_CALLBACK;
997 retVal = lpwai->lpfnStatusCB;
998 lpwai->lpfnStatusCB = lpfnIntCB;
1000 return retVal;
1004 /***********************************************************************
1005 * InternetWriteFile (WININET.@)
1007 * Write data to an open internet file
1009 * RETURNS
1010 * TRUE on success
1011 * FALSE on failure
1014 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
1015 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1017 BOOL retval = FALSE;
1018 int nSocket = -1;
1019 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
1021 TRACE("\n");
1022 if (NULL == lpwh)
1023 return FALSE;
1025 switch (lpwh->htype)
1027 case WH_HHTTPREQ:
1028 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
1029 break;
1031 case WH_HFILE:
1032 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
1033 break;
1035 default:
1036 break;
1039 if (nSocket != -1)
1041 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
1042 retval = (res >= 0);
1043 *lpdwNumOfBytesWritten = retval ? res : 0;
1046 return retval;
1050 /***********************************************************************
1051 * InternetReadFile (WININET.@)
1053 * Read data from an open internet file
1055 * RETURNS
1056 * TRUE on success
1057 * FALSE on failure
1060 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1061 DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
1063 BOOL retval = FALSE;
1064 int nSocket = -1;
1065 LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;
1067 TRACE("\n");
1069 if (NULL == lpwh)
1070 return FALSE;
1072 switch (lpwh->htype)
1074 case WH_HHTTPREQ:
1075 nSocket = ((LPWININETHTTPREQA)hFile)->nSocketFD;
1076 break;
1078 case WH_HFILE:
1079 nSocket = ((LPWININETFILE)hFile)->nDataSocket;
1080 break;
1082 default:
1083 break;
1086 if (nSocket != -1)
1088 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, 0);
1089 retval = (res >= 0);
1090 *dwNumOfBytesRead = retval ? res : 0;
1092 return retval;
1095 /***********************************************************************
1096 * InternetReadFileExA (WININET.@)
1098 * Read data from an open internet file
1100 * RETURNS
1101 * TRUE on success
1102 * FALSE on failure
1105 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffer,
1106 DWORD dwFlags, DWORD dwContext)
1108 FIXME("stub\n");
1109 return FALSE;
1112 /***********************************************************************
1113 * InternetReadFileExW (WININET.@)
1115 * Read data from an open internet file
1117 * RETURNS
1118 * TRUE on success
1119 * FALSE on failure
1122 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1123 DWORD dwFlags, DWORD dwContext)
1125 FIXME("stub\n");
1127 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1128 return FALSE;
1131 /***********************************************************************
1132 * InternetQueryOptionA (WININET.@)
1134 * Queries an options on the specified handle
1136 * RETURNS
1137 * TRUE on success
1138 * FALSE on failure
1141 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
1142 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1144 LPWININETHANDLEHEADER lpwhh;
1145 BOOL bSuccess = FALSE;
1147 TRACE("0x%08lx\n", dwOption);
1149 if (NULL == hInternet)
1151 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1152 return FALSE;
1155 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1157 switch (dwOption)
1159 case INTERNET_OPTION_HANDLE_TYPE:
1161 ULONG type = lpwhh->htype;
1162 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
1164 if (*lpdwBufferLength < sizeof(ULONG))
1165 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1166 else
1168 memcpy(lpBuffer, &type, sizeof(ULONG));
1169 *lpdwBufferLength = sizeof(ULONG);
1170 bSuccess = TRUE;
1172 break;
1175 case INTERNET_OPTION_REQUEST_FLAGS:
1177 ULONG flags = 4;
1178 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
1179 if (*lpdwBufferLength < sizeof(ULONG))
1180 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1181 else
1183 memcpy(lpBuffer, &flags, sizeof(ULONG));
1184 *lpdwBufferLength = sizeof(ULONG);
1185 bSuccess = TRUE;
1187 break;
1190 case INTERNET_OPTION_URL:
1191 case INTERNET_OPTION_DATAFILE_NAME:
1193 ULONG type = lpwhh->htype;
1194 if (type == WH_HHTTPREQ)
1196 LPWININETHTTPREQA lpreq = hInternet;
1197 char url[1023];
1199 sprintf(url,"http://%s%s",lpreq->lpszHostName,lpreq->lpszPath);
1200 TRACE("INTERNET_OPTION_URL: %s\n",url);
1201 if (*lpdwBufferLength < strlen(url)+1)
1202 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1203 else
1205 memcpy(lpBuffer, url, strlen(url)+1);
1206 *lpdwBufferLength = strlen(url)+1;
1207 bSuccess = TRUE;
1210 break;
1212 case INTERNET_OPTION_HTTP_VERSION:
1215 * Presently hardcoded to 1.1
1217 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1218 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1219 bSuccess = TRUE;
1220 break;
1223 default:
1224 FIXME("Stub! %ld \n",dwOption);
1225 break;
1228 return bSuccess;
1232 /***********************************************************************
1233 * InternetSetOptionW (WININET.@)
1235 * Sets an options on the specified handle
1237 * RETURNS
1238 * TRUE on success
1239 * FALSE on failure
1242 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
1243 LPVOID lpBuffer, DWORD dwBufferLength)
1245 LPWININETHANDLEHEADER lpwhh;
1246 BOOL bSuccess = FALSE;
1248 TRACE("0x%08lx\n", dwOption);
1250 if (NULL == hInternet)
1252 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1253 return FALSE;
1256 lpwhh = (LPWININETHANDLEHEADER) hInternet;
1258 switch (dwOption)
1260 default:
1261 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1262 FIXME("STUB\n");
1263 break;
1266 return bSuccess;
1270 /***********************************************************************
1271 * InternetSetOptionA (WININET.@)
1273 * Sets an options on the specified handle.
1275 * RETURNS
1276 * TRUE on success
1277 * FALSE on failure
1280 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
1281 LPVOID lpBuffer, DWORD dwBufferLength)
1283 /* FIXME!!! implement if lpBuffer is a string, dwBufferLength is
1284 in TCHARs */
1285 return InternetSetOptionW(hInternet,dwOption, lpBuffer,
1286 dwBufferLength);
1290 /***********************************************************************
1291 * InternetGetCookieA (WININET.@)
1293 * Retrieve cookie from the specified url
1295 * RETURNS
1296 * TRUE on success
1297 * FALSE on failure
1300 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1301 LPSTR lpCookieData, LPDWORD lpdwSize)
1303 FIXME("STUB\n");
1304 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl),debugstr_a(lpszCookieName),
1305 lpCookieData);
1306 return FALSE;
1310 /***********************************************************************
1311 * InternetGetCookieW (WININET.@)
1313 * Retrieve cookie from the specified url
1315 * RETURNS
1316 * TRUE on success
1317 * FALSE on failure
1320 BOOL WINAPI InternetGetCookieW(LPCSTR lpszUrl, LPCWSTR lpszCookieName,
1321 LPWSTR lpCookieData, LPDWORD lpdwSize)
1323 FIXME("STUB\n");
1324 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_w(lpszCookieName),
1325 lpCookieData);
1326 return FALSE;
1330 /***********************************************************************
1331 * InternetSetCookieA (WININET.@)
1333 * Sets cookie for the specified url
1335 * RETURNS
1336 * TRUE on success
1337 * FALSE on failure
1340 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1341 LPCSTR lpCookieData)
1343 FIXME("STUB\n");
1344 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
1345 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
1346 return FALSE;
1350 /***********************************************************************
1351 * InternetSetCookieW (WININET.@)
1353 * Sets cookie for the specified url
1355 * RETURNS
1356 * TRUE on success
1357 * FALSE on failure
1360 BOOL WINAPI InternetSetCookieW(LPCSTR lpszUrl, LPCWSTR lpszCookieName,
1361 LPCWSTR lpCookieData)
1363 FIXME("STUB\n");
1364 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
1365 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
1366 return FALSE;
1370 /***********************************************************************
1371 * GetInternetScheme (internal)
1373 * Get scheme of url
1375 * RETURNS
1376 * scheme on success
1377 * INTERNET_SCHEME_UNKNOWN on failure
1380 INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
1382 TRACE("\n");
1383 if(lpszScheme==NULL)
1384 return INTERNET_SCHEME_UNKNOWN;
1386 if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
1387 return INTERNET_SCHEME_FTP;
1388 else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
1389 return INTERNET_SCHEME_GOPHER;
1390 else if (!strncasecmp("http", lpszScheme, nMaxCmp))
1391 return INTERNET_SCHEME_HTTP;
1392 else if (!strncasecmp("https", lpszScheme, nMaxCmp))
1393 return INTERNET_SCHEME_HTTPS;
1394 else if (!strncasecmp("file", lpszScheme, nMaxCmp))
1395 return INTERNET_SCHEME_FILE;
1396 else if (!strncasecmp("news", lpszScheme, nMaxCmp))
1397 return INTERNET_SCHEME_NEWS;
1398 else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
1399 return INTERNET_SCHEME_MAILTO;
1400 else
1401 return INTERNET_SCHEME_UNKNOWN;
1404 /***********************************************************************
1405 * InternetCheckConnectionA (WININET.@)
1407 * Pings a requested host to check internet connection
1409 * RETURNS
1410 * TRUE on success and FALSE on failure. If a failure then
1411 * ERROR_NOT_CONNECTED is placesd into GetLastError
1414 BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
1417 * this is a kludge which runs the resident ping program and reads the output.
1419 * Anyone have a better idea?
1422 BOOL rc = FALSE;
1423 char command[1024];
1424 char host[1024];
1425 int status = -1;
1427 FIXME("\n");
1430 * Crack or set the Address
1432 if (lpszUrl == NULL)
1435 * According to the doc we are supost to use the ip for the next
1436 * server in the WnInet internal server database. I have
1437 * no idea what that is or how to get it.
1439 * So someone needs to implement this.
1441 FIXME("Unimplemented with URL of NULL\n");
1442 return TRUE;
1444 else
1446 URL_COMPONENTSA componets;
1448 ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
1449 componets.lpszHostName = (LPSTR)&host;
1450 componets.dwHostNameLength = 1024;
1452 if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
1453 goto End;
1455 TRACE("host name : %s\n",componets.lpszHostName);
1459 * Build our ping command
1461 strcpy(command,"ping -w 1 ");
1462 strcat(command,host);
1463 strcat(command," >/dev/null 2>/dev/null");
1465 TRACE("Ping command is : %s\n",command);
1467 status = system(command);
1469 TRACE("Ping returned a code of %i \n",status);
1471 /* Ping return code of 0 indicates success */
1472 if (status == 0)
1473 rc = TRUE;
1475 End:
1477 if (rc == FALSE)
1478 SetLastError(ERROR_NOT_CONNECTED);
1480 return rc;
1484 /***********************************************************************
1485 * InternetCheckConnectionW (WININET.@)
1487 * Pings a requested host to check internet connection
1489 * RETURNS
1490 * TRUE on success and FALSE on failure. If a failure then
1491 * ERROR_NOT_CONNECTED is placed into GetLastError
1494 BOOL WINAPI InternetCheckConnectionW(LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
1496 CHAR *szUrl;
1497 INT len;
1498 BOOL rc;
1500 len = lstrlenW(lpszUrl)+1;
1501 if (!(szUrl = (CHAR *)malloc(len*sizeof(CHAR))))
1502 return FALSE;
1503 WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, len, NULL, NULL);
1504 rc = InternetCheckConnectionA((LPCSTR)szUrl, dwFlags, dwReserved);
1505 free(szUrl);
1507 return rc;
1511 /**********************************************************
1512 * InternetOpenUrlA (WININET.@)
1514 * Opens an URL
1516 * RETURNS
1517 * handle of connection or NULL on failure
1519 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
1520 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1522 URL_COMPONENTSA urlComponents;
1523 char protocol[32], hostName[MAXHOSTNAME], userName[1024];
1524 char password[1024], path[2048], extra[1024];
1525 HINTERNET client = NULL, client1 = NULL;
1526 urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
1527 urlComponents.lpszScheme = protocol;
1528 urlComponents.dwSchemeLength = 32;
1529 urlComponents.lpszHostName = hostName;
1530 urlComponents.dwHostNameLength = MAXHOSTNAME;
1531 urlComponents.lpszUserName = userName;
1532 urlComponents.dwUserNameLength = 1024;
1533 urlComponents.lpszPassword = password;
1534 urlComponents.dwPasswordLength = 1024;
1535 urlComponents.lpszUrlPath = path;
1536 urlComponents.dwUrlPathLength = 2048;
1537 urlComponents.lpszExtraInfo = extra;
1538 urlComponents.dwExtraInfoLength = 1024;
1539 if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
1540 return NULL;
1541 switch(urlComponents.nScheme) {
1542 case INTERNET_SCHEME_FTP:
1543 if(urlComponents.nPort == 0)
1544 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
1545 client = InternetConnectA(hInternet, hostName, urlComponents.nPort,
1546 userName, password, INTERNET_SERVICE_FTP, dwFlags, dwContext);
1547 return FtpOpenFileA(client, path, GENERIC_READ, dwFlags, dwContext);
1548 break;
1549 case INTERNET_SCHEME_HTTP:
1550 case INTERNET_SCHEME_HTTPS:
1552 LPCSTR accept[2] = { "*/*", NULL };
1553 char *hostreq=(char*)malloc(strlen(hostName)+9);
1554 sprintf(hostreq, "Host: %s\r\n", hostName);
1555 if(urlComponents.nPort == 0) {
1556 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
1557 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1558 else
1559 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1561 client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName,
1562 password, INTERNET_SERVICE_HTTP, dwFlags, dwContext);
1563 if(client == NULL)
1564 return NULL;
1565 client1 = HttpOpenRequestA(hInternet, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
1566 if(client1 == NULL) {
1567 InternetCloseHandle(client);
1568 return NULL;
1570 HttpAddRequestHeadersA(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
1571 HttpAddRequestHeadersA(client1, hostreq, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1572 if(!HttpSendRequestA(client1, NULL, 0, NULL, 0)) {
1573 InternetCloseHandle(client1);
1574 InternetCloseHandle(client);
1575 return NULL;
1577 return client1;
1578 break;
1580 case INTERNET_SCHEME_GOPHER:
1581 /* gopher doesn't seem to be implemented in wine, but it's supposed
1582 * to be supported by InternetOpenUrlA. */
1583 default:
1584 return NULL;
1586 if(client != NULL)
1587 InternetCloseHandle(client);
1591 /**********************************************************
1592 * InternetOpenUrlW (WININET.@)
1594 * Opens an URL
1596 * RETURNS
1597 * handle of connection or NULL on failure
1599 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
1600 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
1602 HINTERNET rc = (HINTERNET)NULL;
1604 INT lenUrl = lstrlenW(lpszUrl)+1;
1605 INT lenHeaders = lstrlenW(lpszHeaders)+1;
1606 CHAR *szUrl = (CHAR *)malloc(lenUrl*sizeof(CHAR));
1607 CHAR *szHeaders = (CHAR *)malloc(lenHeaders*sizeof(CHAR));
1609 if (!szUrl || !szHeaders)
1611 if (szUrl)
1612 free(szUrl);
1613 if (szHeaders)
1614 free(szHeaders);
1615 return (HINTERNET)NULL;
1618 WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, lenUrl,
1619 NULL, NULL);
1620 WideCharToMultiByte(CP_ACP, -1, lpszHeaders, -1, szHeaders, lenHeaders,
1621 NULL, NULL);
1623 rc = InternetOpenUrlA(hInternet, szUrl, szHeaders,
1624 dwHeadersLength, dwFlags, dwContext);
1626 free(szUrl);
1627 free(szHeaders);
1629 return rc;
1633 /***********************************************************************
1634 * INTERNET_SetLastError (internal)
1636 * Set last thread specific error
1638 * RETURNS
1641 void INTERNET_SetLastError(DWORD dwError)
1643 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1645 SetLastError(dwError);
1646 lpwite->dwError = dwError;
1650 /***********************************************************************
1651 * INTERNET_GetLastError (internal)
1653 * Get last thread specific error
1655 * RETURNS
1658 DWORD INTERNET_GetLastError()
1660 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1661 return lpwite->dwError;
1665 /***********************************************************************
1666 * INTERNET_WorkerThreadFunc (internal)
1668 * Worker thread execution function
1670 * RETURNS
1673 DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
1675 DWORD dwWaitRes;
1677 while (1)
1679 if(dwNumJobs > 0) {
1680 INTERNET_ExecuteWork();
1681 continue;
1683 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
1685 if (dwWaitRes == WAIT_OBJECT_0 + 1)
1686 INTERNET_ExecuteWork();
1687 else
1688 break;
1690 InterlockedIncrement(&dwNumIdleThreads);
1693 InterlockedDecrement(&dwNumIdleThreads);
1694 InterlockedDecrement(&dwNumThreads);
1695 TRACE("Worker thread exiting\n");
1696 return TRUE;
1700 /***********************************************************************
1701 * INTERNET_InsertWorkRequest (internal)
1703 * Insert work request into queue
1705 * RETURNS
1708 BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
1710 BOOL bSuccess = FALSE;
1711 LPWORKREQUEST lpNewRequest;
1713 TRACE("\n");
1715 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
1716 if (lpNewRequest)
1718 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
1719 lpNewRequest->prev = NULL;
1721 EnterCriticalSection(&csQueue);
1723 lpNewRequest->next = lpWorkQueueTail;
1724 if (lpWorkQueueTail)
1725 lpWorkQueueTail->prev = lpNewRequest;
1726 lpWorkQueueTail = lpNewRequest;
1727 if (!lpHeadWorkQueue)
1728 lpHeadWorkQueue = lpWorkQueueTail;
1730 LeaveCriticalSection(&csQueue);
1732 bSuccess = TRUE;
1733 InterlockedIncrement(&dwNumJobs);
1736 return bSuccess;
1740 /***********************************************************************
1741 * INTERNET_GetWorkRequest (internal)
1743 * Retrieves work request from queue
1745 * RETURNS
1748 BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
1750 BOOL bSuccess = FALSE;
1751 LPWORKREQUEST lpRequest = NULL;
1753 TRACE("\n");
1755 EnterCriticalSection(&csQueue);
1757 if (lpHeadWorkQueue)
1759 lpRequest = lpHeadWorkQueue;
1760 lpHeadWorkQueue = lpHeadWorkQueue->prev;
1761 if (lpRequest == lpWorkQueueTail)
1762 lpWorkQueueTail = lpHeadWorkQueue;
1765 LeaveCriticalSection(&csQueue);
1767 if (lpRequest)
1769 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
1770 HeapFree(GetProcessHeap(), 0, lpRequest);
1771 bSuccess = TRUE;
1772 InterlockedDecrement(&dwNumJobs);
1775 return bSuccess;
1779 /***********************************************************************
1780 * INTERNET_AsyncCall (internal)
1782 * Retrieves work request from queue
1784 * RETURNS
1787 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
1789 HANDLE hThread;
1790 DWORD dwTID;
1791 BOOL bSuccess = FALSE;
1793 TRACE("\n");
1795 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
1797 InterlockedIncrement(&dwNumIdleThreads);
1799 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
1800 !(hThread = CreateThread(NULL, 0,
1801 (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
1803 InterlockedDecrement(&dwNumThreads);
1804 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
1805 goto lerror;
1808 TRACE("Created new thread\n");
1811 bSuccess = TRUE;
1812 INTERNET_InsertWorkRequest(lpWorkRequest);
1813 SetEvent(hWorkEvent);
1815 lerror:
1817 return bSuccess;
1821 /***********************************************************************
1822 * INTERNET_ExecuteWork (internal)
1824 * RETURNS
1827 VOID INTERNET_ExecuteWork()
1829 WORKREQUEST workRequest;
1831 TRACE("\n");
1833 if (INTERNET_GetWorkRequest(&workRequest))
1835 TRACE("Got work %d\n", workRequest.asyncall);
1836 switch (workRequest.asyncall)
1838 case FTPPUTFILEA:
1839 FTP_FtpPutFileA((HINTERNET)workRequest.HFTPSESSION, (LPCSTR)workRequest.LPSZLOCALFILE,
1840 (LPCSTR)workRequest.LPSZNEWREMOTEFILE, workRequest.DWFLAGS, workRequest.DWCONTEXT);
1841 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZLOCALFILE);
1842 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWREMOTEFILE);
1843 break;
1845 case FTPSETCURRENTDIRECTORYA:
1846 FTP_FtpSetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1847 (LPCSTR)workRequest.LPSZDIRECTORY);
1848 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1849 break;
1851 case FTPCREATEDIRECTORYA:
1852 FTP_FtpCreateDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1853 (LPCSTR)workRequest.LPSZDIRECTORY);
1854 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1855 break;
1857 case FTPFINDFIRSTFILEA:
1858 FTP_FtpFindFirstFileA((HINTERNET)workRequest.HFTPSESSION,
1859 (LPCSTR)workRequest.LPSZSEARCHFILE,
1860 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA, workRequest.DWFLAGS,
1861 workRequest.DWCONTEXT);
1862 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSEARCHFILE);
1863 break;
1865 case FTPGETCURRENTDIRECTORYA:
1866 FTP_FtpGetCurrentDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1867 (LPSTR)workRequest.LPSZDIRECTORY, (LPDWORD)workRequest.LPDWDIRECTORY);
1868 break;
1870 case FTPOPENFILEA:
1871 FTP_FtpOpenFileA((HINTERNET)workRequest.HFTPSESSION,
1872 (LPCSTR)workRequest.LPSZFILENAME,
1873 workRequest.FDWACCESS,
1874 workRequest.DWFLAGS,
1875 workRequest.DWCONTEXT);
1876 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1877 break;
1879 case FTPGETFILEA:
1880 FTP_FtpGetFileA((HINTERNET)workRequest.HFTPSESSION,
1881 (LPCSTR)workRequest.LPSZREMOTEFILE,
1882 (LPCSTR)workRequest.LPSZNEWFILE,
1883 (BOOL)workRequest.FFAILIFEXISTS,
1884 workRequest.DWLOCALFLAGSATTRIBUTE,
1885 workRequest.DWFLAGS,
1886 workRequest.DWCONTEXT);
1887 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREMOTEFILE);
1888 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZNEWFILE);
1889 break;
1891 case FTPDELETEFILEA:
1892 FTP_FtpDeleteFileA((HINTERNET)workRequest.HFTPSESSION,
1893 (LPCSTR)workRequest.LPSZFILENAME);
1894 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZFILENAME);
1895 break;
1897 case FTPREMOVEDIRECTORYA:
1898 FTP_FtpRemoveDirectoryA((HINTERNET)workRequest.HFTPSESSION,
1899 (LPCSTR)workRequest.LPSZDIRECTORY);
1900 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDIRECTORY);
1901 break;
1903 case FTPRENAMEFILEA:
1904 FTP_FtpRenameFileA((HINTERNET)workRequest.HFTPSESSION,
1905 (LPCSTR)workRequest.LPSZSRCFILE,
1906 (LPCSTR)workRequest.LPSZDESTFILE);
1907 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZSRCFILE);
1908 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZDESTFILE);
1909 break;
1911 case INTERNETFINDNEXTA:
1912 INTERNET_FindNextFileA((HINTERNET)workRequest.HFTPSESSION,
1913 (LPWIN32_FIND_DATAA)workRequest.LPFINDFILEDATA);
1914 break;
1916 case HTTPSENDREQUESTA:
1917 HTTP_HttpSendRequestA((HINTERNET)workRequest.HFTPSESSION,
1918 (LPCSTR)workRequest.LPSZHEADER,
1919 workRequest.DWHEADERLENGTH,
1920 (LPVOID)workRequest.LPOPTIONAL,
1921 workRequest.DWOPTIONALLENGTH);
1922 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZHEADER);
1923 break;
1925 case HTTPOPENREQUESTA:
1926 HTTP_HttpOpenRequestA((HINTERNET)workRequest.HFTPSESSION,
1927 (LPCSTR)workRequest.LPSZVERB,
1928 (LPCSTR)workRequest.LPSZOBJECTNAME,
1929 (LPCSTR)workRequest.LPSZVERSION,
1930 (LPCSTR)workRequest.LPSZREFERRER,
1931 (LPCSTR*)workRequest.LPSZACCEPTTYPES,
1932 workRequest.DWFLAGS,
1933 workRequest.DWCONTEXT);
1934 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERB);
1935 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZOBJECTNAME);
1936 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZVERSION);
1937 HeapFree(GetProcessHeap(), 0, (LPVOID)workRequest.LPSZREFERRER);
1938 break;
1940 case SENDCALLBACK:
1941 SendAsyncCallbackInt((LPWININETAPPINFOA)workRequest.param1,
1942 (HINTERNET)workRequest.param2, workRequest.param3,
1943 workRequest.param4, (LPVOID)workRequest.param5,
1944 workRequest.param6);
1945 break;
1951 /***********************************************************************
1952 * INTERNET_GetResponseBuffer
1954 * RETURNS
1957 LPSTR INTERNET_GetResponseBuffer()
1959 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
1960 TRACE("\n");
1961 return lpwite->response;
1965 /***********************************************************************
1966 * INTERNET_GetNextLine (internal)
1968 * Parse next line in directory string listing
1970 * RETURNS
1971 * Pointer to beginning of next line
1972 * NULL on failure
1976 LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
1978 struct timeval tv;
1979 fd_set infd;
1980 BOOL bSuccess = FALSE;
1981 INT nRecv = 0;
1983 TRACE("\n");
1985 FD_ZERO(&infd);
1986 FD_SET(nSocket, &infd);
1987 tv.tv_sec=RESPONSE_TIMEOUT;
1988 tv.tv_usec=0;
1990 while (nRecv < *dwBuffer)
1992 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
1994 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
1996 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1997 goto lend;
2000 if (lpszBuffer[nRecv] == '\n')
2002 bSuccess = TRUE;
2003 break;
2005 if (lpszBuffer[nRecv] != '\r')
2006 nRecv++;
2008 else
2010 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
2011 goto lend;
2015 lend:
2016 if (bSuccess)
2018 lpszBuffer[nRecv] = '\0';
2019 *dwBuffer = nRecv - 1;
2020 TRACE(":%d %s\n", nRecv, lpszBuffer);
2021 return lpszBuffer;
2023 else
2025 return NULL;
2029 /***********************************************************************
2032 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
2033 LPDWORD lpdwNumberOfBytesAvailble,
2034 DWORD dwFlags, DWORD dwConext)
2036 LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hFile;
2037 INT retval = -1;
2038 int nSocket = -1;
2041 if (NULL == lpwhr)
2043 SetLastError(ERROR_NO_MORE_FILES);
2044 return FALSE;
2047 TRACE("--> %p %i %i\n",lpwhr,lpwhr->hdr.htype,lpwhr->nSocketFD);
2049 switch (lpwhr->hdr.htype)
2051 case WH_HHTTPREQ:
2052 nSocket = lpwhr->nSocketFD;
2053 break;
2055 default:
2056 break;
2059 if (nSocket != -1)
2061 char buffer[4048];
2063 retval = recv(nSocket,buffer,4048,MSG_PEEK);
2065 else
2067 SetLastError(ERROR_NO_MORE_FILES);
2070 if (lpdwNumberOfBytesAvailble)
2072 (*lpdwNumberOfBytesAvailble) = retval;
2075 TRACE("<-- %i\n",retval);
2076 return (retval+1);
2080 /***********************************************************************
2083 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
2084 *lphLockReqHandle)
2086 FIXME("STUB\n");
2087 return FALSE;
2090 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
2092 FIXME("STUB\n");
2093 return FALSE;
2097 /***********************************************************************
2098 * InternetAutoDial
2100 * On windows this function is supposed to dial the default internet
2101 * connection. We don't want to have Wine dial out to the internet so
2102 * we return TRUE by default. It might be nice to check if we are connected.
2104 * RETURNS
2105 * TRUE on success
2106 * FALSE on failure
2109 BOOL WINAPI InternetAutoDial(DWORD dwFlags, HWND hwndParent)
2111 FIXME("STUB\n");
2113 /* Tell that we are connected to the internet. */
2114 return TRUE;
2117 /***********************************************************************
2118 * InternetAutoDialHangup
2120 * Hangs up an connection made with InternetAutoDial
2122 * PARAM
2123 * dwReserved
2124 * RETURNS
2125 * TRUE on success
2126 * FALSE on failure
2129 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
2131 FIXME("STUB\n");
2133 /* we didn't dial, we don't disconnect */
2134 return TRUE;