aboutsummaryrefslogtreecommitdiff
path: root/src/net/lua.c
diff options
context:
space:
mode:
authorame <[email protected]>2026-02-15 04:08:16 -0600
committerame <[email protected]>2026-02-15 04:08:16 -0600
commitdb2611fcad18f73572dd1b344e4197536086be53 (patch)
tree8d6df833110e57fa7d77753571acfda2ebb23f95 /src/net/lua.c
parent0a909a9dc5879e592d92c6eedeb59da8cf503392 (diff)
ssl server support, websocket upgrades, and net changes
Diffstat (limited to 'src/net/lua.c')
-rw-r--r--src/net/lua.c109
1 files changed, 67 insertions, 42 deletions
diff --git a/src/net/lua.c b/src/net/lua.c
index f005185..d2b0d35 100644
--- a/src/net/lua.c
+++ b/src/net/lua.c
@@ -1,6 +1,9 @@
#include "lua.h"
#include "luai.h"
+#include "util.h"
#include "common.h"
+#include "../hash/fnv.h"
+#include "websocket.h"
int l_write(lua_State* L){
int res_idx = 1;
@@ -12,11 +15,12 @@ int l_write(lua_State* L){
int head = strcmp(luaL_checkstring(L, -1), "HEAD") == 0;
lua_pushvalue(L, res_idx);
- lua_pushstring(L, "client_fd");
+ lua_pushstring(L, "_");
lua_gettable(L, res_idx);
- int client_fd = luaL_checkinteger(L, -1);
+ struct net_data* ctx = lua_touserdata(L, -1);
+
- client_fd_errors(client_fd);
+ client_fd_errors(ctx->sock);
size_t len;
char* content = (char*)luaL_checklstring(L, 2, &len);
@@ -42,7 +46,7 @@ int l_write(lua_State* L){
resp = str_init(content);
}
- send(client_fd, resp->c, resp->len, MSG_NOSIGNAL);
+ net_ctx_write(ctx, resp->c, resp->len);
str_free(resp);
return 0;
@@ -51,11 +55,11 @@ int l_write(lua_State* L){
int l_send(lua_State* L){
int res_idx = 1;
lua_pushvalue(L, res_idx);
- lua_pushstring(L, "client_fd");
+ lua_pushstring(L, "_");
lua_gettable(L, res_idx);
- int client_fd = luaL_checkinteger(L, -1);
+ struct net_data* ctx = lua_touserdata(L, -1);
- client_fd_errors(client_fd);
+ client_fd_errors(ctx->sock);
size_t len;
char* content = (char*)luaL_checklstring(L, 2, &len);
@@ -75,13 +79,12 @@ int l_send(lua_State* L){
} else
i_write_header(L, header, &resp, content, len);
- send(client_fd, resp->c, resp->len, MSG_NOSIGNAL);
+ net_ctx_write(ctx, resp->c, resp->len);
+
+ if(ctx->ssl != NULL) SSL_shutdown(ctx->ssl);
+ else closesocket(ctx->sock);
+ ctx->sock = -1;
- //
- 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;
@@ -91,15 +94,14 @@ int l_close(lua_State* L){
int res_idx = 1;
lua_pushvalue(L, res_idx);
- lua_pushstring(L, "client_fd");
+ lua_pushstring(L, "_");
lua_gettable(L, res_idx);
- int client_fd = luaL_checkinteger(L, -1);
- client_fd_errors(client_fd);
+ struct net_data* ctx = lua_touserdata(L, -1);
+ client_fd_errors(ctx->sock);
- lua_pushstring(L, "client_fd");
- lua_pushinteger(L, -1);
- lua_settable(L, res_idx);
- closesocket(client_fd);
+ if(ctx->ssl != NULL) SSL_shutdown(ctx->ssl);
+ else closesocket(ctx->sock);
+ ctx->sock = -1;
return 0;
}
@@ -127,7 +129,7 @@ int l_roll(lua_State* L){
lua_gettable(L, 1);
int64_t bytes = luaL_checkinteger(L, -1);
- lua_pushstring(L, "Content-Length");
+ lua_pushstring(L, "content-length");
lua_gettable(L, 1);
if(lua_type(L, -1) == LUA_TNIL) {
lua_pushinteger(L, -1);
@@ -139,30 +141,31 @@ int l_roll(lua_State* L){
struct file_parse* data = (void*)lua_topointer(L, -1);
lua_pushvalue(L, 1);
- lua_pushstring(L, "client_fd");
+ lua_pushstring(L, "_");
lua_gettable(L, 1);
- int client_fd = luaL_checkinteger(L, -1);
- client_fd_errors(client_fd);
+ struct net_data* ctx = lua_touserdata(L, -1);
+
+ client_fd_errors(ctx->sock);
- fd_set rfd;
- FD_ZERO(&rfd);
- FD_SET(client_fd, &rfd);
+ //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){
+ /*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);
+ int r = net_ctx_read(ctx, buffer, alen);
if(r <= 0){
lua_pushinteger(L, r - 1);
return 1;
@@ -173,7 +176,7 @@ int l_roll(lua_State* L){
lua_pushinteger(L, bytes + r);
lua_settable(L, 1);
- lua_pushstring(L, "Body");
+ lua_pushstring(L, "body");
lua_gettable(L, 1);
int body_idx = lua_gettop(L);
@@ -183,7 +186,7 @@ int l_roll(lua_State* 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, "body", body_idx);
luaI_tsetv(L, 1, "files", files_idx);
free(buffer);
@@ -214,17 +217,17 @@ int l_sendfile(lua_State* L){
luaI_assert(L, !access(path, R_OK) /*missing permissions*/);
lua_pushvalue(L, res_idx);
- lua_pushstring(L, "client_fd");
+ lua_pushstring(L, "_");
lua_gettable(L, res_idx);
- int client_fd = luaL_checkinteger(L, -1);
- client_fd_errors(client_fd);
+ struct net_data* ctx = lua_touserdata(L, -1);
+ client_fd_errors(ctx->sock);
lua_pushvalue(L, res_idx);
lua_pushstring(L, "header");
lua_gettable(L, -2);
int header = lua_gettop(L);
- lua_pushstring(L, "Content-Type");
+ lua_pushstring(L, "content-type");
lua_gettable(L, header);
char* ext = strrchr(path, '.');
@@ -232,7 +235,7 @@ int l_sendfile(lua_State* L){
char* content_type = map_get(mime_type, ext + 1);
if(content_type)
- {luaI_tsets(L, header, "Content-Type", content_type);}
+ {luaI_tsets(L, header, "content-type", content_type);}
}
char* buffer = calloc(sizeof* buffer, bsize + 1);
@@ -243,24 +246,24 @@ int l_sendfile(lua_State* L){
char size[256];
sprintf(size, "%li", sz);
- luaI_tsets(L, header, "Content-Length", size);
+ 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);
+ luaI_tsets(L, header, "content-disposition", disp);
} else {
- luaI_tsets(L, header, "Content-Disposition", "inline;");
+ 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);
+ net_ctx_write(ctx, r->c, r->len);
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)
+ if(net_ctx_write(ctx, buffer, bsize > sz - i ? sz - i : bsize) == -1)
break;
}
@@ -269,3 +272,25 @@ int l_sendfile(lua_State* L){
return 0;
}
+
+int l_connection_upgrade(lua_State* L){
+ int res_idx = 1;
+ lua_getfield(L, res_idx, "req");
+ int req_idx = 2;
+
+ lua_getfield(L, req_idx, "upgrade");
+ uint64_t hash, len;
+ uint8_t* s = (uint8_t*)luaL_checklstring(L, -1, &len);
+ hash = fnv_1(s, len, v_1);
+
+ switch(hash){
+ case 0xf042f81495060e72: //websocket
+ l_websocket_upgrade(L);
+ break;
+ default:
+ luaI_error(L, -1, "can't upgrade");
+ break;
+ }
+
+ return 0;
+}