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>
35 #include "lwip/debug.h"
37 #include "lwip/stats.h"
47 const unsigned char *file;
51 /*-----------------------------------------------------------------------------------*/
53 conn_err(void *arg, err_t err)
55 struct http_state *hs;
62 /*-----------------------------------------------------------------------------------*/
64 close_conn(struct tcp_pcb *pcb, struct http_state *hs)
72 /*-----------------------------------------------------------------------------------*/
74 send_data(struct tcp_pcb *pcb, struct http_state *hs)
79 /* We cannot send more data than space available in the send
81 if (tcp_sndbuf(pcb) < hs->left) {
82 len = tcp_sndbuf(pcb);
85 LWIP_ASSERT((len == hs->left), "hs->left did not fit into u16_t!");
89 err = tcp_write(pcb, hs->file, len, 0);
91 outputf("Insufficient memory to send %d", len);
94 } while (err == ERR_MEM && len > 1);
100 outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
103 /*-----------------------------------------------------------------------------------*/
105 http_poll(void *arg, struct tcp_pcb *pcb)
107 struct http_state *hs;
111 /* printf("Polll\n");*/
113 /* printf("Null, close\n");*/
118 if (hs->retries == 4) {
127 /*-----------------------------------------------------------------------------------*/
129 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
131 struct http_state *hs;
133 LWIP_UNUSED_ARG(len);
147 /*-----------------------------------------------------------------------------------*/
149 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
154 struct http_state *hs;
158 if (err == ERR_OK && p != NULL) {
160 /* Inform TCP that we have taken the data. */
161 tcp_recved(pcb, p->tot_len);
163 if (hs->file == NULL) {
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;
175 outputf("httpd: serving %s", (char*)data+4);
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);
184 hs->file = file.data;
185 LWIP_ASSERT((file.len >= 0), "File length must be positive!");
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);
203 if (err == ERR_OK && p == NULL) {
208 /*-----------------------------------------------------------------------------------*/
210 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
212 struct http_state *hs;
214 LWIP_UNUSED_ARG(arg);
215 LWIP_UNUSED_ARG(err);
217 tcp_setprio(pcb, TCP_PRIO_MIN);
219 /* Allocate memory for the structure that holds the state of the
221 hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
224 outputf("http_accept: Out of memory\n");
228 /* Initialize the structure. */
233 /* Tell TCP that this is the structure we wish to be passed for our
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);
241 tcp_err(pcb, conn_err);
243 tcp_poll(pcb, http_poll, 4);
246 /*-----------------------------------------------------------------------------------*/
253 tcp_bind(pcb, IP_ADDR_ANY, 80);
254 pcb = tcp_listen(pcb);
255 tcp_accept(pcb, http_accept);
257 /*-----------------------------------------------------------------------------------*/