1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
* Copyright (c) 2011 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup websrv
* @{
*/
/**
* @file Skeletal web server.
*/
#include <bool.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <net/in.h>
#include <net/inet.h>
#include <net/socket.h>
#include <arg_parse.h>
#include <macros.h>
#include <str.h>
#include <str_error.h>
#define NAME "websrv"
#define DEFAULT_PORT 8080
#define BACKLOG_SIZE 3
#define WEB_ROOT "/data/web"
/** Buffer for receiving the request. */
#define BUFFER_SIZE 1024
static uint16_t port = DEFAULT_PORT;
static char rbuf[BUFFER_SIZE];
static size_t rbuf_out;
static size_t rbuf_in;
static char lbuf[BUFFER_SIZE + 1];
static size_t lbuf_used;
static char fbuf[BUFFER_SIZE];
/** Responses to send to client. */
static const char *msg_ok =
"HTTP/1.0 200 OK\r\n"
"\r\n";
static const char *msg_bad_request =
"HTTP/1.0 400 Bad Request\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<html><head>\r\n"
"<title>400 Bad Request</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h1>Bad Request</h1>\r\n"
"<p>The requested URL has bad syntax.</p>\r\n"
"</body>\r\n"
"</html>\r\n";
static const char *msg_not_found =
"HTTP/1.0 404 Not Found\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<html><head>\r\n"
"<title>404 Not Found</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h1>Not Found</h1>\r\n"
"<p>The requested URL was not found on this server.</p>\r\n"
"</body>\r\n"
"</html>\r\n";
static const char *msg_not_implemented =
"HTTP/1.0 501 Not Implemented\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<html><head>\r\n"
"<title>501 Not Implemented</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h1>Not Implemented</h1>\r\n"
"<p>The requested method is not implemented on this server.</p>\r\n"
"</body>\r\n"
"</html>\r\n";
/** Receive one character (with buffering) */
static int recv_char(int fd, char *c)
{
if (rbuf_out == rbuf_in) {
rbuf_out = 0;
rbuf_in = 0;
ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);
if (rc <= 0) {
fprintf(stderr, "recv() failed (%zd)\n", rc);
return rc;
}
rbuf_in = rc;
}
*c = rbuf[rbuf_out++];
return EOK;
}
/** Receive one line with length limit */
static int recv_line(int fd)
{
char *bp = lbuf;
char c = '\0';
while (bp < lbuf + BUFFER_SIZE) {
char prev = c;
int rc = recv_char(fd, &c);
if (rc != EOK)
return rc;
*bp++ = c;
if ((prev == '\r') && (c == '\n'))
break;
}
lbuf_used = bp - lbuf;
*bp = '\0';
if (bp == lbuf + BUFFER_SIZE)
return ELIMIT;
return EOK;
}
static bool uri_is_valid(char *uri)
{
if (uri[0] != '/')
return false;
if (uri[1] == '.')
return false;
char *cp = uri + 1;
while (*cp != '\0') {
char c = *cp++;
if (c == '/')
return false;
}
return true;
}
static int send_response(int conn_sd, const char *msg)
{
size_t response_size = str_size(msg);
fprintf(stderr, "Sending response\n");
ssize_t rc = send(conn_sd, (void *) msg, response_size, 0);
if (rc < 0) {
fprintf(stderr, "send() failed\n");
return rc;
}
return EOK;
}
static int uri_get(const char *uri, int conn_sd)
{
if (str_cmp(uri, "/") == 0)
uri = "/index.html";
char *fname;
int rc = asprintf(&fname, "%s%s", WEB_ROOT, uri);
if (rc < 0)
return ENOMEM;
int fd = open(fname, O_RDONLY);
if (fd < 0) {
rc = send_response(conn_sd, msg_not_found);
free(fname);
return rc;
}
free(fname);
rc = send_response(conn_sd, msg_ok);
if (rc != EOK)
return rc;
while (true) {
ssize_t nr = read(fd, fbuf, BUFFER_SIZE);
if (nr == 0)
break;
if (nr < 0) {
close(fd);
return EIO;
}
rc = send(conn_sd, fbuf, nr, 0);
if (rc < 0) {
fprintf(stderr, "send() failed\n");
close(fd);
return rc;
}
}
close(fd);
return EOK;
}
static int req_process(int conn_sd)
{
int rc = recv_line(conn_sd);
if (rc != EOK) {
fprintf(stderr, "recv_line() failed\n");
return rc;
}
fprintf(stderr, "Request: %s", lbuf);
if (str_lcmp(lbuf, "GET ", 4) != 0) {
rc = send_response(conn_sd, msg_not_implemented);
return rc;
}
char *uri = lbuf + 4;
char *end_uri = str_chr(uri, ' ');
if (end_uri == NULL) {
end_uri = lbuf + lbuf_used - 2;
assert(*end_uri == '\r');
}
*end_uri = '\0';
fprintf(stderr, "Requested URI: %s\n", uri);
if (!uri_is_valid(uri)) {
rc = send_response(conn_sd, msg_bad_request);
return rc;
}
return uri_get(uri, conn_sd);
}
static void usage(void)
{
printf("Skeletal server\n"
"\n"
"Usage: " NAME " [options]\n"
"\n"
"Where options are:\n"
"-p port_number | --port=port_number\n"
"\tListening port (default " STRING(DEFAULT_PORT) ").\n"
"\n"
"-h | --help\n"
"\tShow this application help.\n");
}
static int parse_option(int argc, char *argv[], int *index)
{
int value;
int rc;
switch (argv[*index][1]) {
case 'h':
usage();
exit(0);
break;
case 'p':
rc = arg_parse_int(argc, argv, index, &value, 0);
if (rc != EOK)
return rc;
port = (uint16_t) value;
break;
/* Long options with double dash */
case '-':
if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
usage();
exit(0);
} else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
rc = arg_parse_int(argc, argv, index, &value, 7);
if (rc != EOK)
return rc;
port = (uint16_t) value;
} else {
usage();
return EINVAL;
}
break;
default:
usage();
return EINVAL;
}
return EOK;
}
int main(int argc, char *argv[])
{
/* Parse command line arguments */
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
int rc = parse_option(argc, argv, &i);
if (rc != EOK)
return rc;
} else {
usage();
return EINVAL;
}
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
int rc = inet_pton(AF_INET, "127.0.0.1", (void *)
&addr.sin_addr.s_addr);
if (rc != EOK) {
fprintf(stderr, "Error parsing network address (%s)\n",
str_error(rc));
return 1;
}
fprintf(stderr, "Creating socket\n");
int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
if (listen_sd < 0) {
fprintf(stderr, "Error creating listening socket (%s)\n",
str_error(listen_sd));
return 2;
}
rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
if (rc != EOK) {
fprintf(stderr, "Error binding socket (%s)\n",
str_error(rc));
return 3;
}
rc = listen(listen_sd, BACKLOG_SIZE);
if (rc != EOK) {
fprintf(stderr, "listen() failed (%s)\n", str_error(rc));
return 4;
}
fprintf(stderr, "Listening for connections at port %" PRIu16 "\n",
port);
while (true) {
struct sockaddr_in raddr;
socklen_t raddr_len = sizeof(raddr);
int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
&raddr_len);
if (conn_sd < 0) {
fprintf(stderr, "accept() failed (%s)\n", str_error(rc));
continue;
}
fprintf(stderr, "Connection accepted (sd=%d), "
"waiting for request\n", conn_sd);
rbuf_out = 0;
rbuf_in = 0;
rc = req_process(conn_sd);
if (rc != EOK)
fprintf(stderr, "Error processing request (%s)\n",
str_error(rc));
rc = closesocket(conn_sd);
if (rc != EOK) {
fprintf(stderr, "Error closing connection socket (%s)\n",
str_error(rc));
closesocket(listen_sd);
return 5;
}
fprintf(stderr, "Connection closed\n");
}
/* Not reached */
return 0;
}
/** @}
*/
|