Oops - PDI handshake delay was too long, causing the device's /RESET functionality...
[pub/USBasp.git] / Projects / Webserver / Lib / HTTPServerApp.c
index 01aab76..5f0b2cf 100644 (file)
  */\r
 char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"\r
                                "Server: LUFA RNDIS\r\n"\r
  */\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
 \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
 /** 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
 /** Initialization function for the simple HTTP webserver. */\r
 void WebserverApp_Init(void)\r
 {\r
@@ -74,104 +96,155 @@ void WebserverApp_Callback(void)
        char*                     AppData     = (char*)uip_appdata;\r
        uint16_t                  AppDataSize = 0;\r
 \r
        char*                     AppData     = (char*)uip_appdata;\r
        uint16_t                  AppDataSize = 0;\r
 \r
-       if (uip_aborted() || uip_timedout())\r
+       if (uip_aborted() || uip_timedout() || uip_closed())\r
        {\r
        {\r
-               /* Close the file before terminating, if it is open */\r
-               f_close(&AppState->FileToSend);\r
-\r
+               /* Check if the open file needs to be closed */\r
+               if (AppState->FileOpen)\r
+               {\r
+                       f_close(&AppState->FileHandle);\r
+                       AppState->FileOpen = false;\r
+               }\r
+\r
+               AppState->PrevState    = WEBSERVER_STATE_Closed;\r
                AppState->CurrentState = WEBSERVER_STATE_Closed;\r
 \r
                return;\r
        }\r
                AppState->CurrentState = WEBSERVER_STATE_Closed;\r
 \r
                return;\r
        }\r
-       else if (uip_closed())\r
-       {\r
-               /* Completed connection, just return */\r
-               return;\r
-       }\r
        else if (uip_connected())\r
        {\r
        else if (uip_connected())\r
        {\r
-               /* New connection - initialize connection state and data pointer to the appropriate HTTP header */\r
+               /* New connection - initialize connection state values */\r
+               AppState->PrevState    = WEBSERVER_STATE_OpenRequestedFile;\r
                AppState->CurrentState = WEBSERVER_STATE_OpenRequestedFile;\r
                AppState->CurrentState = WEBSERVER_STATE_OpenRequestedFile;\r
+               AppState->FileOpen     = false;\r
+       }\r
+       else if (uip_rexmit())\r
+       {\r
+               /* Re-try last state */\r
+               AppState->CurrentState = AppState->PrevState;\r
        }\r
        \r
        switch (AppState->CurrentState)\r
        {\r
                case WEBSERVER_STATE_OpenRequestedFile:\r
                        /* Wait for the packet containing the request header */\r
        }\r
        \r
        switch (AppState->CurrentState)\r
        {\r
                case WEBSERVER_STATE_OpenRequestedFile:\r
                        /* Wait for the packet containing the request header */\r
-                       if (uip_datalen())\r
+                       if (uip_newdata())\r
                        {\r
                        {\r
+                               char* RequestToken = strtok(AppData, " ");\r
+                       \r
                                /* Must be a GET request, abort otherwise */\r
                                /* Must be a GET request, abort otherwise */\r
-                               if (strncmp(AppData, "GET ", (sizeof("GET ") - 1)) != 0)\r
+                               if (strcmp(RequestToken, "GET") != 0)\r
                                {\r
                                        uip_abort();\r
                                        break;\r
                                }\r
                \r
                                {\r
                                        uip_abort();\r
                                        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
-                               {\r
-                                       FileName[i] = AppData[sizeof("GET ") + i];\r
-                                       \r
-                                       if (FileName[i] == ' ')\r
-                                       {\r
-                                               FileName[i] = 0x00;\r
-                                               break;\r
-                                       }\r
-                               }\r
+                               char* RequestedFileName = strtok(NULL, " ");\r
                                \r
                                \r
-                               /* Ensure requested filename is null-terminated */\r
-                               FileName[(sizeof(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 the requested filename has more that just the leading '/' path in it, copy it over */\r
+                               if (strlen(RequestedFileName) > 1)\r
+                                 strncpy(AppState->FileName, &RequestedFileName[1], (sizeof(AppState->FileName) - 1));\r
+                               else\r
+                                 strcpy(AppState->FileName, "index.htm");\r
+\r
+                               /* Ensure filename is null-terminated */\r
+                               AppState->FileName[(sizeof(AppState->FileName) - 1)] = 0x00;\r
                                \r
                                /* Try to open the file from the Dataflash disk */\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->FileHandle, AppState->FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK);\r
+                               AppState->CurrentFilePos = 0;\r
 \r
 \r
-                               AppState->CurrentState = WEBSERVER_STATE_SendHeaders;\r
+                               AppState->PrevState    = WEBSERVER_STATE_OpenRequestedFile;\r
+                               AppState->CurrentState = WEBSERVER_STATE_SendResponseHeader;\r
                        }\r
 \r
                        break;\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
                        /* 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
                        }\r
                        else\r
                        {\r
                                AppDataSize = strlen_P(HTTP404Header);\r
-                               strncpy_P(AppData, HTTP404Header, AppDataSize);                         \r
+                               strncpy_P(AppData, HTTP404Header, AppDataSize);\r
                        }\r
                        \r
                        }\r
                        \r
-                       uip_send(AppData, AppDataSize);\r
-                       \r
-                       AppState->CurrentState = WEBSERVER_STATE_SendData;\r
+                       AppState->PrevState    = WEBSERVER_STATE_SendResponseHeader;\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
+                               \r
+                       AppState->PrevState    = WEBSERVER_STATE_SendMIMETypeHeader;\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
                        break;\r
                case WEBSERVER_STATE_SendData:\r
                        /* If end of file/file not open, progress to the close state */\r
-                       if (!(AppState->FileOpen))\r
+                       if (!(AppState->FileOpen) && !(uip_rexmit()))\r
                        {\r
                        {\r
-                               f_close(&AppState->FileToSend);\r
+                               f_close(&AppState->FileHandle);\r
                                uip_close();\r
                                uip_close();\r
+\r
+                               AppState->PrevState    = WEBSERVER_STATE_Closed;\r
                                AppState->CurrentState = WEBSERVER_STATE_Closed;\r
                                break;\r
                        }\r
 \r
                        uint16_t MaxSegSize = uip_mss();\r
                        \r
                                AppState->CurrentState = WEBSERVER_STATE_Closed;\r
                                break;\r
                        }\r
 \r
                        uint16_t MaxSegSize = uip_mss();\r
                        \r
+                       /* Return file pointer to the last ACKed position */\r
+                       f_lseek(&AppState->FileHandle, AppState->CurrentFilePos);\r
+\r
                        /* Read the next chunk of data from the open file */\r
                        /* Read the next chunk of data from the open file */\r
-                       f_read(&AppState->FileToSend, AppData, MaxSegSize, &AppDataSize);\r
-                       AppState->FileOpen = (MaxSegSize == AppDataSize);\r
+                       f_read(&AppState->FileHandle, AppData, MaxSegSize, &AppDataSize);\r
+\r
+                       /* If we are not re-transmitting a lost segment, advance file position */\r
+                       if (uip_acked() && !(uip_rexmit()))\r
+                       {\r
+                               AppState->FileOpen = (AppDataSize > 0);\r
+                               AppState->CurrentFilePos += AppDataSize;\r
+                       }\r
+                       \r
+                       /* Stay in the SendData state if retransmission is required until all data sent */\r
+                       AppState->PrevState = WEBSERVER_STATE_SendData;\r
 \r
 \r
-                       /* If data was read, send it to the client */\r
-                       if (AppDataSize)\r
-                         uip_send(AppData, AppDataSize);\r
-                                       \r
                        break;\r
        }\r
                        break;\r
        }\r
+\r
+       /* If data has been loaded into the application buffer by the server, send it to the client */\r
+       if (AppDataSize)\r
+         uip_send(AppData, AppDataSize);\r
 }\r
 }\r