From fa8f6fac5877020d377e7b58b56b7338ca70993a Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sat, 9 Nov 2019 23:16:53 +0100 Subject: [PATCH] pan_lwip_http_server: serve test file for /NumberOfBytes.txt --- .../core/src/include/lwip/apps/httpd_opts.h | 6 +- example/pan_lwip_http_server.c | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/3rd-party/lwip/core/src/include/lwip/apps/httpd_opts.h b/3rd-party/lwip/core/src/include/lwip/apps/httpd_opts.h index 573eb86ce..78e1308a3 100644 --- a/3rd-party/lwip/core/src/include/lwip/apps/httpd_opts.h +++ b/3rd-party/lwip/core/src/include/lwip/apps/httpd_opts.h @@ -178,7 +178,7 @@ * the (readonly) fsdata will grow a bit as every file includes the HTTP * header. */ #if !defined LWIP_HTTPD_DYNAMIC_HEADERS || defined __DOXYGEN__ -#define LWIP_HTTPD_DYNAMIC_HEADERS 0 +#define LWIP_HTTPD_DYNAMIC_HEADERS 1 #endif #if !defined HTTPD_DEBUG || defined __DOXYGEN__ @@ -357,7 +357,7 @@ * Called to free resources allocated by fs_open_custom(). */ #if !defined LWIP_HTTPD_CUSTOM_FILES || defined __DOXYGEN__ -#define LWIP_HTTPD_CUSTOM_FILES 0 +#define LWIP_HTTPD_CUSTOM_FILES 1 #endif /** Set this to 1 to support fs_read() to dynamically read file data. @@ -365,7 +365,7 @@ * and the contents must be ready after fs_open(). */ #if !defined LWIP_HTTPD_DYNAMIC_FILE_READ || defined __DOXYGEN__ -#define LWIP_HTTPD_DYNAMIC_FILE_READ 0 +#define LWIP_HTTPD_DYNAMIC_FILE_READ 1 #endif /** Set this to 1 to include an application state argument per file diff --git a/example/pan_lwip_http_server.c b/example/pan_lwip_http_server.c index 2e8c53264..acadec515 100644 --- a/example/pan_lwip_http_server.c +++ b/example/pan_lwip_http_server.c @@ -58,6 +58,7 @@ // ***************************************************************************** #include +#include #include "btstack_config.h" #include "bnep_lwip.h" @@ -66,9 +67,17 @@ #include "lwip/init.h" #include "lwip/opt.h" #include "lwip/tcpip.h" +#include "lwip/apps/fs.h" #include "lwip/apps/httpd.h" #include "dhserver.h" +#if !LWIP_HTTPD_CUSTOM_FILES +#error This needs LWIP_HTTPD_CUSTOM_FILES +#endif +#if !LWIP_HTTPD_DYNAMIC_HEADERS +#error This needs LWIP_HTTPD_DYNAMIC_HEADERS +#endif + // network types #define NETWORK_TYPE_IPv4 0x0800 #define NETWORK_TYPE_ARP 0x0806 @@ -217,6 +226,84 @@ static dhcp_config_t dhcp_config = /* LISTING_END */ +/* @section Large File Download + * + * @text Listing LargeFile Shows how a configurable test file for performance tests is generated on the fly. + * The filename is the number of bytes to generate, e.g. /1048576.txt results in a 1MB file. + */ + +/* LISTING_START(LargeFile): Provide large file. */ + +int fs_open_custom(struct fs_file *file, const char *name){ + if (*name != '/') return 0; + uint32_t file_size = btstack_atoi(&name[1]); + if (file_size > 0) { + printf("Serving '%s' with %u bytes\n", name, file_size); + /* initialize fs_file correctly */ + memset(file, 0, sizeof(struct fs_file)); + file->len = file_size; + file->index = 0; + file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT; + return 1; + } + return 0; +} + +static uint16_t cgi_buffer_fill(char * cgi_buffer, uint16_t cgi_buffer_size, uint32_t start_pos){ + // fill buffer with dots + memset(cgi_buffer, (uint8_t) '.', cgi_buffer_size); + uint16_t cursor = 10; + uint16_t pos = 0; + const uint16_t line_length = 64; + + while ((pos + line_length ) < cgi_buffer_size){ + // write position + sprintf((char *) &cgi_buffer[pos], "%08"PRIx32, start_pos + pos); + cgi_buffer[pos + 8] = '-'; + // cursor + cgi_buffer[pos+cursor] = '+'; + cursor++; + // new line + cgi_buffer[pos+line_length-1] = '\n'; + pos += line_length; + } + + // handle last incomplete line + if (pos == 0){ + const char * last_line_text = "Thank you for evaluating BTstack!"; + uint16_t bytes_to_copy = btstack_min(strlen(last_line_text), cgi_buffer_size); + memcpy(cgi_buffer, last_line_text, bytes_to_copy); + pos = cgi_buffer_size; + } + + return pos; +} + +int fs_read_custom(struct fs_file *file, char *buffer, int count) +{ + uint32_t remaining_data = file->len - file->index; + if (remaining_data == 0) { + // all bytes already read + return FS_READ_EOF; + } + + uint32_t bytes_to_fill = remaining_data; + if (bytes_to_fill > count){ + bytes_to_fill = count; + } + + uint32_t bytes_written = cgi_buffer_fill(buffer, bytes_to_fill, file->index); + file->index += bytes_written; + + return bytes_written; +} + +void +fs_close_custom(struct fs_file *file){ +} + +/* LISTING_END */ + /* @section DHCP Server Setup * * @text Listing LwipSetup shows the setup of the lwIP network stack and starts the DHCP Server