Begin converting URL struct into modern C++ class
This commit is contained in:
128
src/form.cc
128
src/form.cc
@ -29,6 +29,14 @@
|
||||
|
||||
#include <memory>
|
||||
#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 dw;
|
||||
@ -84,19 +92,19 @@ class DilloHtmlForm {
|
||||
bool enabled;
|
||||
void eventHandler(Resource *resource, EventButton *event);
|
||||
DilloUrl *buildQueryUrl(DilloHtmlInput *active_input);
|
||||
Dstr *buildQueryData(DilloHtmlInput *active_submit);
|
||||
std::optional< std::string > buildQueryData(DilloHtmlInput *active_submit);
|
||||
char *makeMultipartBoundary(iconv_t char_encoder,
|
||||
DilloHtmlInput *active_submit);
|
||||
Dstr *encodeText(iconv_t char_encoder, Dstr **input);
|
||||
void strUrlencodeAppend(Dstr *dstr, const char *str);
|
||||
void inputUrlencodeAppend(Dstr *data, const char *name, const char *value);
|
||||
void inputMultipartAppend(Dstr *data, const char *boundary,
|
||||
void strUrlencodeAppend(std::string &dstr, const char *str);
|
||||
void inputUrlencodeAppend(std::string &data, const char *name, const char *value);
|
||||
void inputMultipartAppend(std::string &data, const char *boundary,
|
||||
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 *filename);
|
||||
void imageInputUrlencodeAppend(Dstr *data, Dstr *name, Dstr *x, Dstr *y);
|
||||
void imageInputMultipartAppend(Dstr *data, const char *boundary, Dstr *name,
|
||||
void imageInputUrlencodeAppend(std::string &data, Dstr *name, Dstr *x, Dstr *y);
|
||||
void imageInputMultipartAppend(std::string &data, const char *boundary, Dstr *name,
|
||||
Dstr *x, Dstr *y);
|
||||
|
||||
public: //BUG: for now everything is public
|
||||
@ -1076,7 +1084,7 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
|
||||
|
||||
if ((method == DILLO_HTML_METHOD_GET) ||
|
||||
(method == DILLO_HTML_METHOD_POST)) {
|
||||
Dstr *DataStr;
|
||||
std::string DataStr;
|
||||
DilloHtmlInput *active_submit = NULL;
|
||||
|
||||
_MSG("DilloHtmlForm::buildQueryUrl: action=%s\n",URL_STR_(action));
|
||||
@ -1089,15 +1097,16 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
|
||||
}
|
||||
}
|
||||
|
||||
DataStr = buildQueryData(active_submit);
|
||||
if (DataStr) {
|
||||
auto queryStr= buildQueryData(active_submit);
|
||||
if (queryStr.has_value()) {
|
||||
auto DataStr= queryStr.value();
|
||||
/* action was previously resolved against base URL */
|
||||
char *action_str = dStrdup(URL_STR(action));
|
||||
|
||||
if (method == DILLO_HTML_METHOD_POST) {
|
||||
new_url = a_Url_new(action_str, 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);
|
||||
if (content_type == DILLO_HTML_ENC_MULTIPART)
|
||||
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, '?')))
|
||||
*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);
|
||||
a_Url_set_flags(new_url, URL_FLAGS(new_url) | URL_Get);
|
||||
dFree(url_str);
|
||||
}
|
||||
dStr_free(DataStr, 1);
|
||||
dFree(action_str);
|
||||
}
|
||||
} else {
|
||||
@ -1127,10 +1135,10 @@ DilloUrl *DilloHtmlForm::buildQueryUrl(DilloHtmlInput *active_input)
|
||||
/**
|
||||
* 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;
|
||||
char *boundary = NULL;
|
||||
std::string DataStr;
|
||||
char *boundary = nullptr;
|
||||
iconv_t char_encoder = (iconv_t) -1;
|
||||
|
||||
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)) {
|
||||
Dlist *values = dList_new(5);
|
||||
|
||||
DataStr = dStr_sized_new(4096);
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
auto input = inputs.at (i);
|
||||
Dstr *name = dStr_new(input->name);
|
||||
@ -1230,12 +1237,12 @@ Dstr *DilloHtmlForm::buildQueryData(DilloHtmlInput *active_submit)
|
||||
}
|
||||
dStr_free(name, 1);
|
||||
}
|
||||
if (DataStr->len > 0) {
|
||||
if( not DataStr.empty() ) {
|
||||
if (content_type == DILLO_HTML_ENC_URLENCODED) {
|
||||
if (DataStr->str[DataStr->len - 1] == '&')
|
||||
dStr_truncate(DataStr, DataStr->len - 1);
|
||||
if (DataStr.at( DataStr.size() - 1 ) == '&')
|
||||
DataStr.pop_back();
|
||||
} else if (content_type == DILLO_HTML_ENC_MULTIPART) {
|
||||
dStr_append(DataStr, "--");
|
||||
DataStr+= "--";
|
||||
}
|
||||
}
|
||||
dList_free(values);
|
||||
@ -1393,24 +1400,27 @@ Dstr *DilloHtmlForm::encodeText(iconv_t char_encoder, Dstr **input)
|
||||
/**
|
||||
* 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);
|
||||
dStr_append(dstr, encoded);
|
||||
dFree(encoded);
|
||||
auto encoded= Alepha::AutoRAII
|
||||
{
|
||||
[&]{ return a_Url_encode_hex_str(str); },
|
||||
dFree
|
||||
};
|
||||
dstr+= encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if (name && name[0]) {
|
||||
strUrlencodeAppend(data, name);
|
||||
dStr_append_c(data, '=');
|
||||
data+= '=';
|
||||
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.
|
||||
* Currently only accepts one file.
|
||||
*/
|
||||
void DilloHtmlForm::filesInputMultipartAppend(Dstr* data,
|
||||
void DilloHtmlForm::filesInputMultipartAppend(std::string &data,
|
||||
const char *boundary,
|
||||
const char *name,
|
||||
Dstr *file,
|
||||
@ -1435,14 +1445,13 @@ void DilloHtmlForm::filesInputMultipartAppend(Dstr* data,
|
||||
ctype = "text/html";
|
||||
}
|
||||
|
||||
if (data->len == 0) {
|
||||
dStr_append(data, "--");
|
||||
dStr_append(data, boundary);
|
||||
if (data.empty()) {
|
||||
data+= "--";
|
||||
data+= boundary;
|
||||
}
|
||||
dStr_sprintfa(data,
|
||||
"\r\n"
|
||||
"Content-Disposition: form-data; name=\"%s\"; "
|
||||
"filename=\"", name);
|
||||
data+= "\r\n"s
|
||||
+ "Content-Disposition: form-data; name=\"" + name +"\"; "
|
||||
+ "filename=\"";
|
||||
/*
|
||||
* Replace the characters that are the most likely to damage things.
|
||||
* 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++) {
|
||||
if (c == '\"' || c == '\r' || c == '\n')
|
||||
c = '_';
|
||||
dStr_append_c(data, c);
|
||||
data+= c;
|
||||
}
|
||||
dStr_sprintfa(data,
|
||||
"\"\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"\r\n", ctype);
|
||||
data+= "\"\r\n"s
|
||||
+ "Content-Type: " + ctype + "\r\n"
|
||||
+ "\r\n";
|
||||
|
||||
dStr_append_l(data, file->str, file->len);
|
||||
data+= std::string_view{ file->str, file->str + file->len };
|
||||
|
||||
dStr_sprintfa(data,
|
||||
"\r\n"
|
||||
"--%s", boundary);
|
||||
data+= "\r\n--"s + boundary;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 *name,
|
||||
const char *value)
|
||||
{
|
||||
if (name && name[0]) {
|
||||
if (data->len == 0) {
|
||||
dStr_append(data, "--");
|
||||
dStr_append(data, boundary);
|
||||
if (data.empty()) {
|
||||
data+= "--";
|
||||
data+= boundary;
|
||||
}
|
||||
dStr_sprintfa(data,
|
||||
"\r\n"
|
||||
"Content-Disposition: form-data; name=\"%s\"\r\n"
|
||||
"\r\n"
|
||||
"%s\r\n"
|
||||
"--%s",
|
||||
name, value, boundary);
|
||||
data+=
|
||||
"\r\n"s
|
||||
+ "Content-Disposition: form-data; name=\"" + name + "\"\r\n"
|
||||
+ "\r\n"
|
||||
+ value + "\r\n"
|
||||
+ "--"
|
||||
+ boundary;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if (name->len) {
|
||||
strUrlencodeAppend(data, name->str);
|
||||
dStr_sprintfa(data, ".x=%s&", x->str);
|
||||
data+= ".x="s + x->str + "&";
|
||||
strUrlencodeAppend(data, name->str);
|
||||
dStr_sprintfa(data, ".y=%s&", y->str);
|
||||
data+= ".y="s + y->str + "&";
|
||||
} 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.
|
||||
*/
|
||||
void DilloHtmlForm::imageInputMultipartAppend(Dstr *data, const char *boundary,
|
||||
void DilloHtmlForm::imageInputMultipartAppend(std::string &data, const char *boundary,
|
||||
Dstr *name, Dstr *x, Dstr *y)
|
||||
{
|
||||
int orig_len = name->len;
|
||||
|
||||
Reference in New Issue
Block a user