aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-03-09 18:06:10 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-03-09 18:06:10 +0300
commit18ce121d4243358bc55a0474a529efe2580a0610 (patch)
tree046805a312734ed2b477b6782d66e754e928f371 /src
parent8869f95373fa61cd425da31dbc5e2b1b536363d3 (diff)
downloadmagi-18ce121d4243358bc55a0474a529efe2580a0610.tar
magi-18ce121d4243358bc55a0474a529efe2580a0610.tar.xz
magi-18ce121d4243358bc55a0474a529efe2580a0610.zip
[magi]
Diffstat (limited to 'src')
-rw-r--r--src/cookie.c43
-rw-r--r--src/cookies.h2
-rw-r--r--src/file.c38
-rw-r--r--src/param.c34
-rw-r--r--src/request.c124
-rw-r--r--src/response.c156
6 files changed, 226 insertions, 171 deletions
diff --git a/src/cookie.c b/src/cookie.c
index d1d82b3..7b40f1a 100644
--- a/src/cookie.c
+++ b/src/cookie.c
@@ -4,42 +4,39 @@
#include <string.h>
-int magi_cookie_list_add(struct magi_cookie_list ** list,
- struct magi_cookie * item)
+void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem)
{
- struct magi_cookie_list * node = malloc(sizeof(*node));
+ magi_cookies *node = malloc(sizeof(*node));
if (node) {
- node->next = *list;
- node->item = *item;
- *list = node;
+ node->next = *cookies;
+ node->item = *newitem;
+ *cookies = node;
}
- return !!node;
}
-char * magi_cookie_list_get(struct magi_cookie_list * list, const char * name)
+magi_cookie *magi_cookies_get(magi_cookies *cookies, const char *name)
{
- char * res = 0;
- if (!list || !name) {
+ magi_cookie *res = 0;
+ if (!cookies || !name) {
return 0;
}
- while (list) {
- if (!strcmp(list->item.name, name)) {
- res = list->item.data;
+ for (; cookies; cookies = cookies->next) {
+ if (!strcmp(cookies->item.name, name)) {
+ res = &cookies->item;
}
- list = list->next;
}
return res;
}
-void magi_cookie_list_destroy(struct magi_cookie_list * list)
+void magi_cookies_free(magi_cookies *cookies)
{
- if (list) {
- magi_cookie_list_destroy(list->next);
- free(list->next);
- free(list->item.name);
- free(list->item.data);
- free(list->item.path);
- free(list->item.domain);
- free(list->item.max_age);
+ if (cookies) {
+ magi_cookies_free(cookies->next);
+ free(cookies->next);
+ free(cookies->item.name);
+ free(cookies->item.data);
+ free(cookies->item.path);
+ free(cookies->item.domain);
+ free(cookies->item.max_age);
}
}
diff --git a/src/cookies.h b/src/cookies.h
index c336cf7..b83955d 100644
--- a/src/cookies.h
+++ b/src/cookies.h
@@ -4,7 +4,7 @@
#include "request.h"
-void magi_cookies(magi_request *request, const char *data);
+void magi_parse_cookies(magi_request *request, const char *data);
#endif
diff --git a/src/file.c b/src/file.c
index 9bf70c4..dd4802a 100644
--- a/src/file.c
+++ b/src/file.c
@@ -4,37 +4,35 @@
#include <string.h>
-int magi_file_list_add(struct magi_file_list ** list, struct magi_file * item)
+void magi_files_add(magi_files **files, magi_file *newitem)
{
- struct magi_file_list * node = malloc(sizeof(*node));
+ magi_files *node = malloc(sizeof(*node));
if (node) {
- node->next = *list;
- node->item = *item;
- *list = node;
+ node->next = *files;
+ node->item = *newitem;
+ *files = node;
}
- return !!node;
}
-struct magi_file * magi_file_list_get(struct magi_file_list * list,
- const char * name)
+magi_file *magi_files_get(magi_files *files, const char *name)
{
- if (!list || !name) {
+ if (!files || !name) {
return 0;
- } else if (!strcmp(list->item.param_name, name)) {
- return &list->item;
+ } else if (!strcmp(files->item.field, name)) {
+ return &files->item;
} else {
- return magi_file_list_get(list->next, name);
+ return magi_files_get(files->next, name);
}
}
-void magi_file_list_destroy(struct magi_file_list * list)
+void magi_files_free(magi_files *files)
{
- if (list) {
- magi_file_list_destroy(list->next);
- free(list->next);
- free(list->item.param_name);
- free(list->item.file_name);
- magi_param_list_destroy(list->item.params);
- free(list->item.params);
+ if (files) {
+ magi_files_free(files->next);
+ free(files->next);
+ free(files->item.field);
+ free(files->item.filename);
+ magi_params_free(files->item.params);
+ free(files->item.params);
}
}
diff --git a/src/param.c b/src/param.c
index e9f43f8..42785fc 100644
--- a/src/param.c
+++ b/src/param.c
@@ -4,35 +4,33 @@
#include <string.h>
-int magi_param_list_add(struct magi_param_list ** list,
- struct magi_param * item)
+void magi_params_add(magi_params **params, magi_param *newitem)
{
- struct magi_param_list * node = malloc(sizeof(*node));
+ magi_params *node = malloc(sizeof(*node));
if (node) {
- node->next = *list;
- node->item = *item;
- *list = node;
+ node->next = *params;
+ node->item = *newitem;
+ *params = node;
}
- return !!node;
}
-char * magi_param_list_get(struct magi_param_list * list, const char * name)
+char *magi_params_get(magi_params *params, const char *name)
{
- if (!list || !name) {
+ if (!params || !name) {
return 0;
- } else if (!strcmp(list->item.name, name)) {
- return list->item.data;
+ } else if (!strcmp(params->item.name, name)) {
+ return params->item.data;
} else {
- return magi_param_list_get(list->next, name);
+ return magi_params_get(params->next, name);
}
}
-void magi_param_list_destroy(struct magi_param_list * list)
+void magi_params_free(magi_params *params)
{
- if (list) {
- magi_param_list_destroy(list->next);
- free(list->next);
- free(list->item.name);
- free(list->item.data);
+ if (params) {
+ magi_params_free(params->next);
+ free(params->next);
+ free(params->item.name);
+ free(params->item.data);
}
}
diff --git a/src/request.c b/src/request.c
index 3677eb7..2917508 100644
--- a/src/request.c
+++ b/src/request.c
@@ -1,80 +1,96 @@
#include "request.h"
-#include "cookie.h"
-#include "file.h"
-#include "param.h"
#include <stdlib.h>
-void magi_request_setup(struct magi_request * request)
+void magi_request_init(magi_request *request)
{
if (request) {
- request->file_callback = 0;
- request->file_callback_userdata = 0;
- request->file_callback_addon_max = 64;
- request->cookies_max = 1024 * 8;
- request->url_params_max = 1024 * 2;
- request->http_params_max = 1024 * 16;
- request->params_max = 0;
+ request->callback.act = 0;
+ request->callback.userdata = 0;
+ request->callback.addon_max = 1024;
+ request->limits.cookies = 1024 * 16;
+ request->limits.params_meta = 1024 * 16;
+ request->limits.params_head = 1024 * 16;
+ request->limits.params_body = 1024 * 16;
}
}
-static void request_free(struct magi_request * request)
+static void request_free(magi_request *request)
{
free(request->cookies);
- free(request->http_params);
- free(request->url_params);
- free(request->params);
+ free(request->meta);
+ free(request->head);
+ free(request->body);
free(request->files);
free(request->method);
- free(request->uri);
- free(request->document_root);
- free(request->document_uri);
- free(request->script_name);
- free(request->script_filename);
- free(request->remote_addr);
- free(request->remote_port);
- free(request->server_addr);
- free(request->server_name);
- free(request->server_port);
- free(request->server_protocol);
- free(request->server_software);
- free(request->path_info);
+ free(request->address);
+ free(request->script);
+ free(request->path);
}
-static void request_annul(struct magi_request * request)
+static void request_annul(magi_request *request)
{
- request->cookies = 0;
- request->http_params = 0;
- request->url_params = 0;
- request->params = 0;
- request->files = 0;
- request->method = 0;
- request->uri = 0;
- request->document_root = 0;
- request->document_uri = 0;
- request->script_name = 0;
- request->script_filename = 0;
- request->remote_addr = 0;
- request->remote_port = 0;
- request->server_addr = 0;
- request->server_name = 0;
- request->server_port = 0;
- request->server_protocol = 0;
- request->server_software = 0;
- request->path_info = 0;
+ request->cookies = 0;
+ request->meta = 0;
+ request->head = 0;
+ request->body = 0;
+ request->files = 0;
+ request->method = 0;
+ request->address = 0;
+ request->script = 0;
+ request->path = 0;
}
-void magi_request_destroy(struct magi_request * request)
+void magi_request_free(magi_request *request)
{
if (request) {
- magi_cookie_list_destroy(request->cookies);
- magi_param_list_destroy(request->http_params);
- magi_param_list_destroy(request->url_params);
- magi_param_list_destroy(request->params);
- magi_file_list_destroy(request->files);
+ magi_cookies_free(request->cookies);
+ magi_params_free(request->meta);
+ magi_params_free(request->head);
+ magi_params_free(request->body);
+ magi_files_free(request->files);
request_free(request);
request_annul(request);
}
}
+
+
+char *magi_request_meta(magi_request *r, const char *name)
+{
+ return magi_params_get(r->meta, name);
+}
+
+char *magi_request_param(magi_request *r, const char *name)
+{
+ char *res = magi_params_get(r->body, name);
+ if (!res) {
+ return magi_params_get(r->head, name);
+ }
+ return res;
+}
+
+char *magi_request_urlparam(magi_request *r, const char *name)
+{
+ return magi_params_get(r->head, name);
+}
+
+magi_file *magi_request_file(magi_request *r, const char *name)
+{
+ return magi_files_get(r->files, name);
+}
+
+char *magi_request_cookie(magi_request *r, const char *name)
+{
+ magi_cookie *res = magi_cookies_get(r->cookies, name);
+ if (!res) {
+ return 0;
+ }
+ return res->data;
+}
+
+magi_cookie *magi_request_cookie_complex(magi_request *r, const char *name)
+{
+ return magi_cookies_get(r->cookies, name);
+}
diff --git a/src/response.c b/src/response.c
index 904b855..403e0fe 100644
--- a/src/response.c
+++ b/src/response.c
@@ -6,86 +6,132 @@
#include <string.h>
-void magi_response_setup(magi_response *response)
+void magi_response_status(magi_request *r, int code, const char *description)
{
- response->cookies = 0;
- response->http_params = 0;
- response->content_type = 0;
- magi_response_content_type(response, "application/xhtml+xml");
- response->content = magi_str_create(0);
- response->len = 0;
- response->size = 1;
+ int dlen;
+ magi_param addon;
+ if (r->response->head_done || code <= 99 || 600 <= code) {
+ return;
+ }
+ dlen = strlen(description);
+ addon.name = magi_str_create_copy("Status", 6);
+ addon.data = malloc(5 + dlen);
+ addon.data[0] = '0' + code / 100;
+ addon.data[1] = '0' + code / 10 % 10;
+ addon.data[2] = '0' + code % 10;
+ addon.data[3] = ' ';
+ memcpy(addon.data + 4, description, dlen + 1);
+ magi_params_set(r->response->head, &addon);
}
-void magi_response_content_type(magi_response *response, const char *type)
+void magi_response_cookie(magi_request *r, const char *name, const char *data)
{
- static const char *const ct = "Content-Type: ";
- static const int ctlen = 15;
- const int len = strlen(type);
- free(response->content_type);
- response->content_type = malloc(ctlen + len + 1);
- memcpy(response->content_type, ct, ctlen);
- memcpy(response->content_type + ctlen, type, len + 1);
+ magi_param addon;
+ if (r->response->head_done) {
+ return;
+ }
+ addon.name = magi_str_create_copy("Set-Cookie", 10);
+ /* TODO */
+ magi_params_add(r->response->head + 1, &addon);
}
-void magi_response_add(magi_response *r, const char *addon)
+void magi_response_cookie_complex(magi_request *r, magi_cookie *c)
{
- int addon_len;
- if (!addon) {
+ magi_param addon;
+ if (r->response->head_done) {
return;
}
- addon_len = strlen(addon);
- if (r->len + addon_len + 1 >= r->size) {
- r->content = realloc(r->content, r->len + addon_len + 1);
- }
- memcpy(r->content + r->len, addon, addon_len + 1);
- r->len += addon_len;
+ addon.name = magi_str_create_copy("Set-Cookie", 10);
+ /* TODO */
+ magi_params_add(r->response->head + 1, &addon);
}
-void magi_response_cookie(magi_response *response, magi_cookie *cookie)
+void magi_response_cookie_discard(magi_request *r, const char *name)
{
- magi_cookie_list_add(&response->cookies, cookie);
+ magi_param addon;
+ if (r->response->head_done) {
+ return;
+ }
+ addon.name = magi_str_create_copy("Set-Cookie", 10);
+ /* TODO */
+ magi_params_add(r->response->head + 1, &addon);
}
-void magi_response_cookie_easy(magi_response *response,
- const char *name,
- const char *value)
+void magi_response_header(magi_request *r, const char *name, const char *data)
{
- magi_cookie cookie = { 0, 0, 0, 0, 0 };
- cookie.name = magi_str_create_copy(name, strlen(name));
- cookie.data = magi_str_create_copy(value, strlen(value));
- magi_cookie_list_add(&response->cookies, &cookie);
+ 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 + 1, &addon);
}
-void magi_response_cookie_discard(magi_response *response, const char *name)
+void magi_response_content_length(magi_request *r, int length)
{
- magi_cookie cookie = { 0, 0, 0, 0, 0 };
- cookie.name = magi_str_create_copy(name, strlen(name));
- cookie.max_age = magi_str_create(1);
- cookie.max_age[0] = '0';
- magi_cookie_list_add(&response->cookies, &cookie);
+ magi_param addon;
+ int len;
+ if (r->response->head_done) {
+ return;
+ }
+ addon.name = magi_str_create_copy("Content-Length", 14);
+ len = 1;
+ addon.data = malloc(len + 1);
+ *addon.data = '0' + length % 10;
+ while (length /= 10) {
+ addon.data = realloc(addon.data, len + 2);
+ addon.data[len] = '0' + length % 10;
+ ++len;
+ }
+ addon.data[len] = 0;
+ magi_params_set(r->response->head + 2, &addon);
}
-void magi_response_http(magi_response *response,
- const char *name,
- const char *data)
+void magi_response_content_type(magi_request *r, const char *type)
{
- magi_param param = { 0, 0 };
- param.name = magi_str_create_copy(name, strlen(name));
- param.data = magi_str_create_copy(data, strlen(data));
- magi_param_list_add(&response->http_params, &param);
+ 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 + 2, &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_destroy(magi_response *response)
+void magi_response_head(magi_request *r)
{
- if (!response) {
+ magi_params *current;
+ if (r->response->head_done) {
return;
}
- magi_cookie_list_destroy(response->cookies);
- magi_param_list_destroy(response->http_params);
- free(response->cookies);
- free(response->http_params);
- free(response->content_type);
- free(response->content);
+ response_headers(r->response, r->response->head[0]);
+ response_headers(r->response, r->response->head[1]);
+ response_headers(r->response, r->response->head[2]);
+ r->response->head_done = 1;
+}
+
+void magi_response(magi_request *r, const char *addon)
+{
+ magi_response_head(r);
+ r->response->methods->body(r->response->userdata, addon, strlen(addon));
+}
+
+void magi_response_format(magi_request *r, const char *format, ...)
+{
+ magi_response_head(r);
+ /* TODO */
+}
+
+void magi_response_file(magi_request *r, FILE *file)
+{
+ magi_response_head(r);
+ r->response->methods->file(r->response->userdata, file);
}