Added comments to cgiwebserversetup

pull/193/head
cskarai 8 years ago committed by Thorsten von Eicken
parent 6e01a07459
commit a3b38459ad
  1. 46
      esp-link/cgiwebserversetup.c

@ -9,28 +9,31 @@
#include "config.h" #include "config.h"
#include "web-server.h" #include "web-server.h"
int html_offset = 0; int upload_offset = 0; // flash offset where to store page upload
int html_header_len = 0; int html_header_len = 0; // HTML header length (for uploading HTML files)
// this is the header to add if user uploads HTML file
const char * HTML_HEADER = "<!doctype html><html><head><title>esp-link</title>" const char * HTML_HEADER = "<!doctype html><html><head><title>esp-link</title>"
"<link rel=stylesheet href=\"/pure.css\"><link rel=stylesheet href=\"/style.css\">" "<link rel=stylesheet href=\"/pure.css\"><link rel=stylesheet href=\"/style.css\">"
"<meta name=viewport content=\"width=device-width, initial-scale=1\"><script src=\"/ui.js\">" "<meta name=viewport content=\"width=device-width, initial-scale=1\"><script src=\"/ui.js\">"
"</script><script src=\"/userpage.js\"></script></head><body><div id=layout> "; "</script><script src=\"/userpage.js\"></script></head><body><div id=layout> ";
// multipart callback for uploading user defined pages
int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *data, int dataLen, int position) int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *data, int dataLen, int position)
{ {
switch(cmd) switch(cmd)
{ {
case FILE_START: case FILE_START:
html_offset = 0; upload_offset = 0;
html_header_len = 0; html_header_len = 0;
// simple HTML file // simple HTML file
if( ( dataLen > 5 ) && ( os_strcmp(data + dataLen - 5, ".html") == 0 ) ) if( ( dataLen > 5 ) && ( os_strcmp(data + dataLen - 5, ".html") == 0 ) ) // if the file ends with .html, wrap into an espfs image
{ {
// write the start block on esp-fs // write the start block on esp-fs
int spi_flash_addr = getUserPageSectionStart(); int spi_flash_addr = getUserPageSectionStart();
spi_flash_erase_sector(spi_flash_addr/SPI_FLASH_SEC_SIZE); spi_flash_erase_sector(spi_flash_addr/SPI_FLASH_SEC_SIZE);
EspFsHeader hdr; EspFsHeader hdr;
hdr.magic = 0xFFFFFFFF; hdr.magic = 0xFFFFFFFF; // espfs magic is invalid during upload
hdr.flags = 0; hdr.flags = 0;
hdr.compression = 0; hdr.compression = 0;
@ -41,25 +44,25 @@ int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *da
hdr.nameLen = len; hdr.nameLen = len;
hdr.fileLenComp = hdr.fileLenDecomp = 0xFFFFFFFF; hdr.fileLenComp = hdr.fileLenDecomp = 0xFFFFFFFF;
spi_flash_write( spi_flash_addr + html_offset, (uint32_t *)(&hdr), sizeof(EspFsHeader) ); spi_flash_write( spi_flash_addr + upload_offset, (uint32_t *)(&hdr), sizeof(EspFsHeader) );
html_offset += sizeof(EspFsHeader); upload_offset += sizeof(EspFsHeader);
char nameBuf[len]; char nameBuf[len];
os_memset(nameBuf, 0, len); os_memset(nameBuf, 0, len);
os_memcpy(nameBuf, data, dataLen); os_memcpy(nameBuf, data, dataLen);
spi_flash_write( spi_flash_addr + html_offset, (uint32_t *)(nameBuf), len ); spi_flash_write( spi_flash_addr + upload_offset, (uint32_t *)(nameBuf), len );
html_offset += len; upload_offset += len;
html_header_len = os_strlen(HTML_HEADER) & ~3; // upload only 4 byte aligned part html_header_len = os_strlen(HTML_HEADER) & ~3; // upload only 4 byte aligned part
char buf[html_header_len]; char buf[html_header_len];
os_memcpy(buf, HTML_HEADER, html_header_len); os_memcpy(buf, HTML_HEADER, html_header_len);
spi_flash_write( spi_flash_addr + html_offset, (uint32_t *)(buf), html_header_len ); spi_flash_write( spi_flash_addr + upload_offset, (uint32_t *)(buf), html_header_len );
html_offset += html_header_len; upload_offset += html_header_len;
} }
break; break;
case FILE_DATA: case FILE_DATA:
if(( position < 4 ) && (html_offset == 0)) if(( position < 4 ) && (upload_offset == 0)) // for espfs images check the magic number
{ {
for(int p = position; p < 4; p++ ) for(int p = position; p < 4; p++ )
{ {
@ -68,11 +71,11 @@ int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *da
os_printf("Not an espfs image!\n"); os_printf("Not an espfs image!\n");
return 1; return 1;
} }
data[p - position] = 0xFF; // clean espfs magic to mark as invalid data[p - position] = 0xFF; // espfs magic is invalid during upload
} }
} }
int spi_flash_addr = getUserPageSectionStart() + html_offset + position; int spi_flash_addr = getUserPageSectionStart() + upload_offset + position;
int spi_flash_end_addr = spi_flash_addr + dataLen; int spi_flash_end_addr = spi_flash_addr + dataLen;
if( spi_flash_end_addr + dataLen >= getUserPageSectionEnd() ) if( spi_flash_end_addr + dataLen >= getUserPageSectionEnd() )
{ {
@ -100,10 +103,10 @@ int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *da
break; break;
case FILE_DONE: case FILE_DONE:
{ {
if( html_offset != 0 ) if( html_header_len != 0 )
{ {
// write the terminating block on esp-fs // write the terminating block on esp-fs
int spi_flash_addr = getUserPageSectionStart() + html_offset + position; int spi_flash_addr = getUserPageSectionStart() + upload_offset + position;
uint32_t pad = 0; uint32_t pad = 0;
uint8_t pad_cnt = (4 - position) & 3; uint8_t pad_cnt = (4 - position) & 3;
@ -112,8 +115,9 @@ int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *da
spi_flash_addr += pad_cnt; spi_flash_addr += pad_cnt;
// create ESPFS image
EspFsHeader hdr; EspFsHeader hdr;
hdr.magic = ESPFS_MAGIC; hdr.magic = ESPFS_MAGIC;
hdr.flags = 1; hdr.flags = 1;
hdr.compression = 0; hdr.compression = 0;
hdr.nameLen = 0; hdr.nameLen = 0;
@ -123,24 +127,28 @@ int ICACHE_FLASH_ATTR webServerSetupMultipartCallback(MultipartCmd cmd, char *da
uint32_t totallen = html_header_len + position; uint32_t totallen = html_header_len + position;
// restore ESPFS magic
spi_flash_write( (int)getUserPageSectionStart(), (uint32_t *)&hdr.magic, sizeof(uint32_t) ); spi_flash_write( (int)getUserPageSectionStart(), (uint32_t *)&hdr.magic, sizeof(uint32_t) );
// set file size
spi_flash_write( (int)getUserPageSectionStart() + 8, &totallen, sizeof(uint32_t) ); spi_flash_write( (int)getUserPageSectionStart() + 8, &totallen, sizeof(uint32_t) );
spi_flash_write( (int)getUserPageSectionStart() + 12, &totallen, sizeof(uint32_t) ); spi_flash_write( (int)getUserPageSectionStart() + 12, &totallen, sizeof(uint32_t) );
} }
else else
{ {
// set espfs magic (set it valid)
uint32_t magic = ESPFS_MAGIC; uint32_t magic = ESPFS_MAGIC;
spi_flash_write( (int)getUserPageSectionStart(), (uint32_t *)&magic, sizeof(uint32_t) ); spi_flash_write( (int)getUserPageSectionStart(), (uint32_t *)&magic, sizeof(uint32_t) );
} }
WEB_Init(); WEB_Init(); // reload the content
} }
break; break;
} }
return 0; return 0;
} }
MultipartCtx * webServerContext = NULL; MultipartCtx * webServerContext = NULL; // multipart upload context for web server
// this callback is called when user uploads the web-page
int ICACHE_FLASH_ATTR cgiWebServerSetupUpload(HttpdConnData *connData) int ICACHE_FLASH_ATTR cgiWebServerSetupUpload(HttpdConnData *connData)
{ {
if( webServerContext == NULL ) if( webServerContext == NULL )

Loading…
Cancel
Save