mirror of https://github.com/jeelabs/esp-link.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.6 KiB
190 lines
5.6 KiB
10 years ago
|
/*
|
||
10 years ago
|
Connector to let httpd use the espfs filesystem to serve the files in it.
|
||
10 years ago
|
*/
|
||
|
|
||
10 years ago
|
/*
|
||
|
* ----------------------------------------------------------------------------
|
||
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||
|
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
|
||
|
* this notice you can do whatever you want with this stuff. If we meet some day,
|
||
|
* and you think this stuff is worth it, you can buy me a beer in return.
|
||
|
* ----------------------------------------------------------------------------
|
||
|
*/
|
||
|
|
||
10 years ago
|
#include <esp8266.h>
|
||
10 years ago
|
#include "httpdespfs.h"
|
||
10 years ago
|
#include "espfs.h"
|
||
10 years ago
|
#include "espfsformat.h"
|
||
|
|
||
|
// The static files marked with FLAG_GZIP are compressed and will be served with GZIP compression.
|
||
|
// If the client does not advertise that he accepts GZIP send following warning message (telnet users for e.g.)
|
||
10 years ago
|
static const char *gzipNonSupportedMessage = "HTTP/1.0 501 Not implemented\r\nServer: esp8266-httpd/"HTTPDVER"\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 52\r\n\r\nYour browser does not accept gzip-compressed data.\r\n";
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
//This is a catch-all cgi function. It takes the url passed to it, looks up the corresponding
|
||
|
//path in the filesystem and if it exists, passes the file through. This simulates what a normal
|
||
|
//webserver would do with static files.
|
||
10 years ago
|
int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) {
|
||
|
EspFsFile *file=connData->cgiData;
|
||
|
int len;
|
||
|
char buff[1024];
|
||
10 years ago
|
char acceptEncodingBuffer[64];
|
||
|
int isGzip;
|
||
10 years ago
|
|
||
|
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;
|
||
|
}
|
||
10 years ago
|
|
||
|
// The gzip checking code is intentionally without #ifdefs because checking
|
||
|
// for FLAG_GZIP (which indicates gzip compressed file) is very easy, doesn't
|
||
|
// mean additional overhead and is actually safer to be on at all times.
|
||
|
// If there are no gzipped files in the image, the code bellow will not cause any harm.
|
||
|
|
||
|
// Check if requested file was GZIP compressed
|
||
|
isGzip = espFsFlags(file) & FLAG_GZIP;
|
||
|
if (isGzip) {
|
||
|
// Check the browser's "Accept-Encoding" header. If the client does not
|
||
|
// advertise that he accepts GZIP send a warning message (telnet users for e.g.)
|
||
|
httpdGetHeader(connData, "Accept-Encoding", acceptEncodingBuffer, 64);
|
||
|
if (os_strstr(acceptEncodingBuffer, "gzip") == NULL) {
|
||
|
//No Accept-Encoding: gzip header present
|
||
|
httpdSend(connData, gzipNonSupportedMessage, -1);
|
||
|
espFsClose(file);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
connData->cgiData=file;
|
||
|
httpdStartResponse(connData, 200);
|
||
|
httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
|
||
10 years ago
|
if (isGzip) {
|
||
|
httpdHeader(connData, "Content-Encoding", "gzip");
|
||
10 years ago
|
}
|
||
10 years ago
|
httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate");
|
||
10 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
|
||
10 years ago
|
//cgiEspFsTemplate can be used as a template.
|
||
|
|
||
10 years ago
|
typedef struct {
|
||
|
EspFsFile *file;
|
||
|
void *tplArg;
|
||
|
char token[64];
|
||
|
int tokenPos;
|
||
|
} TplData;
|
||
|
|
||
10 years ago
|
typedef void (* TplCallback)(HttpdConnData *connData, char *token, void **arg);
|
||
10 years ago
|
|
||
|
int ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData) {
|
||
|
TplData *tpd=connData->cgiData;
|
||
|
int len;
|
||
10 years ago
|
int x, sp=0;
|
||
|
char *e=NULL;
|
||
10 years ago
|
char buff[1025];
|
||
|
|
||
|
if (connData->conn==NULL) {
|
||
|
//Connection aborted. Clean up.
|
||
|
((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg);
|
||
|
espFsClose(tpd->file);
|
||
|
os_free(tpd);
|
||
|
return HTTPD_CGI_DONE;
|
||
|
}
|
||
|
|
||
|
if (tpd==NULL) {
|
||
|
//First call to this cgi. Open the file so we can read it.
|
||
|
tpd=(TplData *)os_malloc(sizeof(TplData));
|
||
|
tpd->file=espFsOpen(connData->url);
|
||
|
tpd->tplArg=NULL;
|
||
|
tpd->tokenPos=-1;
|
||
|
if (tpd->file==NULL) {
|
||
10 years ago
|
espFsClose(tpd->file);
|
||
|
os_free(tpd);
|
||
|
return HTTPD_CGI_NOTFOUND;
|
||
|
}
|
||
|
if (espFsFlags(tpd->file) & FLAG_GZIP) {
|
||
|
os_printf("cgiEspFsTemplate: Trying to use gzip-compressed file %s as template!\n", connData->url);
|
||
|
espFsClose(tpd->file);
|
||
|
os_free(tpd);
|
||
10 years ago
|
return HTTPD_CGI_NOTFOUND;
|
||
|
}
|
||
|
connData->cgiData=tpd;
|
||
|
httpdStartResponse(connData, 200);
|
||
|
httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
|
||
|
httpdEndHeaders(connData);
|
||
|
return HTTPD_CGI_MORE;
|
||
|
}
|
||
|
|
||
|
len=espFsRead(tpd->file, buff, 1024);
|
||
|
if (len>0) {
|
||
|
sp=0;
|
||
|
e=buff;
|
||
|
for (x=0; x<len; x++) {
|
||
|
if (tpd->tokenPos==-1) {
|
||
|
//Inside ordinary text.
|
||
|
if (buff[x]=='%') {
|
||
|
//Send raw data up to now
|
||
10 years ago
|
if (sp!=0) httpdSend(connData, e, sp);
|
||
10 years ago
|
sp=0;
|
||
|
//Go collect token chars.
|
||
|
tpd->tokenPos=0;
|
||
|
} else {
|
||
|
sp++;
|
||
|
}
|
||
|
} else {
|
||
|
if (buff[x]=='%') {
|
||
10 years ago
|
if (tpd->tokenPos==0) {
|
||
|
//This is the second % of a %% escape string.
|
||
|
//Send a single % and resume with the normal program flow.
|
||
10 years ago
|
httpdSend(connData, "%", 1);
|
||
10 years ago
|
} else {
|
||
|
//This is an actual token.
|
||
|
tpd->token[tpd->tokenPos++]=0; //zero-terminate token
|
||
|
((TplCallback)(connData->cgiArg))(connData, tpd->token, &tpd->tplArg);
|
||
|
}
|
||
10 years ago
|
//Go collect normal chars again.
|
||
|
e=&buff[x+1];
|
||
|
tpd->tokenPos=-1;
|
||
|
} else {
|
||
|
if (tpd->tokenPos<(sizeof(tpd->token)-1)) tpd->token[tpd->tokenPos++]=buff[x];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//Send remaining bit.
|
||
10 years ago
|
if (sp!=0) httpdSend(connData, e, sp);
|
||
10 years ago
|
if (len!=1024) {
|
||
|
//We're done.
|
||
|
((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg);
|
||
|
espFsClose(tpd->file);
|
||
10 years ago
|
os_free(tpd);
|
||
10 years ago
|
return HTTPD_CGI_DONE;
|
||
|
} else {
|
||
|
//Ok, till next time.
|
||
|
return HTTPD_CGI_MORE;
|
||
|
}
|
||
|
}
|
||
|
|