diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2019-10-11 15:27:47 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2019-10-11 15:27:47 +0300 |
commit | 75eb52872d4c19eb7988e33f173f2763322ae8ff (patch) | |
tree | 02675927996b1c609291c471f5f8fcc1064795c8 | |
parent | b0a358a5f2ee27c052c163606a20135f37761884 (diff) | |
download | magi-75eb52872d4c19eb7988e33f173f2763322ae8ff.tar magi-75eb52872d4c19eb7988e33f173f2763322ae8ff.tar.xz magi-75eb52872d4c19eb7988e33f173f2763322ae8ff.zip |
Echo added + http_params fix.
-rw-r--r-- | examples/echo.c | 122 | ||||
-rw-r--r-- | examples/makefile | 2 | ||||
-rw-r--r-- | src/request.c | 4 |
3 files changed, 125 insertions, 3 deletions
diff --git a/examples/echo.c b/examples/echo.c new file mode 100644 index 0000000..33094a4 --- /dev/null +++ b/examples/echo.c @@ -0,0 +1,122 @@ +#include <stdio.h> +#include <stdlib.h> +#include <cookie.h> +#include <param.h> +#include <request.h> + + +void print_preamble() +{ + puts( + "Content-type: application/xhtml+xml\r\n\r\n" + "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' " + "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>" + "<html xmlns='http://www.w3.org/1999/xhtml'>" + "<head><title>Echo</title></head>" + "<body>" + ); +} + +void proceed_cookies(struct magi_cookie_list *cookies) +{ + puts("<h2>Cookies:</h2>"); + while (cookies) { + puts("Cookie with name ["); + puts(cookies->item.name); + if (cookies->item.data) { + puts("] is ["); + puts(cookies->item.data); + } + if (cookies->item.domain) { + puts("] for domain ["); + puts(cookies->item.domain); + } + if (cookies->item.port) { + puts("] for port ["); + puts(cookies->item.port); + } + if (cookies->item.path) { + puts("] for path ["); + puts(cookies->item.path); + } + puts("]<br/>"); + cookies = cookies->next; + } + puts("<hr/>"); +} + +void proceed_fields(struct magi_field_list *fields) +{ + puts("<h2>Feilds:</h2>"); + while (fields) { + puts("Feild ["); + puts(fields->item.name); + puts("] = ["); + puts(fields->item.data); + puts("]<br/>"); + fields = fields->next; + } + puts("<hr/>"); +} + +void proceed_params(struct magi_param_list *params) +{ + puts("<h2>HTTP Parameters:</h2>"); + while (params) { + puts("Param ["); + puts(params->item.name); + puts("] is ["); + puts(params->item.data); + puts("]<br/>"); + params = params->next; + } + puts("<hr/>"); +} + +void handle_request() +{ + struct magi_request request; + if (magi_request_build_cgi(&request, 0, 0)) { + puts("<h1>Echo CGI Script</h1>"); + puts("I was called with method ["); + puts(request.method); + if (request.uri) { + puts("] with URL ["); + puts(request.uri); + } + if (request.server_name) { + puts("] for server ["); + puts(request.server_name); + } + if (request.server_port) { + puts("] on port ["); + puts(request.server_port); + } + if (request.server_protocol) { + puts("] with protocol ["); + puts(request.server_protocol); + } + if (request.server_software) { + puts("] and I am running on software ["); + puts(request.server_software); + } + puts("]<br/>"); + proceed_cookies(request.cookies); + proceed_fields(request.fields); + proceed_params(request.http_params); + magi_request_destroy(&request); + } +} + +void print_footer() +{ + puts("</body></html>"); +} + +int main(int argc, char const *argv[]) +{ + print_preamble(); + handle_request(); + print_footer(); + return 0; +} diff --git a/examples/makefile b/examples/makefile index de6b310..e3205aa 100644 --- a/examples/makefile +++ b/examples/makefile @@ -2,7 +2,7 @@ # DEBUG = yes CC = gcc -EXAMPLES = append cookie upload +EXAMPLES = append cookie upload echo CFLAGS = -xc -ansi -pedantic -Wall ifeq '$(DEBUG)' 'yes' diff --git a/src/request.c b/src/request.c index 8da8975..aa2cd9b 100644 --- a/src/request.c +++ b/src/request.c @@ -117,9 +117,9 @@ static int cgi_http_env(struct magi_request *r) struct magi_param param; /* At least one '=' must be in *env, according to format. */ char *name_end = strchr(*env, '='); - param.name = str_alloc(name_end - *env); + param.name = str_alloc(name_end - *env - 5); if (param.name) { - memcpy(param.name, *env, name_end - *env); + memcpy(param.name, *env + 5, name_end - *env - 5); param.data = str_alloc(strlen(name_end + 1)); if (param.data) { strcpy(param.data, name_end + 1); |