Add FatFS library to the Webserver project, extend the HTTP server so that it now...
[pub/USBasp.git] / Projects / Webserver / Lib / HTTPServerApp.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Simple HTTP Webserver Application. When connected to the uIP stack,
34 * this will serve out files to HTTP clients.
35 */
36
37 #include "HTTPServerApp.h"
38
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.
41 */
42 char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"
43 "Server: LUFA RNDIS\r\n"
44 "Content-type: text/html\r\n"
45 "Connection: close\r\n\r\n";
46
47 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
48 * given URL is invalid, and gives extra error information.
49 */
50 char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
51 "Server: LUFA RNDIS\r\n"
52 "Connection: close\r\n\r\n"
53 "The requested file was not found.";
54
55 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
56 FATFS DiskFATState;
57
58 /** Initialization function for the simple HTTP webserver. */
59 void WebserverApp_Init(void)
60 {
61 /* Listen on port 80 for HTTP connections from hosts */
62 uip_listen(HTONS(HTTP_SERVER_PORT));
63
64 /* Mount the dataflash disk via FatFS */
65 f_mount(0, &DiskFATState);
66 }
67
68 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
69 * TCP/IP stack needs a TCP packet to be processed.
70 */
71 void WebserverApp_Callback(void)
72 {
73 uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
74 char* AppData = (char*)uip_appdata;
75 uint16_t AppDataSize = 0;
76
77 if (uip_aborted() || uip_timedout())
78 {
79 /* Close the file before terminating, if it is open */
80 f_close(&AppState->FileToSend);
81
82 AppState->CurrentState = WEBSERVER_STATE_Closed;
83
84 return;
85 }
86 else if (uip_closed())
87 {
88 /* Completed connection, just return */
89 return;
90 }
91 else if (uip_connected())
92 {
93 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
94 AppState->CurrentState = WEBSERVER_STATE_OpenRequestedFile;
95 }
96
97 switch (AppState->CurrentState)
98 {
99 case WEBSERVER_STATE_OpenRequestedFile:
100 /* Wait for the packet containing the request header */
101 if (uip_datalen())
102 {
103 /* Must be a GET request, abort otherwise */
104 if (strncmp(AppData, "GET ", (sizeof("GET ") - 1)) != 0)
105 {
106 uip_abort();
107 break;
108 }
109
110 char FileName[13];
111
112 /* Copy over the requested filename from the GET request */
113 for (uint8_t i = 0; i < (sizeof(FileName) - 1); i++)
114 {
115 FileName[i] = AppData[sizeof("GET ") + i];
116
117 if (FileName[i] == ' ')
118 {
119 FileName[i] = 0x00;
120 break;
121 }
122 }
123
124 /* Ensure requested filename is null-terminated */
125 FileName[(sizeof(FileName) - 1)] = 0x00;
126
127 /* If no filename specified, assume the default of INDEX.HTM */
128 if (FileName[0] == 0x00)
129 strcpy(FileName, "INDEX.HTM");
130
131 /* Try to open the file from the Dataflash disk */
132 AppState->FileOpen = (f_open(&AppState->FileToSend, FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK);
133
134 AppState->CurrentState = WEBSERVER_STATE_SendHeaders;
135 }
136
137 break;
138 case WEBSERVER_STATE_SendHeaders:
139 /* Determine what HTTP header should be sent to the client */
140 if (AppState->FileOpen)
141 {
142 AppDataSize = strlen_P(HTTP200Header);
143 strncpy_P(AppData, HTTP200Header, AppDataSize);
144 }
145 else
146 {
147 AppDataSize = strlen_P(HTTP404Header);
148 strncpy_P(AppData, HTTP404Header, AppDataSize);
149 }
150
151 uip_send(AppData, AppDataSize);
152
153 AppState->CurrentState = WEBSERVER_STATE_SendData;
154 break;
155 case WEBSERVER_STATE_SendData:
156 /* If end of file/file not open, progress to the close state */
157 if (!(AppState->FileOpen))
158 {
159 f_close(&AppState->FileToSend);
160 uip_close();
161 AppState->CurrentState = WEBSERVER_STATE_Closed;
162 break;
163 }
164
165 uint16_t MaxSegSize = uip_mss();
166
167 /* Read the next chunk of data from the open file */
168 f_read(&AppState->FileToSend, AppData, MaxSegSize, &AppDataSize);
169 AppState->FileOpen = (MaxSegSize == AppDataSize);
170
171 /* If data was read, send it to the client */
172 if (AppDataSize)
173 uip_send(AppData, AppDataSize);
174
175 break;
176 }
177 }