source: mainline/uspace/app/websrv/websrv.c@ 5302493

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5302493 was d076f16, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Websrv cannot use global buffers to handle HTTP requests concurrently.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup websrv
30 * @{
31 */
32/**
33 * @file Skeletal web server.
34 */
35
36#include <errno.h>
37#include <assert.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <stdint.h>
41#include <stddef.h>
42#include <stdlib.h>
43#include <task.h>
44
45#include <vfs/vfs.h>
46
47#include <inet/addr.h>
48#include <inet/endpoint.h>
49#include <inet/tcp.h>
50
51#include <arg_parse.h>
52#include <macros.h>
53#include <str.h>
54#include <str_error.h>
55
56#define NAME "websrv"
57
58#define DEFAULT_PORT 8080
59
60#define WEB_ROOT "/data/web"
61
62/** Buffer for receiving the request. */
63#define BUFFER_SIZE 1024
64
65static void websrv_new_conn(tcp_listener_t *, tcp_conn_t *);
66
67static tcp_listen_cb_t listen_cb = {
68 .new_conn = websrv_new_conn
69};
70
71static tcp_cb_t conn_cb = {
72 .connected = NULL
73};
74
75static uint16_t port = DEFAULT_PORT;
76
77typedef struct {
78 tcp_conn_t *conn;
79
80 char rbuf[BUFFER_SIZE];
81 size_t rbuf_out;
82 size_t rbuf_in;
83
84 char lbuf[BUFFER_SIZE + 1];
85 size_t lbuf_used;
86} recv_t;
87
88static bool verbose = false;
89
90/** Responses to send to client. */
91
92static const char *msg_ok =
93 "HTTP/1.0 200 OK\r\n"
94 "\r\n";
95
96static const char *msg_bad_request =
97 "HTTP/1.0 400 Bad Request\r\n"
98 "\r\n"
99 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
100 "<html><head>\r\n"
101 "<title>400 Bad Request</title>\r\n"
102 "</head>\r\n"
103 "<body>\r\n"
104 "<h1>Bad Request</h1>\r\n"
105 "<p>The requested URL has bad syntax.</p>\r\n"
106 "</body>\r\n"
107 "</html>\r\n";
108
109static const char *msg_not_found =
110 "HTTP/1.0 404 Not Found\r\n"
111 "\r\n"
112 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
113 "<html><head>\r\n"
114 "<title>404 Not Found</title>\r\n"
115 "</head>\r\n"
116 "<body>\r\n"
117 "<h1>Not Found</h1>\r\n"
118 "<p>The requested URL was not found on this server.</p>\r\n"
119 "</body>\r\n"
120 "</html>\r\n";
121
122static const char *msg_not_implemented =
123 "HTTP/1.0 501 Not Implemented\r\n"
124 "\r\n"
125 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
126 "<html><head>\r\n"
127 "<title>501 Not Implemented</title>\r\n"
128 "</head>\r\n"
129 "<body>\r\n"
130 "<h1>Not Implemented</h1>\r\n"
131 "<p>The requested method is not implemented on this server.</p>\r\n"
132 "</body>\r\n"
133 "</html>\r\n";
134
135
136static int recv_create(tcp_conn_t *conn, recv_t **rrecv)
137{
138 recv_t *recv;
139
140 recv = calloc(1, sizeof(recv_t));
141 if (recv == NULL)
142 return ENOMEM;
143
144 recv->conn = conn;
145 recv->rbuf_out = 0;
146 recv->rbuf_in = 0;
147 recv->lbuf_used = 0;
148
149 *rrecv = recv;
150 return EOK;
151}
152
153static void recv_destroy(recv_t *recv)
154{
155 free(recv);
156}
157
158/** Receive one character (with buffering) */
159static int recv_char(recv_t *recv, char *c)
160{
161 size_t nrecv;
162 int rc;
163
164 if (recv->rbuf_out == recv->rbuf_in) {
165 recv->rbuf_out = 0;
166 recv->rbuf_in = 0;
167
168 rc = tcp_conn_recv_wait(recv->conn, recv->rbuf, BUFFER_SIZE, &nrecv);
169 if (rc != EOK) {
170 fprintf(stderr, "tcp_conn_recv() failed (%d)\n", rc);
171 return rc;
172 }
173
174 recv->rbuf_in = nrecv;
175 }
176
177 *c = recv->rbuf[recv->rbuf_out++];
178 return EOK;
179}
180
181/** Receive one line with length limit */
182static int recv_line(recv_t *recv, char **rbuf)
183{
184 char *bp = recv->lbuf;
185 char c = '\0';
186
187 while (bp < recv->lbuf + BUFFER_SIZE) {
188 char prev = c;
189 int rc = recv_char(recv, &c);
190
191 if (rc != EOK)
192 return rc;
193
194 *bp++ = c;
195 if ((prev == '\r') && (c == '\n'))
196 break;
197 }
198
199 recv->lbuf_used = bp - recv->lbuf;
200 *bp = '\0';
201
202 if (bp == recv->lbuf + BUFFER_SIZE)
203 return ELIMIT;
204
205 *rbuf = recv->lbuf;
206 return EOK;
207}
208
209static bool uri_is_valid(char *uri)
210{
211 if (uri[0] != '/')
212 return false;
213
214 if (uri[1] == '.')
215 return false;
216
217 char *cp = uri + 1;
218
219 while (*cp != '\0') {
220 char c = *cp++;
221 if (c == '/')
222 return false;
223 }
224
225 return true;
226}
227
228static int send_response(tcp_conn_t *conn, const char *msg)
229{
230 size_t response_size = str_size(msg);
231
232 if (verbose)
233 fprintf(stderr, "Sending response\n");
234
235 int rc = tcp_conn_send(conn, (void *) msg, response_size);
236 if (rc != EOK) {
237 fprintf(stderr, "tcp_conn_send() failed\n");
238 return rc;
239 }
240
241 return EOK;
242}
243
244static int uri_get(const char *uri, tcp_conn_t *conn)
245{
246 char *fbuf = NULL;
247 char *fname = NULL;
248 int rc;
249 int fd = -1;
250
251 fbuf = calloc(BUFFER_SIZE, 1);
252 if (fbuf == NULL) {
253 rc = ENOMEM;
254 goto out;
255 }
256
257 if (str_cmp(uri, "/") == 0)
258 uri = "/index.html";
259
260 rc = asprintf(&fname, "%s%s", WEB_ROOT, uri);
261 if (rc < 0) {
262 rc = ENOMEM;
263 goto out;
264 }
265
266 fd = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ);
267 if (fd < 0) {
268 rc = send_response(conn, msg_not_found);
269 goto out;
270 }
271
272 free(fname);
273 fname = NULL;
274
275 rc = send_response(conn, msg_ok);
276 if (rc != EOK)
277 goto out;
278
279 aoff64_t pos = 0;
280 while (true) {
281 ssize_t nr = vfs_read(fd, &pos, fbuf, BUFFER_SIZE);
282 if (nr == 0)
283 break;
284
285 if (nr < 0) {
286 rc = EIO;
287 goto out;
288 }
289
290 rc = tcp_conn_send(conn, fbuf, nr);
291 if (rc != EOK) {
292 fprintf(stderr, "tcp_conn_send() failed\n");
293 goto out;
294 }
295 }
296
297 rc = EOK;
298out:
299 if (fd >= 0)
300 vfs_put(fd);
301 free(fname);
302 free(fbuf);
303 return rc;
304}
305
306static int req_process(tcp_conn_t *conn, recv_t *recv)
307{
308 char *reqline = NULL;
309
310 int rc = recv_line(recv, &reqline);
311 if (rc != EOK) {
312 fprintf(stderr, "recv_line() failed\n");
313 return rc;
314 }
315
316 if (verbose)
317 fprintf(stderr, "Request: %s", reqline);
318
319 if (str_lcmp(reqline, "GET ", 4) != 0) {
320 rc = send_response(conn, msg_not_implemented);
321 return rc;
322 }
323
324 char *uri = reqline + 4;
325 char *end_uri = str_chr(uri, ' ');
326 if (end_uri == NULL) {
327 end_uri = reqline + str_size(reqline) - 2;
328 assert(*end_uri == '\r');
329 }
330
331 *end_uri = '\0';
332 if (verbose)
333 fprintf(stderr, "Requested URI: %s\n", uri);
334
335 if (!uri_is_valid(uri)) {
336 rc = send_response(conn, msg_bad_request);
337 return rc;
338 }
339
340 return uri_get(uri, conn);
341}
342
343static void usage(void)
344{
345 printf("Simple web server\n"
346 "\n"
347 "Usage: " NAME " [options]\n"
348 "\n"
349 "Where options are:\n"
350 "-p port_number | --port=port_number\n"
351 "\tListening port (default " STRING(DEFAULT_PORT) ").\n"
352 "\n"
353 "-h | --help\n"
354 "\tShow this application help.\n"
355 "-v | --verbose\n"
356 "\tVerbose mode\n");
357}
358
359static int parse_option(int argc, char *argv[], int *index)
360{
361 int value;
362 int rc;
363
364 switch (argv[*index][1]) {
365 case 'h':
366 usage();
367 exit(0);
368 break;
369 case 'p':
370 rc = arg_parse_int(argc, argv, index, &value, 0);
371 if (rc != EOK)
372 return rc;
373
374 port = (uint16_t) value;
375 break;
376 case 'v':
377 verbose = true;
378 break;
379 /* Long options with double dash */
380 case '-':
381 if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
382 usage();
383 exit(0);
384 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
385 rc = arg_parse_int(argc, argv, index, &value, 7);
386 if (rc != EOK)
387 return rc;
388
389 port = (uint16_t) value;
390 } else if (str_cmp(argv[*index] +2, "verbose") == 0) {
391 verbose = true;
392 } else {
393 usage();
394 return EINVAL;
395 }
396 break;
397 default:
398 usage();
399 return EINVAL;
400 }
401
402 return EOK;
403}
404
405static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
406{
407 int rc;
408 recv_t *recv = NULL;
409
410 if (verbose)
411 fprintf(stderr, "New connection, waiting for request\n");
412
413 rc = recv_create(conn, &recv);
414 if (rc != EOK) {
415 fprintf(stderr, "Out of memory.\n");
416 goto error;
417 }
418
419 rc = req_process(conn, recv);
420 if (rc != EOK) {
421 fprintf(stderr, "Error processing request (%s)\n",
422 str_error(rc));
423 goto error;
424 }
425
426 rc = tcp_conn_send_fin(conn);
427 if (rc != EOK) {
428 fprintf(stderr, "Error sending FIN.\n");
429 goto error;
430 }
431
432 recv_destroy(recv);
433 return;
434error:
435 rc = tcp_conn_reset(conn);
436 if (rc != EOK)
437 fprintf(stderr, "Error resetting connection.\n");
438
439 recv_destroy(recv);
440}
441
442int main(int argc, char *argv[])
443{
444 inet_ep_t ep;
445 tcp_listener_t *lst;
446 tcp_t *tcp;
447 int rc;
448
449 /* Parse command line arguments */
450 for (int i = 1; i < argc; i++) {
451 if (argv[i][0] == '-') {
452 rc = parse_option(argc, argv, &i);
453 if (rc != EOK)
454 return rc;
455 } else {
456 usage();
457 return EINVAL;
458 }
459 }
460
461 printf("%s: HelenOS web server\n", NAME);
462
463 if (verbose)
464 fprintf(stderr, "Creating listener\n");
465
466 inet_ep_init(&ep);
467 ep.port = port;
468
469 rc = tcp_create(&tcp);
470 if (rc != EOK) {
471 fprintf(stderr, "Error initializing TCP.\n");
472 return 1;
473 }
474
475 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
476 &lst);
477 if (rc != EOK) {
478 fprintf(stderr, "Error creating listener.\n");
479 return 2;
480 }
481
482 fprintf(stderr, "%s: Listening for connections at port %" PRIu16 "\n",
483 NAME, port);
484
485 task_retval(0);
486 async_manager();
487
488 /* Not reached */
489 return 0;
490}
491
492/** @}
493 */
Note: See TracBrowser for help on using the repository browser.