aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-13 20:55:11 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-13 20:55:11 +0300
commitca08ed93e67a99868f01d21f0d2e34d6a6757c75 (patch)
tree0475bec225b561f018b3d7b3d2961266c9d5b191 /examples
parent23f3f8592c21ee58b0ac040736b5b766d52de193 (diff)
downloadmagi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.tar
magi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.tar.xz
magi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.zip
[magi]
Diffstat (limited to 'examples')
-rw-r--r--examples/append.c4
-rw-r--r--examples/cookie.c55
-rw-r--r--examples/echo.c157
-rw-r--r--examples/fcgi.c40
-rw-r--r--examples/upload.c5
5 files changed, 152 insertions, 109 deletions
diff --git a/examples/append.c b/examples/append.c
index b6ee217..0c7df7e 100644
--- a/examples/append.c
+++ b/examples/append.c
@@ -14,8 +14,10 @@ void handle_request()
fputs(a->data, file);
fclose(file);
}
- magi_request_destroy(&request);
+ } else {
+ magi_cgi_error(request.error);
}
+ magi_request_destroy(&request);
}
void print_preamble()
diff --git a/examples/cookie.c b/examples/cookie.c
index 5ef56ac..6ba5bd0 100644
--- a/examples/cookie.c
+++ b/examples/cookie.c
@@ -5,45 +5,38 @@
#include <stdlib.h>
-void print_preamble()
+void response_request(struct magi_request * req, struct magi_response * res)
{
- puts("Set-Cookie:cookie=monstre\r\n" /* Important to set cookies before: */
- "Content-Type: application/xhtml+xml\r\n\r\n");
-}
+ struct magi_cookie_list * cookie;
-void print_webpage_top()
-{
- 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>Cookie Listing and Setting</title></head>"
- "<body>");
-}
+ 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>Cookie Listing and Setting</title></head>"
+ "<body>");
-void read_and_print_cookies()
-{
- struct magi_request request;
- if (magi_cgi(&request, 0, 0)) {
- struct magi_cookie_list * cookie;
- for (cookie = request.cookies; cookie; cookie = cookie->next) {
- printf("[%s] = [%s]<br/>", cookie->item.name, cookie->item.data);
- }
- magi_request_destroy(&request);
+ for (cookie = req->cookies; cookie; cookie = cookie->next) {
+ printf("[%s] = [%s]<br/>", cookie->item.name, cookie->item.data);
}
-}
-void print_webpage_bottom()
-{
- puts("</body>"
- "</html>");
+ magi_response_content(res, "</body></html>");
+
+ magi_response_cookie_build(res, "cookie", "monstre", 0, 0, 0);
}
int main(int argc, char const * argv[])
{
- print_preamble();
- /* Following probably will be much more pleasant with use of templates. */
- print_webpage_top();
- read_and_print_cookies();
- print_webpage_bottom();
+ struct magi_request request;
+ if (magi_cgi(&request, 0, 0)) {
+ 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);
return 0;
}
diff --git a/examples/echo.c b/examples/echo.c
index 0345b8c..853e6ce 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -6,116 +6,121 @@
#include <stdlib.h>
-void print_preamble()
+void proceed_cookies(
+ struct magi_cookie_list * cookies, struct magi_response * response)
{
- puts("Content-type: application/xhtml+xml\r\n\r\n"
- "<!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>Echo</title></head>"
- "<body>");
-}
-
-void proceed_cookies(struct magi_cookie_list * cookies)
-{
- puts("<h2>Cookies:</h2>");
+ magi_response_content(response, "<h2>Cookies:</h2>");
while (cookies) {
- puts("Cookie with name [");
- puts(cookies->item.name);
+ magi_response_content(response, "Cookie with name [");
+ magi_response_content(response, cookies->item.name);
if (cookies->item.data) {
- puts("] is [");
- puts(cookies->item.data);
+ magi_response_content(response, "] is [");
+ magi_response_content(response, cookies->item.data);
}
if (cookies->item.domain) {
- puts("] for domain [");
- puts(cookies->item.domain);
+ magi_response_content(response, "] for domain [");
+ magi_response_content(response, cookies->item.domain);
}
if (cookies->item.port) {
- puts("] for port [");
- puts(cookies->item.port);
+ magi_response_content(response, "] for port [");
+ magi_response_content(response, cookies->item.port);
}
if (cookies->item.path) {
- puts("] for path [");
- puts(cookies->item.path);
+ magi_response_content(response, "] for path [");
+ magi_response_content(response, cookies->item.path);
}
- puts("]<br/>");
+ magi_response_content(response, "]<br/>");
cookies = cookies->next;
}
- puts("<hr/>");
+ magi_response_content(response, "<hr/>");
}
-void proceed_fields(struct magi_field_list * fields)
+void proceed_fields(
+ struct magi_field_list * fields, struct magi_response * response)
{
- puts("<h2>Feilds:</h2>");
+ magi_response_content(response, "<h2>Feilds:</h2>");
while (fields) {
- puts("Feild [");
- puts(fields->item.name);
- puts("] = [");
- puts(fields->item.data);
- puts("]<br/>");
+ 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;
}
- puts("<hr/>");
+ magi_response_content(response, "<hr/>");
}
-void proceed_params(struct magi_param_list * params)
+void proceed_params(
+ struct magi_param_list * params, struct magi_response * response)
{
- puts("<h2>HTTP Parameters:</h2>");
+ magi_response_content(response, "<h2>HTTP Parameters:</h2>");
while (params) {
- puts("Param [");
- puts(params->item.name);
- puts("] is [");
- puts(params->item.data);
- puts("]<br/>");
+ 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/>");
params = params->next;
}
- puts("<hr/>");
+ magi_response_content(response, "<hr/>");
}
-void handle_request()
+void process_meta(struct magi_request * req, struct magi_response * res)
{
- struct magi_request request;
- if (magi_cgi(&request, 0, 0)) {
- puts("<h1>Echo CGI Script</h1>");
- puts("I was called with method [");
- puts(request.method);
- if (request.uri) {
- puts("] with URL [");
- puts(request.uri);
- }
- if (request.server_name) {
- puts("] for server [");
- puts(request.server_name);
- }
- if (request.server_port) {
- puts("] on port [");
- puts(request.server_port);
- }
- if (request.server_protocol) {
- puts("] with protocol [");
- puts(request.server_protocol);
- }
- if (request.server_software) {
- puts("] and I am running on software [");
- puts(request.server_software);
- }
- puts("]<br/>");
- proceed_cookies(request.cookies);
- proceed_fields(request.fields);
- proceed_params(request.http_params);
- magi_request_destroy(&request);
+ magi_response_content(res,
+ "<h1>Echo CGI Script</h1>"
+ "I was called with method [");
+ magi_response_content(res, req->method);
+ if (req->uri) {
+ magi_response_content(res, "] with URL [");
+ magi_response_content(res, req->uri);
+ }
+ if (req->server_name) {
+ magi_response_content(res, "] for server [");
+ magi_response_content(res, req->server_name);
+ }
+ if (req->server_port) {
+ magi_response_content(res, "] on port [");
+ magi_response_content(res, req->server_port);
+ }
+ if (req->server_protocol) {
+ magi_response_content(res, "] with protocol [");
+ magi_response_content(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_content(res, "]<br/>");
}
-void print_footer()
+void response_request(struct magi_request * req, struct magi_response * res)
{
- puts("</body></html>");
+ 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>Echo</title></head>"
+ "<body>");
+ process_meta(req, res);
+ proceed_cookies(req->cookies, res);
+ proceed_fields(req->fields, res);
+ proceed_params(req->http_params, res);
+ magi_response_content(res, "</body></html>");
}
int main(int argc, char const * argv[])
{
- print_preamble();
- handle_request();
- print_footer();
+ struct magi_request request;
+ if (magi_cgi(&request, 0, 0)) {
+ 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);
return 0;
}
diff --git a/examples/fcgi.c b/examples/fcgi.c
new file mode 100644
index 0000000..5972cdd
--- /dev/null
+++ b/examples/fcgi.c
@@ -0,0 +1,40 @@
+#include <fastcgi.h>
+#include <request.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+void response_request(struct magi_request * req, struct magi_resopnse * 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>Fast CGI</title></head>"
+ "<body>Hi!</body>"
+ "</html>");
+}
+
+int main(int argc, char const * argv[])
+{
+ struct magi_session session;
+ if (magi_fcgi(&session)) {
+ struct magi_request request;
+ while (magi_accept(&request, &session)) {
+ if (!request.error) {
+ struct magi_response response;
+ response_request(&request, &response);
+ magi_fcgi_response(response);
+ magi_reponse_destroy(response);
+ } else {
+ magi_fcgi_error(request.error, &session);
+ }
+ magi_request_destroy(&request);
+ }
+ /* Fast CGI session error */
+ }
+ /* Fast CGI session error */
+ return 0;
+}
diff --git a/examples/upload.c b/examples/upload.c
index 1cf6f5f..06a3cf9 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
+
void tempfile_callback(struct magi_field * field, char * buffer, int len)
{
static FILE * file = 0;
@@ -37,8 +38,10 @@ void handle_request()
if (name && name->data && data) {
rename("data", name->data);
}
- magi_request_destroy(&request);
+ } else {
+ magi_cgi_error(request.error);
}
+ magi_request_destroy(&request);
}
void print_preamble()