From 64d87d4e5aa0e6142a71b45be34ec5c016168d9c Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Mon, 2 Aug 2021 14:46:43 -0700 Subject: [PATCH] Add GetRedbeanVersion & rename GetVersion (#219) - Add GetRedbeanVersion() to redbean (#97) - Rename GetVersion to GetHttpVersion() for consistency (#97) - Update Content-Length format to use a proper size modifier (#97, #218) --- tool/net/help.txt | 7 ++++++- tool/net/redbean.c | 15 +++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 55c91376a..cd3dd91d0 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -664,10 +664,15 @@ FUNCTIONS if redbean is being used as an HTTP proxy server. In the future this API might change to return an object instead. - GetVersion() → int + GetHttpVersion() → int Returns the request HTTP protocol version, which can be 9 for HTTP/0.9, 10 for HTTP/1.0, or 11 for HTTP/1.1. + GetRedbeanVersion() → int + Returns the Redbean version in the format 0xMMmmpp, with major (MM), + minor (mm), and patch (pp) versions encoded. The version value 1.4 + would be represented as 0x010400. + GetZipPaths() → array[str] Returns paths of all assets in the zip central directory, prefixed by a slash. diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 2605547d2..ee307d5d6 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -142,6 +142,7 @@ #define REDBEAN "redbean" #endif +#define VERSION 0x010400 #define HASH_LOAD_FACTOR /* 1. / */ 4 #define read(F, P, N) readv(F, &(struct iovec){P, N}, 1) #define write(F, P, N) writev(F, &(struct iovec){P, N}, 1) @@ -960,7 +961,7 @@ static void ProgramCache(long x) { } static void SetDefaults(void) { - ProgramBrand(REDBEAN "/1.4"); + ProgramBrand(gc(xasprintf("%s/%hhd.%hhd", REDBEAN, VERSION>>020, VERSION>>010))); __log_level = kLogInfo; maxpayloadsize = 64 * 1024; ProgramCache(-1); @@ -3727,7 +3728,7 @@ static int LuaFetch(lua_State *L) { if (bodylen > 0 || !(methodidx == kHttpGet || methodidx == kHttpHead || methodidx == kHttpTrace || methodidx == kHttpDelete || methodidx == kHttpConnect)) - conlenhdr = gc(xasprintf("Content-Length: %d\r\n", bodylen)); + conlenhdr = gc(xasprintf("Content-Length: %zu\r\n", bodylen)); /* * Parse URL. @@ -4017,11 +4018,16 @@ static int LuaGetDate(lua_State *L) { return 1; } -static int LuaGetVersion(lua_State *L) { +static int LuaGetHttpVersion(lua_State *L) { lua_pushinteger(L, msg.version); return 1; } +static int LuaGetRedbeanVersion(lua_State *L) { + lua_pushinteger(L, VERSION); + return 1; +} + static int LuaGetMethod(lua_State *L) { if (msg.method) { lua_pushstring(L, kHttpMethod[msg.method]); @@ -5198,7 +5204,8 @@ static const luaL_Reg kLuaFuncs[] = { {"GetServerAddr", LuaGetServerAddr}, // {"GetUrl", LuaGetUrl}, // {"GetUser", LuaGetUser}, // - {"GetVersion", LuaGetVersion}, // + {"GetHttpVersion", LuaGetHttpVersion}, // + {"GetRedbeanVersion", LuaGetRedbeanVersion}, // {"GetZipPaths", LuaGetZipPaths}, // {"HasControlCodes", LuaHasControlCodes}, // {"HasParam", LuaHasParam}, //