X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b6a4584a19e81c2e1f909355bbc64c2b8cea84f6..f4f44f9fc11d0ee9e0dbaf3323d095af32e8b0ef:/Projects/Webserver/Lib/HTTPServerApp.c diff --git a/Projects/Webserver/Lib/HTTPServerApp.c b/Projects/Webserver/Lib/HTTPServerApp.c index e31bdbb0b..081207c6b 100644 --- a/Projects/Webserver/Lib/HTTPServerApp.c +++ b/Projects/Webserver/Lib/HTTPServerApp.c @@ -59,7 +59,7 @@ char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n" /** Default MIME type sent if no other MIME type can be determined */ char PROGMEM DefaultMIMEType[] = "text/plain"; -/** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */ +/** List of MIME types for each supported file extension. */ MIME_Type_t PROGMEM MIMETypes[] = { {.Extension = "htm", .MIMEType = "text/html"}, @@ -67,9 +67,9 @@ MIME_Type_t PROGMEM MIMETypes[] = {.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"}, }; @@ -79,7 +79,7 @@ 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,60 +91,60 @@ 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; + 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_newdata() || uip_acked() || uip_connected() || uip_poll()) + if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll()) { - switch (AppState->CurrentState) + switch (AppState->HTTPServer.CurrentState) { case WEBSERVER_STATE_OpenRequestedFile: - Webserver_OpenRequestedFile(); + HTTPServerApp_OpenRequestedFile(); break; case WEBSERVER_STATE_SendResponseHeader: - Webserver_SendResponseHeader(); + HTTPServerApp_SendResponseHeader(); break; case WEBSERVER_STATE_SendMIMETypeHeader: - Webserver_SendMIMETypeHeader(); + HTTPServerApp_SendMIMETypeHeader(); break; case WEBSERVER_STATE_SendData: - Webserver_SendData(); + HTTPServerApp_SendData(); break; case WEBSERVER_STATE_Closing: uip_close(); - AppState->NextState = WEBSERVER_STATE_Closed; + AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; break; } } @@ -153,7 +153,7 @@ void WebserverApp_Callback(void) /** HTTP Server State handler for the Request Process state. This state manages the processing of incomming 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; @@ -175,59 +175,57 @@ static void Webserver_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 * 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* 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 * MIME type header for the requested file to the receiving HTTP client. */ -static void Webserver_SendMIMETypeHeader(void) +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 */ @@ -261,13 +259,13 @@ static void Webserver_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 * 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; @@ -276,14 +274,14 @@ static void Webserver_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; }