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