HTTPServerApp.c 10.3 KB
Newer Older
1 2
/*
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7 8 9
*/

/*
10
  Copyright 2011  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 17 18
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Simple HTTP Webserver Application. When connected to the uIP stack,
 *  this will serve out files to HTTP clients on port 80.
 */
36

37 38 39 40 41 42 43 44 45 46 47 48 49
#define  INCLUDE_FROM_HTTPSERVERAPP_C
#include "HTTPServerApp.h"

/** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
 *  given location, and gives extra connection information.
 */
const char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"
                                     "Server: LUFA " LUFA_VERSION_STRING "\r\n"
                                     "Connection: close\r\n"
                                     "MIME-version: 1.0\r\n"
                                     "Content-Type: ";

/** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
50
 *  URL is invalid, and gives extra error information.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 */
const char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
                                     "Server: LUFA " LUFA_VERSION_STRING "\r\n"
                                     "Connection: close\r\n"
                                     "MIME-version: 1.0\r\n"
                                     "Content-Type: text/plain\r\n\r\n"
                                     "Error 404: File Not Found: /";

/** Default filename to fetch when a directory is requested */
const char PROGMEM DefaultDirFileName[] = "index.htm";

/** Default MIME type sent if no other MIME type can be determined. */
const char PROGMEM DefaultMIMEType[] = "text/plain";

/** List of MIME types for each supported file extension. */
const MIME_Type_t MIMETypes[] =
	{
		{.Extension = "htm", .MIMEType = "text/html"},
		{.Extension = "jpg", .MIMEType = "image/jpeg"},
		{.Extension = "gif", .MIMEType = "image/gif"},
		{.Extension = "bmp", .MIMEType = "image/bmp"},
		{.Extension = "png", .MIMEType = "image/png"},
		{.Extension = "ico", .MIMEType = "image/x-icon"},
		{.Extension = "exe", .MIMEType = "application/octet-stream"},
		{.Extension = "gz",  .MIMEType = "application/x-gzip"},
		{.Extension = "zip", .MIMEType = "application/zip"},
		{.Extension = "pdf", .MIMEType = "application/pdf"},
	};

80
/** FATFs structure to hold the internal state of the FAT driver for the Dataflash contents. */
81 82 83 84 85 86 87 88
FATFS DiskFATState;


/** Initialization function for the simple HTTP webserver. */
void HTTPServerApp_Init(void)
{
	/* Listen on port 80 for HTTP connections from hosts */
	uip_listen(HTONS(HTTP_SERVER_PORT));
89

90
	/* Mount the Dataflash disk via FatFS */
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	f_mount(0, &DiskFATState);
}

/** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
 *  TCP/IP stack needs a TCP packet to be processed.
 */
void HTTPServerApp_Callback(void)
{
	uip_tcp_appstate_t* const AppState = &uip_conn->appstate;

	if (uip_aborted() || uip_timedout() || uip_closed())
	{
		/* Lock to the closed state so that no further processing will occur on the connection */
		AppState->HTTPServer.CurrentState  = WEBSERVER_STATE_Closing;
		AppState->HTTPServer.NextState     = WEBSERVER_STATE_Closing;
	}

	if (uip_connected())
	{
		/* New connection - initialize connection state values */
		AppState->HTTPServer.CurrentState  = WEBSERVER_STATE_OpenRequestedFile;
		AppState->HTTPServer.NextState     = WEBSERVER_STATE_OpenRequestedFile;
		AppState->HTTPServer.FileOpen      = false;
		AppState->HTTPServer.ACKedFilePos  = 0;
		AppState->HTTPServer.SentChunkSize = 0;
	}

	if (uip_acked())
	{
		/* Add the amount of ACKed file data to the total sent file bytes counter */
		AppState->HTTPServer.ACKedFilePos += AppState->HTTPServer.SentChunkSize;

		/* Progress to the next state once the current state's data has been ACKed */
		AppState->HTTPServer.CurrentState = AppState->HTTPServer.NextState;
	}

	if (uip_rexmit())
	{
		/* Return file pointer to the last ACKed position */
130
		f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.ACKedFilePos);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	}

	if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())
	{
		switch (AppState->HTTPServer.CurrentState)
		{
			case WEBSERVER_STATE_OpenRequestedFile:
				HTTPServerApp_OpenRequestedFile();
				break;
			case WEBSERVER_STATE_SendResponseHeader:
				HTTPServerApp_SendResponseHeader();
				break;
			case WEBSERVER_STATE_SendData:
				HTTPServerApp_SendData();
				break;
			case WEBSERVER_STATE_Closing:
				/* Connection is being terminated for some reason - close file handle */
				f_close(&AppState->HTTPServer.FileHandle);
				AppState->HTTPServer.FileOpen = false;
150

151 152
				/* If connection is not already closed, close it */
				uip_close();
153

154 155 156
				AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed;
				AppState->HTTPServer.NextState    = WEBSERVER_STATE_Closed;
				break;
157 158
		}
	}
159 160 161 162 163 164 165 166 167
}

/** HTTP Server State handler for the Request Process state. This state manages the processing of incoming HTTP
 *  GET requests to the server from the receiving HTTP client.
 */
static void HTTPServerApp_OpenRequestedFile(void)
{
	uip_tcp_appstate_t* const AppState    = &uip_conn->appstate;
	char*               const AppData     = (char*)uip_appdata;
168

169 170 171
	/* No HTTP header received from the client, abort processing */
	if (!(uip_newdata()))
	  return;
172

173 174
	char* RequestToken      = strtok(AppData, " ");
	char* RequestedFileName = strtok(NULL, " ");
175

176 177 178 179 180 181
	/* Must be a GET request, abort otherwise */
	if (strcmp_P(RequestToken, PSTR("GET")) != 0)
	{
		uip_abort();
		return;
	}
182

183
	/* Copy over the requested filename */
184
	strlcpy(AppState->HTTPServer.FileName, &RequestedFileName[1], sizeof(AppState->HTTPServer.FileName));
185

186 187 188 189
	/* Determine the length of the URI so that it can be checked to see if it is a directory */
	uint8_t FileNameLen = strlen(AppState->HTTPServer.FileName);

	/* If the URI is a directory, append the default filename */
190
	if ((AppState->HTTPServer.FileName[FileNameLen - 1] == '/') || !(FileNameLen))
191
	{
192
		strlcpy_P(&AppState->HTTPServer.FileName[FileNameLen], DefaultDirFileName,
193 194
		          (sizeof(AppState->HTTPServer.FileName) - FileNameLen));
	}
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	/* Try to open the file from the Dataflash disk */
	AppState->HTTPServer.FileOpen     = (f_open(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.FileName,
	                                            (FA_OPEN_EXISTING | FA_READ)) == FR_OK);

	/* Lock to the SendResponseHeader state until connection terminated */
	AppState->HTTPServer.CurrentState = WEBSERVER_STATE_SendResponseHeader;
	AppState->HTTPServer.NextState    = WEBSERVER_STATE_SendResponseHeader;
}

/** HTTP Server State handler for the HTTP Response Header Send state. This state manages the transmission of
 *  the HTTP response header to the receiving HTTP client.
 */
static void HTTPServerApp_SendResponseHeader(void)
{
	uip_tcp_appstate_t* const AppState    = &uip_conn->appstate;
	char*               const AppData     = (char*)uip_appdata;

	char* Extension     = strpbrk(AppState->HTTPServer.FileName, ".");
	bool  FoundMIMEType = false;

	/* If the file isn't already open, it wasn't found - send back a 404 error response and abort */
	if (!(AppState->HTTPServer.FileOpen))
	{
		/* Copy over the HTTP 404 response header and send it to the receiving client */
		strcpy_P(AppData, HTTP404Header);
221
		strcat(AppData, AppState->HTTPServer.FileName);
222
		uip_send(AppData, strlen(AppData));
223

224 225 226
		AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing;
		return;
	}
227

228 229 230 231 232 233 234 235 236 237 238
	/* Copy over the HTTP 200 response header and send it to the receiving client */
	strcpy_P(AppData, HTTP200Header);

	/* Check to see if a MIME type for the requested file's extension was found */
	if (Extension != NULL)
	{
		/* Look through the MIME type list, copy over the required MIME type if found */
		for (uint8_t i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++)
		{
			if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0)
			{
239
				strcat(AppData, MIMETypes[i].MIMEType);
240 241 242
				FoundMIMEType = true;
				break;
			}
243
		}
244 245 246 247 248 249
	}

	/* Check if a MIME type was found and copied to the output buffer */
	if (!(FoundMIMEType))
	{
		/* MIME type not found - copy over the default MIME type */
250
		strcat_P(AppData, DefaultMIMEType);
251
	}
252

253
	/* Add the end-of-line terminator and end-of-headers terminator after the MIME type */
254
	strcat_P(AppData, PSTR("\r\n\r\n"));
255

256 257
	/* Send the MIME header to the receiving client */
	uip_send(AppData, strlen(AppData));
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	/* When the MIME header is ACKed, progress to the data send stage */
	AppState->HTTPServer.NextState = WEBSERVER_STATE_SendData;
}

/** HTTP Server State handler for the Data Send state. This state manages the transmission of file chunks
 *  to the receiving HTTP client.
 */
static void HTTPServerApp_SendData(void)
{
	uip_tcp_appstate_t* const AppState    = &uip_conn->appstate;
	char*               const AppData     = (char*)uip_appdata;

	/* Get the maximum segment size for the current packet */
	uint16_t MaxChunkSize = uip_mss();

	/* Read the next chunk of data from the open file */
	f_read(&AppState->HTTPServer.FileHandle, AppData, MaxChunkSize, &AppState->HTTPServer.SentChunkSize);
276

277 278
	/* Send the next file chunk to the receiving client */
	uip_send(AppData, AppState->HTTPServer.SentChunkSize);
279

280 281 282 283
	/* Check if we are at the last chunk of the file, if so next ACK should close the connection */
	if (MaxChunkSize != AppState->HTTPServer.SentChunkSize)
	  AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing;
}
284