aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-10-11 15:27:47 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-10-11 15:27:47 +0300
commit75eb52872d4c19eb7988e33f173f2763322ae8ff (patch)
tree02675927996b1c609291c471f5f8fcc1064795c8 /examples
parentb0a358a5f2ee27c052c163606a20135f37761884 (diff)
downloadmagi-75eb52872d4c19eb7988e33f173f2763322ae8ff.tar
magi-75eb52872d4c19eb7988e33f173f2763322ae8ff.tar.xz
magi-75eb52872d4c19eb7988e33f173f2763322ae8ff.zip
Echo added + http_params fix.
Diffstat (limited to 'examples')
-rw-r--r--examples/echo.c122
-rw-r--r--examples/makefile2
2 files changed, 123 insertions, 1 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'