2008-08-15 14:41:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* Based on the handle implementation from wininet.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2019-11-20 14:29:08 +01:00
|
|
|
#include "ws2tcpip.h"
|
2008-08-15 14:41:36 +02:00
|
|
|
#include "winhttp.h"
|
|
|
|
|
2018-08-27 14:36:38 +02:00
|
|
|
#include "wine/debug.h"
|
2008-08-15 14:41:36 +02:00
|
|
|
#include "winhttp_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
|
|
|
|
|
|
|
|
#define HANDLE_CHUNK_SIZE 0x10
|
|
|
|
|
|
|
|
static CRITICAL_SECTION handle_cs;
|
|
|
|
static CRITICAL_SECTION_DEBUG handle_cs_debug =
|
|
|
|
{
|
|
|
|
0, 0, &handle_cs,
|
|
|
|
{ &handle_cs_debug.ProcessLocksList, &handle_cs_debug.ProcessLocksList },
|
|
|
|
0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
2018-11-15 12:38:50 +01:00
|
|
|
static struct object_header **handles;
|
2008-08-15 14:41:36 +02:00
|
|
|
static ULONG_PTR next_handle;
|
|
|
|
static ULONG_PTR max_handles;
|
|
|
|
|
2018-11-15 12:38:50 +01:00
|
|
|
struct object_header *addref_object( struct object_header *hdr )
|
2008-08-15 14:41:36 +02:00
|
|
|
{
|
|
|
|
ULONG refs = InterlockedIncrement( &hdr->refs );
|
2022-02-08 11:20:54 +01:00
|
|
|
TRACE( "%p -> refcount = %lu\n", hdr, refs );
|
2008-08-15 14:41:36 +02:00
|
|
|
return hdr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:38:50 +01:00
|
|
|
struct object_header *grab_object( HINTERNET hinternet )
|
2008-08-15 14:41:36 +02:00
|
|
|
{
|
2018-11-15 12:38:50 +01:00
|
|
|
struct object_header *hdr = NULL;
|
2008-08-15 14:41:36 +02:00
|
|
|
ULONG_PTR handle = (ULONG_PTR)hinternet;
|
|
|
|
|
|
|
|
EnterCriticalSection( &handle_cs );
|
|
|
|
|
|
|
|
if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
|
|
|
|
hdr = addref_object( handles[handle - 1] );
|
|
|
|
|
|
|
|
LeaveCriticalSection( &handle_cs );
|
|
|
|
|
2022-02-08 11:20:54 +01:00
|
|
|
TRACE( "handle %Ix -> %p\n", handle, hdr );
|
2008-08-15 14:41:36 +02:00
|
|
|
return hdr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:38:50 +01:00
|
|
|
void release_object( struct object_header *hdr )
|
2008-08-15 14:41:36 +02:00
|
|
|
{
|
|
|
|
ULONG refs = InterlockedDecrement( &hdr->refs );
|
2022-02-08 11:20:54 +01:00
|
|
|
TRACE( "object %p refcount = %lu\n", hdr, refs );
|
2008-08-15 14:41:36 +02:00
|
|
|
if (!refs)
|
|
|
|
{
|
2018-11-22 21:15:54 +01:00
|
|
|
if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (struct request *)hdr );
|
2008-08-26 11:03:40 +02:00
|
|
|
|
2008-08-15 14:43:07 +02:00
|
|
|
send_callback( hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, &hdr->handle, sizeof(HINTERNET) );
|
|
|
|
|
2022-02-08 11:20:54 +01:00
|
|
|
TRACE( "destroying object %p\n", hdr );
|
2008-08-15 14:41:36 +02:00
|
|
|
hdr->vtbl->destroy( hdr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:38:50 +01:00
|
|
|
HINTERNET alloc_handle( struct object_header *hdr )
|
2008-08-15 14:41:36 +02:00
|
|
|
{
|
2018-11-15 12:38:50 +01:00
|
|
|
struct object_header **p;
|
2016-06-26 01:53:28 +02:00
|
|
|
ULONG_PTR handle, num;
|
2008-08-15 14:41:36 +02:00
|
|
|
|
2016-06-26 01:53:28 +02:00
|
|
|
hdr->handle = NULL;
|
2008-08-15 14:41:36 +02:00
|
|
|
|
|
|
|
EnterCriticalSection( &handle_cs );
|
|
|
|
if (!max_handles)
|
|
|
|
{
|
|
|
|
num = HANDLE_CHUNK_SIZE;
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(p = calloc( 1, sizeof(ULONG_PTR) * num ))) goto end;
|
2008-08-15 14:41:36 +02:00
|
|
|
handles = p;
|
|
|
|
max_handles = num;
|
|
|
|
}
|
|
|
|
if (max_handles == next_handle)
|
|
|
|
{
|
2021-06-08 16:27:44 +02:00
|
|
|
size_t new_size, old_size = max_handles * sizeof(ULONG_PTR);
|
2011-12-01 13:02:53 +01:00
|
|
|
num = max_handles * 2;
|
2021-06-08 16:27:44 +02:00
|
|
|
new_size = num * sizeof(ULONG_PTR);
|
|
|
|
if (!(p = realloc( handles, new_size ))) goto end;
|
|
|
|
memset( (char *)p + old_size, 0, new_size - old_size );
|
2008-08-15 14:41:36 +02:00
|
|
|
handles = p;
|
|
|
|
max_handles = num;
|
|
|
|
}
|
|
|
|
handle = next_handle;
|
|
|
|
if (handles[handle]) ERR("handle isn't free but should be\n");
|
|
|
|
|
|
|
|
handles[handle] = addref_object( hdr );
|
2016-06-26 01:53:28 +02:00
|
|
|
hdr->handle = (HINTERNET)(handle + 1);
|
2016-06-26 01:45:36 +02:00
|
|
|
while ((next_handle < max_handles) && handles[next_handle]) next_handle++;
|
2008-08-15 14:41:36 +02:00
|
|
|
|
|
|
|
end:
|
|
|
|
LeaveCriticalSection( &handle_cs );
|
2016-06-26 01:53:28 +02:00
|
|
|
return hdr->handle;
|
2008-08-15 14:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL free_handle( HINTERNET hinternet )
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
ULONG_PTR handle = (ULONG_PTR)hinternet;
|
2020-06-22 21:39:20 +02:00
|
|
|
struct object_header *hdr = NULL;
|
2008-08-15 14:41:36 +02:00
|
|
|
|
|
|
|
EnterCriticalSection( &handle_cs );
|
|
|
|
|
|
|
|
if ((handle > 0) && (handle <= max_handles))
|
|
|
|
{
|
|
|
|
handle--;
|
|
|
|
if (handles[handle])
|
|
|
|
{
|
|
|
|
hdr = handles[handle];
|
2022-02-08 11:20:54 +01:00
|
|
|
TRACE( "destroying handle %Ix for object %p\n", handle + 1, hdr );
|
2008-08-15 14:41:36 +02:00
|
|
|
handles[handle] = NULL;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection( &handle_cs );
|
|
|
|
|
2020-06-22 21:39:20 +02:00
|
|
|
if (hdr) release_object( hdr );
|
2008-08-15 14:41:36 +02:00
|
|
|
|
|
|
|
EnterCriticalSection( &handle_cs );
|
|
|
|
if (next_handle > handle && !handles[handle]) next_handle = handle;
|
|
|
|
LeaveCriticalSection( &handle_cs );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|