From 8acbd33a82d2c13e70eb17447bc6abfd86cf9512 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 20 Mar 2020 23:06:46 +0300 Subject: [magi] --- src/cgi.c | 7 +++--- src/cookie.c | 5 ++-- src/fastcgi.c | 22 ++++++++++++++++ src/file.c | 2 +- src/loadfile.c | 78 --------------------------------------------------------- src/loadfiles.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/multipart.c | 8 +++--- src/param.c | 2 +- src/request.c | 15 +++++------ src/session.c | 40 +++++++++++++++++++++++++++++ 10 files changed, 160 insertions(+), 97 deletions(-) delete mode 100644 src/loadfile.c create mode 100644 src/loadfiles.c create mode 100644 src/session.c (limited to 'src') diff --git a/src/cgi.c b/src/cgi.c index 0fd258e..7b0551d 100644 --- a/src/cgi.c +++ b/src/cgi.c @@ -161,9 +161,8 @@ static void mhead(void *any, magi_param *header) fputs("\r\n", stdout); } -static void mstart_body(void *any) +static void mstart_body() { - (void)any; fputs("\r\n", stdout); } @@ -189,7 +188,7 @@ static void mfile(void *any, FILE *file) } } -static void mclose(void *any) { (void)any; } +static void mclose() {} static void setup_response(magi_request *r) { @@ -208,7 +207,7 @@ static void setup_response(magi_request *r) r->response->head_general = 0; r->response->head_entity = 0; r->response->head_done = 0; - magi_response_content_type(r, "application/xhtml+xml"); + magi_response_content_type(r, "text/html"); magi_response_status(r, 200, "OK"); } diff --git a/src/cookie.c b/src/cookie.c index 7b40f1a..a64f5cb 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -14,9 +14,10 @@ void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem) } } -magi_cookie *magi_cookies_get(magi_cookies *cookies, const char *name) +const magi_cookie *magi_cookies_get(const magi_cookies *cookies, + const char *name) { - magi_cookie *res = 0; + const magi_cookie *res = 0; if (!cookies || !name) { return 0; } diff --git a/src/fastcgi.c b/src/fastcgi.c index 39c57ca..8959c14 100644 --- a/src/fastcgi.c +++ b/src/fastcgi.c @@ -1 +1,23 @@ #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/file.c b/src/file.c index dd4802a..7e3d2f8 100644 --- a/src/file.c +++ b/src/file.c @@ -14,7 +14,7 @@ void magi_files_add(magi_files **files, magi_file *newitem) } } -magi_file *magi_files_get(magi_files *files, const char *name) +const magi_file *magi_files_get(const magi_files *files, const char *name) { if (!files || !name) { return 0; diff --git a/src/loadfile.c b/src/loadfile.c deleted file mode 100644 index d76f562..0000000 --- a/src/loadfile.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "loadfile.h" - -#include -#include -#include - - -void magi_loadfiles_add(magi_loadfiles *table, - const char *name, - const char *path, - int max) -{ - static const int size = sizeof(*table->files); - if (!table) { - return; - } - if (table->count) { - table->files = realloc(table->files, size * table->count + size); - } else { - table->files = malloc(size); - } - table->files[table->count].name = name; - table->files[table->count].path = path; - table->files[table->count].max = max; - table->count++; -} - -void magi_loadfiles_free(magi_loadfiles *table) -{ - if (!table) { - return; - } - free(table->files); - table->count = 0; -} - -static void loadfiles(void *userdata, - magi_file *file, - char *addon, - int addon_len) -{ - magi_loadfiles *table = userdata; - int pos; - if (!file->filename || !strcmp(file->filename, "")) { - return; - } - for (pos = 0; pos != table->count; ++pos) { - if (!strcmp(table->files[pos].name, file->field)) { - static FILE *f = 0; - static int unlimited; - static int left; - if (!f) { - const char *path = table->files[pos].path; - f = fopen(path, "wb"); - left = table->files[pos].max; - unlimited = !left; - } - if (unlimited) { - fwrite(addon, 1, addon_len, f); - } else { - int min = left < addon_len ? left : addon_len; - fwrite(addon, 1, min, f); - left -= min; - } - if (!addon_len) { - fclose(f); - f = 0; - } - return; - } - } -} - -void magi_loadfiles_set(magi_request *request, magi_loadfiles *table) -{ - request->callback.act = loadfiles; - request->callback.userdata = table; -} diff --git a/src/loadfiles.c b/src/loadfiles.c new file mode 100644 index 0000000..b3efff7 --- /dev/null +++ b/src/loadfiles.c @@ -0,0 +1,78 @@ +#include "loadfiles.h" + +#include +#include +#include + + +void magi_loadfiles_add(magi_loadfiles *table, + const char *name, + const char *path, + int max) +{ + static const int size = sizeof(*table->files); + if (!table) { + return; + } + if (table->count) { + table->files = realloc(table->files, size * table->count + size); + } else { + table->files = malloc(size); + } + table->files[table->count].name = name; + table->files[table->count].path = path; + table->files[table->count].max = max; + table->count++; +} + +void magi_loadfiles_free(magi_loadfiles *table) +{ + if (!table) { + return; + } + free(table->files); + table->count = 0; +} + +static void loadfiles(void *userdata, + magi_file *file, + char *addon, + int addon_len) +{ + magi_loadfiles *table = userdata; + int pos; + if (!file->filename || !strcmp(file->filename, "")) { + return; + } + for (pos = 0; pos != table->count; ++pos) { + if (!strcmp(table->files[pos].name, file->field)) { + static FILE *f = 0; + static int unlimited; + static int left; + if (!f) { + const char *path = table->files[pos].path; + f = fopen(path, "wb"); + left = table->files[pos].max; + unlimited = !left; + } + if (unlimited) { + fwrite(addon, 1, addon_len, f); + } else { + int min = left < addon_len ? left : addon_len; + fwrite(addon, 1, min, f); + left -= min; + } + if (!addon_len) { + fclose(f); + f = 0; + } + return; + } + } +} + +void magi_loadfiles_set(magi_request *request, magi_loadfiles *table) +{ + request->callback.act = loadfiles; + request->callback.userdata = table; +} diff --git a/src/multipart.c b/src/multipart.c index 2284ced..d2a1c7f 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -63,10 +63,10 @@ static char *extract_filename(char *n) if (!n) { return 0; } - n += strspn(n, " \t") + 1; + n += strspn(n + 1, " \t") + 1; if (*n == '"') { ++n; - return magi_str_create_copy(n, n - strchr(n, '"')); + return magi_str_create_copy(n, strchr(n, '"') - n); } else { return magi_str_create_copy(n, strcspn(n, " \t")); } @@ -78,10 +78,10 @@ static int content_disposition(automata *a) if (!n) { return 0; } - n += strspn(n, " \t") + 1; + n += strspn(n + 1, " \t") + 1; if (*n == '"') { ++n; - a->param.name = magi_str_create_copy(n, n - strchr(n, '"')); + a->param.name = magi_str_create_copy(n, strchr(n, '"') - n); if (!a->param.name || !*a->param.name) { return 0; } diff --git a/src/param.c b/src/param.c index 9c94555..dcf33d4 100644 --- a/src/param.c +++ b/src/param.c @@ -28,7 +28,7 @@ void magi_params_set(magi_params **params, magi_param *newitem) } } -char *magi_params_get(magi_params *params, const char *name) +char *magi_params_get(const magi_params *params, const char *name) { if (!params || !name) { return 0; diff --git a/src/request.c b/src/request.c index ce0f84f..d80bf20 100644 --- a/src/request.c +++ b/src/request.c @@ -69,12 +69,12 @@ void magi_request_free(magi_request *request) } -char *magi_request_meta(magi_request *r, const char *name) +char *magi_request_meta(const magi_request *r, const char *name) { return magi_params_get(r->meta, name); } -char *magi_request_param(magi_request *r, const char *name) +char *magi_request_param(const magi_request *r, const char *name) { char *res = magi_params_get(r->body, name); if (!res) { @@ -83,26 +83,27 @@ char *magi_request_param(magi_request *r, const char *name) return res; } -char *magi_request_urlparam(magi_request *r, const char *name) +char *magi_request_urlparam(const magi_request *r, const char *name) { return magi_params_get(r->head, name); } -magi_file *magi_request_file(magi_request *r, const char *name) +const magi_file *magi_request_file(const magi_request *r, const char *name) { return magi_files_get(r->files, name); } -char *magi_request_cookie(magi_request *r, const char *name) +char *magi_request_cookie(const magi_request *r, const char *name) { - magi_cookie *res = magi_cookies_get(r->cookies, name); + const 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) +const magi_cookie *magi_request_cookie_complex(const magi_request *r, + const char *name) { return magi_cookies_get(r->cookies, name); } diff --git a/src/session.c b/src/session.c new file mode 100644 index 0000000..35e9200 --- /dev/null +++ b/src/session.c @@ -0,0 +1,40 @@ +#include "session.h" + +#include +#include +#include +#include + + +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; + s->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = inet_addr(address); + return !connect(s->socket, (struct sockaddr *)&addr, sizeof(addr)); +} + +int magi_session_unix(magi_session *s, const char *path) +{ + struct sockaddr_un addr; + s->socket = socket(AF_UNIX, SOCK_STREAM, IPPROTO_TCP); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); + return !connect(s->socket, (struct sockaddr *)&addr, sizeof(addr)); +} -- cgit v1.2.3