diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/append.c | 64 | ||||
-rw-r--r-- | examples/cookie.c | 12 | ||||
-rw-r--r-- | examples/echo.c | 29 | ||||
-rw-r--r-- | examples/fcgi.c | 21 | ||||
-rw-r--r-- | examples/upload.c | 65 |
5 files changed, 91 insertions, 100 deletions
diff --git a/examples/append.c b/examples/append.c index 0c7df7e..e51745a 100644 --- a/examples/append.c +++ b/examples/append.c @@ -4,46 +4,42 @@ #include <stdlib.h> -void handle_request() +void response_request(struct magi_request * req, struct magi_response * res) +{ + magi_response_content_type(res, magi_xhtml); + magi_response_content( + res, + "<!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>" + "<form action='/cgi-bin/append' method='get'><fieldset>" + "<input type='text' name='addon' value='Whatever you want to add.'/>" + "<input type='submit' value='Append'/>" + "</fieldset></form>" + "</body>" + "</html>"); + + struct magi_field * a = magi_field_list_get(req->fields, "addon"); + if (a && a->data) { + FILE * file = fopen("file_to_append", "a"); + fputs(a->data, file); + fclose(file); + } +} + +int main(int argc, char const * argv[]) { struct magi_request request; if (magi_cgi(&request, 0, 0)) { - struct magi_field * a = magi_field_list_get(request.fields, "addon"); - if (a && a->data) { - FILE * file = fopen("file_to_append", "a"); - fputs(a->data, file); - fclose(file); - } + struct magi_response response; + response_request(&request, &response); + magi_cgi_response(&response); + magi_response_destroy(); } else { magi_cgi_error(request.error); } magi_request_destroy(&request); -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts("<!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>" - "<form action='/cgi-bin/append' method='get'><fieldset>" - "<input type='text' name='addon' value='Whatever you want to add.'/>" - "<input type='submit' value='Append'/>" - "</fieldset></form>" - "</body>" - "</html>"); -} - -int main(int argc, char const * argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); return 0; } diff --git a/examples/cookie.c b/examples/cookie.c index a2b9425..47929cb 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -11,12 +11,12 @@ void response_request(struct magi_request * req, struct magi_response * res) struct magi_cookie_list * cookie; magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "<!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>"); + magi_response_content( + res, "<!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>"); for (cookie = req->cookies; cookie; cookie = cookie->next) { printf("[%s] = [%s]<br/>", cookie->item.name, cookie->item.data); diff --git a/examples/echo.c b/examples/echo.c index 853e6ce..6901b71 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -6,8 +6,8 @@ #include <stdlib.h> -void proceed_cookies( - struct magi_cookie_list * cookies, struct magi_response * response) +void proceed_cookies(struct magi_cookie_list * cookies, + struct magi_response * response) { magi_response_content(response, "<h2>Cookies:</h2>"); while (cookies) { @@ -35,8 +35,8 @@ void proceed_cookies( magi_response_content(response, "<hr/>"); } -void proceed_fields( - struct magi_field_list * fields, struct magi_response * response) +void proceed_fields(struct magi_field_list * fields, + struct magi_response * response) { magi_response_content(response, "<h2>Feilds:</h2>"); while (fields) { @@ -50,8 +50,8 @@ void proceed_fields( magi_response_content(response, "<hr/>"); } -void proceed_params( - struct magi_param_list * params, struct magi_response * response) +void proceed_params(struct magi_param_list * params, + struct magi_response * response) { magi_response_content(response, "<h2>HTTP Parameters:</h2>"); while (params) { @@ -67,9 +67,8 @@ void proceed_params( void process_meta(struct magi_request * req, struct magi_response * res) { - magi_response_content(res, - "<h1>Echo CGI Script</h1>" - "I was called with method ["); + magi_response_content(res, "<h1>Echo CGI Script</h1>" + "I was called with method ["); magi_response_content(res, req->method); if (req->uri) { magi_response_content(res, "] with URL ["); @@ -97,12 +96,12 @@ void process_meta(struct magi_request * req, struct magi_response * res) void response_request(struct magi_request * req, struct magi_response * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "<!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_content( + res, "<!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>"); process_meta(req, res); proceed_cookies(req->cookies, res); proceed_fields(req->fields, res); diff --git a/examples/fcgi.c b/examples/fcgi.c index 5972cdd..9cdbe90 100644 --- a/examples/fcgi.c +++ b/examples/fcgi.c @@ -1,3 +1,4 @@ +#include <error.h> #include <fastcgi.h> #include <request.h> #include <stdio.h> @@ -8,13 +9,13 @@ void response_request(struct magi_request * req, struct magi_resopnse * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "<!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>"); + magi_response_content( + res, "<!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(int argc, char const * argv[]) @@ -22,7 +23,7 @@ int main(int argc, char const * argv[]) struct magi_session session; if (magi_fcgi(&session)) { struct magi_request request; - while (magi_accept(&request, &session)) { + while (magi_fcgi_accept(&request, &session)) { if (!request.error) { struct magi_response response; response_request(&request, &response); @@ -33,8 +34,8 @@ int main(int argc, char const * argv[]) } magi_request_destroy(&request); } - /* Fast CGI session error */ } - /* Fast CGI session error */ + puts(session.error->message); + magi_session_destroy(&session); return 0; } diff --git a/examples/upload.c b/examples/upload.c index 06a3cf9..5fe3338 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -29,47 +29,42 @@ void tempfile_callback(struct magi_field * field, char * buffer, int len) } } -void handle_request() +void response_request(struct magi_request * req, struct magi_response * res) +{ + magi_response_content_type(res, magi_xhtml); + magi_response_content( + res, "<!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>" + "<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>"); + + struct magi_field * name = magi_field_list_get(req->fields, "name"); + struct magi_field * data = magi_field_list_get(req->fields, "data"); + if (name && name->data && data) { + rename("data", name->data); + } +} + +int main(int argc, char const * argv[]) { struct magi_request request; if (magi_cgi(&request, tempfile_callback, 0)) { - struct magi_field * name = magi_field_list_get(request.fields, "name"); - struct magi_field * data = magi_field_list_get(request.fields, "data"); - if (name && name->data && data) { - rename("data", name->data); - } + struct magi_response response; + response_request(&request, &response); + magi_cgi_response(&response); + magi_response_destroy(); } else { magi_cgi_error(request.error); } magi_request_destroy(&request); -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts("<!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>" - "<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>"); -} - -int main(int argc, char const * argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); return 0; } |