Begin converting URL struct into modern C++ class

This commit is contained in:
2025-04-12 22:20:34 -04:00
parent 26b903f02f
commit 27ae7d91c1
18 changed files with 171 additions and 104 deletions

View File

@ -538,6 +538,23 @@ const char *dStr_printable(Dstr *in, int maxlen)
return out->str; return out->str;
} }
extern "C++"
{
#include <Alepha/AutoRAII.h>
}
std::string
dStr_printable_std(const std::string &in, const int maxlen)
{
const auto wrapped= Alepha::AutoRAII
{
[&]{ return dStr_new( in.c_str() ); },
[]( auto d ) { dStr_free( d, true ); }
};
return dStr_printable( wrapped, maxlen );
}
/* /*
*- dList --------------------------------------------------------------------- *- dList ---------------------------------------------------------------------
*/ */

View File

@ -124,6 +124,13 @@ void dStr_sprintfa (Dstr *ds, const char *format, ...);
int dStr_cmp(Dstr *ds1, Dstr *ds2); int dStr_cmp(Dstr *ds1, Dstr *ds2);
char *dStr_memmem(Dstr *haystack, Dstr *needle); char *dStr_memmem(Dstr *haystack, Dstr *needle);
const char *dStr_printable(Dstr *in, int maxlen); const char *dStr_printable(Dstr *in, int maxlen);
#ifdef __cplusplus
extern "C++"
{
#include <string>
}
std::string dStr_printable_std(const std::string &in, int maxlen);
#endif
/* /*
*-- dList -------------------------------------------------------------------- *-- dList --------------------------------------------------------------------

View File

@ -45,12 +45,12 @@ datauri_filter_dpi_LDADD = \
downloads_dpi_CXXFLAGS = @LIBFLTK_CXXFLAGS@ downloads_dpi_CXXFLAGS = @LIBFLTK_CXXFLAGS@
bookmarks_dpi_SOURCES = bookmarks.c dpiutil.c dpiutil.h bookmarks_dpi_SOURCES = bookmarks.c dpiutil.cc dpiutil.h
downloads_dpi_SOURCES = downloads.cc dpiutil.c dpiutil.h downloads_dpi_SOURCES = downloads.cc dpiutil.cc dpiutil.h
ftp_filter_dpi_SOURCES = ftp.c dpiutil.c dpiutil.h ftp_filter_dpi_SOURCES = ftp.c dpiutil.cc dpiutil.h
hello_filter_dpi_SOURCES = hello.c dpiutil.c dpiutil.h hello_filter_dpi_SOURCES = hello.c dpiutil.cc dpiutil.h
vsource_filter_dpi_SOURCES = vsource.c dpiutil.c dpiutil.h vsource_filter_dpi_SOURCES = vsource.c dpiutil.cc dpiutil.h
file_dpi_SOURCES = file.c dpiutil.c dpiutil.h file_dpi_SOURCES = file.c dpiutil.cc dpiutil.h
cookies_dpi_SOURCES = cookies.c dpiutil.c dpiutil.h cookies_dpi_SOURCES = cookies.c dpiutil.cc dpiutil.h
datauri_filter_dpi_SOURCES = datauri.c dpiutil.c dpiutil.h datauri_filter_dpi_SOURCES = datauri.c dpiutil.cc dpiutil.h

View File

@ -97,7 +97,7 @@ char *Escape_html_str(const char *str)
Dstr *dstr = dStr_sized_new(64); Dstr *dstr = dStr_sized_new(64);
for (i = 0; str[i]; ++i) { for (i = 0; str[i]; ++i) {
if ((p = strchr(unsafe_chars, str[i]))) if ((p = const_cast< char * >( strchr(unsafe_chars, str[i]) )))
dStr_append(dstr, unsafe_rep[p - unsafe_chars]); dStr_append(dstr, unsafe_rep[p - unsafe_chars]);
else else
dStr_append_c(dstr, str[i]); dStr_append_c(dstr, str[i]);

View File

@ -19,14 +19,14 @@ dpid_SOURCES = \
dpid.h \ dpid.h \
dpid_common.h \ dpid_common.h \
misc_new.h \ misc_new.h \
dpi.c \ dpi.cc \
dpi_socket_dir.c \ dpi_socket_dir.c \
dpid.c \ dpid.cc \
dpid_common.c \ dpid_common.c \
main.c \ main.c \
misc_new.c misc_new.c
dpidc_SOURCES = dpidc.c dpidc_SOURCES = dpidc.cc
sysconf_DATA = dpidrc sysconf_DATA = dpidrc
CLEANFILES = $(sysconf_DATA) CLEANFILES = $(sysconf_DATA)

View File

@ -7,6 +7,12 @@
#ifndef DPI_H #ifndef DPI_H
#define DPI_H #define DPI_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <unistd.h> /* for socklen_t */ #include <unistd.h> /* for socklen_t */
#include <sys/socket.h> /* for socklen_t and AF_LOCAL */ #include <sys/socket.h> /* for socklen_t and AF_LOCAL */
@ -42,4 +48,8 @@ char *a_Dpi_rd_dpi_socket_dir(char *dirname);
char *a_Dpi_srs(void); char *a_Dpi_srs(void);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -98,7 +98,7 @@ void free_services_list(Dlist *s_list)
struct service *s; struct service *s;
for (i=0; i < dList_length(s_list) ; i++) { for (i=0; i < dList_length(s_list) ; i++) {
s = dList_nth_data(s_list, i); s = reinterpret_cast< service * >( dList_nth_data(s_list, i) );
dFree(s->name); dFree(s->name);
} }
dList_free(s_list); dList_free(s_list);
@ -888,7 +888,7 @@ void send_sockport(int sock_fd, char *dpi_tag, struct dp *dpi_attr_list)
dReturn_if_fail((dpi_id = get_message(sock_fd, dpi_tag)) != NULL); dReturn_if_fail((dpi_id = get_message(sock_fd, dpi_tag)) != NULL);
serv = dList_find_custom(services_list,dpi_id,(dCompareFunc)service_match); serv = reinterpret_cast< service * >( dList_find_custom(services_list,dpi_id,(dCompareFunc)service_match) );
if (serv == NULL || (i = serv->dp_index) == -1) if (serv == NULL || (i = serv->dp_index) == -1)
for (i = 0; i < numdpis; i++) for (i = 0; i < numdpis; i++)

View File

@ -5,6 +5,11 @@
#ifndef DPID_H #ifndef DPID_H
#define DPID_H #define DPID_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h> /* for fd_set */ #include <sys/select.h> /* for fd_set */
#include <sys/un.h> #include <sys/un.h>
@ -112,4 +117,8 @@ int service_match(const struct service *A, const char *B);
void send_sockport(int sock_fd, char * dpi_tag, struct dp *dpi_attr_list); void send_sockport(int sock_fd, char * dpi_tag, struct dp *dpi_attr_list);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -9,6 +9,11 @@
* the next patch * the next patch
*/ */
#ifdef __cplusplus
extern "C"
{
#endif
#include <dirent.h> #include <dirent.h>
#include <stddef.h> /* size_t */ #include <stddef.h> /* size_t */
#include <sys/types.h> /* ssize_t */ #include <sys/types.h> /* ssize_t */
@ -51,4 +56,8 @@ void errmsg(char *caller, char *called, int errornum, char *file, int line);
ssize_t ckd_write(int fd, char *msg, char *file, int line); ssize_t ckd_write(int fd, char *msg, char *file, int line);
ssize_t ckd_close(int fd, char *file, int line); ssize_t ckd_close(int fd, char *file, int line);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -1,10 +1,18 @@
#ifndef MISC_NEW_H #ifndef MISC_NEW_H
#define MISC_NEW_H #define MISC_NEW_H
#ifdef __cplusplus
extern "C"
{
#endif
Dstr *a_Misc_rdtag(int socket); Dstr *a_Misc_rdtag(int socket);
char *a_Misc_readtag(int sock); char *a_Misc_readtag(int sock);
char *a_Misc_mkdtemp(char *template); char *a_Misc_mkdtemp(char *template_);
char *a_Misc_mkfname(char *template); char *a_Misc_mkfname(char *template_);
char *a_Misc_mksecret(int nchar); char *a_Misc_mksecret(int nchar);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -360,9 +360,9 @@ static Dstr *Http_make_content_type(const DilloUrl *url)
if (URL_FLAGS(url) & URL_MultipartEnc) { if (URL_FLAGS(url) & URL_MultipartEnc) {
_MSG("submitting multipart/form-data!\n"); _MSG("submitting multipart/form-data!\n");
dstr = dStr_new("multipart/form-data; boundary=\""); dstr = dStr_new("multipart/form-data; boundary=\"");
if (URL_DATA(url)->len > 2) { if (URL_DATA(url).size() > 2) {
/* boundary lines have "--" prepended. Skip that. */ /* boundary lines have "--" prepended. Skip that. */
const char *start = URL_DATA(url)->str + 2; const char *start = URL_DATA(url).c_str() + 2;
const char *eol = strchr(start, '\r'); const char *eol = strchr(start, '\r');
if (eol) if (eol)
dStr_append_l(dstr, start, eol - start); dStr_append_l(dstr, start, eol - start);
@ -438,8 +438,8 @@ static Dstr *Http_make_query_str(DilloWeb *web, bool_t use_proxy, bool_t use_tls
request_uri->str, URL_AUTHORITY(url), prefs.http_user_agent, request_uri->str, URL_AUTHORITY(url), prefs.http_user_agent,
accept_hdr_value, HTTP_Language_hdr, auth ? auth : "", accept_hdr_value, HTTP_Language_hdr, auth ? auth : "",
proxy_auth->str, referer, connection_hdr_val, content_type->str, proxy_auth->str, referer, connection_hdr_val, content_type->str,
(long)URL_DATA(url)->len, cookies); (long)URL_DATA(url).size(), cookies);
dStr_append_l(query, URL_DATA(url)->str, URL_DATA(url)->len); dStr_append_l(query, URL_DATA(url).c_str(), URL_DATA(url).size());
dStr_free(content_type, TRUE); dStr_free(content_type, TRUE);
} else { } else {
dStr_sprintfa( dStr_sprintfa(

View File

@ -258,7 +258,7 @@ int a_Capi_dpi_verify_request(BrowserWindow *bw, DilloUrl *url)
MSG("a_Capi_dpi_verify_request: Permission Denied!\n"); MSG("a_Capi_dpi_verify_request: Permission Denied!\n");
MSG(" URL_STR : %s\n", URL_STR(url)); MSG(" URL_STR : %s\n", URL_STR(url));
if (URL_FLAGS(url) & URL_Post) { if (URL_FLAGS(url) & URL_Post) {
MSG(" URL_DATA: %s\n", dStr_printable(URL_DATA(url), 1024)); MSG(" URL_DATA: %s\n", dStr_printable_std(URL_DATA(url), 1024).c_str());
} }
} }
return allow; return allow;

View File

@ -15,11 +15,13 @@
#include "msg.hh" #include "msg.hh"
#include "../dlib/dlib.h" #include "../dlib/dlib.h"
#include <string_view>
static const char *ALGORITHM2STR[] = { NULL, "MD5", "MD5-sess" }; static const char *ALGORITHM2STR[] = { NULL, "MD5", "MD5-sess" };
static const char *QOP2STR[] = { NULL, "auth", "auth-int" }; static const char *QOP2STR[] = { NULL, "auth", "auth-int" };
static const char hexchars[] = "0123456789abcdef"; static const char hexchars[] = "0123456789abcdef";
static Dstr *md5hexdigest(const Dstr *data) static Dstr *md5hexdigest(const std::string_view &data)
{ {
md5_state_t state; md5_state_t state;
md5_byte_t digest[16]; md5_byte_t digest[16];
@ -27,7 +29,7 @@ static Dstr *md5hexdigest(const Dstr *data)
int i; int i;
md5_init(&state); md5_init(&state);
md5_append(&state, (const md5_byte_t *)data->str, data->len); md5_append(&state, (const md5_byte_t *)data.data(), data.size());
md5_finish(&state, digest); md5_finish(&state, digest);
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
@ -68,7 +70,7 @@ int a_Digest_compute_digest(AuthRealm_t *realm, const char *username,
*/ */
Dstr *a0 = dStr_new(NULL); Dstr *a0 = dStr_new(NULL);
dStr_sprintf(a0, "%s:%s:%s", username, realm->name, passwd); dStr_sprintf(a0, "%s:%s:%s", username, realm->name, passwd);
Dstr *ha0 = md5hexdigest(a0); Dstr *ha0 = md5hexdigest(std::string_view{a0->str});
a1 = dStr_new(NULL); a1 = dStr_new(NULL);
dStr_sprintf(a1, "%s:%s:%s", ha0, realm->nonce, realm->cnonce); dStr_sprintf(a1, "%s:%s:%s", ha0, realm->nonce, realm->cnonce);
dStr_free(a0, 1); dStr_free(a0, 1);
@ -78,7 +80,7 @@ int a_Digest_compute_digest(AuthRealm_t *realm, const char *username,
return 0; return 0;
} }
digest = md5hexdigest(a1); digest = md5hexdigest(a1->str);
realm->authorization = digest->str; realm->authorization = digest->str;
dStr_shred(a1); dStr_shred(a1);
dStr_free(a1, 1); dStr_free(a1, 1);
@ -91,7 +93,7 @@ int a_Digest_compute_digest(AuthRealm_t *realm, const char *username,
*/ */
static Dstr *Digest_create_response(AuthRealm_t *realm, const char *method, static Dstr *Digest_create_response(AuthRealm_t *realm, const char *method,
const char *digest_uri, const char *digest_uri,
const Dstr *entity_body) const std::string &entity_body)
{ {
Dstr *ha2; Dstr *ha2;
Dstr *result; Dstr *result;
@ -100,14 +102,14 @@ static Dstr *Digest_create_response(AuthRealm_t *realm, const char *method,
/* A2 = Method ":" digest-uri-value */ /* A2 = Method ":" digest-uri-value */
Dstr *a2 = dStr_new(NULL); Dstr *a2 = dStr_new(NULL);
dStr_sprintf(a2, "%s:%s", method, digest_uri); dStr_sprintf(a2, "%s:%s", method, digest_uri);
ha2 = md5hexdigest(a2); ha2 = md5hexdigest(a2->str);
dStr_free(a2, 1); dStr_free(a2, 1);
} else if (realm->qop == AUTHINT) { } else if (realm->qop == AUTHINT) {
/* A2 = Method ":" digest-uri-value ":" H(entity-body) */ /* A2 = Method ":" digest-uri-value ":" H(entity-body) */
Dstr *hentity = md5hexdigest(entity_body); Dstr *hentity = md5hexdigest(entity_body);
Dstr *a2 = dStr_new(NULL); Dstr *a2 = dStr_new(NULL);
dStr_sprintf(a2, "%s:%s:%s", method, digest_uri, hentity->str); dStr_sprintf(a2, "%s:%s:%s", method, digest_uri, hentity->str);
ha2 = md5hexdigest(a2); ha2 = md5hexdigest(a2->str);
dStr_free(hentity, 1); dStr_free(hentity, 1);
dStr_free(a2, 1); dStr_free(a2, 1);
} else { } else {
@ -133,7 +135,7 @@ static Dstr *Digest_create_response(AuthRealm_t *realm, const char *method,
ha2->str); ha2->str);
} }
Dstr *request_digest = md5hexdigest(result); Dstr *request_digest = md5hexdigest(result->str);
dStr_free(result, 1); dStr_free(result, 1);
dStr_free(ha2, 1); dStr_free(ha2, 1);
return request_digest; return request_digest;

View File

@ -29,6 +29,14 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string>
#include <optional>
#include <Alepha/AutoRAII.h>
using namespace std::literals::string_literals;
using namespace std::literals::string_view_literals;
using namespace lout; using namespace lout;
using namespace dw; using namespace dw;
@ -84,19 +92,19 @@ class DilloHtmlForm {
bool enabled; bool enabled;
void eventHandler(Resource *resource, EventButton *event); void eventHandler(Resource *resource, EventButton *event);
DilloUrl *buildQueryUrl(DilloHtmlInput *active_input); DilloUrl *buildQueryUrl(DilloHtmlInput *active_input);
Dstr *buildQueryData(DilloHtmlInput *active_submit); std::optional< std::string > buildQueryData(DilloHtmlInput *active_submit);
char *makeMultipartBoundary(iconv_t char_encoder, char *makeMultipartBoundary(iconv_t char_encoder,
DilloHtmlInput *active_submit); DilloHtmlInput *active_submit);
Dstr *encodeText(iconv_t char_encoder, Dstr **input); Dstr *encodeText(iconv_t char_encoder, Dstr **input);
void strUrlencodeAppend(Dstr *dstr, const char *str); void strUrlencodeAppend(std::string &dstr, const char *str);
void inputUrlencodeAppend(Dstr *data, const char *name, const char *value); void inputUrlencodeAppend(std::string &data, const char *name, const char *value);
void inputMultipartAppend(Dstr *data, const char *boundary, void inputMultipartAppend(std::string &data, const char *boundary,
const char *name, const char *value); const char *name, const char *value);
void filesInputMultipartAppend(Dstr* data, const char *boundary, void filesInputMultipartAppend(std::string &data, const char *boundary,
const char *name, Dstr *file, const char *name, Dstr *file,
const char *filename); const char *filename);
void imageInputUrlencodeAppend(Dstr *data, Dstr *name, Dstr *x, Dstr *y); void imageInputUrlencodeAppend(std::string &data, Dstr *name, Dstr *x, Dstr *y);
void imageInputMultipartAppend(Dstr *data, const char *boundary, Dstr *name, void imageInputMultipartAppend(std::string &data, const char *boundary, Dstr *name,
Dstr *x, Dstr *y); Dstr *x, Dstr *y);
public: //BUG: for now everything is public public: //BUG: for now everything is public
@ -1076,7 +1084,7 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
if ((method == DILLO_HTML_METHOD_GET) || if ((method == DILLO_HTML_METHOD_GET) ||
(method == DILLO_HTML_METHOD_POST)) { (method == DILLO_HTML_METHOD_POST)) {
Dstr *DataStr; std::string DataStr;
DilloHtmlInput *active_submit = NULL; DilloHtmlInput *active_submit = NULL;
_MSG("DilloHtmlForm::buildQueryUrl: action=%s\n",URL_STR_(action)); _MSG("DilloHtmlForm::buildQueryUrl: action=%s\n",URL_STR_(action));
@ -1089,15 +1097,16 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
} }
} }
DataStr = buildQueryData(active_submit); auto queryStr= buildQueryData(active_submit);
if (DataStr) { if (queryStr.has_value()) {
auto DataStr= queryStr.value();
/* action was previously resolved against base URL */ /* action was previously resolved against base URL */
char *action_str = dStrdup(URL_STR(action)); char *action_str = dStrdup(URL_STR(action));
if (method == DILLO_HTML_METHOD_POST) { if (method == DILLO_HTML_METHOD_POST) {
new_url = a_Url_new(action_str, NULL); new_url = a_Url_new(action_str, NULL);
/* new_url keeps the dStr and sets DataStr to NULL */ /* new_url keeps the dStr and sets DataStr to NULL */
a_Url_set_data(new_url, &DataStr); a_Url_set_data(new_url, DataStr);
a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_Post); a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_Post);
if (content_type == DILLO_HTML_ENC_MULTIPART) if (content_type == DILLO_HTML_ENC_MULTIPART)
a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_MultipartEnc); a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_MultipartEnc);
@ -1109,12 +1118,11 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
if ((p = strchr(action_str, '?'))) if ((p = strchr(action_str, '?')))
*p = 0; *p = 0;
url_str = dStrconcat(action_str, "?", DataStr->str, NULL); url_str = dStrconcat(action_str, "?", DataStr.c_str(), NULL);
new_url = a_Url_new(url_str, NULL); new_url = a_Url_new(url_str, NULL);
a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_Get); a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_Get);
dFree(url_str); dFree(url_str);
} }
dStr_free(DataStr, 1);
dFree(action_str); dFree(action_str);
} }
} else { } else {
@ -1127,10 +1135,10 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
/** /**
* Construct the data for a query URL * Construct the data for a query URL
*/ */
Dstr *DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit) std::optional< std::string > DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit)
{ {
Dstr *DataStr = NULL; std::string DataStr;
char *boundary = NULL; char *boundary = nullptr;
iconv_t char_encoder = (iconv_t) -1; iconv_t char_encoder = (iconv_t) -1;
if (submit_charset && dStrAsciiCasecmp(submit_charset, "UTF-8")) { if (submit_charset && dStrAsciiCasecmp(submit_charset, "UTF-8")) {
@ -1162,7 +1170,6 @@ Dstr *DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit)
if ((content_type == DILLO_HTML_ENC_URLENCODED) || (boundary != NULL)) { if ((content_type == DILLO_HTML_ENC_URLENCODED) || (boundary != NULL)) {
Dlist *values = dList_new(5); Dlist *values = dList_new(5);
DataStr = dStr_sized_new(4096);
for (int i = 0; i < inputs.size(); i++) { for (int i = 0; i < inputs.size(); i++) {
auto input = inputs.at (i); auto input = inputs.at (i);
Dstr *name = dStr_new(input->name); Dstr *name = dStr_new(input->name);
@ -1230,12 +1237,12 @@ Dstr *DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit)
} }
dStr_free(name, 1); dStr_free(name, 1);
} }
if (DataStr->len > 0) { if( not DataStr.empty() ) {
if (content_type == DILLO_HTML_ENC_URLENCODED) { if (content_type == DILLO_HTML_ENC_URLENCODED) {
if (DataStr->str[DataStr->len - 1] == '&') if (DataStr.at( DataStr.size() - 1 ) == '&')
dStr_truncate(DataStr, DataStr->len - 1); DataStr.pop_back();
} else if (content_type == DILLO_HTML_ENC_MULTIPART) { } else if (content_type == DILLO_HTML_ENC_MULTIPART) {
dStr_append(DataStr, "--"); DataStr+= "--";
} }
} }
dList_free(values); dList_free(values);
@ -1393,24 +1400,27 @@ Dstr *DilloHtmlForm::encodeText(iconv_t char_encoder, Dstr **input)
/** /**
* Urlencode 'str' and append it to 'dstr' * Urlencode 'str' and append it to 'dstr'
*/ */
void DilloHtmlForm::strUrlencodeAppend(Dstr *dstr, const char *str) void DilloHtmlForm::strUrlencodeAppend(std::string &dstr, const char *str)
{ {
char *encoded = a_Url_encode_hex_str(str); auto encoded= Alepha::AutoRAII
dStr_append(dstr, encoded); {
dFree(encoded); [&]{ return a_Url_encode_hex_str(str); },
dFree
};
dstr+= encoded;
} }
/** /**
* Append a name-value pair to url data using url encoding. * Append a name-value pair to url data using url encoding.
*/ */
void DilloHtmlForm::inputUrlencodeAppend(Dstr *data, const char *name, void DilloHtmlForm::inputUrlencodeAppend(std::string &data, const char *name,
const char *value) const char *value)
{ {
if (name && name[0]) { if (name && name[0]) {
strUrlencodeAppend(data, name); strUrlencodeAppend(data, name);
dStr_append_c(data, '='); data+= '=';
strUrlencodeAppend(data, value); strUrlencodeAppend(data, value);
dStr_append_c(data, '&'); data+= '&';
} }
} }
@ -1418,7 +1428,7 @@ void DilloHtmlForm::inputUrlencodeAppend(Dstr *data, const char *name,
* Append files to URL data using multipart encoding. * Append files to URL data using multipart encoding.
* Currently only accepts one file. * Currently only accepts one file.
*/ */
void DilloHtmlForm::filesInputMultipartAppend(Dstr* data, void DilloHtmlForm::filesInputMultipartAppend(std::string &data,
const char *boundary, const char *boundary,
const char *name, const char *name,
Dstr *file, Dstr *file,
@ -1435,14 +1445,13 @@ void DilloHtmlForm::filesInputMultipartAppend(Dstr* data,
ctype = "text/html"; ctype = "text/html";
} }
if (data->len == 0) { if (data.empty()) {
dStr_append(data, "--"); data+= "--";
dStr_append(data, boundary); data+= boundary;
} }
dStr_sprintfa(data, data+= "\r\n"s
"\r\n" + "Content-Disposition: form-data; name=\"" + name +"\"; "
"Content-Disposition: form-data; name=\"%s\"; " + "filename=\"";
"filename=\"", name);
/* /*
* Replace the characters that are the most likely to damage things. * Replace the characters that are the most likely to damage things.
* For a while, there was some momentum to standardize on an encoding, * For a while, there was some momentum to standardize on an encoding,
@ -1452,63 +1461,60 @@ void DilloHtmlForm::filesInputMultipartAppend(Dstr* data,
for (int i = 0; char c = filename[i]; i++) { for (int i = 0; char c = filename[i]; i++) {
if (c == '\"' || c == '\r' || c == '\n') if (c == '\"' || c == '\r' || c == '\n')
c = '_'; c = '_';
dStr_append_c(data, c); data+= c;
} }
dStr_sprintfa(data, data+= "\"\r\n"s
"\"\r\n" + "Content-Type: " + ctype + "\r\n"
"Content-Type: %s\r\n" + "\r\n";
"\r\n", ctype);
dStr_append_l(data, file->str, file->len); data+= std::string_view{ file->str, file->str + file->len };
dStr_sprintfa(data, data+= "\r\n--"s + boundary;
"\r\n"
"--%s", boundary);
} }
} }
/** /**
* Append a name-value pair to url data using multipart encoding. * Append a name-value pair to url data using multipart encoding.
*/ */
void DilloHtmlForm::inputMultipartAppend(Dstr *data, void DilloHtmlForm::inputMultipartAppend(std::string &data,
const char *boundary, const char *boundary,
const char *name, const char *name,
const char *value) const char *value)
{ {
if (name && name[0]) { if (name && name[0]) {
if (data->len == 0) { if (data.empty()) {
dStr_append(data, "--"); data+= "--";
dStr_append(data, boundary); data+= boundary;
} }
dStr_sprintfa(data, data+=
"\r\n" "\r\n"s
"Content-Disposition: form-data; name=\"%s\"\r\n" + "Content-Disposition: form-data; name=\"" + name + "\"\r\n"
"\r\n" + "\r\n"
"%s\r\n" + value + "\r\n"
"--%s", + "--"
name, value, boundary); + boundary;
} }
} }
/** /**
* Append an image button click position to url data using url encoding. * Append an image button click position to url data using url encoding.
*/ */
void DilloHtmlForm::imageInputUrlencodeAppend(Dstr *data, Dstr *name, Dstr *x, void DilloHtmlForm::imageInputUrlencodeAppend(std::string &data, Dstr *name, Dstr *x,
Dstr *y) Dstr *y)
{ {
if (name->len) { if (name->len) {
strUrlencodeAppend(data, name->str); strUrlencodeAppend(data, name->str);
dStr_sprintfa(data, ".x=%s&", x->str); data+= ".x="s + x->str + "&";
strUrlencodeAppend(data, name->str); strUrlencodeAppend(data, name->str);
dStr_sprintfa(data, ".y=%s&", y->str); data+= ".y="s + y->str + "&";
} else } else
dStr_sprintfa(data, "x=%s&y=%s&", x->str, y->str); data+= "x="s + x->str + "&y=" + y->str + "&";
} }
/** /**
* Append an image button click position to url data using multipart encoding. * Append an image button click position to url data using multipart encoding.
*/ */
void DilloHtmlForm::imageInputMultipartAppend(Dstr *data, const char *boundary, void DilloHtmlForm::imageInputMultipartAppend(std::string &data, const char *boundary,
Dstr *name, Dstr *x, Dstr *y) Dstr *name, Dstr *x, Dstr *y)
{ {
int orig_len = name->len; int orig_len = name->len;

View File

@ -143,7 +143,7 @@ static DilloUrl *Url_object_new(const char *uri_str)
dReturn_val_if_fail (uri_str != NULL, NULL); dReturn_val_if_fail (uri_str != NULL, NULL);
url = dNew0(DilloUrl, 1); url = new DilloUrl{};
/* url->buffer is given a little extra room in case HSTS needs to transform /* url->buffer is given a little extra room in case HSTS needs to transform
* a URL string ending in ":80" to ":443". * a URL string ending in ":80" to ":443".
@ -214,7 +214,6 @@ DilloUrl::~DilloUrl()
if (this->hostname != this->authority) if (this->hostname != this->authority)
dFree((char *)this->hostname); dFree((char *)this->hostname);
dFree((char *)this->buffer); dFree((char *)this->buffer);
dStr_free(this->data, 1);
} }
/** /**
@ -424,7 +423,7 @@ DilloUrl* a_Url_new(const char *url_str, const char *base_url)
/* Fill url data */ /* Fill url data */
url = Url_object_new(SolvedUrl->str); url = Url_object_new(SolvedUrl->str);
url->data = dStr_new(""); url->data = "";
url->url_string = SolvedUrl; url->url_string = SolvedUrl;
url->illegal_chars = n_ic; url->illegal_chars = n_ic;
url->illegal_chars_spc = n_ic_spc; url->illegal_chars_spc = n_ic_spc;
@ -488,8 +487,7 @@ DilloUrl* a_Url_dup(const DilloUrl *ori)
url->ismap_url_len = ori->ismap_url_len; url->ismap_url_len = ori->ismap_url_len;
url->illegal_chars = ori->illegal_chars; url->illegal_chars = ori->illegal_chars;
url->illegal_chars_spc = ori->illegal_chars_spc; url->illegal_chars_spc = ori->illegal_chars_spc;
url->data = dStr_sized_new(URL_DATA(ori)->len); url->data = ori->data;
dStr_append_l(url->data, URL_DATA(ori)->str, URL_DATA(ori)->len);
return url; return url;
} }
@ -516,7 +514,7 @@ int a_Url_cmp(const DilloUrl *A, const DilloUrl *B)
B->path ? B->path + (*B->path == '/') : "")) == 0 && B->path ? B->path + (*B->path == '/') : "")) == 0 &&
//(st = URL_STR_FIELD_CMP(A->path, B->path)) == 0 && //(st = URL_STR_FIELD_CMP(A->path, B->path)) == 0 &&
(st = URL_STR_FIELD_CMP(A->query, B->query)) == 0 && (st = URL_STR_FIELD_CMP(A->query, B->query)) == 0 &&
(st = dStr_cmp(A->data, B->data)) == 0 && (st = strcmp(A->data.c_str(), B->data.c_str())) == 0 &&
(st = URL_STR_FIELD_I_CMP(A->scheme, B->scheme)) == 0)) (st = URL_STR_FIELD_I_CMP(A->scheme, B->scheme)) == 0))
return 0; return 0;
return st; return st;
@ -534,13 +532,9 @@ void a_Url_set_flags(DilloUrl *u, int flags)
/** /**
* Set DilloUrl data (like POST info, etc.) * Set DilloUrl data (like POST info, etc.)
*/ */
void a_Url_set_data(DilloUrl *u, Dstr **data) void a_Url_set_data(DilloUrl *u, std::string_view data)
{ {
if (u) { u->data= data;
dStr_free(u->data, 1);
u->data = *data;
*data = NULL;
}
} }
/** /**

View File

@ -15,6 +15,11 @@
#include "d_size.h" #include "d_size.h"
#include "../dlib/dlib.h" #include "../dlib/dlib.h"
extern "C++"
{
#include <string>
}
#define URL_HTTP_PORT 80 #define URL_HTTP_PORT 80
#define URL_HTTPS_PORT 443 #define URL_HTTPS_PORT 443
@ -98,7 +103,7 @@ struct DilloUrl {
const char *hostname; /**/ const char *hostname; /**/
int port; int port;
int flags; int flags;
Dstr *data; /**< POST */ std::string data; /**< POST */
int ismap_url_len; /**< Used by server side image maps */ int ismap_url_len; /**< Used by server side image maps */
int illegal_chars; /**< number of illegal chars */ int illegal_chars; /**< number of illegal chars */
int illegal_chars_spc; /**< number of illegal space chars */ int illegal_chars_spc; /**< number of illegal space chars */
@ -114,7 +119,7 @@ const char *a_Url_hostname(const DilloUrl *u);
DilloUrl* a_Url_dup(const DilloUrl *u); DilloUrl* a_Url_dup(const DilloUrl *u);
int a_Url_cmp(const DilloUrl *A, const DilloUrl *B); int a_Url_cmp(const DilloUrl *A, const DilloUrl *B);
void a_Url_set_flags(DilloUrl *u, int flags); void a_Url_set_flags(DilloUrl *u, int flags);
void a_Url_set_data(DilloUrl *u, Dstr **data); void a_Url_set_data(DilloUrl *u, std::string_view data);
void a_Url_set_ismap_coords(DilloUrl *u, char *coord_str); void a_Url_set_ismap_coords(DilloUrl *u, char *coord_str);
char *a_Url_decode_hex_str(const char *str); char *a_Url_decode_hex_str(const char *str);
char *a_Url_encode_hex_str(const char *str); char *a_Url_encode_hex_str(const char *str);