aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--license2
-rw-r--r--readme42
-rw-r--r--src/cgi.c6
-rw-r--r--src/cookies.c2
-rw-r--r--src/inner_tools.c51
-rw-r--r--src/inner_tools.h19
-rw-r--r--src/multipart.c10
-rw-r--r--src/response.c16
-rw-r--r--src/urlenc.c2
-rw-r--r--src/utils.c50
-rw-r--r--src/utils.h15
11 files changed, 131 insertions, 84 deletions
diff --git a/license b/license
index 8f83b9c..d89e508 100644
--- a/license
+++ b/license
@@ -1,4 +1,4 @@
-Copyright 2019 Aleksey Veresov
+Copyright 2019-2020 Aleksey Veresov
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
diff --git a/readme b/readme
new file mode 100644
index 0000000..8cc3b8b
--- /dev/null
+++ b/readme
@@ -0,0 +1,42 @@
+ Description
+Magi Library (libmagi) implements Gateway Interfaces, namely CGI and FastCGI.
+
+ Overview
+Magi is free and open-source software: legal info is in the 'license' file.
+It is written in ANSI C, without any dependecies, except for C standard
+library. All source and header files are located in the 'src' directory.
+Files with 'inner' part in their names are not intended for external use.
+Since filenames in the project are short and simple consider placing
+header files in subdirectory 'magi' of your include.
+
+ Compiling
+Compilation and its options are described in the 'Makefile'.
+Running 'make' produces 'libmagi.a' file, which is precompiled library itself,
+ready to be statically linked into your project.
+
+ Examples
+Compilation and its options are described in the local 'examples/Makefile'.
+Running 'make' in the 'examples' directory produces 'append', 'cookie',
+'upload', and 'echo' CGI executables. Descriptions and details are in
+corresponding source files.
+
+ Usage
+Magi main purpose is to analyse request and provide it in 'struct magi_request'
+variable. It is described in 'src/request.h'; to setup defaults before actual
+processing run 'magi_request_setup'. Next step is to analyse post-unrelated
+part of request via 'magi_request_cgi'. Since post part can be realy big,
+it is generally useful to constrain its analysis biased on the rest of request.
+Post part analyse is done via 'magi_request_resume_cgi'. You can destroy
+request structure via 'magi_request_destroy'.
+
+Also magi provide output capabilities via 'struct magi_response'.
+Similary to request, 'magi_response_setup' is initialisation of response with
+defaults. Content type need to be specified for response, to set it run
+'magi_response_content_type'. You can set cookie with 'magi_response_cookie',
+as well as discard them with 'magi_response_cookie_discard'. You can set HTTP
+headers with 'magi_response_http'. For filling response body use
+'magi_response_add' and 'magi_response_add_format'. To send response call
+'magi_response_cgi'. Desctruction is provided in 'magi_response_destroy'.
+
+ Motivation
+Web must be fun.
diff --git a/src/cgi.c b/src/cgi.c
index 6e8c98e..fac6a4a 100644
--- a/src/cgi.c
+++ b/src/cgi.c
@@ -4,11 +4,11 @@
#include "cookies.h"
#include "error.h"
#include "file.h"
+#include "inner_tools.h"
#include "multipart.h"
#include "param.h"
#include "request.h"
#include "urlenc.h"
-#include "utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -169,9 +169,9 @@ static char * bound(const char * type)
type += strspn(type, " \t") + 1;
if (*type == '"') {
++type;
- res = magi_str_create_copy(type, strchr(type, '"'));
+ res = magi_str_create_copy(type, type - strchr(type, '"'));
} else {
- res = magi_str_create_copy(type, type + strcspn(type, " \t"));
+ res = magi_str_create_copy(type, strcspn(type, " \t"));
}
}
return res;
diff --git a/src/cookies.c b/src/cookies.c
index 0c5560b..f9e84da 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -1,7 +1,7 @@
/* * * TODO * * */
#include "cookies.h"
-#include "utils.h"
+#include "inner_tools.h"
#include <stdlib.h>
#include <string.h>
diff --git a/src/inner_tools.c b/src/inner_tools.c
new file mode 100644
index 0000000..d958851
--- /dev/null
+++ b/src/inner_tools.c
@@ -0,0 +1,51 @@
+#include "inner_tools.h"
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+void magi_str_lowercase(char *str)
+{
+ if (!str) {
+ return;
+ }
+ for (; *str; ++str) {
+ *str = tolower(*str);
+ }
+}
+
+char *magi_str_create_copy(const char *first, int len)
+{
+ char *copy = magi_str_create(len);
+ if (copy) {
+ memcpy(copy, first, len);
+ }
+ return copy;
+}
+
+char *magi_str_create(int len)
+{
+ char *str = malloc(len + 1);
+ if (str) {
+ str[len] = 0;
+ }
+ return str;
+}
+
+int magi_str_add(char **dest, int *len, int *size, char c)
+{
+ if (!*dest) {
+ *dest = magi_str_create(1);
+ } else if (*len + 1 == *size) {
+ *size *= 2;
+ *dest = realloc(*dest, *size);
+ }
+ if (!*dest) {
+ return 0;
+ }
+ (*dest)[*len] = c;
+ ++*len;
+ (*dest)[*len] = 0;
+ return 1;
+}
diff --git a/src/inner_tools.h b/src/inner_tools.h
new file mode 100644
index 0000000..87489c8
--- /dev/null
+++ b/src/inner_tools.h
@@ -0,0 +1,19 @@
+#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.
+ */
+
+
+void magi_str_lowercase(char *str);
+
+/* Results of both create functions are malloced, so need to be freed. */
+char *magi_str_create_copy(const char *first, int len);
+char *magi_str_create(int len);
+
+/* Null only in case of error; if *dest is null creates string. */
+int magi_str_add(char **dest, int *len, int *size, char c);
+
+
+#endif
diff --git a/src/multipart.c b/src/multipart.c
index 7b829d8..aca0fd9 100644
--- a/src/multipart.c
+++ b/src/multipart.c
@@ -2,8 +2,8 @@
#include "multipart.h"
#include "error.h"
+#include "inner_tools.h"
#include "param.h"
-#include "utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -74,9 +74,9 @@ static char * extract_filename(char * n)
n += strspn(n, " \t") + 1;
if (*n == '"') {
++n;
- return magi_str_create_copy(n, strchr(n, '"'));
+ return magi_str_create_copy(n, n - strchr(n, '"'));
} else {
- return magi_str_create_copy(n, n + strcspn(n, " \t"));
+ return magi_str_create_copy(n, strcspn(n, " \t"));
}
}
@@ -89,12 +89,12 @@ static int content_disposition(struct automata * a)
n += strspn(n, " \t") + 1;
if (*n == '"') {
++n;
- a->param.name = magi_str_create_copy(n, strchr(n, '"'));
+ a->param.name = magi_str_create_copy(n, n - strchr(n, '"'));
if (!a->param.name || !*a->param.name) {
return 0;
}
} else {
- a->param.name = magi_str_create_copy(n, n + strcspn(n, " \t"));
+ a->param.name = magi_str_create_copy(n, strcspn(n, " \t"));
if (!a->param.name || !is_str_token(a->param.name)) {
return 0;
}
diff --git a/src/response.c b/src/response.c
index 5e2f326..914f021 100644
--- a/src/response.c
+++ b/src/response.c
@@ -1,6 +1,6 @@
#include "response.h"
-#include "utils.h"
+#include "inner_tools.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -27,8 +27,8 @@ void magi_response_content_type(struct magi_response * response,
"Content-Type: application/xhtml+xml", /* magi_xhtml */
};
if (!response->content_type) {
- const char * end = messages[type] + strlen(messages[type]);
- response->content_type = magi_str_create_copy(messages[type], end);
+ response->content_type = magi_str_create_copy(messages[type],
+ strlen(messages[type]));
}
}
@@ -77,8 +77,8 @@ void magi_response_cookie_easy(struct magi_response * response,
const char * value)
{
struct magi_cookie cookie = { 0, 0, 0, 0, 0 };
- cookie.name = magi_str_create_copy(name, name + strlen(name));
- cookie.data = magi_str_create_copy(value, value + strlen(value));
+ 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);
}
@@ -86,7 +86,7 @@ void magi_response_cookie_discard(struct magi_response * response,
const char * name)
{
struct magi_cookie cookie = { 0, 0, 0, 0, 0 };
- cookie.name = magi_str_create_copy(name, name + strlen(name));
+ 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);
@@ -97,8 +97,8 @@ void magi_response_http(struct magi_response * response,
const char * data)
{
struct magi_param param = { 0, 0 };
- param.name = magi_str_create_copy(name, name + strlen(name));
- param.data = magi_str_create_copy(data, data + strlen(data));
+ 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);
}
diff --git a/src/urlenc.c b/src/urlenc.c
index 8c38a0a..f0a01ca 100644
--- a/src/urlenc.c
+++ b/src/urlenc.c
@@ -1,6 +1,6 @@
#include "urlenc.h"
-#include "utils.h"
+#include "inner_tools.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
diff --git a/src/utils.c b/src/utils.c
deleted file mode 100644
index 931d1e4..0000000
--- a/src/utils.c
+++ /dev/null
@@ -1,50 +0,0 @@
-#include "utils.h"
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-void magi_str_lowercase(char * str)
-{
- if (str) {
- while (*str) {
- *str = tolower(*str);
- ++str;
- }
- }
-}
-
-char * magi_str_create_copy(const char * begin, const char * end)
-{
- char * res;
- res = malloc(end - begin + 1);
- if (res) {
- memcpy(res, begin, end - begin);
- res[end - begin] = 0;
- }
- return res;
-}
-
-char * magi_str_create(int len)
-{
- char * str = malloc(len + 1);
- if (str) {
- str[len] = 0;
- }
- return str;
-}
-
-int magi_str_add(char ** dest, int * len, int * size, char c)
-{
- if (*len + 1 == *size) {
- *size *= 2;
- *dest = realloc(*dest, *size);
- }
- if (*dest) {
- (*dest)[*len] = c;
- ++*len;
- (*dest)[*len] = 0;
- }
- return !!*dest;
-}
diff --git a/src/utils.h b/src/utils.h
deleted file mode 100644
index f67c66c..0000000
--- a/src/utils.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef MAGI_INCLUDED_UTILS
-#define MAGI_INCLUDED_UTILS
-
-
-void magi_str_lowercase(char * str);
-
-/* Results of both create functions are malloced, so need to be freed. */
-char * magi_str_create_copy(const char * begin, const char * end);
-char * magi_str_create(int len);
-
-/* Null only in case of error. */
-int magi_str_add(char ** dest, int * len, int * size, char c);
-
-
-#endif