source: mainline/uspace/app/websrv/websrv.c@ 550ed86

topic/simplify-dev-export
Last change on this file since 550ed86 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[f4a2d624]1/*
[26b6789]2 * Copyright (c) 2012 Jiri Svoboda
[f4a2d624]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/**
[4a4cc150]33 * @file Skeletal web server.
[f4a2d624]34 */
35
[4a4cc150]36#include <errno.h>
[d4d74dc]37#include <assert.h>
[8d2dd7f2]38#include <stdbool.h>
[f4a2d624]39#include <stdio.h>
[8d2dd7f2]40#include <stdint.h>
41#include <stddef.h>
[9c3bba0]42#include <stdlib.h>
[26b6789]43#include <task.h>
[f4a2d624]44
[b19e892]45#include <vfs/vfs.h>
46
[fab2746]47#include <inet/addr.h>
48#include <inet/endpoint.h>
49#include <inet/tcp.h>
[f4a2d624]50
[0a549cc]51#include <arg_parse.h>
52#include <macros.h>
[f4a2d624]53#include <str.h>
[0a549cc]54#include <str_error.h>
[f4a2d624]55
[0a549cc]56#define NAME "websrv"
[f4a2d624]57
[0a549cc]58#define DEFAULT_PORT 8080
59
60#define WEB_ROOT "/data/web"
[4a4cc150]61
[f4a2d624]62/** Buffer for receiving the request. */
[0a549cc]63#define BUFFER_SIZE 1024
64
[fab2746]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
[0a549cc]75static uint16_t port = DEFAULT_PORT;
76
[d076f16]77typedef struct {
78 tcp_conn_t *conn;
[4a4cc150]79
[d076f16]80 char rbuf[BUFFER_SIZE];
81 size_t rbuf_out;
82 size_t rbuf_in;
[4a4cc150]83
[d076f16]84 char lbuf[BUFFER_SIZE + 1];
85 size_t lbuf_used;
86} recv_t;
[f4a2d624]87
[26b6789]88static bool verbose = false;
89
[0a549cc]90/** Responses to send to client. */
91
92static const char *msg_ok =
[f4a2d624]93 "HTTP/1.0 200 OK\r\n"
[4a4cc150]94 "\r\n";
95
[0a549cc]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
[b7fd2a0]135static errno_t recv_create(tcp_conn_t *conn, recv_t **rrecv)
[d076f16]136{
137 recv_t *recv;
[a35b458]138
[d076f16]139 recv = calloc(1, sizeof(recv_t));
140 if (recv == NULL)
141 return ENOMEM;
[a35b458]142
[d076f16]143 recv->conn = conn;
144 recv->rbuf_out = 0;
145 recv->rbuf_in = 0;
146 recv->lbuf_used = 0;
[a35b458]147
[d076f16]148 *rrecv = recv;
149 return EOK;
150}
151
152static void recv_destroy(recv_t *recv)
153{
154 free(recv);
155}
156
[4a4cc150]157/** Receive one character (with buffering) */
[b7fd2a0]158static errno_t recv_char(recv_t *recv, char *c)
[4a4cc150]159{
[fab2746]160 size_t nrecv;
[b7fd2a0]161 errno_t rc;
[a35b458]162
[d076f16]163 if (recv->rbuf_out == recv->rbuf_in) {
164 recv->rbuf_out = 0;
165 recv->rbuf_in = 0;
[a35b458]166
[d076f16]167 rc = tcp_conn_recv_wait(recv->conn, recv->rbuf, BUFFER_SIZE, &nrecv);
[fab2746]168 if (rc != EOK) {
[c1694b6b]169 fprintf(stderr, "tcp_conn_recv() failed: %s\n", str_error(rc));
[4a4cc150]170 return rc;
171 }
[a35b458]172
[d076f16]173 recv->rbuf_in = nrecv;
[4a4cc150]174 }
[a35b458]175
[d076f16]176 *c = recv->rbuf[recv->rbuf_out++];
[4a4cc150]177 return EOK;
178}
179
[0a549cc]180/** Receive one line with length limit */
[b7fd2a0]181static errno_t recv_line(recv_t *recv, char **rbuf)
[4a4cc150]182{
[d076f16]183 char *bp = recv->lbuf;
[0a549cc]184 char c = '\0';
[a35b458]185
[d076f16]186 while (bp < recv->lbuf + BUFFER_SIZE) {
[0a549cc]187 char prev = c;
[b7fd2a0]188 errno_t rc = recv_char(recv, &c);
[a35b458]189
[4a4cc150]190 if (rc != EOK)
191 return rc;
[a35b458]192
[4a4cc150]193 *bp++ = c;
[0a549cc]194 if ((prev == '\r') && (c == '\n'))
[4a4cc150]195 break;
196 }
[a35b458]197
[d076f16]198 recv->lbuf_used = bp - recv->lbuf;
[4a4cc150]199 *bp = '\0';
[a35b458]200
[d076f16]201 if (bp == recv->lbuf + BUFFER_SIZE)
[4a4cc150]202 return ELIMIT;
[a35b458]203
[d076f16]204 *rbuf = recv->lbuf;
[4a4cc150]205 return EOK;
206}
207
208static bool uri_is_valid(char *uri)
209{
210 if (uri[0] != '/')
211 return false;
[a35b458]212
[4a4cc150]213 if (uri[1] == '.')
214 return false;
[a35b458]215
[0a549cc]216 char *cp = uri + 1;
[a35b458]217
[4a4cc150]218 while (*cp != '\0') {
[0a549cc]219 char c = *cp++;
[4a4cc150]220 if (c == '/')
221 return false;
222 }
[a35b458]223
[4a4cc150]224 return true;
225}
226
[b7fd2a0]227static errno_t send_response(tcp_conn_t *conn, const char *msg)
[4a4cc150]228{
[0a549cc]229 size_t response_size = str_size(msg);
[a35b458]230
[26b6789]231 if (verbose)
[3bacee1]232 fprintf(stderr, "Sending response\n");
[a35b458]233
[b7fd2a0]234 errno_t rc = tcp_conn_send(conn, (void *) msg, response_size);
[fab2746]235 if (rc != EOK) {
236 fprintf(stderr, "tcp_conn_send() failed\n");
[4a4cc150]237 return rc;
238 }
[a35b458]239
[4a4cc150]240 return EOK;
241}
242
[b7fd2a0]243static errno_t uri_get(const char *uri, tcp_conn_t *conn)
[4a4cc150]244{
[d076f16]245 char *fbuf = NULL;
246 char *fname = NULL;
[b7fd2a0]247 errno_t rc;
[8e3498b]248 size_t nr;
[d076f16]249 int fd = -1;
[a35b458]250
[d076f16]251 fbuf = calloc(BUFFER_SIZE, 1);
252 if (fbuf == NULL) {
253 rc = ENOMEM;
254 goto out;
255 }
[a35b458]256
[4a4cc150]257 if (str_cmp(uri, "/") == 0)
[6bb169b5]258 uri = "/index.html";
[a35b458]259
[d5c1051]260 if (asprintf(&fname, "%s%s", WEB_ROOT, uri) < 0) {
[d076f16]261 rc = ENOMEM;
262 goto out;
263 }
[a35b458]264
[f77c1c9]265 rc = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ, &fd);
266 if (rc != EOK) {
[fab2746]267 rc = send_response(conn, msg_not_found);
[d076f16]268 goto out;
[4a4cc150]269 }
[a35b458]270
[9c3bba0]271 free(fname);
[d076f16]272 fname = NULL;
[a35b458]273
[fab2746]274 rc = send_response(conn, msg_ok);
[4a4cc150]275 if (rc != EOK)
[d076f16]276 goto out;
[a35b458]277
[58898d1d]278 aoff64_t pos = 0;
[4a4cc150]279 while (true) {
[8e3498b]280 rc = vfs_read(fd, &pos, fbuf, BUFFER_SIZE, &nr);
281 if (rc != EOK)
282 goto out;
[a35b458]283
[4a4cc150]284 if (nr == 0)
285 break;
[a35b458]286
[fab2746]287 rc = tcp_conn_send(conn, fbuf, nr);
288 if (rc != EOK) {
289 fprintf(stderr, "tcp_conn_send() failed\n");
[d076f16]290 goto out;
[4a4cc150]291 }
292 }
[a35b458]293
[d076f16]294 rc = EOK;
295out:
296 if (fd >= 0)
297 vfs_put(fd);
298 free(fname);
299 free(fbuf);
300 return rc;
[4a4cc150]301}
302
[b7fd2a0]303static errno_t req_process(tcp_conn_t *conn, recv_t *recv)
[4a4cc150]304{
[d076f16]305 char *reqline = NULL;
306
[b7fd2a0]307 errno_t rc = recv_line(recv, &reqline);
[4a4cc150]308 if (rc != EOK) {
[0a549cc]309 fprintf(stderr, "recv_line() failed\n");
[4a4cc150]310 return rc;
311 }
[a35b458]312
[26b6789]313 if (verbose)
[d076f16]314 fprintf(stderr, "Request: %s", reqline);
[a35b458]315
[d076f16]316 if (str_lcmp(reqline, "GET ", 4) != 0) {
[fab2746]317 rc = send_response(conn, msg_not_implemented);
[0a549cc]318 return rc;
[4a4cc150]319 }
[a35b458]320
[d076f16]321 char *uri = reqline + 4;
[0a549cc]322 char *end_uri = str_chr(uri, ' ');
[4a4cc150]323 if (end_uri == NULL) {
[d076f16]324 end_uri = reqline + str_size(reqline) - 2;
[4a4cc150]325 assert(*end_uri == '\r');
326 }
[a35b458]327
[4a4cc150]328 *end_uri = '\0';
[26b6789]329 if (verbose)
330 fprintf(stderr, "Requested URI: %s\n", uri);
[a35b458]331
[4a4cc150]332 if (!uri_is_valid(uri)) {
[fab2746]333 rc = send_response(conn, msg_bad_request);
[0a549cc]334 return rc;
[4a4cc150]335 }
[a35b458]336
[fab2746]337 return uri_get(uri, conn);
[4a4cc150]338}
[f4a2d624]339
[0a549cc]340static void usage(void)
[f4a2d624]341{
[d076f16]342 printf("Simple web server\n"
[0a549cc]343 "\n"
344 "Usage: " NAME " [options]\n"
345 "\n"
346 "Where options are:\n"
347 "-p port_number | --port=port_number\n"
348 "\tListening port (default " STRING(DEFAULT_PORT) ").\n"
349 "\n"
350 "-h | --help\n"
[26b6789]351 "\tShow this application help.\n"
352 "-v | --verbose\n"
353 "\tVerbose mode\n");
[0a549cc]354}
[f4a2d624]355
[b7fd2a0]356static errno_t parse_option(int argc, char *argv[], int *index)
[0a549cc]357{
358 int value;
[b7fd2a0]359 errno_t rc;
[a35b458]360
[0a549cc]361 switch (argv[*index][1]) {
362 case 'h':
363 usage();
364 exit(0);
365 break;
366 case 'p':
367 rc = arg_parse_int(argc, argv, index, &value, 0);
368 if (rc != EOK)
369 return rc;
[a35b458]370
[0a549cc]371 port = (uint16_t) value;
372 break;
[26b6789]373 case 'v':
374 verbose = true;
375 break;
[0a549cc]376 case '-':
[6ff23ff]377 /* Long options with double dash */
[0a549cc]378 if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
379 usage();
380 exit(0);
381 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
382 rc = arg_parse_int(argc, argv, index, &value, 7);
383 if (rc != EOK)
384 return rc;
[a35b458]385
[0a549cc]386 port = (uint16_t) value;
[3bacee1]387 } else if (str_cmp(argv[*index] + 2, "verbose") == 0) {
[26b6789]388 verbose = true;
[0a549cc]389 } else {
390 usage();
391 return EINVAL;
392 }
393 break;
394 default:
395 usage();
396 return EINVAL;
397 }
[a35b458]398
[0a549cc]399 return EOK;
400}
[f4a2d624]401
[fab2746]402static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
403{
[b7fd2a0]404 errno_t rc;
[d076f16]405 recv_t *recv = NULL;
[a35b458]406
[fab2746]407 if (verbose)
408 fprintf(stderr, "New connection, waiting for request\n");
[a35b458]409
[d076f16]410 rc = recv_create(conn, &recv);
411 if (rc != EOK) {
412 fprintf(stderr, "Out of memory.\n");
413 goto error;
414 }
[a35b458]415
[d076f16]416 rc = req_process(conn, recv);
[fab2746]417 if (rc != EOK) {
418 fprintf(stderr, "Error processing request (%s)\n",
419 str_error(rc));
[d076f16]420 goto error;
[b99f6e2]421 }
[a35b458]422
[b99f6e2]423 rc = tcp_conn_send_fin(conn);
424 if (rc != EOK) {
425 fprintf(stderr, "Error sending FIN.\n");
[d076f16]426 goto error;
[fab2746]427 }
[d076f16]428
429 recv_destroy(recv);
430 return;
431error:
432 rc = tcp_conn_reset(conn);
433 if (rc != EOK)
434 fprintf(stderr, "Error resetting connection.\n");
[a35b458]435
[d076f16]436 recv_destroy(recv);
[fab2746]437}
438
[0a549cc]439int main(int argc, char *argv[])
440{
[fab2746]441 inet_ep_t ep;
442 tcp_listener_t *lst;
443 tcp_t *tcp;
[b7fd2a0]444 errno_t rc;
[a35b458]445
[0a549cc]446 /* Parse command line arguments */
447 for (int i = 1; i < argc; i++) {
448 if (argv[i][0] == '-') {
[fab2746]449 rc = parse_option(argc, argv, &i);
[0a549cc]450 if (rc != EOK)
451 return rc;
452 } else {
453 usage();
454 return EINVAL;
455 }
456 }
[a35b458]457
[26b6789]458 printf("%s: HelenOS web server\n", NAME);
459
460 if (verbose)
[fab2746]461 fprintf(stderr, "Creating listener\n");
[a35b458]462
[fab2746]463 inet_ep_init(&ep);
464 ep.port = port;
465
466 rc = tcp_create(&tcp);
[f4a2d624]467 if (rc != EOK) {
[fab2746]468 fprintf(stderr, "Error initializing TCP.\n");
469 return 1;
[f4a2d624]470 }
[fab2746]471
472 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
473 &lst);
[f4a2d624]474 if (rc != EOK) {
[fab2746]475 fprintf(stderr, "Error creating listener.\n");
476 return 2;
[f4a2d624]477 }
[a35b458]478
[26b6789]479 fprintf(stderr, "%s: Listening for connections at port %" PRIu16 "\n",
480 NAME, port);
481
482 task_retval(0);
[fab2746]483 async_manager();
[a35b458]484
[0a549cc]485 /* Not reached */
[f4a2d624]486 return 0;
487}
488
489/** @}
490 */
Note: See TracBrowser for help on using the repository browser.