From b0fa1d1bcd4c07ce8623a5b6e8fdfb4c06ec6afc Mon Sep 17 00:00:00 2001 From: amelia squires Date: Thu, 9 Jan 2025 15:11:45 -0600 Subject: http(s) request --- makefile | 2 +- src/net.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/net.h | 6 +++ tests/req.lua | 4 ++ 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/req.lua diff --git a/makefile b/makefile index 086ee34..991b84f 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ CC := clang GIT_COMMIT := "$(shell git describe --tags)-$(shell git describe --always --match 'NOT A TAG')" CFLAGS := -fPIC -DGIT_COMMIT='$(GIT_COMMIT)' -LFLAGS := -lm -shared +LFLAGS := -lm -shared -lcrypto -lssl LINKER := $(CC) TARGET := lullaby.so diff --git a/src/net.c b/src/net.c index 7cc3b12..91abdcb 100644 --- a/src/net.c +++ b/src/net.c @@ -3,9 +3,134 @@ #include "net/lua.h" #include "net/luai.h" +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#define pab(M) {printf(M);abort();} + +SSL* ssl_connect(SSL_CTX* ctx, int sockfd, const char* hostname){ + SSL* ssl = SSL_new(ctx); + if(hostname != NULL) + SSL_set_tlsext_host_name(ssl, hostname); + + SSL_set_fd(ssl, sockfd); + + if(SSL_connect(ssl) < 0){ + SSL_free(ssl); + + return NULL; + } + + return ssl; +} + +int get_host(char* hostname, char* port){ + int sockfd; + struct addrinfo hints; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = 0; + + struct addrinfo *result, *awa; + if(getaddrinfo(hostname, port, &hints, &result) != 0) + return -1; + + for(awa = result; awa != NULL; awa = awa->ai_next){ + sockfd = socket(awa->ai_family, awa->ai_socktype, awa->ai_protocol); + + if(sockfd == -1) continue; + + if(connect(sockfd, awa->ai_addr, awa->ai_addrlen) != -1) + break; + + close(sockfd); + sockfd = -1; + } + + freeaddrinfo(result); + + return sockfd; +} + +int l_srequest(lua_State* L){ + const char* host = luaL_checkstring(L, 1); + int sock = get_host((char*)host, lua_tostring(L, 2)); + if(sock == -1) abort(); + + SSL_library_init(); + SSL_load_error_strings(); + SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method()); + SSL* ssl = ssl_connect(ctx, sock, host); + + char* path = "/"; + if(lua_gettop(L) >= 3) + path = (char*)luaL_checkstring(L, 3); + + //char* req = "GET / HTTP/1.1\nHost: amyy.cc\nConnection: Close\n\n"; + + char request[2000]; + sprintf(request, "GET %s HTTP/1.1\nHost: %s\nConnection: Close\n\n", path, host); + printf("%s\n", request); + SSL_write(ssl, request, strlen(request)); + + str* a = str_init(""); + char buffer[51222]; + int len = 0; + + for(; (len = SSL_read(ssl, buffer, 511)) > 0;){ + str_pushl(a, buffer, len); + memset(buffer, 0, 512); + } + + lua_pushstring(L, a->c); + + return 1; +} + +int l_request(lua_State* L){ + const char* host = luaL_checkstring(L, 1); + int sock = get_host((char*)host, lua_tostring(L, 2)); + + char* path = "/"; + if(lua_gettop(L) >= 3) + path = (char*)luaL_checkstring(L, 3); + + //char* req = "GET / HTTP/1.1\nHost: amyy.cc\nConnection: Close\n\n"; + + char request[2000]; + sprintf(request, "GET %s HTTP/1.1\nHost: %s\nConnection: Close\n\n", path, host); + write(sock, request, strlen(request)); + + str* a = str_init(""); + char buffer[512]; + int len = 0; + + for(; (len = read(sock, buffer, 511)) != 0;){ + str_pushl(a, buffer, len); + memset(buffer, 0, 512); + } + + lua_pushstring(L, a->c); + + return 1; +} + #define max_uri_size 2048 _Atomic size_t threads = 0; + void* handle_client(void *_arg){ //printf("--\n"); //pthread_mutex_lock(&mutex); diff --git a/src/net.h b/src/net.h index d2b7435..e7d7d62 100644 --- a/src/net.h +++ b/src/net.h @@ -16,6 +16,10 @@ int l_listen(lua_State*); +int l_request(lua_State*); +int l_srequest(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); @@ -37,6 +41,8 @@ static char* http_codes[600] = {0}; static const luaL_Reg net_function_list [] = { {"listen",l_listen}, + {"request",l_request}, + {"srequest",l_srequest}, {NULL,NULL} }; diff --git a/tests/req.lua b/tests/req.lua new file mode 100644 index 0000000..59325e6 --- /dev/null +++ b/tests/req.lua @@ -0,0 +1,4 @@ +local awa = require "lullaby" + +print(awa.net.srequest("amyy.cc", 443, "/")) + -- cgit v1.2.3