[mw-devel] [Git][arthur/mw][master] duktape: Implement urlget()

Andrew Price welshbyte at sucs.org
Tue Jul 11 23:13:49 BST 2017


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
bea3734d by Andrew Price at 2017-07-11T23:13:51+01:00
duktape: Implement urlget()

Since duktape provides a nice way to concatenate strings, use that
instead of the urldata() function.

- - - - -


4 changed files:

- src/client/js-duk.c
- src/client/js-moz.c
- src/client/js.h
- src/client/log.c


Changes:

=====================================
src/client/js-duk.c
=====================================
--- a/src/client/js-duk.c
+++ b/src/client/js-duk.c
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <jansson.h>
+#include <curl/curl.h>
 #include <duktape.h>
 
 #include "js.h"
@@ -233,6 +234,53 @@ static duk_ret_t js_ipc(duk_context *cx)
 	return 0;
 }
 
+struct urlget {
+	duk_context *cx;
+	duk_idx_t nchunks;
+};
+/* Consume data chunk acquired by curl */
+static size_t omnomnom(void *ptr, size_t size, size_t n, void *data)
+{
+	struct urlget *ug = data;
+	if (data == NULL)
+		return 0;
+	duk_push_lstring(ug->cx, ptr, size * n);
+	ug->nchunks++;
+	return size * n;
+}
+
+static duk_ret_t js_urlget(duk_context *cx)
+{
+	struct urlget ug = { .cx = cx, .nchunks = 0 };
+	char cerr[CURL_ERROR_SIZE];
+	const char *url;
+	CURL *cl;
+
+	if (duk_is_undefined(cx, -1)) {
+		fprintf(stderr, "mwjs error: urlget() expects an argument\n");
+		return DUK_RET_SYNTAX_ERROR;
+	}
+	if (!duk_is_string(cx, -1)) {
+		fprintf(stderr, "mwjs error: urlget() requires a string\n");
+		return DUK_RET_SYNTAX_ERROR;
+	}
+	url = duk_get_string(cx, -1);
+	cl = curl_easy_init();
+	curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, omnomnom);
+	curl_easy_setopt(cl, CURLOPT_WRITEDATA, &ug);
+	curl_easy_setopt(cl, CURLOPT_URL, url);
+	curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
+	curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VERSION);
+	if (curl_easy_perform(cl)) {
+		fprintf(stderr, "mwjs error: urlget(): '%s': '%s'\n", url, cerr);
+		return DUK_RET_ERROR;
+	}
+	curl_easy_cleanup(cl);
+	/* Join the chunks together */
+	duk_concat(cx, ug.nchunks);
+	return 1; /* Result is at the top of the stack */
+}
+
 static duk_ret_t js_beep(duk_context *cx)
 {
 	int i, beeps;
@@ -522,6 +570,7 @@ int setup_js(void)
 	define_func("wholist", js_wholist, 0);
 	define_func("rpc", js_rpc, 3);
 	define_func("ipc", js_ipc, 2);
+	define_func("urlget", js_urlget, 1);
 	define_func("beep", js_beep, 1);
 	define_func("termsize", js_termsize, 0);
 	define_func("bind", js_bind, 3);
@@ -529,8 +578,3 @@ int setup_js(void)
 	duk_pop(ctx);
 	return 0;
 }
-
-size_t urldata(void *ptr, size_t size, size_t nmemb, void *stream)
-{
-	return 0;
-}


=====================================
src/client/js-moz.c
=====================================
--- a/src/client/js-moz.c
+++ b/src/client/js-moz.c
@@ -707,7 +707,7 @@ static JSBool js_wholist(JSContext *cx, unsigned int argc, jsval *vp) {
 }
 
 /* recieve data from curl into a malloced memory chunk */
-size_t urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
+static size_t urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
 {
 	int addsize = size*nmemb;
 	struct block_t *b = stream;


=====================================
src/client/js.h
=====================================
--- a/src/client/js.h
+++ b/src/client/js.h
@@ -10,7 +10,6 @@ int is_js(char *name);
 void js_stop_execution(void);
 int stop_js(void);
 int setup_js(void);
-size_t urldata(void *ptr, size_t size, size_t nmemb, void *stream);
 
 enum bindtype {
 	K_BIND = 0,


=====================================
src/client/log.c
=====================================
--- a/src/client/log.c
+++ b/src/client/log.c
@@ -218,6 +218,16 @@ static size_t headlimit(void *ptr, size_t size, size_t nmemb, void *stream)
 	return done;
 }
 
+/* Receive data from curl into a malloced memory chunk */
+static size_t urldata(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+	int addsize = size*nmemb;
+	struct block_t *b = stream;
+
+	if (stream == NULL) return 0;
+	return block_append(b, ptr, addsize);
+}
+
 static void *file_url(void * data)
 {
 	struct urihit *uri = data;



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/bea3734d234f4b88e2df77668b7cec5edea3b33e

---
View it on GitLab: https://projects.sucs.org/arthur/mw/commit/bea3734d234f4b88e2df77668b7cec5edea3b33e
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20170711/422cf3e2/attachment-0001.html>


More information about the mw-devel mailing list