]> Joshua Wise's Git repositories - netwatch.git/blob - net/http/httpd.c
ed08b0ccbce75081e69e131d6a519fdc6162f84e
[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 "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
45 struct http_state {
46   u32_t left;
47   const char *file;
48   u8_t retries;
49 };
50
51 /*-----------------------------------------------------------------------------------*/
52 static void
53 conn_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 /*-----------------------------------------------------------------------------------*/
63 static void
64 close_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 /*-----------------------------------------------------------------------------------*/
73 static void
74 send_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   outputf("send_data trying %d bytes", len);
89
90   do {
91     err = tcp_write(pcb, hs->file, len, 0);
92     if (err == ERR_MEM) {
93       outputf("Insufficient memory to send %d", len);
94       len /= 2;
95     }
96   } while (err == ERR_MEM && len > 1);  
97   
98   if (err == ERR_OK) {
99     hs->file += len;
100     hs->left -= len;
101       } else {
102     outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));
103   }
104 }
105 /*-----------------------------------------------------------------------------------*/
106 static err_t
107 http_poll(void *arg, struct tcp_pcb *pcb)
108 {
109   struct http_state *hs;
110
111   hs = arg;
112   
113   /*  printf("Polll\n");*/
114   if (hs == NULL) {
115     /*    printf("Null, close\n");*/
116     tcp_abort(pcb);
117     return ERR_ABRT;
118   } else {
119     ++hs->retries;
120     if (hs->retries == 4) {
121       tcp_abort(pcb);
122       return ERR_ABRT;
123     }
124     send_data(pcb, hs);
125   }
126
127   return ERR_OK;
128 }
129 /*-----------------------------------------------------------------------------------*/
130 static err_t
131 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
132 {
133   struct http_state *hs;
134
135   LWIP_UNUSED_ARG(len);
136
137   hs = arg;
138
139   hs->retries = 0;
140   
141   if (hs->left > 0) {    
142     send_data(pcb, hs);
143   } else {
144     close_conn(pcb, hs);
145   }
146
147   return ERR_OK;
148 }
149 /*-----------------------------------------------------------------------------------*/
150 static err_t
151 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
152 {
153   int i;
154   char *data;
155   struct fs_file file;
156   struct http_state *hs;
157
158   hs = arg;
159
160   if (err == ERR_OK && p != NULL) {
161
162     /* Inform TCP that we have taken the data. */
163     tcp_recved(pcb, p->tot_len);
164     
165     if (hs->file == NULL) {
166       data = p->payload;
167       
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;
174           }
175         }
176         
177         outputf("httpd: serving %s", (char*)data+4);
178
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);
184         }
185
186         hs->file = file.data;
187         LWIP_ASSERT((file.len >= 0), "File length must be positive!");
188         hs->left = file.len;
189         
190         pbuf_free(p);
191         send_data(pcb, hs);
192
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);
196       } else {
197         pbuf_free(p);
198         close_conn(pcb, hs);
199       }
200     } else {
201       pbuf_free(p);
202     }
203   }
204
205   if (err == ERR_OK && p == NULL) {
206     close_conn(pcb, hs);
207   }
208   return ERR_OK;
209 }
210 /*-----------------------------------------------------------------------------------*/
211 static err_t
212 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
213 {
214   struct http_state *hs;
215
216   LWIP_UNUSED_ARG(arg);
217   LWIP_UNUSED_ARG(err);
218
219   tcp_setprio(pcb, TCP_PRIO_MIN);
220   
221   /* Allocate memory for the structure that holds the state of the
222      connection. */
223   hs = (struct http_state *)mem_malloc(sizeof(struct http_state));
224
225   if (hs == NULL) {
226     outputf("http_accept: Out of memory\n");
227     return ERR_MEM;
228   }
229   
230   /* Initialize the structure. */
231   hs->file = NULL;
232   hs->left = 0;
233   hs->retries = 0;
234   
235   /* Tell TCP that this is the structure we wish to be passed for our
236      callbacks. */
237   tcp_arg(pcb, hs);
238
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);
242
243   tcp_err(pcb, conn_err);
244   
245   tcp_poll(pcb, http_poll, 4);
246   return ERR_OK;
247 }
248 /*-----------------------------------------------------------------------------------*/
249 void
250 httpd_init(void)
251 {
252   struct tcp_pcb *pcb;
253
254   pcb = tcp_new();
255   tcp_bind(pcb, IP_ADDR_ANY, 80);
256   pcb = tcp_listen(pcb);
257   tcp_accept(pcb, http_accept);
258 }
259 /*-----------------------------------------------------------------------------------*/
260
This page took 0.027237 seconds and 2 git commands to generate.