wininet: Handles handling clean up.

This commit is contained in:
Jacek Caban 2011-02-02 22:50:31 +01:00 committed by Alexandre Julliard
parent e1815f5e3b
commit f165e25e59
5 changed files with 132 additions and 110 deletions

View File

@ -70,7 +70,7 @@ static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
BOOL ret = FALSE; BOOL ret = FALSE;
LPWSTR p; LPWSTR p;
lpwhr = (http_request_t*) WININET_GetObject( hRequest ); lpwhr = (http_request_t*) get_handle_object( hRequest );
if (NULL == lpwhr) if (NULL == lpwhr)
return FALSE; return FALSE;
@ -107,7 +107,7 @@ static BOOL WININET_GetServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
http_session_t *lpwhs = NULL; http_session_t *lpwhs = NULL;
BOOL ret = FALSE; BOOL ret = FALSE;
lpwhr = (http_request_t*) WININET_GetObject( hRequest ); lpwhr = (http_request_t*) get_handle_object( hRequest );
if (NULL == lpwhr) if (NULL == lpwhr)
return FALSE; return FALSE;
@ -246,7 +246,7 @@ static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
BOOL ret = FALSE; BOOL ret = FALSE;
LPWSTR p, q; LPWSTR p, q;
lpwhr = (http_request_t*) WININET_GetObject( hRequest ); lpwhr = (http_request_t*) get_handle_object( hRequest );
if( !lpwhr ) if( !lpwhr )
return FALSE; return FALSE;

View File

@ -288,7 +288,7 @@ BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
return FALSE; return FALSE;
} }
lpwfs = (ftp_session_t*) WININET_GetObject( hConnect ); lpwfs = (ftp_session_t*) get_handle_object( hConnect );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -470,7 +470,7 @@ BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
goto lend; goto lend;
} }
lpwfs = (ftp_session_t*) WININET_GetObject( hConnect ); lpwfs = (ftp_session_t*) get_handle_object( hConnect );
if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype) if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
{ {
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE); INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
@ -610,7 +610,7 @@ BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
BOOL r = FALSE; BOOL r = FALSE;
lpwfs = (ftp_session_t*) WININET_GetObject( hConnect ); lpwfs = (ftp_session_t*) get_handle_object( hConnect );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -766,7 +766,7 @@ HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
HINTERNET r = NULL; HINTERNET r = NULL;
lpwfs = (ftp_session_t*) WININET_GetObject( hConnect ); lpwfs = (ftp_session_t*) get_handle_object( hConnect );
if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype) if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
{ {
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE); INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
@ -964,7 +964,7 @@ BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDir
TRACE("%p %p %p\n", hFtpSession, lpszCurrentDirectory, lpdwCurrentDirectory); TRACE("%p %p %p\n", hFtpSession, lpszCurrentDirectory, lpdwCurrentDirectory);
lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession ); lpwfs = (ftp_session_t*) get_handle_object( hFtpSession );
if (NULL == lpwfs) if (NULL == lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -1327,6 +1327,7 @@ static HINTERNET FTP_FtpOpenFileW(ftp_session_t *lpwfs,
ftp_file_t *lpwh = NULL; ftp_file_t *lpwh = NULL;
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
HINTERNET handle = NULL; HINTERNET handle = NULL;
DWORD res = ERROR_SUCCESS;
TRACE("\n"); TRACE("\n");
@ -1364,8 +1365,8 @@ static HINTERNET FTP_FtpOpenFileW(ftp_session_t *lpwfs,
lpwh->lpFtpSession = lpwfs; lpwh->lpFtpSession = lpwfs;
list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry ); list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
handle = WININET_AllocHandle( &lpwh->hdr ); res = alloc_handle(&lpwh->hdr, &handle);
if( !handle ) if (res != ERROR_SUCCESS)
goto lend; goto lend;
/* Indicate that a download is currently in progress */ /* Indicate that a download is currently in progress */
@ -1440,6 +1441,8 @@ lend:
if( lpwh ) if( lpwh )
WININET_Release( &lpwh->hdr ); WININET_Release( &lpwh->hdr );
if(res != ERROR_SUCCESS)
INTERNET_SetLastError(res);
return handle; return handle;
} }
@ -1501,7 +1504,7 @@ HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession, TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext); debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession ); lpwfs = (ftp_session_t*) get_handle_object( hFtpSession );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -1624,7 +1627,7 @@ BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lps
return FALSE; return FALSE;
} }
lpwfs = (ftp_session_t*) WININET_GetObject( hInternet ); lpwfs = (ftp_session_t*) get_handle_object( hInternet );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -1818,7 +1821,7 @@ BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
BOOL r = FALSE; BOOL r = FALSE;
lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession ); lpwfs = (ftp_session_t*) get_handle_object( hFtpSession );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -1963,7 +1966,7 @@ BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
BOOL r = FALSE; BOOL r = FALSE;
lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession ); lpwfs = (ftp_session_t*) get_handle_object( hFtpSession );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -2113,7 +2116,7 @@ BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszD
appinfo_t *hIC = NULL; appinfo_t *hIC = NULL;
BOOL r = FALSE; BOOL r = FALSE;
lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession ); lpwfs = (ftp_session_t*) get_handle_object( hFtpSession );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -2279,7 +2282,7 @@ BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags
return FALSE; return FALSE;
} }
lpwfs = (ftp_session_t*) WININET_GetObject( hConnect ); lpwfs = (ftp_session_t*) get_handle_object( hConnect );
if (!lpwfs) if (!lpwfs)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -2451,6 +2454,7 @@ HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
ftp_session_t *lpwfs = NULL; ftp_session_t *lpwfs = NULL;
HINTERNET handle = NULL; HINTERNET handle = NULL;
char szaddr[INET_ADDRSTRLEN]; char szaddr[INET_ADDRSTRLEN];
DWORD res;
TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n", TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
hIC, debugstr_w(lpszServerName), hIC, debugstr_w(lpszServerName),
@ -2492,11 +2496,11 @@ HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
lpwfs->lpAppInfo = hIC; lpwfs->lpAppInfo = hIC;
list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry ); list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
handle = WININET_AllocHandle( &lpwfs->hdr ); res = alloc_handle(&lpwfs->hdr, &handle);
if( !handle ) if(res != ERROR_SUCCESS)
{ {
ERR("Failed to alloc handle\n"); ERR("Failed to alloc handle\n");
INTERNET_SetLastError(ERROR_OUTOFMEMORY); INTERNET_SetLastError(res);
goto lerror; goto lerror;
} }
@ -2604,7 +2608,7 @@ lerror:
if (!bSuccess && handle) if (!bSuccess && handle)
{ {
WININET_FreeHandle( handle ); InternetCloseHandle(handle);
handle = NULL; handle = NULL;
} }
@ -3531,6 +3535,7 @@ static HINTERNET FTP_ReceiveFileList(ftp_session_t *lpwfs, INT nSocket, LPCWSTR
LPFILEPROPERTIESW lpafp = NULL; LPFILEPROPERTIESW lpafp = NULL;
LPWININETFTPFINDNEXTW lpwfn = NULL; LPWININETFTPFINDNEXTW lpwfn = NULL;
HINTERNET handle = 0; HINTERNET handle = 0;
DWORD res;
TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext); TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
@ -3555,7 +3560,9 @@ static HINTERNET FTP_ReceiveFileList(ftp_session_t *lpwfs, INT nSocket, LPCWSTR
lpwfn->lpFtpSession = lpwfs; lpwfn->lpFtpSession = lpwfs;
list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry ); list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
handle = WININET_AllocHandle( &lpwfn->hdr ); res = alloc_handle(&lpwfn->hdr, &handle);
if(res != ERROR_SUCCESS)
SetLastError(res);
} }
} }

View File

@ -1066,7 +1066,7 @@ BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
if (!lpszHeader) if (!lpszHeader)
return TRUE; return TRUE;
lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest ); lpwhr = (http_request_t*) get_handle_object( hHttpRequest );
if (lpwhr && lpwhr->hdr.htype == WH_HHTTPREQ) if (lpwhr && lpwhr->hdr.htype == WH_HHTTPREQ)
res = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier ); res = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
if( lpwhr ) if( lpwhr )
@ -2604,12 +2604,9 @@ static DWORD HTTP_HttpOpenRequestW(http_session_t *lpwhs,
goto lend; goto lend;
} }
handle = WININET_AllocHandle( &lpwhr->hdr ); res = alloc_handle(&lpwhr->hdr, &handle);
if (NULL == handle) if (res != ERROR_SUCCESS)
{
res = ERROR_OUTOFMEMORY;
goto lend; goto lend;
}
if ((res = NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE)) != ERROR_SUCCESS) if ((res = NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE)) != ERROR_SUCCESS)
{ {
@ -2727,7 +2724,7 @@ HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i])); TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
} }
lpwhs = (http_session_t*) WININET_GetObject( hHttpSession ); lpwhs = (http_session_t*) get_handle_object( hHttpSession );
if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION) if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
{ {
res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
@ -3174,7 +3171,7 @@ BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
TRACE("\n"); TRACE("\n");
} }
lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest ); lpwhr = (http_request_t*) get_handle_object( hHttpRequest );
if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ) if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
{ {
res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
@ -4045,7 +4042,7 @@ BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
return FALSE; return FALSE;
} }
lpwhr = (http_request_t*) WININET_GetObject( hRequest ); lpwhr = (http_request_t*) get_handle_object( hRequest );
if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ) if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
{ {
@ -4160,7 +4157,7 @@ BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn, TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
lpBuffersOut, dwFlags, dwContext); lpBuffersOut, dwFlags, dwContext);
lpwhr = (http_request_t*) WININET_GetObject( hRequest ); lpwhr = (http_request_t*) get_handle_object( hRequest );
if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ) if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
{ {
@ -4259,7 +4256,7 @@ BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest, TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength); debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest ); lpwhr = (http_request_t*) get_handle_object( hHttpRequest );
if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ) if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
{ {
res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
@ -4444,7 +4441,7 @@ DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
{ {
http_session_t *lpwhs = NULL; http_session_t *lpwhs = NULL;
HINTERNET handle = NULL; HINTERNET handle = NULL;
DWORD res = ERROR_SUCCESS; DWORD res;
TRACE("-->\n"); TRACE("-->\n");
@ -4473,11 +4470,10 @@ DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
lpwhs->lpAppInfo = hIC; lpwhs->lpAppInfo = hIC;
list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry ); list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
handle = WININET_AllocHandle( &lpwhs->hdr ); res = alloc_handle(&lpwhs->hdr, &handle);
if (NULL == handle) if (res != ERROR_SUCCESS)
{ {
ERR("Failed to alloc handle\n"); ERR("Failed to alloc handle\n");
res = ERROR_OUTOFMEMORY;
goto lerror; goto lerror;
} }

View File

@ -90,8 +90,6 @@ typedef struct
static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES; static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
HMODULE WININET_hModule; HMODULE WININET_hModule;
#define HANDLE_CHUNK_SIZE 0x10
static CRITICAL_SECTION WININET_cs; static CRITICAL_SECTION WININET_cs;
static CRITICAL_SECTION_DEBUG WININET_cs_debug = static CRITICAL_SECTION_DEBUG WININET_cs_debug =
{ {
@ -101,9 +99,9 @@ static CRITICAL_SECTION_DEBUG WININET_cs_debug =
}; };
static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 }; static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
static object_header_t **WININET_Handles; static object_header_t **handle_table;
static UINT_PTR WININET_dwNextHandle; static UINT_PTR next_handle;
static UINT_PTR WININET_dwMaxHandles; static UINT_PTR handle_table_size;
typedef struct typedef struct
{ {
@ -119,48 +117,51 @@ static const WCHAR szInternetSettings[] =
static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 }; static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 }; static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
HINTERNET WININET_AllocHandle( object_header_t *info ) DWORD alloc_handle( object_header_t *info, HINTERNET *ret )
{ {
object_header_t **p; object_header_t **p;
UINT_PTR handle = 0, num; UINT_PTR handle = 0, num;
DWORD res = ERROR_SUCCESS;
list_init( &info->children ); list_init( &info->children );
EnterCriticalSection( &WININET_cs ); EnterCriticalSection( &WININET_cs );
if( !WININET_dwMaxHandles )
{ if(!handle_table_size) {
num = HANDLE_CHUNK_SIZE; num = 16;
p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, p = heap_alloc_zero(sizeof(handle_table[0]) * num);
sizeof (*WININET_Handles)* num); if(p) {
if( !p ) handle_table = p;
goto end; handle_table_size = num;
WININET_Handles = p; next_handle = 1;
WININET_dwMaxHandles = num; }else {
} res = ERROR_OUTOFMEMORY;
if( WININET_dwMaxHandles == WININET_dwNextHandle ) }
{ }else if(next_handle == handle_table_size) {
num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE; num = handle_table_size * 2;
p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
WININET_Handles, sizeof (*WININET_Handles)* num); if(p) {
if( !p ) handle_table = p;
goto end; handle_table_size = num;
WININET_Handles = p; }else {
WININET_dwMaxHandles = num; res = ERROR_OUTOFMEMORY;
}
} }
handle = WININET_dwNextHandle; if(res == ERROR_SUCCESS) {
if( WININET_Handles[handle] ) handle = next_handle;
ERR("handle isn't free but should be\n"); if(handle_table[handle])
WININET_Handles[handle] = WININET_AddRef( info ); ERR("handle isn't free but should be\n");
handle_table[handle] = WININET_AddRef( info );
while( WININET_Handles[WININET_dwNextHandle] && while(handle_table[next_handle] && next_handle < handle_table_size)
(WININET_dwNextHandle < WININET_dwMaxHandles ) ) next_handle++;
WININET_dwNextHandle++; }
end:
LeaveCriticalSection( &WININET_cs ); LeaveCriticalSection( &WININET_cs );
return info->hInternet = (HINTERNET) (handle+1); info->hInternet = *ret = (HINTERNET)handle;
return res;
} }
object_header_t *WININET_AddRef( object_header_t *info ) object_header_t *WININET_AddRef( object_header_t *info )
@ -170,16 +171,15 @@ object_header_t *WININET_AddRef( object_header_t *info )
return info; return info;
} }
object_header_t *WININET_GetObject( HINTERNET hinternet ) object_header_t *get_handle_object( HINTERNET hinternet )
{ {
object_header_t *info = NULL; object_header_t *info = NULL;
UINT_PTR handle = (UINT_PTR) hinternet; UINT_PTR handle = (UINT_PTR) hinternet;
EnterCriticalSection( &WININET_cs ); EnterCriticalSection( &WININET_cs );
if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) && if(handle > 0 && handle < handle_table_size && handle_table[handle])
WININET_Handles[handle-1] ) info = WININET_AddRef(handle_table[handle]);
info = WININET_AddRef( WININET_Handles[handle-1] );
LeaveCriticalSection( &WININET_cs ); LeaveCriticalSection( &WININET_cs );
@ -215,23 +215,19 @@ BOOL WININET_Release( object_header_t *info )
return TRUE; return TRUE;
} }
BOOL WININET_FreeHandle( HINTERNET hinternet ) static void invalidate_handle( HINTERNET hinternet )
{ {
BOOL ret = FALSE;
UINT_PTR handle = (UINT_PTR) hinternet; UINT_PTR handle = (UINT_PTR) hinternet;
object_header_t *info = NULL, *child, *next; object_header_t *info = NULL, *child, *next;
EnterCriticalSection( &WININET_cs ); EnterCriticalSection( &WININET_cs );
if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) ) if(handle && handle < handle_table_size)
{ {
handle--; if(handle_table[handle]) {
if( WININET_Handles[handle] ) info = handle_table[handle];
{
info = WININET_Handles[handle];
TRACE( "destroying handle %ld for object %p\n", handle+1, info); TRACE( "destroying handle %ld for object %p\n", handle+1, info);
WININET_Handles[handle] = NULL; handle_table[handle] = NULL;
ret = TRUE;
} }
} }
@ -247,19 +243,17 @@ BOOL WININET_FreeHandle( HINTERNET hinternet )
{ {
TRACE( "freeing child handle %ld for parent handle %ld\n", TRACE( "freeing child handle %ld for parent handle %ld\n",
(UINT_PTR)child->hInternet, handle+1); (UINT_PTR)child->hInternet, handle+1);
WININET_FreeHandle( child->hInternet ); invalidate_handle( child->hInternet );
} }
WININET_Release( info ); WININET_Release( info );
} }
EnterCriticalSection( &WININET_cs ); EnterCriticalSection( &WININET_cs );
if( WININET_dwNextHandle > handle && !WININET_Handles[handle] ) if(next_handle > handle && !handle_table[handle])
WININET_dwNextHandle = handle; next_handle = handle;
LeaveCriticalSection( &WININET_cs ); LeaveCriticalSection( &WININET_cs );
return ret;
} }
/*********************************************************************** /***********************************************************************
@ -843,6 +837,7 @@ HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
{ {
appinfo_t *lpwai = NULL; appinfo_t *lpwai = NULL;
HINTERNET handle = NULL; HINTERNET handle = NULL;
DWORD res;
if (TRACE_ON(wininet)) { if (TRACE_ON(wininet)) {
#define FE(x) { x, #x } #define FE(x) { x, #x }
@ -887,11 +882,11 @@ HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
lpwai->lpszProxyUsername = NULL; lpwai->lpszProxyUsername = NULL;
lpwai->lpszProxyPassword = NULL; lpwai->lpszProxyPassword = NULL;
handle = WININET_AllocHandle( &lpwai->hdr ); res = alloc_handle(&lpwai->hdr, &handle);
if( !handle ) if(res != ERROR_SUCCESS)
{ {
HeapFree( GetProcessHeap(), 0, lpwai ); HeapFree( GetProcessHeap(), 0, lpwai );
INTERNET_SetLastError(ERROR_OUTOFMEMORY); INTERNET_SetLastError(res);
goto lend; goto lend;
} }
@ -1142,7 +1137,7 @@ HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
return NULL; return NULL;
} }
hIC = (appinfo_t*)WININET_GetObject( hInternet ); hIC = (appinfo_t*)get_handle_object( hInternet );
if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) ) if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
{ {
res = ERROR_INVALID_HANDLE; res = ERROR_INVALID_HANDLE;
@ -1249,7 +1244,7 @@ BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
TRACE("\n"); TRACE("\n");
hdr = WININET_GetObject(hFind); hdr = get_handle_object(hFind);
if(!hdr) { if(!hdr) {
WARN("Invalid handle\n"); WARN("Invalid handle\n");
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
@ -1286,7 +1281,7 @@ BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
TRACE("%p\n",hInternet); TRACE("%p\n",hInternet);
lpwh = WININET_GetObject( hInternet ); lpwh = get_handle_object( hInternet );
if (NULL == lpwh) if (NULL == lpwh)
{ {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
@ -1294,7 +1289,7 @@ BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
} }
WININET_Release( lpwh ); WININET_Release( lpwh );
WININET_FreeHandle( hInternet ); invalidate_handle( hInternet );
return TRUE; return TRUE;
} }
@ -2023,7 +2018,7 @@ INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
TRACE("%p\n", hInternet); TRACE("%p\n", hInternet);
if (!(lpwh = WININET_GetObject(hInternet))) if (!(lpwh = get_handle_object(hInternet)))
return INTERNET_INVALID_STATUS_CALLBACK; return INTERNET_INVALID_STATUS_CALLBACK;
retVal = set_status_callback(lpwh, lpfnIntCB, FALSE); retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
@ -2051,7 +2046,7 @@ INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
TRACE("%p\n", hInternet); TRACE("%p\n", hInternet);
if (!(lpwh = WININET_GetObject(hInternet))) if (!(lpwh = get_handle_object(hInternet)))
return INTERNET_INVALID_STATUS_CALLBACK; return INTERNET_INVALID_STATUS_CALLBACK;
retVal = set_status_callback(lpwh, lpfnIntCB, TRUE); retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
@ -2088,7 +2083,7 @@ BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten); TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
lpwh = WININET_GetObject( hFile ); lpwh = get_handle_object( hFile );
if (!lpwh) { if (!lpwh) {
WARN("Invalid handle\n"); WARN("Invalid handle\n");
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
@ -2128,7 +2123,7 @@ BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead); TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
hdr = WININET_GetObject(hFile); hdr = get_handle_object(hFile);
if (!hdr) { if (!hdr) {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
@ -2182,7 +2177,7 @@ BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOu
TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext); TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
hdr = WININET_GetObject(hFile); hdr = get_handle_object(hFile);
if (!hdr) { if (!hdr) {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
@ -2214,7 +2209,7 @@ BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext); TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
hdr = WININET_GetObject(hFile); hdr = get_handle_object(hFile);
if (!hdr) { if (!hdr) {
INTERNET_SetLastError(ERROR_INVALID_HANDLE); INTERNET_SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
@ -2440,7 +2435,7 @@ BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength); TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
if(hInternet) { if(hInternet) {
hdr = WININET_GetObject(hInternet); hdr = get_handle_object(hInternet);
if (hdr) { if (hdr) {
res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE); res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
WININET_Release(hdr); WININET_Release(hdr);
@ -2473,7 +2468,7 @@ BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength); TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
if(hInternet) { if(hInternet) {
hdr = WININET_GetObject(hInternet); hdr = get_handle_object(hInternet);
if (hdr) { if (hdr) {
res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE); res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
WININET_Release(hdr); WININET_Release(hdr);
@ -2506,7 +2501,7 @@ BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength); TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
lpwhh = (object_header_t*) WININET_GetObject( hInternet ); lpwhh = (object_header_t*) get_handle_object( hInternet );
if(lpwhh && lpwhh->vtbl->SetOption) { if(lpwhh && lpwhh->vtbl->SetOption) {
DWORD res; DWORD res;
@ -2764,7 +2759,7 @@ BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
{ {
object_header_t *lpwh; object_header_t *lpwh;
if (!(lpwh = WININET_GetObject(hInternet))) if (!(lpwh = get_handle_object(hInternet)))
{ {
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE); INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
return FALSE; return FALSE;
@ -3379,7 +3374,7 @@ HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
goto lend; goto lend;
} }
hIC = (appinfo_t*)WININET_GetObject( hInternet ); hIC = (appinfo_t*)get_handle_object( hInternet );
if (NULL == hIC || hIC->hdr.htype != WH_HINIT) { if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE); SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
goto lend; goto lend;
@ -3679,7 +3674,7 @@ BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext); TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
hdr = WININET_GetObject( hFile ); hdr = get_handle_object( hFile );
if (!hdr) { if (!hdr) {
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;

View File

@ -58,6 +58,31 @@ typedef struct
DWORD security_flags; DWORD security_flags;
} WININET_NETCONNECTION; } WININET_NETCONNECTION;
static inline void * __WINE_ALLOC_SIZE(1) heap_alloc(size_t len)
{
return HeapAlloc(GetProcessHeap(), 0, len);
}
static inline void * __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t len)
{
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
}
static inline void * __WINE_ALLOC_SIZE(2) heap_realloc(void *mem, size_t len)
{
return HeapReAlloc(GetProcessHeap(), 0, mem, len);
}
static inline void * __WINE_ALLOC_SIZE(2) heap_realloc_zero(void *mem, size_t len)
{
return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len);
}
static inline BOOL heap_free(void *mem)
{
return HeapFree(GetProcessHeap(), 0, mem);
}
static inline LPWSTR heap_strdupW(LPCWSTR str) static inline LPWSTR heap_strdupW(LPCWSTR str)
{ {
LPWSTR ret = NULL; LPWSTR ret = NULL;
@ -409,11 +434,10 @@ typedef struct WORKREQ
} WORKREQUEST, *LPWORKREQUEST; } WORKREQUEST, *LPWORKREQUEST;
HINTERNET WININET_AllocHandle( object_header_t *info ); DWORD alloc_handle( object_header_t*, HINTERNET*);
object_header_t *WININET_GetObject( HINTERNET hinternet ); object_header_t *get_handle_object( HINTERNET hinternet );
object_header_t *WININET_AddRef( object_header_t *info ); object_header_t *WININET_AddRef( object_header_t *info );
BOOL WININET_Release( object_header_t *info ); BOOL WININET_Release( object_header_t *info );
BOOL WININET_FreeHandle( HINTERNET hinternet );
DWORD INET_QueryOption( object_header_t *, DWORD, void *, DWORD *, BOOL ); DWORD INET_QueryOption( object_header_t *, DWORD, void *, DWORD *, BOOL );