aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cgi.c62
-rw-r--r--src/cgi.h5
-rw-r--r--src/error.c57
-rw-r--r--src/error.h17
-rw-r--r--src/multipart.c16
-rw-r--r--src/multipart.h7
6 files changed, 73 insertions, 91 deletions
diff --git a/src/cgi.c b/src/cgi.c
index e5a9972..cf74f14 100644
--- a/src/cgi.c
+++ b/src/cgi.c
@@ -74,7 +74,7 @@ static int cgi_http_env(struct magi_request * r)
return ok;
}
-static void cgi_env(struct magi_error ** e, struct magi_request * r)
+static void cgi_env(enum magi_error * e, struct magi_request * r)
{
cgi_http_env(e, r);
lower_env(e, &r->method, "REQUEST_METHOD");
@@ -93,15 +93,16 @@ static void cgi_env(struct magi_error ** e, struct magi_request * r)
plain_env(e, &r->path_info, "PATH_INFO");
}
-static void cgi_cookies(struct magi_error ** e,
- struct magi_cookie_list ** list)
+static void cgi_cookies(enum magi_error * e, struct magi_cookie_list ** list)
{
- const char * env = getenv("HTTP_COOKIE");
- *list = 0;
- if (env && *env) {
- magi_parse_cookie(e, list, env);
- } else {
- *list = 0;
+ if (!*e) {
+ const char * env = getenv("HTTP_COOKIE");
+ *list = 0;
+ if (env && *env) {
+ magi_parse_cookie(e, list, env);
+ } else {
+ *list = 0;
+ }
}
}
@@ -120,22 +121,21 @@ static int cgi_input_get(char ** input)
return ok;
}
-static int cgi_input_post(char ** input, int max)
+static void cgi_input_post(enum magi_error * e, char ** input, int max)
{
- int ok = 1;
- int input_len = strtoul(getenv("CONTENT_LENGTH"), 0, 10);
- if (input_len && (input_len < max || !max)) {
- *input = str_alloc(input_len);
- if (*input) {
- if (fread(*input, 1, input_len, stdin) != input_len) {
- ok = 0;
- magi_error_set("[request:cgi] Content-length is not correct.");
+ if (!*e) {
+ int input_len = strtoul(getenv("CONTENT_LENGTH"), 0, 10);
+ if (input_len && (input_len < max || !max)) {
+ *input = str_alloc(input_len);
+ if (*input) {
+ if (fread(*input, 1, input_len, stdin) != input_len) {
+ *e = magi_error_length;
+ }
+ } else {
+ *e = magi_error_input;
}
- } else {
- ok = 0;
}
}
- return ok;
}
static char * bound(const char * type)
@@ -161,13 +161,14 @@ static int intput_getter(void * any)
/* Interfacial CGI Request Handling */
int magi_cgi_request(struct magi_request * request,
- void (*callback)(struct magi_field * field, char * buffer,
- int len),
+ void (*callback)(struct magi_field * field,
+ char * buffer,
+ int len),
int max_post)
{
- request->fields = 0;
- request->error = 0;
- struct magi_error ** e = &request->error;
+ request->fields = 0;
+ request->error = magi_error_none;
+ enum magi_error * e = &request->error;
cgi_env(e, request);
cgi_cookies(e, &request->cookies);
if (request->method) {
@@ -181,8 +182,7 @@ int magi_cgi_request(struct magi_request * request,
intput_getter, 0, boundary,
callback);
} else {
- magi_error_add(
- e, "[request:cgi] Multipart bound is not set.");
+ *e = magi_error_nobound;
}
free(boundary);
} else if (!strcmp(t, "application/x-www-form-urlencoded")) {
@@ -191,10 +191,10 @@ int magi_cgi_request(struct magi_request * request,
magi_parse_urlencoded(e, &request->fields, in);
free(in);
} else {
- magi_error_add(e, "[request:cgi] Unknown content type.");
+ *e = magi_error_unknown;
}
} else {
- magi_error_add(e, "[request:cgi] Content-type is not set.");
+ *e = magi_error_notype;
}
} else if (!strcmp(request->method, "get")) {
char * in = 0;
@@ -203,5 +203,5 @@ int magi_cgi_request(struct magi_request * request,
free(in);
}
}
- return !request->error;
+ return request->error == magi_error_none;
}
diff --git a/src/cgi.h b/src/cgi.h
index 2211e33..331e306 100644
--- a/src/cgi.h
+++ b/src/cgi.h
@@ -12,8 +12,9 @@
int magi_cgi(struct magi_request * request,
/* Callback will be used only for fields loaded via multipart. */
/* Null callback disables callback system. */
- void (*callback)(struct magi_field * field, char * buffer,
- int len),
+ void (*callback)(struct magi_field * field,
+ char * buffer,
+ int len),
int max_post);
diff --git a/src/error.c b/src/error.c
index 01fae36..4b6f7c2 100644
--- a/src/error.c
+++ b/src/error.c
@@ -1,49 +1,18 @@
#include "error.h"
-#include "utils.h"
-#include <stdarg.h>
-
-void magi_error_add(struct magi_error ** error, char * format, ...)
-{
- if (error && format) {
- struct magi_error * head = malloc(sizeof(*head));
- if (head) {
- int size;
- va_list args;
-
- va_start(args, format);
- size = vsnprintf(0, 0, format, args);
- va_end(args);
-
- if (size >= 0) {
- head->message = malloc(++size);
- if (head->message) {
- va_start(args, format);
- size = vsnprintf(head->message, size, format, args);
- va_end(args);
- if (size >= 0) {
- head->prev = *error;
- *error = head;
- return;
- } else {
- free(head->message);
- }
- }
- }
-
- free(head);
- }
- }
-}
-
-void magi_error_destroy(struct magi_error * error)
+const char * magi_error_message(enum magi_error error)
{
- if (error) {
- magi_error_destroy(error->prev);
- free(error->prev);
- free(error->message);
- error->prev = 0;
- error->message = 0;
- }
+ static const char * const messages[] = {
+ 0, /* magi_error_none */
+ "No boundary for multipart.", /* magi_error_nobound */
+ "Content-type is unknown.", /* magi_error_unknown */
+ "Content-type is not given.", /* magi_error_notype */
+ "Mismatch of content length.", /* magi_error_length */
+ "Cannot allocate memmory.", /* magi_error_memmory */
+ "Cannot read cookies.", /* magi_error_cookies */
+ "Cannot decode URL.", /* magi_error_urlenc */
+ "Cannot read multipart body." /* magi_error_multipart */
+ };
+ return messages[error];
}
diff --git a/src/error.h b/src/error.h
index b3014c2..ee1c8a6 100644
--- a/src/error.h
+++ b/src/error.h
@@ -2,14 +2,19 @@
#define MAGI_INCLUDED_ERROR
-struct magi_error {
- struct magi_error * prev;
- char * message; /* enum magi_error_type { ... } type; */
+enum magi_error {
+ magi_error_none = 0,
+ magi_error_nobound,
+ magi_error_unknown,
+ magi_error_notype,
+ magi_error_length,
+ magi_error_memmory,
+ magi_error_cookies,
+ magi_error_urlenc,
+ magi_error_multipart
};
-
-void magi_error_add(struct magi_error ** error, char * format, ...);
-void magi_error_destroy(struct magi_error * error);
+const char * magi_error_message(enum magi_error error);
#endif
diff --git a/src/multipart.c b/src/multipart.c
index 7847a41..6ab1258 100644
--- a/src/multipart.c
+++ b/src/multipart.c
@@ -433,8 +433,9 @@ static enum st parse_end(struct automata * a, char c)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Automata Runner
*/
-static int run_automata(struct automata * a, int (*next)(void * thing),
- void * thing)
+static int run_automata(struct automata * a,
+ int (*next)(void * thing),
+ void * thing)
{
int ok = 1;
enum st state = st_begin;
@@ -485,13 +486,16 @@ static int run_automata(struct automata * a, int (*next)(void * thing),
* Automata Interfaces
*/
int magi_parse_multipart(struct magi_field_list ** list,
- int (*get_next)(void *), void * get_next_arg,
+ int (*get_next)(void *),
+ void * get_next_arg,
char * boundary,
void (*callback)(struct magi_field * field,
- char * buffer, int len))
+ char * buffer,
+ int len))
{
- struct automata a
- = { 0, { 0, 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0 };
+ struct automata a = {
+ 0, { 0, 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0
+ };
int ok = 0;
a.list = list;
a.boundary = boundary;
diff --git a/src/multipart.h b/src/multipart.h
index e9a0004..43b6212 100644
--- a/src/multipart.h
+++ b/src/multipart.h
@@ -7,8 +7,11 @@
enum { magi_parse_multipart_callback_size = 64 };
-int magi_parse_multipart(struct magi_field_list ** list,
- int (*get_next)(void *), void * get_next_arg, char * boundary,
+int magi_parse_multipart(
+ struct magi_field_list ** list,
+ int (*get_next)(void *),
+ void * get_next_arg,
+ char * boundary,
/* End if size < magi_parse_multipart_callback_size. */
/* Null callback means filling list. */
void (*callback)(struct magi_field * field, char * buffer, int size));