esp-open-rtos/examples/esphttpd/cgi.c
Sashka 09b8b8087c Add libesphttpd to extras and supporting example (#458)
* libesphttpd: added extras and example

* Added "heatshrink" as a submodule

* Updated libesphttpd

* Updated libesphttpd

* Trying to fix the commit id

* Updated libesphttpd

* Added zlib1g-dev package

* Use native gcc to build mkespfsimage and mkupgimg

* Added NS and GW for DHCP server configuration
2017-10-23 23:59:13 +02:00

81 lines
2 KiB
C

/*
Some random cgi routines. Used in the LED example and the page that returns the entire
flash as a binary. Also handles the hit counter on the main page.
*/
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <espressif/esp_common.h>
#include "cgi.h"
#include "io.h"
//cause I can't be bothered to write an ioGetLed()
static char currLedState=0;
//Cgi that turns the LED on or off according to the 'led' param in the POST data
int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) {
int len;
char buff[1024];
if (connData->conn==NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
len=httpdFindArg(connData->post->buff, "led", buff, sizeof(buff));
if (len!=0) {
currLedState=atoi(buff);
ioLed(currLedState);
}
httpdRedirect(connData, "led.tpl");
return HTTPD_CGI_DONE;
}
//Template code for the led page.
int ICACHE_FLASH_ATTR tplLed(HttpdConnData *connData, char *token, void **arg) {
char buff[128];
if (token==NULL) return HTTPD_CGI_DONE;
strcpy(buff, "Unknown");
if (strcmp(token, "ledstate")==0) {
if (currLedState) {
strcpy(buff, "on");
} else {
strcpy(buff, "off");
}
}
httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE;
}
static int hitCounter=0;
//Template code for the counter on the index page.
int ICACHE_FLASH_ATTR tplCounter(HttpdConnData *connData, char *token, void **arg) {
char buff[128];
if (token==NULL) return HTTPD_CGI_DONE;
if (strcmp(token, "counter")==0) {
hitCounter++;
sprintf(buff, "%d", hitCounter);
}
httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE;
}