diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cgi.h | 22 | ||||
-rw-r--r-- | src/cookie.h | 31 | ||||
-rw-r--r-- | src/cookies.c | 0 | ||||
-rw-r--r-- | src/cookies.h | 10 | ||||
-rw-r--r-- | src/error.c | 2 | ||||
-rw-r--r-- | src/error.h | 2 | ||||
-rw-r--r-- | src/fastcgi.h | 26 | ||||
-rw-r--r-- | src/file.h | 11 | ||||
-rw-r--r-- | src/multipart.c | 11 | ||||
-rw-r--r-- | src/multipart.h | 19 | ||||
-rw-r--r-- | src/param.h | 24 | ||||
-rw-r--r-- | src/request.c | 86 | ||||
-rw-r--r-- | src/request.h | 100 | ||||
-rw-r--r-- | src/response.h | 36 | ||||
-rw-r--r-- | src/urlenc.c (renamed from src/urlencoded.c) | 0 | ||||
-rw-r--r-- | src/urlenc.h | 13 | ||||
-rw-r--r-- | src/urlencoded.h | 10 | ||||
-rw-r--r-- | src/utils.c | 19 | ||||
-rw-r--r-- | src/utils.h | 2 |
19 files changed, 248 insertions, 176 deletions
@@ -1,23 +1,17 @@ #ifndef MAGI_INCLUDED_CGI #define MAGI_INCLUDED_CGI -#include "field.h" #include "request.h" -/* - * Constructs request using environment variables and standart I/O; - * Returns null if succeed, otherwise error code. - */ -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 * thing), - void * thing, - int max_post); +/* Constructs request from environment and standard input; + * Returns null only in case of error. */ +int magi_request_cgi(struct magi_request * request); + + +/* Sends response to standard output; + * Returns null only in case of error. */ +int magi_response_cgi(struct magi_response * response); #endif diff --git a/src/cookie.h b/src/cookie.h index 7a7365e..479ab9f 100644 --- a/src/cookie.h +++ b/src/cookie.h @@ -6,40 +6,29 @@ * Cookie */ struct magi_cookie { - char * name; /* name: free(name) is valid. */ - char * data; /* data: free(data) is valid. */ - /* Following is used in Cookie2: */ - char * path; /* path: free(path) is valid. */ - char * domain; /* domain: free(domain) is valid. */ - char * port; /* port: free(port) is valid. */ + char * name; + char * data; + char * path; + char * domain; + char * port; }; -/* Null is valid "struct magi_cookie_list *" object. */ struct magi_cookie_list { struct magi_cookie_list * next; struct magi_cookie item; }; -/* Returns null in case of error. */ -int magi_parse_cookie(struct magi_cookie_list ** list, const char * input); -/* - * Adds *item to the begining of *list, item and list are dereferencable; - * Returns null in case of error. - */ +/* Returns null only in case of error. */ int magi_cookie_list_add(struct magi_cookie_list ** list, struct magi_cookie * item); -/* - * Searchs for first node in list: node.name == name, name is C-string; - * Returns node.data if succeed, otherwise result is null. - */ +/* Searchs for first node in list: node.name == name, name is C-string; + * Returns node.data if succeed, otherwise result is null. */ char * magi_cookie_list_get(struct magi_cookie_list * list, const char * name); -/* - * Destroys list; list is not valid after destruction. - */ -void magi_cookie_list_destroy(struct magi_cookie_list * list); +/* Destroys list. */ +void magi_cookie_list_free(struct magi_cookie_list * list); #endif diff --git a/src/cookies.c b/src/cookies.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/cookies.c diff --git a/src/cookies.h b/src/cookies.h new file mode 100644 index 0000000..3da16e7 --- /dev/null +++ b/src/cookies.h @@ -0,0 +1,10 @@ +#ifndef MAGI_INCLUDED_COOKIES +#define MAGI_INCLUDED_COOKIES + +#include "cookie.h" + + +void magi_cookies(struct magi_request * request, const char * data); + + +#endif diff --git a/src/error.c b/src/error.c index 4b6f7c2..48c3c36 100644 --- a/src/error.c +++ b/src/error.c @@ -9,7 +9,7 @@ const char * magi_error_message(enum magi_error error) "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 allocate memory.", /* magi_error_memory */ "Cannot read cookies.", /* magi_error_cookies */ "Cannot decode URL.", /* magi_error_urlenc */ "Cannot read multipart body." /* magi_error_multipart */ diff --git a/src/error.h b/src/error.h index ee1c8a6..46a07cd 100644 --- a/src/error.h +++ b/src/error.h @@ -8,7 +8,7 @@ enum magi_error { magi_error_unknown, magi_error_notype, magi_error_length, - magi_error_memmory, + magi_error_memory, magi_error_cookies, magi_error_urlenc, magi_error_multipart diff --git a/src/fastcgi.h b/src/fastcgi.h index d2199b4..5410955 100644 --- a/src/fastcgi.h +++ b/src/fastcgi.h @@ -1,30 +1,22 @@ #ifndef MAGI_INCLUDED_FASTCGI #define MAGI_INCLUDED_FASTCGI -#include "field.h" +#include "error.h" +#include "file.h" #include "request.h" struct magi_session { - void (*callback)(struct magi_field * field, char * buffer, int len); - int max_post; struct magi_socket_list * sockets; + enum magi_error error; }; -int magi_fcgi(struct magi_session * session, int socket); - -/* - * Returns null if succeed, otherwise error code. - */ -int magi_fcgi_accept( - struct magi_request * request, - struct magi_session * session, - /* Callback will be used only for files loaded via multipart. */ - /* Null callback disables callback system. */ - void (*callback)( - struct magi_field * field, char * buffer, int len, void * thing), - void * thing, - int max_post); +int magi_session_fcgi(struct magi_session * session, int socket); + + +/* Returns null only in case of error. */ +int magi_request_fcgi(struct magi_request * request, + struct magi_session * session); #endif @@ -0,0 +1,11 @@ +#ifndef MAGI_INCLUDED_FILE +#define MAGI_INCLUDED_FILE + + +struct magi_file { + char * param_name; + struct magi_para_list * params; +}; + + +#endif diff --git a/src/multipart.c b/src/multipart.c index 6ab1258..c3e4a2d 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -485,13 +485,10 @@ static int run_automata(struct automata * a, /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Automata Interfaces */ -int magi_parse_multipart(struct magi_field_list ** list, - int (*get_next)(void *), - void * get_next_arg, - char * boundary, - void (*callback)(struct magi_field * field, - char * buffer, - int len)) +int magi_parse_multipart(struct magi_request * request, + char * boundary, + int (*get_next)(void * userdata), + void * get_next_userdata) { struct automata a = { 0, { 0, 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0 diff --git a/src/multipart.h b/src/multipart.h index bb51d5c..47cfefe 100644 --- a/src/multipart.h +++ b/src/multipart.h @@ -1,22 +1,13 @@ #ifndef MAGI_INCLUDED_MULTIPART #define MAGI_INCLUDED_MULTIPART -#include "field.h" +#include "request.h" -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, - /* End if size < magi_parse_multipart_callback_size. */ - /* Null callback means skipping. */ - void (*callback)( - struct magi_file * file, char * buffer, int size, void * thing), - void * thing); +void magi_multipart(struct magi_request * request, + char * boundary, + int (*next)(void * userdata), + void * next_userdata); #endif diff --git a/src/param.h b/src/param.h index 21e9538..6fd0ba0 100644 --- a/src/param.h +++ b/src/param.h @@ -5,36 +5,28 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Parameter */ -enum { magi_param_max = 1024 }; - struct magi_param { - char * name; /* name: free(name) is valid. */ - char * data; /* data: free(data) is valid. */ + char * name; + char * data; }; -/* Null is valid "struct magi_param_list *" object. */ struct magi_param_list { struct magi_param_list * next; struct magi_param item; }; -/* - * Adds *item to the begining of *list, item and list are dereferencable; - * Returns null in case of error. - */ + +/* Adds *item to the begining of *list, item and list are dereferencable; + * Returns null in case of error. */ int magi_param_list_add(struct magi_param_list ** list, struct magi_param * item); -/* - * Searchs for first node in list: node.name == name, name is C-string; - * Returns node itself if succeed, otherwise result is null. - */ +/* Searchs for first node in list: node.name == name, name is C-string; + * Returns node itself if succeed, otherwise result is null. */ struct magi_param * magi_param_list_get(struct magi_param_list * list, const char * name); -/* - * Destroys list; list is not valid after destruction. - */ +/* Destroys list; list is not valid after destruction. */ void magi_param_list_destroy(struct magi_param_list * list); diff --git a/src/request.c b/src/request.c index 73c4730..2d9625e 100644 --- a/src/request.c +++ b/src/request.c @@ -1,35 +1,79 @@ #include "request.h" #include "cookie.h" -#include "field.h" +#include "file.h" #include "param.h" #include <stdlib.h> -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Common Request Handling - */ +void magi_request_setup(struct magi_request * request) +{ + if (request) { + request->file_callback = 0; + request->file_callback_userdata = 0; + request->file_callback_addon_len_max = 64; + request->max_cookies_size = 0; + request->max_http_params_size = 0; + request->max_params_size = 0; + } +} + + +static void request_free(struct magi_request * request) +{ + free(request->cookies); + free(request->http_params); + free(request->url_params); + free(request->params); + free(request->files); + free(request->method); + free(request->uri); + free(request->document_root); + free(request->document_uri); + free(request->script_name); + free(request->script_filename); + free(request->remote_addr); + free(request->remote_port); + free(request->server_addr); + free(request->server_name); + free(request->server_port); + free(request->server_protocol); + free(request->server_software); + free(request->path_info); +} + +void magi_request_annul(struct magi_request * request) +{ + request->cookies = 0; + request->http_params = 0; + request->url_params = 0; + request->params = 0; + request->files = 0; + request->method = 0; + request->uri = 0; + request->document_root = 0; + request->document_uri = 0; + request->script_name = 0; + request->script_filename = 0; + request->remote_addr = 0; + request->remote_port = 0; + request->server_addr = 0; + request->server_name = 0; + request->server_port = 0; + request->server_protocol = 0; + request->server_software = 0; + request->path_info = 0; +} + void magi_request_destroy(struct magi_request * request) { if (request) { - magi_field_list_destroy(request->fields); magi_cookie_list_destroy(request->cookies); - free(request->fields); - free(request->cookies); - free(request->method); - free(request->uri); - free(request->document_root); - free(request->document_uri); - free(request->script_name); - free(request->script_filename); - free(request->remote_addr); - free(request->remote_port); - free(request->server_addr); - free(request->server_name); - free(request->server_port); - free(request->server_protocol); - free(request->server_software); magi_param_list_destroy(request->http_params); - free(request->http_params); + magi_param_list_destroy(request->url_params); + magi_param_list_destroy(request->params); + magi_file_list_destroy(request->files); + request_free(request); + request_annul(request); } } diff --git a/src/request.h b/src/request.h index d74dced..15a5da6 100644 --- a/src/request.h +++ b/src/request.h @@ -2,7 +2,8 @@ #define MAGI_INCLUDED_REQUEST #include "cookie.h" -#include "field.h" +#include "error.h" +#include "file.h" #include "param.h" @@ -10,52 +11,71 @@ * Request * * Can be generated via CGI handler (magi_cgi_request) or - * as session in Fast CGI (magi_fcgi_request). + * in a Fast CGI session (magi_fcgi_request). * - * Example: http://example.com/cgi-bin/script/foo/bar?var2=url%20enc - * method: get - * uri: /cgi-bin/script/foo/bar?var2=url%20enc - * document_root: { absolute path to root directory of domain } - * document_uri: /cgi-bin/script/foo/bar - * script_name: /cgi-bin/script - * script_filename: { absolute path to script on server machine } - * remote_addr: { client IP } - * remote_port: { client port } - * server_addr: { server IP } - * server_name: example.com - * (^ Better to use http_params["HTTP_HOST"] -- server_name can be IP too.) - * server_port: 80 - * server_protocol: http/1.1 - * server_software: { name of web server software } - * path_info: /foo/bar + * + * 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 */ struct magi_request { - struct magi_param_list * url_params; - struct magi_param_list * params; - struct magi_file_list * files; + /* * * Results * * */ + + /* Parsed */ struct magi_cookie_list * cookies; - char * method; - char * uri; - char * document_root; - char * document_uri; - char * script_name; - char * script_filename; - char * remote_addr; - char * remote_port; - char * server_addr; - char * server_name; - char * server_port; - char * server_protocol; - char * server_software; - char * path_info; - struct magi_param_list * http_params; - enum magi_error error; + struct magi_param_list * http_params; /* HTTP Header parameters */ + struct magi_param_list * url_params; /* Urlencoded paramteres from URL */ + struct magi_param_list * params; /* Parameters from 'post' body */ + struct magi_file_list * files; /* Files loaded via multipart */ + + /* 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 */ + /* Following can be not a domain name, even if request is done with it. + * (Use http_params["HTTP_HOST"] instead.) */ + char * server_name; /* SERVER_NAME */ + char * server_port; /* SERVER_PORT */ + char * server_protocol; /* SERVER_PROTOCOL */ + char * server_software; /* SERVER_COFTWARE */ + char * path_info; /* PATH_INFO */ + + /* Request Error Code */ + enum magi_error error; + + + /* * * Internal Data (no need for user to analyse) * * */ + + /* Callback for processing files */ + void (*file_callback)(struct magi_file * file, + char * addon, + int addon_len, + int is_addon_last, + void * userdata); + void * file_callback_userdata; + int file_callback_addon_len_max; + + /* Limit for memory used (disables with zero) */ + int max_cookies_size; + int max_http_params_size; + int max_params_size; }; -/* - * Destroys request; request is not valid after destruction. - */ +/* Setup request with default settings. */ +void magi_request_setup(struct magi_request * request); + + +/* Destroys request. */ void magi_request_destroy(struct magi_request * request); diff --git a/src/response.h b/src/response.h index e69de29..04d7350 100644 --- a/src/response.h +++ b/src/response.h @@ -0,0 +1,36 @@ +#ifndef MAGI_INCLUDED_RESPONSE +#define MAGI_INCLUDED_RESPONSE + +#include "error.h" + + +enum magi_content_type { magi_xhtml }; + +struct magi_response { + struct magi_cookie_list * cookies; + struct magi_param_list * http_params; + struct magi_param_list * url_params; + char * content_type; + char * content; +}; + +void magi_response_content_type(struct magi_response * response, + enum magi_content_type type); + +void magi_reponse_add(struct magi_response * response, const char * addon); +void magi_reponse_add_format(struct magi_response * response, + const char * addon, + ...); + +void magi_response_cookie(struct magi_response * response, + const struct magi_cookie * cookie); + +void magi_response_cookie_easy(struct magi_response * response, + const char * name, + const char * value); + +void magi_response_cookie_delete(struct magi_response * response, + const char * name); + + +#endif diff --git a/src/urlencoded.c b/src/urlenc.c index 336dde0..336dde0 100644 --- a/src/urlencoded.c +++ b/src/urlenc.c diff --git a/src/urlenc.h b/src/urlenc.h new file mode 100644 index 0000000..9c14ccb --- /dev/null +++ b/src/urlenc.h @@ -0,0 +1,13 @@ +#ifndef MAGI_INCLUDED_URLENC +#define MAGI_INCLUDED_URLENC + +#include "request.h" + + +void magi_urlenc(struct magi_request * request, const char * encoded); + +int magi_urlenc_size(const char * plain); +void magi_urlencode(const char * plain, char * code); + + +#endif diff --git a/src/urlencoded.h b/src/urlencoded.h deleted file mode 100644 index 838ae57..0000000 --- a/src/urlencoded.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MAGI_INCLUDED_URLENCODED -#define MAGI_INCLUDED_URLENCODED - -#include "field.h" - - -int magi_parse_urlencoded(struct magi_field_list ** list, const char * input); - - -#endif diff --git a/src/utils.c b/src/utils.c index 01969a2..29e8219 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,8 +22,6 @@ char * magi_str_create_copy(const char * begin, const char * end) if (res) { memcpy(res, begin, end - begin); res[end - begin] = 0; - } else { - magi_error_set("Cannot allocate string."); } return res; } @@ -33,26 +31,21 @@ char * magi_str_create(int len) char * str = malloc(len + 1); if (str) { str[len] = 0; - } else { - magi_error_set("Cannot allocate string."); } return str; } -int magi_str_add(char ** dest, int * len, int * size, char c) +char * magi_str_add(char * dest, int * len, int * size, char c) { int ok = 1; if (*len + 1 == *size) { *size *= 2; - *dest = realloc(*dest, *size); + dest = realloc(dest, *size); } - if (*dest == 0) { - ok = 0; - magi_error_set("Cannot allocate string."); - } else { - (*dest)[*len] = c; + if (dest) { + dest[*len] = c; ++*len; - (*dest)[*len] = 0; + dest[*len] = 0; } - return ok; + return dest; } diff --git a/src/utils.h b/src/utils.h index fa7db40..15904d0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -5,7 +5,7 @@ void magi_str_lowercase(char * str); char * magi_str_create_copy(const char * begin, const char * end); char * magi_str_create(int len); -int magi_str_add(char ** dest, int * len, int * size, char c); +char * magi_str_add(char * dest, int * len, int * size, char c); #endif |