1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-10-01 03:36:26 -04:00

[mujs] xhr.c

This commit is contained in:
Witold Filipczyk 2023-04-13 16:03:12 +02:00
parent 9ed1ace293
commit d15138944c
7 changed files with 1740 additions and 2 deletions

View File

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

View File

@ -39,6 +39,22 @@ struct classcomp {
}
};
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_clear_map(void *m)
{
@ -88,3 +104,163 @@ attr_erase_from_map(void *m, void *node)
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
mapa->erase(node);
}
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();
}
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 mjs_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->statusText, stracpy(statusText.c_str()));
}
void
set_xhr_header(char *normalized_value, const char *h_name, struct mjs_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;
}
}
const char *
get_output_headers(struct mjs_xhr *x)
{
std::string output = "";
auto responseHeaders = get_responseHeaders(x->responseHeaders);
for (auto h: *responseHeaders) {
output += h.first + ": " + h.second + "\r\n";
}
return output.c_str();
}
const char *
get_output_header(const char *header_name, struct mjs_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 output.c_str();
}
return NULL;
}

View File

@ -5,6 +5,8 @@
extern "C" {
#endif
struct mjs_xhr;
extern void *map_attrs;
void attr_save_in_map(void *m, void *node, void *value);
@ -16,6 +18,16 @@ void *attr_find_in_map(void *m, void *node);
void attr_erase_from_map(void *m, void *node);
void attr_clear_map(void *m);
void attr_clear_map_str(void *m);
void delete_map_str(void *m);
void *attr_create_new_requestHeaders_map(void);
void *attr_create_new_responseHeaders_map(void);
void process_xhr_headers(char *head, struct mjs_xhr *x);
void set_xhr_header(char *normalized_value, const char *h_name, struct mjs_xhr *x);
const char *get_output_headers(struct mjs_xhr *x);
const char *get_output_header(const char *header_name, struct mjs_xhr *x);
#ifdef __cplusplus
}

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -53,6 +53,8 @@
#include <sstream>
#include <vector>
#ifndef CONFIG_LIBDOM
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
@ -1630,3 +1632,4 @@ mjs_xhr_init(js_State *J)
js_defglobal(J, "XMLHttpRequest", JS_DONTENUM);
return 0;
}
#endif

View File

@ -2,7 +2,70 @@
#define EL__ECMASCRIPT_MUJS_XHR_H
#include <mujs.h>
#include "session/download.h"
#include "util/lists.h"
#ifdef __cplusplus
extern "C" {
#endif
enum {
GET = 1,
HEAD = 2,
POST = 3
};
struct xhr_listener {
LIST_HEAD(struct xhr_listener);
char *typ;
const char *fun;
};
struct mjs_xhr {
//std::map<std::string, std::string> requestHeaders;
//std::map<std::string, std::string, classcomp> responseHeaders;
void *requestHeaders;
void *responseHeaders;
struct download download;
struct ecmascript_interpreter *interpreter;
LIST_OF(struct xhr_listener) listeners;
const char *thisval;
const char *onabort;
const char *onerror;
const char *onload;
const char *onloadend;
const char *onloadstart;
const char *onprogress;
const char *onreadystatechange;
const char *ontimeout;
struct uri *uri;
char *response;
char *responseText;
char *responseType;
char *responseURL;
char *statusText;
char *upload;
bool async;
bool withCredentials;
bool isSend;
bool isUpload;
int method;
int status;
int timeout;
size_t responseLength;
unsigned short readyState;
};
int mjs_xhr_init(js_State *J);
char *normalize(char *value);
#ifdef __cplusplus
}
#endif
#endif