summaryrefslogtreecommitdiff
path: root/lib/LuaHttp.cc
blob: 92af75982f0163b818f304503113b5e00675bb73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 *  Baltisot
 *  Copyright (C) 1999-2007 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: LuaHttp.cc,v 1.12 2007-05-30 22:36:31 pixel Exp $ */

#include "Domain.h"
#include "LuaHttp.h"
#include "LuaHandle.h"
#include "LuaTask.h"

class LuaDomain;

class LuaLuaDomain : public LuaObject {
  public:
    static void pushstatics(Lua *) throw (GeneralException);
      LuaLuaDomain(LuaDomain *);
  protected:
    virtual void pushmembers(Lua *);
    LuaDomain * d;
};

LuaLuaDomain::LuaLuaDomain(LuaDomain * _d) : d(_d) { }

enum LuaDomain_methods_t {
    LUADOMAIN_ONTOP = 0,
};

enum LuaDomain_functions_t {
    LUADOMAIN_NEWDOMAIN = 0,
};

struct lua_functypes_t LuaDomain_methods[] = {
    { LUADOMAIN_ONTOP,	"OnTop",	0, 0, { } },
    { -1, 0, 0, 0, 0 },
};

struct lua_functypes_t LuaDomain_functions[] = {
    { LUADOMAIN_NEWDOMAIN,	"Domain",	2, 2, { LUA_STRING, LUA_FUNCTION } },
    { -1, 0, 0, 0, 0 },
};

class sLua_LuaDomain : public Base {
  public:
    DECLARE_METHOD(LuaDomain, LUADOMAIN_ONTOP);
    
    DECLARE_FUNCTION(LuaDomain, LUADOMAIN_NEWDOMAIN);

  private:
    static int LuaDomain_proceed(Lua * L, int n, LuaDomain * obj, int caller);
    static int LuaDomain_proceed_statics(Lua * L, int n, int caller);
};

void LuaLuaDomain::pushmembers(Lua * L) {
    pushme(L, d, "LuaDomain");
    
    PUSH_METHOD(LuaDomain, LUADOMAIN_ONTOP);
}

void LuaLuaDomain::pushstatics(Lua * L) throw (GeneralException) {
    CHECK_METHODS(LuaDomain);
    CHECK_FUNCTIONS(LuaDomain);
    
    PUSH_FUNCTION(LuaDomain, LUADOMAIN_NEWDOMAIN);
}

#define DOMAIN_REGISTRY "DOMAINS_KEYS"

#define export_enum(L, n) \
  L->push(#n); \
  L->push((lua_Number) HttpResponse::n); \
  L->settable(LUA_GLOBALSINDEX);

LuaHttpResponse::LuaHttpResponse(HttpResponse * _r) : r(_r) { }

enum HttpResponse_method_t {
    HTTPRESPONSE_INDEX = 0,
    HTTPRESPONSE_NEWINDEX,
};

enum HttpResponse_functions_t {
    HTTPRESPONSE_NEWHTTPRESPONSE = 0,
};

struct lua_functypes_t HttpResponse_methods[] = {
    { HTTPRESPONSE_INDEX,    "index",    1, 1, { LUA_STRING } },
    { HTTPRESPONSE_NEWINDEX, "newindex", 2, 2, { LUA_STRING, LUA_ANY } },
    { -1, 0, 0, 0, 0 }
};

struct lua_functypes_t HttpResponse_functions[] = {
    { HTTPRESPONSE_NEWHTTPRESPONSE, "HttpResponse",   0, 0, { } },
    { -1, 0, 0, 0, 0 }
};

class sLua_HttpResponse : public Base {
  public:
    DECLARE_METHOD(HttpResponse, HTTPRESPONSE_INDEX);
    DECLARE_METHOD(HttpResponse, HTTPRESPONSE_NEWINDEX);
    
    DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_NEWHTTPRESPONSE);
  private:
    static int HttpResponse_proceed(Lua * L, int n, HttpResponse * obj, int caller);
    static int HttpResponse_proceed_statics(Lua * L, int n, int caller);
};

void LuaHttpResponse::pushmembers(Lua * L) {
    pushme(L, r, "HttpResponse");
    
    PUSH_METAMETHOD(HttpResponse, HTTPRESPONSE_INDEX);
    PUSH_METAMETHOD(HttpResponse, HTTPRESPONSE_NEWINDEX);
}

void LuaHttpResponse::pushstatics(Lua * L) throw (GeneralException) {
    CHECK_METHODS(HttpResponse);
    CHECK_FUNCTIONS(HttpResponse);
    
    PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_NEWHTTPRESPONSE);
    
    export_enum(L, HTTP_200_OK);
    export_enum(L, HTTP_301_PERM_MOVED);
    export_enum(L, HTTP_302_FOUND);
    export_enum(L, HTTP_400_BAD_REQUEST);
    export_enum(L, HTTP_401_UNAUTHORIZED);
    export_enum(L, HTTP_403_FORBIDDEN);
    export_enum(L, HTTP_404_NOT_FOUND);
    export_enum(L, HTTP_500_INTERNAL_ERROR);
    export_enum(L, HTTP_503_SERVICE_UNAVAILABLE);
    
    L->push(DOMAIN_REGISTRY);
    L->newtable();
    L->settable(LUA_REGISTRYINDEX);
    
    LuaLuaDomain::pushstatics(L);
}

int sLua_HttpResponse::HttpResponse_proceed(Lua * L, int n, HttpResponse * res, int caller) {
    String i;
    int r = 0;
    
    switch (caller) {
    case HTTPRESPONSE_INDEX:
        i = L->tostring(2);
        if (i == "mime_type") {
            L->push(res->mime_type);
        } else if (i == "location") {
            L->push(res->location);
        } else if (i == "server_name") {
            L->push(res->server_name);
        } else if (i == "return_code") {
            L->push((lua_Number) res->return_code);
        } else if (i == "last_modified") {
            L->push((lua_Number) res->last_modified);
        } else if (i == "contents") {
            {
                LuaBuffer b(&res->contents);
                b.push(L);
            }
        } else if (i == "cache") {
            L->push(res->cache);
        } else {
            L->error("Unknow field in HttpResponse object: " + i);
        }
        r = 1;
        break;
    case HTTPRESPONSE_NEWINDEX:
        i = L->tostring(2);
        if (i == "mime_type") {
            res->mime_type = L->tostring(3);
        } else if (i == "location") {
            res->location = L->tostring(3);
        } else if (i == "server_name") {
            res->server_name = L->tostring(3);
        } else if (i == "return_code") {
            res->return_code = HttpResponse::return_code_t(L->tonumber(3));
        } else if (i == "last_modified") {
            res->last_modified = L->tonumber(3);
        } else if (i == "contents") {
            L->error("Can't alter field buffer in HttpResponse.");
        } else if (i == "cache") {
            res->cache = L->toboolean(3);
        } else {
            L->error("Unknow field in HttpResponse object: " + i);
        }
        break;
    }
    
    return r;
}

class LuaDomain : public Domain {
  public:
      LuaDomain(Lua * _L, String r) : Domain(r), L(_L->Father()) {
          id = max_id++;
          
          L->push(DOMAIN_REGISTRY);
          L->gettable(LUA_REGISTRYINDEX);
          L->push((lua_Number) id);
          L->copy(2);
          L->settable();
	  L->pop();
      }
      virtual ~LuaDomain() {
	  L->push(DOMAIN_REGISTRY);
	  L->gettable(LUA_REGISTRYINDEX);
          L->push((lua_Number) id);
          L->push();
          L->settable();
          L->pop();
      }
    virtual void Do(const HttpRequest & req, HttpResponse * res) throw (GeneralException) {
        int i;
	
	Lua * oldL = L;
	Lua * L = oldL->thread(false);
	oldL->pop();
        
        L->push(DOMAIN_REGISTRY);
        L->gettable(LUA_REGISTRYINDEX);
        L->push((lua_Number) id);
        L->gettable();
        L->remove();
        
        L->newtable();

        L->push("vars");
        L->newtable();
        for (i = 0; i < req.vars->GetNb(); i++) {
            char * v = (*(req.vars))[i].strdup(), * p;
            
            p = strchr(v, '=');
            *p = 0;
            p++;
            
            L->push(v);
            L->push(p);
            L->settable();
            
            free(v);
        }
        L->settable();
        
        L->push("headers");
        L->newtable();
        for (i = 0; i < req.headers->GetNb(); i++) {
            char * v = (*(req.headers))[i].strdup(), * p;
            
            p = strchr(v, '=');
            *p = 0;
            p++;
            
            L->push(v);
            L->push(p);
            L->settable();
            
            free(v);
        }
        L->settable();
        
        L->push("uri");
        L->push(req.uri);
        L->settable();
        
        LuaHttpResponse r(res);
        r.push(L);
        
        res->builder = new LuaTask(L, 2);
    }
  private:
    Lua * L;
    static int max_id;
    int id;
};

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;
    
    switch (caller) {
    case LUADOMAIN_ONTOP:
	obj->OnTop();
	break;
    }
    
    return r;
}

int sLua_LuaDomain::LuaDomain_proceed_statics(Lua * L, int n, int caller) {
    int r = 0;
    
    switch (caller) {
    case LUADOMAIN_NEWDOMAIN:
	{
	    LuaLuaDomain lld(new LuaDomain(L, L->tostring(1)));
	    lld.pushdestruct(L);
	    r = 1;
	}
	break;
    }
    
    return r;
}