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"
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!");
88 outputf("send_data trying %d bytes", len);
91 err = tcp_write(pcb, hs->file, len, 0);
93 outputf("Insufficient memory to send %d", len);
96 } while (err == ERR_MEM && len > 1);
102 outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
105 /*-----------------------------------------------------------------------------------*/
107 http_poll(void *arg, struct tcp_pcb *pcb)
109 struct http_state *hs;
113 /* printf("Polll\n");*/
115 /* printf("Null, close\n");*/
120 if (hs->retries == 4) {
129 /*-----------------------------------------------------------------------------------*/
131 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
133 struct http_state *hs;
135 LWIP_UNUSED_ARG(len);
149 /*-----------------------------------------------------------------------------------*/
151 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
156 struct http_state *hs;
160 if (err == ERR_OK && p != NULL) {
162 /* Inform TCP that we have taken the data. */
163 tcp_recved(pcb, p->tot_len);
165 if (hs->file == NULL) {
168 if (strncmp(data, "GET ", 4) == 0) {
169 for(i = 0; i < 40; i++) {
170 if (((char *)data + 4)[i] == ' ' ||
171 ((char *)data + 4)[i] == '\r' ||
172 ((char *)data + 4)[i] == '\n') {
173 ((char *)data + 4)[i] = 0;
177 outputf("httpd: serving %s", (char*)data+4);
179 if (*(char *)(data + 4) == '/' &&
180 *(char *)(data + 5) == 0) {
181 fs_open("/index.html", &file);
182 } else if (!fs_open((char *)data + 4, &file)) {
183 fs_open("/404.html", &file);
186 hs->file = file.data;
187 LWIP_ASSERT((file.len >= 0), "File length must be positive!");
193 /* Tell TCP that we wish be to informed of data that has been
194 successfully sent by a call to the http_sent() function. */
195 tcp_sent(pcb, http_sent);
205 if (err == ERR_OK && p == NULL) {
210 /*-----------------------------------------------------------------------------------*/
212 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
214 struct http_state *hs;
216 LWIP_UNUSED_ARG(arg);
217 LWIP_UNUSED_ARG(err);
219 tcp_setprio(pcb, TCP_PRIO_MIN);
221 /* Allocate memory for the structure that holds the state of the
223 hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
226 outputf("http_accept: Out of memory\n");
230 /* Initialize the structure. */
235 /* Tell TCP that this is the structure we wish to be passed for our
239 /* Tell TCP that we wish to be informed of incoming data by a call
240 to the http_recv() function. */
241 tcp_recv(pcb, http_recv);
243 tcp_err(pcb, conn_err);
245 tcp_poll(pcb, http_poll, 4);
248 /*-----------------------------------------------------------------------------------*/
255 tcp_bind(pcb, IP_ADDR_ANY, 80);
256 pcb = tcp_listen(pcb);
257 tcp_accept(pcb, http_accept);
259 /*-----------------------------------------------------------------------------------*/