1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-27 01:25:34 +00:00

[quickjs] C only

There are some bugs left.
This commit is contained in:
Witold Filipczyk 2023-12-31 16:08:35 +01:00
parent 4c7364c14e
commit 378842a349
7 changed files with 767 additions and 638 deletions

View File

@ -94,25 +94,6 @@ quickjs_done(struct module *xxx)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
attr_clear_map(map_attrs);
attr_clear_map(map_attributes);
attr_clear_map_rev(map_rev_attributes);
attr_clear_map(map_collections);
attr_clear_map_rev(map_rev_collections);
attr_clear_map(map_doctypes);
attr_clear_map(map_elements);
attr_clear_map_void(map_privates);
attr_clear_map(map_form);
attr_clear_map_rev(map_form_rev);
attr_clear_map(map_forms);
attr_clear_map_rev(map_rev_forms);
attr_clear_map(map_inputs);
attr_clear_map(map_nodelist);
attr_clear_map_rev(map_rev_nodelist);
attr_clear_map(map_form_elements);
attr_clear_map_rev(map_form_elements_rev);
attr_delete_map(map_attrs);
attr_delete_map(map_attributes);
attr_delete_map_rev(map_rev_attributes);

View File

@ -2,6 +2,6 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o document.o element.o form.o forms.o heartbeat.o history.o implementation.o input.o \
keyboard.o localstorage.o location.o mapa.obj message.o navigator.o nodelist.o screen.o style.o unibar.o window.o xhr.o
keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o screen.o style.o unibar.o window.o xhr.o
include $(top_srcdir)/Makefile.lib

View File

@ -0,0 +1,756 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdbool.h>
#include <stdlib.h>
#include "elinks.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/mapa.h"
#include "ecmascript/quickjs/xhr.h"
#include "util/hash.h"
#include "util/memory.h"
#include "util/string.h"
void
attr_save_in_map(void *m, void *node, JSValueConst value)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&node, sizeof(node));
if (key) {
char *v = memacpy((const char *)&value, sizeof(value));
if (v) {
add_hash_item(hash, key, sizeof(node), v);
}
}
}
}
void attr_save_in_map_void(void *m, void *node, void *value)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&node, sizeof(node));
if (key) {
add_hash_item(hash, key, sizeof(node), value);
}
}
}
void *
attr_create_new_attrs_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_attributes_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_attributes_map_rev(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_collections_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_doctypes_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_elements_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_privates_map_void(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_form_elements_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_form_elements_map_rev(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_form_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_form_map_rev(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_forms_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_forms_map_rev(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_input_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_collections_map_rev(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_nodelist_map(void)
{
return (void *)init_hash8();
}
#if 0
struct classcomp {
bool operator() (const std::string& lhs, const std::string& rhs) const
{
return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
}
};
#endif
void *
attr_create_new_requestHeaders_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_responseHeaders_map(void)
{
return (void *)init_hash8();
}
void *
attr_create_new_nodelist_map_rev(void)
{
return (void *)init_hash8();
}
void
delete_map_str(void *m)
{
struct hash *hash = (struct hash *)m;
if (hash) {
struct hash_item *item;
int i;
foreach_hash_item (item, *hash, i) {
mem_free_set(&item->key, NULL);
mem_free_set(&item->value, NULL);
}
free_hash(&hash);
}
}
void
attr_delete_map(void *m)
{
struct hash *hash = (struct hash *)m;
if (hash) {
struct hash_item *item;
int i;
foreach_hash_item (item, *hash, i) {
mem_free_set(&item->key, NULL);
mem_free_set(&item->value, NULL);
}
free_hash(&hash);
}
}
void
attr_delete_map_rev(void *m)
{
struct hash *hash = (struct hash *)m;
if (hash) {
struct hash_item *item;
int i;
foreach_hash_item (item, *hash, i) {
mem_free_set(&item->key, NULL);
mem_free_set(&item->value, NULL);
}
free_hash(&hash);
}
}
void
attr_delete_map_void(void *m)
{
struct hash *hash = (struct hash *)m;
if (hash) {
struct hash_item *item;
int i;
foreach_hash_item (item, *hash, i) {
mem_free_set(&item->key, NULL);
}
free_hash(&hash);
}
}
JSValue
attr_find_in_map(void *m, void *node)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&node, sizeof(node));
if (key) {
struct hash_item *item = get_hash_item(hash, key, sizeof(node));
mem_free(key);
if (item) {
char *v = (char *)item->value;
JSValue ret;
memcpy(&ret, v, sizeof(ret));
return ret;
}
}
}
return JS_NULL;
}
void *
attr_find_in_map_void(void *m, void *node)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&node, sizeof(node));
if (key) {
struct hash_item *item = get_hash_item(hash, key, sizeof(node));
mem_free(key);
if (item) {
return item->value;
}
}
}
return NULL;
}
void
attr_erase_from_map(void *m, void *node)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&node, sizeof(node));
if (key) {
struct hash_item *item = get_hash_item(hash, key, sizeof(node));
if (item) {
mem_free_set(&item->key, NULL);
mem_free_set(&item->value, NULL);
del_hash_item(hash, item);
}
mem_free(key);
}
}
}
void
attr_save_in_map_rev(void *m, JSValueConst value, void *node)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&value, sizeof(value));
if (key) {
add_hash_item(hash, key, sizeof(value), node);
}
}
}
void *
attr_find_in_map_rev(void *m, JSValueConst value)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&value, sizeof(value));
if (key) {
struct hash_item *item = get_hash_item(hash, key, sizeof(value));
mem_free(key);
if (item) {
return item->value;
}
}
}
return NULL;
}
void
attr_erase_from_map_rev(void *m, JSValueConst value)
{
struct hash *hash = (struct hash *)m;
if (hash) {
char *key = memacpy((const char *)&value, sizeof(value));
if (key) {
struct hash_item *item = get_hash_item(hash, key, sizeof(value));
if (item) {
mem_free_set(&item->key, NULL);
del_hash_item(hash, item);
}
mem_free(key);
}
}
}
static int
explode(char *s, const char c, char **header, char **value)
{
char *next;
char *colon = strchr(s, c);
if (!colon) {
return 0;
}
*header = memacpy(s, colon - s);
next = colon + 1;
colon = strchr(next, c);
if (colon) {
return 1;
}
*value = stracpy(next);
return 2;
}
static void *
get_requestHeaders(void *h)
{
return h;
}
static void *
get_responseHeaders(void *h)
{
return h;
}
void
process_xhr_headers(char *head, struct Xhr *x)
{
char *next, *next2, *line;
char *headers = stracpy(head);
char *space;
char *statusText;
char *newline;
char *stat;
int status;
if (!headers) {
return;
}
newline = strchr(headers, '\n');
if (newline) {
*newline = '\0';
}
space = strchr(headers, ' ');
if (!space) {
return;
}
stat = space + 1;
space = strchr(stat, ' ');
if (space) {
*space = '\0';
statusText = space + 1;
} else {
statusText = NULL;
}
status = atoi(stat);
struct hash *responseHeaders = get_responseHeaders(x->responseHeaders);
struct hash_item *item;
if (newline) {
next = newline + 1;
next2 = strchr(next, '\n');
} else {
next = next2 = NULL;
}
while (1) {
char *value = NULL;
char *header = NULL;
int size;
if (!next) {
break;
}
line = next;
if (!next2) {
next = NULL;
} else {
next = next2 + 1;
next2 = strchr(next, '\n');
}
next = next2 + 1;
size = explode(line, ':', &header, &value);
if (size == 0) {
continue;
}
if (size == 1 || !value) {
mem_free_set(&header, NULL);
continue;
}
if (!header) {
mem_free(value);
continue;
}
char *normalized_value = normalize(value);
item = get_hash_item(responseHeaders, header, strlen(header));
if (item) {
struct string hh;
if (init_string(&hh)) {
add_to_string(&hh, (char *)item->value);
add_to_string(&hh, ", ");
add_to_string(&hh, normalized_value);
mem_free_set(&item->value, hh.source);
}
} else {
add_hash_item(responseHeaders, header, strlen(header), stracpy(normalized_value));
}
mem_free(header);
mem_free(value);
}
x->status = status;
mem_free_set(&x->status_text, null_or_stracpy(statusText));
}
void
set_xhr_header(char *normalized_value, const char *h_name, struct Xhr *x)
{
struct hash *requestHeaders = get_requestHeaders(x->requestHeaders);
struct hash_item *item;
char *key;
if (!requestHeaders || !h_name) {
return;
}
item = get_hash_item(requestHeaders, h_name, strlen(h_name));
if (item) {
struct string hh;
if (init_string(&hh)) {
add_to_string(&hh, item->value);
add_to_string(&hh, ", ");
add_to_string(&hh, normalized_value);
mem_free_set(&item->value, hh.source);
}
return;
}
key = stracpy(h_name);
if (key) {
add_hash_item(requestHeaders, key, strlen(key), stracpy(normalized_value));
}
}
char *
get_output_headers(struct Xhr *x)
{
struct string output;
struct hash *hash = (struct hash *)get_responseHeaders(x->responseHeaders);;
struct hash_item *item;
int i;
if (!hash || !init_string(&output)) {
return NULL;
}
foreach_hash_item (item, *hash, i) {
add_to_string(&output, item->key);
add_to_string(&output, ": ");
add_to_string(&output, item->value);
add_to_string(&output, "\r\n");
}
return output.source;
}
char *
get_output_header(const char *header_name, struct Xhr *x)
{
struct hash *hash = get_responseHeaders(x->responseHeaders);
struct hash_item *item;
if (!hash || !header_name) {
return NULL;
}
item = get_hash_item(hash, header_name, strlen(header_name));
if (!item) {
return NULL;
}
return null_or_stracpy(item->value);
}
const char *good[] = { "background",
"background-color",
"color",
"display",
"font-style",
"font-weight",
"list-style",
"list-style-type",
"text-align",
"text-decoration",
"white-space",
NULL
};
bool
isGood(const char *param)
{
int i;
for (i = 0; good[i]; i++) {
if (!strcmp(param, good[i])) {
return true;
}
}
return false;
}
char *
trimString(char *str)
{
char *whitespace = " \t\n\r\f\v";
char *end = strchr(str, '\0') - 1;
str += strspn(str, whitespace);
while (end > str) {
char *s;
int bad = 0;
for (s = whitespace; *s; s++) {
if (*end == *s) {
bad = 1;
*end = '\0';
break;
}
}
if (!bad) {
break;
}
end--;
}
return str;
}
void *
set_elstyle(const char *text)
{
char *str, *param, *value, *next, *next2;
struct hash *css = NULL;
if (!text || !*text) {
return NULL;
}
str = stracpy(text);
next = str;
while (next) {
char *semicolon = strchr(str, ';');
char *colon;
char *params;
if (semicolon) {
*semicolon = '\0';
next2 = semicolon + 1;
} else {
next2 = NULL;
}
params = next;
colon = strchr(params, ':');
if (!colon) {
goto next_iter;
}
*colon = '\0';
param = trimString(params);
params = colon + 1;
colon = strchr(params, ':');
if (colon) {
*colon = '\0';
}
value = trimString(params);
if (isGood(param)) {
if (!css) {
css = init_hash8();
}
if (css) {
char *key = stracpy(param);
if (key) {
add_hash_item(css, key, strlen(key), stracpy(value));
}
}
}
next_iter:
next = next2;
}
return (void *)css;
}
char *
get_elstyle(void *m)
{
struct hash *css = (struct hash *)m;
char *delimiter = "";
struct hash_item *item;
struct string output;
int i;
if (!init_string(&output)) {
delete_map_str(css);
return NULL;
}
foreach_hash_item (item, *css, i) {
add_to_string(&output, delimiter);
add_to_string(&output, item->key);
add_char_to_string(&output, ':');
add_to_string(&output, item->value);
delimiter = ";";
}
delete_map_str(css);
return output.source;
}
char *
get_css_value(const char *text, const char *param)
{
void *m = set_elstyle(text);
char *res = NULL;
struct hash *css;
struct hash_item *item;
if (!m) {
return stracpy("");
}
css = (struct hash *)m;
item = get_hash_item(css, param, strlen(param));
if (item) {
res = stracpy(item->value);
} else {
res = stracpy("");
}
delete_map_str(css);
return res;
}
char *
set_css_value(const char *text, const char *param, const char *value)
{
void *m = set_elstyle(text);
if (m) {
if (isGood(param)) {
struct hash *hash = (struct hash *)m;
char *key = stracpy(param);
if (key) {
add_hash_item(hash, key, strlen(key), stracpy(value));
}
}
return get_elstyle(m);
}
if (isGood(param)) {
struct hash *css = init_hash8();
char *key;
if (!css) {
return stracpy("");
}
key = stracpy(param);
if (key) {
add_hash_item(css, key, strlen(key), stracpy(value));
}
return get_elstyle((void *)css);
}
return stracpy("");
}

View File

@ -1,607 +0,0 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "elinks.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/mapa.h"
#include "ecmascript/quickjs/xhr.h"
#include "util/memory.h"
#include "util/string.h"
#include <cstddef>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
void
attr_save_in_map(void *m, void *node, JSValueConst value)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
(*mapa)[node] = value;
}
void attr_save_in_map_void(void *m, void *node, void *value)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
(*mapa)[node] = value;
}
void *
attr_create_new_attrs_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_attributes_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_attributes_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void *
attr_create_new_collections_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_doctypes_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_elements_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_privates_map_void(void)
{
std::map<void *, void *> *mapa = new std::map<void *, void *>;
return (void *)mapa;
}
void *
attr_create_new_form_elements_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_form_elements_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void *
attr_create_new_form_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_form_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void *
attr_create_new_forms_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_forms_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void *
attr_create_new_input_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void *
attr_create_new_collections_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void *
attr_create_new_nodelist_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
struct classcomp {
bool operator() (const std::string& lhs, const std::string& rhs) const
{
return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
}
};
void *
attr_create_new_requestHeaders_map(void)
{
std::map<std::string, std::string> *mapa = new std::map<std::string, std::string>;
return (void *)mapa;
}
void *
attr_create_new_responseHeaders_map(void)
{
std::map<std::string, std::string, classcomp> *mapa = new std::map<std::string, std::string, classcomp>;
return (void *)mapa;
}
void *
attr_create_new_nodelist_map_rev(void)
{
std::map<JSValueConst, void *> *mapa = new std::map<JSValueConst, void *>;
return (void *)mapa;
}
void
attr_clear_map(void *m)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
mapa->clear();
}
void
attr_clear_map_rev(void *m)
{
std::map<JSValueConst, void *> *mapa = static_cast<std::map<JSValueConst, void *> *>(m);
mapa->clear();
}
void
attr_clear_map_void(void *m)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
mapa->clear();
}
void
attr_clear_map_str(void *m)
{
std::map<std::string, std::string> *mapa = static_cast<std::map<std::string, std::string> *>(m);
mapa->clear();
}
void
delete_map_str(void *m)
{
std::map<std::string, std::string> *mapa = static_cast<std::map<std::string, std::string> *>(m);
if (mapa) {
delete(mapa);
}
}
void
attr_delete_map(void *m)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
if (mapa) {
delete(mapa);
}
}
void
attr_delete_map_rev(void *m)
{
std::map<JSValueConst, void *> *mapa = static_cast<std::map<JSValueConst, void *> *>(m);
if (mapa) {
delete(mapa);
}
}
void
attr_delete_map_void(void *m)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
if (mapa) {
delete(mapa);
}
}
JSValue
attr_find_in_map(void *m, void *node)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
if (!mapa) {
return JS_NULL;
}
auto value = (*mapa).find(node);
if (value == (*mapa).end()) {
return JS_NULL;
}
return value->second;
}
void *
attr_find_in_map_void(void *m, void *node)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
if (!mapa) {
return NULL;
}
auto value = (*mapa).find(node);
if (value == (*mapa).end()) {
return NULL;
}
return value->second;
}
void
attr_erase_from_map(void *m, void *node)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
mapa->erase(node);
}
void
attr_save_in_map_rev(void *m, JSValueConst value, void *node)
{
std::map<JSValueConst, void *> *mapa = static_cast<std::map<JSValueConst, void *> *>(m);
(*mapa)[value] = node;
}
void *
attr_find_in_map_rev(void *m, JSValueConst value)
{
std::map<JSValueConst, void *> *mapa = static_cast<std::map<JSValueConst, void *> *>(m);
if (!mapa) {
return NULL;
}
auto v = (*mapa).find(value);
if (v == (*mapa).end()) {
return NULL;
}
return v->second;
}
void
attr_erase_from_map_rev(void *m, JSValueConst value)
{
std::map<JSValueConst, void *> *mapa = static_cast<std::map<JSValueConst, void *> *>(m);
mapa->erase(value);
}
static const std::vector<std::string>
explode(const std::string& s, const char& c)
{
std::string buff("");
std::vector<std::string> v;
bool found = false;
for (auto n:s) {
if (found) {
buff += n;
continue;
}
if (n != c) {
buff += n;
}
else if (n == c && buff != "") {
v.push_back(buff);
buff = "";
found = true;
}
}
if (buff != "") {
v.push_back(buff);
}
return v;
}
static std::map<std::string, std::string> *
get_requestHeaders(void *h)
{
return static_cast<std::map<std::string, std::string> *>(h);
}
static std::map<std::string, std::string, classcomp> *
get_responseHeaders(void *h)
{
return static_cast<std::map<std::string, std::string, classcomp> *>(h);
}
void
process_xhr_headers(char *head, struct Xhr *x)
{
std::istringstream headers(head);
std::string http;
int status;
std::string statusText;
std::string line;
std::getline(headers, line);
std::istringstream linestream(line);
linestream >> http >> status >> statusText;
auto responseHeaders = get_responseHeaders(x->responseHeaders);
while (1) {
std::getline(headers, line);
if (line.empty()) {
break;
}
std::vector<std::string> v = explode(line, ':');
if (v.size() == 2) {
char *value = stracpy(v[1].c_str());
if (!value) {
continue;
}
char *header = stracpy(v[0].c_str());
if (!header) {
mem_free(value);
continue;
}
char *normalized_value = normalize(value);
bool found = false;
for (auto h: *responseHeaders) {
const std::string hh = h.first;
if (!strcasecmp(hh.c_str(), header)) {
(*responseHeaders)[hh] = (*responseHeaders)[hh] + ", " + normalized_value;
found = true;
break;
}
}
if (!found) {
(*responseHeaders)[header] = normalized_value;
}
mem_free(header);
mem_free(value);
}
}
x->status = status;
mem_free_set(&x->status_text, stracpy(statusText.c_str()));
}
void
set_xhr_header(char *normalized_value, const char *h_name, struct Xhr *x)
{
bool found = false;
auto requestHeaders = get_requestHeaders(x->requestHeaders);
for (auto h: *requestHeaders) {
const std::string hh = h.first;
if (!strcasecmp(hh.c_str(), h_name)) {
(*requestHeaders)[hh] = (*requestHeaders)[hh] + ", " + normalized_value;
found = true;
break;
}
}
if (!found) {
(*requestHeaders)[h_name] = normalized_value;
}
}
char *
get_output_headers(struct Xhr *x)
{
std::string output = "";
auto responseHeaders = get_responseHeaders(x->responseHeaders);
for (auto h: *responseHeaders) {
output += h.first + ": " + h.second + "\r\n";
}
return memacpy(output.c_str(), output.length());
}
char *
get_output_header(const char *header_name, struct Xhr *x)
{
std::string output = "";
auto responseHeaders = get_responseHeaders(x->responseHeaders);
for (auto h: *responseHeaders) {
if (!strcasecmp(header_name, h.first.c_str())) {
output = h.second;
break;
}
}
if (!output.empty()) {
return memacpy(output.c_str(), output.length());
}
return NULL;
}
const std::map<std::string, bool> good = {
{ "background", true },
{ "background-color", true },
{ "color", true },
{ "display", true },
{ "font-style", true },
{ "font-weight", true },
{ "list-style", true },
{ "list-style-type", true },
{ "text-align", true },
{ "text-decoration", true },
{ "white-space", true }
};
static std::string
trimString(std::string str)
{
const std::string whiteSpaces = " \t\n\r\f\v";
// Remove leading whitespace
size_t first_non_space = str.find_first_not_of(whiteSpaces);
str.erase(0, first_non_space);
// Remove trailing whitespace
size_t last_non_space = str.find_last_not_of(whiteSpaces);
str.erase(last_non_space + 1);
return str;
}
void *
set_elstyle(const char *text)
{
if (!text || !*text) {
return NULL;
}
std::stringstream str(text);
std::string word;
std::string param, value;
std::map<std::string, std::string> *css = NULL;
while (!str.eof()) {
getline(str, word, ';');
std::stringstream params(word);
getline(params, param, ':');
getline(params, value, ':');
param = trimString(param);
value = trimString(value);
if (good.find(param) != good.end()) {
if (!css) {
css = new std::map<std::string, std::string>;
}
if (css) {
(*css)[param] = value;
}
}
}
return (void *)css;
}
char *
get_elstyle(void *m)
{
std::map<std::string, std::string> *css = static_cast<std::map<std::string, std::string> *>(m);
std::string delimiter("");
std::stringstream output("");
std::map<std::string, std::string>::iterator it;
for (it = css->begin(); it != css->end(); it++) {
output << delimiter << it->first << ":" << it->second;
delimiter = ";";
}
return stracpy(output.str().c_str());
}
char *
get_css_value(const char *text, const char *param)
{
void *m = set_elstyle(text);
char *res = NULL;
if (!m) {
return stracpy("");
}
std::map<std::string, std::string> *css = static_cast<std::map<std::string, std::string> *>(m);
if (css->find(param) != css->end()) {
res = stracpy((*css)[param].c_str());
} else {
res = stracpy("");
}
css->clear();
delete css;
return res;
}
char *
set_css_value(const char *text, const char *param, const char *value)
{
void *m = set_elstyle(text);
std::map<std::string, std::string> *css = NULL;
if (m) {
css = static_cast<std::map<std::string, std::string> *>(m);
if (good.find(param) != good.end()) {
(*css)[param] = value;
}
return get_elstyle(m);
}
if (good.find(param) != good.end()) {
css = new std::map<std::string, std::string>;
if (!css) {
return stracpy("");
}
(*css)[param] = value;
return get_elstyle((void *)css);
}
return stracpy("");
}

View File

@ -51,10 +51,10 @@ void *attr_create_new_nodelist_map_rev(void);
void *attr_create_new_requestHeaders_map(void);
void *attr_create_new_responseHeaders_map(void);
void attr_clear_map(void *m);
void attr_clear_map_rev(void *m);
void attr_clear_map_void(void *m);
void attr_clear_map_str(void *m);
//void attr_clear_map(void *m);
//void attr_clear_map_rev(void *m);
//void attr_clear_map_void(void *m);
//void attr_clear_map_str(void *m);
void delete_map_str(void *m);
void attr_delete_map(void *m);
void attr_delete_map_rev(void *m);
@ -66,7 +66,7 @@ void *attr_find_in_map_void(void *m, void *node);
void attr_erase_from_map(void *m, void *node);
void attr_save_in_map_rev(void *m, JSValueConst value, void *node);
void attr_clear_map_rev(void *m);
//void attr_clear_map_rev(void *m);
void *attr_find_in_map_rev(void *m, JSValueConst value);
void attr_erase_from_map_rev(void *m, JSValueConst value);

View File

@ -1,3 +1,3 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'document.c', 'element.c', 'form.c', 'forms.c', 'heartbeat.c', 'history.c',
'implementation.c', 'input.c', 'keyboard.c', 'localstorage.c', 'location.c',
'mapa.cpp', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'style.c', 'unibar.c', 'window.c', 'xhr.c')
'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'style.c', 'unibar.c', 'window.c', 'xhr.c')

View File

@ -322,9 +322,6 @@ xhr_finalizer(JSRuntime *rt, JSValue val)
mem_free_if(x->responseURL);
mem_free_if(x->status_text);
attr_clear_map_str(x->responseHeaders);
attr_clear_map_str(x->requestHeaders);
struct xhr_listener *l;
foreach(l, x->listeners) {
@ -1119,8 +1116,10 @@ xhr_open(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
x->uri = get_uri(url2, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD);
mem_free(url2);
attr_clear_map_str(x->requestHeaders);
attr_clear_map_str(x->responseHeaders);
delete_map_str(x->requestHeaders);
delete_map_str(x->responseHeaders);
x->requestHeaders = attr_create_new_requestHeaders_map();
x->responseHeaders = attr_create_new_responseHeaders_map();
x->ready_state = XHR_RSTATE_OPENED;
register_bottom_half(onreadystatechange_run, x);