Add MIME type handling to the Webserver project, so that files of different types...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 29 Jan 2010 02:43:07 +0000 (02:43 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 29 Jan 2010 02:43:07 +0000 (02:43 +0000)
LUFA/Doxygen.conf
LUFA/ManPages/ChangeLog.txt
LUFA/ManPages/MigrationInformation.txt
LUFA/makefile
Projects/Webserver/Lib/HTTPServerApp.c
Projects/Webserver/Lib/HTTPServerApp.h
Projects/Webserver/Lib/uip/conf/apps-conf.h

index c46aba6..3497d34 100644 (file)
@@ -139,7 +139,7 @@ STRIP_FROM_INC_PATH    =
 # (but less readable) file names. This can be useful is your file systems\r
 # doesn't support long names like on DOS, Mac, or CD-ROM.\r
 \r
-SHORT_NAMES            = YES\r
+SHORT_NAMES            = NO\r
 \r
 # If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen\r
 # will interpret the first line (until the first dot) of a JavaDoc-style\r
@@ -496,7 +496,7 @@ SHOW_DIRECTORIES       = YES
 # This will remove the Files entry from the Quick Index and from the\r
 # Folder Tree View (if specified). The default is YES.\r
 \r
-SHOW_FILES             = YES\r
+SHOW_FILES             = NO\r
 \r
 # Set the SHOW_NAMESPACES tag to NO to disable the generation of the\r
 # Namespaces page.\r
index 79010bd..8104bc2 100644 (file)
   *  - Added MIDI event packing support to the MIDI Device and Host mode Class drivers, allowing for multiple MIDI events to\r
   *    sent or received in packed form in a single USB packet\r
   *  - Added new MIDI send buffer flush routines to the MIDI Device and Host mode Class drivers, to flush packed events\r
-  *  - Added master mode hardware TWI driver\r
+  *  - Added master mode hardware TWI driver for easy TWI peripheral control\r
   *  - Added ADC MUX masks for the standard ADC input channels on all AVR models with an ADC, altered demos to use these masks\r
   *    as on some models, the channel number is not identical to its single-ended ADC MUX mask\r
-  *  - New Webserver project, a RNDIS host USB webserver using the open source uIP TCP/IP network stack\r
+  *  - New Webserver project, a RNDIS host USB webserver using the open source uIP TCP/IP network stack and FatFS library\r
   *\r
   *  <b>Changed:</b>\r
   *  - Slowed down software USART carried PDI programming in the AVRISP project to prevent transmission errors\r
index 9f21118..f9864cb 100644 (file)
@@ -14,9 +14,9 @@
  *\r
  *  <b>Non-USB Library Components</b>\r
  *    - Due to some ADC channels not being identical to their ADC MUX selection masks for single-ended conversions on some AVR models,\r
- *      the ADC driver now has explicit masks for each of the standard ADC channels. These masks should be used when calling the ADC\r
- *      functions to ensure proper operation across all AVR models. Note that the \ref ADC_SetupChannel() function is an exception, and\r
- *      should always be called with a channel number rather than a channel mask.\r
+ *      the ADC driver now has explicit masks for each of the standard ADC channels (see \ref Group_ADC). These masks should be used \r
+ *      when calling the ADC functions to ensure proper operation across all AVR models. Note that the \ref ADC_SetupChannel() function \r
+ *      is an exception, and should always be called with a channel number rather than a channel mask.\r
  *\r
  *  <b>Host Mode</b>\r
  *    - The MIDI Host Class driver send and receive routines now operate on packed events, where multiple MIDI events may be\r
index 1b4fc05..ed1c55a 100644 (file)
@@ -51,7 +51,7 @@ clean_list:
 \r
 doxygen:\r
        @echo Generating Library Documentation...\r
-       @doxygen Doxygen.conf\r
+       ( cat Doxygen.conf ; echo "PROJECT_NUMBER=`grep LUFA_VERSION_STRING Version.h | cut -d'"' -f2`" ) | doxygen -\r
        @echo Documentation Generation Complete.\r
 \r
 clean_doxygen:\r
index 01aab76..3f8a2c3 100644 (file)
  */\r
 char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"\r
                                "Server: LUFA RNDIS\r\n"\r
-                               "Content-type: text/html\r\n"\r
-                               "Connection: close\r\n\r\n";\r
+                               "Connection: close\r\n"\r
+                                                          "MIME-version: 1.0\r\n"\r
+                                                          "Content-Type: ";\r
 \r
 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given\r
  *  given URL is invalid, and gives extra error information.\r
  */\r
 char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"\r
                                "Server: LUFA RNDIS\r\n"\r
-                               "Connection: close\r\n\r\n"\r
-                                                          "The requested file was not found.";\r
+                               "Connection: close\r\n"\r
+                                                          "MIME-version: 1.0\r\n"\r
+                                                          "Content-Type: text/plain\r\n\r\n"\r
+                                                          "Error 404: File Not Found";\r
+\r
+/** Default MIME type sent if no other MIME type can be determined */\r
+char PROGMEM DefaultMIMEType[] = "text/plain";\r
+\r
+/** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */\r
+MIME_Type_t PROGMEM MIMETypes[] =\r
+       {\r
+               {.Extension = "htm", .MIMEType = "text/html"},\r
+               {.Extension = "jpg", .MIMEType = "image/jpeg"},\r
+               {.Extension = "gif", .MIMEType = "image/gif"},\r
+               {.Extension = "bmp", .MIMEType = "image/bmp"},\r
+               {.Extension = "png", .MIMEType = "image/png"},\r
+               {.Extension = "exe", .MIMEType = "application/octet-stream"},\r
+               {.Extension = "gz",  .MIMEType = "application/x-gzip"},\r
+               {.Extension = "ico", .MIMEType = "image/x-icon"},\r
+               {.Extension = "zip", .MIMEType = "application/zip"},\r
+               {.Extension = "pdf", .MIMEType = "application/pdf"},\r
+       };\r
 \r
 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */\r
 FATFS DiskFATState;\r
 \r
+\r
 /** Initialization function for the simple HTTP webserver. */\r
 void WebserverApp_Init(void)\r
 {\r
@@ -85,7 +107,8 @@ void WebserverApp_Callback(void)
        }\r
        else if (uip_closed())\r
        {\r
-               /* Completed connection, just return */\r
+               AppState->CurrentState = WEBSERVER_STATE_Closed;\r
+\r
                return;\r
        }\r
        else if (uip_connected())\r
@@ -107,50 +130,86 @@ void WebserverApp_Callback(void)
                                        break;\r
                                }\r
                \r
-                               char FileName[13];\r
-\r
-                               /* Copy over the requested filename from the GET request */\r
-                               for (uint8_t i = 0; i < (sizeof(FileName) - 1); i++)\r
+                               /* Copy over the requested filename from the GET request as all-lowercase */\r
+                               for (uint8_t i = 0; i < (sizeof(AppState->FileName) - 1); i++)\r
                                {\r
-                                       FileName[i] = AppData[sizeof("GET ") + i];\r
+                                       AppState->FileName[i] = tolower(AppData[sizeof("GET ") + i]);\r
                                        \r
-                                       if (FileName[i] == ' ')\r
+                                       if (AppState->FileName[i] == ' ')\r
                                        {\r
-                                               FileName[i] = 0x00;\r
+                                               AppState->FileName[i] = 0x00;\r
                                                break;\r
                                        }\r
                                }\r
                                \r
                                /* Ensure requested filename is null-terminated */\r
-                               FileName[(sizeof(FileName) - 1)] = 0x00;\r
+                               AppState->FileName[(sizeof(AppState->FileName) - 1)] = 0x00;\r
                                \r
-                               /* If no filename specified, assume the default of INDEX.HTM */\r
-                               if (FileName[0] == 0x00)\r
-                                 strcpy(FileName, "INDEX.HTM");\r
+                               /* If no filename specified, assume the default of index.htm */\r
+                               if (AppState->FileName[0] == 0x00)\r
+                                 strcpy(AppState->FileName, "index.htm");\r
                                \r
                                /* Try to open the file from the Dataflash disk */\r
-                               AppState->FileOpen = (f_open(&AppState->FileToSend, FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK);\r
+                               AppState->FileOpen = (f_open(&AppState->FileToSend, AppState->FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK);\r
 \r
-                               AppState->CurrentState = WEBSERVER_STATE_SendHeaders;\r
+                               AppState->CurrentState = WEBSERVER_STATE_SendResponseHeader;\r
                        }\r
 \r
                        break;\r
-               case WEBSERVER_STATE_SendHeaders:\r
+               case WEBSERVER_STATE_SendResponseHeader:\r
                        /* Determine what HTTP header should be sent to the client */\r
                        if (AppState->FileOpen)\r
                        {\r
                                AppDataSize = strlen_P(HTTP200Header);\r
-                               strncpy_P(AppData, HTTP200Header, AppDataSize);                         \r
+                               strncpy_P(AppData, HTTP200Header, AppDataSize);\r
                        }\r
                        else\r
                        {\r
                                AppDataSize = strlen_P(HTTP404Header);\r
-                               strncpy_P(AppData, HTTP404Header, AppDataSize);                         \r
+                               strncpy_P(AppData, HTTP404Header, AppDataSize);\r
                        }\r
                        \r
                        uip_send(AppData, AppDataSize);\r
                        \r
-                       AppState->CurrentState = WEBSERVER_STATE_SendData;\r
+                       AppState->CurrentState = WEBSERVER_STATE_SendMIMETypeHeader;\r
+                       break;\r
+               case WEBSERVER_STATE_SendMIMETypeHeader:\r
+                       /* File must have been found and opened for MIME header to be sent */\r
+                       if (AppState->FileOpen)\r
+                       {\r
+                               char* Extension = strpbrk(AppState->FileName, ".");\r
+                               \r
+                               /* Check to see if a file extension was found for the requested filename */\r
+                               if (Extension != NULL)\r
+                               {\r
+                                       /* Look through the MIME type list, copy over the required MIME type if found */\r
+                                       for (int i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++)\r
+                                       {\r
+                                               if (strcmp_P(&Extension[1], MIMETypes[i].Extension) == 0)\r
+                                               {\r
+                                                       AppDataSize = strlen_P(MIMETypes[i].MIMEType);\r
+                                                       strncpy_P(AppData, MIMETypes[i].MIMEType, AppDataSize);                                         \r
+                                                       break;\r
+                                               }\r
+                                       } \r
+                               }\r
+\r
+                               /* Check if a MIME type was found and copied to the output buffer */\r
+                               if (!(AppDataSize))\r
+                               {\r
+                                       /* MIME type not found - copy over the default MIME type */\r
+                                       AppDataSize = strlen_P(DefaultMIMEType);\r
+                                       strncpy_P(AppData, DefaultMIMEType, AppDataSize);                               \r
+                               }\r
+                               \r
+                               /* Add the end-of line terminator and end-of-headers terminator after the MIME type */\r
+                               strncpy(&AppData[AppDataSize], "\r\n\r\n", sizeof("\r\n\r\n"));\r
+                               AppDataSize += (sizeof("\r\n\r\n") - 1);\r
+                               \r
+                               uip_send(AppData, AppDataSize);\r
+                       }\r
+                               \r
+                       AppState->CurrentState = WEBSERVER_STATE_SendData;                              \r
                        break;\r
                case WEBSERVER_STATE_SendData:\r
                        /* If end of file/file not open, progress to the close state */\r
index b96e2e3..7a4c873 100644 (file)
        /* Includes: */\r
                #include <avr/pgmspace.h>\r
                #include <string.h>\r
+               #include <ctype.h>\r
                \r
                #include <LUFA/Version.h>\r
                \r
                #include <uip.h>\r
                #include <ff.h>\r
-               \r
+       \r
        /* Enums: */\r
                /** States for each HTTP connection to the webserver. */\r
                enum Webserver_States_t\r
                {\r
                        WEBSERVER_STATE_OpenRequestedFile, /** Currently opening requested file */\r
-                       WEBSERVER_STATE_SendHeaders, /**< Currently sending HTTP headers to the client */\r
+                       WEBSERVER_STATE_SendResponseHeader, /**< Currently sending HTTP response headers to the client */\r
+                       WEBSERVER_STATE_SendMIMETypeHeader, /**< Currently sending HTTP MIME type header to the client */\r
                        WEBSERVER_STATE_SendData,    /**< Currently sending HTTP page data to the client */\r
                        WEBSERVER_STATE_Closed,      /**< Connection closed after all data sent */\r
                };\r
+               \r
+       /* Type Defines: */\r
+               /** Type define for a MIME type handler. */\r
+               typedef struct\r
+               {\r
+                       char Extension[4]; /**< 3 or less character file extension */\r
+                       char MIMEType[30]; /**< Appropriate MIME type to send when the extension is encountered */\r
+               } MIME_Type_t;\r
        \r
        /* Macros: */\r
                /** TCP listen port for incomming HTTP traffic */\r
index 76cd937..63a445c 100644 (file)
@@ -6,6 +6,7 @@
        typedef struct\r
        {\r
                uint8_t CurrentState;\r
+               char    FileName[13];\r
                FIL     FileToSend;\r
                bool    FileOpen;\r
        } uip_tcp_appstate_t;\r