aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parentcb6a86b8db5a26b92073c4fc5ceffffa8cd842c2 (diff)
downloadmagi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar
magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar.xz
magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.zip
[magi] Finalization.
Diffstat (limited to 'src')
-rw-r--r--src/error.c19
-rw-r--r--src/fastcgi.c23
-rw-r--r--src/parse.c (renamed from src/cgi.c)70
-rw-r--r--src/request.c11
-rw-r--r--src/response.c151
-rw-r--r--src/session.c58
6 files changed, 86 insertions, 246 deletions
diff --git a/src/error.c b/src/error.c
index a1c3153..eb16eb6 100644
--- a/src/error.c
+++ b/src/error.c
@@ -1,9 +1,11 @@
#include "error.h"
+#include <stdio.h>
+
const char *magi_error_message(magi_error error)
{
- const char *const messages[] = {
+ static const char *const messages[] = {
0, /* magi_error_none */
"No boundary for multipart.", /* magi_error_nobound */
"Content-Type is unknown.", /* magi_error_unknown */
@@ -17,3 +19,18 @@ const char *magi_error_message(magi_error error)
};
return messages[error];
}
+
+void magi_error_response(magi_error error)
+{
+ fputs("Status: 400 Bad Request\r\n"
+ "Content-Type: text/html\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>400 Bad Request</title></head>"
+ "<body>"
+ "<h1>400 <i>Bad Request</i></h1>"
+ "<h2>", stdout);
+ fputs(magi_error_message(error), stdout);
+ fputs("</h2></body></html>", stdout);
+}
diff --git a/src/fastcgi.c b/src/fastcgi.c
deleted file mode 100644
index 8959c14..0000000
--- a/src/fastcgi.c
+++ /dev/null
@@ -1,23 +0,0 @@
-#include "fastcgi.h"
-
-
-int magi_fastcgi_head(magi_session *s, magi_request *r)
-{
- (void)s;
- (void)r;
- return 1;
-}
-
-
-int magi_fastcgi_body(magi_session *s, magi_request *r)
-{
- (void)s;
- (void)r;
- return 1;
-}
-
-
-int magi_fastcgi(magi_session *s, magi_request *r)
-{
- return magi_fastcgi_head(s, r) && magi_fastcgi_body(s, r);
-}
diff --git a/src/cgi.c b/src/parse.c
index 7b0551d..58ae001 100644
--- a/src/cgi.c
+++ b/src/parse.c
@@ -1,4 +1,4 @@
-#include "cgi.h"
+#include "parse.h"
#include "cookie.h"
#include "cookies.h"
@@ -152,67 +152,8 @@ static int next()
return getchar();
}
-static void mhead(void *any, magi_param *header)
-{
- (void)any;
- fputs(header->name, stdout);
- fputs(": ", stdout);
- fputs(header->data, stdout);
- fputs("\r\n", stdout);
-}
-
-static void mstart_body()
-{
- fputs("\r\n", stdout);
-}
-
-static void mbody(void *any, const char *data, int len)
-{
- (void)any;
- fwrite(data, 1, len, stdout);
-}
-
-static void mformat(void *any, const char *format, va_list args)
-{
- (void)any;
- vprintf(format, args);
-}
-
-static void mfile(void *any, FILE *file)
-{
- (void)any;
- while (!feof(file)) {
- char buf[64];
- int len = fread(buf, 1, 64, file);
- fwrite(buf, 1, len, stdout);
- }
-}
-
-static void mclose() {}
-
-static void setup_response(magi_request *r)
-{
- static const magi_response_methods m = {
- mhead,
- mstart_body,
- mbody,
- mformat,
- mfile,
- mclose
- };
- r->response = malloc(sizeof(*r->response));
- r->response->methods = &m;
- r->response->userdata = 0;
- r->response->head_response = 0;
- r->response->head_general = 0;
- r->response->head_entity = 0;
- r->response->head_done = 0;
- magi_response_content_type(r, "text/html");
- magi_response_status(r, 200, "OK");
-}
-
/* Interfacial CGI Request Handling */
-int magi_cgi_head(magi_request *request)
+int magi_parse_head(magi_request *request)
{
request->cookies = 0;
request->files = 0;
@@ -223,11 +164,10 @@ int magi_cgi_head(magi_request *request)
cgi_env(request);
cgi_cookies(request);
cgi_url(request);
- setup_response(request);
return !request->error;
}
-int magi_cgi_body(magi_request *request)
+int magi_parse_body(magi_request *request)
{
magi_error *e = &request->error;
request->error = magi_error_none;
@@ -260,7 +200,7 @@ int magi_cgi_body(magi_request *request)
return !request->error;
}
-int magi_cgi(magi_request *request)
+int magi_parse(magi_request *request)
{
- return magi_cgi_head(request) && magi_cgi_body(request);
+ return magi_parse_head(request) && magi_parse_body(request);
}
diff --git a/src/request.c b/src/request.c
index d80bf20..bf8fc1d 100644
--- a/src/request.c
+++ b/src/request.c
@@ -1,6 +1,5 @@
#include "request.h"
-#include "response.h"
#include <stdlib.h>
@@ -30,7 +29,6 @@ static void request_free(magi_request *request)
free(request->host);
free(request->script);
free(request->path);
- free(request->response);
}
static void request_annul(magi_request *request)
@@ -44,7 +42,6 @@ static void request_annul(magi_request *request)
request->host = 0;
request->script = 0;
request->path = 0;
- request->response = 0;
}
void magi_request_free(magi_request *request)
@@ -55,14 +52,6 @@ void magi_request_free(magi_request *request)
magi_params_free(request->head);
magi_params_free(request->body);
magi_files_free(request->files);
- request->response->methods->close(request->response->userdata);
- free(request->response->userdata);
- magi_params_free(request->response->head_response);
- magi_params_free(request->response->head_general);
- magi_params_free(request->response->head_entity);
- free(request->response->head_response);
- free(request->response->head_general);
- free(request->response->head_entity);
request_free(request);
request_annul(request);
}
diff --git a/src/response.c b/src/response.c
index dc36bb0..c5b7575 100644
--- a/src/response.c
+++ b/src/response.c
@@ -6,11 +6,55 @@
#include <string.h>
-void magi_response_status(magi_request *r, int code, const char *description)
+void magi_response_init(magi_response *r)
+{
+ r->head_response = 0;
+ r->head_general = 0;
+ r->head_entity = 0;
+ magi_response_status(r, 200, "OK");
+ magi_response_content_type(r, "text/html");
+}
+
+static void response_headers(magi_params *p)
+{
+ for (; p; p = p->next) {
+ fputs(p->item.name, stdout);
+ fputs(": ", stdout);
+ fputs(p->item.data, stdout);
+ fputs("\r\n", stdout);
+ }
+}
+
+void magi_response_free(magi_response *r)
+{
+ response_headers(r->head_response);
+ response_headers(r->head_general);
+ response_headers(r->head_entity);
+ fputs("\r\n", stdout);
+ magi_params_free(r->head_response);
+ magi_params_free(r->head_general);
+ magi_params_free(r->head_entity);
+ free(r->head_response);
+ free(r->head_general);
+ free(r->head_entity);
+ r->head_response = 0;
+ r->head_general = 0;
+ r->head_entity = 0;
+}
+
+
+void magi_response_default()
+{
+ fputs("Status: 200 Ok\r\n"
+ "Content-Type: text/html\r\n\r\n", stdout);
+}
+
+
+void magi_response_status(magi_response *r, int code, const char *description)
{
int dlen;
magi_param addon;
- if (r->response->head_done || code <= 99 || 600 <= code) {
+ if (code <= 99 || 600 <= code) {
return;
}
dlen = strlen(description);
@@ -21,15 +65,15 @@ void magi_response_status(magi_request *r, int code, const char *description)
addon.data[2] = '0' + code % 10;
addon.data[3] = ' ';
memcpy(addon.data + 4, description, dlen + 1);
- magi_params_set(&r->response->head_response, &addon);
+ magi_params_set(&r->head_response, &addon);
}
-void magi_response_cookie(magi_request *r, const char *name, const char *data)
+void magi_response_cookie(magi_response *r, const char *name, const char *data)
{
magi_param addon;
int nlen;
int dlen;
- if (r->response->head_done || !name || !data) {
+ if (!name || !data) {
return;
}
nlen = strlen(name);
@@ -39,10 +83,10 @@ void magi_response_cookie(magi_request *r, const char *name, const char *data)
memcpy(addon.data, name, nlen);
addon.data[nlen] = '=';
memcpy(addon.data + nlen + 1, data, dlen + 1);
- magi_params_add(&r->response->head_general, &addon);
+ magi_params_add(&r->head_general, &addon);
}
-void magi_response_cookie_complex(magi_request *r, magi_cookie *c)
+void magi_response_cookie_complex(magi_response *r, magi_cookie *c)
{
magi_param addon;
char *pointer;
@@ -50,7 +94,7 @@ void magi_response_cookie_complex(magi_request *r, magi_cookie *c)
const int cdsize = 9;
const int cpsize = 7;
const int cmsize = 10;
- if (r->response->head_done || !c->name) {
+ if (!c->name) {
return;
}
nlen = strlen(c->name);
@@ -83,14 +127,14 @@ void magi_response_cookie_complex(magi_request *r, magi_cookie *c)
memcpy(pointer, "; Max-Age=", cmsize);
memcpy(pointer + cmsize, c->max_age, msize - cmsize);
}
- magi_params_add(&r->response->head_general, &addon);
+ magi_params_add(&r->head_general, &addon);
}
-void magi_response_cookie_discard(magi_request *r, const char *name)
+void magi_response_cookie_discard(magi_response *r, const char *name)
{
magi_param addon;
int len;
- if (r->response->head_done || !name) {
+ if (!name) {
return;
}
len = strlen(name);
@@ -98,29 +142,22 @@ void magi_response_cookie_discard(magi_request *r, const char *name)
addon.data = malloc(len + 13);
memcpy(addon.data, name, len);
memcpy(addon.data + len, "=; Max-Age=1", 13);
- magi_params_add(&r->response->head_general, &addon);
+ magi_params_add(&r->head_general, &addon);
}
-void magi_response_header(magi_request *r, const char *name, const char *data)
+void magi_response_header(magi_response *r, const char *name, const char *data)
{
magi_param addon;
- if (r->response->head_done) {
- return;
- }
addon.name = magi_str_create_copy(name, strlen(name));
addon.data = magi_str_create_copy(data, strlen(data));
- magi_params_add(&r->response->head_general, &addon);
+ magi_params_add(&r->head_general, &addon);
}
-void magi_response_content_length(magi_request *r, int length)
+void magi_response_content_length(magi_response *r, int length)
{
magi_param addon;
- int len;
- if (r->response->head_done) {
- return;
- }
+ int len = 1;
addon.name = magi_str_create_copy("Content-Length", 14);
- len = 1;
addon.data = malloc(len + 1);
*addon.data = '0' + length % 10;
while (length /= 10) {
@@ -129,75 +166,13 @@ void magi_response_content_length(magi_request *r, int length)
++len;
}
addon.data[len] = 0;
- magi_params_set(&r->response->head_entity, &addon);
+ magi_params_set(&r->head_entity, &addon);
}
-void magi_response_content_type(magi_request *r, const char *type)
+void magi_response_content_type(magi_response *r, const char *type)
{
magi_param addon;
- if (r->response->head_done) {
- return;
- }
addon.name = magi_str_create_copy("Content-Type", 12);
addon.data = magi_str_create_copy(type, strlen(type));
- magi_params_set(&r->response->head_entity, &addon);
-}
-
-static void response_headers(magi_response_implementation *r, magi_params *p)
-{
- for (; p; p = p->next) {
- r->methods->head(r->userdata, &p->item);
- }
-}
-
-void magi_response_head(magi_request *r)
-{
- if (r->response->head_done) {
- return;
- }
- response_headers(r->response, r->response->head_response);
- response_headers(r->response, r->response->head_general);
- response_headers(r->response, r->response->head_entity);
- r->response->methods->start_body(r->response->userdata);
- r->response->head_done = 1;
-}
-
-void magi_response(magi_request *r, const char *addon)
-{
- magi_response_head(r);
- if (!addon) {
- return;
- }
- r->response->methods->body(r->response->userdata, addon, strlen(addon));
-}
-
-void magi_response_format(magi_request *r, const char *format, ...)
-{
- va_list args;
- magi_response_head(r);
- va_start(args, format);
- r->response->methods->format(r->response->userdata, format, args);
- va_end(args);
-}
-
-void magi_response_file(magi_request *r, FILE *file)
-{
- magi_response_head(r);
- r->response->methods->file(r->response->userdata, file);
-}
-
-
-void magi_response_error(magi_request *r)
-{
- magi_response_status(r, 400, "Bad Request");
- 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>400 Bad Request</title></head>"
- "<body>"
- "<h1>400 <i>Bad Request</i></h1>"
- "<h2>");
- magi_response(r, magi_error_message(r->error));
- magi_response(r, "</h2></body></html>");
+ magi_params_set(&r->head_entity, &addon);
}
diff --git a/src/session.c b/src/session.c
deleted file mode 100644
index aec0e6d..0000000
--- a/src/session.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include "session.h"
-
-#include <arpa/inet.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-
-
-enum { listen_backlog = 64 };
-
-void magi_session_init(magi_session *s)
-{
- s->socket = 0;
-}
-
-void magi_session_free(magi_session *s)
-{
- if (s->socket) {
- shutdown(s->socket, SHUT_RDWR);
- s->socket = 0;
- }
-}
-
-
-int magi_session_inet(magi_session *s, const char *address, int port)
-{
- struct sockaddr_in addr;
- if (s->socket) {
- return 0;
- }
- s->socket = socket(AF_INET, SOCK_STREAM, 0);
- if (s->socket == -1) {
- s->socket = 0;
- return 0;
- }
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(address);
- return !bind(s->socket, (struct sockaddr *)&addr, sizeof(addr)) &&
- !listen(s->socket, listen_backlog);
-}
-
-int magi_session_unix(magi_session *s, const char *path)
-{
- struct sockaddr_un addr;
- if (s->socket) {
- return 0;
- }
- s->socket = socket(AF_UNIX, SOCK_STREAM, 0);
- if (s->socket == -1) {
- s->socket = 0;
- return 0;
- }
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
- return !bind(s->socket, (struct sockaddr *)&addr, sizeof(addr)) &&
- !listen(s->socket, listen_backlog);
-}