More modern code for FD management in HTTP.

This commit is contained in:
2025-06-01 05:25:17 -04:00
parent 88ff5aa44d
commit 21b581afb9

View File

@ -44,6 +44,10 @@
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
/* Used to send a message to the bw's status bar */
#define MSG_BW(web, root, ...) \
@ -87,10 +91,10 @@ typedef struct {
Dlist *queue;
} Server_t;
typedef struct {
struct FdMapEntry_t {
int fd;
int skey;
} FdMapEntry_t;
};
static void Http_socket_enqueue(Server_t *srv, SocketData_t* sock);
static Server_t *Http_server_get(const char *host, uint_t port, bool_t https);
@ -113,7 +117,7 @@ static Dlist *servers;
/* TODO: If fd_map will stick around in its present form (FDs and SocketData_t)
* then consider whether having both this and ValidSocks is necessary.
*/
static Dlist *fd_map;
static std::map< int, std::unique_ptr< FdMapEntry_t > > fd_map;
/**
* Initialize proxy vars and Accept-Language header
@ -139,7 +143,6 @@ int a_Http_init(void)
*/
servers = dList_new(5);
fd_map = dList_new(20);
return 0;
}
@ -187,16 +190,16 @@ static int Http_fd_map_cmp(const void *v1, const void *v2)
static void Http_fd_map_add_entry(SocketData_t *sd)
{
FdMapEntry_t *e = dNew0(FdMapEntry_t, 1);
auto e = std::make_unique< FdMapEntry_t >();
e->fd = sd->SockFD;
e->skey = VOIDP2INT(sd->Info->LocalKey);
if (dList_find_custom(fd_map, INT2VOIDP(e->fd), Http_fd_map_cmp)) {
if (fd_map.contains( e->fd )) {
MSG_ERR("FD ENTRY ALREADY FOUND FOR %d\n", e->fd);
assert(0);
}
dList_append(fd_map, e);
fd_map.emplace( e->fd, std::move( e ) );
}
/**
@ -204,11 +207,8 @@ static void Http_fd_map_add_entry(SocketData_t *sd)
*/
static void Http_fd_map_remove_entry(int fd)
{
void *data = dList_find_custom(fd_map, INT2VOIDP(fd), Http_fd_map_cmp);
if (data) {
dList_remove_fast(fd_map, data);
dFree(data);
if (fd_map.contains( fd )) {
fd_map.erase( fd );
} else {
MSG("FD ENTRY NOT FOUND FOR %d\n", fd);
}
@ -217,8 +217,8 @@ static void Http_fd_map_remove_entry(int fd)
void a_Http_connect_done(int fd, bool_t success)
{
SocketData_t *sd;
FdMapEntry_t *fme = reinterpret_cast< FdMapEntry_t * >( dList_find_custom(fd_map, INT2VOIDP(fd),
Http_fd_map_cmp) );
std::optional< FdMapEntry_t > fme;
if( fd_map.contains( fd ) ) fme= *fd_map.at( fd );
if (fme && (sd = reinterpret_cast< SocketData_t * >( a_Klist_get_data(ValidSocks, fme->skey) ))) {
ChainLink *info = sd->Info;
@ -237,7 +237,7 @@ void a_Http_connect_done(int fd, bool_t success)
dFree(info);
}
} else {
MSG("**** but no luck with fme %p or sd\n", (void *) fme);
MSG("**** but no luck with fme %p or sd\n", (void *) &*fme);
}
}
@ -1025,9 +1025,8 @@ void a_Http_ccc(int Op, int Branch, int Dir, ChainLink *Info,
if (Data2) {
if (!strcmp(reinterpret_cast< const char * >( Data2 ), "FD")) {
int fd = *(int*)Data1;
FdMapEntry_t *fme = reinterpret_cast< FdMapEntry_t * >( dList_find_custom(fd_map, INT2VOIDP(fd),
Http_fd_map_cmp) );
Info->LocalKey = INT2VOIDP(fme->skey);
const FdMapEntry_t &fme = *fd_map.at( fd );
Info->LocalKey = INT2VOIDP(fme.skey);
a_Chain_bcb(OpSend, Info, Data1, Data2);
} else if (!strcmp(reinterpret_cast< const char * >( Data2 ), "reply_complete")) {
a_Chain_bfcb(OpEnd, Info, NULL, NULL);
@ -1128,14 +1127,7 @@ static void Http_servers_remove_all(void)
static void Http_fd_map_remove_all(void)
{
FdMapEntry_t *fme;
int i, n = dList_length(fd_map);
for (i = 0; i < n; i++) {
fme = (FdMapEntry_t *) dList_nth_data(fd_map, i);
dFree(fme);
}
dList_free(fd_map);
fd_map.clear();
}
/**