diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2020-02-06 19:43:57 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2020-02-06 19:43:57 +0300 |
commit | 83cd2267b309fa7411acaed356f88101196bb2ad (patch) | |
tree | 8ab5f2ebdfbbc5020f13329858d6364d82beabd9 /src | |
parent | 37cd01d27efc7988548dbfde307e41be7d4f41a9 (diff) | |
download | magi-83cd2267b309fa7411acaed356f88101196bb2ad.tar magi-83cd2267b309fa7411acaed356f88101196bb2ad.tar.xz magi-83cd2267b309fa7411acaed356f88101196bb2ad.zip |
[magi] Broken to become better.
Diffstat (limited to 'src')
-rw-r--r-- | src/cgi.c | 12 | ||||
-rw-r--r-- | src/cgi.h | 34 | ||||
-rw-r--r-- | src/cookie.h | 32 | ||||
-rw-r--r-- | src/cookies.c (renamed from src/inner_cookies.c) | 0 | ||||
-rw-r--r-- | src/cookies.h (renamed from src/inner_cookies.h) | 0 | ||||
-rw-r--r-- | src/error.h | 21 | ||||
-rw-r--r-- | src/fastcgi.h | 10 | ||||
-rw-r--r-- | src/file.h | 30 | ||||
-rw-r--r-- | src/magi.h | 16 | ||||
-rw-r--r-- | src/multipart.c (renamed from src/inner_multipart.c) | 0 | ||||
-rw-r--r-- | src/multipart.h (renamed from src/inner_multipart.h) | 0 | ||||
-rw-r--r-- | src/param.h | 28 | ||||
-rw-r--r-- | src/request.h | 117 | ||||
-rw-r--r-- | src/response.c | 24 | ||||
-rw-r--r-- | src/response.h | 43 | ||||
-rw-r--r-- | src/tools.c (renamed from src/inner_tools.c) | 0 | ||||
-rw-r--r-- | src/tools.h (renamed from src/inner_tools.h) | 10 | ||||
-rw-r--r-- | src/urlenc.h | 10 | ||||
-rw-r--r-- | src/urlencoded.c (renamed from src/inner_urlencoded.c) | 0 | ||||
-rw-r--r-- | src/urlencoded.h (renamed from src/inner_urlencoded.h) | 0 |
20 files changed, 9 insertions, 378 deletions
@@ -274,21 +274,15 @@ void output_cookies(struct magi_cookie_list * list) } -int magi_response_cgi(struct magi_response * response) +int magi_response_cgi(struct magi_response *response) { output_http_params(response->http_params); output_cookies(response->cookies); fputs(response->content_type, stdout); fputs("\r\n\r\n", stdout); fputs(response->content, stdout); - return 1; -} - -int magi_response_cgi_clear(magi_response *response) -{ - int ok = magi_response_cgi(response); magi_response_destroy(response); - return ok; + return 1; } @@ -308,5 +302,5 @@ int magi_error_cgi(enum magi_error error) "</body>" "</html>", magi_error_message(error)); - return magi_response_cgi_clear(&res); + return magi_response_cgi(&res); } diff --git a/src/cgi.h b/src/cgi.h deleted file mode 100644 index 7e47ad0..0000000 --- a/src/cgi.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef MAGI_INCLUDED_CGI -#define MAGI_INCLUDED_CGI - -#include "error.h" -#include "request.h" -#include "response.h" - - -/* Constructs non-post part of request from environment; - * Returns null only in case of error. */ -int magi_request_cgi(magi_request *request); - -/* Complete request with post data from standard input; - * Returns null only in case of error. */ -int magi_request_resume_cgi(magi_request *request); - -/* Shortcut for applying both previous. */ -int magi_request_full_cgi(magi_request *request); - - -/* Sends response to standard output; - * Returns null only in case of error. */ -int magi_response_cgi(magi_response *response); - -/* Shortcut for magi_response_cgi with desctruction after it. */ -int magi_response_cgi_clear(magi_response *response); - - -/* Sends a standart response of Bad Request error to standard output; - * Returns null only in case of error. */ -int magi_error_cgi(magi_error error); - - -#endif diff --git a/src/cookie.h b/src/cookie.h deleted file mode 100644 index 7eb3e9d..0000000 --- a/src/cookie.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MAGI_INCLUDED_COOKIE -#define MAGI_INCLUDED_COOKIE - - -typedef struct magi_cookie { - /* All pointers must be valid as 'free' arguments. */ - char *name; - char *data; - char *path; /* Without '/' at the end. */ - char *domain; /* With dot at the begining. */ - char *max_age; /* In seconds until discard, used only in response. */ -} magi_cookie; - -typedef struct magi_cookie_list { - struct magi_cookie_list *next; /* Must be valid as 'free' arguments. */ - magi_cookie item; -} magi_cookie_list; - - -/* Addition of item to top of list. Null <=> error. */ -int magi_cookie_list_add(magi_cookie_list **list, magi_cookie *item); - -/* Data of last cookie in list: cookie.name == name, returns null if no such; - * Last cookie in list is first from request, and from RFC 2109 we know that - * first cookie is the most accurate for request's domain and path. */ -char *magi_cookie_list_get(magi_cookie_list *list, const char *name); - -/* Freeing and invalidation of list. */ -void magi_cookie_list_destroy(magi_cookie_list *list); - - -#endif diff --git a/src/inner_cookies.c b/src/cookies.c index 9dfde7a..9dfde7a 100644 --- a/src/inner_cookies.c +++ b/src/cookies.c diff --git a/src/inner_cookies.h b/src/cookies.h index c336cf7..c336cf7 100644 --- a/src/inner_cookies.h +++ b/src/cookies.h diff --git a/src/error.h b/src/error.h deleted file mode 100644 index 4a91962..0000000 --- a/src/error.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef MAGI_INCLUDED_ERROR -#define MAGI_INCLUDED_ERROR - - -typedef enum magi_error { - magi_error_none = 0, - magi_error_nobound, - magi_error_unknown, - magi_error_notype, - magi_error_length, - magi_error_memory, - magi_error_cookies, - magi_error_urlenc, - magi_error_multipart, - magi_error_limit -} magi_error; - -const char *magi_error_message(magi_error error); - - -#endif diff --git a/src/fastcgi.h b/src/fastcgi.h deleted file mode 100644 index 0abd152..0000000 --- a/src/fastcgi.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MAGI_INCLUDED_FASTCGI -#define MAGI_INCLUDED_FASTCGI - - -/* * * TODO * * */ - -enum { magi_to_avoid_warning }; - - -#endif diff --git a/src/file.h b/src/file.h deleted file mode 100644 index 4705031..0000000 --- a/src/file.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef MAGI_INCLUDED_FILE -#define MAGI_INCLUDED_FILE - -#include "param.h" - - -typedef struct magi_file { - /* All pointers must be valid as 'free' arguments. */ - char *param_name; /* Name of corresponding form field */ - char *file_name; /* File name on user's computer */ - magi_param_list *params; /* Multipart params (e.g. type) */ -} magi_file; - -typedef struct magi_file_list { - struct magi_file_list *next; /* Must be valid as 'free' argument. */ - magi_file item; -} magi_file_list; - - -/* Addition of item to top of list. Null <=> error. */ -int magi_file_list_add(magi_file_list **list, magi_file *item); - -/* First node in list: node.param_name == name; else null. */ -magi_file *magi_file_list_get(magi_file_list *list, const char *name); - -/* Freeing and invalidation of list. */ -void magi_file_list_destroy(magi_file_list *list); - - -#endif diff --git a/src/magi.h b/src/magi.h deleted file mode 100644 index b7df63c..0000000 --- a/src/magi.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef MAGI_INCLUDED -#define MAGI_INCLUDED -/* Magi Header Collection - * You can include this, if you don't want to think about headers. - */ -#include "cgi.h" -#include "cookie.h" -#include "error.h" -#include "fastcgi.h" -#include "file.h" -#include "param.h" -#include "request.h" -#include "response.h" -#include "urlenc.h" - -#endif diff --git a/src/inner_multipart.c b/src/multipart.c index 0187f7c..0187f7c 100644 --- a/src/inner_multipart.c +++ b/src/multipart.c diff --git a/src/inner_multipart.h b/src/multipart.h index c3f7b65..c3f7b65 100644 --- a/src/inner_multipart.h +++ b/src/multipart.h diff --git a/src/param.h b/src/param.h deleted file mode 100644 index bebacd7..0000000 --- a/src/param.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef MAGI_INCLUDED_PARAM -#define MAGI_INCLUDED_PARAM - - -typedef struct magi_param { - /* All pointers must be valid as 'free' arguments. */ - char *name; - char *data; -} magi_param; - -typedef struct magi_param_list { - struct magi_param_list *next; /* Must be valid as 'free' argument. */ - magi_param item; -} magi_param_list; - - -/* Addition of item to top of list. Null <=> error. */ -int magi_param_list_add(magi_param_list **list, - magi_param *item); - -/* Data of the first node in list: node.name == name; else null. */ -char *magi_param_list_get(magi_param_list *list, const char *name); - -/* Freeing and invalidation of list. */ -void magi_param_list_destroy(magi_param_list *list); - - -#endif diff --git a/src/request.h b/src/request.h deleted file mode 100644 index 18d6c8e..0000000 --- a/src/request.h +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef MAGI_INCLUDED_REQUEST -#define MAGI_INCLUDED_REQUEST - -#include "cookie.h" -#include "error.h" -#include "file.h" -#include "param.h" - - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Request - * - * Can be created via 'magi_request_{gateway interface name}', but will have - * nullified 'post'-related fields ('params' & 'files'). Reason is unlimited - * 'post' body size, with possible dependence of wanted limits from data of - * headers (e.g. session id from cookies, enabling some users to load more). - * To proceed 'post' use 'magi_request_resume_{gateway interface name}', - * specifying settings if necessary. - * - * - * Cheatsheet on environment: - * - * Request URL: http[s]://{server_name}[:{server_port}]{uri} - * example.com 80 - * uri: {document_uri}[?{urlencode(url_params)}] - * alfar=9973&veles=on - * document_uri: {script_name}{path_info} - * /bin/script /article/magic - */ -typedef struct magi_request { - /* * * Results * * */ - /* All pointers of this section must be valid as 'free' arguments. */ - - /* Parsed */ - magi_cookie_list *cookies; - magi_param_list *http_params; /* HTTP Header parameters */ - magi_param_list *url_params; /* Paramteres from URL */ - magi_param_list *params; /* Parameters from 'post' body */ - magi_file_list *files; /* 'Post' multipart files */ - - /* Environment Shortcuts */ - char *method; /* REQUEST_METHOD */ - char *uri; /* REQUEST_URI */ - char *document_root; /* DOCUMENT_ROOT */ - char *document_uri; /* DOCUMENT_URI */ - char *script_name; /* SCRIPT_NAME */ - char *script_filename; /* SCRIPT_FILENAME */ - char *remote_addr; /* REMOTE_ADDR */ - char *remote_port; /* REMOTE_PORT */ - char *server_addr; /* SERVER_ADDR */ - char *server_name; /* SERVER_NAME */ - /* server_name can be not a domain name, even if request is done with it. - * (Use http_params["HTTP_HOST"] instead.) */ - char *server_port; /* SERVER_PORT */ - char *server_protocol; /* SERVER_PROTOCOL */ - char *server_software; /* SERVER_COFTWARE */ - char *path_info; /* PATH_INFO */ - - /* Request Error Code */ - magi_error error; - - - /* * * Settings * * */ - - /* Callback for processing files */ - void (*file_callback)(magi_file *file, - char *addon, - int addon_len, - int is_addon_last, - void *userdata); - void *file_callback_userdata; - int file_callback_addon_max; - - /* Limits for memory used (null <=> unlimitted) */ - int cookies_max; - int url_params_max; - int http_params_max; - int params_max; -} magi_request; - - -/* Setup request with default settings. */ -void magi_request_setup(magi_request *request); - - -/* Destroys request. */ -void magi_request_destroy(magi_request *request); - - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Tempfiles Callback - */ -typedef struct magi_tempfile { - const char *param_name; /* Form field name, in which file is expected. */ - const char *location; /* Location to load file in. */ - int maximum; /* Null <=> unlimited. */ -} magi_tempfile; - -typedef struct magi_tempfiles { - int count; - magi_tempfile *tmps; -} magi_tempfiles; - -void magi_tempfiles_add(magi_tempfiles *tmps, - const char *name, - const char *path, - int max); - -void magi_tempfiles_destroy(magi_tempfiles *tmps); - -/* Setup request callback with files loaded into corresponding to their - * parameter names locations; paths are in magi_tempfiles struct. */ -void magi_request_setup_tempfiles(magi_request *request, - magi_tempfiles *table); - - -#endif diff --git a/src/response.c b/src/response.c index 9570cd1..904b855 100644 --- a/src/response.c +++ b/src/response.c @@ -1,14 +1,10 @@ #include "response.h" -#include "inner_tools.h" -#include <stdarg.h> +#include "tools.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -/* TODO: realise vsnprintf or eliminate need of it. (see musl) */ -int vsnprintf(char *s, size_t n, const char *format, va_list arg); - void magi_response_setup(magi_response *response) { @@ -46,24 +42,6 @@ void magi_response_add(magi_response *r, const char *addon) r->len += addon_len; } -void magi_response_add_format(magi_response *response, const char *addon, ...) -{ - char *act_addon; - int n; - va_list args; - va_start(args, addon); - n = vsnprintf(0, 0, addon, args); - va_end(args); - if (n >= 0) { - act_addon = malloc(n + 1); - va_start(args, addon); - vsnprintf(act_addon, n + 1, addon, args); - va_end(args); - magi_response_add(response, act_addon); - free(act_addon); - } -} - void magi_response_cookie(magi_response *response, magi_cookie *cookie) { magi_cookie_list_add(&response->cookies, cookie); diff --git a/src/response.h b/src/response.h deleted file mode 100644 index a51d4a4..0000000 --- a/src/response.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef MAGI_INCLUDED_RESPONSE -#define MAGI_INCLUDED_RESPONSE - -#include "cookie.h" -#include "param.h" - - -typedef struct magi_response { - magi_cookie_list *cookies; - magi_param_list *http_params; - char *content_type; - char *content; - int len; - int size; -} magi_response; - - -void magi_response_setup(magi_response *response); - - -void magi_response_content_type(magi_response *response, const char *type); - -void magi_response_add(magi_response *response, const char *addon); -void magi_response_add_format(magi_response *response, const char *addon, ...); - -void magi_response_cookie(magi_response *response, magi_cookie *cookie); - -void magi_response_cookie_easy(magi_response *response, - const char *name, - const char *value); - -void magi_response_cookie_discard(magi_response *response, - const char *name); - -void magi_response_http(magi_response *response, - const char *name, - const char *data); - - -void magi_response_destroy(magi_response *response); - - -#endif diff --git a/src/inner_tools.c b/src/tools.c index d958851..d958851 100644 --- a/src/inner_tools.c +++ b/src/tools.c diff --git a/src/inner_tools.h b/src/tools.h index 87489c8..8d66131 100644 --- a/src/inner_tools.h +++ b/src/tools.h @@ -1,8 +1,8 @@ -#ifndef MAGI_INCLUDED_INNER_TOOLS -#define MAGI_INCLUDED_INNER_TOOLS -/* Magi Inner Tools - * This file must not be included in header files intended for external use, - * it contains some common utility functions for use in source files. +#ifndef MAGI_INCLUDED_TOOLS +#define MAGI_INCLUDED_TOOLS +/* * Collection of helpful functions for internal use. + * + * blah... */ diff --git a/src/urlenc.h b/src/urlenc.h deleted file mode 100644 index edaec08..0000000 --- a/src/urlenc.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MAGI_INCLUDED_URLENC -#define MAGI_INCLUDED_URLENC - - -/* 'code' from urlenc must have size of at least magi_urlenc_size(plain). */ -int magi_urlenc_size(const char *plain); -void magi_urlenc(const char *plain, char *code); - - -#endif diff --git a/src/inner_urlencoded.c b/src/urlencoded.c index e3ea56a..e3ea56a 100644 --- a/src/inner_urlencoded.c +++ b/src/urlencoded.c diff --git a/src/inner_urlencoded.h b/src/urlencoded.h index e07ed6f..e07ed6f 100644 --- a/src/inner_urlencoded.h +++ b/src/urlencoded.h |