aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-22 18:55:54 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-22 18:55:54 +0300
commit88081d9a4376b1ee4feed3bc843c46096e087b1e (patch)
tree5780b195f16975e890a8a98b0dc17d6ce2da3001 /examples
parent0f6943feff48e92b071726abcf83070d8a529a41 (diff)
downloadmagi-88081d9a4376b1ee4feed3bc843c46096e087b1e.tar
magi-88081d9a4376b1ee4feed3bc843c46096e087b1e.tar.xz
magi-88081d9a4376b1ee4feed3bc843c46096e087b1e.zip
.
Diffstat (limited to 'examples')
-rw-r--r--examples/append.c18
-rw-r--r--examples/cookie.c21
-rw-r--r--examples/echo.c99
-rw-r--r--examples/fcgi.c12
-rw-r--r--examples/upload.c28
5 files changed, 88 insertions, 90 deletions
diff --git a/examples/append.c b/examples/append.c
index 30060fa..3320fe0 100644
--- a/examples/append.c
+++ b/examples/append.c
@@ -1,5 +1,6 @@
#include <cgi.h>
#include <request.h>
+#include <response.h>
#include <stdio.h>
#include <stdlib.h>
@@ -7,7 +8,7 @@
void response_request(struct magi_request * req, struct magi_response * res)
{
magi_response_content_type(res, magi_xhtml);
- magi_response_content(
+ magi_response_add(
res,
"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' "
"'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>"
@@ -21,10 +22,10 @@ void response_request(struct magi_request * req, struct magi_response * res)
"</body>"
"</html>");
- struct magi_field * a = magi_field_list_get(req->fields, "addon");
- if (a && a->data) {
+ struct magi_param * addon = magi_param_list_get(req->url_params, "addon");
+ if (addon && addon->data) {
FILE * file = fopen("file_to_append", "a");
- fputs(a->data, file);
+ fputs(addon->data, file);
fclose(file);
}
}
@@ -32,13 +33,14 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0, 0)) {
+ magi_request_setup(&request);
+ if (magi_request_cgi(&request)) {
struct magi_response response;
response_request(&request, &response);
- magi_cgi_response(&response);
- magi_response_destroy();
+ magi_response_cgi(&response);
+ magi_response_destroy(&response);
} else {
- magi_cgi_error(request.error);
+ magi_error_cgi(request.error);
}
magi_request_destroy(&request);
return 0;
diff --git a/examples/cookie.c b/examples/cookie.c
index f99f07f..01d6dea 100644
--- a/examples/cookie.c
+++ b/examples/cookie.c
@@ -8,35 +8,38 @@
void response_request(struct magi_request * req, struct magi_response * res)
{
- struct magi_cookie_list * cookie;
+ struct magi_cookie_list * current;
magi_response_content_type(res, magi_xhtml);
- magi_response_content(
+ magi_response_add(
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);
+ for (current = req->cookies; current; current = current->next) {
+ magi_response_add_format(res, "[%s] = [%s]<br/>", current->item.name,
+ current->item.data);
}
- magi_response_content(res, "</body></html>");
+ magi_response_add(res, "</body></html>");
- magi_response_cookie_build(res, "cookie", "monstre", 0, 0, 0);
+ magi_response_cookie_easy(res, "cookie", "monster");
+ magi_response_cookie_delete(res, "monster");
}
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0, 0)) {
+ magi_request_setup(&request);
+ if (magi_request_cgi(&request)) {
struct magi_response response;
response_request(&request, &response);
- magi_cgi_response(&response);
+ magi_response_cgi(&response);
magi_response_destroy();
} else {
- magi_cgi_error(request.error);
+ magi_error_cgi(request.error);
}
magi_request_destroy(&request);
return 0;
diff --git a/examples/echo.c b/examples/echo.c
index 16e9d56..95219fc 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -2,6 +2,7 @@
#include <cookie.h>
#include <param.h>
#include <request.h>
+#include <response.h>
#include <stdio.h>
#include <stdlib.h>
@@ -9,94 +10,75 @@
void proceed_cookies(struct magi_cookie_list * cookies,
struct magi_response * response)
{
- magi_response_content(response, "<h2>Cookies:</h2>");
+ magi_response_add(response, "<h2>Cookies:</h2>");
while (cookies) {
- magi_response_content(response, "Cookie with name [");
- magi_response_content(response, cookies->item.name);
+ magi_response_add(response, "Cookie with name [");
+ magi_response_add(response, cookies->item.name);
if (cookies->item.data) {
- magi_response_content(response, "] is [");
- magi_response_content(response, cookies->item.data);
+ magi_response_add(response, "] is [");
+ magi_response_add(response, cookies->item.data);
}
if (cookies->item.domain) {
- magi_response_content(response, "] for domain [");
- magi_response_content(response, cookies->item.domain);
+ magi_response_add(response, "] for domain [");
+ magi_response_add(response, cookies->item.domain);
}
if (cookies->item.port) {
- magi_response_content(response, "] for port [");
- magi_response_content(response, cookies->item.port);
+ magi_response_add(response, "] for port [");
+ magi_response_add(response, cookies->item.port);
}
if (cookies->item.path) {
- magi_response_content(response, "] for path [");
- magi_response_content(response, cookies->item.path);
+ magi_response_add(response, "] for path [");
+ magi_response_add(response, cookies->item.path);
}
- magi_response_content(response, "]<br/>");
+ magi_response_add(response, "]<br/>");
cookies = cookies->next;
}
- magi_response_content(response, "<hr/>");
-}
-
-void proceed_fields(struct magi_field_list * fields,
- struct magi_response * response)
-{
- magi_response_content(response, "<h2>Feilds:</h2>");
- while (fields) {
- magi_response_content(response, "Feild [");
- magi_response_content(response, fields->item.name);
- magi_response_content(response, "] = [");
- magi_response_content(response, fields->item.data);
- magi_response_content(response, "]<br/>");
- fields = fields->next;
- }
- magi_response_content(response, "<hr/>");
+ magi_response_add(response, "<hr/>");
}
void proceed_params(struct magi_param_list * params,
struct magi_response * response)
{
- magi_response_content(response, "<h2>HTTP Parameters:</h2>");
while (params) {
- magi_response_content(response, "Param [");
- magi_response_content(response, params->item.name);
- magi_response_content(response, "] is [");
- magi_response_content(response, params->item.data);
- magi_response_content(response, "]<br/>");
+ magi_response_add_format(response, "[%s] is [%s]<br/>",
+ params->item.name, params->item.data);
params = params->next;
}
- magi_response_content(response, "<hr/>");
+ magi_response_add(response, "<hr/>");
}
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, req->method);
+ magi_response_add(res,
+ "<h1>Echo CGI Script</h1>I was called with method [%s",
+ req->method);
if (req->uri) {
- magi_response_content(res, "] with URL [");
- magi_response_content(res, req->uri);
+ magi_response_add(res, "] with URL [");
+ magi_response_add(res, req->uri);
}
if (req->server_name) {
- magi_response_content(res, "] for server [");
- magi_response_content(res, req->server_name);
+ magi_response_add(res, "] for server [");
+ magi_response_add(res, req->server_name);
}
if (req->server_port) {
- magi_response_content(res, "] on port [");
- magi_response_content(res, req->server_port);
+ magi_response_add(res, "] on port [");
+ magi_response_add(res, req->server_port);
}
if (req->server_protocol) {
- magi_response_content(res, "] with protocol [");
- magi_response_content(res, req->server_protocol);
+ magi_response_add(res, "] with protocol [");
+ magi_response_add(res, req->server_protocol);
}
if (req->server_software) {
- magi_response_content(res, "] and I am running on software [");
- magi_response_content(res, req->server_software);
+ magi_response_add(res, "] and I am running on software [");
+ magi_response_add(res, req->server_software);
}
- magi_response_content(res, "]<br/>");
+ magi_response_add(res, "]<br/>");
}
void response_request(struct magi_request * req, struct magi_response * res)
{
magi_response_content_type(res, magi_xhtml);
- magi_response_content(
+ magi_response_add(
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'>"
@@ -104,21 +86,26 @@ void response_request(struct magi_request * req, struct magi_response * res)
"<body>");
process_meta(req, res);
proceed_cookies(req->cookies, res);
- proceed_fields(req->fields, res);
+ magi_response_add(res, "<h2>Parameters:</h2>");
+ proceed_params(req->params, res);
+ magi_response_add(res, "<h2>URL Parameters:</h2>");
+ proceed_params(req->url_params, res);
+ magi_response_add(res, "<h2>HTTP Parameters:</h2>");
proceed_params(req->http_params, res);
- magi_response_content(res, "</body></html>");
+ magi_response_add(res, "</body></html>");
}
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0, 0)) {
+ magi_request_setup(&request);
+ if (magi_request_cgi(&request)) {
struct magi_response response;
response_request(&request, &response);
- magi_cgi_response(&response);
- magi_response_destroy();
+ magi_response_cgi(&response);
+ magi_response_destroy(&response);
} else {
- magi_cgi_error(request.error);
+ magi_error_cgi(request.error);
}
magi_request_destroy(&request);
return 0;
diff --git a/examples/fcgi.c b/examples/fcgi.c
index d6cc478..72fe83e 100644
--- a/examples/fcgi.c
+++ b/examples/fcgi.c
@@ -1,6 +1,7 @@
#include <error.h>
#include <fastcgi.h>
#include <request.h>
+#include <response.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -9,7 +10,7 @@
void response_request(struct magi_request * req, struct magi_resopnse * res)
{
magi_response_content_type(res, magi_xhtml);
- magi_response_content(
+ magi_response_add(
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'>"
@@ -23,16 +24,17 @@ int main(int argc, char const * argv[])
struct magi_session session;
int sock = magi_socket_inet("localhost", 9973);
/* E.g. also magi_socket_file("fcgi.sock") can be used. */
- if (magi_fcgi(&session, sock)) {
+ if (magi_session_fcgi(&session, sock)) {
struct magi_request request;
- while (magi_fcgi_accept(&request, &session)) {
+ magi_request_setup(&request);
+ while (magi_request_fcgi(&request, &session)) {
if (!request.error) {
struct magi_response response;
response_request(&request, &response);
- magi_fcgi_response(&response, &session);
+ magi_response_fcgi(&response, &session);
magi_response_destroy(&response);
} else {
- magi_fcgi_error(request.error, &session);
+ magi_error_fcgi(request.error, &session);
}
magi_request_destroy(&request);
}
diff --git a/examples/upload.c b/examples/upload.c
index 2ef8bc5..25fefaf 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -1,15 +1,17 @@
#include <cgi.h>
#include <multipart.h>
#include <request.h>
+#include <response.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void tempfile_callback(struct magi_file * file,
- char * buffer,
- int len,
- void * _)
+ char * addon,
+ int addon_len,
+ int is_addon_last,
+ void * unused)
{
if (!strcmp(file->param_name, "data")) {
static FILE * f = 0;
@@ -17,10 +19,10 @@ void tempfile_callback(struct magi_file * file,
remove(file->param_name);
f = fopen(file->param_name, "wb");
}
- if (len) {
- fwrite(buffer, 1, len, f);
+ if (addon_len) {
+ fwrite(addon, 1, addon_len, f);
}
- if (len < magi_file_callback_portion_max) {
+ if (is_addon_last) {
fclose(f);
f = 0;
}
@@ -30,7 +32,7 @@ void tempfile_callback(struct magi_file * file,
void response_request(struct magi_request * req, struct magi_response * res)
{
magi_response_content_type(res, magi_xhtml);
- magi_response_content(
+ magi_response_add(
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'>"
@@ -45,8 +47,8 @@ void response_request(struct magi_request * req, struct magi_response * res)
"</body>"
"</html>");
- struct magi_field * name = magi_field_list_get(req->fields, "name");
- struct magi_field * data = magi_field_list_get(req->fields, "data");
+ 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);
}
@@ -55,13 +57,15 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, tempfile_callback, 0, 0)) {
+ magi_request_setup(&request);
+ request.file_callback = tempfile_callback;
+ if (magi_request_cgi(&request)) {
struct magi_response response;
response_request(&request, &response);
- magi_cgi_response(&response);
+ magi_response_cgi(&response);
magi_response_destroy();
} else {
- magi_cgi_error(request.error);
+ magi_error_cgi(request.error);
}
magi_request_destroy(&request);
return 0;