From cd1c124e2def659dd2919e4387047de733afcc59 Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 1 Aug 2025 21:27:11 -0500 Subject: make user-agent editable with default header values --- makefile | 3 ++- src/lua.c | 12 +++++++----- src/lua.h | 2 ++ src/net.c | 46 ++++++++++++++++++++++++---------------------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/makefile b/makefile index 3d10a62..2e82331 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,6 @@ CC := clang +MAJOR_VERSION := "$(shell git -c safe.directory='*' describe --tags --abbrev=0)" GIT_COMMIT := "$(shell git -c safe.directory='*' describe --tags)-$(shell git -c safe.directory='*' describe --always --match 'NOT A TAG')" version ?= 5.4 @@ -9,7 +10,7 @@ ifeq ($(version),jit) install_version = 5.1 endif -CFLAGS := -D_GNU_SOURCE -Wall -fPIC -DGIT_COMMIT='$(GIT_COMMIT)' `pkg-config --cflags lua$(version)` +CFLAGS := -D_GNU_SOURCE -Wall -fPIC -DGIT_COMMIT='$(GIT_COMMIT)' -DMAJOR_VERSION='$(MAJOR_VERSION)' `pkg-config --cflags lua$(version)` LFLAGS := -lm -shared -lcrypto -lssl LINKER := $(CC) diff --git a/src/lua.c b/src/lua.c index ef3c032..e138f77 100644 --- a/src/lua.c +++ b/src/lua.c @@ -374,19 +374,21 @@ int env_table(lua_State* L, int provide_table){ return 1; } -//top table is prioritized -void luaI_jointable(lua_State* L){ - int idx = lua_gettop(L) - 1; +//main is the default values, merge is the new and overridden ones +void luaI_jointable(lua_State* L, int main, int merge){ + int idx = lua_gettop(L); + + lua_pushvalue(L, merge); lua_pushnil(L); for(;lua_next(L, -2) != 0;){ lua_pushvalue(L, -2); lua_pushvalue(L, -2); - lua_settable(L, idx); + lua_settable(L, main); lua_pop(L, 1); } - lua_pushvalue(L, idx); + lua_settop(L, idx); } //copys all variables from state A to B, including locals (stored in _locals) diff --git a/src/lua.h b/src/lua.h index 2e7aa5e..db58862 100644 --- a/src/lua.h +++ b/src/lua.h @@ -38,6 +38,8 @@ typedef int (*stream_free_function)(void**); void luaI_newstream(lua_State* L, stream_read_function, stream_free_function, void*); int luaI_nothing(lua_State*); +void luaI_jointable(lua_State* L, int main, int merge); + //generic macro that takes other macros (see below) #define _tset_b(L, Tidx, K, V, F)\ diff --git a/src/net.c b/src/net.c index b5b75bf..34e20c0 100644 --- a/src/net.c +++ b/src/net.c @@ -484,7 +484,6 @@ int _srequest_chunked_encoding(char* input, int length, struct chunked_encoding_ int l_srequest(lua_State* L){ int params = lua_gettop(L); - int check = 1; uint64_t ilen = 0; char* request_url = (char*)lua_tolstring(L, 1, &ilen); struct url awa = parse_url(request_url, ilen); @@ -510,12 +509,11 @@ int l_srequest(lua_State* L){ char* cont = ""; size_t cont_len = 0; - if(params >= check + 1){ - check++; - switch(lua_type(L, check)){ + if(params >= 2){ + switch(lua_type(L, 2)){ case LUA_TNUMBER: case LUA_TSTRING: - cont = (char*)luaL_tolstring(L, check, &cont_len); + cont = (char*)luaL_tolstring(L, 2, &cont_len); break; default: p_fatal("cant send type"); @@ -523,31 +521,35 @@ int l_srequest(lua_State* L){ } } - str* header = str_init(""); - if(params >= check + 1){ - check++; - lua_pushnil(L); - - for(;lua_next(L, check) != 0;){ - str_push(header, "\r\n"); - str_push(header, lua_tostring(L, -2)); - str_push(header, ": "); - str_push(header, lua_tostring(L, -1)); - lua_pop(L, 1); - } + lua_newtable(L); + int header_idx = lua_gettop(L); + luaI_tsets(L, header_idx, "User-Agent", "lullaby/"MAJOR_VERSION); + + if(params >= 3){ + luaI_jointable(L, header_idx, 3); } + + str* header = str_init(""); + lua_pushnil(L); + + for(;lua_next(L, header_idx) != 0;){ + str_push(header, "\r\n"); + str_push(header, lua_tostring(L, -2)); + str_push(header, ": "); + str_push(header, lua_tostring(L, -1)); + lua_pop(L, 1); + } char* action = "GET"; - if(params >= check + 1){ - check++; - action = (char*)lua_tostring(L, check); + if(params >= 4){ + action = (char*)lua_tostring(L, 4); } //char* req = "GET / HTTP/1.1\nHost: amyy.cc\nConnection: Close\n\n"; char* request = calloc(cont_len + header->len + strlen(host) + strlen(path) + 512, sizeof * request); - sprintf(request, "%s %s HTTP/1.1\r\nUser-Agent: lullaby/"MAJOR_VERSION"\r\nHost: %s\r\nConnection: Close%s\r\n\r\n%s", action, path, host, header->c, cont); - //printf("%s\n", request); + sprintf(request, "%s %s HTTP/1.1\r\nHost: %s\r\nConnection: Close%s\r\n\r\n%s", action, path, host, header->c, cont); + str_free(header); int s = SSL_write(ssl, request, strlen(request)); -- cgit v1.2.3