2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
36 #include "lwip/debug.h"
38 #include "lwip/stats.h"
52 /*-----------------------------------------------------------------------------------*/
54 conn_err(void *arg, err_t err)
56 struct http_state *hs;
63 /*-----------------------------------------------------------------------------------*/
65 close_conn(struct tcp_pcb *pcb, struct http_state *hs)
73 /*-----------------------------------------------------------------------------------*/
75 send_data(struct tcp_pcb *pcb, struct http_state *hs)
80 /* We cannot send more data than space available in the send
82 if (tcp_sndbuf(pcb) < hs->left) {
83 len = tcp_sndbuf(pcb);
86 LWIP_ASSERT((len == hs->left), "hs->left did not fit into u16_t!");
89 outputf("send_data trying %d bytes", len);
92 err = tcp_write(pcb, hs->file, len, 0);
94 outputf("Insufficient memory to send %d", len);
97 } while (err == ERR_MEM && len > 1);
103 outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
106 /*-----------------------------------------------------------------------------------*/
108 http_poll(void *arg, struct tcp_pcb *pcb)
110 struct http_state *hs;
114 /* printf("Polll\n");*/
116 /* printf("Null, close\n");*/
121 if (hs->retries == 4) {
130 /*-----------------------------------------------------------------------------------*/
132 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
134 struct http_state *hs;
136 LWIP_UNUSED_ARG(len);
150 /*-----------------------------------------------------------------------------------*/
152 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
157 struct http_state *hs;
161 if (err == ERR_OK && p != NULL) {
163 /* Inform TCP that we have taken the data. */
164 tcp_recved(pcb, p->tot_len);
166 if (hs->file == NULL) {
169 if (strncmp(data, "GET ", 4) == 0) {
170 for(i = 0; i < 40; i++) {
171 if (((char *)data + 4)[i] == ' ' ||
172 ((char *)data + 4)[i] == '\r' ||
173 ((char *)data + 4)[i] == '\n') {
174 ((char *)data + 4)[i] = 0;
178 outputf("httpd: serving %s", (char*)data+4);
180 if (*(char *)(data + 4) == '/' &&
181 *(char *)(data + 5) == 0) {
182 fs_open("/index.html", &file);
183 } else if (!fs_open((char *)data + 4, &file)) {
184 fs_open("/404.html", &file);
187 hs->file = file.data;
188 LWIP_ASSERT((file.len >= 0), "File length must be positive!");
194 /* Tell TCP that we wish be to informed of data that has been
195 successfully sent by a call to the http_sent() function. */
196 tcp_sent(pcb, http_sent);
206 if (err == ERR_OK && p == NULL) {
211 /*-----------------------------------------------------------------------------------*/
213 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
215 struct http_state *hs;
217 LWIP_UNUSED_ARG(arg);
218 LWIP_UNUSED_ARG(err);
220 tcp_setprio(pcb, TCP_PRIO_MIN);
222 /* Allocate memory for the structure that holds the state of the
224 hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
227 outputf("http_accept: Out of memory\n");
231 /* Initialize the structure. */
236 /* Tell TCP that this is the structure we wish to be passed for our
240 /* Tell TCP that we wish to be informed of incoming data by a call
241 to the http_recv() function. */
242 tcp_recv(pcb, http_recv);
244 tcp_err(pcb, conn_err);
246 tcp_poll(pcb, http_poll, 4);
249 /*-----------------------------------------------------------------------------------*/
256 tcp_bind(pcb, IP_ADDR_ANY, 80);
257 pcb = tcp_listen(pcb);
258 tcp_accept(pcb, http_accept);
260 /*-----------------------------------------------------------------------------------*/
262 PROTOCOL(httpd_init);