Skip to content

Commit d42a855

Browse files
committed
Add some auxiliary Lua methods
1 parent 5727010 commit d42a855

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/lib/lwan-lua.c

+38
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ ALWAYS_INLINE struct lwan_request *lwan_lua_get_request_from_userdata(lua_State
4444
return *r;
4545
}
4646

47+
LWAN_LUA_METHOD(http_version)
48+
{
49+
if (request->flags & REQUEST_IS_HTTP_1_0)
50+
lua_pushstring(L, "HTTP/1.0");
51+
else
52+
lua_pushstring(L, "HTTP/1.1");
53+
return 1;
54+
}
55+
56+
LWAN_LUA_METHOD(http_method)
57+
{
58+
lua_pushstring(L, lwan_request_get_method_str(request));
59+
return 1;
60+
}
61+
62+
LWAN_LUA_METHOD(http_headers)
63+
{
64+
const struct lwan_request_parser_helper *helper = request->helper;
65+
66+
lua_newtable(L);
67+
68+
for (size_t i = 0; i < helper->n_header_start; i++) {
69+
const char *key = helper->header_start[i];
70+
const char *key_end = strchr(key, ':');
71+
72+
if (!key_end)
73+
break;
74+
75+
const char *value = key_end + 2;
76+
77+
lua_pushlstring(L, key, (size_t)(key_end - key));
78+
lua_pushstring(L, value);
79+
lua_rawset(L, -3);
80+
}
81+
82+
return 1;
83+
}
84+
4785
LWAN_LUA_METHOD(say)
4886
{
4987
size_t response_str_len;

0 commit comments

Comments
 (0)