diff --git a/user/httpdespfs.c b/user/httpdespfs.c new file mode 100644 index 0000000..0636145 --- /dev/null +++ b/user/httpdespfs.c @@ -0,0 +1,46 @@ +/* +Connector to let httpd use the espfs filesystem to serve the files in that. +*/ + +#include +#include +#include "httpd.h" +#include "espfs.h" +#include "httpdespfs.h" + +int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) { + EspFsFile *file=connData->cgiData; + int len; + char buff[1024]; + + if (connData->conn==NULL) { + //Connection aborted. Clean up. + espFsClose(file); + return HTTPD_CGI_DONE; + } + + if (file==NULL) { + //First call to this cgi. Open the file so we can read it. + file=espFsOpen(connData->url); + if (file==NULL) { + return HTTPD_CGI_NOTFOUND; + } + connData->cgiData=file; + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url)); + httpdEndHeaders(connData); + return HTTPD_CGI_MORE; + } + + len=espFsRead(file, buff, 1024); + if (len>0) espconn_sent(connData->conn, (uint8 *)buff, len); + if (len!=1024) { + //We're done. + espFsClose(file); + return HTTPD_CGI_DONE; + } else { + //Ok, till next time. + return HTTPD_CGI_MORE; + } +} + diff --git a/user/httpdespfs.h b/user/httpdespfs.h new file mode 100644 index 0000000..1c14b5d --- /dev/null +++ b/user/httpdespfs.h @@ -0,0 +1,9 @@ +#ifndef HTTPDESPFS_H +#define HTTPDESPFS_H + +#include "httpd.h" +#include "espfs.h" + +int cgiEspFsHook(HttpdConnData *connData); + +#endif \ No newline at end of file