X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/08e8d6393edeaf775b5c3fc3bf2a76e62698d129..8154331da60ac08b0e2b09ca67008ec4a8c7698b:/Projects/Webserver/Lib/HTTPServerApp.c diff --git a/Projects/Webserver/Lib/HTTPServerApp.c b/Projects/Webserver/Lib/HTTPServerApp.c index 18366888d..e781beb22 100644 --- a/Projects/Webserver/Lib/HTTPServerApp.c +++ b/Projects/Webserver/Lib/HTTPServerApp.c @@ -40,27 +40,27 @@ /** 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 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. */ -MIME_Type_t PROGMEM MIMETypes[] = +const MIME_Type_t MIMETypes[] = { {.Extension = "htm", .MIMEType = "text/html"}, {.Extension = "jpg", .MIMEType = "image/jpeg"}, @@ -98,36 +98,36 @@ void HTTPServerApp_Callback(void) if (uip_aborted() || uip_timedout() || uip_closed()) { /* Connection is being terminated for some reason - close file handle */ - f_close(&AppState->FileHandle); - AppState->FileOpen = false; + 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->CurrentState = WEBSERVER_STATE_Closed; - AppState->NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; } 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_acked() || uip_newdata() || uip_connected() || uip_poll()) { - switch (AppState->CurrentState) + switch (AppState->HTTPServer.CurrentState) { case WEBSERVER_STATE_OpenRequestedFile: HTTPServerApp_OpenRequestedFile(); @@ -144,13 +144,13 @@ void HTTPServerApp_Callback(void) case WEBSERVER_STATE_Closing: uip_close(); - AppState->NextState = 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 HTTPServerApp_OpenRequestedFile(void) @@ -175,19 +175,19 @@ static void HTTPServerApp_OpenRequestedFile(void) /* 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)); + strncpy(AppState->HTTPServer.FileName, &RequestedFileName[1], (sizeof(AppState->HTTPServer.FileName) - 1)); else - strcpy(AppState->FileName, "index.htm"); + strcpy(AppState->HTTPServer.FileName, "index.htm"); /* Ensure filename is null-terminated */ - AppState->FileName[(sizeof(AppState->FileName) - 1)] = 0x00; + 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 @@ -198,25 +198,23 @@ 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; + const char* HeaderToSend; /* Determine which HTTP header should be sent to the client */ - if (AppState->FileOpen) + if (AppState->HTTPServer.FileOpen) { HeaderToSend = HTTP200Header; - AppState->NextState = WEBSERVER_STATE_SendMIMETypeHeader; + AppState->HTTPServer.NextState = WEBSERVER_STATE_SendMIMETypeHeader; } else { HeaderToSend = HTTP404Header; - AppState->NextState = WEBSERVER_STATE_Closing; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; } /* 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); + strcpy_P(AppData, HeaderToSend); + uip_send(AppData, strlen(AppData)); } /** HTTP Server State handler for the MIME Header Send state. This state manages the transmission of the file @@ -227,7 +225,7 @@ static void HTTPServerApp_SendMIMETypeHeader(void) uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; - char* Extension = strpbrk(AppState->FileName, "."); + char* Extension = strpbrk(AppState->HTTPServer.FileName, "."); uint16_t MIMEHeaderLength = 0; /* Check to see if a file extension was found for the requested filename */ @@ -236,10 +234,10 @@ static void HTTPServerApp_SendMIMETypeHeader(void) /* 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++) { - 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); + MIMEHeaderLength = strlen(MIMETypes[i].MIMEType); + strncpy(AppData, MIMETypes[i].MIMEType, MIMEHeaderLength); break; } } @@ -261,7 +259,7 @@ static void HTTPServerApp_SendMIMETypeHeader(void) uip_send(AppData, MIMEHeaderLength); /* 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 @@ -276,14 +274,14 @@ static void HTTPServerApp_SendData(void) uint16_t MaxSegmentSize = uip_mss(); /* Return file pointer to the last ACKed position */ - f_lseek(&AppState->FileHandle, AppState->ACKedFilePos); + f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.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, MaxSegmentSize, &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; + AppState->HTTPServer.NextState = (MaxSegmentSize != AppState->HTTPServer.SentChunkSize) ? WEBSERVER_STATE_Closing : WEBSERVER_STATE_SendData; }