aboutsummaryrefslogtreecommitdiff
path: root/examples/upload.c
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-15 18:01:45 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-15 18:01:45 +0300
commit32c1fbd2cf779a65e807d0d82412ffffa2401962 (patch)
treebb0020c18a72c4622e68258d8911956ecaca3765 /examples/upload.c
parente6d5f9552e66ee2db3056e810b58b2bf82551356 (diff)
downloadmagi-32c1fbd2cf779a65e807d0d82412ffffa2401962.tar
magi-32c1fbd2cf779a65e807d0d82412ffffa2401962.tar.xz
magi-32c1fbd2cf779a65e807d0d82412ffffa2401962.zip
[magi]
Diffstat (limited to 'examples/upload.c')
-rw-r--r--examples/upload.c65
1 files changed, 30 insertions, 35 deletions
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;
}