Commits

mcc  committed 05eaaf7

Non-functional PUT

  • Participants
  • Parent commits ba9ff00

Comments (0)

Files changed (6)

File curl/PolycodeDownloader.cpp

 
 #include "PolycodeDownloader.h"
+#include "tinyxml.h"
 
 size_t DownloaderCallback(char *ptr, size_t size, size_t nmemb, void *userdata) {
 
 	data = (char*)malloc(0);
 	size = 0;
 	returned = false;
+	put_progress = 0;
 }
 
 bool PolycodeDownloader::writeToFile(String fileName) {
 	return true;
 }
 
+void PolycodeDownloader::putString(String _put) {
+	put = _put;
+}
+
+void pop_objectEntry(lua_State *L, ObjectEntry *o);
+
+// Arguments: root name, lua table to save
+// Returns: nothing
+int PolycodeDownloader::putTable(lua_State *L) {
+	luaL_checktype(L, 2, LUA_TSTRING);
+	luaL_checktype(L, -1, LUA_TTABLE);
+	Object o;
+	o.root.name = lua_tostring(L, 2);
+	
+	pop_objectEntry(L, &o.root);
+	
+	// Nothing for this in Object, so I'll build it myself.
+	TiXmlDocument doc;  	
+	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
+	doc.LinkEndChild( decl ); 
+	
+	TiXmlElement * rootElement = o.createElementFromObjectEntry(&o.root);
+	doc.LinkEndChild(rootElement);
+	
+	TiXmlPrinter printer;
+	printer.SetStreamPrinting();
+	doc.Accept(&printer);
+	put = printer.CStr();
+	
+	return 0;
+}
+
+static size_t read_callback_trampoline(void *ptr, size_t size, size_t nmemb, void *userp)
+{
+	PolycodeDownloader *d = (PolycodeDownloader *)userp;
+	d->read_callback(ptr,size,nmemb);
+}
+
+size_t PolycodeDownloader::read_callback(void *ptr, size_t size, size_t nmemb) {
+	if(size*nmemb < 1)
+		return 0;
+	
+	if(put.size()>put_progress) {
+		*(char *)ptr = put[put_progress++]; /* copy one single byte */ 
+		return 1;                        /* we return 1 byte at a time! */ 
+	}
+	
+	return 0;                          /* no more data left to deliver */ 
+}
+
 void PolycodeDownloader::runThread() {
 	curl = curl_easy_init();
 		
 	curl_easy_setopt(curl, CURLOPT_URL, url.c_str());	
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DownloaderCallback);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
+	if (put.size() > 0) {
+		put_progress = 0;
+		
+		curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
+				
+		/* we want to use our own read function */ 
+		curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback_trampoline);
+		
+		/* pointer to pass to our read function */ 
+		curl_easy_setopt(curl, CURLOPT_READDATA, this);
+	}
 			
 	CURLcode curl_res = curl_easy_perform(curl);
 	

File curl/PolycodeDownloader.h

 #include <Polycode.h>
 #include <curl/curl.h>
 
+extern "C" {	
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+}
+
 using namespace Polycode;
 
 class PolycodeDownloader : public Threaded {
 		
 		bool returned;
 		
+		void putString(String _put);
+		int putTable(lua_State *L);
+		size_t read_callback(void *ptr, size_t size, size_t nmemb);
+	
 		void *userData;
 		
 	protected:
-		String url;		
+		String url;
+		String put;
+		int put_progress;
 		CURL *curl;
 };

File media/overlay/play/onLoad.lua

 	xhr(um.level, function(t)
 		dos:clear()
 		startWithSpec(Spec(t))
-	end)
+	end, {"FUCK!!!"})
 end
 
 -- Set up input handler

File media/overlay/pregame/onLoad.lua

 	print(v)
 end
 
-function webString(s)
+function webString(s,put)
 	local d = a(PolycodeDownloader(s))
+	if put then d:putTable("twhy", put) end
 	d:runThread()
 	return d:getDataAsString()
 end
 
-function webTable(s)
-	local w = webString(s)
+function webTable(s,put)
+	local w = webString(s,put)
 	if sillyDebug then print(w) end
 	return bridge:loadTableFromXml(w)
 end
 	act:push({this=f})
 end
 
-function xhr(url, success)
+function xhr(url, success, put)
 	please(function()
-		local t = webTable(url)
+		local t = webTable(url,put)
 		if t then
 			success(t)
 		else dosFail()

File server/server.py

 			self.specs.append(s)
 
 	def any(self):
-		print(self.issue_next)
 		s = self.specs[self.issue_next]
 		self.issue_next = self.issue_next + 1
 		if self.issue_next >= len(self.specs):
 		try:
 			post_obj = None
 			if (post):
+				if options.debug > 0:
+					print("POST: %s" % post)
 				dom = xml.dom.minidom.parseString(post)
 				document = dom.documentElement
 				post_obj = document.firstChild

File source/bridge.cpp

 	return 1;
 }
 
-static void pop_objectEntry(lua_State *L, ObjectEntry *o) {
+void pop_objectEntry(lua_State *L, ObjectEntry *o) {
 	int ty = lua_type (L, -1);
 	switch (ty) {
 		case LUA_TNIL: case LUA_TLIGHTUSERDATA: case LUA_TFUNCTION: case LUA_TUSERDATA: case LUA_TTHREAD: default: