MeterLogger
Data Structures | Macros | Typedefs | Functions
httpd.h File Reference
#include <c_types.h>
#include <esp8266.h>
Include dependency graph for httpd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  HttpdConnData
 
struct  HttpdBuiltInUrl
 

Macros

#define HTTPDVER   "0.2"
 
#define HTTPD_CGI_MORE   0
 
#define HTTPD_CGI_DONE   1
 
#define HTTPD_CGI_NOTFOUND   2
 
#define HTTPD_CGI_AUTHENTICATED   2
 

Typedefs

typedef struct HttpdPriv HttpdPriv
 
typedef struct HttpdConnData HttpdConnData
 
typedef int(* cgiSendCallback) (HttpdConnData *connData)
 

Functions

ICACHE_FLASH_ATTR int cgiRedirect (HttpdConnData *connData)
 
ICACHE_FLASH_ATTR void httpdRedirect (HttpdConnData *conn, char *newUrl)
 
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 void httpdInit (HttpdBuiltInUrl *fixedUrls, int port)
 
const char * httpdGetMimetype (char *url)
 
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 int httpdGetHeader (HttpdConnData *conn, char *header, char *ret, int retLen)
 
ICACHE_FLASH_ATTR int httpdSend (HttpdConnData *conn, const char *data, int len)
 
ICACHE_FLASH_ATTR void httpdStop ()
 

Macro Definition Documentation

◆ HTTPD_CGI_AUTHENTICATED

#define HTTPD_CGI_AUTHENTICATED   2

Definition at line 11 of file httpd.h.

Referenced by authBasic().

◆ HTTPD_CGI_DONE

#define HTTPD_CGI_DONE   1

◆ HTTPD_CGI_MORE

#define HTTPD_CGI_MORE   0

Definition at line 8 of file httpd.h.

Referenced by cgiEspFsHook(), and cgiEspFsTemplate().

◆ HTTPD_CGI_NOTFOUND

#define HTTPD_CGI_NOTFOUND   2

Definition at line 10 of file httpd.h.

Referenced by cgiEspFsHook(), cgiEspFsTemplate(), and httpdSendResp().

◆ HTTPDVER

#define HTTPDVER   "0.2"

Definition at line 6 of file httpd.h.

Referenced by httpdStartResponse().

Typedef Documentation

◆ cgiSendCallback

typedef int(* cgiSendCallback) (HttpdConnData *connData)

Definition at line 16 of file httpd.h.

◆ HttpdConnData

typedef struct HttpdConnData HttpdConnData

Definition at line 14 of file httpd.h.

◆ HttpdPriv

typedef struct HttpdPriv HttpdPriv

Definition at line 13 of file httpd.h.

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:

◆ 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:

◆ 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()

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:

◆ 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:

◆ 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:

◆ 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:

◆ 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: