diff options
Diffstat (limited to 'examples/append.c')
-rw-r--r-- | examples/append.c | 64 |
1 files changed, 30 insertions, 34 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; } |