Skip to content

Commit bf933d0

Browse files
committed
Allow generic Lua handles (independent of method and path!)
1 parent d42a855 commit bf933d0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ single script, embedded in the configuration file or otherwise, can service
609609
many different endpoints. Scripts are supposed to implement functions with
610610
the following signature: `handle_${METHOD}_${ENDPOINT}(req)`, where
611611
`${METHOD}` can be a HTTP method (i.e. `get`, `post`, `head`, etc.), and
612-
`${ENDPOINT}` is the desired endpoint to be handled by that function.
612+
`${ENDPOINT}` is the desired endpoint to be handled by that function. A generic
613+
`handle(req)` function will be called if the specific version doesn't exist.
613614

614615
> [!TIP]
615616
>
@@ -643,6 +644,9 @@ information from the request, or to set the response, as seen below:
643644
- `req:request_date()` returns the date as it'll be written in the `Date` response header.
644645
- `req:is_https()` returns `true` if this request is serviced through HTTPS, `false` otherwise.
645646
- `req:host()` returns the value of the `Host` header if present, otherwise `nil`.
647+
- `req:http_version()` returns `HTTP/1.0` or `HTTP/1.1` depending on the request version.
648+
- `req:http_method()` returns a string, in uppercase, with the HTTP method (e.g. `"GET"`).
649+
- `req:http_headers()` returns a table with all headers and their values.
646650

647651
Handler functions may return either `nil` (in which case, a `200 OK` response
648652
is generated), or a number matching an HTTP status code. Attempting to return

src/lib/lwan-mod-lua.c

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ static bool get_handler_function(lua_State *L, struct lwan_request *request)
158158
memcpy(method_name, url, url_len + 1);
159159

160160
lua_getglobal(L, handler_name);
161+
if (lua_isfunction(L, -1))
162+
return true;
163+
164+
lua_pop(L, 1);
165+
lua_getglobal(L, "handle");
161166
return lua_isfunction(L, -1);
162167
}
163168

0 commit comments

Comments
 (0)