Many members of DilloWeb are now C++ RAII.

This commit is contained in:
2025-08-01 00:34:12 -04:00
parent 4093da9640
commit 58eceb9a46
8 changed files with 44 additions and 48 deletions

View File

@ -386,7 +386,7 @@ static Dstr *Http_make_content_type(const DilloUrl *url)
static Dstr *Http_make_query_str(DilloWeb *web, bool_t use_proxy, bool_t use_tls) static Dstr *Http_make_query_str(DilloWeb *web, bool_t use_proxy, bool_t use_tls)
{ {
char *ptr, *cookies, *referer, *auth; char *ptr, *cookies, *referer, *auth;
const DilloUrl *url = web->url; const DilloUrl *url = web->url.get();
Dstr *query = dStr_new(""), Dstr *query = dStr_new(""),
*request_uri = dStr_new(""), *request_uri = dStr_new(""),
*proxy_auth = dStr_new(""); *proxy_auth = dStr_new("");
@ -416,7 +416,7 @@ static Dstr *Http_make_query_str(DilloWeb *web, bool_t use_proxy, bool_t use_tls
URL_QUERY(url)); URL_QUERY(url));
} }
cookies = a_Cookies_get_query(url, web->requester); cookies = a_Cookies_get_query(url, web->requester.get());
auth = a_Auth_get_auth_str(url, request_uri->str); auth = a_Auth_get_auth_str(url, request_uri->str);
referer = Http_get_referer(url); referer = Http_get_referer(url);
if (URL_FLAGS(url) & URL_Post) { if (URL_FLAGS(url) & URL_Post) {
@ -807,15 +807,15 @@ static int Http_get(ChainLink *Info, void *Data1)
S->Info = Info; S->Info = Info;
/* Proxy support */ /* Proxy support */
if (Http_must_use_proxy(URL_HOST(S->web->url))) { if (Http_must_use_proxy(URL_HOST(S->web->url.get()))) {
url = HTTP_Proxy; url = HTTP_Proxy;
S->flags |= HTTP_SOCKET_USE_PROXY; S->flags |= HTTP_SOCKET_USE_PROXY;
} else { } else {
url = S->web->url; url = S->web->url.get();
} }
hostname = dStrdup(URL_HOST(url)); hostname = dStrdup(URL_HOST(url));
S->connect_port = URL_PORT(url); S->connect_port = URL_PORT(url);
S->url = a_Url_dup(S->web->url).release(); S->url = a_Url_dup(S->web->url.get()).release();
if (!dStrAsciiCasecmp(URL_SCHEME(S->url), "https")) if (!dStrAsciiCasecmp(URL_SCHEME(S->url), "https"))
S->flags |= HTTP_SOCKET_TLS; S->flags |= HTTP_SOCKET_TLS;

View File

@ -377,7 +377,7 @@ int a_Cache_open_url(void *web, CA_Callback_t Call, void *CbData)
int ClientKey; int ClientKey;
CacheEntry_t *entry; CacheEntry_t *entry;
DilloWeb *Web = reinterpret_cast< DilloWeb * >( web ); DilloWeb *Web = reinterpret_cast< DilloWeb * >( web );
DilloUrl *Url = Web->url; DilloUrl *Url = Web->url.get();
if (URL_FLAGS(Url) & URL_E2EQuery) { if (URL_FLAGS(Url) & URL_E2EQuery) {
/* remove current entry */ /* remove current entry */

View File

@ -307,12 +307,12 @@ static char *Capi_dpi_build_cmd(DilloWeb *web, char *server)
if (strcmp(server, "downloads") == 0) { if (strcmp(server, "downloads") == 0) {
/* let the downloads server get it */ /* let the downloads server get it */
cmd = a_Dpip_build_cmd("cmd=%s url=%s destination=%s user-agent=%s", cmd = a_Dpip_build_cmd("cmd=%s url=%s destination=%s user-agent=%s",
"download", URL_STR(web->url), web->filename, "download", URL_STR(web->url.get()), web->filename.value().c_str(),
prefs.http_user_agent); prefs.http_user_agent);
} else { } else {
/* For everyone else, the url string is enough... */ /* For everyone else, the url string is enough... */
cmd = a_Dpip_build_cmd("cmd=%s url=%s", "open_url", URL_STR(web->url)); cmd = a_Dpip_build_cmd("cmd=%s url=%s", "open_url", URL_STR(web->url.get()));
} }
return cmd; return cmd;
} }
@ -371,13 +371,13 @@ static bool_t Capi_request_permitted(DilloWeb *web)
*/ */
if (dStrAsciiCasecmp(s, "https") && dStrAsciiCasecmp(s, "data")) { if (dStrAsciiCasecmp(s, "https") && dStrAsciiCasecmp(s, "data")) {
MSG("capi: Blocked mixed content: %s -> %s\n", MSG("capi: Blocked mixed content: %s -> %s\n",
URL_STR(web->requester), URL_STR(web->url)); URL_STR(web->requester.get()), URL_STR(web->url.get()));
return FALSE; return FALSE;
} }
} }
if (a_Capi_get_flags(web->url) & CAPI_IsCached || if (a_Capi_get_flags(web->url.get()) & CAPI_IsCached ||
a_Domain_permit(web->requester, web->url)) { a_Domain_permit(web->requester.get(), web->url.get())) {
permit = TRUE; permit = TRUE;
} }
return permit; return permit;
@ -401,53 +401,53 @@ int a_Capi_open_url(DilloWeb *web, CA_Callback_t Call, void *CbData)
if (Capi_request_permitted(web)) { if (Capi_request_permitted(web)) {
/* reload test */ /* reload test */
reload = (!(a_Capi_get_flags(web->url) & CAPI_IsCached) || reload = (!(a_Capi_get_flags(web->url.get()) & CAPI_IsCached) ||
(URL_FLAGS(web->url) & URL_E2EQuery)); (URL_FLAGS(web->url.get()) & URL_E2EQuery));
if (web->flags & WEB_Download) { if (web->flags & WEB_Download) {
/* download request: if cached save from cache, else /* download request: if cached save from cache, else
* for http, ftp or https, use the downloads dpi */ * for http, ftp or https, use the downloads dpi */
if (a_Capi_get_flags_with_redirection(web->url) & CAPI_IsCached) { if (a_Capi_get_flags_with_redirection(web->url.get()) & CAPI_IsCached) {
if (web->filename) { if (web->filename.has_value()) {
if ((web->stream = fopen(web->filename, "w"))) { if ((web->stream = fopen(web->filename.value().c_str(), "w"))) {
use_cache = 1; use_cache = 1;
} else { } else {
MSG_WARN("Cannot open \"%s\" for writing: %s.\n", MSG_WARN("Cannot open \"%s\" for writing: %s.\n",
web->filename, dStrerror(errno)); web->filename.value().c_str(), dStrerror(errno));
} }
} }
} else if (a_Cache_download_enabled(web->url)) { } else if (a_Cache_download_enabled(web->url.get())) {
server = const_cast< char * >( "downloads" ); server = const_cast< char * >( "downloads" );
cmd = Capi_dpi_build_cmd(web, server); cmd = Capi_dpi_build_cmd(web, server);
a_Capi_dpi_send_cmd(web->url, web->bw, cmd, server, 1); a_Capi_dpi_send_cmd(web->url.get(), web->bw, cmd, server, 1);
dFree(cmd); dFree(cmd);
} else { } else {
MSG_WARN("Ignoring download request for '%s': " MSG_WARN("Ignoring download request for '%s': "
"not in cache and not downloadable.\n", "not in cache and not downloadable.\n",
URL_STR(web->url)); URL_STR(web->url.get()));
} }
} else if (Capi_url_uses_dpi(web->url, &server)) { } else if (Capi_url_uses_dpi(web->url.get(), &server)) {
/* dpi request */ /* dpi request */
if ((safe = a_Capi_dpi_verify_request(web->bw, web->url))) { if ((safe = a_Capi_dpi_verify_request(web->bw, web->url.get()))) {
if (dStrAsciiCasecmp(scheme, "dpi") == 0) { if (dStrAsciiCasecmp(scheme, "dpi") == 0) {
if (strcmp(server, "vsource") == 0) { if (strcmp(server, "vsource") == 0) {
/* allow "view source" reload upon user request */ /* allow "view source" reload upon user request */
} else { } else {
/* make the other "dpi:/" prefixed urls always reload. */ /* make the other "dpi:/" prefixed urls always reload. */
a_Url_set_flags(web->url, URL_FLAGS(web->url) |URL_E2EQuery); a_Url_set_flags(web->url.get(), URL_FLAGS(web->url.get()) |URL_E2EQuery);
reload = 1; reload = 1;
} }
} }
if (reload) { if (reload) {
a_Capi_conn_abort_by_url(web->url); a_Capi_conn_abort_by_url(web->url.get());
/* Send dpip command */ /* Send dpip command */
_MSG("a_Capi_open_url, reload url='%s'\n", URL_STR(web->url)); _MSG("a_Capi_open_url, reload url='%s'\n", URL_STR(web->url.get()));
cmd = Capi_dpi_build_cmd(web, server); cmd = Capi_dpi_build_cmd(web, server);
a_Capi_dpi_send_cmd(web->url, web->bw, cmd, server, 1); a_Capi_dpi_send_cmd(web->url.get(), web->bw, cmd, server, 1);
dFree(cmd); dFree(cmd);
if (strcmp(server, "vsource") == 0) { if (strcmp(server, "vsource") == 0) {
Capi_dpi_send_source(web->bw, web->url); Capi_dpi_send_source(web->bw, web->url.get());
} }
} }
use_cache = 1; use_cache = 1;
@ -468,9 +468,9 @@ int a_Capi_open_url(DilloWeb *web, CA_Callback_t Call, void *CbData)
} }
#endif #endif
if (reload) { if (reload) {
a_Capi_conn_abort_by_url(web->url); a_Capi_conn_abort_by_url(web->url.get());
/* create a new connection and start the CCC operations */ /* create a new connection and start the CCC operations */
conn = Capi_conn_new(web->url, web->bw, const_cast< char * >( "http" ), const_cast< char * >( "none" )); conn = Capi_conn_new(web->url.get(), web->bw, const_cast< char * >( "http" ), const_cast< char * >( "none" ));
/* start the reception branch before the query one because the DNS /* start the reception branch before the query one because the DNS
* may callback immediately. This may avoid a race condition. */ * may callback immediately. This may avoid a race condition. */
a_Capi_ccc(OpStart, 2, BCK, a_Chain_new(), conn, const_cast< char * >( "http" )); a_Capi_ccc(OpStart, 2, BCK, a_Chain_new(), conn, const_cast< char * >( "http" ));

View File

@ -402,10 +402,10 @@ static void *Dicache_image(int ImgType, const char *MimeType, void *Ptr,
a_Image_ref(web->Image); a_Image_ref(web->Image);
} }
DicEntry = a_Dicache_get_entry(web->url, DIC_Last); DicEntry = a_Dicache_get_entry(web->url.get(), DIC_Last);
if (!DicEntry) { if (!DicEntry) {
/* Create an entry for this image... */ /* Create an entry for this image... */
DicEntry = Dicache_add_entry(web->url); DicEntry = Dicache_add_entry(web->url.get());
/* Attach a decoder */ /* Attach a decoder */
if (ImgType == DIC_Jpeg) { if (ImgType == DIC_Jpeg) {
DicEntry->Decoder = (CA_Callback_t)a_Jpeg_callback; DicEntry->Decoder = (CA_Callback_t)a_Jpeg_callback;
@ -494,7 +494,7 @@ void a_Dicache_callback(int Op, CacheClient_t *Client)
uint_t i; uint_t i;
DilloWeb *Web = reinterpret_cast< DilloWeb * >( Client->Web ); DilloWeb *Web = reinterpret_cast< DilloWeb * >( Client->Web );
DilloImage *Image = Web->Image; DilloImage *Image = Web->Image;
DICacheEntry *DicEntry = a_Dicache_get_entry(Web->url, DIC_Last); DICacheEntry *DicEntry = a_Dicache_get_entry(Web->url.get(), DIC_Last);
dReturn_if_fail ( DicEntry != NULL ); dReturn_if_fail ( DicEntry != NULL );

View File

@ -223,7 +223,7 @@ std::unique_ptr< DilloUrl > a_Html_url_new(DilloHtml *html,
void *a_Html_text(const char *Type, void *P, CA_Callback_t *Call, void **Data) void *a_Html_text(const char *Type, void *P, CA_Callback_t *Call, void **Data)
{ {
DilloWeb *web = (DilloWeb*)P; DilloWeb *web = (DilloWeb*)P;
DilloHtml *html = new DilloHtml(web->bw, web->url, Type); DilloHtml *html = new DilloHtml(web->bw, web->url.get(), Type);
*Data = (void*)html; *Data = (void*)html;
*Call = (CA_Callback_t)Html_callback; *Call = (CA_Callback_t)Html_callback;

View File

@ -551,7 +551,7 @@ void a_Nav_save_url(BrowserWindow *bw,
const DilloUrl *url, const char *filename) const DilloUrl *url, const char *filename)
{ {
DilloWeb *Web = a_Web_new(bw, url, NULL); DilloWeb *Web = a_Web_new(bw, url, NULL);
Web->filename = dStrdup(filename); Web->filename = filename;
Web->flags |= WEB_Download; Web->flags |= WEB_Download;
/* TODO: keep track of this client */ /* TODO: keep track of this client */
a_Capi_open_url(Web, Nav_save_cb, Web); a_Capi_open_url(Web, Nav_save_cb, Web);

View File

@ -73,7 +73,7 @@ int a_Web_dispatch_by_type (const char *Type, DilloWeb *Web,
style::createPerLength (0)); style::createPerLength (0));
/* Set a style for the widget */ /* Set a style for the widget */
StyleEngine styleEngine (layout, Web->url, Web->url, bw->zoom); StyleEngine styleEngine (layout, Web->url.get(), Web->url.get(), bw->zoom);
styleEngine.startElement ("body", Web->bw); styleEngine.startElement ("body", Web->bw);
std::unique_ptr< Widget > dw( reinterpret_cast< Widget* >( viewer(Type, Web, Call, Data) ) ); std::unique_ptr< Widget > dw( reinterpret_cast< Widget* >( viewer(Type, Web, Call, Data) ) );
@ -87,10 +87,10 @@ int a_Web_dispatch_by_type (const char *Type, DilloWeb *Web,
/* Set the page title with the bare filename (e.g. for images), /* Set the page title with the bare filename (e.g. for images),
* HTML pages with a <TITLE> tag will overwrite it later */ * HTML pages with a <TITLE> tag will overwrite it later */
const char *p = strrchr(URL_STR(Web->url), '/'); const char *p = strrchr(URL_STR(Web->url.get()), '/');
a_UIcmd_set_page_title(Web->bw, p ? p+1 : ""); a_UIcmd_set_page_title(Web->bw, p ? p+1 : "");
a_UIcmd_set_location_text(Web->bw, URL_STR(Web->url)); a_UIcmd_set_location_text(Web->bw, URL_STR(Web->url.get()));
/* Reset both progress bars */ /* Reset both progress bars */
a_UIcmd_set_page_prog(Web->bw, 0, 2); a_UIcmd_set_page_prog(Web->bw, 0, 2);
a_UIcmd_set_img_prog(Web->bw, 0, 0, 2); a_UIcmd_set_img_prog(Web->bw, 0, 0, 2);
@ -111,7 +111,7 @@ int a_Web_dispatch_by_type (const char *Type, DilloWeb *Web,
} }
MSG_HTTP("'%s' cannot be displayed as image; has media type '%s'\n", MSG_HTTP("'%s' cannot be displayed as image; has media type '%s'\n",
URL_STR(Web->url), Type); URL_STR(Web->url.get()), Type);
return -1; return -1;
} }
@ -125,12 +125,11 @@ DilloWeb* a_Web_new(BrowserWindow *bw, const DilloUrl *url,
DilloWeb *web= new DilloWeb; DilloWeb *web= new DilloWeb;
_MSG(" a_Web_new: ValidWebs ==> %d\n", dList_length(ValidWebs)); _MSG(" a_Web_new: ValidWebs ==> %d\n", dList_length(ValidWebs));
web->url = a_Url_dup(url).release(); web->url = a_Url_dup(url);
web->requester = a_Url_dup(requester).release(); web->requester = a_Url_dup(requester);
web->bw = bw; web->bw = bw;
web->flags = 0; web->flags = 0;
web->Image = NULL; web->Image = NULL;
web->filename = NULL;
web->stream = NULL; web->stream = NULL;
web->SavedBytes = 0; web->SavedBytes = 0;
web->bgColor = 0x000000; /* Dummy value will be overwritten web->bgColor = 0x000000; /* Dummy value will be overwritten
@ -153,10 +152,7 @@ int a_Web_valid(DilloWeb *web)
void a_Web_free(DilloWeb *web) void a_Web_free(DilloWeb *web)
{ {
if (!web) return; if (!web) return;
delete web->url;
delete web->requester;
a_Image_unref(web->Image); a_Image_unref(web->Image);
dFree(web->filename);
dList_remove(ValidWebs, (void *)web); dList_remove(ValidWebs, (void *)web);
_MSG("a_Web_free: ValidWebs=%d\n", dList_length(ValidWebs)); _MSG("a_Web_free: ValidWebs=%d\n", dList_length(ValidWebs));
delete web; delete web;

View File

@ -20,8 +20,8 @@ extern "C" {
struct DilloWeb { struct DilloWeb {
DilloUrl *url; /**< Requested URL */ std::unique_ptr< DilloUrl > url; /**< Requested URL */
DilloUrl *requester; /**< URL that caused this request, or std::unique_ptr< DilloUrl > requester; /**< URL that caused this request, or
**< NULL if user-initiated. */ **< NULL if user-initiated. */
BrowserWindow *bw; /**< The requesting browser window [reference] */ BrowserWindow *bw; /**< The requesting browser window [reference] */
int flags; /**< Additional info */ int flags; /**< Additional info */
@ -29,7 +29,7 @@ struct DilloWeb {
DilloImage *Image; /**< For image urls [reference] */ DilloImage *Image; /**< For image urls [reference] */
int32_t bgColor; /**< for image backgrounds */ int32_t bgColor; /**< for image backgrounds */
char *filename; /**< Variables for Local saving */ std::optional< std::string > filename; /**< Variables for Local saving */
FILE *stream; FILE *stream;
int SavedBytes; int SavedBytes;
}; };