#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[]="
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