#include "driver/uart.h" #include "c_types.h" #include "user_interface.h" #include "espconn.h" #include "mem.h" #include "osapi.h" #include "espconn.h" #include "httpd.h" #include "io.h" #define MAX_HEAD_LEN 1024 int cgiSet(struct espconn *conn); typedef int (* cgiSendCallback)(struct espconn *conn); typedef struct { const char *url; const char *fixedResp; cgiSendCallback cgiCb; } UrlData; const char htmlIndex[]="Hello World \

Hello, World!

\ "; static const UrlData urls[]={ {"/", htmlIndex, NULL}, {"/set", NULL, cgiSet}, {NULL, NULL, NULL}, }; static struct espconn conn; static esp_tcp tcp; static int recLen; static char recBuff[MAX_HEAD_LEN]; typedef struct { char *url; char *getArgs; const UrlData *effUrl; } GetData; static GetData getData; //Find a specific arg in a string of get- or post-data. static char *ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg) { char *p; int al; if (line==NULL) return NULL; p=line; al=os_strlen(arg); os_printf("Finding %s in %s\n", arg, line); while (p[0]!=0) { if (os_strncmp(p, arg, al)==0 && p[al]=='=') { //Gotcha. return &p[al+1]; } else { //Wrong arg. Advance to start of next arg. p+=os_strlen(p)+1; } } os_printf("Finding %s in %s: Not found :/\n", arg, line); return NULL; //not found } //ToDo: Move cgi functions to somewhere else int cgiSet(struct espconn *conn) { char *on; static const char okStr[]="OK

OK

"; on=httpdFindArg(getData.getArgs, "led"); os_printf("cgiSet: on=%s\n", on?on:"not found"); if (on!=NULL) ioLed(atoi(on)); espconn_sent(conn, (uint8 *)okStr, os_strlen(okStr)); return 1; } static const char *httpOkHeader="HTTP/1.0 200 OK\r\nServer: esp8266-thingie/0.1\r\nContent-Type: text/html\r\n\r\n"; static const char *httpNotFoundHeader="HTTP/1.0 404 Not Found\r\nServer: esp8266-thingie/0.1\r\n\r\nNot Found.\r\n"; static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { struct espconn *conn=arg; if (getData.effUrl==NULL) { espconn_disconnect(conn); return; } if (getData.effUrl->cgiCb!=NULL) { if (getData.effUrl->cgiCb(conn)) getData.effUrl=NULL; } else { espconn_sent(conn, (uint8 *)getData.effUrl->fixedResp, os_strlen(getData.effUrl->fixedResp)); getData.effUrl=NULL; } } static void ICACHE_FLASH_ATTR httpdSendResp(struct espconn *conn) { int i=0; while (urls[i].url!=NULL) { if (os_strcmp(urls[i].url, getData.url)==0) { getData.effUrl=&urls[i]; os_printf("Is url index %d\n", i); espconn_sent(conn, (uint8 *)httpOkHeader, os_strlen(httpOkHeader)); return; } i++; } //Can't find :/ espconn_sent(conn, (uint8 *)httpNotFoundHeader, os_strlen(httpNotFoundHeader)); } static void ICACHE_FLASH_ATTR httpdParseHeader(char *h) { os_printf("Got header %s\n", h); if (os_strncmp(h, "GET ", 4)==0) { char *e; getData.url=h+4; e=(char*)os_strstr(getData.url, " "); if (e==NULL) return; //wtf? *e=0; //terminate url part os_printf("URL = %s\n", getData.url); getData.getArgs=(char*)os_strstr(getData.url, "?"); if (getData.getArgs!=0) { int x,l; *getData.getArgs=0; getData.getArgs++; os_printf("GET args = %s\n", getData.getArgs); l=os_strlen(getData.getArgs); for (x=0; x