Commit bea3734d authored by Andrew Price's avatar Andrew Price
Browse files

duktape: Implement urlget()

Since duktape provides a nice way to concatenate strings, use that
instead of the urldata() function.
parent b41ed3a3
Loading
Loading
Loading
Loading
Loading
+49 −5
Original line number Diff line number Diff line
@@ -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;
}
+1 −1
Original line number Diff line number Diff line
@@ -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;
+0 −1
Original line number Diff line number Diff line
@@ -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,
+10 −0
Original line number Diff line number Diff line
@@ -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;