[mw-devel] MW3 r976 - trunk/src

arthur at sucs.org arthur at sucs.org
Fri Oct 26 10:17:30 BST 2007


Author: arthur
Date: 2007-10-26 10:17:30 +0100 (Fri, 26 Oct 2007)
New Revision: 976

Modified:
   trunk/src/js.c
Log:
add crude url fetching function to mwjs


Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2007-10-23 15:55:18 UTC (rev 975)
+++ trunk/src/js.c	2007-10-26 09:17:30 UTC (rev 976)
@@ -12,6 +12,7 @@
 #include <sys/types.h>
 #include <pwd.h>
 #include <readline/readline.h>
+#include <curl/curl.h>
 
 #include "bb.h"
 #include "proto.h"
@@ -721,6 +722,87 @@
 	return JS_TRUE;
 }
 
+/* recieve data from curl into a malloced memory chunk */
+ size_t  urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
+{
+	int addsize = size*nmemb;
+	char **data = stream;
+	static int datasize = 0;
+	int offset;
+
+	if (stream == NULL) return 0;
+	/* a new one, zero the count, otherwise extend and append */
+	if (*data == NULL) {
+		offset = 0;
+		datasize = addsize + 1;
+	} else  {
+		offset = datasize - 1;
+		datasize += addsize;
+	}
+	/* resize, copy on the end, and null terminate */
+	*data = realloc(*data, datasize);
+	memcpy( *data + offset, ptr, addsize );
+	memset( *data + datasize - 1, 0, 1);
+	return addsize;
+}
+
+/* Function to make a url GET request and return the resulting page
+ */
+static JSBool js_urlget(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+{
+	uintN i;
+	int conversion_result;
+	char msg[MAXTEXTLENGTH*4];
+	JSString *jsstr;
+
+	if (argc < 1) {
+		printf("Bad Call to js_urlget\n");
+		return JS_FALSE;
+	}
+	
+	for (i = 0; i<argc; i++) {
+		if (JSVAL_IS_STRING(argv[i])) {
+			conversion_result=jsval_to_utf8string(cx, argv[0], msg, MAXTEXTLENGTH*4);
+			if( conversion_result >= 0) {
+				CURL *cl;
+				char cerr[CURL_ERROR_SIZE];
+				char *answer = NULL;
+
+				if( conversion_result & WOUTPUTTOOSHORT ) {
+					printf("JavaScript geturl() command: URL too long. It was truncated\n");
+				}
+				cl = curl_easy_init();
+				curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, urldata);
+				curl_easy_setopt(cl, CURLOPT_WRITEDATA, &answer);
+				curl_easy_setopt(cl, CURLOPT_URL, msg);
+				curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
+#ifdef RELEASE
+                if (atoi(VER_TWK) > 0)
+                        curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK);
+                else
+                        curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN);
+#else
+                curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK " (Dev)");
+#endif
+                if (curl_easy_perform(cl))
+                        fprintf(stderr, "JavaScript urlget failed %s: %s\n", msg, cerr);
+                curl_easy_cleanup(cl);
+
+		jsstr = JS_NewStringCopyZ(cx, answer);
+		*rval = STRING_TO_JSVAL(jsstr);
+
+				free(answer);
+				return JS_TRUE;
+
+
+			} else {
+				return JS_FALSE;
+			}
+		} 
+	}
+	return JS_FALSE;
+}
+
 // Create a javascript array of strings from a struct db_data
 JSObject *dbdata_to_jsarray(JSContext *cx, struct db_data *data, int ncols) {
 	JSObject *jsdata;
@@ -1252,6 +1334,7 @@
 	JS_DefineFunction(jscx, jsroot, "wholist", js_wholist, 0, 1);
 	JS_DefineFunction(jscx, jsroot, "rpc", js_rpc, 3, 0);
 	JS_DefineFunction(jscx, jsroot, "ipc", js_ipc, 2, 0);
+	JS_DefineFunction(jscx, jsroot, "urlget", js_urlget, 1, 1);
 
 	JS_DefineProperty(jscx, jsroot, "whoami", STRING_TO_JSVAL(JS_NewStringCopyZ(jscx,user->name)), NULL, NULL, JSPROP_READONLY|JSPROP_PERMANENT);
 





More information about the mw-devel mailing list