Commit | Line | Data |
---|---|---|
8512671a LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
96748ca7 | 3 | /* linker: color.o debug.o http.o server.o signal.o */ |
8512671a | 4 | |
184be781 | 5 | #include <assert.h> |
8512671a LM |
6 | #include <stdio.h> |
7 | #include <stdlib.h> | |
89f0e084 | 8 | #include <unistd.h> |
163f56b1 | 9 | #include <signal.h> |
8512671a LM |
10 | |
11 | #include "debug.h" | |
96748ca7 | 12 | #include "http.h" |
00afbb65 | 13 | #include "server.h" |
06ec8057 | 14 | #include "signal.h" |
8512671a | 15 | |
184be781 LM |
16 | /* types */ |
17 | ||
8512671a LM |
18 | /* constants */ |
19 | ||
20 | #define BUFFER_SIZE 4096 | |
21 | ||
22 | /* macros */ | |
23 | ||
24 | /* gobal variables */ | |
25 | ||
26 | char *progname = NULL; | |
21ac031b | 27 | int port = 8080; |
06ec8057 | 28 | socket_t sock = INVALID_SOCKET; |
89f0e084 | 29 | socket_t conn = INVALID_SOCKET; |
8512671a LM |
30 | |
31 | /* help function */ | |
32 | ||
33 | int usage (int ret) | |
34 | { | |
35 | FILE *fid = ret ? stderr : stdout; | |
36 | fprintf (fid, "usage: %s\n", progname); | |
37 | fprintf (fid, " -h : help message\n"); | |
21ac031b | 38 | fprintf (fid, " -p : port number (%d)\n", port); |
8512671a LM |
39 | fprintf (fid, " -v : verbose level (%d)\n", verbose); |
40 | ||
41 | return ret; | |
42 | } | |
43 | ||
06ec8057 LM |
44 | void stop_server (__attribute__((unused)) int sig) |
45 | { | |
89f0e084 LM |
46 | if (conn != INVALID_SOCKET) { |
47 | close_socket (conn); | |
48 | } | |
06ec8057 | 49 | if (sock != INVALID_SOCKET) { |
89f0e084 | 50 | close_socket (sock); |
06ec8057 | 51 | } |
89f0e084 LM |
52 | terminate_network_context (); |
53 | exit (0); | |
06ec8057 LM |
54 | } |
55 | ||
8512671a LM |
56 | /* main function */ |
57 | ||
58 | int main (int argc, char *argv[]) | |
59 | { | |
60 | int i = 0; | |
8512671a LM |
61 | |
62 | /* program name */ | |
63 | ||
64 | progname = argv[0]; | |
65 | while (progname[i] != '\0') { | |
66 | if ((progname[i] == '/') || (progname[i] == '\\')) { | |
67 | progname += i + 1; | |
68 | i = 0; | |
69 | } else { | |
70 | i++; | |
71 | } | |
72 | } | |
73 | ||
74 | /* argument processing */ | |
75 | ||
76 | while (argc-- > 1) { | |
77 | char *arg = *(++argv); | |
78 | if (arg[0] != '-') { | |
163f56b1 | 79 | VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1)); |
8512671a LM |
80 | return 1; |
81 | } | |
82 | char c = arg[1]; | |
83 | switch (c) { | |
21ac031b | 84 | case 'p': |
8512671a LM |
85 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
86 | if (arg == NULL) { | |
163f56b1 | 87 | VERBOSE (ERROR, PERROR ("%s: missing port number\n", progname); usage (1)); |
21ac031b LM |
88 | return 1; |
89 | } | |
90 | port = atoi (arg); | |
91 | if (port <= 0) { | |
163f56b1 | 92 | VERBOSE (ERROR, PERROR ("%s: incorrect port number (%s)\n", progname, arg); usage (1)); |
8512671a LM |
93 | return 1; |
94 | } | |
8512671a | 95 | break; |
8512671a LM |
96 | case 'v': |
97 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; | |
98 | if (arg == NULL) { | |
163f56b1 | 99 | VERBOSE (ERROR, PERROR ("%s: missing verbose level\n", progname); usage (1)); |
8512671a LM |
100 | return 1; |
101 | } | |
102 | verbose = atoi (arg); | |
103 | break; | |
104 | case 'h': | |
105 | default: | |
106 | return usage (c != 'h'); | |
107 | } | |
108 | } | |
109 | ||
163f56b1 | 110 | VERBOSE (DEBUG, PRINT ("Initializing socket\n")); |
89f0e084 | 111 | init_network_context (); |
06ec8057 LM |
112 | sock = open_listening_socket (port); |
113 | if (sock == INVALID_SOCKET) { | |
163f56b1 | 114 | VERBOSE (ERROR, PERROR ("Can't open listening socket\n")); |
184be781 LM |
115 | return 1; |
116 | } | |
163f56b1 LM |
117 | VERBOSE (INFO, PRINT ("Listening socket on port %d\n", port)); |
118 | ||
119 | signal (SIGTERM, &stop_server); | |
04a2223d LM |
120 | |
121 | /* main loop */ | |
122 | while (1) { | |
89f0e084 LM |
123 | conn = accept_incoming_connection (sock); |
124 | if (conn == INVALID_SOCKET) { | |
125 | usleep (1e5); | |
04a2223d LM |
126 | continue; |
127 | } | |
128 | ||
163f56b1 | 129 | VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n")); |
04a2223d | 130 | |
89f0e084 | 131 | unsigned char *data = {0}; |
04a2223d | 132 | |
89f0e084 LM |
133 | int len = receive_data (conn, &data); |
134 | if (len == 0) { | |
163f56b1 | 135 | VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n")); |
89f0e084 | 136 | } else if (len < 0) { |
163f56b1 | 137 | VERBOSE (WARNING, PRINT ("Connection in error (rx)\n")); |
89f0e084 | 138 | } else { |
96748ca7 | 139 | VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len)); |
04a2223d LM |
140 | |
141 | // processing | |
96748ca7 ML |
142 | VERBOSE (DEBUG, PRINT ("Processing %s\n", data)); |
143 | len = processing (data, len, &data); | |
89f0e084 LM |
144 | |
145 | int rc = send_data (conn, data, len); | |
146 | if (rc == 0) { | |
163f56b1 | 147 | VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n")); |
89f0e084 | 148 | } else if (rc < 0) { |
163f56b1 | 149 | VERBOSE (WARNING, PRINT ("Connection in error (tx)\n")); |
04a2223d | 150 | } |
89f0e084 | 151 | } |
04a2223d | 152 | |
163f56b1 | 153 | VERBOSE (DEBUG, PRINT ("Closing connection\n")); |
89f0e084 LM |
154 | if (data) { |
155 | free (data); | |
04a2223d | 156 | } |
89f0e084 LM |
157 | close_socket (conn); |
158 | conn = INVALID_SOCKET; | |
04a2223d | 159 | } |
89f0e084 | 160 | |
163f56b1 | 161 | VERBOSE (DEBUG, PRINT ("Closing socket\n")); |
89f0e084 LM |
162 | close_socket (sock); |
163 | terminate_network_context (); | |
184be781 | 164 | |
06ec8057 | 165 | return 2; |
8512671a LM |
166 | } |
167 | ||
168 | // test: webserver.exe -h | |
169 | // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
21ac031b | 170 | // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 = |
8512671a LM |
171 | // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
172 | // test: webserver.exe error 2>&1 | grep -q 'invalid option' | |
21ac031b LM |
173 | // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level' |
174 | // test: webserver.exe -p 2>&1 | grep -q 'missing port number' | |
175 | // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number' | |
163f56b1 LM |
176 | // test: webserver.exe > test.log & pid=$!; sleep 1; kill -9 $pid; grep -q 'Listening socket on port 8080' test.log |
177 | // test: webserver.exe -p 8000 > test.log & pid=$!; sleep 1; kill -9 $pid; grep -q 'Listening socket on port 8000' test.log | |
178 | // test: webserver.exe & pid=$!; sleep 1; kill -TERM $pid; ps aux | grep -q [w]ebserver.exe && kill -9 $pid || rc=1; test x$rc = x1 | |
8512671a LM |
179 | |
180 | /* vim: set ts=4 sw=4 et: */ |