HAnother member RAII'ed.

This commit is contained in:
2025-08-18 02:53:32 -04:00
parent 2d6deff51b
commit b1e1f8e98e

View File

@ -55,7 +55,7 @@ struct CacheEntry_t
char *TypeHdr; /**< MIME type string as from the HTTP Header */ char *TypeHdr; /**< MIME type string as from the HTTP Header */
char *TypeMeta; /**< MIME type string from META HTTP-EQUIV */ char *TypeMeta; /**< MIME type string from META HTTP-EQUIV */
char *TypeNorm; /**< MIME type string normalized */ char *TypeNorm; /**< MIME type string normalized */
Dstr *Header; /**< HTTP header */ std::string Header; /**< HTTP header */
std::unique_ptr< const DilloUrl > Location; /**< New URI for redirects */ std::unique_ptr< const DilloUrl > Location; /**< New URI for redirects */
Dlist *Auth; /**< Authentication fields */ Dlist *Auth; /**< Authentication fields */
Dstr *Data; /**< Pointer to raw data */ Dstr *Data; /**< Pointer to raw data */
@ -198,7 +198,6 @@ static void Cache_entry_init(CacheEntry_t *NewEntry, const DilloUrl *Url)
NewEntry->TypeHdr = NULL; NewEntry->TypeHdr = NULL;
NewEntry->TypeMeta = NULL; NewEntry->TypeMeta = NULL;
NewEntry->TypeNorm = NULL; NewEntry->TypeNorm = NULL;
NewEntry->Header = dStr_new("");
NewEntry->Location = NULL; NewEntry->Location = NULL;
NewEntry->Auth = NULL; NewEntry->Auth = NULL;
NewEntry->Data = dStr_sized_new(8*1024); NewEntry->Data = dStr_sized_new(8*1024);
@ -304,7 +303,6 @@ CacheEntry_t::~CacheEntry_t()
dFree(entry->TypeHdr); dFree(entry->TypeHdr);
dFree(entry->TypeMeta); dFree(entry->TypeMeta);
dFree(entry->TypeNorm); dFree(entry->TypeNorm);
dStr_free(entry->Header, TRUE);
Cache_auth_free(entry->Auth); Cache_auth_free(entry->Auth);
dStr_free(entry->Data, 1); dStr_free(entry->Data, 1);
dStr_free(entry->UTF8Data, 1); dStr_free(entry->UTF8Data, 1);
@ -659,8 +657,8 @@ static Dlist *Cache_parse_multiple_fields(const char *header,
*/ */
static void Cache_parse_header(CacheEntry_t *entry) static void Cache_parse_header(CacheEntry_t *entry)
{ {
char *header = entry->Header->str; const char *header = entry->Header.c_str();
bool_t server1point0 = !strncmp(entry->Header->str, "HTTP/1.0", 8); bool_t server1point0 = !strncmp(entry->Header.c_str(), "HTTP/1.0", 8);
char *Length, *Type, *location_str, *encoding, *connection, *hsts; char *Length, *Type, *location_str, *encoding, *connection, *hsts;
#ifndef DISABLE_COOKIES #ifndef DISABLE_COOKIES
Dlist *Cookies; Dlist *Cookies;
@ -671,13 +669,12 @@ static void Cache_parse_header(CacheEntry_t *entry)
_MSG("Cache_parse_header\n"); _MSG("Cache_parse_header\n");
if (entry->Header->len > 12) { if (entry->Header.size() > 12) {
if (header[9] == '1' && header[10] == '0' && header[11] == '0') { if (header[9] == '1' && header[10] == '0' && header[11] == '0') {
/* 100: Continue. The "real" header has not come yet. */ /* 100: Continue. The "real" header has not come yet. */
MSG("An actual 100 Continue header!\n"); MSG("An actual 100 Continue header!\n");
entry->Flags &= ~CA_GotHeader; entry->Flags &= ~CA_GotHeader;
dStr_free(entry->Header, 1); entry->Header = "";
entry->Header = dStr_new("");
return; return;
} }
if (header[9] == '3' && header[10] == '0' && if (header[9] == '3' && header[10] == '0' &&
@ -825,27 +822,27 @@ static int Cache_get_header(CacheEntry_t *entry,
const char *buf, size_t buf_size) const char *buf, size_t buf_size)
{ {
size_t N, i; size_t N, i;
Dstr *hdr = entry->Header; std::string &hdr = entry->Header;
/* Header finishes when N = 2 */ /* Header finishes when N = 2 */
N = (hdr->len && hdr->str[hdr->len - 1] == '\n'); N = (not hdr.empty() && hdr.back() == '\n');
for (i = 0; i < buf_size && N < 2; ++i) { for (i = 0; i < buf_size && N < 2; ++i) {
if (buf[i] == '\r' || !buf[i]) if (buf[i] == '\r' || !buf[i])
continue; continue;
if (N == 1 && (buf[i] == ' ' || buf[i] == '\t')) { if (N == 1 && (buf[i] == ' ' || buf[i] == '\t')) {
/* unfold multiple-line header */ /* unfold multiple-line header */
_MSG("Multiple-line header!\n"); _MSG("Multiple-line header!\n");
dStr_erase(hdr, hdr->len - 1, 1); hdr.pop_back();
} }
N = (buf[i] == '\n') ? N + 1 : 0; N = (buf[i] == '\n') ? N + 1 : 0;
dStr_append_c(hdr, buf[i]); hdr+= buf[i];
} }
if (N == 2) { if (N == 2) {
/* Got whole header */ /* Got whole header */
_MSG("Header [buf_size=%d]\n%s", i, hdr->str); _MSG("Header [buf_size=%d]\n%s", i, hdr.c_str());
entry->Flags |= CA_GotHeader; entry->Flags |= CA_GotHeader;
dStr_fit(hdr); hdr.shrink_to_fit();
/* Return number of header bytes in 'buf' [1 based] */ /* Return number of header bytes in 'buf' [1 based] */
return i; return i;
} }
@ -1117,22 +1114,23 @@ static void Cache_null_client(int Op, CacheClient_t *Client)
return; return;
} }
typedef struct { struct Cache_savelink_t
{
~Cache_savelink_t();
BrowserWindow *bw; BrowserWindow *bw;
DilloUrl *url; std::unique_ptr< DilloUrl > url;
} Cache_savelink_t; };
/** /**
* Save link from behind a timeout so that Cache_process_queue() can * Save link from behind a timeout so that Cache_process_queue() can
* get on with its work. * get on with its work.
*/ */
static void Cache_savelink_cb(void *vdata) Cache_savelink_t::~Cache_savelink_t()
{ {
Cache_savelink_t *data = (Cache_savelink_t*) vdata; Cache_savelink_t *data= this;
a_UIcmd_save_link(data->bw, data->url); a_UIcmd_save_link(data->bw, data->url.get());
delete data->url;
dFree(data);
} }
/** /**
@ -1331,10 +1329,10 @@ static CacheEntry_t *Cache_process_queue(CacheEntry_t *entry)
/* Remove entry when 'conn' is already done */ /* Remove entry when 'conn' is already done */
Cache_entry_remove(NULL, url.get()); Cache_entry_remove(NULL, url.get());
if (a_Cache_download_enabled(url.get())) { if (a_Cache_download_enabled(url.get())) {
Cache_savelink_t *data = dNew(Cache_savelink_t, 1); auto data = std::make_unique< Cache_savelink_t >();
data->bw = Client_bw; data->bw = Client_bw;
data->url = a_Url_dup(url.get()).release(); data->url = a_Url_dup(url.get());
Timeout::add(0.0, [=]{ Cache_savelink_cb( data ); } ); Timeout::add(0.0, [data= data.release()]{ delete data; } );
} }
} }
} else if (entry->Auth && !(entry->Flags & CA_InProgress)) { } else if (entry->Auth && !(entry->Flags & CA_InProgress)) {