aboutsummaryrefslogtreecommitdiff
path: root/examples/append.c
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-04-04 19:39:38 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-04-04 19:39:38 +0300
commit07657b8a9f5c2fd9047594ec8604b9c439a999e4 (patch)
treeefc15cde781a74f5782c780422dde6ec0dfb1af4 /examples/append.c
parentcb6a86b8db5a26b92073c4fc5ceffffa8cd842c2 (diff)
downloadmagi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar
magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar.xz
magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.zip
[magi] Finalization.
Diffstat (limited to 'examples/append.c')
-rw-r--r--examples/append.c52
1 files changed, 29 insertions, 23 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;
}