]> Joshua Wise's Git repositories - netwatch.git/blame - net/http/httpd.c
Add httpd.
[netwatch.git] / net / http / httpd.c
CommitLineData
6f9272bd
JW
1/*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32
33#include <minilib.h>
34#include <output.h>
35#include "lwip/debug.h"
36
37#include "lwip/stats.h"
38
39#include "httpd.h"
40
41#include "lwip/tcp.h"
42
43#include "fs.h"
44
45struct http_state {
46 u32_t left;
47 const unsigned char *file;
48 u8_t retries;
49};
50
51/*-----------------------------------------------------------------------------------*/
52static void
53conn_err(void *arg, err_t err)
54{
55 struct http_state *hs;
56
57 LWIP_UNUSED_ARG(err);
58
59 hs = arg;
60 mem_free(hs);
61}
62/*-----------------------------------------------------------------------------------*/
63static void
64close_conn(struct tcp_pcb *pcb, struct http_state *hs)
65{
66 tcp_arg(pcb, NULL);
67 tcp_sent(pcb, NULL);
68 tcp_recv(pcb, NULL);
69 mem_free(hs);
70 tcp_close(pcb);
71}
72/*-----------------------------------------------------------------------------------*/
73static void
74send_data(struct tcp_pcb *pcb, struct http_state *hs)
75{
76 err_t err;
77 u16_t len;
78
79 /* We cannot send more data than space available in the send
80 buffer. */
81 if (tcp_sndbuf(pcb) < hs->left) {
82 len = tcp_sndbuf(pcb);
83 } else {
84 len = hs->left;
85 LWIP_ASSERT((len == hs->left), "hs->left did not fit into u16_t!");
86 }
87
88 do {
89 err = tcp_write(pcb, hs->file, len, 0);
90 if (err == ERR_MEM) {
91 outputf("Insufficient memory to send %d", len);
92 len /= 2;
93 }
94 } while (err == ERR_MEM && len > 1);
95
96 if (err == ERR_OK) {
97 hs->file += len;
98 hs->left -= len;
99 } else {
100 outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
101 }
102}
103/*-----------------------------------------------------------------------------------*/
104static err_t
105http_poll(void *arg, struct tcp_pcb *pcb)
106{
107 struct http_state *hs;
108
109 hs = arg;
110
111 /* printf("Polll\n");*/
112 if (hs == NULL) {
113 /* printf("Null, close\n");*/
114 tcp_abort(pcb);
115 return ERR_ABRT;
116 } else {
117 ++hs->retries;
118 if (hs->retries == 4) {
119 tcp_abort(pcb);
120 return ERR_ABRT;
121 }
122 send_data(pcb, hs);
123 }
124
125 return ERR_OK;
126}
127/*-----------------------------------------------------------------------------------*/
128static err_t
129http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
130{
131 struct http_state *hs;
132
133 LWIP_UNUSED_ARG(len);
134
135 hs = arg;
136
137 hs->retries = 0;
138
139 if (hs->left > 0) {
140 send_data(pcb, hs);
141 } else {
142 close_conn(pcb, hs);
143 }
144
145 return ERR_OK;
146}
147/*-----------------------------------------------------------------------------------*/
148static err_t
149http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
150{
151 int i;
152 char *data;
153 struct fs_file file;
154 struct http_state *hs;
155
156 hs = arg;
157
158 if (err == ERR_OK && p != NULL) {
159
160 /* Inform TCP that we have taken the data. */
161 tcp_recved(pcb, p->tot_len);
162
163 if (hs->file == NULL) {
164 data = p->payload;
165
166 if (strncmp(data, "GET ", 4) == 0) {
167 for(i = 0; i < 40; i++) {
168 if (((char *)data + 4)[i] == ' ' ||
169 ((char *)data + 4)[i] == '\r' ||
170 ((char *)data + 4)[i] == '\n') {
171 ((char *)data + 4)[i] = 0;
172 }
173 }
174
175 outputf("httpd: serving %s", (char*)data+4);
176
177 if (*(char *)(data + 4) == '/' &&
178 *(char *)(data + 5) == 0) {
179 fs_open("/index.html", &file);
180 } else if (!fs_open((char *)data + 4, &file)) {
181 fs_open("/404.html", &file);
182 }
183
184 hs->file = file.data;
185 LWIP_ASSERT((file.len >= 0), "File length must be positive!");
186 hs->left = file.len;
187
188 pbuf_free(p);
189 send_data(pcb, hs);
190
191 /* Tell TCP that we wish be to informed of data that has been
192 successfully sent by a call to the http_sent() function. */
193 tcp_sent(pcb, http_sent);
194 } else {
195 pbuf_free(p);
196 close_conn(pcb, hs);
197 }
198 } else {
199 pbuf_free(p);
200 }
201 }
202
203 if (err == ERR_OK && p == NULL) {
204 close_conn(pcb, hs);
205 }
206 return ERR_OK;
207}
208/*-----------------------------------------------------------------------------------*/
209static err_t
210http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
211{
212 struct http_state *hs;
213
214 LWIP_UNUSED_ARG(arg);
215 LWIP_UNUSED_ARG(err);
216
217 tcp_setprio(pcb, TCP_PRIO_MIN);
218
219 /* Allocate memory for the structure that holds the state of the
220 connection. */
221 hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
222
223 if (hs == NULL) {
224 outputf("http_accept: Out of memory\n");
225 return ERR_MEM;
226 }
227
228 /* Initialize the structure. */
229 hs->file = NULL;
230 hs->left = 0;
231 hs->retries = 0;
232
233 /* Tell TCP that this is the structure we wish to be passed for our
234 callbacks. */
235 tcp_arg(pcb, hs);
236
237 /* Tell TCP that we wish to be informed of incoming data by a call
238 to the http_recv() function. */
239 tcp_recv(pcb, http_recv);
240
241 tcp_err(pcb, conn_err);
242
243 tcp_poll(pcb, http_poll, 4);
244 return ERR_OK;
245}
246/*-----------------------------------------------------------------------------------*/
247void
248httpd_init(void)
249{
250 struct tcp_pcb *pcb;
251
252 pcb = tcp_new();
253 tcp_bind(pcb, IP_ADDR_ANY, 80);
254 pcb = tcp_listen(pcb);
255 tcp_accept(pcb, http_accept);
256}
257/*-----------------------------------------------------------------------------------*/
258
This page took 0.04233 seconds and 4 git commands to generate.