aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-15 15:06:13 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-15 15:06:13 +0300
commite6d5f9552e66ee2db3056e810b58b2bf82551356 (patch)
tree28aba181f716f5ab9df442531d8c72c3c8b4d197 /src
parentca08ed93e67a99868f01d21f0d2e34d6a6757c75 (diff)
downloadmagi-e6d5f9552e66ee2db3056e810b58b2bf82551356.tar
magi-e6d5f9552e66ee2db3056e810b58b2bf82551356.tar.xz
magi-e6d5f9552e66ee2db3056e810b58b2bf82551356.zip
Merge remote-tracking branch 'croco/master' into test
Diffstat (limited to 'src')
-rw-r--r--src/error.c55
-rw-r--r--src/error.h15
-rw-r--r--src/log.c17
-rw-r--r--src/log.h8
-rw-r--r--src/multipart.c19
-rw-r--r--src/response.c0
-rw-r--r--src/response.h0
-rw-r--r--src/urlencoded.c21
-rw-r--r--src/utils.c57
-rw-r--r--src/utils.h87
10 files changed, 171 insertions, 108 deletions
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 <stdarg.h>
+
+
+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 <stdarg.h>
-#include <stdio.h>
-
-
-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
@@ -10,6 +10,25 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * 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
*/
enum st {
diff --git a/src/response.c b/src/response.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/response.c
diff --git a/src/response.h b/src/response.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/response.h
diff --git a/src/urlencoded.c b/src/urlencoded.c
index 1edcf99..9d12b84 100644
--- a/src/urlencoded.c
+++ b/src/urlencoded.c
@@ -8,6 +8,27 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * 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
*/
static int deurl(char ** data)
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 <ctype.h>
+#include <stdlib.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;
+ } 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