aboutsummaryrefslogtreecommitdiff
path: root/src/net/lua.c
blob: f005185b8eff4396b0a1648d5e3965e2dbfebff8 (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
#include "lua.h"
#include "luai.h"
#include "common.h"

int l_write(lua_State* L){
  int res_idx = 1;

  lua_pushvalue(L, 1);
  lua_pushstring(L, "_request");
  lua_gettable(L, -2);

  int head = strcmp(luaL_checkstring(L, -1), "HEAD") == 0;

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "client_fd");
  lua_gettable(L, res_idx);
  int client_fd = luaL_checkinteger(L, -1);

  client_fd_errors(client_fd);

  size_t len;
  char* content = (char*)luaL_checklstring(L, 2, &len);

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "header");
  lua_gettable(L, -2);
  int header_top = lua_gettop(L);

  lua_pushstring(L, "_sent");
  lua_gettable(L, -2);
  str* resp;
  if(lua_isnil(L, -1)){
    if(head) i_write_header(L, header_top, &resp, "", 0);
    else i_write_header(L, header_top, &resp, content, len);

    lua_pushvalue(L, header_top);
    lua_pushstring(L, "_sent");
    lua_pushinteger(L, 1);
    lua_settable(L, -3);
  } else {
    if(head) return 0;
    resp = str_init(content);
  }

  send(client_fd, resp->c, resp->len, MSG_NOSIGNAL);

  str_free(resp);
  return 0;
}

int l_send(lua_State* L){
  int res_idx = 1;
  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "client_fd");
  lua_gettable(L, res_idx);
  int client_fd = luaL_checkinteger(L, -1);

  client_fd_errors(client_fd);

  size_t len;
  char* content = (char*)luaL_checklstring(L, 2, &len);

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "header");
  lua_gettable(L, -2);
  int header = lua_gettop(L);

  str* resp;
  lua_pushvalue(L, 1);
  lua_pushstring(L, "_request");
  lua_gettable(L, -2);

  if(strcmp(luaL_checkstring(L, -1), "HEAD") == 0){
    i_write_header(L, header, &resp, "", 0);
  } else 
    i_write_header(L, header, &resp, content, len);

  send(client_fd, resp->c, resp->len, MSG_NOSIGNAL);

  //
  lua_pushstring(L, "client_fd");
  lua_pushinteger(L, -1);
  lua_settable(L, res_idx);
  closesocket(client_fd);
  //printf("%i | %i\n'%s'\n%i\n",client_fd,a,resp->c,resp->len);
  str_free(resp);
  return 0;
}

int l_close(lua_State* L){
  int res_idx = 1;

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "client_fd");
  lua_gettable(L, res_idx);
  int client_fd = luaL_checkinteger(L, -1);
  client_fd_errors(client_fd);

  lua_pushstring(L, "client_fd");
  lua_pushinteger(L, -1);
  lua_settable(L, res_idx);
  closesocket(client_fd);

  return 0;
}

int l_stop(lua_State* L){
  int res_idx = 1;

  lua_pushstring(L, "_stop");
  lua_pushboolean(L, 1);
  lua_settable(L, res_idx);

  return 0;
}

int l_roll(lua_State* L){
  int64_t alen;
  if(lua_gettop(L) > 2) {
    alen = luaL_checkinteger(L, 2);
  } else {
    alen = -1;
  }

  lua_pushvalue(L, 1);
  lua_pushstring(L, "_bytes");
  lua_gettable(L, 1);
  int64_t bytes = luaL_checkinteger(L, -1);

  lua_pushstring(L, "Content-Length");
  lua_gettable(L, 1);
  if(lua_type(L, -1) == LUA_TNIL) {
    lua_pushinteger(L, -1);
    return 1;
  }
  uint64_t content_length = strtol(luaL_checkstring(L, -1), NULL, 10);
  lua_pushstring(L, "_data");
  lua_gettable(L, 1);
  struct file_parse* data = (void*)lua_topointer(L, -1); 

  lua_pushvalue(L, 1);
  lua_pushstring(L, "client_fd");
  lua_gettable(L, 1);
  int client_fd = luaL_checkinteger(L, -1);
  client_fd_errors(client_fd);

  fd_set rfd;
  FD_ZERO(&rfd);
  FD_SET(client_fd, &rfd);
  //printf("* %li / %li\n", bytes, content_length);
  if(bytes >= content_length){
    lua_pushinteger(L, -1);
    return 1;
  }

  if(select(client_fd+1, &rfd, NULL, NULL, &((struct timeval){.tv_sec = 0, .tv_usec = 0})) == 0){
    lua_pushinteger(L, 0);
    return 1;
  }


  //time_start(recv)
  if(alen == -1) alen = content_length - bytes;
  char* buffer = malloc(alen * sizeof * buffer);
  int r = recv(client_fd, buffer, alen, 0);
  if(r <= 0){
    lua_pushinteger(L, r - 1);
    return 1;
  }
  //time_end("recv", recv)

  lua_pushstring(L, "_bytes");
  lua_pushinteger(L, bytes + r);
  lua_settable(L, 1);

  lua_pushstring(L, "Body");
  lua_gettable(L, 1);
  int body_idx = lua_gettop(L);

  lua_pushstring(L, "files");
  lua_gettable(L, 1);
  int files_idx = lua_gettop(L);
  //time_start(parse)
  rolling_file_parse(L, &files_idx, &body_idx, buffer, NULL, r, data);
  //time_end("parse", parse)
  luaI_tsetv(L, 1, "Body", body_idx);
  luaI_tsetv(L, 1, "files", files_idx);

  free(buffer);
  lua_pushinteger(L, r);
  return 1;
}

#define bsize 32768
//#define bsize 12
int l_sendfile(lua_State* L){
  int res_idx = 1;
  int attachment = 0;

  char* path = (char*)luaL_checkstring(L, 2);
  char* filename = path;

  if(lua_gettop(L) > 2 && lua_type(L, 3) == LUA_TTABLE){
    lua_pushstring(L, "attachment");
    lua_gettable(L, 3);
    if(!lua_isnil(L, -1)) attachment = lua_toboolean(L, -1);

    lua_pushstring(L, "filename");
    lua_gettable(L, 3);
    if(!lua_isnil(L, -1)) filename = (char*)lua_tostring(L, -1);
  }

  luaI_assert(L, !access(path, F_OK) /*file not found*/);
  luaI_assert(L, !access(path, R_OK) /*missing permissions*/);

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "client_fd");
  lua_gettable(L, res_idx);
  int client_fd = luaL_checkinteger(L, -1);
  client_fd_errors(client_fd);

  lua_pushvalue(L, res_idx);
  lua_pushstring(L, "header");
  lua_gettable(L, -2);
  int header = lua_gettop(L);

  lua_pushstring(L, "Content-Type");
  lua_gettable(L, header);

  char* ext = strrchr(path, '.');
  if(lua_isnil(L, -1) && ext && mime_type != NULL){
    char* content_type = map_get(mime_type, ext + 1);

    if(content_type) 
    {luaI_tsets(L, header, "Content-Type", content_type);}
  }

  char* buffer = calloc(sizeof* buffer, bsize + 1);
  FILE* fp = fopen(path, "rb");
  fseek(fp, 0L, SEEK_END);
  size_t sz = ftell(fp);
  fseek(fp, 0L, SEEK_SET);

  char size[256];
  sprintf(size, "%li", sz);
  luaI_tsets(L, header, "Content-Length", size);

  if(attachment) {
    char disp[256];
    sprintf(disp, "attachment; filename=\"%s\"", filename);
    luaI_tsets(L, header, "Content-Disposition", disp);
  } else {
    luaI_tsets(L, header, "Content-Disposition", "inline;");
  }

  str* r;
  i_write_header(L, header, &r, "", 0);
  send(client_fd, r->c, r->len, MSG_NOSIGNAL);
  str_free(r);

  for(size_t i = 0; i < sz; i += bsize){
    fread(buffer, sizeof * buffer, bsize, fp);
    if(send(client_fd, buffer, bsize > sz - i ? sz - i : bsize, MSG_NOSIGNAL) == -1)
      break;
  }

  free(buffer);
  fclose(fp);

  return 0;
}