forked from cesanta/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge dev branch code named Fossa as next stable Mongoose
- Loading branch information
Marko Mikulicic
committed
Sep 8, 2015
1 parent
28eb251
commit 8927c9d
Showing
126 changed files
with
161,499 additions
and
7,952 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,15 @@ Copyright (c) 2004-2013 Sergey Lyubka <[email protected]> | |
Copyright (c) 2013-2015 Cesanta Software Limited | ||
All rights reserved | ||
|
||
This code is dual-licensed: you can redistribute it and/or modify | ||
This software is dual-licensed: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License version 2 as | ||
published by the Free Software Foundation. For the terms of this | ||
license, see <http://www.gnu.org/licenses>. | ||
license, see <http://www.gnu.org/licenses/>. | ||
|
||
You are free to use this code under the terms of the GNU General | ||
You are free to use this software under the terms of the GNU General | ||
Public License, but WITHOUT ANY WARRANTY; without even the implied | ||
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
See the GNU General Public License for more details. | ||
|
||
Alternatively, you can license this code under a commercial | ||
license, as set out in <http://cesanta.com/>. | ||
Alternatively, you can license this software under a commercial | ||
license, as set out in <https://www.cesanta.com/license>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
# Copyright (c) 2014 Cesanta Software | ||
# All rights reserved | ||
|
||
SUBDIRS = $(sort $(filter-out csharp/, $(dir $(wildcard */)))) | ||
SUBDIRS = $(sort $(dir $(wildcard */))) | ||
X = $(SUBDIRS) | ||
ifdef WINDIR | ||
# appending the Winsock2 library at the end of the compiler | ||
# invocation | ||
CFLAGS_EXTRA += -lws2_32 | ||
endif | ||
|
||
.PHONY: $(SUBDIRS) | ||
|
||
all: $(SUBDIRS) | ||
|
||
$(SUBDIRS): | ||
@$(MAKE) CFLAGS_EXTRA="$(CFLAGS_EXTRA)" -C $@ | ||
@$(MAKE) -C $@ | ||
|
||
clean: | ||
for d in $(SUBDIRS) ; do $(MAKE) -C $$d clean ; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
PROG = api_server | ||
SOURCES = $(PROG).c sqlite3.c db_plugin_sqlite.c ../../mongoose.c | ||
CFLAGS = -W -Wall -pthread $(CFLAGS_EXTRA) | ||
|
||
ifeq ($(OS), Windows_NT) | ||
else | ||
UNAME_S := $(shell uname -s) | ||
ifeq ($(UNAME_S), Linux) | ||
CFLAGS += -ldl -lm | ||
endif | ||
endif | ||
|
||
all: $(PROG) | ||
|
||
$(PROG): $(SOURCES) | ||
$(CC) $(SOURCES) -o $@ $(CFLAGS) | ||
|
||
$(PROG).exe: $(SOURCES) | ||
cl $(SOURCES) /I.. /MD /Fe$@ | ||
|
||
test: $(PROG) | ||
sh unit_test.sh $$(pwd)/$(PROG) | ||
|
||
clean: | ||
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2014 Cesanta Software Limited | ||
* All rights reserved | ||
*/ | ||
|
||
#include "db_plugin.h" | ||
|
||
static const char *s_http_port = "8000"; | ||
static struct mg_serve_http_opts s_http_server_opts; | ||
static int s_sig_num = 0; | ||
static void *s_db_handle = NULL; | ||
static const char *s_db_path = "api_server.db"; | ||
static const struct mg_str s_get_method = NS_STR("GET"); | ||
static const struct mg_str s_put_method = NS_STR("PUT"); | ||
static const struct mg_str s_delele_method = NS_STR("DELETE"); | ||
|
||
static void signal_handler(int sig_num) { | ||
signal(sig_num, signal_handler); | ||
s_sig_num = sig_num; | ||
} | ||
|
||
static int has_prefix(const struct mg_str *uri, const struct mg_str *prefix) { | ||
return uri->len > prefix->len && memcmp(uri->p, prefix->p, prefix->len) == 0; | ||
} | ||
|
||
static int is_equal(const struct mg_str *s1, const struct mg_str *s2) { | ||
return s1->len == s2->len && memcmp(s1->p, s2->p, s2->len) == 0; | ||
} | ||
|
||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { | ||
static const struct mg_str api_prefix = NS_STR("/api/v1"); | ||
struct http_message *hm = (struct http_message *) ev_data; | ||
struct mg_str key; | ||
|
||
switch (ev) { | ||
case NS_HTTP_REQUEST: | ||
if (has_prefix(&hm->uri, &api_prefix)) { | ||
key.p = hm->uri.p + api_prefix.len; | ||
key.len = hm->uri.len - api_prefix.len; | ||
if (is_equal(&hm->method, &s_get_method)) { | ||
db_op(nc, hm, &key, s_db_handle, API_OP_GET); | ||
} else if (is_equal(&hm->method, &s_put_method)) { | ||
db_op(nc, hm, &key, s_db_handle, API_OP_SET); | ||
} else if (is_equal(&hm->method, &s_delele_method)) { | ||
db_op(nc, hm, &key, s_db_handle, API_OP_DEL); | ||
} else { | ||
mg_printf(nc, "%s", | ||
"HTTP/1.0 501 Not Implemented\r\n" | ||
"Content-Length: 0\r\n\r\n"); | ||
} | ||
} else { | ||
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
struct mg_mgr mgr; | ||
struct mg_connection *nc; | ||
int i; | ||
|
||
/* Open listening socket */ | ||
mg_mgr_init(&mgr, NULL); | ||
nc = mg_bind(&mgr, s_http_port, ev_handler); | ||
mg_set_protocol_http_websocket(nc); | ||
s_http_server_opts.document_root = "web_root"; | ||
|
||
/* Parse command line arguments */ | ||
for (i = 1; i < argc; i++) { | ||
if (strcmp(argv[i], "-D") == 0) { | ||
mgr.hexdump_file = argv[++i]; | ||
} else if (strcmp(argv[i], "-f") == 0) { | ||
s_db_path = argv[++i]; | ||
} else if (strcmp(argv[i], "-r") == 0) { | ||
s_http_server_opts.document_root = argv[++i]; | ||
} | ||
} | ||
|
||
signal(SIGINT, signal_handler); | ||
signal(SIGTERM, signal_handler); | ||
|
||
/* Open database */ | ||
if ((s_db_handle = db_open(s_db_path)) == NULL) { | ||
fprintf(stderr, "Cannot open DB [%s]\n", s_db_path); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
/* Run event loop until signal is received */ | ||
printf("Starting RESTful server on port %s\n", s_http_port); | ||
while (s_sig_num == 0) { | ||
mg_mgr_poll(&mgr, 1000); | ||
} | ||
|
||
/* Cleanup */ | ||
mg_mgr_free(&mgr); | ||
db_close(&s_db_handle); | ||
|
||
printf("Exiting on signal %d\n", s_sig_num); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright (c) 2014 Cesanta Software Limited | ||
* All rights reserved | ||
*/ | ||
|
||
#ifndef DB_PLUGIN_HEADER_DEFINED | ||
#define DB_PLUGIN_HEADER_DEFINED | ||
|
||
#include "../../mongoose.h" | ||
|
||
void *db_open(const char *db_path); | ||
void db_close(void **db_handle); | ||
|
||
enum { API_OP_GET, API_OP_SET, API_OP_DEL }; | ||
|
||
void db_op(struct mg_connection *nc, const struct http_message *hm, | ||
const struct mg_str *key, void *db, int op); | ||
|
||
#endif /* DB_PLUGIN_HEADER_DEFINED */ |
Oops, something went wrong.