-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjorgeLua.cpp
More file actions
401 lines (300 loc) · 10.3 KB
/
jorgeLua.cpp
File metadata and controls
401 lines (300 loc) · 10.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <jorgeLua.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
struct jlua_response_body_node{
char *data;
struct jlua_response_body_node *next;
};
struct jlua_global_data{
int connection;
jlua_response_body_node *response_body;
int response_body_length;
char *response_header;
};
// Creates the default folder and hello world lua script, if not existing
// Also sets up the database.
void jlua_setup_environment(){
{ // Lua scripts
int mkdirStatus = mkdir(JLUA_SCRIPT_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH );
if (mkdirStatus != -1){
printf("Assuming setup.\nCreating jorgeScripts folder and index.lua.\n");
FILE *helloWorldFile = fopen (JLUA_SCRIPT_PATH"/index.lua","w");
fputs (JLUA_HELLOW_TEXT, helloWorldFile); // Saves data defined on jorgeLua.h
fclose (helloWorldFile);
FILE *notFoundFile = fopen (JLUA_SCRIPT_PATH"/404.lua","w");
fputs (JLUA_404_TEXT, notFoundFile); // Saves data defined on jorgeLua.h
fclose (notFoundFile);
// TODO try to bake in lua scripts from standalone files at compile time.
// Cmake possibly could help.
}
}
{ // Setup database
sqlite3 *db;
int dbErr = sqlite3_open("jorge.db", &db);
if(dbErr){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return;
}
printf("Opened database successfully\n");
sqlite3_stmt *statement;
sqlite3_prepare_v2(db,
"CREATE TABLE messages("
"id INTEGER PRIMARY KEY,"
"msg TEXT NOT NULL,"
"sender TEXT,"
"time TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
")",
-1, &statement, NULL);
int result = sqlite3_step(statement);
if(result == SQLITE_DONE){
printf("Setting up tables\n");
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
printf("Welcome to jorge!\n");
}
// This global is the way jluaf functions can interact with request data
// This should be only accessible inside the jLua module.
struct jlua_global_data jData;
// The entry point to the lua module. Gets a connection and request data,
// starts a lua interpreter and runs the related scripts
// TODO Select script to run based on the request path
void jlua_interpret(int conn_fd, struct jnet_request_data request){
// Setup connection data
jData.connection = conn_fd;
jData.response_body_length = 0;
jData.response_body = NULL;
jData.response_header = NULL;
// Deal with request data
// Fire up interpreter
int luaStatus;
lua_State *L = luaL_newstate();
// Init functions
luaL_openlibs(L);
lua_register(L, "echo", jluaf_echo);
lua_register(L, "setHeader", jluaf_setHeader);
lua_register(L, "sqlQuery", jluaf_sqlQuery);
// Globals
lua_pushstring( L, request.path);
lua_setglobal( L, "PATH");
lua_pushstring( L, request.verb);
lua_setglobal( L, "VERB");
lua_pushstring( L, request.body);
lua_setglobal( L, "BODY");
// Load Script
bool scriptError = false;
{
int lastChar = 0;
while(request.path[lastChar] != '\0') lastChar++;
// This here is a REALLY HACKY solution
// Since the line which contains the path ends with ` HTTP/1.X\r\n`
// and the version is saved as value, we assume that `index.lua\0`
// will fit there without messing up the next line.
// Coincidence? sure. but it works.
if(request.path[lastChar-1] == '/'){
request.path[lastChar++] = 'i';
request.path[lastChar++] = 'n';
request.path[lastChar++] = 'd';
request.path[lastChar++] = 'e';
request.path[lastChar++] = 'x';
request.path[lastChar++] = '.';
request.path[lastChar++] = 'l';
request.path[lastChar++] = 'u';
request.path[lastChar++] = 'a';
request.path[lastChar] = '\0';
}
size_t scriptPathLength = strlen(JLUA_SCRIPT_PATH) + strlen(request.path) + 1;
char * scriptPath = (char *) malloc(scriptPathLength * sizeof(*scriptPath));
memset(scriptPath, '\0', scriptPathLength);
strcat(scriptPath, JLUA_SCRIPT_PATH);
strcat(scriptPath, request.path);
luaStatus = luaL_loadfile(L, scriptPath);
free(scriptPath);
if(luaStatus){
jlua_print_error(L);
luaStatus = luaL_loadfile(L, JLUA_SCRIPT_PATH"/404.lua");
if(luaStatus){
jlua_print_error(L);
scriptError = true;
}
}
}
if(!scriptError)luaStatus = lua_pcall(L, 0, 0, 0);
if(luaStatus){
jlua_print_error(L);
// Free Header
if(jData.response_header != NULL)free(jData.response_header);
// Free body
jlua_response_body_node *walker = jData.response_body;
jlua_response_body_node *last;
while(walker != NULL){
last = walker;
walker = walker->next;
free(last->data);
free(last);
}
jData.response_body_length = 0;
jData.response_body = NULL;
jData.response_header = NULL;
lua_pushinteger( L, 503);
lua_setglobal( L, "ERR");
luaL_loadfile(L, JLUA_SCRIPT_PATH"/404.lua");
lua_pcall(L, 0, 0, 0);
}
// Send Header
if(jData.response_header != NULL){
size_t headerLen = strlen(jData.response_header);
jnet_send_all(jData.connection, jData.response_header, &headerLen, 0);
free(jData.response_header);
}
// Send body
jlua_response_body_node *walker = jData.response_body;
jlua_response_body_node *last;
while(walker != NULL){
size_t bodyLen = strlen(walker->data);
if(!scriptError) jnet_send_all(jData.connection, walker->data, &bodyLen, !walker->next);
last = walker;
walker = walker->next;
free(last->data);
free(last);
}
lua_close(L);
}
int jlua_run_file(lua_State* L, char *name){
int luaStatus;
return luaStatus;
}
// Prints a lua error.
// The existance of an error is indicated by a lua state function returning a false value
void jlua_print_error(lua_State* L) {
// The error message is on top of the stack.
// Fetch it, print it and then pop it off the stack.
const char* message = lua_tostring(L, -1);
fprintf(stderr, "%s\n", message);
lua_pop(L, 1);
}
// All jluaf functions are made to be registered and used in lua
// Receives any number of strings and adds them to the body linked list
// The list will be unrolled, sent and freed after the headers are sent
int jluaf_echo(lua_State* L){
int args = lua_gettop(L);
for(int i=1; i<=args; i++){
struct jlua_response_body_node *response_body_end;
response_body_end =
(struct jlua_response_body_node*) malloc(sizeof(*response_body_end)+1);
response_body_end->next = NULL;
response_body_end->data = strdup(lua_tostring(L, i));
size_t body_bytes = strlen(response_body_end->data);
jData.response_body_length += body_bytes;
lua_pushnumber(L, body_bytes);
// TODO maybe checkout and correct linebreaks
// Though this is easier to be done on the scripts
// Maybe we could create a lua function that abstracts this callable
if(jData.response_body){
struct jlua_response_body_node *walker = jData.response_body;
while(walker->next != NULL) walker = walker->next;
walker->next = response_body_end;
}else jData.response_body = response_body_end;
}
return 1;
}
// Adds a single string to the headers linked list
// The list will be unrolled, sent and freed at the end of the request processing
int jluaf_setHeader(lua_State* L){
if(jData.response_header) free(jData.response_header);
jData.response_header = strdup(lua_tostring(L, 1));
lua_pushnumber(L, strlen(jData.response_header));
return 1;
}
int jluaf_sqlQuery(lua_State *L){ //TODO Add binding to avoid injections
int args = lua_gettop(L);
if(args < 1 || lua_type(L, -1) != LUA_TSTRING){
lua_newtable(L);
lua_pushinteger(L, 0);
lua_setfield(L, -2, "count");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "modified");
lua_newtable(L);
lua_setfield(L, -2, "data");
lua_pushstring(L, "Missing query");
return 2;
}
char *queryString = strdup(lua_tostring(L, -1));
sqlite3 *db;
int dbErr = sqlite3_open("jorge.db", &db);
if(dbErr){
lua_newtable(L);
lua_pushinteger(L, 0);
lua_setfield(L, -2, "count");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "modified");
lua_newtable(L);
lua_setfield(L, -2, "data");
lua_pushstring(L, sqlite3_errmsg(db));
return 2;
}
sqlite3_stmt *statement;
sqlite3_prepare_v2(db, queryString, -1, &statement, NULL);
int result = sqlite3_step(statement);
int modRows= sqlite3_changes(db);
lua_newtable(L);
lua_newtable(L);
char *err = NULL;
int rows = 0;
while(result != SQLITE_DONE){
if(result != SQLITE_ROW){
err = strdup(sqlite3_errmsg(db));
break;
}
rows++;
int colCount = sqlite3_column_count(statement);
lua_pushnumber(L, rows);
lua_createtable(L, 0, colCount);
for(int i=0; i < colCount; i++){
//printf(" %s is ", sqlite3_column_name(statement, i));
switch(sqlite3_column_type(statement, i)){
case SQLITE_INTEGER:
lua_pushinteger(L, sqlite3_column_int(statement, i));
//printf("int: %d|", sqlite3_column_int(statement, i));
break;
case SQLITE_FLOAT:
lua_pushnumber(L, sqlite3_column_double(statement, i));
//printf("float: %.2f|", sqlite3_column_double(statement, i));
break;
case SQLITE_TEXT:
lua_pushstring(L, (const char*)sqlite3_column_text(statement, i));
//printf("text: %s|", (const char*)sqlite3_column_text(statement, i));
break;
case SQLITE_BLOB:
lua_pushnil(L);
//printf("BLOB|");
break;
case SQLITE_NULL:
lua_pushnil(L);
//printf("NULL|");
break;
}
lua_setfield(L, -2, sqlite3_column_name(statement, i));
}
lua_settable(L, -3);
result = sqlite3_step(statement);
//printf("\n");
}
lua_setfield(L, -2, "data");
lua_pushinteger(L, rows);
lua_setfield(L, -2, "count");
lua_pushinteger(L, modRows);
lua_setfield(L, -2, "modified");
if(err){
lua_pushstring(L, err);
free(err);
}else{
lua_pushnil(L);
}
sqlite3_finalize(statement);
sqlite3_close(db);
free(queryString);
return 2;
}