summaryrefslogtreecommitdiff
path: root/lib/LuaHttp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/LuaHttp.cc')
-rw-r--r--lib/LuaHttp.cc85
1 files changed, 67 insertions, 18 deletions
diff --git a/lib/LuaHttp.cc b/lib/LuaHttp.cc
index 5d2cbb2..9d40d7e 100644
--- a/lib/LuaHttp.cc
+++ b/lib/LuaHttp.cc
@@ -17,12 +17,14 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* $Id: LuaHttp.cc,v 1.13 2007-06-04 14:24:18 pixel Exp $ */
+/* $Id: LuaHttp.cc,v 1.14 2007-06-11 11:27:21 pixel Exp $ */
+#include "md5.h"
#include "Domain.h"
#include "LuaHttp.h"
#include "LuaHandle.h"
#include "LuaTask.h"
+#include "Base64.h"
class LuaDomain;
@@ -95,6 +97,9 @@ enum HttpResponse_method_t {
enum HttpResponse_functions_t {
HTTPRESPONSE_NEWHTTPRESPONSE = 0,
+ HTTPRESPONSE_BASE64_ENCODE,
+ HTTPRESPONSE_BASE64_DECODE,
+ HTTPRESPONSE_MD5,
};
struct lua_functypes_t HttpResponse_methods[] = {
@@ -105,6 +110,9 @@ struct lua_functypes_t HttpResponse_methods[] = {
struct lua_functypes_t HttpResponse_functions[] = {
{ HTTPRESPONSE_NEWHTTPRESPONSE, "HttpResponse", 0, 0, { } },
+ { HTTPRESPONSE_BASE64_ENCODE, "Base64Encode", 1, 1, { LUA_STRING } },
+ { HTTPRESPONSE_BASE64_DECODE, "Base64Decode", 1, 1, { LUA_STRING } },
+ { HTTPRESPONSE_MD5, "MD5", 1, 1, { LUA_STRING } },
{ -1, 0, 0, 0, 0 }
};
@@ -114,6 +122,9 @@ class sLua_HttpResponse : public Base {
DECLARE_METHOD(HttpResponse, HTTPRESPONSE_NEWINDEX);
DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_NEWHTTPRESPONSE);
+ DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_ENCODE);
+ DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE);
+ DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_MD5);
private:
static int HttpResponse_proceed(Lua * L, int n, HttpResponse * obj, int caller);
static int HttpResponse_proceed_statics(Lua * L, int n, int caller);
@@ -131,6 +142,9 @@ void LuaHttpResponse::pushstatics(Lua * L) throw (GeneralException) {
CHECK_FUNCTIONS(HttpResponse);
PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_NEWHTTPRESPONSE);
+ PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_ENCODE);
+ PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE);
+ PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_MD5);
export_enum(L, HTTP_200_OK);
export_enum(L, HTTP_301_PERM_MOVED);
@@ -203,6 +217,58 @@ int sLua_HttpResponse::HttpResponse_proceed(Lua * L, int n, HttpResponse * res,
return r;
}
+int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) {
+ int r = 0, l, i;
+ String enc, dec;
+ char * enc_t;
+ const char * dec_t;
+ md5_context ctx;
+ unsigned char md5sum[16];
+ String md5sum_r;
+ static const char hconv[] = "0123456789ABCDEF";
+
+ switch (caller) {
+ case HTTPRESPONSE_NEWHTTPRESPONSE:
+ {
+ LuaHttpResponse r(new HttpResponse());
+ r.pushdestruct(L);
+ }
+ r = 1;
+ break;
+
+ case HTTPRESPONSE_BASE64_ENCODE:
+ dec = L->tostring();
+ dec_t = dec.to_charp();
+ L->push(Base64::encode(dec_t, dec.strlen()));
+ r = 1;
+ break;
+
+ case HTTPRESPONSE_BASE64_DECODE:
+ enc_t = (char *) Base64::decode(L->tostring(), &l);
+ enc_t[l] = 0;
+ L->push(enc_t);
+ r = 1;
+ break;
+
+ case HTTPRESPONSE_MD5:
+ dec = L->tostring();
+ dec_t = dec.to_charp();
+ md5_starts(&ctx);
+ md5_update(&ctx, (uint8 *) dec_t, dec.strlen());
+ md5_finish(&ctx, md5sum);
+ md5sum[16] = 0;
+ for (i = 0; i < 16; i++) {
+ md5sum_r = md5sum_r + hconv[md5sum[i] % 16];
+ md5sum_r = md5sum_r + hconv[md5sum[i] >> 4];
+ }
+ L->push(md5sum_r);
+ r = 1;
+ break;
+ }
+
+ return r;
+}
+
class LuaDomain : public Domain {
public:
LuaDomain(Lua * _L, String r) : Domain(r), L(_L->Father()) {
@@ -297,22 +363,6 @@ class LuaDomain : public Domain {
int LuaDomain::max_id = 1;
-int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) {
- int r = 0;
-
- switch (caller) {
- case HTTPRESPONSE_NEWHTTPRESPONSE:
- {
- LuaHttpResponse r(new HttpResponse());
- r.pushdestruct(L);
- }
- r = 1;
- break;
- }
-
- return r;
-}
-
int sLua_LuaDomain::LuaDomain_proceed(Lua * L, int n, LuaDomain * obj, int caller) {
int r = 0;
@@ -340,4 +390,3 @@ int sLua_LuaDomain::LuaDomain_proceed_statics(Lua * L, int n, int caller) {
return r;
}
-