Add comments

This commit is contained in:
2025-09-22 16:43:08 +02:00
parent 6487384e4e
commit 45a9a4e9a1
9 changed files with 201 additions and 182 deletions
+22 -15
View File
@@ -836,7 +836,11 @@ bool cweb_match_endpoint(CWEB_Request *req, CWEB_String str)
CWEB_String cweb_get_param_s(CWEB_Request *req, CWEB_String name)
{
HTTP_String res = http_get_param(req->req->body,
HTTP_String src = req->req->url.query;
if (req->req->method == HTTP_METHOD_POST)
src = req->req->body;
HTTP_String res = http_get_param(src,
(HTTP_String) { name.ptr, name.len },
req->arena.ptr + req->arena.cur,
req->arena.len - req->arena.cur
@@ -848,7 +852,11 @@ CWEB_String cweb_get_param_s(CWEB_Request *req, CWEB_String name)
int cweb_get_param_i(CWEB_Request *req, CWEB_String name)
{
return http_get_param_i(req->req->body, (HTTP_String) { name.ptr, name.len });
HTTP_String src = req->req->url.query;
if (req->req->method == HTTP_METHOD_POST)
src = req->req->body;
return http_get_param_i(src, (HTTP_String) { name.ptr, name.len });
}
static int set_auth_cookie_if_necessary(CWEB_Request *req)
@@ -997,31 +1005,23 @@ static void evaluate_format(StaticOutputBuffer *out, CWEB_String format, CWEB_VA
}
}
static CWEB_String evaluate_format_for_request_impl(CWEB_Request *req, CWEB_String format, CWEB_VArgs args)
CWEB_String cweb_format_impl(CWEB_Request *req, char *fmt, CWEB_VArgs args)
{
StaticOutputBuffer out = {
.dst = req->arena.ptr + req->arena.cur,
.cap = req->arena.len - req->arena.cur,
.len = 0
};
evaluate_format(&out, format, args);
evaluate_format(&out, (CWEB_String) { fmt, strlen(fmt) }, args);
if (out.len > req->arena.len - req->arena.cur)
return (CWEB_String) { NULL, 0 };
req->arena.cur += out.len;
return (CWEB_String) { out.dst, out.len };
}
#define evaluate_format_for_request(req, format, ...) evaluate_format_for_request_impl((req), (format), CWEB_VARGS(__VA_ARGS__))
void cweb_respond_redirect_impl(CWEB_Request *req, CWEB_String target_format, CWEB_VArgs args)
void cweb_respond_redirect(CWEB_Request *req, CWEB_String target)
{
CWEB_String target = evaluate_format_for_request_impl(req, target_format, args);
if (target.len == 0) {
http_response_builder_status(req->builder, 500);
http_response_builder_done(req->builder);
return;
}
CWEB_String location_header = evaluate_format_for_request(req, CWEB_STR("Location: {}"), target);
CWEB_String location_header = cweb_format(req, "Location: {}", target);
if (location_header.len == 0) {
http_response_builder_status(req->builder, 500);
http_response_builder_done(req->builder);
@@ -1342,7 +1342,7 @@ static int query_routine(WL_Runtime *rt, SQLiteCache *dbcache)
return 0;
}
static void push_sysvar(WL_Runtime *rt, WL_String name, SQLiteCache *dbcache, HTTP_String csrf, int user_id, int resource_id)
static void push_sysvar(WL_Runtime *rt, WL_String name, SQLiteCache *dbcache, CWEB_String csrf, int user_id, int resource_id)
{
(void) dbcache;
@@ -1410,6 +1410,13 @@ static int get_or_create_program(TemplateCache *cache, WL_String path, WL_Arena
void cweb_respond_template(CWEB_Request *req, int status, CWEB_String template_file, int resource_id)
{
http_response_builder_status(req->builder, status);
int ret = set_auth_cookie_if_necessary(req);
if (ret < 0) {
http_response_builder_undo(req->builder);
http_response_builder_status(req->builder, -ret);
http_response_builder_done(req->builder);
return;
}
WL_Program program;
int ret = get_or_create_program(req->cweb->tpcache, template_file, &req->arena, &program);
+54 -27
View File
@@ -221,49 +221,76 @@ void cweb_free(CWEB *cweb);
int cweb_enable_database(CWEB *cweb, CWEB_String file);
// Pause execution until a request is available.
// TODO: When does this function return NULL?
CWEB_Request *cweb_wait(CWEB *cweb);
//////////////////////////////////////
// Session
// Returns true iff the request matches the specified endpoint
bool cweb_match_endpoint(CWEB_Request *req, CWEB_String str);
// Returns the CSRF token associated to the current session
CWEB_String cweb_get_session_csrf(CWEB_Request *req);
int cweb_get_session_user_id(CWEB_Request *req);
int cweb_set_session_user_id(CWEB_Request *req, int user_id);
//////////////////////////////////////
// Request
// Returns the user ID for the current session, or -1 if there is no session
int cweb_get_session_user_id(CWEB_Request *req);
bool cweb_match_endpoint(CWEB_Request *req, CWEB_String str);
// Sets the user ID for the current session (it must be a positive integer).
// If the ID is -1, the session is deleted.
int cweb_set_session_user_id(CWEB_Request *req, int user_id);
// Returns the request parameter with the specified name
// If the request uses POST, the parameter is taken from the body,
// else it's taken from the URL. If the parameter is not present,
// an empty string is returned.
CWEB_String cweb_get_param_s(CWEB_Request *req, CWEB_String name);
int cweb_get_param_i(CWEB_Request *req, CWEB_String name);
//////////////////////////////////////
// Response
// Like cweb_get_param_s, but also parser the argument as an integer.
// If parsing fails or the parameter is missing, -1 is returned.
int cweb_get_param_i(CWEB_Request *req, CWEB_String name);
// Create a string by evaluating a format. Memory is allocated from the arena of the request.
// If the arena is full, an empty string is returned.
CWEB_String cweb_format_impl(CWEB_Request *req, char *fmt, CWEB_VArgs args);
// Helper
#define cweb_format(req, fmt, ...) cweb_format_impl((req), (fmt), CWEB_VARGS(__VA_ARGS__))
// Responds to the specified request with the given status code and content
void cweb_respond_basic(CWEB_Request *req, int status, CWEB_String content);
void cweb_respond_redirect_impl(CWEB_Request *req, CWEB_String target_format, CWEB_VArgs args);
// Responds to the request by redirecting the client to the given target
void cweb_respond_redirect(CWEB_Request *req, CWEB_String target);
// Responds to the request by evaluating a WL template file
void cweb_respond_template(CWEB_Request *req, int status, CWEB_String template_file, int resource_id);
#define cweb_respond_redirect(req, format, ...) cweb_respond_redirect_impl((req), (format), CWEB_VARGS(__VA_ARGS__))
//////////////////////////////////////
// Database
typedef struct {
void *handle;
} CWEB_QueryResult;
int64_t cweb_database_insert_impl(CWEB *cweb, const char *fmt, CWEB_VArgs args);
CWEB_QueryResult cweb_database_select_impl(CWEB *cweb, const char *fmt, CWEB_VArgs args);
int cweb_next_query_row_impl(CWEB_QueryResult *res, CWEB_VArgs args);
void cweb_free_query_result(CWEB_QueryResult *res);
// Evaluates an SQL INSERT statement and returns the ID of the last inserted row. On error -1 is returned
int64_t cweb_database_insert_impl(CWEB *cweb, const char *fmt, CWEB_VArgs args);
// Helper
#define cweb_database_insert(cweb, fmt, ...) cweb_database_insert_impl((cweb), (fmt), CWEB_VARGS(__VA_ARGS__))
// Iterator over database rows
typedef struct { void *handle; } CWEB_QueryResult;
// Evaluates an SQL SELECT statement, returning a scanner over the returned rows.
// You don't have to check for errors with this function
CWEB_QueryResult cweb_database_select_impl(CWEB *cweb, const char *fmt, CWEB_VArgs args);
// Helper
#define cweb_database_select(cweb, fmt, ...) cweb_database_select_impl((cweb), (fmt), CWEB_VARGS(__VA_ARGS__))
#define cweb_next_query_row(res, ...) cweb_next_query_row_impl((res), CWEB_VARGS(__VA_ARGS__))
//////////////////////////////////////
// Password
// Returns the next row from the query result iterator.
int cweb_next_query_row_impl(CWEB_QueryResult *res, CWEB_VArgs args);
// Helper
#define cweb_next_query_row(res, ...) cweb_next_query_row_impl((res), CWEB_VARGS(__VA_ARGS__))
// Frees the result of a database query
void cweb_free_query_result(CWEB_QueryResult *res);
// Calculates the bcrypt hash of the specified password
int cweb_hash_password(char *pass, int passlen, int cost, CWEB_PasswordHash *hash);
// Checks whether the password matches the given hash
int cweb_check_password(char *pass, int passlen, CWEB_PasswordHash hash);