aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorame <[email protected]>2026-06-08 22:43:05 -0500
committerame <[email protected]>2026-06-08 22:43:05 -0500
commit66aedfd65cff3494b70e8072646094479e3bfed8 (patch)
tree0106935549143d542819da445e9a74740d617d94 /src/net
parent4cbf47f295fc65e4a470d93194259d80c942b32b (diff)
net util
Diffstat (limited to 'src/net')
-rw-r--r--src/net/lua.c24
-rw-r--r--src/net/lua.h1
-rw-r--r--src/net/util.c22
-rw-r--r--src/net/util.h1
4 files changed, 47 insertions, 1 deletions
diff --git a/src/net/lua.c b/src/net/lua.c
index ffa10c4..025828b 100644
--- a/src/net/lua.c
+++ b/src/net/lua.c
@@ -85,11 +85,29 @@ int l_send(lua_State* L){
else closesocket(ctx->sock);
ctx->sock = -1;
+ lua_pushboolean(L, 0);
+ lua_setfield(L, res_idx, "open");
+
//printf("%i | %i\n'%s'\n%i\n",client_fd,a,resp->c,resp->len);
str_free(resp);
return 0;
}
+int l_neterror(lua_State* L){
+ int res_idx = 1;
+ lua_getfield(L, res_idx, "_");
+ struct net_data* ctx = lua_touserdata(L, -1);
+
+ client_fd_errors(ctx->sock);
+
+ net_error(ctx, luaL_checkinteger(L, 2));
+
+ lua_pushboolean(L, 0);
+ lua_setfield(L, res_idx, "open");
+
+ return 0;
+}
+
int l_close(lua_State* L){
int res_idx = 1;
@@ -103,6 +121,9 @@ int l_close(lua_State* L){
else closesocket(ctx->sock);
ctx->sock = -1;
+ lua_pushboolean(L, 0);
+ lua_setfield(L, res_idx, "open");
+
return 0;
}
@@ -267,6 +288,9 @@ int l_sendfile(lua_State* L){
break;
}
+ lua_pushboolean(L, 0);
+ lua_setfield(L, res_idx, "open");
+
free(buffer);
fclose(fp);
diff --git a/src/net/lua.h b/src/net/lua.h
index 492a999..ef4c0e4 100644
--- a/src/net/lua.h
+++ b/src/net/lua.h
@@ -2,6 +2,7 @@
int l_write(lua_State* L);
int l_send(lua_State* L);
+int l_neterror(lua_State* L);
int l_close(lua_State* L);
int l_stop(lua_State* L);
int l_roll(lua_State* L);
diff --git a/src/net/util.c b/src/net/util.c
index d770f5e..ce4fc26 100644
--- a/src/net/util.c
+++ b/src/net/util.c
@@ -1,5 +1,6 @@
#include "common.h"
#include "util.h"
+#include "ssl.h"
#include <ctype.h>
int64_t recv_header(struct net_data* data, char** _buffer, char** header_eof){
@@ -467,7 +468,11 @@ void _parse_mimetypes(){
int net_error(struct net_data* ctx, int code){
char out[512] = {0};
- sprintf(out, "HTTP/1.1 %i %s\n\n", code, http_code(code));
+ str* s = str_init(http_code(code));
+ str_lowercase(s);
+
+ sprintf(out, "HTTP/1.1 %i %s\n\n%s", code, http_code(code), s->c);
+ free(s);
net_ctx_write(ctx, out, strlen(out));
return 0;
}
@@ -513,3 +518,18 @@ int net_ctx_write(struct net_data* data, void* buffer, size_t c){
}
return SSL_write(data->ssl, buffer, c);
}
+
+int net_ctx_close(struct net_data* data){
+ if(data->sock != -1){
+ if(data->ssl != NULL) SSL_shutdown(data->ssl);
+ else {
+ shutdown(data->sock, 2);
+ closesocket(data->sock);
+ }
+ }
+
+ data->sock = -1;
+ free(data);
+
+ return 0;
+}
diff --git a/src/net/util.h b/src/net/util.h
index 8b11e85..602ee53 100644
--- a/src/net/util.h
+++ b/src/net/util.h
@@ -60,3 +60,4 @@ int percent_decode(str* input, str** _output);
int net_ctx_read(struct net_data* data, void* buffer, size_t c);
int net_ctx_write(struct net_data* data, void* buffer, size_t c);
+int net_ctx_close(struct net_data* data);