X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b6a4584a19e81c2e1f909355bbc64c2b8cea84f6..aca7863350509a1f390eda93ac0150378d8cd16c:/Projects/Webserver/Lib/HTTPServerApp.c diff --git a/Projects/Webserver/Lib/HTTPServerApp.c b/Projects/Webserver/Lib/HTTPServerApp.c index e31bdbb0b..08d849433 100644 --- a/Projects/Webserver/Lib/HTTPServerApp.c +++ b/Projects/Webserver/Lib/HTTPServerApp.c @@ -31,7 +31,7 @@ /** \file * * Simple HTTP Webserver Application. When connected to the uIP stack, - * this will serve out files to HTTP clients. + * this will serve out files to HTTP clients on port 80. */ #define INCLUDE_FROM_HTTPSERVERAPP_C @@ -40,46 +40,49 @@ /** 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. */ -char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n" - "Server: LUFA RNDIS\r\n" - "Connection: close\r\n" - "MIME-version: 1.0\r\n" - "Content-Type: "; +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 * given URL is invalid, and gives extra error information. */ -char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n" - "Server: LUFA RNDIS\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"; +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 MIME type sent if no other MIME type can be determined */ -char PROGMEM DefaultMIMEType[] = "text/plain"; +/** Default filename to fetch when a directory is requested */ +const char PROGMEM DefaultDirFileName[] = "index.htm"; -/** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */ -MIME_Type_t PROGMEM MIMETypes[] = +/** 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 = "ico", .MIMEType = "image/x-icon"}, {.Extension = "zip", .MIMEType = "application/zip"}, {.Extension = "pdf", .MIMEType = "application/pdf"}, }; -/** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */ +/** FATFs structure to hold the internal state of the FAT driver for the dataflash contents. */ FATFS DiskFATState; /** Initialization function for the simple HTTP webserver. */ -void WebserverApp_Init(void) +void HTTPServerApp_Init(void) { /* Listen on port 80 for HTTP connections from hosts */ uip_listen(HTONS(HTTP_SERVER_PORT)); @@ -91,69 +94,74 @@ void WebserverApp_Init(void) /** 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 WebserverApp_Callback(void) +void HTTPServerApp_Callback(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; if (uip_aborted() || uip_timedout() || uip_closed()) { - /* Connection is being terminated for some reason - close file handle */ - f_close(&AppState->FileHandle); - AppState->FileOpen = false; - /* Lock to the closed state so that no further processing will occur on the connection */ - AppState->CurrentState = WEBSERVER_STATE_Closed; - AppState->NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closing; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; } if (uip_connected()) { /* New connection - initialize connection state values */ - AppState->CurrentState = WEBSERVER_STATE_OpenRequestedFile; - AppState->NextState = WEBSERVER_STATE_OpenRequestedFile; - AppState->FileOpen = false; - AppState->ACKedFilePos = 0; - AppState->SentChunkSize = 0; + 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->ACKedFilePos += AppState->SentChunkSize; + AppState->HTTPServer.ACKedFilePos += AppState->HTTPServer.SentChunkSize; /* Progress to the next state once the current state's data has been ACKed */ - AppState->CurrentState = AppState->NextState; + AppState->HTTPServer.CurrentState = AppState->HTTPServer.NextState; } - if (uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) + if (uip_rexmit()) { - switch (AppState->CurrentState) + /* Return file pointer to the last ACKed position */ + f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.ACKedFilePos); + } + + if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll()) + { + switch (AppState->HTTPServer.CurrentState) { case WEBSERVER_STATE_OpenRequestedFile: - Webserver_OpenRequestedFile(); + HTTPServerApp_OpenRequestedFile(); break; case WEBSERVER_STATE_SendResponseHeader: - Webserver_SendResponseHeader(); - break; - case WEBSERVER_STATE_SendMIMETypeHeader: - Webserver_SendMIMETypeHeader(); + HTTPServerApp_SendResponseHeader(); break; case WEBSERVER_STATE_SendData: - Webserver_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; + + /* If connection is not already closed, close it */ uip_close(); - AppState->NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; break; } } } -/** HTTP Server State handler for the Request Process state. This state manages the processing of incomming HTTP +/** 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 Webserver_OpenRequestedFile(void) +static void HTTPServerApp_OpenRequestedFile(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; @@ -162,128 +170,120 @@ static void Webserver_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 */ - if (strcmp(RequestToken, "GET") != 0) + if (strcmp_P(RequestToken, PSTR("GET")) != 0) { uip_abort(); return; } - - char* RequestedFileName = strtok(NULL, " "); - /* If the requested filename has more that just the leading '/' path in it, copy it over */ - if (strlen(RequestedFileName) > 1) - strncpy(AppState->FileName, &RequestedFileName[1], (sizeof(AppState->FileName) - 1)); - else - strcpy(AppState->FileName, "index.htm"); - + /* Copy over the requested filename */ + strncpy(AppState->HTTPServer.FileName, &RequestedFileName[1], (sizeof(AppState->HTTPServer.FileName) - 1)); + /* Ensure filename is null-terminated */ - AppState->FileName[(sizeof(AppState->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[FileNameLen - 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; + } /* Try to open the file from the Dataflash disk */ - AppState->FileOpen = (f_open(&AppState->FileHandle, AppState->FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK); + 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->CurrentState = WEBSERVER_STATE_SendResponseHeader; - AppState->NextState = WEBSERVER_STATE_SendResponseHeader; + 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 Webserver_SendResponseHeader(void) +static void HTTPServerApp_SendResponseHeader(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; - char* HeaderToSend; - uint16_t HeaderLength; + char* Extension = strpbrk(AppState->HTTPServer.FileName, "."); + bool FoundMIMEType = false; - /* Determine which HTTP header should be sent to the client */ - if (AppState->FileOpen) - { - HeaderToSend = HTTP200Header; - AppState->NextState = WEBSERVER_STATE_SendMIMETypeHeader; - } - else + /* If the file isn't already open, it wasn't found - send back a 404 error response and abort */ + if (!(AppState->HTTPServer.FileOpen)) { - HeaderToSend = HTTP404Header; - AppState->NextState = WEBSERVER_STATE_Closing; + /* Copy over the HTTP 404 response header and send it to the receiving client */ + strcpy_P(AppData, HTTP404Header); + strcpy(&AppData[strlen(AppData)], AppState->HTTPServer.FileName); + uip_send(AppData, strlen(AppData)); + + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; + return; } + + /* Copy over the HTTP 200 response header and send it to the receiving client */ + strcpy_P(AppData, HTTP200Header); - /* Copy over the HTTP response header and send it to the receiving client */ - HeaderLength = strlen_P(HeaderToSend); - strncpy_P(AppData, HeaderToSend, HeaderLength); - uip_send(AppData, HeaderLength); -} - -/** HTTP Server State handler for the MIME Header Send state. This state manages the transmission of the file - * MIME type header for the requested file to the receiving HTTP client. - */ -static void Webserver_SendMIMETypeHeader(void) -{ - uip_tcp_appstate_t* const AppState = &uip_conn->appstate; - char* const AppData = (char*)uip_appdata; - - char* Extension = strpbrk(AppState->FileName, "."); - uint16_t MIMEHeaderLength = 0; - - /* Check to see if a file extension was found for the requested filename */ + /* 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 (int i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++) + for (uint8_t i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++) { - if (strcmp_P(&Extension[1], MIMETypes[i].Extension) == 0) + if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0) { - MIMEHeaderLength = strlen_P(MIMETypes[i].MIMEType); - strncpy_P(AppData, MIMETypes[i].MIMEType, MIMEHeaderLength); + strcpy(&AppData[strlen(AppData)], MIMETypes[i].MIMEType); + FoundMIMEType = true; break; } } } /* Check if a MIME type was found and copied to the output buffer */ - if (!(MIMEHeaderLength)) + if (!(FoundMIMEType)) { /* MIME type not found - copy over the default MIME type */ - MIMEHeaderLength = strlen_P(DefaultMIMEType); - strncpy_P(AppData, DefaultMIMEType, MIMEHeaderLength); + strcpy_P(&AppData[strlen(AppData)], DefaultMIMEType); } - /* Add the end-of line terminator and end-of-headers terminator after the MIME type */ - strncpy(&AppData[MIMEHeaderLength], "\r\n\r\n", sizeof("\r\n\r\n")); - MIMEHeaderLength += (sizeof("\r\n\r\n") - 1); + /* Add the end-of-line terminator and end-of-headers terminator after the MIME type */ + strcpy_P(&AppData[strlen(AppData)], PSTR("\r\n\r\n")); /* Send the MIME header to the receiving client */ - uip_send(AppData, MIMEHeaderLength); + uip_send(AppData, strlen(AppData)); /* When the MIME header is ACKed, progress to the data send stage */ - AppState->NextState = WEBSERVER_STATE_SendData; + 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 Webserver_SendData(void) +static void HTTPServerApp_SendData(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; - /* Must determine the maximum segment size to determine maximum file chunk size */ - uint16_t MaxSegmentSize = uip_mss(); + /* Get the maximum segment size for the current packet */ + uint16_t MaxChunkSize = uip_mss(); - /* Return file pointer to the last ACKed position */ - f_lseek(&AppState->FileHandle, AppState->ACKedFilePos); - /* Read the next chunk of data from the open file */ - f_read(&AppState->FileHandle, AppData, MaxSegmentSize, &AppState->SentChunkSize); + f_read(&AppState->HTTPServer.FileHandle, AppData, MaxChunkSize, &AppState->HTTPServer.SentChunkSize); /* Send the next file chunk to the receiving client */ - uip_send(AppData, AppState->SentChunkSize); + uip_send(AppData, AppState->HTTPServer.SentChunkSize); /* Check if we are at the last chunk of the file, if so next ACK should close the connection */ - AppState->NextState = (MaxSegmentSize != AppState->SentChunkSize) ? WEBSERVER_STATE_Closing : WEBSERVER_STATE_SendData; + if (MaxChunkSize != AppState->HTTPServer.SentChunkSize) + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; }