From 9c524c971f967ccd981ec02891b931587d6b1477 Mon Sep 17 00:00:00 2001
From: pixel <pixel>
Date: Sun, 17 Jun 2007 15:24:44 +0000
Subject: Adding POST system to the HttpClient, and variables and headers sent
 from Lua

---
 lib/HttpClient.cc | 24 ++++++++++++++++++++----
 lib/LuaTask.cc    | 34 +++++++++++++++++++++++++++++++---
 lib/tasklib.lua   | 13 ++++++++++---
 3 files changed, 61 insertions(+), 10 deletions(-)

(limited to 'lib')

diff --git a/lib/HttpClient.cc b/lib/HttpClient.cc
index 6182da4..8b18f07 100644
--- a/lib/HttpClient.cc
+++ b/lib/HttpClient.cc
@@ -17,19 +17,20 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/* $Id: HttpClient.cc,v 1.10 2007-05-30 11:57:09 pixel Exp $ */
+/* $Id: HttpClient.cc,v 1.11 2007-06-17 15:24:44 pixel Exp $ */
 
 #include <TaskMan.h>
 #include <HttpClient.h>
 #include <BRegex.h>
 #include <CopyJob.h>
 #include <ReadJob.h>
+#include <Variables.h>
 
 t_headers no_headers;
 
 const timeval timeout = { 8, 0 };
 
-HttpClient::HttpClient(const String & _url, Handle * _out, const String & _fake_host, t_headers _headers) : url(_url), out(_out), fake_host(_fake_host), headers(_headers), host(""), uri("") {
+HttpClient::HttpClient(const String & _url, Handle * _out, const String & _fake_host, t_headers _headers, const Variables & _vars) : url(_url), out(_out), fake_host(_fake_host), headers(_headers), vars(_vars), host(""), uri("") {
     DecodeURL();
     
     Client.SetNonBlock();
@@ -49,9 +50,11 @@ HttpClient::~HttpClient() {
 int HttpClient::Do() throw (GeneralException) {
     t_headers::iterator i;
     String t;
-    int l;
+    int l, j;
     Regex h_reply("^HTTP/1.1 ");
     Regex chunked_header("^Transfer-Encoding: chunked$");
+    bool do_post = vars.GetNb();
+    String rendered_variables;
     
     switch (current) {
     case 0:
@@ -67,15 +70,28 @@ int HttpClient::Do() throw (GeneralException) {
         }
         RemoveTimeout();
 
-        b << "GET " + uri + " HTTP/1.1\r\n"
+        b << (do_post ? "GET " : "POST ") + uri + " HTTP/1.1\r\n"
              "Host: " + host + "\r\n"
              "Connection: close\r\n";
         
         for (i = headers.begin(); i != headers.end(); i++) {
             b << *i + "\r\n";
         }
+	
+	if (do_post) {
+	    for (j = 0; j < vars.GetNb(); j++) {
+		if (j != 0) {
+		    rendered_variables += "&";
+		}
+		rendered_variables += vars[j];
+	    }
+	    b << "Content-Type: application/x-www-form-urlencoded\r\n";
+	    b << "Content-Length: " + String(rendered_variables.strlen()) + "\r\n\r\n";
+	}
         
         b << "\r\n";
+	
+	b << rendered_variables;
         
         c = new CopyJob(&b, &Client);
         WaitFor(timeout);
diff --git a/lib/LuaTask.cc b/lib/LuaTask.cc
index e38c136..622cb16 100644
--- a/lib/LuaTask.cc
+++ b/lib/LuaTask.cc
@@ -17,7 +17,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/* $Id: LuaTask.cc,v 1.13 2007-06-14 17:33:50 pixel Exp $ */
+/* $Id: LuaTask.cc,v 1.14 2007-06-17 15:24:44 pixel Exp $ */
 
 #include <LuaTask.h>
 #include <LuaHandle.h>
@@ -126,11 +126,39 @@ int LuaTask::Do() throw (GeneralException) {
             } else if (task == "HttpClient") {
                 String url = L->tostring(2);
                 t_headers headers;
+		Variables vars;
+		
+		if (L->gettop() >= 4) {
+		    if (!L->istable(4)) {
+			L->error("Incorrect parameters to HttpClient.");
+			return TASK_DONE;
+		    }
+		    // read variables
+		    L->push();
+		    while (L->next(4) != 0) {
+			String v = L->tostring(-2) + "=" + L->tostring(-1);
+			vars.Add(v);
+			L->pop();
+		    }
+		}
+		if (L->gettop() >= 3) {
+		    if (!L->istable(3)) {
+			L->error("Incorrect parameters to HttpClient.");
+			return TASK_DONE;
+		    }
+		    // read headers
+		    L->push();
+		    while (L->next(3) != 0) {
+			String h = L->tostring(-2) + ": " + L->tostring(-1);
+			headers.push_back(h);
+			L->pop();
+		    }
+		}
 
                 b = new Buffer(true);
 
-                headers.push_back("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0");
-                c = new HttpClient(url, b, "", headers);
+                //headers.push_back("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0");
+                c = new HttpClient(url, b, "", headers, vars);
                 WaitFor(c);
                 Suspend(TASK_ON_HOLD);
 #endif
diff --git a/lib/tasklib.lua b/lib/tasklib.lua
index f506e34..764750b 100644
--- a/lib/tasklib.lua
+++ b/lib/tasklib.lua
@@ -19,7 +19,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/* $Id: tasklib.lua,v 1.3 2007-05-30 11:57:10 pixel Exp $ */
+/* $Id: tasklib.lua,v 1.4 2007-06-17 15:24:44 pixel Exp $ */
 
 ]]--
 
@@ -27,8 +27,15 @@
 -- Create some simple bindings for the LuaTask system to hide the various ugly yields.
 --
 
-function HttpClient(url)
-    return coroutine.yield("HttpClient", url)
+function HttpClient(url, headers, vars)
+    if type(headers) == "nil" then
+	headers = {}
+    end
+    if type(vars) == "nil" then
+	vars = {}
+    end
+    headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0"
+    return coroutine.yield("HttpClient", url, headers, vars)
 end
 
 function Command(command, ...)
-- 
cgit v1.2.3