Compare commits
2 Commits
599ea0fbb6
...
4a306746f9
| Author | SHA1 | Date | |
|---|---|---|---|
|
4a306746f9
|
|||
|
3dc05cccb0
|
@@ -1,14 +1,14 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
INCLUDE := include
|
INCLUDE := include
|
||||||
CFLAGS = -Wall -I$(INCLUDE)
|
CFLAGS = -Wall -I$(INCLUDE) -g
|
||||||
|
|
||||||
BUILD_DIR := build
|
BUILD_DIR := build
|
||||||
|
|
||||||
all: server
|
all: server
|
||||||
|
|
||||||
server: $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o
|
server: $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o $(BUILD_DIR)/array.o
|
||||||
$(CC) $(CFLAGS) -o $(BUILD_DIR)/server $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o
|
$(CC) $(CFLAGS) -o $(BUILD_DIR)/server $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o $(BUILD_DIR)/array.o
|
||||||
|
|
||||||
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
|
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|||||||
30
server/array.c
Normal file
30
server/array.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include "include/array.h"
|
||||||
|
#include "include/mongoose.h"
|
||||||
|
|
||||||
|
void array_mg_str_append(array_mg_str* arr, struct mg_str item) {
|
||||||
|
if (arr->count >= arr->capacity) {
|
||||||
|
if (arr->capacity == 0) arr->capacity = 8;
|
||||||
|
else arr->capacity *= 2;
|
||||||
|
|
||||||
|
arr->items = realloc(arr->items, arr->capacity * sizeof(*arr->items));
|
||||||
|
}
|
||||||
|
arr->items[arr->count++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
void split_mg_str(struct mg_str* string, const char token, array_mg_str* return_array) {
|
||||||
|
int start_index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < string->len; i++) {
|
||||||
|
if (string->buf[i] == token || string->len == i + 1) {
|
||||||
|
if (i != start_index) {
|
||||||
|
int slice_len = i - start_index;
|
||||||
|
if (string->len == i + 1 && string->buf[i] != token) slice_len++;
|
||||||
|
|
||||||
|
struct mg_str tmp_mg_str = mg_str_n(string->buf + start_index, slice_len);
|
||||||
|
|
||||||
|
array_mg_str_append(return_array, tmp_mg_str);
|
||||||
|
}
|
||||||
|
start_index = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
server/include/array.h
Normal file
15
server/include/array.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef HMAIL_ARRAY_H
|
||||||
|
#define HMAIL_ARRAY_H
|
||||||
|
|
||||||
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
struct mg_str* items;
|
||||||
|
size_t count;
|
||||||
|
size_t capacity;
|
||||||
|
} array_mg_str;
|
||||||
|
|
||||||
|
void array_mg_str_append(array_mg_str* arr, struct mg_str item);
|
||||||
|
void split_mg_str(struct mg_str* string, const char token, array_mg_str* return_array);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
#include "include/mongoose.h"
|
#include "include/mongoose.h"
|
||||||
|
#include "include/array.h"
|
||||||
|
|
||||||
|
static void api_handler(struct mg_connection *c, int ev, struct mg_http_message *hm) {
|
||||||
|
mg_http_reply(c, 200, "", "%s : %.*s", "api route response", hm->uri.len, hm->uri.buf);
|
||||||
|
}
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||||
if (ev == MG_EV_HTTP_MSG) {
|
if (ev == MG_EV_HTTP_MSG) {
|
||||||
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
|
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
|
||||||
printf("URL: %.*s\n", (int) hm->uri.len, hm->uri.buf);
|
|
||||||
if (mg_match(hm->uri, mg_str("/api/test"), NULL)) {
|
array_mg_str path_array = {0};
|
||||||
mg_http_reply(c, 200, "", "%s", "test");
|
split_mg_str(&hm->uri, '/', &path_array);
|
||||||
|
if (path_array.count > 0) {
|
||||||
|
if (mg_match(path_array.items[0], mg_str("api"), NULL)) {
|
||||||
|
api_handler(c, ev, hm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
mg_http_reply(c, 200, "", "%s", "<html>Hello world</html>");
|
mg_http_reply(c, 200, "", "%s", "<html>Hello world</html>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user