X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/3b6987bca3ef746fd9a5d4baec6d40b65c9b4101..ce8d0424b1a59bb2b0bd3ab8f69f4e4cf8c9930b:/Projects/Webserver/Lib/HTTPServerApp.c?ds=sidebyside diff --git a/Projects/Webserver/Lib/HTTPServerApp.c b/Projects/Webserver/Lib/HTTPServerApp.c index 4f8490e96..4e00952f3 100644 --- a/Projects/Webserver/Lib/HTTPServerApp.c +++ b/Projects/Webserver/Lib/HTTPServerApp.c @@ -100,13 +100,9 @@ void HTTPServerApp_Callback(void) if (uip_aborted() || uip_timedout() || uip_closed()) { - /* Connection is being terminated for some reason - close file handle */ - f_close(&AppState->HTTPServer.FileHandle); - AppState->HTTPServer.FileOpen = false; - /* Lock to the closed state so that no further processing will occur on the connection */ - AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed; - AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closing; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; } if (uip_connected()) @@ -148,9 +144,15 @@ void HTTPServerApp_Callback(void) 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; + + /* If connection is not already closed, close it */ uip_close(); - AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; break; } } @@ -168,7 +170,7 @@ static void HTTPServerApp_OpenRequestedFile(void) if (!(uip_newdata())) return; - char* RequestToken = strtok(AppData, " "); + char* RequestToken = strtok(AppData, " "); char* RequestedFileName = strtok(NULL, " "); /* Must be a GET request, abort otherwise */ @@ -182,16 +184,19 @@ static void HTTPServerApp_OpenRequestedFile(void) strncpy(AppState->HTTPServer.FileName, &RequestedFileName[1], (sizeof(AppState->HTTPServer.FileName) - 1)); /* Ensure filename is null-terminated */ - AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00; + AppState->HTTPServer.FileName[sizeof(AppState->HTTPServer.FileName) - 1] = 0x00; + + /* 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 */ - if (AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName) - 1] == '/') + if (AppState->HTTPServer.FileName[FileNameLen - 1] == '/') { - strncpy_P(&AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName)], DefaultDirFileName, - (sizeof(AppState->HTTPServer.FileName) - (strlen(AppState->HTTPServer.FileName) + 1))); + strncpy_P(&AppState->HTTPServer.FileName[FileNameLen], DefaultDirFileName, + (sizeof(AppState->HTTPServer.FileName) - FileNameLen)); /* Ensure altered filename is still null-terminated */ - AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00; + AppState->HTTPServer.FileName[sizeof(AppState->HTTPServer.FileName) - 1] = 0x00; } /* Try to open the file from the Dataflash disk */ @@ -233,7 +238,7 @@ static void HTTPServerApp_SendResponseHeader(void) if (Extension != NULL) { /* Look through the MIME type list, copy over the required MIME type if found */ - for (int i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++) + for (uint8_t i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++) { if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0) { @@ -251,7 +256,7 @@ static void HTTPServerApp_SendResponseHeader(void) strcpy_P(&AppData[strlen(AppData)], DefaultMIMEType); } - /* Add the end-of line terminator and end-of-headers terminator after the MIME type */ + /* Add the end-of-line terminator and end-of-headers terminator after the MIME type */ strcpy(&AppData[strlen(AppData)], "\r\n\r\n"); /* Send the MIME header to the receiving client */