improved template to support multipart substitution

This commit is contained in:
2016-03-17 23:38:37 +01:00
parent ed17164e1c
commit b9885c8b1a
6 changed files with 242 additions and 66 deletions
+53
View File
@@ -29,15 +29,68 @@ int ICACHE_FLASH_ATTR tplCounter(HttpdConnData *connData, char *token, void **ar
os_sprintf(buff, "%ld", hitCounter);
}
httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE;
}
typedef struct {
uint32_t count_remain;
} RandomNumberState;
//Template code for the counter on the index page.
int ICACHE_FLASH_ATTR tplMultipart(HttpdConnData *connData, char *token, void **arg)
{
if (token == NULL) {
if (*arg != NULL) free(*arg);
return HTTPD_CGI_DONE; // cleanup
}
if (os_strcmp(token, "numbers") == 0) {
RandomNumberState *rns = *arg;
char buff[20];
if (rns == NULL) {
//First call to this cgi. Open the file so we can read it.
rns=(RandomNumberState *)malloc(sizeof(RandomNumberState));
*arg=rns;
// parse count
uint32_t count = 1;
int len = httpdFindArg(connData->getArgs, "count", buff, sizeof(buff));
if (len==-1) {
// no such get arg
} else {
count = (uint32_t)atoi(buff);
}
rns->count_remain = count;
printf("User wants %d numbers.", count);
}
for (int i = 0; i < 100; i++) {
os_sprintf(buff, "<li>%lu\n", os_random());
httpdSend(connData, buff, -1);
if (--rns->count_remain == 0) {
break;
}
}
if (rns->count_remain == 0) {
free(rns);
return HTTPD_CGI_DONE;
}
return HTTPD_CGI_MORE;
}
return HTTPD_CGI_DONE;
}
// better to put it in the fs...
int FLASH_FN cgiRandomNumbers(HttpdConnData *connData) {
+2
View File
@@ -8,4 +8,6 @@ int tplCounter(HttpdConnData *connData, char *token, void **arg);
int cgiRandomNumbers(HttpdConnData *connData);
int tplMultipart(HttpdConnData *connData, char *token, void **arg);
#endif
+18
View File
@@ -28,6 +28,16 @@
#include "sbmp.h"
#define SHOW_HEAP_USE
#ifdef SHOW_HEAP_USE
static ETSTimer prHeapTimer;
static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) {
os_printf("Heap: %ld\n", (unsigned long)system_get_free_heap_size());
}
#endif
/**
* @brief BasicAuth name/password checking function.
@@ -101,6 +111,8 @@ static HttpdBuiltInUrl builtInUrls[] = {
{"*", cgiRedirectApClientToHostname, "esp8266.nonet"}, // redirect func for the captive portal
{"/", cgiEspFsTemplate, (void *)tplCounter},
{"/multipart.tpl", cgiEspFsTemplate, (void *)tplMultipart},
{"/random.tpl", cgiRandomNumbers, NULL},
//Enable the line below to protect the WiFi configuration with an username/password combo.
@@ -166,6 +178,12 @@ void user_init(void)
os_printf("\nReady\n");
#ifdef SHOW_HEAP_USE
os_timer_disarm(&prHeapTimer);
os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
os_timer_arm(&prHeapTimer, 3000, 1);
#endif
// // print TEST on the command interface every 500 ms
// os_timer_disarm(&prTestTimer);
// os_timer_setfn(&prTestTimer, test_timer_task, NULL);