Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 69 additions & 28 deletions lib/src/http_wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class HTTPWasmClient : public HTTPClient {
memcpy(z[i], h.first.c_str(), h.first.size());
i++;
z[i] = (char *)malloc(h.second.size() * 4 + 1);
memset(z[i], 0, h.first.size() * 4 + 1);
memset(z[i], 0, h.second.size() * 4 + 1);
memcpy(z[i], h.second.c_str(), h.second.size());
i++;
}
Expand All @@ -51,17 +51,27 @@ class HTTPWasmClient : public HTTPClient {
return 0;
}
const xhr = new XMLHttpRequest();
if (false && url.startsWith("http://")) {
url = "https://" + url.substr(7);
}
xhr.open(UTF8ToString($3), url, false);
xhr.responseType = "arraybuffer";

var i = 0;
var len = $1;
while (i < len) {
var ptr1 = HEAP32[($2 + (i * 4)) >> 2];
var ptr2 = HEAP32[($2 + ((i + 1) * 4)) >> 2];
while (i < len*2) {
var ptr1 = HEAP32[($2)/4 + i ];
var ptr2 = HEAP32[($2)/4 + i + 1];

try {
xhr.setRequestHeader(encodeURI(UTF8ToString(ptr1)), encodeURI(UTF8ToString(ptr2)));
var z = encodeURI(UTF8ToString(ptr1));
if (z === "Host") z = "X-Host-Override";
if (z === "Authorization") {
xhr.setRequestHeader(z, UTF8ToString(ptr2));
} else {

xhr.setRequestHeader(z, encodeURI(UTF8ToString(ptr2)));
}
} catch (error) {
console.warn("Error while performing XMLHttpRequest.setRequestHeader()", error);
}
Expand Down Expand Up @@ -139,7 +149,7 @@ class HTTPWasmClient : public HTTPClient {

return res;
}
unique_ptr<HTTPResponse> Post(PostRequestInfo &info) override {
unique_ptr<HTTPResponse> Head(HeadRequestInfo &info) override {
unique_ptr<HTTPResponse> res;

string path = host_port + info.url;
Expand All @@ -159,7 +169,7 @@ class HTTPWasmClient : public HTTPClient {
memcpy(z[i], h.first.c_str(), h.first.size());
i++;
z[i] = (char *)malloc(h.second.size() * 4 + 1);
memset(z[i], 0, h.first.size() * 4 + 1);
memset(z[i], 0, h.second.size() * 4 + 1);
memcpy(z[i], h.second.c_str(), h.second.size());
i++;
}
Expand All @@ -173,29 +183,39 @@ class HTTPWasmClient : public HTTPClient {
return 0;
}
const xhr = new XMLHttpRequest();
if (false && url.startsWith("http://")) {
url = "https://" + url.substr(7);
}
xhr.open(UTF8ToString($3), url, false);
xhr.responseType = "arraybuffer";

var i = 0;
var len = $1;
while (i < len) {
var ptr1 = HEAP32[($2 + (i * 4)) >> 2];
var ptr2 = HEAP32[($2 + ((i + 1) * 4)) >> 2];
while (i < len*2) {
var ptr1 = HEAP32[($2)/4 + i ];
var ptr2 = HEAP32[($2)/4 + i + 1];

try {
xhr.setRequestHeader(encodeURI(UTF8ToString(ptr1)), encodeURI(UTF8ToString(ptr2)));
var z = encodeURI(UTF8ToString(ptr1));
if (z === "Host") z = "X-Host-Override";
if (z === "Authorization") {
xhr.setRequestHeader(z, UTF8ToString(ptr2));
} else {

xhr.setRequestHeader(z, encodeURI(UTF8ToString(ptr2)));
}
} catch (error) {
console.warn("Error while performing XMLHttpRequest.setRequestHeader()", error);
}
i += 2;
}

try {
xhr.send(UTF8ToString($4));
xhr.send(null);
} catch {
return 0;
}
if (xhr.status != 200) return 0;
if (xhr.status >= 400) return 0;
var uInt8Array = xhr.response;

var len = uInt8Array.byteLength;
Expand All @@ -222,7 +242,8 @@ class HTTPWasmClient : public HTTPClient {
Module.HEAPU8.set(LEN123, fileOnWasmHeap);
return fileOnWasmHeap;
},
path.c_str(), n, z, "POST", info.buffer_in);
path.c_str(), n, z, "HEAD");
// clang-format on

i = 0;
for (auto h : info.headers) {
Expand All @@ -236,8 +257,8 @@ class HTTPWasmClient : public HTTPClient {
if (!exe) {
res = make_uniq<HTTPResponse>(HTTPStatusCode::NotFound_404);
res->reason =
"Unknown error, something went quack in Wasm land! Please consult the console and or the docs at "
"https://duckdb.org/community_extensions/extensions/webmacro";
"Unknown error, something went wrong in Wasm land! Please consult the console and consider reporting a "
"bug";
} else {
res = duckdb::make_uniq<HTTPResponse>(HTTPStatusCode::OK_200);
uint64_t LEN = 0;
Expand All @@ -250,15 +271,18 @@ class HTTPWasmClient : public HTTPClient {
LEN *= 256;
LEN += ((uint8_t *)exe)[0];
res->body = string(exe + 4, LEN);
/*
if (info.content_handler) {
info.content_handler((const unsigned char *)exe + 4, LEN);
}
*/

free(exe);
}
// clang-format on

return res;
}
unique_ptr<HTTPResponse> Put(PutRequestInfo &info) override { return nullptr; }

unique_ptr<HTTPResponse> Head(HeadRequestInfo &info) override {
unique_ptr<HTTPResponse> Post(PostRequestInfo &info) override {
unique_ptr<HTTPResponse> res;

string path = host_port + info.url;
Expand All @@ -278,7 +302,7 @@ class HTTPWasmClient : public HTTPClient {
memcpy(z[i], h.first.c_str(), h.first.size());
i++;
z[i] = (char *)malloc(h.second.size() * 4 + 1);
memset(z[i], 0, h.first.size() * 4 + 1);
memset(z[i], 0, h.second.size() * 4 + 1);
memcpy(z[i], h.second.c_str(), h.second.size());
i++;
}
Expand All @@ -292,29 +316,39 @@ class HTTPWasmClient : public HTTPClient {
return 0;
}
const xhr = new XMLHttpRequest();
if (false && url.startsWith("http://")) {
url = "https://" + url.substr(7);
}
xhr.open(UTF8ToString($3), url, false);
xhr.responseType = "arraybuffer";

var i = 0;
var len = $1;
while (i < len) {
var ptr1 = HEAP32[($2 + (i * 4)) >> 2];
var ptr2 = HEAP32[($2 + ((i + 1) * 4)) >> 2];
while (i < len*2) {
var ptr1 = HEAP32[($2)/4 + i ];
var ptr2 = HEAP32[($2)/4 + i + 1];

try {
xhr.setRequestHeader(encodeURI(UTF8ToString(ptr1)), encodeURI(UTF8ToString(ptr2)));
var z = encodeURI(UTF8ToString(ptr1));
if (z === "Host") z = "X-Host-Override";
if (z === "Authorization") {
xhr.setRequestHeader(z, UTF8ToString(ptr2));
} else {

xhr.setRequestHeader(z, encodeURI(UTF8ToString(ptr2)));
}
} catch (error) {
console.warn("Error while performing XMLHttpRequest.setRequestHeader()", error);
}
i += 2;
}

try {
xhr.send(null);
xhr.send(UTF8ToString($4));
} catch {
return 0;
}
if (xhr.status != 200) return 0;
if (xhr.status >= 400) return 0;
var uInt8Array = xhr.response;

var len = uInt8Array.byteLength;
Expand All @@ -341,7 +375,7 @@ class HTTPWasmClient : public HTTPClient {
Module.HEAPU8.set(LEN123, fileOnWasmHeap);
return fileOnWasmHeap;
},
path.c_str(), n, z, "HEAD");
path.c_str(), n, z, "POST", info.buffer_in);
// clang-format on

i = 0;
Expand Down Expand Up @@ -370,11 +404,18 @@ class HTTPWasmClient : public HTTPClient {
LEN *= 256;
LEN += ((uint8_t *)exe)[0];
res->body = string(exe + 4, LEN);
/*
if (info.content_handler) {
info.content_handler((const unsigned char *)exe + 4, LEN);
}
*/

free(exe);
}

return res;
}
unique_ptr<HTTPResponse> Put(PutRequestInfo &info) override { return nullptr; }
unique_ptr<HTTPResponse> Delete(DeleteRequestInfo &info) override { return nullptr; }

private:
Expand Down