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 #include "HTTPServerApp.h"
39 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
40 * given location, and gives extra connection information.
42 char PROGMEM HTTP200Header
[] = "HTTP/1.1 200 OK\r\n"
43 "Server: LUFA RNDIS\r\n"
44 "Connection: close\r\n"
45 "MIME-version: 1.0\r\n"
48 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
49 * given URL is invalid, and gives extra error information.
51 char PROGMEM HTTP404Header
[] = "HTTP/1.1 404 Not Found\r\n"
52 "Server: LUFA RNDIS\r\n"
53 "Connection: close\r\n"
54 "MIME-version: 1.0\r\n"
55 "Content-Type: text/plain\r\n\r\n"
56 "Error 404: File Not Found";
58 /** Default MIME type sent if no other MIME type can be determined */
59 char PROGMEM DefaultMIMEType
[] = "text/plain";
61 /** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */
62 MIME_Type_t PROGMEM MIMETypes
[] =
64 {.Extension
= "htm", .MIMEType
= "text/html"},
65 {.Extension
= "jpg", .MIMEType
= "image/jpeg"},
66 {.Extension
= "gif", .MIMEType
= "image/gif"},
67 {.Extension
= "bmp", .MIMEType
= "image/bmp"},
68 {.Extension
= "png", .MIMEType
= "image/png"},
69 {.Extension
= "exe", .MIMEType
= "application/octet-stream"},
70 {.Extension
= "gz", .MIMEType
= "application/x-gzip"},
71 {.Extension
= "ico", .MIMEType
= "image/x-icon"},
72 {.Extension
= "zip", .MIMEType
= "application/zip"},
73 {.Extension
= "pdf", .MIMEType
= "application/pdf"},
76 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
80 /** Initialization function for the simple HTTP webserver. */
81 void WebserverApp_Init(void)
83 /* Listen on port 80 for HTTP connections from hosts */
84 uip_listen(HTONS(HTTP_SERVER_PORT
));
86 /* Mount the dataflash disk via FatFS */
87 f_mount(0, &DiskFATState
);
90 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
91 * TCP/IP stack needs a TCP packet to be processed.
93 void WebserverApp_Callback(void)
95 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
96 char* AppData
= (char*)uip_appdata
;
97 uint16_t AppDataSize
= 0;
99 if (uip_aborted() || uip_timedout() || uip_closed())
101 /* Check if the open file needs to be closed */
102 if (AppState
->FileOpen
)
104 f_close(&AppState
->FileHandle
);
105 AppState
->FileOpen
= false;
108 AppState
->PrevState
= WEBSERVER_STATE_Closed
;
109 AppState
->CurrentState
= WEBSERVER_STATE_Closed
;
113 else if (uip_connected())
115 /* New connection - initialize connection state values */
116 AppState
->PrevState
= WEBSERVER_STATE_OpenRequestedFile
;
117 AppState
->CurrentState
= WEBSERVER_STATE_OpenRequestedFile
;
118 AppState
->FileOpen
= false;
120 else if (uip_rexmit())
122 /* Re-try last state */
123 AppState
->CurrentState
= AppState
->PrevState
;
126 switch (AppState
->CurrentState
)
128 case WEBSERVER_STATE_OpenRequestedFile
:
129 /* Wait for the packet containing the request header */
132 char* RequestToken
= strtok(AppData
, " ");
134 /* Must be a GET request, abort otherwise */
135 if (strcmp(RequestToken
, "GET") != 0)
141 char* RequestedFileName
= strtok(NULL
, " ");
143 /* If the requested filename has more that just the leading '/' path in it, copy it over */
144 if (strlen(RequestedFileName
) > 1)
145 strncpy(AppState
->FileName
, &RequestedFileName
[1], (sizeof(AppState
->FileName
) - 1));
147 strcpy(AppState
->FileName
, "index.htm");
149 /* Ensure filename is null-terminated */
150 AppState
->FileName
[(sizeof(AppState
->FileName
) - 1)] = 0x00;
152 /* Try to open the file from the Dataflash disk */
153 AppState
->FileOpen
= (f_open(&AppState
->FileHandle
, AppState
->FileName
, FA_OPEN_EXISTING
| FA_READ
) == FR_OK
);
154 AppState
->CurrentFilePos
= 0;
156 AppState
->PrevState
= WEBSERVER_STATE_OpenRequestedFile
;
157 AppState
->CurrentState
= WEBSERVER_STATE_SendResponseHeader
;
161 case WEBSERVER_STATE_SendResponseHeader
:
162 /* Determine what HTTP header should be sent to the client */
163 if (AppState
->FileOpen
)
165 AppDataSize
= strlen_P(HTTP200Header
);
166 strncpy_P(AppData
, HTTP200Header
, AppDataSize
);
170 AppDataSize
= strlen_P(HTTP404Header
);
171 strncpy_P(AppData
, HTTP404Header
, AppDataSize
);
174 AppState
->PrevState
= WEBSERVER_STATE_SendResponseHeader
;
175 AppState
->CurrentState
= WEBSERVER_STATE_SendMIMETypeHeader
;
177 case WEBSERVER_STATE_SendMIMETypeHeader
:
178 /* File must have been found and opened for MIME header to be sent */
179 if (AppState
->FileOpen
)
181 char* Extension
= strpbrk(AppState
->FileName
, ".");
183 /* Check to see if a file extension was found for the requested filename */
184 if (Extension
!= NULL
)
186 /* Look through the MIME type list, copy over the required MIME type if found */
187 for (int i
= 0; i
< (sizeof(MIMETypes
) / sizeof(MIMETypes
[0])); i
++)
189 if (strcmp_P(&Extension
[1], MIMETypes
[i
].Extension
) == 0)
191 AppDataSize
= strlen_P(MIMETypes
[i
].MIMEType
);
192 strncpy_P(AppData
, MIMETypes
[i
].MIMEType
, AppDataSize
);
198 /* Check if a MIME type was found and copied to the output buffer */
201 /* MIME type not found - copy over the default MIME type */
202 AppDataSize
= strlen_P(DefaultMIMEType
);
203 strncpy_P(AppData
, DefaultMIMEType
, AppDataSize
);
206 /* Add the end-of line terminator and end-of-headers terminator after the MIME type */
207 strncpy(&AppData
[AppDataSize
], "\r\n\r\n", sizeof("\r\n\r\n"));
208 AppDataSize
+= (sizeof("\r\n\r\n") - 1);
211 AppState
->PrevState
= WEBSERVER_STATE_SendMIMETypeHeader
;
212 AppState
->CurrentState
= WEBSERVER_STATE_SendData
;
214 case WEBSERVER_STATE_SendData
:
215 /* If end of file/file not open, progress to the close state */
216 if (!(AppState
->FileOpen
) && !(uip_rexmit()))
218 f_close(&AppState
->FileHandle
);
221 AppState
->PrevState
= WEBSERVER_STATE_Closed
;
222 AppState
->CurrentState
= WEBSERVER_STATE_Closed
;
226 uint16_t MaxSegSize
= uip_mss();
228 /* Return file pointer to the last ACKed position */
229 f_lseek(&AppState
->FileHandle
, AppState
->CurrentFilePos
);
231 /* Read the next chunk of data from the open file */
232 f_read(&AppState
->FileHandle
, AppData
, MaxSegSize
, &AppDataSize
);
234 /* If we are not re-transmitting a lost segment, advance file position */
235 if (uip_acked() && !(uip_rexmit()))
237 AppState
->FileOpen
= (AppDataSize
> 0);
238 AppState
->CurrentFilePos
+= AppDataSize
;
241 /* Stay in the SendData state if retransmission is required until all data sent */
242 AppState
->PrevState
= WEBSERVER_STATE_SendData
;
247 /* If data has been loaded into the application buffer by the server, send it to the client */
249 uip_send(AppData
, AppDataSize
);