Added incomplete MIDIToneGenerator project.
[pub/USBasp.git] / Projects / Webserver / Lib / HTTPServerApp.c
index 4f8490e..08d8494 100644 (file)
@@ -56,12 +56,12 @@ const char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
                                      "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
-const char PROGMEM DefaultMIMEType[] = "text/plain";\r
-\r
 /** Default filename to fetch when a directory is requested */\r
 const char PROGMEM DefaultDirFileName[] = "index.htm";\r
 \r
+/** Default MIME type sent if no other MIME type can be determined. */\r
+const char PROGMEM DefaultMIMEType[] = "text/plain";\r
+\r
 /** List of MIME types for each supported file extension. */\r
 const MIME_Type_t MIMETypes[] =\r
        {\r
@@ -77,7 +77,7 @@ const MIME_Type_t MIMETypes[] =
                {.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 structure to hold the internal state of the FAT driver for the dataflash contents. */\r
 FATFS DiskFATState;\r
 \r
 \r
@@ -100,13 +100,9 @@ void HTTPServerApp_Callback(void)
 \r
        if (uip_aborted() || uip_timedout() || uip_closed())\r
        {\r
-               /* Connection is being terminated for some reason - close file handle */\r
-               f_close(&AppState->HTTPServer.FileHandle);\r
-               AppState->HTTPServer.FileOpen = false;\r
-               \r
                /* Lock to the closed state so that no further processing will occur on the connection */\r
-               AppState->HTTPServer.CurrentState  = WEBSERVER_STATE_Closed;\r
-               AppState->HTTPServer.NextState     = WEBSERVER_STATE_Closed;\r
+               AppState->HTTPServer.CurrentState  = WEBSERVER_STATE_Closing;\r
+               AppState->HTTPServer.NextState     = WEBSERVER_STATE_Closing;\r
        }\r
 \r
        if (uip_connected())\r
@@ -148,9 +144,15 @@ void HTTPServerApp_Callback(void)
                                HTTPServerApp_SendData();\r
                                break;\r
                        case WEBSERVER_STATE_Closing:\r
+                               /* Connection is being terminated for some reason - close file handle */\r
+                               f_close(&AppState->HTTPServer.FileHandle);\r
+                               AppState->HTTPServer.FileOpen = false;\r
+               \r
+                               /* If connection is not already closed, close it */\r
                                uip_close();\r
                                \r
-                               AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed;\r
+                               AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed;\r
+                               AppState->HTTPServer.NextState    = WEBSERVER_STATE_Closed;\r
                                break;\r
                }                 \r
        }               \r
@@ -168,11 +170,11 @@ static void HTTPServerApp_OpenRequestedFile(void)
        if (!(uip_newdata()))\r
          return;\r
          \r
-       char* RequestToken = strtok(AppData, " ");\r
+       char* RequestToken      = strtok(AppData, " ");\r
        char* RequestedFileName = strtok(NULL, " ");\r
                        \r
        /* Must be a GET request, abort otherwise */\r
-       if (strcmp(RequestToken, "GET") != 0)\r
+       if (strcmp_P(RequestToken, PSTR("GET")) != 0)\r
        {\r
                uip_abort();\r
                return;\r
@@ -182,16 +184,19 @@ static void HTTPServerApp_OpenRequestedFile(void)
        strncpy(AppState->HTTPServer.FileName, &RequestedFileName[1], (sizeof(AppState->HTTPServer.FileName) - 1));\r
        \r
        /* Ensure filename is null-terminated */\r
-       AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00;\r
+       AppState->HTTPServer.FileName[sizeof(AppState->HTTPServer.FileName) - 1] = 0x00;\r
+       \r
+       /* Determine the length of the URI so that it can be checked to see if it is a directory */\r
+       uint8_t FileNameLen = strlen(AppState->HTTPServer.FileName);\r
 \r
        /* If the URI is a directory, append the default filename */\r
-       if (AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName) - 1] == '/')\r
+       if (AppState->HTTPServer.FileName[FileNameLen - 1] == '/')\r
        {\r
-               strncpy_P(&AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName)], DefaultDirFileName,\r
-                         (sizeof(AppState->HTTPServer.FileName) - (strlen(AppState->HTTPServer.FileName) + 1)));\r
+               strncpy_P(&AppState->HTTPServer.FileName[FileNameLen], DefaultDirFileName,\r
+                         (sizeof(AppState->HTTPServer.FileName) - FileNameLen));\r
 \r
                /* Ensure altered filename is still null-terminated */\r
-               AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00;\r
+               AppState->HTTPServer.FileName[sizeof(AppState->HTTPServer.FileName) - 1] = 0x00;\r
        }\r
        \r
        /* Try to open the file from the Dataflash disk */\r
@@ -233,7 +238,7 @@ static void HTTPServerApp_SendResponseHeader(void)
        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
+               for (uint8_t i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++)\r
                {\r
                        if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0)\r
                        {\r
@@ -251,8 +256,8 @@ static void HTTPServerApp_SendResponseHeader(void)
                strcpy_P(&AppData[strlen(AppData)], DefaultMIMEType);\r
        }\r
        \r
-       /* Add the end-of line terminator and end-of-headers terminator after the MIME type */\r
-       strcpy(&AppData[strlen(AppData)], "\r\n\r\n");\r
+       /* Add the end-of-line terminator and end-of-headers terminator after the MIME type */\r
+       strcpy_P(&AppData[strlen(AppData)], PSTR("\r\n\r\n"));\r
        \r
        /* Send the MIME header to the receiving client */\r
        uip_send(AppData, strlen(AppData));\r