-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaWebserverHelper.c
More file actions
135 lines (120 loc) · 4.02 KB
/
LuaWebserverHelper.c
File metadata and controls
135 lines (120 loc) · 4.02 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
#include "LuaWebserverHelper.h"
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
LuaHttpResponse* callLuaFunc(lua_State* L, int luaRef, HttpRequest* request)
{
if (L == NULL || request == NULL) {
return NULL;
}
LuaHttpResponse* response = (LuaHttpResponse*)malloc(sizeof(LuaHttpResponse));
if (!response) {
lua_pushstring(L, "Error when allocating memory for response.");
return NULL;
}
lua_settop(L, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaRef);
lua_newtable(L);
lua_pushstring(L, request->method);
lua_setfield(L, -2, "method");
lua_pushstring(L, request->path);
lua_setfield(L, -2, "path");
lua_pushstring(L, request->url);
lua_setfield(L, -2, "url");
lua_pushinteger(L, request->contentLength);
lua_setfield(L, -2, "contentLength");
lua_pushstring(L, request->host);
lua_setfield(L, -2, "host");
lua_pushstring(L, request->remoteAddr);
lua_setfield(L, -2, "remoteAddr");
lua_pushstring(L, request->body);
lua_setfield(L, -2, "body");
lua_newtable(L);
for (int i = 0; i < request->headersCount; ++i) {
lua_pushstring(L, request->headersValues[i]);
lua_setfield(L, -2, request->headersKeys[i]);
}
lua_setfield(L, -2, "headers");
lua_pushinteger(L, request->headersCount);
lua_setfield(L, -2, "headersCount");
int error = 0;
error = lua_pcall(L, 1, LUA_MULTRET, 0);
if (error != 0) {
const char* errorMsg = lua_tostring(L, -1);
lua_pop(L, 1); // Remove the error message from the stack
free(response);
lua_pushfstring(L, "Error when calling hook function: %s", errorMsg);
return NULL;
}
if (!lua_isnumber(L, 1)) {
free(response);
lua_pushfstring(L, "Argument %d (%s) must be a %s in function %s", 1, "status code", "integer", "http hook return");
return NULL;
}
int statusCode = (int)lua_tonumber(L, 1);
response->statusCode = statusCode;
if (statusCode == 404 && lua_gettop(L) == 1) {
response->headersCount = 0;
response->responseBody = NULL;
return response;
}
if (!lua_isstring(L, 2)) {
free(response);
lua_pushfstring(L, "Argument %d (%s) must be a %s in function %s", 2, "response body", "string", "http hook return");
return NULL;
}
if (!lua_istable(L, 3)) {
free(response);
lua_pushfstring(L, "Argument %d (%s) must be a %s in function %s", 3, "header table", "table", "http hook return");
return NULL;
}
const char* responseBody = lua_tostring(L, -2);
response->responseBody = malloc(strlen(responseBody) + 1);
if (response->responseBody != NULL) {
strcpy(response->responseBody, responseBody);
}
if (lua_istable(L, -1)) {
lua_pushnil(L);
int i = 0;
while (lua_next(L, -2) != 0) {
const char* headerName = lua_tostring(L, -2);
const char* headerValue = lua_tostring(L, -1);
if (i < 50) {
strncpy(response->headersKeys[i], headerName, 255);
response->headersKeys[i][255] = '\0';
strncpy(response->headersValues[i], headerValue, 255);
response->headersValues[i][255] = '\0';
}
lua_pop(L, 1);
i++;
}
response->headersCount = i;
}
lua_pop(L, 3);
return response;
}
void callLuaWebSocketFunc(lua_State* L, int luaRef, char* client, int messagetype, char* message)
{
if (L == NULL || message == NULL) {
return;
}
lua_settop(L, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaRef);
lua_pushstring(L, client);
lua_pushinteger(L, messagetype);
lua_pushstring(L, message);
free(message);
free(client);
int error = 0;
error = lua_pcall(L, 3, 0, 0);
if (error != 0) {
const char* errorMsg = lua_tostring(L, -1);
lua_pop(L, 1);
printf("%s\n", errorMsg);
return;
}
}