From 75eb52872d4c19eb7988e33f173f2763322ae8ff Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 11 Oct 2019 15:27:47 +0300 Subject: Echo added + http_params fix. --- examples/echo.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/makefile | 2 +- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 examples/echo.c (limited to 'examples') 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 +#include +#include +#include +#include + + +void print_preamble() +{ + puts( + "Content-type: application/xhtml+xml\r\n\r\n" + "" + "" + "Echo" + "" + ); +} + +void proceed_cookies(struct magi_cookie_list *cookies) +{ + puts("

Cookies:

"); + 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("]
"); + cookies = cookies->next; + } + puts("
"); +} + +void proceed_fields(struct magi_field_list *fields) +{ + puts("

Feilds:

"); + while (fields) { + puts("Feild ["); + puts(fields->item.name); + puts("] = ["); + puts(fields->item.data); + puts("]
"); + fields = fields->next; + } + puts("
"); +} + +void proceed_params(struct magi_param_list *params) +{ + puts("

HTTP Parameters:

"); + while (params) { + puts("Param ["); + puts(params->item.name); + puts("] is ["); + puts(params->item.data); + puts("]
"); + params = params->next; + } + puts("
"); +} + +void handle_request() +{ + struct magi_request request; + if (magi_request_build_cgi(&request, 0, 0)) { + puts("

Echo CGI Script

"); + 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("]
"); + proceed_cookies(request.cookies); + proceed_fields(request.fields); + proceed_params(request.http_params); + magi_request_destroy(&request); + } +} + +void print_footer() +{ + puts(""); +} + +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' -- cgit v1.2.3