kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 517de467906dfb4ff412fa3a0dbda62aa84677a4
parent ef4289689f086a51f0beb89e115f155c43b0ba57
Author: Joris Vink <joris@coders.se>
Date:   Mon, 13 Jan 2014 20:21:20 +0100

Kore can now do query strings without lots of dynamic handler voodoo.

Any handler can receive query strings, however if you do not specify
parameters allowed in a param {} block Kore will discard them.

Diffstat:
includes/http.h | 1+
modules/example/module.conf | 9+++------
src/http.c | 11+++++++----
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/includes/http.h b/includes/http.h @@ -131,6 +131,7 @@ struct http_request { struct spdy_stream *stream; struct kore_buf *post_data; void *hdlr_extra; + char *query_string; u_int8_t *multipart_body; struct kore_module_handle *hdlr; diff --git a/modules/example/module.conf b/modules/example/module.conf @@ -110,17 +110,14 @@ domain localhost { static /lock-test serve_lock_test static /validator serve_validator - # - # We can use a dynamic handler to accept GET parameters. - # - dynamic ^/params-test(\??)[A-Z0-9a-z=&]*$ serve_params_test + static /params-test serve_params_test # Configure /params-test POST to only accept the following parameters. # They are automatically tested against the validator listed. # If the validator would fail Kore will automatically remove the # failing parameter, indicating something was wrong with it. # Any parameters not present in the params block are also filtered out. - params post ^/params-test(\??)[A-Z0-9a-z=&]*$ { + params post /params-test { validate test1 v_example validate test2 v_regex } @@ -128,7 +125,7 @@ domain localhost { # Configure a GET parameter that /params-test can received. As before # this is validated against the validator and removed if validation # fails. All extra parameters in the GET query are filtered out. - params get ^/params-test(\??)[A-Z0-9a-z=&]*$ { + params get /params-test { validate arg1 v_example validate id v_number } diff --git a/src/http.c b/src/http.c @@ -80,6 +80,7 @@ http_request_new(struct connection *c, struct spdy_stream *s, char *host, req->stream = s; req->post_data = NULL; req->hdlr_extra = NULL; + req->query_string = NULL; req->multipart_body = NULL; if ((p = strrchr(host, ':')) != NULL) @@ -88,6 +89,9 @@ http_request_new(struct connection *c, struct spdy_stream *s, char *host, kore_strlcpy(req->host, host, sizeof(req->host)); kore_strlcpy(req->path, path, sizeof(req->path)); + if ((req->query_string = strchr(req->path, '?')) != NULL) + *(req->query_string)++ = '\0'; + TAILQ_INIT(&(req->resp_headers)); TAILQ_INIT(&(req->req_headers)); TAILQ_INIT(&(req->arguments)); @@ -516,15 +520,14 @@ http_populate_arguments(struct http_request *req) { u_int32_t len; int i, v, c, count; - char *p, *query, *args[HTTP_MAX_QUERY_ARGS], *val[3]; + char *query, *args[HTTP_MAX_QUERY_ARGS], *val[3]; if (req->method == HTTP_METHOD_POST) { query = http_post_data_text(req); } else { - if ((p = strchr(req->path, '?')) == NULL) + if (req->query_string == NULL) return (0); - p++; - query = kore_strdup(p); + query = kore_strdup(req->query_string); } count = 0;