diff options
| author | ame <[email protected]> | 2025-08-01 21:27:11 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2025-08-01 21:27:11 -0500 |
| commit | cd1c124e2def659dd2919e4387047de733afcc59 (patch) | |
| tree | 07d7d81c3be94e4b7cdf3284e7a78939c3d683d4 | |
| parent | 6b0cda77a3e04e4fb3024b21bb648b6bf9f62568 (diff) | |
make user-agent editable with default header values
| -rw-r--r-- | makefile | 3 | ||||
| -rw-r--r-- | src/lua.c | 12 | ||||
| -rw-r--r-- | src/lua.h | 2 | ||||
| -rw-r--r-- | src/net.c | 46 |
4 files changed, 35 insertions, 28 deletions
@@ -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)
@@ -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)
@@ -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)\
@@ -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));
|
