From 7a388dad85152a203033c14fee3c64607301865a Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Sat, 8 Jul 2023 15:06:33 -0300 Subject: libactivity: (tmp name) http request parse the parse works via callbacks, registered to a method and path. further work will be done to simplify extraction of path, and to make the request struct more private. missing the parsing and handling of reponses. Signed-off-by: Anna (navi) Figueiredo Gomes --- src/util.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/util.c (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..e6b7977 --- /dev/null +++ b/src/util.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +#include "util.h" + +int vasprintf(char **dest, const char *fmt, va_list ap) { + assert(*dest == NULL); + char *buf = NULL; size_t len, size; + va_list ap2; + + size = 4096; + buf = malloc(size); + + va_copy(ap2, ap); + len = vsnprintf(buf, size, fmt, ap); + va_end(ap); + + if (len >= size) { + size = len + 1; + buf = realloc(buf, size); + va_copy(ap2, ap); + len = vsnprintf(buf, size, fmt, ap); + va_end(ap2); + } + if (len < 0 || len >= size) { + fprintf(stderr, "asprintf: failed to format buffer"); + free(buf); + exit(EXIT_FAILURE); + } + + *dest = buf; + return len; +} + +int asprintf(char **dest, const char *fmt, ...) { + va_list ap; + int ret; + va_start(ap, fmt); + ret = vasprintf(dest, fmt, ap); + va_end(ap); + return ret; +} + +size_t hash(const char *str, int max_val) { + size_t ret = 0; + + for (const char *p = str; *p != '\0'; p++) { + ret += (size_t)*p + ((size_t)*p << 6) + ((size_t)*p << 16); + } + + ret = ret % max_val; + + return ret; +} -- cgit v1.2.3