From 32c1fbd2cf779a65e807d0d82412ffffa2401962 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 15 Nov 2019 18:01:45 +0300 Subject: [magi] --- src/cgi.c | 87 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) (limited to 'src/cgi.c') diff --git a/src/cgi.c b/src/cgi.c index 222c2ee..e5a9972 100644 --- a/src/cgi.c +++ b/src/cgi.c @@ -1,8 +1,8 @@ #include "cgi.h" #include "cookie.h" +#include "error.h" #include "field.h" -#include "log.h" #include "multipart.h" #include "param.h" #include "request.h" @@ -14,6 +14,7 @@ extern char ** environ; + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * CGI Request Handling */ @@ -73,37 +74,35 @@ static int cgi_http_env(struct magi_request * r) return ok; } -static int cgi_env(struct magi_request * r) +static void cgi_env(struct magi_error ** e, struct magi_request * r) { - int ok = cgi_http_env(r); - ok = ok && lower_env(&r->method, "REQUEST_METHOD"); - ok = ok && plain_env(&r->uri, "REQUEST_URI"); - ok = ok && plain_env(&r->document_root, "DOCUMENT_ROOT"); - ok = ok && plain_env(&r->document_uri, "DOCUMENT_URI"); - ok = ok && plain_env(&r->script_name, "SCRIPT_NAME"); - ok = ok && plain_env(&r->script_filename, "SCRIPT_FILENAME"); - ok = ok && plain_env(&r->remote_addr, "REMOTE_ADDR"); - ok = ok && plain_env(&r->remote_port, "REMOTE_PORT"); - ok = ok && plain_env(&r->server_addr, "SERVER_ADDR"); - ok = ok && lower_env(&r->server_name, "SERVER_NAME"); - ok = ok && plain_env(&r->server_port, "SERVER_PORT"); - ok = ok && lower_env(&r->server_protocol, "SERVER_PROTOCOL"); - ok = ok && plain_env(&r->server_software, "SERVER_SOFTWARE"); - ok = ok && plain_env(&r->path_info, "PATH_INFO"); - return ok; + cgi_http_env(e, r); + lower_env(e, &r->method, "REQUEST_METHOD"); + plain_env(e, &r->uri, "REQUEST_URI"); + plain_env(e, &r->document_root, "DOCUMENT_ROOT"); + plain_env(e, &r->document_uri, "DOCUMENT_URI"); + plain_env(e, &r->script_name, "SCRIPT_NAME"); + plain_env(e, &r->script_filename, "SCRIPT_FILENAME"); + plain_env(e, &r->remote_addr, "REMOTE_ADDR"); + plain_env(e, &r->remote_port, "REMOTE_PORT"); + plain_env(e, &r->server_addr, "SERVER_ADDR"); + lower_env(e, &r->server_name, "SERVER_NAME"); + plain_env(e, &r->server_port, "SERVER_PORT"); + lower_env(e, &r->server_protocol, "SERVER_PROTOCOL"); + plain_env(e, &r->server_software, "SERVER_SOFTWARE"); + plain_env(e, &r->path_info, "PATH_INFO"); } -static int cgi_cookies(struct magi_cookie_list ** list) +static void cgi_cookies(struct magi_error ** e, + struct magi_cookie_list ** list) { - int ok = 1; const char * env = getenv("HTTP_COOKIE"); *list = 0; if (env && *env) { - ok = magi_parse_cookie(list, env); + magi_parse_cookie(e, list, env); } else { *list = 0; } - return ok; } static int cgi_input_get(char ** input) @@ -130,7 +129,7 @@ static int cgi_input_post(char ** input, int max) if (*input) { if (fread(*input, 1, input_len, stdin) != input_len) { ok = 0; - magi_log("[request:cgi] Content-length is not correct."); + magi_error_set("[request:cgi] Content-length is not correct."); } } else { ok = 0; @@ -162,11 +161,15 @@ 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), - int max_post) + void (*callback)(struct magi_field * field, char * buffer, + int len), + int max_post) { - int ok = cgi_env(request) && cgi_cookies(&request->cookies); - request->fields = 0; + request->fields = 0; + request->error = 0; + struct magi_error ** e = &request->error; + cgi_env(e, request); + cgi_cookies(e, &request->cookies); if (request->method) { if (!strcmp(request->method, "post")) { const char * t = getenv("CONTENT_TYPE"); @@ -174,37 +177,31 @@ int magi_cgi_request(struct magi_request * request, if (!strncmp(t, "multipart/form-data", 19)) { char * boundary = bound(t); if (boundary && *boundary) { - ok = magi_parse_multipart(&request->fields, - intput_getter, 0, boundary, callback); + magi_parse_multipart(e, &request->fields, + intput_getter, 0, boundary, + callback); } else { - ok = 0; - magi_log("[request:cgi] Multipart bound is not set."); + magi_error_add( + e, "[request:cgi] Multipart bound is not set."); } free(boundary); } else if (!strcmp(t, "application/x-www-form-urlencoded")) { char * in = 0; - ok = cgi_input_post(&in, max_post); - if (ok) { - ok = magi_parse_urlencoded(&request->fields, in); - } + cgi_input_post(e, &in, max_post); + magi_parse_urlencoded(e, &request->fields, in); free(in); } else { - ok = 0; - magi_log("[request:cgi] Unknown content type."); + magi_error_add(e, "[request:cgi] Unknown content type."); } } else { - ok = 0; - magi_log("[request:cgi] Content-type is not set."); + magi_error_add(e, "[request:cgi] Content-type is not set."); } } else if (!strcmp(request->method, "get")) { char * in = 0; - ok = cgi_input_get(&in); - ok = ok && magi_parse_urlencoded(&request->fields, in); + cgi_input_get(e, &in); + magi_parse_urlencoded(e, &request->fields, in); free(in); } } - if (!ok) { - magi_request_destroy(request); - } - return ok; + return !request->error; } -- cgit v1.2.3