From e6d5f9552e66ee2db3056e810b58b2bf82551356 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 15 Nov 2019 15:06:13 +0300 Subject: Merge remote-tracking branch 'croco/master' into test --- examples/cookie.c | 1 + src/error.c | 55 +++++++++++++++++++++++++++++++++++ src/error.h | 15 ++++++++++ src/log.c | 17 ----------- src/log.h | 8 ----- src/multipart.c | 19 ++++++++++++ src/response.c | 0 src/response.h | 0 src/urlencoded.c | 21 ++++++++++++++ src/utils.c | 57 ++++++++++++++++++++++++++++++++++++ src/utils.h | 87 +++---------------------------------------------------- 11 files changed, 172 insertions(+), 108 deletions(-) create mode 100644 src/error.c create mode 100644 src/error.h delete mode 100644 src/log.c delete mode 100644 src/log.h create mode 100644 src/response.c create mode 100644 src/response.h create mode 100644 src/utils.c diff --git a/examples/cookie.c b/examples/cookie.c index 6ba5bd0..a2b9425 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..106ca8c --- /dev/null +++ b/src/error.c @@ -0,0 +1,55 @@ +#include "error.h" + +#include + + +enum control { get, set }; + +static struct magi_error * errorctl( + enum control command, struct magi_error_message * err) +{ + static struct magi_error * error = 0; + if (command == set) { + error = err; + } + return error; +} + + +struct magi_error * magi_error_get() +{ + return errorctl(get, 0); +} + +void magi_error_set(char * format, ...) +{ + for (;;) { + Create(len); + /* Try to print in the allocated space. */ + va_list ap; + va_start(ap, format); + int n = vsnprintf(p->buf, len, format, ap); + va_end(ap); + /* If that worked, we're done */ + if (n > -1 && n < len) + break; + /* Else try again with more space. */ + if (n > -1) /* glibc 2.1 */ + len = n + 1; /* precisely what is needed */ + else /* glibc 2.0 */ + len *= 2; /* twice the old size */ + } + errorctl(); +} + +void magi_error_rid() +{ + struct magi_error * error = errorctl(get, 0); + if (error) { + if (error->message) { + free(error->message); + } + free(error); + errorctl(set, 0); + } +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..80e176f --- /dev/null +++ b/src/error.h @@ -0,0 +1,15 @@ +#ifndef MAGI_INCLUDED_ERROR +#define MAGI_INCLUDED_ERROR + + +struct magi_error { + char * message; +}; + + +struct magi_error * magi_error_get(); +void magi_error_set(char * format, ...); +void magi_error_rid(); + + +#endif diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 968aa54..0000000 --- a/src/log.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "log.h" - -#include -#include - - -void magi_log(const char * format, ...) -{ -#ifdef ERRLOG - va_list args; - va_start(args, format); - fputs("MAGI ERROR: ", stderr); - vfprintf(stderr, format, args); - fputc('\n', stderr); - va_end(args); -#endif -} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index e8f7221..0000000 --- a/src/log.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MAGI_INCLUDED_LOG -#define MAGI_INCLUDED_LOG - - -void magi_log(const char * format, ...); - - -#endif diff --git a/src/multipart.c b/src/multipart.c index c49be9b..f86a66d 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -9,6 +9,25 @@ #include +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Local Shortcuts + */ +static int is_token(char c) +{ + return 32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={} \t", c); +} + +static int is_str_token(char * str) +{ + int is = str && *str; /* Empty string is not valid. */ + while (is && *str) { + is = is_token(*str); + ++str; + } + return is; +} + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Automata for multipart/form-data */ diff --git a/src/response.c b/src/response.c new file mode 100644 index 0000000..e69de29 diff --git a/src/response.h b/src/response.h new file mode 100644 index 0000000..e69de29 diff --git a/src/urlencoded.c b/src/urlencoded.c index 1edcf99..9d12b84 100644 --- a/src/urlencoded.c +++ b/src/urlencoded.c @@ -7,6 +7,27 @@ #include +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Local Shortcuts + */ +/* Shouldn't be called with 'c' as not hex digit. */ +static char from_hex(char c) +{ + char num; + if (isdigit(c)) { + num = c - '0'; + } else { + num = toupper(c) - 'A' + 10; + } + return num; +} + +static int is_hex(char c) +{ + return isdigit(c) || strchr("ABCDEF", toupper(c)); +} + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * URL Decoding */ diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..4212561 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,57 @@ +#include "utils.h" + +#include +#include + + +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; + } else { + magi_log("Cannot allocate string."); + } + return res; +} + +char * magi_str_create(int len) +{ + char * str = malloc(len + 1); + if (str) { + str[len] = 0; + } else { + magi_log("Cannot allocate string."); + } + return str; +} + +int magi_str_add(char ** dest, int * len, int * size, char c) +{ + int ok = 1; + if (*len + 1 == *size) { + *size *= 2; + *dest = realloc(*dest, *size); + } + if (*dest == 0) { + ok = 0; + magi_log("Cannot allocate string."); + } else { + (*dest)[*len] = c; + ++*len; + (*dest)[*len] = 0; + } + return ok; +} diff --git a/src/utils.h b/src/utils.h index f16ea3f..fa7db40 100644 --- a/src/utils.h +++ b/src/utils.h @@ -2,89 +2,10 @@ #define MAGI_INCLUDED_UTILS -static void lowercase(char * str) -{ - if (str) { - while (*str) { - *str = tolower(*str); - ++str; - } - } -} - -static char * create_str(const char * begin, const char * end) -{ - char * res; - res = malloc(end - begin + 1); - if (res) { - memcpy(res, begin, end - begin); - res[end - begin] = 0; - } else { - magi_log("Cannot allocate string."); - } - return res; -} - -static char * str_alloc(int len) -{ - char * str = malloc(len + 1); - if (str) { - str[len] = 0; - } else { - magi_log("[request] Cannot allocate string."); - } - return str; -} - -/* Shouldn't be called with 'c' as not hex digit. */ -static char from_hex(char c) -{ - char num; - if (isdigit(c)) { - num = c - '0'; - } else { - num = toupper(c) - 'A' + 10; - } - return num; -} - -static int is_hex(char c) -{ - return isdigit(c) || strchr("ABCDEF", toupper(c)); -} - -static int add(char ** dest, int * len, int * size, char c) -{ - int ok = 1; - if (*len + 1 == *size) { - *size *= 2; - *dest = realloc(*dest, *size); - } - if (*dest == 0) { - ok = 0; - magi_log("Cannot allocate string."); - } else { - (*dest)[*len] = c; - ++*len; - (*dest)[*len] = 0; - } - return ok; -} - -static int is_token(char c) -{ - return 32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={} \t", c); -} - -static int is_str_token(char * str) -{ - int is = str && *str; /* Empty string is not valid. */ - while (is && *str) { - is = is_token(*str); - ++str; - } - return is; -} +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); #endif -- cgit v1.2.3