]> Joshua Wise's Git repositories - netwatch.git/blob - net/http/httpd.c
Commit GDB patch from Jacob Potter.
[netwatch.git] / net / http / httpd.c
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 <tables.h>
36 #include "lwip/debug.h"
37
38 #include "lwip/stats.h"
39
40 #include "httpd.h"
41
42 #include "lwip/tcp.h"
43
44 #include "fs.h"
45
46 struct http_state {
47   u32_t left;
48   const char *file;
49   u8_t retries;
50 };
51
52 /*-----------------------------------------------------------------------------------*/
53 static void
54 conn_err(void *arg, err_t err)
55 {
56   struct http_state *hs;
57
58   LWIP_UNUSED_ARG(err);
59
60   hs = arg;
61   mem_free(hs);
62 }
63 /*-----------------------------------------------------------------------------------*/
64 static void
65 close_conn(struct tcp_pcb *pcb, struct http_state *hs)
66 {
67   tcp_arg(pcb, NULL);
68   tcp_sent(pcb, NULL);
69   tcp_recv(pcb, NULL);
70   mem_free(hs);
71   tcp_close(pcb);
72 }
73 /*-----------------------------------------------------------------------------------*/
74 static void
75 send_data(struct tcp_pcb *pcb, struct http_state *hs)
76 {
77   err_t err;
78   u16_t len;
79
80   /* We cannot send more data than space available in the send
81      buffer. */     
82   if (tcp_sndbuf(pcb) < hs->left) {
83     len = tcp_sndbuf(pcb);
84   } else {
85     len = hs->left;
86     LWIP_ASSERT((len == hs->left), "hs->left did not fit into u16_t!");
87   }
88   
89   outputf("send_data trying %d bytes", len);
90
91   do {
92     err = tcp_write(pcb, hs->file, len, 0);
93     if (err == ERR_MEM) {
94       outputf("Insufficient memory to send %d", len);
95       len /= 2;
96     }
97   } while (err == ERR_MEM && len > 1);  
98   
99   if (err == ERR_OK) {
100     hs->file += len;
101     hs->left -= len;
102       } else {
103     outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
104   }
105 }
106 /*-----------------------------------------------------------------------------------*/
107 static err_t
108 http_poll(void *arg, struct tcp_pcb *pcb)
109 {
110   struct http_state *hs;
111
112   hs = arg;
113   
114   /*  printf("Polll\n");*/
115   if (hs == NULL) {
116     /*    printf("Null, close\n");*/
117     tcp_abort(pcb);
118     return ERR_ABRT;
119   } else {
120     ++hs->retries;
121     if (hs->retries == 4) {
122       tcp_abort(pcb);
123       return ERR_ABRT;
124     }
125     send_data(pcb, hs);
126   }
127
128   return ERR_OK;
129 }
130 /*-----------------------------------------------------------------------------------*/
131 static err_t
132 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
133 {
134   struct http_state *hs;
135
136   LWIP_UNUSED_ARG(len);
137
138   hs = arg;
139
140   hs->retries = 0;
141   
142   if (hs->left > 0) {    
143     send_data(pcb, hs);
144   } else {
145     close_conn(pcb, hs);
146   }
147
148   return ERR_OK;
149 }
150 /*-----------------------------------------------------------------------------------*/
151 static err_t
152 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
153 {
154   int i;
155   char *data;
156   struct fs_file file;
157   struct http_state *hs;
158
159   hs = arg;
160
161   if (err == ERR_OK && p != NULL) {
162
163     /* Inform TCP that we have taken the data. */
164     tcp_recved(pcb, p->tot_len);
165     
166     if (hs->file == NULL) {
167       data = p->payload;
168       
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;
175           }
176         }
177         
178         outputf("httpd: serving %s", (char*)data+4);
179
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);
185         }
186
187         hs->file = file.data;
188         LWIP_ASSERT((file.len >= 0), "File length must be positive!");
189         hs->left = file.len;
190         
191         pbuf_free(p);
192         send_data(pcb, hs);
193
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);
197       } else {
198         pbuf_free(p);
199         close_conn(pcb, hs);
200       }
201     } else {
202       pbuf_free(p);
203     }
204   }
205
206   if (err == ERR_OK && p == NULL) {
207     close_conn(pcb, hs);
208   }
209   return ERR_OK;
210 }
211 /*-----------------------------------------------------------------------------------*/
212 static err_t
213 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
214 {
215   struct http_state *hs;
216
217   LWIP_UNUSED_ARG(arg);
218   LWIP_UNUSED_ARG(err);
219
220   tcp_setprio(pcb, TCP_PRIO_MIN);
221   
222   /* Allocate memory for the structure that holds the state of the
223      connection. */
224   hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
225
226   if (hs == NULL) {
227     outputf("http_accept: Out of memory\n");
228     return ERR_MEM;
229   }
230   
231   /* Initialize the structure. */
232   hs->file = NULL;
233   hs->left = 0;
234   hs->retries = 0;
235   
236   /* Tell TCP that this is the structure we wish to be passed for our
237      callbacks. */
238   tcp_arg(pcb, hs);
239
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);
243
244   tcp_err(pcb, conn_err);
245   
246   tcp_poll(pcb, http_poll, 4);
247   return ERR_OK;
248 }
249 /*-----------------------------------------------------------------------------------*/
250 void
251 httpd_init(void)
252 {
253   struct tcp_pcb *pcb;
254
255   pcb = tcp_new();
256   tcp_bind(pcb, IP_ADDR_ANY, 80);
257   pcb = tcp_listen(pcb);
258   tcp_accept(pcb, http_accept);
259 }
260 /*-----------------------------------------------------------------------------------*/
261
262 PROTOCOL(httpd_init);
This page took 0.039494 seconds and 4 git commands to generate.