X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/d11ed10c5314c44dc01c06954d1d73d4894cbff8..75d27f8ef0873157c4ca14dc41d1eb4139e0180d:/Projects/Webserver/Lib/HTTPServerApp.c diff --git a/Projects/Webserver/Lib/HTTPServerApp.c b/Projects/Webserver/Lib/HTTPServerApp.c index 01aab76ea..5f0b2cfcd 100644 --- a/Projects/Webserver/Lib/HTTPServerApp.c +++ b/Projects/Webserver/Lib/HTTPServerApp.c @@ -41,20 +41,42 @@ */ char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n" "Server: LUFA RNDIS\r\n" - "Content-type: text/html\r\n" - "Connection: close\r\n\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\r\n" - "The requested file was not found."; + "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"; + +/** List of MIME types for each supported file extension - must be terminated with \ref END_OF_MIME_LIST entry. */ +MIME_Type_t PROGMEM 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 = "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 DiskFATState; + /** Initialization function for the simple HTTP webserver. */ void WebserverApp_Init(void) { @@ -74,104 +96,155 @@ void WebserverApp_Callback(void) char* AppData = (char*)uip_appdata; uint16_t AppDataSize = 0; - if (uip_aborted() || uip_timedout()) + if (uip_aborted() || uip_timedout() || uip_closed()) { - /* Close the file before terminating, if it is open */ - f_close(&AppState->FileToSend); - + /* Check if the open file needs to be closed */ + if (AppState->FileOpen) + { + f_close(&AppState->FileHandle); + AppState->FileOpen = false; + } + + AppState->PrevState = WEBSERVER_STATE_Closed; AppState->CurrentState = WEBSERVER_STATE_Closed; return; } - else if (uip_closed()) - { - /* Completed connection, just return */ - return; - } else if (uip_connected()) { - /* New connection - initialize connection state and data pointer to the appropriate HTTP header */ + /* New connection - initialize connection state values */ + AppState->PrevState = WEBSERVER_STATE_OpenRequestedFile; AppState->CurrentState = WEBSERVER_STATE_OpenRequestedFile; + AppState->FileOpen = false; + } + else if (uip_rexmit()) + { + /* Re-try last state */ + AppState->CurrentState = AppState->PrevState; } switch (AppState->CurrentState) { case WEBSERVER_STATE_OpenRequestedFile: /* Wait for the packet containing the request header */ - if (uip_datalen()) + if (uip_newdata()) { + char* RequestToken = strtok(AppData, " "); + /* Must be a GET request, abort otherwise */ - if (strncmp(AppData, "GET ", (sizeof("GET ") - 1)) != 0) + if (strcmp(RequestToken, "GET") != 0) { uip_abort(); break; } - char FileName[13]; - - /* Copy over the requested filename from the GET request */ - for (uint8_t i = 0; i < (sizeof(FileName) - 1); i++) - { - FileName[i] = AppData[sizeof("GET ") + i]; - - if (FileName[i] == ' ') - { - FileName[i] = 0x00; - break; - } - } + char* RequestedFileName = strtok(NULL, " "); - /* Ensure requested filename is null-terminated */ - FileName[(sizeof(FileName) - 1)] = 0x00; - - /* If no filename specified, assume the default of INDEX.HTM */ - if (FileName[0] == 0x00) - strcpy(FileName, "INDEX.HTM"); + /* 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"); + + /* Ensure filename is null-terminated */ + AppState->FileName[(sizeof(AppState->FileName) - 1)] = 0x00; /* Try to open the file from the Dataflash disk */ - AppState->FileOpen = (f_open(&AppState->FileToSend, FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK); + AppState->FileOpen = (f_open(&AppState->FileHandle, AppState->FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK); + AppState->CurrentFilePos = 0; - AppState->CurrentState = WEBSERVER_STATE_SendHeaders; + AppState->PrevState = WEBSERVER_STATE_OpenRequestedFile; + AppState->CurrentState = WEBSERVER_STATE_SendResponseHeader; } break; - case WEBSERVER_STATE_SendHeaders: + case WEBSERVER_STATE_SendResponseHeader: /* Determine what HTTP header should be sent to the client */ if (AppState->FileOpen) { AppDataSize = strlen_P(HTTP200Header); - strncpy_P(AppData, HTTP200Header, AppDataSize); + strncpy_P(AppData, HTTP200Header, AppDataSize); } else { AppDataSize = strlen_P(HTTP404Header); - strncpy_P(AppData, HTTP404Header, AppDataSize); + strncpy_P(AppData, HTTP404Header, AppDataSize); } - uip_send(AppData, AppDataSize); - - AppState->CurrentState = WEBSERVER_STATE_SendData; + AppState->PrevState = WEBSERVER_STATE_SendResponseHeader; + AppState->CurrentState = WEBSERVER_STATE_SendMIMETypeHeader; + break; + case WEBSERVER_STATE_SendMIMETypeHeader: + /* File must have been found and opened for MIME header to be sent */ + if (AppState->FileOpen) + { + char* Extension = strpbrk(AppState->FileName, "."); + + /* Check to see if a file extension was found for the requested filename */ + 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++) + { + if (strcmp_P(&Extension[1], MIMETypes[i].Extension) == 0) + { + AppDataSize = strlen_P(MIMETypes[i].MIMEType); + strncpy_P(AppData, MIMETypes[i].MIMEType, AppDataSize); + break; + } + } + } + + /* Check if a MIME type was found and copied to the output buffer */ + if (!(AppDataSize)) + { + /* MIME type not found - copy over the default MIME type */ + AppDataSize = strlen_P(DefaultMIMEType); + strncpy_P(AppData, DefaultMIMEType, AppDataSize); + } + + /* Add the end-of line terminator and end-of-headers terminator after the MIME type */ + strncpy(&AppData[AppDataSize], "\r\n\r\n", sizeof("\r\n\r\n")); + AppDataSize += (sizeof("\r\n\r\n") - 1); + } + + AppState->PrevState = WEBSERVER_STATE_SendMIMETypeHeader; + AppState->CurrentState = WEBSERVER_STATE_SendData; break; case WEBSERVER_STATE_SendData: /* If end of file/file not open, progress to the close state */ - if (!(AppState->FileOpen)) + if (!(AppState->FileOpen) && !(uip_rexmit())) { - f_close(&AppState->FileToSend); + f_close(&AppState->FileHandle); uip_close(); + + AppState->PrevState = WEBSERVER_STATE_Closed; AppState->CurrentState = WEBSERVER_STATE_Closed; break; } uint16_t MaxSegSize = uip_mss(); + /* Return file pointer to the last ACKed position */ + f_lseek(&AppState->FileHandle, AppState->CurrentFilePos); + /* Read the next chunk of data from the open file */ - f_read(&AppState->FileToSend, AppData, MaxSegSize, &AppDataSize); - AppState->FileOpen = (MaxSegSize == AppDataSize); + f_read(&AppState->FileHandle, AppData, MaxSegSize, &AppDataSize); + + /* If we are not re-transmitting a lost segment, advance file position */ + if (uip_acked() && !(uip_rexmit())) + { + AppState->FileOpen = (AppDataSize > 0); + AppState->CurrentFilePos += AppDataSize; + } + + /* Stay in the SendData state if retransmission is required until all data sent */ + AppState->PrevState = WEBSERVER_STATE_SendData; - /* If data was read, send it to the client */ - if (AppDataSize) - uip_send(AppData, AppDataSize); - break; } + + /* If data has been loaded into the application buffer by the server, send it to the client */ + if (AppDataSize) + uip_send(AppData, AppDataSize); }