MeterLogger
Functions
httpdespfs.h File Reference
#include "httpd.h"
#include "espfs.h"
Include dependency graph for httpdespfs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int cgiEspFsHook (HttpdConnData *connData)
 
int ICACHE_FLASH_ATTR cgiEspFsTemplate (HttpdConnData *connData)
 

Function Documentation

◆ cgiEspFsHook()

int cgiEspFsHook ( HttpdConnData connData)

Definition at line 39 of file httpdespfs.c.

References HttpdConnData::cgiData, HttpdConnData::conn, espconn_sent(), espFsClose(), espFsOpen(), espFsRead(), HTTPD_CGI_DONE, HTTPD_CGI_MORE, HTTPD_CGI_NOTFOUND, httpdEndHeaders(), httpdGetMimetype(), httpdHeader(), httpdStartResponse(), NULL, and HttpdConnData::url.

39  {
40  EspFsFile *file=connData->cgiData;
41  int len;
42  char buff[1024];
43 
44  if (connData->conn==NULL) {
45  //Connection aborted. Clean up.
46  espFsClose(file);
47  return HTTPD_CGI_DONE;
48  }
49 
50  if (file==NULL) {
51  //First call to this cgi. Open the file so we can read it.
52  file=espFsOpen(connData->url);
53  if (file==NULL) {
54  return HTTPD_CGI_NOTFOUND;
55  }
56  connData->cgiData=file;
57  httpdStartResponse(connData, 200);
58  httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
59  httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate");
60  httpdEndHeaders(connData);
61  return HTTPD_CGI_MORE;
62  }
63 
64  len=espFsRead(file, buff, 1024);
65  if (len>0) espconn_sent(connData->conn, (uint8 *)buff, len);
66  if (len!=1024) {
67  //We're done.
68  espFsClose(file);
69  return HTTPD_CGI_DONE;
70  } else {
71  //Ok, till next time.
72  return HTTPD_CGI_MORE;
73  }
74 }
sint8 espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length)
Definition: espconn.c:368
int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len)
Definition: espfs.c:165
#define NULL
Definition: def.h:47
void ICACHE_FLASH_ATTR espFsClose(EspFsFile *fh)
Definition: espfs.c:214
char * url
Definition: httpd.h:21
#define HTTPD_CGI_NOTFOUND
Definition: httpd.h:10
#define HTTPD_CGI_DONE
Definition: httpd.h:9
#define HTTPD_CGI_MORE
Definition: httpd.h:8
unsigned char uint8
Definition: c_types.h:45
EspFsFile ICACHE_FLASH_ATTR * espFsOpen(char *fileName)
Definition: espfs.c:97
ICACHE_FLASH_ATTR void httpdStartResponse(HttpdConnData *conn, int code)
Definition: httpd.c:219
ICACHE_FLASH_ATTR void httpdEndHeaders(HttpdConnData *conn)
Definition: httpd.c:240
ICACHE_FLASH_ATTR const char * httpdGetMimetype(char *url)
Definition: httpd.c:93
ICACHE_FLASH_ATTR void httpdHeader(HttpdConnData *conn, const char *field, const char *val)
Definition: httpd.c:229
void * cgiData
Definition: httpd.h:24
struct espconn * conn
Definition: httpd.h:20
Here is the call graph for this function:

◆ cgiEspFsTemplate()

int ICACHE_FLASH_ATTR cgiEspFsTemplate ( HttpdConnData connData)

Definition at line 88 of file httpdespfs.c.

References HttpdConnData::cgiArg, HttpdConnData::cgiData, HttpdConnData::conn, espFsClose(), espFsOpen(), espFsRead(), TplData::file, HTTPD_CGI_DONE, HTTPD_CGI_MORE, HTTPD_CGI_NOTFOUND, httpdEndHeaders(), httpdGetMimetype(), httpdHeader(), httpdSend(), httpdStartResponse(), NULL, os_free, os_malloc, TplData::token, TplData::tokenPos, TplData::tplArg, and HttpdConnData::url.

88  {
89  TplData *tpd=connData->cgiData;
90  int len;
91  int x, sp=0;
92  char *e=NULL;
93  char buff[1025];
94 
95  if (connData->conn==NULL) {
96  //Connection aborted. Clean up.
97  ((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg);
98  espFsClose(tpd->file);
99  os_free(tpd);
100  return HTTPD_CGI_DONE;
101  }
102 
103  if (tpd==NULL) {
104  //First call to this cgi. Open the file so we can read it.
105  tpd=(TplData *)os_malloc(sizeof(TplData));
106  tpd->file=espFsOpen(connData->url);
107  tpd->tplArg=NULL;
108  tpd->tokenPos=-1;
109  if (tpd->file==NULL) {
110  return HTTPD_CGI_NOTFOUND;
111  }
112  connData->cgiData=tpd;
113  httpdStartResponse(connData, 200);
114  httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
115  httpdEndHeaders(connData);
116  return HTTPD_CGI_MORE;
117  }
118 
119  len=espFsRead(tpd->file, buff, 1024);
120  if (len>0) {
121  sp=0;
122  e=buff;
123  for (x=0; x<len; x++) {
124  if (tpd->tokenPos==-1) {
125  //Inside ordinary text.
126  if (buff[x]=='%') {
127  //Send raw data up to now
128  if (sp!=0) httpdSend(connData, e, sp);
129  sp=0;
130  //Go collect token chars.
131  tpd->tokenPos=0;
132  } else {
133  sp++;
134  }
135  } else {
136  if (buff[x]=='%') {
137  if (tpd->tokenPos==0) {
138  //This is the second % of a %% escape string.
139  //Send a single % and resume with the normal program flow.
140  httpdSend(connData, "%", 1);
141  } else {
142  //This is an actual token.
143  tpd->token[tpd->tokenPos++]=0; //zero-terminate token
144  ((TplCallback)(connData->cgiArg))(connData, tpd->token, &tpd->tplArg);
145  }
146  //Go collect normal chars again.
147  e=&buff[x+1];
148  tpd->tokenPos=-1;
149  } else {
150  if (tpd->tokenPos<(sizeof(tpd->token)-1)) tpd->token[tpd->tokenPos++]=buff[x];
151  }
152  }
153  }
154  }
155  //Send remaining bit.
156  if (sp!=0) httpdSend(connData, e, sp);
157  if (len!=1024) {
158  //We're done.
159  ((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg);
160  espFsClose(tpd->file);
161  return HTTPD_CGI_DONE;
162  } else {
163  //Ok, till next time.
164  return HTTPD_CGI_MORE;
165  }
166 }
int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len)
Definition: espfs.c:165
#define NULL
Definition: def.h:47
void ICACHE_FLASH_ATTR espFsClose(EspFsFile *fh)
Definition: espfs.c:214
const void * cgiArg
Definition: httpd.h:23
void * tplArg
Definition: httpdespfs.c:81
char * url
Definition: httpd.h:21
#define HTTPD_CGI_NOTFOUND
Definition: httpd.h:10
#define HTTPD_CGI_DONE
Definition: httpd.h:9
#define HTTPD_CGI_MORE
Definition: httpd.h:8
EspFsFile * file
Definition: httpdespfs.c:80
#define os_malloc(s)
Definition: mem.h:41
int tokenPos
Definition: httpdespfs.c:83
char token[64]
Definition: httpdespfs.c:82
EspFsFile ICACHE_FLASH_ATTR * espFsOpen(char *fileName)
Definition: espfs.c:97
#define os_free(s)
Definition: mem.h:40
ICACHE_FLASH_ATTR void httpdStartResponse(HttpdConnData *conn, int code)
Definition: httpd.c:219
ICACHE_FLASH_ATTR void httpdEndHeaders(HttpdConnData *conn)
Definition: httpd.c:240
ICACHE_FLASH_ATTR const char * httpdGetMimetype(char *url)
Definition: httpd.c:93
ICACHE_FLASH_ATTR void httpdHeader(HttpdConnData *conn, const char *field, const char *val)
Definition: httpd.c:229
void * cgiData
Definition: httpd.h:24
struct espconn * conn
Definition: httpd.h:20
ICACHE_FLASH_ATTR int httpdSend(HttpdConnData *conn, const char *data, int len)
Definition: httpd.c:270
void(* TplCallback)(HttpdConnData *connData, char *token, void **arg)
Definition: httpdespfs.c:86
Here is the call graph for this function: