aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cgi.c19
-rw-r--r--src/cookie.c17
-rw-r--r--src/inner_cookies.c24
-rw-r--r--src/inner_multipart.c10
-rw-r--r--src/inner_tools.c51
-rw-r--r--src/inner_tools.h19
-rw-r--r--src/request.c142
-rw-r--r--src/request.h3
-rw-r--r--src/response.c12
-rw-r--r--src/urlenc.c2
-rw-r--r--src/utils.c50
-rw-r--r--src/utils.h15
12 files changed, 190 insertions, 174 deletions
diff --git a/src/cgi.c b/src/cgi.c
index a0661dc..32b5b4d 100644
--- a/src/cgi.c
+++ b/src/cgi.c
@@ -3,12 +3,11 @@
#include "cookie.h"
#include "error.h"
#include "file.h"
-#include "inner_cookies.h"
-#include "inner_multipart.h"
#include "inner_tools.h"
-#include "inner_urlencoded.h"
+#include "multipart.h"
#include "param.h"
#include "request.h"
+#include "urlenc.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -169,9 +168,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;
@@ -185,6 +184,8 @@ static int next(void * any)
/* Interfacial CGI Request Handling */
int magi_request_cgi(struct magi_request * request)
{
+ request->files = 0;
+ request->params = 0;
request->url_params = 0;
request->http_params = 0;
request->error = magi_error_none;
@@ -197,8 +198,6 @@ int magi_request_cgi(struct magi_request * request)
int magi_request_resume_cgi(struct magi_request * request)
{
enum magi_error * e = &request->error;
- request->files = 0;
- request->params = 0;
request->error = magi_error_none;
if (request->method && !strcmp(request->method, "post")) {
const char * t = getenv("CONTENT_TYPE");
@@ -264,9 +263,9 @@ void output_cookies(struct magi_cookie_list * list)
fputs("; Domain=", stdout);
fputs(list->item.domain, stdout);
}
- if (list->item.port) {
- fputs("; Port=", stdout);
- fputs(list->item.port, stdout);
+ if (list->item.max_age) {
+ fputs("; Max-Age=", stdout);
+ fputs(list->item.max_age, stdout);
}
fputs("\r\n", stdout);
list = list->next;
diff --git a/src/cookie.c b/src/cookie.c
index eaa221f..d1d82b3 100644
--- a/src/cookie.c
+++ b/src/cookie.c
@@ -16,16 +16,19 @@ int magi_cookie_list_add(struct magi_cookie_list ** list,
return !!node;
}
-struct magi_cookie * magi_cookie_list_get(struct magi_cookie_list * list,
- const char * name)
+char * magi_cookie_list_get(struct magi_cookie_list * list, const char * name)
{
+ char * res = 0;
if (!list || !name) {
return 0;
- } else if (!strcmp(list->item.name, name)) {
- return &list->item;
- } else {
- return magi_cookie_list_get(list->next, name);
}
+ while (list) {
+ if (!strcmp(list->item.name, name)) {
+ res = list->item.data;
+ }
+ list = list->next;
+ }
+ return res;
}
void magi_cookie_list_destroy(struct magi_cookie_list * list)
@@ -37,6 +40,6 @@ void magi_cookie_list_destroy(struct magi_cookie_list * list)
free(list->item.data);
free(list->item.path);
free(list->item.domain);
- free(list->item.port);
+ free(list->item.max_age);
}
}
diff --git a/src/inner_cookies.c b/src/inner_cookies.c
index d9035c4..9dfde7a 100644
--- a/src/inner_cookies.c
+++ b/src/inner_cookies.c
@@ -1,7 +1,7 @@
/* * * TODO * * */
#include "inner_cookies.h"
-#include "utils.h"
+#include "inner_tools.h"
#include <stdlib.h>
#include <string.h>
@@ -16,7 +16,7 @@ enum st {
st_post_data
};
-enum data_type { dt_plain = 0, dt_version, dt_path, dt_domain, dt_port };
+enum data_type { dt_plain = 0, dt_version, dt_path, dt_domain };
struct automata {
struct magi_cookie_list ** list;
@@ -25,7 +25,7 @@ struct automata {
int buf_len;
int buf_size;
int is_first;
- int is_cookie2;
+ int is_advanced;
int is_quoted;
enum data_type data_t;
};
@@ -33,11 +33,11 @@ struct automata {
static void nulify_cookie(struct automata * a)
{
- a->cookie.name = 0;
- a->cookie.data = 0;
- a->cookie.path = 0;
- a->cookie.domain = 0;
- a->cookie.port = 0;
+ a->cookie.name = 0;
+ a->cookie.data = 0;
+ a->cookie.path = 0;
+ a->cookie.domain = 0;
+ a->cookie.max_age = 0;
}
static void buf_new(struct automata * a)
@@ -53,13 +53,11 @@ static enum data_type what_is_name(const struct automata * a)
enum data_type data_t = dt_plain;
if (a->is_first && !strcmp(a->buf, "$Version")) {
data_t = dt_version;
- } else if (a->is_cookie2) {
+ } else if (a->is_advanced) {
if (!strcmp(a->buf, "$Path")) {
data_t = dt_path;
} else if (!strcmp(a->buf, "$Domain")) {
data_t = dt_domain;
- } else if (!strcmp(a->buf, "$Port")) {
- data_t = dt_port;
}
}
return data_t;
@@ -96,9 +94,6 @@ static int end_data(struct automata * a)
case dt_domain:
a->cookie.domain = a->buf;
break;
- case dt_port:
- a->cookie.port = a->buf;
- break;
case dt_version:
if (strcmp(a->buf, "1")) {
ok = 0;
@@ -291,6 +286,5 @@ void magi_cookies(struct magi_request * request, const char * data)
free(a.cookie.data);
free(a.cookie.path);
free(a.cookie.domain);
- free(a.cookie.port);
free(a.buf);
}
diff --git a/src/inner_multipart.c b/src/inner_multipart.c
index 65252e5..0187f7c 100644
--- a/src/inner_multipart.c
+++ b/src/inner_multipart.c
@@ -2,8 +2,8 @@
#include "inner_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/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/request.c b/src/request.c
index e905e53..8c3a101 100644
--- a/src/request.c
+++ b/src/request.c
@@ -21,73 +21,6 @@ void magi_request_setup(struct magi_request * request)
}
}
-void magi_tempfiles_add(struct magi_tempfiles * tmps,
- const char * name,
- const char * path,
- int max)
-{
- if (!tmps) {
- return;
- }
- if (tmps->count++) {
- tmps->param_names = realloc(tmps->param_names,
- sizeof(*tmps->param_names) * tmps->count);
- tmps->locations =
- realloc(tmps->locations, sizeof(*tmps->locations) * tmps->count);
- tmps->maximums =
- realloc(tmps->maximums, sizeof(*tmps->maximums) * tmps->count);
- } else {
- tmps->param_names = malloc(sizeof(*tmps->param_names));
- tmps->locations = malloc(sizeof(*tmps->locations));
- tmps->maximums = malloc(sizeof(*tmps->maximums));
- }
- tmps->param_names[tmps->count - 1] = name;
- tmps->locations[tmps->count - 1] = path;
- tmps->maximums[tmps->count - 1] = max;
-}
-
-static void tempfiles(struct magi_file * file,
- char * addon,
- int addon_len,
- int is_addon_last,
- void * userdata)
-{
- struct magi_tempfiles * table = userdata;
- int pos;
- for (pos = 0; pos != table->count; ++pos) {
- if (!strcmp(table->param_names[pos], file->param_name)) {
- static FILE * f = 0;
- static int unlimited;
- static int left;
- if (!f) {
- const char * loc = table->locations[pos];
- f = fopen(loc, "wb");
- left = table->maximums[pos];
- 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 (is_addon_last) {
- fclose(f);
- f = 0;
- }
- return;
- }
- }
-}
-
-void magi_request_setup_tempfiles(struct magi_request * request,
- struct magi_tempfiles * table)
-{
- request->file_callback = tempfiles;
- request->file_callback_userdata = table;
-}
-
static void request_free(struct magi_request * request)
{
@@ -147,3 +80,78 @@ void magi_request_destroy(struct magi_request * request)
request_annul(request);
}
}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Tempfiles Callback
+ */
+void magi_tempfiles_add(struct magi_tempfiles * tmps,
+ const char * name,
+ const char * path,
+ int max)
+{
+ if (!tmps) {
+ return;
+ }
+ if (tmps->count++) {
+ tmps->tmps = realloc(tmps->tmps, sizeof(*tmps->tmps) * tmps->count);
+ } else {
+ tmps->tmps = malloc(sizeof(*tmps->tmps));
+ }
+ tmps->tmps[tmps->count - 1].param_name = name;
+ tmps->tmps[tmps->count - 1].location = path;
+ tmps->tmps[tmps->count - 1].maximum = max;
+}
+
+void magi_tempfiles_destroy(struct magi_tempfiles * tmps)
+{
+ if (!tmps) {
+ return;
+ }
+ free(tmps->tmps);
+}
+
+static void tempfiles(struct magi_file * file,
+ char * addon,
+ int addon_len,
+ int is_addon_last,
+ void * userdata)
+{
+ struct magi_tempfiles * table = userdata;
+ int pos;
+ if (!file->file_name || !strcmp(file->file_name, "")) {
+ return;
+ }
+ for (pos = 0; pos != table->count; ++pos) {
+ if (!strcmp(table->tmps[pos].param_name, file->param_name)) {
+ static FILE * f = 0;
+ static int unlimited;
+ static int left;
+ if (!f) {
+ const char * loc = table->tmps[pos].location;
+ f = fopen(loc, "wb");
+ left = table->tmps[pos].maximum;
+ 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 (is_addon_last) {
+ fclose(f);
+ f = 0;
+ }
+ return;
+ }
+ }
+}
+
+void magi_request_setup_tempfiles(struct magi_request * request,
+ struct magi_tempfiles * table)
+{
+ request->file_callback = tempfiles;
+ request->file_callback_userdata = table;
+}
diff --git a/src/request.h b/src/request.h
index ffa239f..18d6c8e 100644
--- a/src/request.h
+++ b/src/request.h
@@ -113,8 +113,5 @@ void magi_tempfiles_destroy(magi_tempfiles *tmps);
void magi_request_setup_tempfiles(magi_request *request,
magi_tempfiles *table);
-/* Destroys request. */
-void magi_request_destroy(struct magi_request * request);
-
#endif
diff --git a/src/response.c b/src/response.c
index e4cd342..fdeba34 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>
@@ -23,6 +23,7 @@ void magi_response_setup(magi_response *response)
void magi_response_content_type(magi_response *response, const char *type)
{
+<<<<<<< HEAD
static const char *const ct = "Content-Type: ";
static const int ctlen = 15;
const int len = strlen(type);
@@ -30,6 +31,15 @@ void magi_response_content_type(magi_response *response, const char *type)
response->content_type = malloc(ctlen + len + 1);
memcpy(response->content_type, ct, ctlen);
memcpy(response->content_type + ctlen, type, len + 1);
+=======
+ const char * const messages[] = {
+ "Content-Type: application/xhtml+xml", /* magi_xhtml */
+ };
+ if (!response->content_type) {
+ response->content_type = magi_str_create_copy(messages[type],
+ strlen(messages[type]));
+ }
+>>>>>>> master
}
void magi_response_add(magi_response *r, const char *addon)
diff --git a/src/urlenc.c b/src/urlenc.c
index bb0bada..64a087e 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 <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