aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/net.c75
-rw-r--r--src/net.h35
2 files changed, 82 insertions, 28 deletions
diff --git a/src/net.c b/src/net.c
index 603c954..6b5df9b 100644
--- a/src/net.c
+++ b/src/net.c
@@ -1,15 +1,5 @@
#include "lua5.4/lauxlib.h"
#include "lua5.4/lua.h"
-#ifdef _WIN32 //add -lws2_32
- #include <winsock2.h>
- //#define socklen_t __socklen_t
- //#define close closesocket
- typedef int socklen_t;
-#else
- #include <sys/socket.h>
- #include <arpa/inet.h>
-#define closesocket close
-#endif
#include <sys/types.h>
#include <stdio.h>
@@ -22,7 +12,6 @@
#include "net.h"
#include "lua.h"
-
#include "io.h"
#include "table.h"
#include "types/str.h"
@@ -423,22 +412,6 @@ int l_close(lua_State* L){
return 0;
}
-int l_serve(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);
-
- char* path = (char*)luaL_checkstring(L, 2);
-
- //continue here
-
- return 0;
-}
-
int content_disposition(str* src, parray_t** _dest){
char* end = strnstr(src->c, ";", src->len);
@@ -714,6 +687,52 @@ int l_roll(lua_State* L){
return 1;
}
+#define bsize 512
+int l_sendfile(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_pushvalue(L, res_idx);
+ lua_pushstring(L, "header");
+ lua_gettable(L, -2);
+ int header = lua_gettop(L);
+
+ char* path = (char*)luaL_checkstring(L, 2);
+
+ if(access(path, F_OK)) {
+ p_fatal("file not found"); //TODO: use diff errors here
+ }
+ if(access(path, R_OK)){
+ p_fatal("missing permissions");
+ }
+
+ str* r;
+ i_write_header(L, header, &r, "", 0);
+ send(client_fd, r->c, r->len, 0);
+ free(r);
+
+ 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);
+
+ for(int i = 0; i < sz; i += bsize){
+ fread(buffer, sizeof * buffer, bsize, fp);
+ send(client_fd, buffer, bsize > sz - i ? sz - i : bsize, 0);
+ }
+
+ free(buffer);
+ fclose(fp);
+
+ return 0;
+}
+
volatile size_t threads = 0;
void* handle_client(void *_arg){
//printf("--\n");
@@ -855,7 +874,7 @@ void* handle_client(void *_arg){
//functions
luaI_tsetcf(L, res_idx, "send", l_send);
- //luaI_tsetcf(L, res_idx, "serve", l_serve);
+ luaI_tsetcf(L, res_idx, "sendfile", l_sendfile);
luaI_tsetcf(L, res_idx, "write", l_write);
luaI_tsetcf(L, res_idx, "close", l_close);
diff --git a/src/net.h b/src/net.h
index 95b6cf9..2efab03 100644
--- a/src/net.h
+++ b/src/net.h
@@ -1,7 +1,42 @@
+#ifdef _WIN32 //add -lws2_32
+ #include <winsock2.h>
+ //#define socklen_t __socklen_t
+ //#define close closesocket
+ typedef int socklen_t;
+#else
+ #include <sys/socket.h>
+ #include <arpa/inet.h>
+#define closesocket close
+#endif
+
#include "lua.h"
+#include "types/str.h"
+#include "types/parray.h"
+#include <stdint.h>
int l_listen(lua_State*);
+int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state);
+
+int parse_header(char* buffer, int header_eof, parray_t** _table);
+
+void http_build(str** _dest, int code, char* code_det, char* header_vs, char* content, size_t len);
+
+void http_code(int code, char* code_det);
+
+void i_write_header(lua_State* L, int header_top, str** _resp, char* content, size_t len);
+
+void client_fd_errors(int client_fd);
+
+int content_disposition(str* src, parray_t** _dest);
+
+//int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer, str* content_type, size_t blen, struct file_parse* _content);
+
+void* handle_client(void *_arg);
+
+int start_serv(lua_State* L, int port);
+
+//
static char* http_codes[600] = {0};
extern volatile size_t threads;