Skip to content

Develop #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ include_directories(src)
include_directories(libs)
include_directories(tests)
find_package(Threads)
set(SOURCE_FILES src/http_structures.c src/http_server.c libs/map.c src/parser.c src/dispatcher.c libs/alist.c)
set(SOURCE_FILES src/http_structures.c src/http_server.c libs/map.c src/parser.c src/dispatcher.c libs/alist.c src/api_funcs.c src/api_funcs.h)
add_executable(shabe ${SOURCE_FILES} examples/main.c)
#add_executable(shabe_test ${SOURCE_FILES} )
target_link_libraries (shabe ${CMAKE_THREAD_LIBS_INIT})
#target_link_libraries (shabe_test ${CMAKE_THREAD_LIBS_INIT})
add_executable(shabe_tests ${SOURCE_FILES} tests/tests.c)
target_link_libraries(shabe_tests ${CMAKE_THREAD_LIBS_INIT})
SET(GCC_COVERAGE_COMPILE_FLAGS "-fno-stack-protector")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# SHABE
1.0.1 latest stable version
1.0.2 latest version

*"Reinventing wheels is the best thing in the world" (c) C programmer*

Shabe is a new http-server written fully in C
Shabe is a new header-only http-server written fully in C

## Contribution
Contribution to this repo (for your experience or just fun) is highly appreciated.
@@ -17,11 +17,8 @@ Simple overview of architecture:

### Installation & Importing

1. Just download this repo and put this folder into your project's root (or anywhere you want)
2. Then you can include it in your project:
`#include "Shabe/src/http_server.h`
`#include "Shabe/src/http_structures.h"`
`#include "Shabe/src/dispatcher.h`
1. Just copy ShabeFramework.h from src folder to your project
2. Link PThread library in your building system

### Usage

@@ -37,31 +34,30 @@ Simple overview of architecture:

**Simple example:**
```c
// includes are just for my example
#include "Shabe/src/http_server.h"
#include "Shabe/src/dispatcher.h"
#include "Shabe/src/http_structures.h"
#include <http_server.h>
#include <dispatcher.h>

// Each function accepts two arguments - request and response
void hello_page(HttpRequest *req, HttpResponse *resp) {
if (req->method == GET) {
resp->status_code = 200;
strcpy(resp->data, "<html><h1>Hello from server!</h1></html");
} else if (req->method == POST){
resp->status_code = 200;
printf("New data %s has come of type %s \n", req->data, get_request_header(req, CONTENT_TYPE));
}
if (req->method == GET) {
make_response(200, "<html><h1>Hello from server!</h1></html", resp);
} else if (req->method == POST){
printf("New data %s has come of type %s \n", req->data, get_request_header(req, CONTENT_TYPE));
BAD_RESPONSE(resp);
}
}

int main() {
// initialize all memory for server
server_init();
// register our function in dispatcher
register_url("/home/", hello_page);
// or register html page from some file
register_static_url("/login/", "temp2.html");
// start to listen on 8000 port
server_listen();
// initialize all memory for server
server_init();
// set static folder
set_static_folder("./examples/");
// register our function in dispatcher
register_url("/home", hello_page);
// or register html page from some file
register_static_url("/login", "./examples/temp2.html");
// start to listen on 8000 port
server_listen();
}
```
*More functions can be found in header files*
13 changes: 6 additions & 7 deletions examples/main.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#include <http_server.h>
#include <dispatcher.h>
#include <http_structures.h>
#include <map.h>

// Each function accepts two arguments - request and response
void hello_page(HttpRequest *req, HttpResponse *resp) {
if (req->method == GET) {
resp->status_code = 200;
strcpy(resp->data, "<html><h1>Hello from server!</h1></html");
make_response(200, "<html><h1>Hello from server!</h1></html", resp);
} else if (req->method == POST){
resp->status_code = 200;
printf("New data %s has come of type %s \n", req->data, get_request_header(req, CONTENT_TYPE));
BAD_RESPONSE(resp);
}
}

int main() {
// initialize all memory for server
server_init();
// set static folder
set_static_folder("./examples/");
// register our function in dispatcher
register_url("/home/", hello_page);
register_url("/home", hello_page);
// or register html page from some file
register_static_url("/login/", "./examples/temp2.html");
register_static_url("/login", "./examples/temp2.html");
// start to listen on 8000 port
server_listen();
}
42 changes: 24 additions & 18 deletions libs/map.c
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@
#include <map.h>

struct map_node_t {
unsigned hash;
void *value;
map_node_t *next;
/* char key[]; */
/* char value[]; */
unsigned hash;
void *value;
map_node_t *next;
/* char key[]; */
/* char value[]; */
};


@@ -30,12 +30,12 @@ static unsigned map_hash(const char *str) {
static map_node_t *map_newnode(const char *key, void *value, int vsize) {
map_node_t *node;
int ksize = strlen(key) + 1;
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
int voffset = ksize + ((sizeof(void *) - ksize) % sizeof(void *));
node = malloc(sizeof(*node) + voffset + vsize);
if (!node) return NULL;
memcpy(node + 1, key, ksize);
node->hash = map_hash(key);
node->value = ((char*) (node + 1)) + voffset;
node->value = ((char *) (node + 1)) + voffset;
memcpy(node->value, value, vsize);
return node;
}
@@ -58,7 +58,7 @@ static void map_addnode(map_base_t *m, map_node_t *node) {
static int map_resize(map_base_t *m, int nbuckets) {
map_node_t *nodes, *node, *next;
map_node_t **buckets;
int i;
int i;
/* Chain all nodes together */
nodes = NULL;
i = m->nbuckets;
@@ -98,7 +98,7 @@ static map_node_t **map_getref(map_base_t *m, const char *key) {
if (m->nbuckets > 0) {
next = &m->buckets[map_bucketidx(m, hash)];
while (*next) {
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) {
if ((*next)->hash == hash && !strcmp((char *) (*next + 1), key)) {
return next;
}
next = &(*next)->next;
@@ -189,16 +189,22 @@ const char *map_next_(map_base_t *m, map_iter_t *iter) {
iter->node = m->buckets[iter->bucketidx];
} while (iter->node == NULL);
}
return (char*) (iter->node + 1);
return (char *) (iter->node + 1);
}

void map_free_all_(map_base_t *m){
const char *key;
map_iter_t iter = map_iter(m);
void map_free_all_(map_base_t *m) {
const char *key;
map_iter_t iter = map_iter(m);

while ((key = map_next_(m, &iter))) {
void **res = map_get_(m, key);
if (res != NULL)
free(*res);
}
while ((key = map_next_(m, &iter))) {
void **res = map_get_(m, key);
if (res != NULL)
free(*res);
}
}

void * map_get_val_(void **data_pointer) {
if (data_pointer == NULL)
return NULL;
return *data_pointer;
}
10 changes: 7 additions & 3 deletions libs/map.h
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ typedef struct {


#define map_t(T)\
struct { map_base_t base; T *ref; T tmp; }
struct { map_base_t base; T *ref; T tmp;}


#define map_init(m)\
@@ -39,8 +39,7 @@ typedef struct {


#define map_get(m, key)\
( (m)->ref = map_get_(&(m)->base, key) )

( (m)->ref = map_get_(&(m)->base, key))

#define map_set(m, key, value)\
( (m)->tmp = (value),\
@@ -56,16 +55,21 @@ typedef struct {
#define map_next(m, iter)\
map_next_(&(m)->base, iter)

#define map_get_val(m, key)\
(map_get_val_((void **)map_get(m, key)))

#define map_free_all(m)\
map_free_all_(&(m)->base)


void map_deinit_(map_base_t *m);
void *map_get_(map_base_t *m, const char *key);
int map_set_(map_base_t *m, const char *key, void *value, int vsize);
void map_remove_(map_base_t *m, const char *key);
map_iter_t map_iter_(void);
const char *map_next_(map_base_t *m, map_iter_t *iter);
void map_free_all_(map_base_t *m);
void *map_get_val_(void **data_pointer);

typedef map_t(void*) map_void_t;
typedef map_t(char*) map_str_t;
53 changes: 53 additions & 0 deletions src/api_funcs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by rinat on 27.04.2020.
//

#include <api_funcs.h>
#include <map.h>
#include <stdlib.h>

map_void_t url_patterns;


/**
* Init api container
*/
void api_funcs_init() {
map_init(&url_patterns);
}

/**
* Replace some url with new function processor
*/
void api_funcs_add(api_url *new_api) {
void *cur_past = api_funcs_get(new_api->url);
api_funcs_remove_and_free(cur_past);
map_set(&url_patterns, new_api->url, new_api);
}

/**
* Remove and free memory of some api processor
*/
void api_funcs_remove_and_free(api_url *api_to_remove) {
if (api_to_remove != NULL) {
map_remove(&url_patterns, api_to_remove->url);
free(api_to_remove);
}
}

/*
* Get api processor from the url
*/
api_url *api_funcs_get(char *url) {
return (api_url*) map_get_val(&url_patterns, url);
}

/**
* Free all url functions from the container
*/
void api_funcs_deinit() {
map_free_all(&url_patterns);
map_deinit(&url_patterns);
}


21 changes: 21 additions & 0 deletions src/api_funcs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by rinat on 27.04.2020.
//

#ifndef SHABEFRAMEWORK_URL_PATTERNS_H
#define SHABEFRAMEWORK_URL_PATTERNS_H

#include <dispatcher.h>
#include <http_structures.h>

void api_funcs_init();

void api_funcs_add(api_url *new_api);

void api_funcs_remove_and_free(api_url *api_to_remove);

api_url *api_funcs_get(char *url);

void api_funcs_deinit();

#endif //SHABEFRAMEWORK_URL_PATTERNS_H
Loading