2015-09-08 19:49:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Cesanta Software Limited
|
|
|
|
* All rights reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
static const char *s_http_port = "8000";
|
|
|
|
static struct mg_serve_http_opts s_http_server_opts;
|
|
|
|
|
|
|
|
static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) {
|
|
|
|
char n1[100], n2[100];
|
|
|
|
double result;
|
|
|
|
|
|
|
|
/* Get form variables */
|
|
|
|
mg_get_http_var(&hm->body, "n1", n1, sizeof(n1));
|
|
|
|
mg_get_http_var(&hm->body, "n2", n2, sizeof(n2));
|
|
|
|
|
|
|
|
/* Send headers */
|
|
|
|
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
|
|
|
|
|
|
|
/* Compute the result and send it back as a JSON object */
|
|
|
|
result = strtod(n1, NULL) + strtod(n2, NULL);
|
|
|
|
mg_printf_http_chunk(nc, "{ \"result\": %lf }", result);
|
2016-04-25 23:40:38 +08:00
|
|
|
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
|
2015-09-08 19:49:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
|
|
|
|
|
|
|
switch (ev) {
|
2015-09-19 19:38:46 +08:00
|
|
|
case MG_EV_HTTP_REQUEST:
|
2015-09-08 19:49:03 +08:00
|
|
|
if (mg_vcmp(&hm->uri, "/api/v1/sum") == 0) {
|
2016-04-25 23:40:38 +08:00
|
|
|
handle_sum_call(nc, hm); /* Handle RESTful call */
|
2015-09-08 19:49:03 +08:00
|
|
|
} else if (mg_vcmp(&hm->uri, "/printcontent") == 0) {
|
|
|
|
char buf[100] = {0};
|
|
|
|
memcpy(buf, hm->body.p,
|
2016-04-25 23:40:38 +08:00
|
|
|
sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len);
|
2015-09-08 19:49:03 +08:00
|
|
|
printf("%s\n", buf);
|
|
|
|
} else {
|
2016-04-25 23:40:38 +08:00
|
|
|
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
|
2015-09-08 19:49:03 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
struct mg_connection *nc;
|
2016-08-23 06:04:44 +08:00
|
|
|
struct mg_bind_opts bind_opts;
|
2015-09-08 19:49:03 +08:00
|
|
|
int i;
|
|
|
|
char *cp;
|
2016-08-23 06:04:44 +08:00
|
|
|
const char *err_str;
|
Change from using #ifdef to #if for features tests
"#if FOO" still works with simple -DFOO, but gives more flexibility.
Specifically, if user expressed no preference (FOO is not defined),
we can apply reasonable defaults (this is the legitimate use of ifdef).
In short, from now on, please use
#if MG_ENABLE_FOO
instead of
#ifdef MG_ENABLE_FOO
Since we are all used to #ifdef, this change also adds a precommit check
to police this. Specifically, in *.h and *.c files that are Copyright Cesanta,
"ifdef" and "if defined()" are not allowed to be used with macros that contain
ENABLE or DISABLE, unless the like also contains "ifdef-ok".
Hence, if you are sure you want to use ifdef, use this:
#ifdef MG_ENABLE_FOO /* ifdef-ok */
PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a
2016-10-14 01:55:08 +08:00
|
|
|
#if MG_ENABLE_SSL
|
2015-09-14 03:08:24 +08:00
|
|
|
const char *ssl_cert = NULL;
|
|
|
|
#endif
|
2015-09-08 19:49:03 +08:00
|
|
|
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
|
|
|
2016-04-25 23:40:38 +08:00
|
|
|
/* Use current binary directory as document root */
|
|
|
|
if (argc > 0 && ((cp = strrchr(argv[0], DIRSEP)) != NULL)) {
|
|
|
|
*cp = '\0';
|
|
|
|
s_http_server_opts.document_root = argv[0];
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
/* Process command line options to customize HTTP server */
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) {
|
|
|
|
mgr.hexdump_file = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
|
|
|
|
s_http_server_opts.document_root = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
|
|
|
s_http_port = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) {
|
|
|
|
s_http_server_opts.auth_domain = argv[++i];
|
Change from using #ifdef to #if for features tests
"#if FOO" still works with simple -DFOO, but gives more flexibility.
Specifically, if user expressed no preference (FOO is not defined),
we can apply reasonable defaults (this is the legitimate use of ifdef).
In short, from now on, please use
#if MG_ENABLE_FOO
instead of
#ifdef MG_ENABLE_FOO
Since we are all used to #ifdef, this change also adds a precommit check
to police this. Specifically, in *.h and *.c files that are Copyright Cesanta,
"ifdef" and "if defined()" are not allowed to be used with macros that contain
ENABLE or DISABLE, unless the like also contains "ifdef-ok".
Hence, if you are sure you want to use ifdef, use this:
#ifdef MG_ENABLE_FOO /* ifdef-ok */
PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a
2016-10-14 01:55:08 +08:00
|
|
|
#if MG_ENABLE_JAVASCRIPT
|
2015-09-22 00:58:45 +08:00
|
|
|
} else if (strcmp(argv[i], "-j") == 0 && i + 1 < argc) {
|
|
|
|
const char *init_file = argv[++i];
|
|
|
|
mg_enable_javascript(&mgr, v7_create(), init_file);
|
|
|
|
#endif
|
2015-09-08 19:49:03 +08:00
|
|
|
} else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
|
|
|
|
s_http_server_opts.global_auth_file = argv[++i];
|
2015-11-27 05:37:20 +08:00
|
|
|
} else if (strcmp(argv[i], "-A") == 0 && i + 1 < argc) {
|
2015-09-08 19:49:03 +08:00
|
|
|
s_http_server_opts.per_directory_auth_file = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
|
|
|
|
s_http_server_opts.url_rewrites = argv[++i];
|
2016-10-15 00:58:14 +08:00
|
|
|
#if MG_ENABLE_HTTP_CGI
|
2015-09-08 19:49:03 +08:00
|
|
|
} else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
|
|
|
|
s_http_server_opts.cgi_interpreter = argv[++i];
|
|
|
|
#endif
|
Change from using #ifdef to #if for features tests
"#if FOO" still works with simple -DFOO, but gives more flexibility.
Specifically, if user expressed no preference (FOO is not defined),
we can apply reasonable defaults (this is the legitimate use of ifdef).
In short, from now on, please use
#if MG_ENABLE_FOO
instead of
#ifdef MG_ENABLE_FOO
Since we are all used to #ifdef, this change also adds a precommit check
to police this. Specifically, in *.h and *.c files that are Copyright Cesanta,
"ifdef" and "if defined()" are not allowed to be used with macros that contain
ENABLE or DISABLE, unless the like also contains "ifdef-ok".
Hence, if you are sure you want to use ifdef, use this:
#ifdef MG_ENABLE_FOO /* ifdef-ok */
PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a
2016-10-14 01:55:08 +08:00
|
|
|
#if MG_ENABLE_SSL
|
2015-09-08 19:49:03 +08:00
|
|
|
} else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
|
2015-09-14 03:08:24 +08:00
|
|
|
ssl_cert = argv[++i];
|
2015-09-08 19:49:03 +08:00
|
|
|
#endif
|
2015-09-22 00:58:45 +08:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown option: [%s]\n", argv[i]);
|
|
|
|
exit(1);
|
2015-09-08 19:49:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set HTTP server options */
|
2016-08-23 06:04:44 +08:00
|
|
|
memset(&bind_opts, 0, sizeof(bind_opts));
|
|
|
|
bind_opts.error_string = &err_str;
|
Change from using #ifdef to #if for features tests
"#if FOO" still works with simple -DFOO, but gives more flexibility.
Specifically, if user expressed no preference (FOO is not defined),
we can apply reasonable defaults (this is the legitimate use of ifdef).
In short, from now on, please use
#if MG_ENABLE_FOO
instead of
#ifdef MG_ENABLE_FOO
Since we are all used to #ifdef, this change also adds a precommit check
to police this. Specifically, in *.h and *.c files that are Copyright Cesanta,
"ifdef" and "if defined()" are not allowed to be used with macros that contain
ENABLE or DISABLE, unless the like also contains "ifdef-ok".
Hence, if you are sure you want to use ifdef, use this:
#ifdef MG_ENABLE_FOO /* ifdef-ok */
PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a
2016-10-14 01:55:08 +08:00
|
|
|
#if MG_ENABLE_SSL
|
2015-09-14 03:08:24 +08:00
|
|
|
if (ssl_cert != NULL) {
|
2016-08-23 06:04:44 +08:00
|
|
|
bind_opts.ssl_cert = ssl_cert;
|
2015-09-14 03:08:24 +08:00
|
|
|
}
|
|
|
|
#endif
|
2016-08-23 06:04:44 +08:00
|
|
|
nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts);
|
|
|
|
if (nc == NULL) {
|
|
|
|
fprintf(stderr, "Error starting server on port %s: %s\n", s_http_port,
|
|
|
|
*bind_opts.error_string);
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-14 03:08:24 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_set_protocol_http_websocket(nc);
|
|
|
|
s_http_server_opts.enable_directory_listing = "yes";
|
|
|
|
|
2016-04-25 23:40:38 +08:00
|
|
|
printf("Starting RESTful server on port %s, serving %s\n", s_http_port,
|
|
|
|
s_http_server_opts.document_root);
|
2015-09-08 19:49:03 +08:00
|
|
|
for (;;) {
|
|
|
|
mg_mgr_poll(&mgr, 1000);
|
|
|
|
}
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|