Skip to content

Commit

Permalink
feat: extended sn_map_t type and added http_method_t type
Browse files Browse the repository at this point in the history
  • Loading branch information
scokmen committed Jul 6, 2024
1 parent acf49a5 commit abe5e71
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 45 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ set(sn_sources
src/server/http.c
src/server/event_loop.c
src/sn_logger.h
src/sn_logger.c)
src/sn_logger.c
src/sn_common.h)

add_library(snail STATIC ${sn_sources})

Expand Down
33 changes: 30 additions & 3 deletions include/headers/http.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#ifndef SNAIL_HTTP_H
#define SNAIL_HTTP_H

#define GET 1
#define HEAD 2
#define POST 3
#define PUT 4
#define DELETE 5
#define CONNECT 6
#define OPTIONS 7
#define TRACE 8
#define PATH 9

#define HTTP_METHOD_MAP(XX) \
XX(GET , "GET") \
XX(HEAD , "HEAD") \
XX(POST , "POST") \
XX(PUT , "PUT") \
XX(DELETE , "DELETE") \
XX(CONNECT , "CONNECT") \
XX(OPTIONS , "OPTIONS") \
XX(TRACE , "TRACE") \
XX(PATH , "PATH") \

#define CONTINUE 100
#define SWITCHING_PROTOCOLS 101
#define PROCESSING 102
Expand Down Expand Up @@ -127,11 +148,17 @@
XX(NETWORK_AUTHENTICATION_REQUIRED , "Network Authentication Required") \

typedef enum {
#define DEF_HTTP_ENUM(code, _) HTTP_ ## code = code,
HTTP_CODE_MAP(DEF_HTTP_ENUM)
#define DEF_HTTP_METHOD_ENUM(code, _) HTTP_ ## code = code,
HTTP_METHOD_MAP(DEF_HTTP_METHOD_ENUM)
#undef DEF_HTTP_METHOD_ENUM
} sn_http_method_t;

typedef enum {
#define DEF_HTTP_CODE_ENUM(code, _) HTTP_ ## code = code,
HTTP_CODE_MAP(DEF_HTTP_CODE_ENUM)
#undef DEF_HTTP_ENUM
} sn_http_code_t;

extern const char *sn_http_get_description(sn_http_code_t code);
const char *sn_http_code_get_description(sn_http_code_t code);

#endif //SNAIL_HTTP_H
12 changes: 10 additions & 2 deletions include/headers/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

typedef void (*sn_map_traversal_cb)(const char* key, void *data);
typedef void (*sn_map_unregister_cb)(const char* key, void *data);

typedef struct sn_map_lln_t {
bool active;
bool clean;
void *data;
const char *label;
struct sn_map_lln_t *next;
Expand All @@ -29,7 +31,7 @@ sn_map_t *sn_map_init(uint16_t bucket_size);
SN_NONNULL(1)
void sn_map_destroy(sn_map_t *map);

SN_NONNULL(1, 2)
SN_NONNULL(1, 2, 3, 4)
bool sn_map_set(sn_map_t *map, const char *key, void *data, sn_map_unregister_cb unregister_cb);

SN_NONNULL(1, 2)
Expand All @@ -41,4 +43,10 @@ void sn_map_del(sn_map_t *map, const char *key);
SN_NONNULL(1, 2)
bool sn_map_has(sn_map_t *map, const char *key);

SN_NONNULL(1)
size_t sn_map_length(sn_map_t *map);

SN_NONNULL(1, 2)
void sn_map_traverse(sn_map_t *map, sn_map_traversal_cb);

#endif //SNAIL_MAP_H
4 changes: 2 additions & 2 deletions src/server/event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "picohttpparser.h"
#include <picohttpparser.h>
#include "snail.h"
#include "sn_logger.h"

Expand Down Expand Up @@ -151,7 +151,7 @@ static sn_http_code_t check_fail_fast_err(sock_http_data *http_data, int minor_v
}

static void send_fail_fast_response(sock_loop_data *loop_data, sn_http_code_t code) {
const char *response_text = sn_http_get_description(code);
const char *response_text = sn_http_code_get_description(code);
sn_log_err("Http early return with Code=%d, Details=%s\n", code, response_text);
asprintf(&(loop_data->http_data->response_buf),
"HTTP/1.1 %d %s\r\nContent-Type: text/plain\r\nContent-Length: %lu\r\n\r\n%s",
Expand Down
2 changes: 1 addition & 1 deletion src/server/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define SN_HTTP_ENUM_GENERATOR(name, msg) case HTTP_ ## name: return msg;

SN_CONST_FN
const char *sn_http_get_description(sn_http_code_t code) {
const char *sn_http_code_get_description(sn_http_code_t code) {
switch (code) {
HTTP_CODE_MAP(SN_HTTP_ENUM_GENERATOR)
default:
Expand Down
9 changes: 9 additions & 0 deletions src/sn_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SNAIL_SN_COMMON_H
#define SNAIL_SN_COMMON_H

#define MALLOC_OR_RETURN_NULL(PTR, TYPE) PTR = malloc(sizeof (TYPE)); \
if (PTR == NULL) { \
return NULL; \
} \

#endif //SNAIL_SN_COMMON_H
10 changes: 5 additions & 5 deletions src/sn_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ static int min_log_level = LOG_LEVEL_DEBUG;

#define GET_OUTPUT_DEV(LEVEL) ((LEVEL < LOG_LEVEL_ERROR) ? stdout : stderr)

#define WRITE_LOG(LEVEL, FMT) if (min_log_level <= LEVEL) { \
va_list args; \
va_start(args, FMT); \
vfprintf(GET_OUTPUT_DEV(LEVEL), fmt, args); \
va_end(args); \
#define WRITE_LOG(LEVEL, FMT) if (min_log_level <= LEVEL) { \
va_list args; \
va_start(args, FMT); \
vfprintf(GET_OUTPUT_DEV(LEVEL), fmt, args); \
va_end(args); \
}

void sn_log_set_level(int level) {
Expand Down
62 changes: 42 additions & 20 deletions src/types/map.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <printf.h>
#include <string.h>
#include <assert.h>
#include "sn_common.h"
#include "snail.h"

SN_CONST_FN
Expand All @@ -17,11 +17,9 @@ static unsigned long djb2_hash(unsigned char *str) {
}

static sn_map_lln_t *create_linked_list_node(const char *key, void *data, sn_map_unregister_cb unregister_cb) {
sn_map_lln_t *node = malloc(sizeof (sn_map_lln_t));
if (node == NULL) {
return NULL;
}
node->active = true;
sn_map_lln_t *node;
MALLOC_OR_RETURN_NULL(node, sn_map_lln_t)
node->clean = true;
node->next = NULL;
node->data = data;
node->label = key;
Expand All @@ -40,7 +38,7 @@ static sn_map_lln_t *find_label_node(sn_map_t *map, const char *key, int* target

node = linked_list.head;
while (node != NULL) {
if (!node->active) {
if (!node->clean) {
node = node->next;
continue;
}
Expand All @@ -55,10 +53,8 @@ static sn_map_lln_t *find_label_node(sn_map_t *map, const char *key, int* target

sn_map_t *sn_map_init(uint16_t bucket_size) {
assert(bucket_size > 0);
sn_map_t *map = malloc(sizeof(sn_map_t));
if (map == NULL) {
return NULL;
}
sn_map_t *map;
MALLOC_OR_RETURN_NULL(map, sn_map_t)
map->bucket_size = bucket_size;
map->buckets = calloc(bucket_size, sizeof(sn_map_ll_t));
if (map->buckets == NULL) {
Expand All @@ -77,7 +73,7 @@ void sn_map_destroy(sn_map_t *map) {
}
while (node != NULL) {
next = node->next;
if (node->active && node->unregister_cb != NULL) {
if (node->clean && node->unregister_cb != NULL) {
node->unregister_cb(node->label, node->data);
}
free(node);
Expand All @@ -92,10 +88,10 @@ bool sn_map_set(sn_map_t *map, const char *key, void *data, sn_map_unregister_cb
int bucket;
sn_map_lln_t *node = find_label_node(map, key, &bucket);
if (node != NULL) {
if (node->active && node->unregister_cb != NULL) {
if (node->clean && node->unregister_cb != NULL) {
node->unregister_cb(node->label, node->data);
}
node->active = true;
node->clean = true;
node->unregister_cb = unregister_cb;
node->data = data;
return true;
Expand All @@ -112,14 +108,14 @@ bool sn_map_set(sn_map_t *map, const char *key, void *data, sn_map_unregister_cb
}

while (node->next != NULL) {
if (!node->active) {
if (!node->clean) {
break;
}
node = node->next;
}

if (!node->active) {
node->active = true;
if (!node->clean) {
node->clean = true;
node->data = data;
node->label = key;
node->unregister_cb = unregister_cb;
Expand All @@ -138,7 +134,7 @@ bool sn_map_set(sn_map_t *map, const char *key, void *data, sn_map_unregister_cb
void *sn_map_get(sn_map_t *map, const char *key) {
int bucket;
sn_map_lln_t *node = find_label_node(map, key, &bucket);
return (node == NULL || !node->active) ? NULL : node->data;
return (node == NULL || !node->clean) ? NULL : node->data;
}

void sn_map_del(sn_map_t *map, const char *key) {
Expand All @@ -150,7 +146,7 @@ void sn_map_del(sn_map_t *map, const char *key) {
if (node->unregister_cb != NULL) {
node->unregister_cb(node->label, node->data);
}
node->active = false;
node->clean = false;
node->data = NULL;
node->unregister_cb = NULL;
map->buckets[bucket].count--;
Expand All @@ -159,5 +155,31 @@ void sn_map_del(sn_map_t *map, const char *key) {
bool sn_map_has(sn_map_t *map, const char *key) {
int bucket;
sn_map_lln_t *node = find_label_node(map, key, &bucket);
return !(node == NULL || !node->active);
return !(node == NULL || !node->clean);
}

size_t sn_map_length(sn_map_t *map) {
size_t length = 0;
for (int i = 0; i < map->bucket_size; i++) {
length += map->buckets[i].count;
}
return length;
}

void sn_map_traverse(sn_map_t *map, sn_map_traversal_cb traversal_cb) {
sn_map_lln_t *node;
for (int i = 0; i < map->bucket_size; i++) {
node = map->buckets[i].head;
if (node == NULL) {
continue;
}
while (node != NULL) {
if (!node->clean) {
node = node->next;
continue;
}
traversal_cb(node->label, node->data);
node = node->next;
}
}
}
Loading

0 comments on commit abe5e71

Please sign in to comment.