summaryrefslogtreecommitdiff
path: root/request.h
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-09 19:51:18 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-09 19:51:18 +0800
commitc6e5f421e2738b6005f6c3da6700b1343214f7e5 (patch)
treef4c45010fac52e4c30610ddd4640fb5b36a602a8 /request.h
parenta0e54cb948c9862936e16eedd2a2015be05408c8 (diff)
Add support for Emscripten
Diffstat (limited to 'request.h')
-rw-r--r--request.h46
1 files changed, 43 insertions, 3 deletions
diff --git a/request.h b/request.h
index a943687..73b26d8 100644
--- a/request.h
+++ b/request.h
@@ -7,18 +7,34 @@
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
+#ifndef __EMSCRIPTEN__
#include <curl/curl.h>
+#endif
+#include "icclient/typedefs.h"
+#ifdef __EMSCRIPTEN__
+extern emscripten_fetch_attr_t attr;
+#else
extern CURL *curl;
extern char *server_url;
+#endif
-inline void request(size_t (*writefunction)(void *, size_t, size_t, void *),
- void *writedata, struct curl_httppost *post, char *fmt, ...)
+inline void request(icclient_handler writefunction, void *writedata,
+#ifdef __EMSCRIPTEN__
+ int
+#else
+ struct curl_httppost *
+#endif
+ post, char *fmt, ...)
{
va_list ap;
char *p, *sval;
unsigned int ival;
- size_t length = strlen(server_url) + strlen(fmt);
+ size_t length =
+#ifndef __EMSCRIPTEN__
+ strlen(server_url) +
+#endif
+ strlen(fmt);
va_start(ap, fmt);
for (p = fmt; *p; p++) {
@@ -43,7 +59,11 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *),
va_end(ap);
char url[length + 1];
+#ifdef __EMSCRIPTEN__
+ memset(url, 0, length + 1);
+#else
strcpy(url, server_url);
+#endif
va_start(ap, fmt);
for (p = fmt; *p; p++) {
@@ -64,17 +84,36 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *),
}
va_end(ap);
+#ifdef __EMSCRIPTEN__
+ attr.onsuccess = writefunction;
+#else
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction);
+#endif
if (writedata)
+#ifdef __EMSCRIPTEN__
+ attr.userData = writedata;
+#else
curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata);
else
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
+#endif
if (post)
+#ifdef __EMSCRIPTEN__
+ strcpy(attr.requestMethod, "POST");
+#else
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
+#endif
else
+#ifdef __EMSCRIPTEN__
+ strcpy(attr.requestMethod, "GET");
+#else
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
+#endif
+#ifdef __EMSCRIPTEN__
+ emscripten_fetch(&attr, url);
+#else
#ifdef DEBUG
CURLcode res =
#endif
@@ -87,6 +126,7 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *),
#else
fprintf(stderr, "%s: %s\n", __func__, error);
#endif
+#endif
}
#endif
}