blob: 0e4209acba6eb513ad614e2e071581621e16e8fa [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <string.h>
25#include <kernel/thread.h>
26#include <lwip/inet.h>
27#include <lwip/sockets.h>
28
29static int listen_socket;
30
31static const char http_msg[] =
32 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
33 "<html>\n"
34 "</html>\n";
35
36static int handle_request(int socket)
37{
38 unsigned char buf[64];
39 int err;
40
41 memset(buf, 0, sizeof(buf));
42 err = lwip_read(socket, buf, sizeof(buf));
43 dprintf("handle_request: read %d bytes from socket\n", err);
44
45 lwip_write(socket, http_msg, sizeof(http_msg));
46
47 return 0;
48}
49
50static int http_server_thread(void *arg)
51{
52 int err;
53 int new_socket;
54
55 listen_socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
56 dprintf("listen_socket %d\n", listen_socket);
57
58 struct sockaddr_in addr;
59 socklen_t addrlen;
60
61 memset(&addr, 0, sizeof(addr));
62 addr.sin_family = AF_INET;
63 addr.sin_addr.s_addr = htonl(INADDR_ANY);
64 addr.sin_port = htons(80);
65
66 err = lwip_bind(listen_socket, (struct sockaddr *)&addr, sizeof(addr));
67 dprintf("lwip_bind returns %d\n", err);
68
69 err = lwip_listen(listen_socket, 4);
70 dprintf("lwip_listen returns %d\n", err);
71
72 while ((new_socket = lwip_accept(listen_socket, (struct sockaddr *)&addr, &addrlen) >= 0)) {
73 dprintf("new connection on socket %d\n", new_socket);
74 handle_request(new_socket);
75 lwip_close(new_socket);
76 }
77
78 dprintf("http server shutting down\n");
79
80 lwip_close(listen_socket);
81
82 return 0;
83}
84
85int httpd_init(void)
86{
87 thread_resume(thread_create("httpd server", &http_server_thread, NULL, DEFAULT_PRIORITY + 5, DEFAULT_STACK_SIZE));
88
89 return 0;
90}
91