From: Rudolf Polzer <divverent@alientrap.org>
Date: Fri, 26 Nov 2010 19:35:00 +0000 (+0100)
Subject: add some test code for uri_post
X-Git-Tag: xonotic-v0.1.0preview~102
X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=eb02f2e761b1a2057b640be43a6f7abc8bcfac74;p=xonotic%2Fxonotic-data.pk3dir.git

add some test code for uri_post
---

diff --git a/qcsrc/menu/gamecommand.qc b/qcsrc/menu/gamecommand.qc
index 4b2b9cf0c..03e863e27 100644
--- a/qcsrc/menu/gamecommand.qc
+++ b/qcsrc/menu/gamecommand.qc
@@ -31,6 +31,30 @@ void _dumptree_close(entity pass, entity me)
 	}
 }
 
+float curl_uri_get_pos;
+float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
+string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
+void Curl_URI_Get_Callback(float id, float status, string data)
+{
+	float i;
+	float do_exec;
+	string do_cvar;
+	i = id - URI_GET_CURL;
+	do_exec = curl_uri_get_exec[i];
+	do_cvar = curl_uri_get_cvar[i];
+	if(status != 0)
+	{
+		print("error: status is ", ftos(status), "\n");
+		return;
+	}
+	if(do_exec)
+		localcmd(data);
+	if(do_cvar)
+		cvar_set(do_cvar, data);
+	if(!do_exec && !do_cvar)
+		print(data);
+}
+
 void GameCommand(string theCommand)
 {
 	float argc;
@@ -100,6 +124,67 @@ void GameCommand(string theCommand)
 		return;
 	}
 
+	if(argv(0) == "curl")
+	{
+		float do_exec;
+		string do_cvar;
+		float key;
+		float i, j;
+		string url;
+		float buf;
+
+		do_exec = FALSE;
+		do_cvar = string_null;
+		key = -1;
+
+		for(i = 1; i+1 < argc; ++i)
+		{
+			if(argv(i) == "--cvar" && i+2 < argc)
+			{
+				++i;
+				do_cvar = argv(i);
+				++i;
+				continue;
+			}
+			if(argv(i) == "--exec")
+			{
+				do_exec = TRUE;
+				++i;
+				continue;
+			}
+			if(argv(i) == "--key" && i+2 < argc)
+			{
+				++i;
+				key = stof(argv(i));
+				++i;
+				continue;
+			}
+			break;
+		}
+
+		// now, argv(i) is the URL
+		// following args may be POST parameters
+		url = argv(i);
+		++i;
+		buf = buf_create();
+		j = 0;
+		for(; i+1 < argc; i += 2)
+			bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
+		if(i < argc)
+			bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
+
+		if(j == 0) // no args: GET
+			crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
+		else // with args: POST
+			crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
+
+		curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
+
+		buf_del(buf);
+
+		return;
+	}
+
 #if 0
 	if(argv(0) == "tokentest")
 	{
diff --git a/qcsrc/menu/mbuiltin.qh b/qcsrc/menu/mbuiltin.qh
index ae1b02dfc..32305c7c4 100644
--- a/qcsrc/menu/mbuiltin.qh
+++ b/qcsrc/menu/mbuiltin.qh
@@ -373,4 +373,5 @@ string(string format, ...) sprintf = #627;
 string crypto_getkeyfp(string serveraddress) = #633; // retrieves the cached host key's CA fingerprint of a server given by IP address
 string crypto_getidfp(string serveraddress) = #634; // retrieves the cached host key fingerprint of a server given by IP address
 string crypto_getencryptlevel(string serveraddress) = #635; // 0 if never encrypting, 1 supported, 2 requested, 3 required, appended by list of allowed methods in order of preference ("AES128"), preceded by a space each
+float(string url, float id, string content_type, string delim, float buf, float keyid) crypto_uri_postbuf = #513;
 //description:
diff --git a/qcsrc/menu/xonotic/util.qc b/qcsrc/menu/xonotic/util.qc
index 6f5b037ac..800f34886 100644
--- a/qcsrc/menu/xonotic/util.qc
+++ b/qcsrc/menu/xonotic/util.qc
@@ -234,11 +234,6 @@ void setDependentWeird(entity e, float(entity) func)
 float _Nex_ExtResponseSystem_Queried;
 string _Nex_ExtResponseSystem_UpdateTo;
 
-float URI_GET_DISCARD = 0;
-
-float URI_GET_UPDATENOTIFICATION = 1;
-void UpdateNotification_URI_Get_Callback(float id, float status, string data);
-
 void URI_Get_Callback(float id, float status, string data)
 {
 	if (id == URI_GET_DISCARD)
@@ -247,9 +242,12 @@ void URI_Get_Callback(float id, float status, string data)
 	}
 	else if(id == URI_GET_UPDATENOTIFICATION)
 	{
-		// online ban list
 		UpdateNotification_URI_Get_Callback(id, status, data);
 	}
+	else if(id >= URI_GET_CURL && id <= URI_GET_CURL_END)
+	{
+		Curl_URI_Get_Callback(id, status, data);
+	}
 	else
 	{
 		print("Received HTTP request data for an invalid id ", ftos(id), ".\n");
diff --git a/qcsrc/menu/xonotic/util.qh b/qcsrc/menu/xonotic/util.qh
index 78eb7c1c4..ace912dc2 100644
--- a/qcsrc/menu/xonotic/util.qh
+++ b/qcsrc/menu/xonotic/util.qh
@@ -20,3 +20,14 @@ string getZonedTooltipForIdentifier(string s);
 string resolvemod(string m);
 
 string HUD_Panel_GetSettingName(float setting);
+
+float URI_GET_DISCARD = 0;
+
+float URI_GET_UPDATENOTIFICATION = 1;
+void UpdateNotification_URI_Get_Callback(float id, float status, string data);
+
+float URI_GET_CURL = 2;
+float URI_GET_CURL_END = 9;
+void Curl_URI_Get_Callback(float id, float status, string data);
+
+void URI_Get_Callback(float id, float status, string data);