1 | /* simple_web_server
|
---|
2 | Copyright (C) 2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "mongoose.h"
|
---|
20 |
|
---|
21 | #ifndef _WIN32
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <sys/wait.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | static struct mg_serve_http_opts server_opts;
|
---|
27 | static int exit_flag = 0;
|
---|
28 |
|
---|
29 | static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
---|
30 | if (ev == MG_EV_HTTP_REQUEST) {
|
---|
31 | mg_serve_http(nc, (struct http_message *) p, server_opts);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | static void __cdecl signal_handler(int sig_num) {
|
---|
36 | // Reinstantiate signal handler
|
---|
37 | signal(sig_num, signal_handler);
|
---|
38 |
|
---|
39 | #ifndef _WIN32
|
---|
40 | if (sig_num == SIGCHLD) {
|
---|
41 | do {} while (waitpid(-1, &sig_num, WNOHANG) > 0);
|
---|
42 | } else
|
---|
43 | #endif
|
---|
44 | { exit_flag = sig_num; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #ifdef _WIN32
|
---|
48 | static void checkCloseEvent() {
|
---|
49 | MSG msg;
|
---|
50 | if (PeekMessage(&msg, NULL, WM_CLOSE, WM_CLOSE, PM_NOREMOVE)) exit_flag = 1;
|
---|
51 | }
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | static void show_usage() {
|
---|
55 | printf("Usage: simple_web_server [OPTIONS] \n");
|
---|
56 | printf("%s\n", "Options:");
|
---|
57 | printf("%s\n", " -p\t port");
|
---|
58 | printf("%s\n", " -r\t document root");
|
---|
59 | printf("%s\n", " -j\t hide directory listing");
|
---|
60 | }
|
---|
61 |
|
---|
62 | int main(int argc, char *argv[]) {
|
---|
63 | struct mg_mgr mgr;
|
---|
64 | struct mg_connection *nc;
|
---|
65 |
|
---|
66 | const char *http_port = "8000";
|
---|
67 | const char *doc_root = ".";
|
---|
68 | const char *listing = "yes";
|
---|
69 | int i;
|
---|
70 |
|
---|
71 | // Parse command line options
|
---|
72 | for (i = 1; i < argc && argv[i][0] == '-'; i++) {
|
---|
73 | if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
---|
74 | http_port = argv[++i];
|
---|
75 | }
|
---|
76 | else
|
---|
77 | if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
|
---|
78 | doc_root = argv[++i];
|
---|
79 | }
|
---|
80 | else
|
---|
81 | if (strcmp(argv[i], "-j") == 0) {
|
---|
82 | listing = "no";
|
---|
83 | }
|
---|
84 | else
|
---|
85 | if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) {
|
---|
86 | show_usage();
|
---|
87 | return -1;
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | fprintf(stderr, "Error: unknown option %s \n", argv[i]);
|
---|
91 | show_usage();
|
---|
92 | return -1;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Setup signal handler: quit on Ctrl-C
|
---|
97 | signal(SIGTERM, signal_handler);
|
---|
98 | signal(SIGINT, signal_handler);
|
---|
99 | #ifndef _WIN32
|
---|
100 | signal(SIGCHLD, signal_handler);
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | mg_mgr_init(&mgr, NULL);
|
---|
104 |
|
---|
105 | printf("Starting web server on port %s\n", http_port);
|
---|
106 | printf("Document root: %s\n", doc_root);
|
---|
107 | printf("Directory listing: %s\n", listing);
|
---|
108 | fflush(stdout);
|
---|
109 |
|
---|
110 | nc = mg_bind(&mgr, http_port, ev_handler);
|
---|
111 | if (nc == NULL) {
|
---|
112 | printf("Failed to create listener\n");
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Set up HTTP server parameters
|
---|
117 | mg_set_protocol_http_websocket(nc);
|
---|
118 | server_opts.document_root = doc_root;
|
---|
119 | server_opts.enable_directory_listing = listing;
|
---|
120 |
|
---|
121 | while (exit_flag == 0) {
|
---|
122 | mg_mgr_poll(&mgr, 1000);
|
---|
123 | #ifdef _WIN32
|
---|
124 | checkCloseEvent();
|
---|
125 | #endif
|
---|
126 | }
|
---|
127 | mg_mgr_free(&mgr);
|
---|
128 |
|
---|
129 | printf("Exiting on signal %d\n", exit_flag);
|
---|
130 |
|
---|
131 | return 0;
|
---|
132 | }
|
---|