MeterLogger
Data Structures | Macros | Functions | Variables
httpd.c File Reference
#include <esp8266.h>
#include <lwip/ip.h>
#include <lwip/udp.h>
#include <lwip/tcp_impl.h>
#include <netif/etharp.h>
#include <lwip/netif.h>
#include <lwip/lwip_napt.h>
#include <lwip/dns.h>
#include <lwip/app/dhcpserver.h>
#include <lwip/opt.h>
#include <espconn.h>
#include "httpd.h"
#include "espfs.h"
#include "debug.h"
#include "tinyprintf.h"
Include dependency graph for httpd.c:

Go to the source code of this file.

Data Structures

struct  HttpdPriv
 
struct  MimeMap
 

Macros

#define MAX_HEAD_LEN   1024
 
#define MAX_CONN   4
 
#define MAX_POST   1024
 
#define MAX_SENDBUFF_LEN   2048
 

Functions

ICACHE_FLASH_ATTR const char * httpdGetMimetype (char *url)
 
static ICACHE_FLASH_ATTR HttpdConnDatahttpdFindConnData (void *arg)
 
static ICACHE_FLASH_ATTR void httpdRetireConn (HttpdConnData *conn)
 
static int httpdHexVal (char c)
 
int httpdUrlDecode (char *val, int valLen, char *ret, int retLen)
 
ICACHE_FLASH_ATTR int httpdFindArg (char *line, char *arg, char *buff, int buffLen)
 
ICACHE_FLASH_ATTR int httpdGetHeader (HttpdConnData *conn, char *header, char *ret, int retLen)
 
ICACHE_FLASH_ATTR void httpdStartResponse (HttpdConnData *conn, int code)
 
ICACHE_FLASH_ATTR void httpdHeader (HttpdConnData *conn, const char *field, const char *val)
 
ICACHE_FLASH_ATTR void httpdEndHeaders (HttpdConnData *conn)
 
ICACHE_FLASH_ATTR void httpdRedirect (HttpdConnData *conn, char *newUrl)
 
ICACHE_FLASH_ATTR int cgiRedirect (HttpdConnData *connData)
 
ICACHE_FLASH_ATTR int httpdSend (HttpdConnData *conn, const char *data, int len)
 
static ICACHE_FLASH_ATTR void xmitSendBuff (HttpdConnData *conn)
 
static ICACHE_FLASH_ATTR void httpdSentCb (void *arg)
 
static ICACHE_FLASH_ATTR void httpdSendResp (HttpdConnData *conn)
 
static ICACHE_FLASH_ATTR void httpdParseHeader (char *h, HttpdConnData *conn)
 
static ICACHE_FLASH_ATTR void httpdRecvCb (void *arg, char *data, unsigned short len)
 
static ICACHE_FLASH_ATTR void httpdReconCb (void *arg, sint8 err)
 
static ICACHE_FLASH_ATTR void httpdDisconCb (void *arg)
 
static ICACHE_FLASH_ATTR void httpdConnectCb (void *arg)
 
ICACHE_FLASH_ATTR void httpdInit (HttpdBuiltInUrl *fixedUrls, int port)
 
ICACHE_FLASH_ATTR static void httpdDisconnectTimerFunc (void *arg)
 
ICACHE_FLASH_ATTR void httpdStop ()
 

Variables

static HttpdBuiltInUrlbuiltInUrls
 
static HttpdPrivconnPrivData
 
static HttpdConnData connData [MAX_CONN]
 
static struct espconn httpdConn = (struct espconn){ 0 }
 
static esp_tcp httpdTcp
 
static const MimeMap mimeTypes []
 
static os_timer_t httpdDisconnectTimer
 
static const char * httpNotFoundHeader ="HTTP/1.0 404 Not Found\r\nServer: esp8266-httpd/0.1\r\nContent-Type: text/plain\r\n\r\nNot Found.\r\n"
 

Macro Definition Documentation

◆ MAX_CONN

#define MAX_CONN   4

Definition at line 41 of file httpd.c.

Referenced by httpdConnectCb(), httpdDisconCb(), httpdFindConnData(), and httpdInit().

◆ MAX_HEAD_LEN

#define MAX_HEAD_LEN   1024

Definition at line 39 of file httpd.c.

Referenced by httpdRecvCb().

◆ MAX_POST

#define MAX_POST   1024

Definition at line 43 of file httpd.c.

Referenced by httpdParseHeader().

◆ MAX_SENDBUFF_LEN

#define MAX_SENDBUFF_LEN   2048

Definition at line 45 of file httpd.c.

Referenced by httpdRecvCb(), httpdSend(), and httpdSentCb().

Function Documentation

◆ cgiRedirect()

ICACHE_FLASH_ATTR int cgiRedirect ( HttpdConnData connData)

Definition at line 256 of file httpd.c.

References HttpdConnData::cgiArg, HttpdConnData::conn, HTTPD_CGI_DONE, httpdRedirect(), ICACHE_FLASH_ATTR, and NULL.

256  {
257  if (connData->conn==NULL) {
258  //Connection aborted. Clean up.
259  return HTTPD_CGI_DONE;
260  }
261  httpdRedirect(connData, (char*)connData->cgiArg);
262  return HTTPD_CGI_DONE;
263 }
#define NULL
Definition: def.h:47
ICACHE_FLASH_ATTR void httpdRedirect(HttpdConnData *conn, char *newUrl)
Definition: httpd.c:246
const void * cgiArg
Definition: httpd.h:23
#define HTTPD_CGI_DONE
Definition: httpd.h:9
struct espconn * conn
Definition: httpd.h:20
Here is the call graph for this function:

◆ httpdConnectCb()

static ICACHE_FLASH_ATTR void httpdConnectCb ( void *  arg)
static

Definition at line 483 of file httpd.c.

References conn, HttpdConnData::conn, espconn_disconnect(), espconn_regist_disconcb(), espconn_regist_reconcb(), espconn_regist_recvcb(), espconn_regist_sentcb(), HttpdPriv::headPos, httpdDisconCb(), httpdReconCb(), httpdRecvCb(), httpdSentCb(), ICACHE_FLASH_ATTR, INFO, MAX_CONN, NULL, HttpdConnData::postBuff, HttpdConnData::postLen, HttpdPriv::postPos, and HttpdConnData::priv.

Referenced by httpdInit().

483  {
484  struct espconn *conn=arg;
485  int i;
486  //Find empty conndata in pool
487  for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break;
488  INFO("Con req, conn=%p, pool slot %d\n", conn, i);
489  if (i==MAX_CONN) {
490  INFO("Aiee, conn pool overflow!\n");
491  espconn_disconnect(conn);
492  return;
493  }
494  connData[i].priv=&connPrivData[i];
495  connData[i].conn=conn;
496  connData[i].priv->headPos=0;
498  connData[i].priv->postPos=0;
499  connData[i].postLen=-1;
500 
505 }
sint8 espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb)
Definition: espconn.c:712
sint8 espconn_regist_recvcb(struct espconn *espconn, espconn_recv_callback recv_cb)
Definition: espconn.c:770
static ICACHE_FLASH_ATTR void httpdRecvCb(void *arg, char *data, unsigned short len)
Definition: httpd.c:394
static struct espconn conn
Definition: captdns.c:50
static ICACHE_FLASH_ATTR void httpdDisconCb(void *arg)
Definition: httpd.c:454
#define NULL
Definition: def.h:47
sint8 espconn_regist_reconcb(struct espconn *espconn, espconn_reconnect_callback recon_cb)
Definition: espconn.c:789
static ICACHE_FLASH_ATTR void httpdReconCb(void *arg, sint8 err)
Definition: httpd.c:446
#define MAX_CONN
Definition: httpd.c:41
int headPos
Definition: httpd.c:54
int postPos
Definition: httpd.c:55
sint8 espconn_disconnect(struct espconn *espconn)
Definition: espconn.c:942
sint8 espconn_regist_disconcb(struct espconn *espconn, espconn_connect_callback discon_cb)
Definition: espconn.c:807
static HttpdPriv * connPrivData
Definition: httpd.c:62
struct espconn * conn
Definition: httpd.h:20
char * postBuff
Definition: httpd.h:28
static HttpdConnData connData[MAX_CONN]
Definition: httpd.c:63
#define INFO(...)
Definition: debug.h:17
int postLen
Definition: httpd.h:27
static ICACHE_FLASH_ATTR void httpdSentCb(void *arg)
Definition: httpd.c:290
HttpdPriv * priv
Definition: httpd.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdDisconCb()

static ICACHE_FLASH_ATTR void httpdDisconCb ( void *  arg)
static

Definition at line 454 of file httpd.c.

References HttpdConnData::cgi, conn, HttpdConnData::conn, ESPCONN_CLOSE, ESPCONN_NONE, httpdFindConnData(), httpdRetireConn(), ICACHE_FLASH_ATTR, INFO, MAX_CONN, NULL, and espconn::state.

Referenced by httpdConnectCb().

454  {
455 #if 0
456  //Stupid esp sdk passes through wrong arg here, namely the one of the *listening* socket.
457  //If it ever gets fixed, be sure to update the code in this snippet; it's probably out-of-date.
459  INFO("Disconnected, conn=%p\n", conn);
460  if (conn==NULL) return;
461  conn->conn=NULL;
462  if (conn->cgi!=NULL) conn->cgi(conn); //flush cgi data
463 #endif
464  //Just look at all the sockets and kill the slot if needed.
465  int i;
466  for (i=0; i<MAX_CONN; i++) {
467  if (connData[i].conn!=NULL) {
468  //Why the >=ESPCONN_CLOSE and not ==? Well, seems the stack sometimes de-allocates
469  //espconns under our noses, especially when connections are interrupted. The memory
470  //is then used for something else, and we can use that to capture *most* of the
471  //disconnect cases.
472  if (connData[i].conn->state==ESPCONN_NONE || connData[i].conn->state>=ESPCONN_CLOSE) {
473  connData[i].conn=NULL;
474  if (connData[i].cgi!=NULL) connData[i].cgi(&connData[i]); //flush cgi data
476  }
477  }
478  }
479 }
static struct espconn conn
Definition: captdns.c:50
#define NULL
Definition: def.h:47
#define MAX_CONN
Definition: httpd.c:41
static ICACHE_FLASH_ATTR void httpdRetireConn(HttpdConnData *conn)
Definition: httpd.c:118
static ICACHE_FLASH_ATTR HttpdConnData * httpdFindConnData(void *arg)
Definition: httpd.c:107
struct espconn * conn
Definition: httpd.h:20
enum espconn_state state
Definition: espconn.h:103
static HttpdConnData connData[MAX_CONN]
Definition: httpd.c:63
#define INFO(...)
Definition: debug.h:17
cgiSendCallback cgi
Definition: httpd.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdDisconnectTimerFunc()

ICACHE_FLASH_ATTR static void httpdDisconnectTimerFunc ( void *  arg)
static

Definition at line 529 of file httpd.c.

References httpdStop(), and ICACHE_FLASH_ATTR.

Referenced by httpdStop().

529  {
530  httpdStop();
531 }
ICACHE_FLASH_ATTR void httpdStop()
Definition: httpd.c:534
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdEndHeaders()

ICACHE_FLASH_ATTR void httpdEndHeaders ( HttpdConnData conn)

Definition at line 240 of file httpd.c.

References httpdSend(), and ICACHE_FLASH_ATTR.

Referenced by authBasic(), cgiEspFsHook(), cgiEspFsTemplate(), and cgiWiFiScan().

240  {
241  httpdSend(conn, "\r\n", -1);
242 }
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdFindArg()

ICACHE_FLASH_ATTR int httpdFindArg ( char *  line,
char *  arg,
char *  buff,
int  buffLen 
)

Definition at line 167 of file httpd.c.

References httpdUrlDecode(), ICACHE_FLASH_ATTR, INFO, NULL, os_strlen, os_strncmp, os_strstr, and strlen.

Referenced by cgiSetup(), and cgiWifiSetMode().

167  {
168  char *p, *e;
169  if (line==NULL) return 0;
170  p=line;
171  while(p!=NULL && *p!='\n' && *p!='\r' && *p!=0) {
172  INFO("findArg: %s\n", p);
173  if (os_strncmp(p, arg, os_strlen(arg))==0 && p[strlen(arg)]=='=') {
174  p+=os_strlen(arg)+1; //move p to start of value
175  e=(char*)os_strstr(p, "&");
176  if (e==NULL) e=p+os_strlen(p);
177  INFO("findArg: val %s len %d\n", p, (e-p));
178  return httpdUrlDecode(p, (e-p), buff, buffLen);
179  }
180  p=(char*)os_strstr(p, "&");
181  if (p!=NULL) p+=1;
182  }
183  INFO("Finding %s in %s: Not found :/\n", arg, line);
184  return -1; //not found
185 }
#define os_strncmp
Definition: osapi.h:44
#define NULL
Definition: def.h:47
#define os_strlen
Definition: osapi.h:43
#define os_strstr
Definition: osapi.h:46
#define INFO(...)
Definition: debug.h:17
#define strlen(a)
Definition: platform.h:25
int httpdUrlDecode(char *val, int valLen, char *ret, int retLen)
Definition: httpd.c:137
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdFindConnData()

static ICACHE_FLASH_ATTR HttpdConnData* httpdFindConnData ( void *  arg)
static

Definition at line 107 of file httpd.c.

References conn, ICACHE_FLASH_ATTR, INFO, MAX_CONN, and NULL.

Referenced by httpdDisconCb(), httpdReconCb(), httpdRecvCb(), and httpdSentCb().

107  {
108  int i;
109  for (i=0; i<MAX_CONN; i++) {
110  if (connData[i].conn==(struct espconn *)arg) return &connData[i];
111  }
112  INFO("FindConnData: Huh? Couldn't find connection for %p\n", arg);
113  return NULL; //WtF?
114 }
static struct espconn conn
Definition: captdns.c:50
#define NULL
Definition: def.h:47
#define MAX_CONN
Definition: httpd.c:41
static HttpdConnData connData[MAX_CONN]
Definition: httpd.c:63
#define INFO(...)
Definition: debug.h:17
Here is the caller graph for this function:

◆ httpdGetHeader()

ICACHE_FLASH_ATTR int httpdGetHeader ( HttpdConnData conn,
char *  header,
char *  ret,
int  retLen 
)

Definition at line 189 of file httpd.c.

References HttpdPriv::head, HttpdPriv::headPos, ICACHE_FLASH_ATTR, os_strncmp, HttpdConnData::priv, and strlen.

Referenced by authBasic().

189  {
190  char *p=conn->priv->head;
191  p=p+strlen(p)+1; //skip GET/POST part
192  p=p+strlen(p)+1; //skip HTTP part
193  while (p<(conn->priv->head+conn->priv->headPos)) {
194  while(*p<=32 && *p!=0) p++; //skip crap at start
195 // INFO("Looking for %s, Header: '%s'\n", header, p);
196  //See if this is the header
197  if (os_strncmp(p, header, strlen(header))==0 && p[strlen(header)]==':') {
198  //Skip 'key:' bit of header line
199  p=p+strlen(header)+1;
200  //Skip past spaces after the colon
201  while(*p==' ') p++;
202  //Copy from p to end
203  while (*p!=0 && *p!='\r' && *p!='\n' && retLen>1) {
204  *ret++=*p++;
205  retLen--;
206  }
207  //Zero-terminate string
208  *ret=0;
209  //All done :)
210  return 1;
211  }
212  p+=strlen(p)+1; //Skip past end of string and \0 terminator
213  }
214  return 0;
215 }
#define os_strncmp
Definition: osapi.h:44
int headPos
Definition: httpd.c:54
char head[MAX_HEAD_LEN]
Definition: httpd.c:53
#define strlen(a)
Definition: platform.h:25
HttpdPriv * priv
Definition: httpd.h:25
Here is the caller graph for this function:

◆ httpdGetMimetype()

ICACHE_FLASH_ATTR const char* httpdGetMimetype ( char *  url)

Definition at line 93 of file httpd.c.

References ICACHE_FLASH_ATTR, MimeMap::mimetype, NULL, os_strcmp, and strlen.

Referenced by cgiEspFsHook(), and cgiEspFsTemplate().

93  {
94  int i=0;
95  //Go find the extension
96  char *ext=url+(strlen(url)-1);
97  while (ext!=url && *ext!='.') ext--;
98  if (*ext=='.') ext++;
99 
100  //ToDo: os_strcmp is case sensitive; we may want to do case-intensive matching here...
101  while (mimeTypes[i].ext!=NULL && os_strcmp(ext, mimeTypes[i].ext)!=0) i++;
102  return mimeTypes[i].mimetype;
103 }
#define NULL
Definition: def.h:47
const char * mimetype
Definition: httpd.c:72
#define os_strcmp
Definition: osapi.h:41
static const MimeMap mimeTypes[]
Definition: httpd.c:77
static const char url[]
Definition: heatshrink.c:27
#define strlen(a)
Definition: platform.h:25
Here is the caller graph for this function:

◆ httpdHeader()

ICACHE_FLASH_ATTR void httpdHeader ( HttpdConnData conn,
const char *  field,
const char *  val 
)

Definition at line 229 of file httpd.c.

References httpdSend(), ICACHE_FLASH_ATTR, strlen, and tfp_snprintf().

Referenced by authBasic(), cgiEspFsHook(), cgiEspFsTemplate(), and cgiWiFiScan().

229  {
230  char buff[256];
231  int l;
232 
233  tfp_snprintf(buff, 256, "%s: %s\r\n", field, val);
234  l = strlen(buff);
235  httpdSend(conn, buff, l);
236 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
#define strlen(a)
Definition: platform.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdHexVal()

static int httpdHexVal ( char  c)
static

Definition at line 126 of file httpd.c.

Referenced by httpdUrlDecode().

126  {
127  if (c>='0' && c<='9') return c-'0';
128  if (c>='A' && c<='F') return c-'A'+10;
129  if (c>='a' && c<='f') return c-'a'+10;
130  return 0;
131 }
Here is the caller graph for this function:

◆ httpdInit()

ICACHE_FLASH_ATTR void httpdInit ( HttpdBuiltInUrl fixedUrls,
int  port 
)

Definition at line 509 of file httpd.c.

References HttpdConnData::conn, espconn_accept(), ESPCONN_NONE, espconn_regist_connectcb(), ESPCONN_TCP, httpdConnectCb(), httpdTcp, ICACHE_FLASH_ATTR, INFO, _esp_tcp::local_port, malloc, MAX_CONN, NULL, espconn::proto, espconn::state, espconn::tcp, and espconn::type.

Referenced by httpd_user_init().

509  {
510  int i;
511 
512  connPrivData = malloc(sizeof(HttpdPriv) * MAX_CONN);
513 
514  for (i=0; i<MAX_CONN; i++) {
515  connData[i].conn=NULL;
516  }
519  httpdTcp.local_port=port;
521  builtInUrls=fixedUrls;
522 
523  INFO("Httpd init, conn=%p\n", &httpdConn);
526 }
static struct espconn httpdConn
Definition: httpd.c:66
esp_tcp * tcp
Definition: espconn.h:105
#define NULL
Definition: def.h:47
int local_port
Definition: espconn.h:72
#define malloc(x)
Definition: platform.h:19
#define MAX_CONN
Definition: httpd.c:41
static esp_tcp httpdTcp
Definition: httpd.c:67
static HttpdBuiltInUrl * builtInUrls
Definition: httpd.c:49
static ICACHE_FLASH_ATTR void httpdConnectCb(void *arg)
Definition: httpd.c:483
sint8 espconn_accept(struct espconn *espconn)
Definition: espconn.c:875
static HttpdPriv * connPrivData
Definition: httpd.c:62
union espconn::@1 proto
struct espconn * conn
Definition: httpd.h:20
enum espconn_type type
Definition: espconn.h:101
enum espconn_state state
Definition: espconn.h:103
sint8 espconn_regist_connectcb(struct espconn *espconn, espconn_connect_callback connect_cb)
Definition: espconn.c:751
static HttpdConnData connData[MAX_CONN]
Definition: httpd.c:63
#define INFO(...)
Definition: debug.h:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdParseHeader()

static ICACHE_FLASH_ATTR void httpdParseHeader ( char *  h,
HttpdConnData conn 
)
static

Definition at line 350 of file httpd.c.

References HttpdConnData::getArgs, ICACHE_FLASH_ATTR, INFO, MAX_POST, NULL, os_malloc, os_strncmp, os_strstr, HttpdConnData::postBuff, HttpdConnData::postLen, HttpdPriv::postPos, HttpdConnData::priv, and HttpdConnData::url.

Referenced by httpdRecvCb().

350  {
351  int i;
352 // INFO("Got header %s\n", h);
353  if (os_strncmp(h, "GET ", 4)==0 || os_strncmp(h, "POST ", 5)==0) {
354  char *e;
355 
356  //Skip past the space after POST/GET
357  i=0;
358  while (h[i]!=' ') i++;
359  conn->url=h+i+1;
360 
361  //Figure out end of url.
362  e=(char*)os_strstr(conn->url, " ");
363  if (e==NULL) return; //wtf?
364  *e=0; //terminate url part
365 
366  INFO("URL = %s\n", conn->url);
367  //Parse out the URL part before the GET parameters.
368  conn->getArgs=(char*)os_strstr(conn->url, "?");
369  if (conn->getArgs!=0) {
370  *conn->getArgs=0;
371  conn->getArgs++;
372  INFO("GET args = %s\n", conn->getArgs);
373  } else {
374  conn->getArgs=NULL;
375  }
376  } else if (os_strncmp(h, "Content-Length: ", 16)==0) {
377  i=0;
378  //Skip trailing spaces
379  while (h[i]!=' ') i++;
380  //Get POST data length
381  conn->postLen=atoi(h+i+1);
382  //Clamp if too big. Hmm, maybe we should error out instead?
383  if (conn->postLen>MAX_POST) conn->postLen=MAX_POST;
384  INFO("Mallocced buffer for %d bytes of post data.\n", conn->postLen);
385  //Alloc the memory.
386  conn->postBuff=(char*)os_malloc(conn->postLen+1);
387  conn->priv->postPos=0;
388  }
389 }
#define os_strncmp
Definition: osapi.h:44
char * getArgs
Definition: httpd.h:22
#define NULL
Definition: def.h:47
#define MAX_POST
Definition: httpd.c:43
char * url
Definition: httpd.h:21
int postPos
Definition: httpd.c:55
#define os_malloc(s)
Definition: mem.h:41
#define os_strstr
Definition: osapi.h:46
char * postBuff
Definition: httpd.h:28
#define INFO(...)
Definition: debug.h:17
int postLen
Definition: httpd.h:27
HttpdPriv * priv
Definition: httpd.h:25
Here is the caller graph for this function:

◆ httpdReconCb()

static ICACHE_FLASH_ATTR void httpdReconCb ( void *  arg,
sint8  err 
)
static

Definition at line 446 of file httpd.c.

References conn, httpdFindConnData(), ICACHE_FLASH_ATTR, INFO, and NULL.

Referenced by httpdConnectCb().

446  {
448  INFO("ReconCb\n");
449  if (conn==NULL) return;
450  //Yeah... No idea what to do here. ToDo: figure something out.
451 }
static struct espconn conn
Definition: captdns.c:50
#define NULL
Definition: def.h:47
static ICACHE_FLASH_ATTR HttpdConnData * httpdFindConnData(void *arg)
Definition: httpd.c:107
#define INFO(...)
Definition: debug.h:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdRecvCb()

static ICACHE_FLASH_ATTR void httpdRecvCb ( void *  arg,
char *  data,
unsigned short  len 
)
static

Definition at line 394 of file httpd.c.

References conn, HttpdPriv::head, HttpdPriv::headPos, httpdFindConnData(), httpdParseHeader(), httpdSendResp(), ICACHE_FLASH_ATTR, INFO, MAX_HEAD_LEN, MAX_SENDBUFF_LEN, NULL, os_strstr, HttpdConnData::postBuff, HttpdConnData::postLen, HttpdPriv::postPos, HttpdConnData::priv, HttpdPriv::sendBuff, HttpdPriv::sendBuffLen, HttpdConnData::url, and xmitSendBuff().

Referenced by httpdConnectCb().

394  {
395  int x;
396  char *p, *e;
397  char sendBuff[MAX_SENDBUFF_LEN];
399  if (conn==NULL) return;
400  conn->priv->sendBuff=sendBuff;
401  conn->priv->sendBuffLen=0;
402 
403  for (x=0; x<len; x++) {
404  if (conn->postLen<0) {
405  //This byte is a header byte.
406  if (conn->priv->headPos!=MAX_HEAD_LEN) conn->priv->head[conn->priv->headPos++]=data[x];
407  conn->priv->head[conn->priv->headPos]=0;
408  //Scan for /r/n/r/n
409  if (data[x]=='\n' && (char *)os_strstr(conn->priv->head, "\r\n\r\n")!=NULL) {
410  //Indicate we're done with the headers.
411  conn->postLen=0;
412  //Reset url data
413  conn->url=NULL;
414  //Find end of next header line
415  p=conn->priv->head;
416  while(p<(&conn->priv->head[conn->priv->headPos-4])) {
417  e=(char *)os_strstr(p, "\r\n");
418  if (e==NULL) break;
419  e[0]=0;
420  httpdParseHeader(p, conn);
421  p=e+2;
422  }
423  //If we don't need to receive post data, we can send the response now.
424  if (conn->postLen==0) {
425  httpdSendResp(conn);
426  }
427  }
428  } else if (conn->priv->postPos!=-1 && conn->postLen!=0 && conn->priv->postPos <= conn->postLen) {
429  //This byte is a POST byte.
430  conn->postBuff[conn->priv->postPos++]=data[x];
431  if (conn->priv->postPos>=conn->postLen) {
432  //Received post stuff.
433  conn->postBuff[conn->priv->postPos]=0; //zero-terminate
434  conn->priv->postPos=-1;
435  INFO("Post data: %s\n", conn->postBuff);
436  //Send the response.
437  httpdSendResp(conn);
438  break;
439  }
440  }
441  }
442  xmitSendBuff(conn);
443 }
static ICACHE_FLASH_ATTR void httpdParseHeader(char *h, HttpdConnData *conn)
Definition: httpd.c:350
static struct espconn conn
Definition: captdns.c:50
#define NULL
Definition: def.h:47
#define MAX_HEAD_LEN
Definition: httpd.c:39
char * url
Definition: httpd.h:21
int headPos
Definition: httpd.c:54
int postPos
Definition: httpd.c:55
static ICACHE_FLASH_ATTR void xmitSendBuff(HttpdConnData *conn)
Definition: httpd.c:280
static ICACHE_FLASH_ATTR HttpdConnData * httpdFindConnData(void *arg)
Definition: httpd.c:107
char head[MAX_HEAD_LEN]
Definition: httpd.c:53
#define os_strstr
Definition: osapi.h:46
static ICACHE_FLASH_ATTR void httpdSendResp(HttpdConnData *conn)
Definition: httpd.c:319
#define MAX_SENDBUFF_LEN
Definition: httpd.c:45
char * sendBuff
Definition: httpd.c:56
char * postBuff
Definition: httpd.h:28
#define INFO(...)
Definition: debug.h:17
int postLen
Definition: httpd.h:27
int sendBuffLen
Definition: httpd.c:57
HttpdPriv * priv
Definition: httpd.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdRedirect()

ICACHE_FLASH_ATTR void httpdRedirect ( HttpdConnData conn,
char *  newUrl 
)

Definition at line 246 of file httpd.c.

References httpdSend(), ICACHE_FLASH_ATTR, strlen, and tfp_snprintf().

Referenced by cgiRedirect(), cgiSetup(), and cgiWifiSetMode().

246  {
247  char buff[1024];
248  int l;
249  tfp_snprintf(buff, 1024, "HTTP/1.1 302 Found\r\nLocation: %s\r\n\r\nMoved to %s\r\n", newUrl, newUrl);
250  l = strlen(buff);
251  httpdSend(conn, buff, l);
252 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
#define strlen(a)
Definition: platform.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdRetireConn()

static ICACHE_FLASH_ATTR void httpdRetireConn ( HttpdConnData conn)
static

Definition at line 118 of file httpd.c.

References HttpdConnData::cgi, HttpdConnData::conn, NULL, os_free, and HttpdConnData::postBuff.

Referenced by httpdDisconCb(), httpdSentCb(), and httpdStop().

118  {
119  if (conn->postBuff!=NULL) os_free(conn->postBuff);
120  conn->postBuff=NULL;
121  conn->cgi=NULL;
122  conn->conn=NULL;
123 }
#define NULL
Definition: def.h:47
#define os_free(s)
Definition: mem.h:40
struct espconn * conn
Definition: httpd.h:20
char * postBuff
Definition: httpd.h:28
cgiSendCallback cgi
Definition: httpd.h:26
Here is the caller graph for this function:

◆ httpdSend()

ICACHE_FLASH_ATTR int httpdSend ( HttpdConnData conn,
const char *  data,
int  len 
)

Definition at line 270 of file httpd.c.

References ICACHE_FLASH_ATTR, MAX_SENDBUFF_LEN, os_memcpy, HttpdConnData::priv, HttpdPriv::sendBuff, HttpdPriv::sendBuffLen, and strlen.

Referenced by authBasic(), cgiEspFsTemplate(), cgiWiFiScan(), httpdEndHeaders(), httpdHeader(), httpdRedirect(), httpdSendResp(), httpdStartResponse(), and tplSetup().

270  {
271  if (len<0) len=strlen(data);
272  if (conn->priv->sendBuffLen+len>MAX_SENDBUFF_LEN) return 0;
273  os_memcpy(conn->priv->sendBuff+conn->priv->sendBuffLen, data, len);
274  conn->priv->sendBuffLen+=len;
275  return 1;
276 }
#define os_memcpy
Definition: osapi.h:36
#define MAX_SENDBUFF_LEN
Definition: httpd.c:45
char * sendBuff
Definition: httpd.c:56
#define strlen(a)
Definition: platform.h:25
int sendBuffLen
Definition: httpd.c:57
HttpdPriv * priv
Definition: httpd.h:25
Here is the caller graph for this function:

◆ httpdSendResp()

static ICACHE_FLASH_ATTR void httpdSendResp ( HttpdConnData conn)
static

Definition at line 319 of file httpd.c.

References HttpdConnData::cgi, HttpdConnData::cgiArg, HttpdBuiltInUrl::cgiArg, HttpdBuiltInUrl::cgiCb, HttpdConnData::cgiData, HTTPD_CGI_DONE, HTTPD_CGI_NOTFOUND, httpdSend(), httpNotFoundHeader, ICACHE_FLASH_ATTR, INFO, NULL, os_strcmp, os_strlen, os_strncmp, HttpdConnData::url, and url.

Referenced by httpdRecvCb().

319  {
320  int i=0;
321  int r;
322  //See if the url is somewhere in our internal url table.
323  while (builtInUrls[i].url!=NULL && conn->url!=NULL) {
324  int match=0;
325 // INFO("%s == %s?\n", builtInUrls[i].url, conn->url);
326  if (os_strcmp(builtInUrls[i].url, conn->url)==0) match=1;
327  if (builtInUrls[i].url[os_strlen(builtInUrls[i].url)-1]=='*' &&
328  os_strncmp(builtInUrls[i].url, conn->url, os_strlen(builtInUrls[i].url)-1)==0) match=1;
329  if (match) {
330  INFO("Is url index %d\n", i);
331  conn->cgiData=NULL;
332  conn->cgi=builtInUrls[i].cgiCb;
333  conn->cgiArg=builtInUrls[i].cgiArg;
334  r=conn->cgi(conn);
335  if (r!=HTTPD_CGI_NOTFOUND) {
336  if (r==HTTPD_CGI_DONE) conn->cgi=NULL; //If cgi finishes immediately: mark conn for destruction.
337  return;
338  }
339  }
340  i++;
341  }
342  //Can't find :/
343  INFO("%s not found. 404!\n", conn->url);
344  httpdSend(conn, httpNotFoundHeader, -1);
345  conn->cgi=NULL; //mark for destruction
346 }
#define os_strncmp
Definition: osapi.h:44
#define NULL
Definition: def.h:47
const void * cgiArg
Definition: httpd.h:23
char * url
Definition: httpd.h:21
#define HTTPD_CGI_NOTFOUND
Definition: httpd.h:10
#define HTTPD_CGI_DONE
Definition: httpd.h:9
const void * cgiArg
Definition: httpd.h:36
static const char * httpNotFoundHeader
Definition: httpd.c:314
#define os_strlen
Definition: osapi.h:43
#define os_strcmp
Definition: osapi.h:41
static HttpdBuiltInUrl * builtInUrls
Definition: httpd.c:49
static const char url[]
Definition: heatshrink.c:27
void * cgiData
Definition: httpd.h:24
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
cgiSendCallback cgiCb
Definition: httpd.h:35
#define INFO(...)
Definition: debug.h:17
cgiSendCallback cgi
Definition: httpd.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdSentCb()

static ICACHE_FLASH_ATTR void httpdSentCb ( void *  arg)
static

Definition at line 290 of file httpd.c.

References HttpdConnData::cgi, conn, HttpdConnData::conn, espconn_disconnect(), HTTPD_CGI_DONE, httpdFindConnData(), httpdRetireConn(), INFO, MAX_SENDBUFF_LEN, NULL, HttpdConnData::priv, HttpdPriv::sendBuff, HttpdPriv::sendBuffLen, and xmitSendBuff().

Referenced by httpdConnectCb().

290  {
291  int r;
293  char sendBuff[MAX_SENDBUFF_LEN];
294 
295 // INFO("Sent callback on conn %p\n", conn);
296  if (conn==NULL) return;
297  conn->priv->sendBuff=sendBuff;
298  conn->priv->sendBuffLen=0;
299 
300  if (conn->cgi==NULL) { //Marked for destruction?
301  INFO("Conn %p is done. Closing.\n", conn->conn);
302  espconn_disconnect(conn->conn);
303  httpdRetireConn(conn);
304  return; //No need to call xmitSendBuff.
305  }
306 
307  r=conn->cgi(conn); //Execute cgi fn.
308  if (r==HTTPD_CGI_DONE) {
309  conn->cgi=NULL; //mark for destruction.
310  }
311  xmitSendBuff(conn);
312 }
static struct espconn conn
Definition: captdns.c:50
#define NULL
Definition: def.h:47
#define HTTPD_CGI_DONE
Definition: httpd.h:9
static ICACHE_FLASH_ATTR void httpdRetireConn(HttpdConnData *conn)
Definition: httpd.c:118
sint8 espconn_disconnect(struct espconn *espconn)
Definition: espconn.c:942
static ICACHE_FLASH_ATTR void xmitSendBuff(HttpdConnData *conn)
Definition: httpd.c:280
static ICACHE_FLASH_ATTR HttpdConnData * httpdFindConnData(void *arg)
Definition: httpd.c:107
struct espconn * conn
Definition: httpd.h:20
#define MAX_SENDBUFF_LEN
Definition: httpd.c:45
char * sendBuff
Definition: httpd.c:56
#define INFO(...)
Definition: debug.h:17
int sendBuffLen
Definition: httpd.c:57
cgiSendCallback cgi
Definition: httpd.h:26
HttpdPriv * priv
Definition: httpd.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdStartResponse()

ICACHE_FLASH_ATTR void httpdStartResponse ( HttpdConnData conn,
int  code 
)

Definition at line 219 of file httpd.c.

References httpdSend(), HTTPDVER, ICACHE_FLASH_ATTR, strlen, and tfp_snprintf().

Referenced by authBasic(), cgiEspFsHook(), cgiEspFsTemplate(), and cgiWiFiScan().

219  {
220  char buff[128];
221  int l;
222  tfp_snprintf(buff, 128, "HTTP/1.0 %d OK\r\nServer: esp8266-httpd/"HTTPDVER"\r\n", code);
223  l = strlen(buff);
224  httpdSend(conn, buff, l);
225 }
#define HTTPDVER
Definition: httpd.h:6
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
#define strlen(a)
Definition: platform.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdStop()

ICACHE_FLASH_ATTR void httpdStop ( )

Definition at line 534 of file httpd.c.

References espconn_delete(), espconn_disconnect(), ESPCONN_NONE, free, httpdDisconnectTimer, httpdDisconnectTimerFunc(), httpdRetireConn(), INFO, NULL, os_timer_arm, os_timer_disarm, os_timer_func_t, os_timer_setfn, and espconn::state.

Referenced by httpdDisconnectTimerFunc().

534  {
535  INFO("Httpd stopping, state=%d conn=%p\n", httpdConn.state, &httpdConn);
536 
537  if (httpdConn.state != ESPCONN_NONE) {
538  if (espconn_delete(&httpdConn) == 0) {
542  INFO("Httpd stopped\n");
543  }
544  else {
549  INFO("Httpd still running rescheduling httpdStop\n");
550  }
551  }
552 }
#define free(x)
Definition: platform.h:20
#define os_timer_disarm
Definition: osapi.h:51
static struct espconn httpdConn
Definition: httpd.c:66
#define NULL
Definition: def.h:47
#define os_timer_func_t
Definition: os_type.h:35
static os_timer_t httpdDisconnectTimer
Definition: httpd.c:89
#define os_timer_setfn
Definition: osapi.h:52
static ICACHE_FLASH_ATTR void httpdRetireConn(HttpdConnData *conn)
Definition: httpd.c:118
sint8 espconn_disconnect(struct espconn *espconn)
Definition: espconn.c:942
ICACHE_FLASH_ATTR static void httpdDisconnectTimerFunc(void *arg)
Definition: httpd.c:529
sint8 espconn_delete(struct espconn *espconn)
Definition: espconn.c:1217
static HttpdPriv * connPrivData
Definition: httpd.c:62
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
enum espconn_state state
Definition: espconn.h:103
static HttpdConnData connData[MAX_CONN]
Definition: httpd.c:63
#define INFO(...)
Definition: debug.h:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpdUrlDecode()

int httpdUrlDecode ( char *  val,
int  valLen,
char *  ret,
int  retLen 
)

Definition at line 137 of file httpd.c.

References httpdHexVal(), and ICACHE_FLASH_ATTR.

Referenced by httpdFindArg().

137  {
138  int s=0, d=0;
139  int esced=0, escVal=0;
140  while (s<valLen && d<retLen) {
141  if (esced==1) {
142  escVal=httpdHexVal(val[s])<<4;
143  esced=2;
144  } else if (esced==2) {
145  escVal+=httpdHexVal(val[s]);
146  ret[d++]=escVal;
147  esced=0;
148  } else if (val[s]=='%') {
149  esced=1;
150  } else if (val[s]=='+') {
151  ret[d++]=' ';
152  } else {
153  ret[d++]=val[s];
154  }
155  s++;
156  }
157  if (d<retLen) ret[d]=0;
158  return d;
159 }
static int httpdHexVal(char c)
Definition: httpd.c:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ xmitSendBuff()

static ICACHE_FLASH_ATTR void xmitSendBuff ( HttpdConnData conn)
static

Definition at line 280 of file httpd.c.

References HttpdConnData::conn, espconn_sent(), ICACHE_FLASH_ATTR, HttpdConnData::priv, HttpdPriv::sendBuff, and HttpdPriv::sendBuffLen.

Referenced by httpdRecvCb(), and httpdSentCb().

280  {
281  if (conn->priv->sendBuffLen!=0) {
282  espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen);
283  conn->priv->sendBuffLen=0;
284  }
285 }
sint8 espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length)
Definition: espconn.c:368
struct espconn * conn
Definition: httpd.h:20
char * sendBuff
Definition: httpd.c:56
int sendBuffLen
Definition: httpd.c:57
HttpdPriv * priv
Definition: httpd.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ builtInUrls

HttpdBuiltInUrl* builtInUrls
static

Definition at line 49 of file httpd.c.

◆ connData

HttpdConnData connData[MAX_CONN]
static

Definition at line 63 of file httpd.c.

◆ connPrivData

HttpdPriv* connPrivData
static

Definition at line 62 of file httpd.c.

◆ httpdConn

struct espconn httpdConn = (struct espconn){ 0 }
static

Definition at line 66 of file httpd.c.

◆ httpdDisconnectTimer

os_timer_t httpdDisconnectTimer
static

Definition at line 89 of file httpd.c.

Referenced by httpdStop().

◆ httpdTcp

esp_tcp httpdTcp
static

Definition at line 67 of file httpd.c.

Referenced by httpdInit().

◆ httpNotFoundHeader

const char* httpNotFoundHeader ="HTTP/1.0 404 Not Found\r\nServer: esp8266-httpd/0.1\r\nContent-Type: text/plain\r\n\r\nNot Found.\r\n"
static

Definition at line 314 of file httpd.c.

Referenced by httpdSendResp().

◆ mimeTypes

const MimeMap mimeTypes[]
static
Initial value:
={
{"htm", "text/htm"},
{"html", "text/html"},
{"css", "text/css"},
{"js", "text/javascript"},
{"txt", "text/plain"},
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"png", "image/png"},
{NULL, "text/html"},
}
#define NULL
Definition: def.h:47

Definition at line 77 of file httpd.c.