3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Simple HTTP Webserver Application. When connected to the uIP stack,
34 * this will serve out files to HTTP clients.
37 #define INCLUDE_FROM_HTTPSERVERAPP_C
38 #include "HTTPServerApp.h"
40 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
41 * given location, and gives extra connection information.
43 char PROGMEM HTTP200Header
[] = "HTTP/1.1 200 OK\r\n"
44 "Server: LUFA RNDIS\r\n"
45 "Connection: close\r\n"
46 "MIME-version: 1.0\r\n"
49 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
50 * given URL is invalid, and gives extra error information.
52 char PROGMEM HTTP404Header
[] = "HTTP/1.1 404 Not Found\r\n"
53 "Server: LUFA RNDIS\r\n"
54 "Connection: close\r\n"
55 "MIME-version: 1.0\r\n"
56 "Content-Type: text/plain\r\n\r\n"
57 "Error 404: File Not Found";
59 /** Default MIME type sent if no other MIME type can be determined */
60 char PROGMEM DefaultMIMEType
[] = "text/plain";
62 /** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */
63 MIME_Type_t PROGMEM MIMETypes
[] =
65 {.Extension
= "htm", .MIMEType
= "text/html"},
66 {.Extension
= "jpg", .MIMEType
= "image/jpeg"},
67 {.Extension
= "gif", .MIMEType
= "image/gif"},
68 {.Extension
= "bmp", .MIMEType
= "image/bmp"},
69 {.Extension
= "png", .MIMEType
= "image/png"},
70 {.Extension
= "exe", .MIMEType
= "application/octet-stream"},
71 {.Extension
= "gz", .MIMEType
= "application/x-gzip"},
72 {.Extension
= "ico", .MIMEType
= "image/x-icon"},
73 {.Extension
= "zip", .MIMEType
= "application/zip"},
74 {.Extension
= "pdf", .MIMEType
= "application/pdf"},
77 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
81 /** Initialization function for the simple HTTP webserver. */
82 void WebserverApp_Init(void)
84 /* Listen on port 80 for HTTP connections from hosts */
85 uip_listen(HTONS(HTTP_SERVER_PORT
));
87 /* Mount the dataflash disk via FatFS */
88 f_mount(0, &DiskFATState
);
91 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
92 * TCP/IP stack needs a TCP packet to be processed.
94 void WebserverApp_Callback(void)
96 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
98 if (uip_aborted() || uip_timedout() || uip_closed())
100 /* Connection is being terminated for some reason - close file handle if open */
101 if (AppState
->FileOpen
)
103 f_close(&AppState
->FileHandle
);
104 AppState
->FileOpen
= false;
107 /* Lock to the closed state so that no further processing will occur on the connection */
108 AppState
->CurrentState
= WEBSERVER_STATE_Closed
;
109 AppState
->NextState
= WEBSERVER_STATE_Closed
;
114 /* New connection - initialize connection state values */
115 AppState
->CurrentState
= WEBSERVER_STATE_OpenRequestedFile
;
116 AppState
->NextState
= WEBSERVER_STATE_OpenRequestedFile
;
117 AppState
->FileOpen
= false;
118 AppState
->ACKedFilePos
= 0;
119 AppState
->SentChunkSize
= 0;
124 /* Add the amount of ACKed file data to the total sent file bytes counter */
125 AppState
->ACKedFilePos
+= AppState
->SentChunkSize
;
127 /* Progress to the next state once the current state's data has been ACKed */
128 AppState
->CurrentState
= AppState
->NextState
;
131 if (uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll())
133 switch (AppState
->CurrentState
)
135 case WEBSERVER_STATE_OpenRequestedFile
:
136 Webserver_OpenRequestedFile();
138 case WEBSERVER_STATE_SendResponseHeader
:
139 Webserver_SendResponseHeader();
141 case WEBSERVER_STATE_SendMIMETypeHeader
:
142 Webserver_SendMIMETypeHeader();
144 case WEBSERVER_STATE_SendData
:
145 Webserver_SendData();
147 case WEBSERVER_STATE_Closing
:
150 AppState
->NextState
= WEBSERVER_STATE_Closed
;
156 /** HTTP Server State handler for the Request Process state. This state manages the processing of incomming HTTP
157 * GET requests to the server from the receiving HTTP client.
159 static void Webserver_OpenRequestedFile(void)
161 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
162 char* AppData
= (char*)uip_appdata
;
164 /* No HTTP header received from the client, abort processing */
165 if (!(uip_newdata()))
168 char* RequestToken
= strtok(AppData
, " ");
170 /* Must be a GET request, abort otherwise */
171 if (strcmp(RequestToken
, "GET") != 0)
177 char* RequestedFileName
= strtok(NULL
, " ");
179 /* If the requested filename has more that just the leading '/' path in it, copy it over */
180 if (strlen(RequestedFileName
) > 1)
181 strncpy(AppState
->FileName
, &RequestedFileName
[1], (sizeof(AppState
->FileName
) - 1));
183 strcpy(AppState
->FileName
, "index.htm");
185 /* Ensure filename is null-terminated */
186 AppState
->FileName
[(sizeof(AppState
->FileName
) - 1)] = 0x00;
188 /* Try to open the file from the Dataflash disk */
189 AppState
->FileOpen
= (f_open(&AppState
->FileHandle
, AppState
->FileName
, FA_OPEN_EXISTING
| FA_READ
) == FR_OK
);
191 /* Lock to the SendResponseHeader state until connection terminated */
192 AppState
->CurrentState
= WEBSERVER_STATE_SendResponseHeader
;
193 AppState
->NextState
= WEBSERVER_STATE_SendResponseHeader
;
196 /** HTTP Server State handler for the HTTP Response Header Send state. This state manages the transmission of
197 * the HTTP response header to the receiving HTTP client.
199 static void Webserver_SendResponseHeader(void)
201 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
202 char* AppData
= (char*)uip_appdata
;
205 uint16_t HeaderLength
;
207 /* Determine what HTTP header should be sent to the client */
208 if (AppState
->FileOpen
)
210 HeaderToSend
= HTTP200Header
;
211 AppState
->NextState
= WEBSERVER_STATE_SendMIMETypeHeader
;
215 HeaderToSend
= HTTP404Header
;
216 AppState
->NextState
= WEBSERVER_STATE_Closing
;
219 HeaderLength
= strlen_P(HeaderToSend
);
220 strncpy_P(AppData
, HeaderToSend
, HeaderLength
);
221 uip_send(AppData
, HeaderLength
);
224 /** HTTP Server State handler for the MIME Header Send state. This state manages the transmission of the file
225 * MIME type header for the requested file to the receiving HTTP client.
227 static void Webserver_SendMIMETypeHeader(void)
229 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
230 char* AppData
= (char*)uip_appdata
;
232 char* Extension
= strpbrk(AppState
->FileName
, ".");
233 uint16_t MIMEHeaderLength
= 0;
235 /* Check to see if a file extension was found for the requested filename */
236 if (Extension
!= NULL
)
238 /* Look through the MIME type list, copy over the required MIME type if found */
239 for (int i
= 0; i
< (sizeof(MIMETypes
) / sizeof(MIMETypes
[0])); i
++)
241 if (strcmp_P(&Extension
[1], MIMETypes
[i
].Extension
) == 0)
243 MIMEHeaderLength
= strlen_P(MIMETypes
[i
].MIMEType
);
244 strncpy_P(AppData
, MIMETypes
[i
].MIMEType
, MIMEHeaderLength
);
250 /* Check if a MIME type was found and copied to the output buffer */
251 if (!(MIMEHeaderLength
))
253 /* MIME type not found - copy over the default MIME type */
254 MIMEHeaderLength
= strlen_P(DefaultMIMEType
);
255 strncpy_P(AppData
, DefaultMIMEType
, MIMEHeaderLength
);
258 /* Add the end-of line terminator and end-of-headers terminator after the MIME type */
259 strncpy(&AppData
[MIMEHeaderLength
], "\r\n\r\n", sizeof("\r\n\r\n"));
260 MIMEHeaderLength
+= (sizeof("\r\n\r\n") - 1);
262 /* Send the MIME header to the receiving client */
263 uip_send(AppData
, MIMEHeaderLength
);
265 /* When the MIME header is ACKed, progress to the data send stage */
266 AppState
->NextState
= WEBSERVER_STATE_SendData
;
269 /** HTTP Server State handler for the Data Send state. This state manages the transmission of file chunks
270 * to the receiving HTTP client.
272 static void Webserver_SendData(void)
274 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
275 char* AppData
= (char*)uip_appdata
;
277 /* Must determine the maximum segment size to determine maximum file chunk size */
278 uint16_t MaxSegmentSize
= uip_mss();
280 /* Return file pointer to the last ACKed position */
281 f_lseek(&AppState
->FileHandle
, AppState
->ACKedFilePos
);
283 /* Read the next chunk of data from the open file */
284 f_read(&AppState
->FileHandle
, AppData
, MaxSegmentSize
, &AppState
->SentChunkSize
);
286 /* Send the next file chunk to the receiving client */
287 uip_send(AppData
, AppState
->SentChunkSize
);
289 /* Check if we are at the last chunk of the file, if so next ACK should close the connection */
290 AppState
->NextState
= (MaxSegmentSize
!= AppState
->SentChunkSize
) ? WEBSERVER_STATE_Closing
: WEBSERVER_STATE_SendData
;