diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/append.c | 52 | ||||
-rw-r--r-- | examples/cookie.c | 40 | ||||
-rw-r--r-- | examples/echo.c | 96 | ||||
-rw-r--r-- | examples/fastcgi.c | 33 | ||||
-rw-r--r-- | examples/upload.c | 62 |
5 files changed, 136 insertions, 147 deletions
diff --git a/examples/append.c b/examples/append.c index 8417796..fe85492 100644 --- a/examples/append.c +++ b/examples/append.c @@ -1,40 +1,46 @@ +/* This is the very basic example, showing work with form fields to append + * specified by user in form addon into some file (i.e. "file_to_append"). + */ #include <magi.h> #include <stdio.h> -void response(magi_request *r) +void append(magi_request *r) { char *data = magi_request_param(r, "addon"); - magi_response(r, - "<!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>Append to File</title></head>" - "<body>"); - if (data) { + if (data) { /* If field "addon" was in request: */ FILE *file = fopen("file_to_append", "a"); - fputs(data, file); + fputs(data, file); /* Append addon into the file. */ fclose(file); - magi_response(r, "<p>Appended!</p>"); + printf("<p>Appended!</p>"); /* And display success message. */ } - magi_response(r, - "<form action='/cgi-bin/append' method='post'><fieldset>" - "<input type='text' name='addon' value='Whatever you want to add.' />" - "<input type='submit' value='Append' />" - "</fieldset></form>" - "</body>" - "</html>"); +} + +void response(magi_request *r) +{ + magi_response_default(); /* Pass default headers and send body: */ + printf("<!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>Append to File</title></head>" + "<body>"); + append(r); + printf("<form action='/cgi-bin/append' method='post'><fieldset>" + "<input type='text' name='addon' value='Whatever you want to add.' />" + "<input type='submit' value='Append' />" + "</fieldset></form>" + "</body></html>"); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/cookie.c b/examples/cookie.c index e18c100..c64ca0c 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -1,38 +1,46 @@ +/* Basic example of cookie handling through response headers. + * It lists all recieved cookies and set one via magi_response, + * showing work with headers in magi. + */ #include <magi.h> void list_cookies(magi_request *r) { magi_cookies *current; - magi_response(r, "Cookies:"); + printf("Cookies:"); + /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { magi_cookie *c = ¤t->item; - magi_response_format(r, "<br />[%s]=[%s]", c->name, c->data); + printf("<br />[%s]=[%s]", c->name, c->data); } } void response(magi_request *r) { - magi_response_cookie(r, "cookie", "monster"); - magi_response(r, - "<!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>Cookie Listing and Setting</title></head>" - "<body><p>"); + magi_response head; + magi_response_init(&head); /* Setting defaults. */ + /* Set cookie "cookie" with value "monster" on clientside: */ + magi_response_cookie(&head, "cookie", "monster"); + magi_response_free(&head); /* Send headers and start sending body: */ + printf("<!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>Cookie Listing and Setting</title></head>" + "<body><p>"); list_cookies(r); - magi_response(r, "</p></body></html>"); + printf("</p></body></html>"); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/echo.c b/examples/echo.c index 7cb4a3b..4f6975a 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -1,111 +1,111 @@ +/* This is useful example echoing request data in response. + */ #include <magi.h> void list_cookies(magi_request *r) { magi_cookies *current = r->cookies; - magi_response(r, "<p>"); + printf("<p>"); + /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { magi_cookie *c = ¤t->item; - magi_response(r, "Cookie with name ["); - magi_response(r, c->name); + printf("Cookie with name [%s", c->name); if (c->data) { - magi_response(r, "] is ["); - magi_response(r, c->data); + printf("] is [%s", c->data); } if (c->domain) { - magi_response(r, "] for domain ["); - magi_response(r, c->domain); + printf("] for domain [%s", c->domain); } if (c->path) { - magi_response(r, "] for path ["); - magi_response(r, c->path); + printf("] for path [%s", c->path); } - magi_response(r, "]<br />"); + printf("]<br />"); } - magi_response(r, "</p>"); + printf("</p>"); } -void list_params(magi_request *r, magi_params *current) +void list_params(magi_params *current) { - magi_response(r, "<p>"); + printf("<p>"); + /* Iterate through specified params to show them in body: */ for (; current; current = current->next) { magi_param *p = ¤t->item; - magi_response_format(r, "[%s] is [%s]<br />", p->name, p->data); + printf("[%s] is [%s]<br />", p->name, p->data); } - magi_response(r, "</p>"); + printf("</p>"); } void list_files(magi_request *r) { magi_files *current; - magi_response(r, "<p>"); + printf("<p>"); + /* Iterate through all field files in request to show them in body: */ for (current = r->files; current; current = current->next) { magi_file *f = ¤t->item; - magi_response_format(r, "[%s] was [%s] on clientside<br />", - f->field, f->filename); + printf("[%s] was [%s] on clientside<br />", f->field, f->filename); } - magi_response(r, "</p>"); + printf("</p>"); } void show_meta(magi_request *r) { - magi_response(r, "<p>I was called "); + printf("<p>I was called "); if (r->is_secure) { - magi_response(r, "securely "); + printf("securely "); } - magi_response_format(r, "with method [%s", r->method); + printf("with method [%s", r->method); if (r->host) { - magi_response_format(r, "] on server [%s", r->host); + printf("] on server [%s", r->host); } if (r->script) { - magi_response_format(r, "] being script on [%s", r->script); + printf("] being script on [%s", r->script); } if (r->path) { - magi_response_format(r, "] with requested path [%s", r->path); + printf("] with requested path [%s", r->path); } - magi_response(r, "]</p>"); + printf("]</p>"); } void response(magi_request *r) { - magi_response(r, - "<!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>"); + magi_response_default(); /* Pass default headers and send body: */ + printf("<!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>"); - magi_response(r, "<h1>Echo CGI Script</h1>"); + printf("<h1>Echo CGI Script</h1>"); show_meta(r); - magi_response(r, "<h2>Cookies:</h2>"); + printf("<h2>Cookies:</h2>"); list_cookies(r); - magi_response(r, "<h2>Parameters:</h2>"); - list_params(r, r->meta); + printf("<h2>Parameters:</h2>"); + list_params(r->meta); - magi_response(r, "<h2>URL Parameters:</h2>"); - list_params(r, r->head); + printf("<h2>URL Parameters:</h2>"); + list_params(r->head); - magi_response(r, "<h2>Body Parameters:</h2>"); - list_params(r, r->body); + printf("<h2>Body Parameters:</h2>"); + list_params(r->body); - magi_response(r, "<h2>Files:</h2>"); + printf("<h2>Files:</h2>"); list_files(r); - magi_response(r, "</body></html>"); + printf("</body></html>"); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/fastcgi.c b/examples/fastcgi.c deleted file mode 100644 index 16668f3..0000000 --- a/examples/fastcgi.c +++ /dev/null @@ -1,33 +0,0 @@ -#include <magi.h> - - -void response(magi_request *r) -{ - magi_response(r, - "<!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>Fast CGI</title></head>" - "<body>Hi!</body>" - "</html>"); -} - -int main() -{ - magi_session session; - magi_request request; - magi_request_init(&request); - magi_session_init(&session); - magi_session_unix(&session, "unix.sock"); - while (magi_fastcgi(&session, &request)) { - if (request.error) { - magi_response_error(&request); - } else { - response(&request); - } - magi_request_free(&request); - } - magi_request_free(&request); - magi_session_free(&session); - return 0; -} diff --git a/examples/upload.c b/examples/upload.c index bf8eed1..d07db16 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -1,51 +1,59 @@ +/* This is basic example of handling file uploading. + * It is using magi_loadfiles callback to load file temporarily and change its + * name into the one specified by user. + */ #include <magi.h> #include <stdio.h> -void response(magi_request *r) +void upload(magi_request *r) { char *name = magi_request_param(r, "name"); const magi_file *data = magi_request_file(r, "data"); - magi_response(r, - "<!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>Upload File</title></head>" - "<body>"); - if (name && data) { - rename("data", name); - magi_response(r, "<p>Uploaded!</p>"); + if (name && data) { /* If file to load and its name are in the request: */ + rename("data", name); /* Rename loaded file to designated name. */ + printf("<p>Uploaded!</p>"); /* And display success message. */ } - magi_response(r, - "<form action='/cgi-bin/upload' method='post' " - "enctype='multipart/form-data'><fieldset>" - "<input type='text' name='name' value='filename' />" - "<input type='file' name='data' />" - "<input type='submit' value='Upload' />" - "</fieldset></form>" - "</body>" - "</html>"); +} + +void response(magi_request *r) +{ + magi_response_default(); /* Pass default headers and send body: */ + printf("<!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>Upload File</title></head>" + "<body>"); + upload(r); + printf("<form action='/cgi-bin/upload' method='post' " + "enctype='multipart/form-data'><fieldset>" + "<input type='text' name='name' value='filename' />" + "<input type='file' name='data' />" + "<input type='submit' value='Upload' />" + "</fieldset></form>" + "</body></html>"); } void get(magi_request *r) { magi_loadfiles rules = { 0, 0 }; + /* Setup callback to load file from "data" field into file "data": */ magi_loadfiles_add(&rules, "data", "data", 0); - magi_loadfiles_set(r, &rules); - magi_cgi(r); - magi_loadfiles_free(&rules); + magi_loadfiles_set(r, &rules); /* Setup request to use the callback. */ + magi_parse(r); + magi_loadfiles_free(&rules); /* Free data of callback. */ } int main() { magi_request request; magi_request_init(&request); - get(&request); - if (request.error) { - magi_response_error(&request); + get(&request); /* Parse request. */ + if (request.error) { /* If error occurred display error message: */ + magi_error_response(request.error); } else { - response(&request); + response(&request); /* Overwise response request. */ } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } |