aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-29 18:50:04 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-29 18:50:04 +0300
commitb08db17162fddda97e6ee1ac625eae1430d22b64 (patch)
tree2c332f2db57a15c72f58704b9175cfece0ae9698 /examples
parentea25abb295d9b4fbaf0ac23af8ffe71247970f1b (diff)
downloadmagi-b08db17162fddda97e6ee1ac625eae1430d22b64.tar
magi-b08db17162fddda97e6ee1ac625eae1430d22b64.tar.xz
magi-b08db17162fddda97e6ee1ac625eae1430d22b64.zip
[magi] Almost done!
Diffstat (limited to 'examples')
-rw-r--r--examples/append.c17
-rw-r--r--examples/cookie.c4
-rw-r--r--examples/echo.c19
-rw-r--r--examples/fcgi.c1
-rw-r--r--examples/upload.c43
5 files changed, 41 insertions, 43 deletions
diff --git a/examples/append.c b/examples/append.c
index 3320fe0..e6e4a8a 100644
--- a/examples/append.c
+++ b/examples/append.c
@@ -7,6 +7,13 @@
void response_request(struct magi_request * req, struct magi_response * res)
{
+ char * data = magi_param_list_get(req->params, "addon");
+ if (data) {
+ FILE * file = fopen("file_to_append", "a");
+ fputs(data, file);
+ fclose(file);
+ }
+
magi_response_content_type(res, magi_xhtml);
magi_response_add(
res,
@@ -15,19 +22,12 @@ void response_request(struct magi_request * req, struct magi_response * res)
"<html xmlns='http://www.w3.org/1999/xhtml'>"
"<head><title>Append to File</title></head>"
"<body>"
- "<form action='/cgi-bin/append' method='get'><fieldset>"
+ "<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>");
-
- struct magi_param * addon = magi_param_list_get(req->url_params, "addon");
- if (addon && addon->data) {
- FILE * file = fopen("file_to_append", "a");
- fputs(addon->data, file);
- fclose(file);
- }
}
int main(int argc, char const * argv[])
@@ -36,6 +36,7 @@ int main(int argc, char const * argv[])
magi_request_setup(&request);
if (magi_request_cgi(&request)) {
struct magi_response response;
+ magi_response_setup(&response);
response_request(&request, &response);
magi_response_cgi(&response);
magi_response_destroy(&response);
diff --git a/examples/cookie.c b/examples/cookie.c
index 01d6dea..f21cb4a 100644
--- a/examples/cookie.c
+++ b/examples/cookie.c
@@ -26,7 +26,6 @@ void response_request(struct magi_request * req, struct magi_response * res)
magi_response_add(res, "</body></html>");
magi_response_cookie_easy(res, "cookie", "monster");
- magi_response_cookie_delete(res, "monster");
}
int main(int argc, char const * argv[])
@@ -35,9 +34,10 @@ int main(int argc, char const * argv[])
magi_request_setup(&request);
if (magi_request_cgi(&request)) {
struct magi_response response;
+ magi_response_setup(&response);
response_request(&request, &response);
magi_response_cgi(&response);
- magi_response_destroy();
+ magi_response_destroy(&response);
} else {
magi_error_cgi(request.error);
}
diff --git a/examples/echo.c b/examples/echo.c
index 95219fc..3f72f8a 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -47,11 +47,23 @@ void proceed_params(struct magi_param_list * params,
magi_response_add(response, "<hr/>");
}
+void proceed_files(struct magi_file_list * files,
+ struct magi_response * response)
+{
+ while (files) {
+ magi_response_add_format(response, "[%s] was [%s] on userside<br/>",
+ files->item.param_name,
+ files->item.file_name);
+ files = files->next;
+ }
+ magi_response_add(response, "<hr/>");
+}
+
void process_meta(struct magi_request * req, struct magi_response * res)
{
magi_response_add(res,
- "<h1>Echo CGI Script</h1>I was called with method [%s",
- req->method);
+ "<h1>Echo CGI Script</h1>I was called with method [");
+ magi_response_add(res, req->method);
if (req->uri) {
magi_response_add(res, "] with URL [");
magi_response_add(res, req->uri);
@@ -92,6 +104,8 @@ void response_request(struct magi_request * req, struct magi_response * res)
proceed_params(req->url_params, res);
magi_response_add(res, "<h2>HTTP Parameters:</h2>");
proceed_params(req->http_params, res);
+ magi_response_add(res, "<h2>Files:</h2>");
+ proceed_files(req->files, res);
magi_response_add(res, "</body></html>");
}
@@ -101,6 +115,7 @@ int main(int argc, char const * argv[])
magi_request_setup(&request);
if (magi_request_cgi(&request)) {
struct magi_response response;
+ magi_response_setup(&response);
response_request(&request, &response);
magi_response_cgi(&response);
magi_response_destroy(&response);
diff --git a/examples/fcgi.c b/examples/fcgi.c
index 7d93614..06384c4 100644
--- a/examples/fcgi.c
+++ b/examples/fcgi.c
@@ -30,6 +30,7 @@ int main(int argc, char const * argv[])
while (magi_request_fcgi(&request, &session)) {
if (!request.error) {
struct magi_response response;
+ magi_response_setup(&response);
response_request(&request, &response);
magi_response_fcgi(&response, &session);
magi_response_destroy(&response);
diff --git a/examples/upload.c b/examples/upload.c
index 25fefaf..f4350ee 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -7,30 +7,14 @@
#include <string.h>
-void tempfile_callback(struct magi_file * file,
- char * addon,
- int addon_len,
- int is_addon_last,
- void * unused)
+void response_request(struct magi_request * req, struct magi_response * res)
{
- if (!strcmp(file->param_name, "data")) {
- static FILE * f = 0;
- if (!f) {
- remove(file->param_name);
- f = fopen(file->param_name, "wb");
- }
- if (addon_len) {
- fwrite(addon, 1, addon_len, f);
- }
- if (is_addon_last) {
- fclose(f);
- f = 0;
- }
+ char * name = magi_param_list_get(req->params, "name");
+ struct magi_file * data = magi_file_list_get(req->files, "data");
+ if (name && data) {
+ rename("data", name);
}
-}
-void response_request(struct magi_request * req, struct magi_response * res)
-{
magi_response_content_type(res, magi_xhtml);
magi_response_add(
res, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' "
@@ -38,7 +22,7 @@ void response_request(struct magi_request * req, struct magi_response * res)
"<html xmlns='http://www.w3.org/1999/xhtml'>"
"<head><title>Upload File</title></head>"
"<body>"
- "<form action='/cgi-bin/upload' method='post' "
+ "<form action='/cgi-bin/echo' method='post' "
"enctype='multipart/form-data'><fieldset>"
"<input type='text' name='name' value='filename'/>"
"<input type='file' name='data'/>"
@@ -46,24 +30,21 @@ void response_request(struct magi_request * req, struct magi_response * res)
"</fieldset></form>"
"</body>"
"</html>");
-
- struct magi_param * name = magi_param_list_get(req->params, "name");
- struct magi_param * data = magi_param_list_get(req->params, "data");
- if (name && name->data && data) {
- rename("data", name->data);
- }
}
int main(int argc, char const * argv[])
{
- struct magi_request request;
+ struct magi_request request;
+ struct magi_tempfiles tmps = { 0, 0, 0, 0 };
magi_request_setup(&request);
- request.file_callback = tempfile_callback;
+ magi_tempfiles_add(&tmps, "data", "data", 0);
+ magi_request_setup_tempfiles(&request, &tmps);
if (magi_request_cgi(&request)) {
struct magi_response response;
+ magi_response_setup(&response);
response_request(&request, &response);
magi_response_cgi(&response);
- magi_response_destroy();
+ magi_response_destroy(&response);
} else {
magi_error_cgi(request.error);
}