|
|
|
@ -82,15 +82,15 @@ EspFsFile *tryOpenIndex(const char *path) |
|
|
|
|
return NULL; // failed to guess the right name
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static httpd_cgi_state serveStaticFile(HttpdConnData *connData, const char *filepath) |
|
|
|
|
static httpd_cgi_state serveStaticFile(HttpdConnData *hconn, const char *filepath) |
|
|
|
|
{ |
|
|
|
|
EspFsFile *file = connData->cgiData; |
|
|
|
|
EspFsFile *file = hconn->cgiData; |
|
|
|
|
int len; |
|
|
|
|
uint8_t buff[FILE_CHUNK_LEN + 1]; |
|
|
|
|
char acceptEncodingBuffer[64 + 1]; |
|
|
|
|
int isGzip; |
|
|
|
|
|
|
|
|
|
if (connData->conn == NULL) { |
|
|
|
|
if (hconn->conn == NULL) { |
|
|
|
|
//Connection aborted. Clean up.
|
|
|
|
|
espFsClose(file); |
|
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
@ -124,31 +124,31 @@ static httpd_cgi_state serveStaticFile(HttpdConnData *connData, const char *file |
|
|
|
|
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); |
|
|
|
|
httpdGetHeader(hconn, "Accept-Encoding", acceptEncodingBuffer, 64); |
|
|
|
|
if (strstr(acceptEncodingBuffer, "gzip") == NULL) { |
|
|
|
|
//No Accept-Encoding: gzip header present
|
|
|
|
|
httpdSendStr(connData, gzipNonSupportedMessage); |
|
|
|
|
httpdSendStr(hconn, gzipNonSupportedMessage); |
|
|
|
|
espFsClose(file); |
|
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
connData->cgiData = file; |
|
|
|
|
httpdStartResponse(connData, 200); |
|
|
|
|
hconn->cgiData = file; |
|
|
|
|
httpdStartResponse(hconn, 200); |
|
|
|
|
const char *mime = httpdGetMimetypeOr(filepath, "application/octet-stream"); |
|
|
|
|
httpdHeader(connData, "Content-Type", mime); |
|
|
|
|
httpdHeader(hconn, "Content-Type", mime); |
|
|
|
|
if (isGzip) { |
|
|
|
|
httpdHeader(connData, "Content-Encoding", "gzip"); |
|
|
|
|
httpdHeader(hconn, "Content-Encoding", "gzip"); |
|
|
|
|
} |
|
|
|
|
httpdAddCacheHeaders(connData, mime); |
|
|
|
|
httpdEndHeaders(connData); |
|
|
|
|
httpdAddCacheHeaders(hconn, mime); |
|
|
|
|
httpdEndHeaders(hconn); |
|
|
|
|
return HTTPD_CGI_MORE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
len = espFsRead(file, buff, FILE_CHUNK_LEN); |
|
|
|
|
if (len > 0) { |
|
|
|
|
espfs_dbg("[EspFS] Read file chunk: %d bytes", len); |
|
|
|
|
httpdSend(connData, buff, len); |
|
|
|
|
httpdSend(hconn, buff, len); |
|
|
|
|
} |
|
|
|
|
if (len != FILE_CHUNK_LEN) { |
|
|
|
|
//We're done.
|
|
|
|
@ -182,13 +182,10 @@ typedef enum { |
|
|
|
|
typedef struct { |
|
|
|
|
EspFsFile *file; |
|
|
|
|
|
|
|
|
|
// this is the struct passed to user
|
|
|
|
|
TplData td; |
|
|
|
|
|
|
|
|
|
int tokenPos; |
|
|
|
|
|
|
|
|
|
char buff[FILE_CHUNK_LEN + 1]; |
|
|
|
|
char token[HTTPD_ESPFS_TOKEN_LEN]; |
|
|
|
|
char *pToken; |
|
|
|
|
|
|
|
|
|
bool chunk_resume; |
|
|
|
|
int buff_len; |
|
|
|
@ -199,17 +196,19 @@ typedef struct { |
|
|
|
|
} TplDataInternal; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int tplSendN(TplData *td, const char *str, int len) |
|
|
|
|
int tplSendN(HttpdConnData *conn, const char *str, int len) |
|
|
|
|
{ |
|
|
|
|
if (td == NULL) { return 0; } |
|
|
|
|
TplDataInternal *tdi = container_of(td, TplDataInternal, td); |
|
|
|
|
if (conn == NULL) { return 0; } |
|
|
|
|
|
|
|
|
|
TplDataInternal *tdi = conn->cgiData; |
|
|
|
|
if (!tdi) { return 0; } |
|
|
|
|
|
|
|
|
|
if (tdi->tokEncode == ENCODE_PLAIN) { |
|
|
|
|
return httpdSendStrN(td->conn, str, len); |
|
|
|
|
return httpdSendStrN(conn, str, len); |
|
|
|
|
} else if (tdi->tokEncode == ENCODE_HTML) { |
|
|
|
|
return httpdSend_html(td->conn, str, len); |
|
|
|
|
return httpdSend_html(conn, str, len); |
|
|
|
|
} else if (tdi->tokEncode == ENCODE_JS) { |
|
|
|
|
return httpdSend_js(td->conn, str, len); |
|
|
|
|
return httpdSend_js(conn, str, len); |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -228,9 +227,7 @@ httpd_cgi_state cgiEspFsTemplate(HttpdConnData *conn) |
|
|
|
|
if (tdi) { |
|
|
|
|
TplCallback callback = (TplCallback) conn->cgiArg; |
|
|
|
|
if (callback) { |
|
|
|
|
tdi->td.conn = NULL; // indicate that this is the final call
|
|
|
|
|
tdi->td.token = NULL; |
|
|
|
|
callback(&tdi->td); |
|
|
|
|
callback(conn, NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
espFsClose(tdi->file); |
|
|
|
@ -275,9 +272,6 @@ httpd_cgi_state cgiEspFsTemplate(HttpdConnData *conn) |
|
|
|
|
return HTTPD_CGI_NOTFOUND; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tdi->td.conn = conn; |
|
|
|
|
tdi->td.userData = NULL; |
|
|
|
|
|
|
|
|
|
tdi->tokenPos = -1; |
|
|
|
|
conn->cgiData = tdi; |
|
|
|
|
httpdStartResponse(conn, 200); |
|
|
|
@ -356,13 +350,13 @@ httpd_cgi_state cgiEspFsTemplate(HttpdConnData *conn) |
|
|
|
|
tdi->tokEncode = ENCODE_JS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tdi->td.token = &tdi->token[prefixLen]; |
|
|
|
|
tdi->pToken = &tdi->token[prefixLen]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tdi->chunk_resume = false; |
|
|
|
|
|
|
|
|
|
TplCallback callback = (TplCallback) conn->cgiArg; |
|
|
|
|
httpd_cgi_state status = callback(&tdi->td); |
|
|
|
|
httpd_cgi_state status = callback(conn, tdi->pToken); |
|
|
|
|
if (status == HTTPD_CGI_MORE) { |
|
|
|
|
// espfs_dbg("Multi-part tpl subst, saving parser state");
|
|
|
|
|
// wants to send more in this token's place.....
|
|
|
|
@ -420,9 +414,7 @@ httpd_cgi_state cgiEspFsTemplate(HttpdConnData *conn) |
|
|
|
|
|
|
|
|
|
TplCallback callback = (TplCallback) conn->cgiArg; |
|
|
|
|
if (callback) { |
|
|
|
|
tdi->td.conn = NULL; |
|
|
|
|
tdi->td.token = NULL; |
|
|
|
|
callback(&tdi->td); |
|
|
|
|
callback(conn, NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
espfs_info("Template sent."); |
|
|
|
|