Add changes we want to upstream.

pull/121/head
David Betz 9 years ago
parent 1d0f3136c5
commit fbfe0a39e9
  1. 6
      Makefile
  2. 18
      cmd/cmd.c
  3. 2
      cmd/cmd.h
  4. 11
      espfs/espfs.c
  5. 1
      espfs/espfs.h
  6. 32
      httpd/httpd.c
  7. 2
      httpd/httpd.h

@ -19,8 +19,8 @@
# The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP # The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP
# mode trying to connect to the specified AP *only* if the flash wireless settings are empty! # mode trying to connect to the specified AP *only* if the flash wireless settings are empty!
# This happens on a full serial flash and avoids having to hunt for the AP... # This happens on a full serial flash and avoids having to hunt for the AP...
# STA_SSID ?= STA_SSID ?= $(ESP_LINK_SSID)
# STA_PASS ?= STA_PASS ?= $(ESP_LINK_PASSWD)
# The SOFTAP configuration can be hard-coded here, the minimum parameters to set are AP_SSID && AP_PASS # The SOFTAP configuration can be hard-coded here, the minimum parameters to set are AP_SSID && AP_PASS
# The AP SSID has to be at least 8 characters long, same for AP PASSWORD # The AP SSID has to be at least 8 characters long, same for AP PASSWORD
@ -56,7 +56,7 @@ XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/
# Base directory of the ESP8266 SDK package, absolute # Base directory of the ESP8266 SDK package, absolute
# Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 # Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5
SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.1) SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.2)
# Esptool.py path and port, only used for 1-time serial flashing # Esptool.py path and port, only used for 1-time serial flashing
# Typically you'll use https://github.com/themadinventor/esptool # Typically you'll use https://github.com/themadinventor/esptool

@ -171,6 +171,24 @@ cmdPopArg(CmdRequest *req, void *data, uint16_t len) {
return 0; return 0;
} }
// Copy the next argument from a command structure into the data pointer, returns 0 on success
// -1 on error
int32_t ICACHE_FLASH_ATTR
cmdPopArgPtr(CmdRequest *req, void **pPtr, uint16_t *pLen) {
if (req->arg_num >= req->cmd->argc)
return -1;
*pLen = *(uint16_t*)req->arg_ptr;
req->arg_ptr += 2;
*pPtr = req->arg_ptr;
req->arg_ptr += (*pLen+3)&~3; // round up to multiple of 4
req->arg_num ++;
return 0;
}
// Skip the next argument // Skip the next argument
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
cmdSkipArg(CmdRequest *req) { cmdSkipArg(CmdRequest *req) {

@ -95,6 +95,8 @@ uint32_t cmdGetArgc(CmdRequest *req);
uint16_t cmdArgLen(CmdRequest *req); uint16_t cmdArgLen(CmdRequest *req);
// Copy next arg from request into the data pointer, returns 0 on success, -1 on error // Copy next arg from request into the data pointer, returns 0 on success, -1 on error
int32_t cmdPopArg(CmdRequest *req, void *data, uint16_t len); int32_t cmdPopArg(CmdRequest *req, void *data, uint16_t len);
// Copy a pointer to the next arg from request into *pPtr and the length into *pLen, returns 0 on success, -1 on error
int32_t cmdPopArgPtr(CmdRequest *req, void **pPtr, uint16_t *pLen);
// Skip next arg // Skip next arg
void cmdSkipArg(CmdRequest *req); void cmdSkipArg(CmdRequest *req);

@ -182,6 +182,17 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
} }
} }
//Get file size.
int ICACHE_FLASH_ATTR espFsSize(EspFsFile *fh) {
int32_t len;
if (fh==NULL) return 0;
if (fh->decompressor==COMPRESS_NONE)
memcpyAligned((char*)&len, (char*)&fh->header->fileLenComp, sizeof(int32_t));
else
memcpyAligned((char*)&len, (char*)&fh->header->fileLenDecomp, sizeof(int32_t));
return len;
}
//Read len bytes from the given file into buff. Returns the actual amount of bytes read. //Read len bytes from the given file into buff. Returns the actual amount of bytes read.
int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) { int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
int flen, fdlen; int flen, fdlen;

@ -12,6 +12,7 @@ typedef struct EspFsFile EspFsFile;
EspFsInitResult espFsInit(void *flashAddress); EspFsInitResult espFsInit(void *flashAddress);
EspFsFile *espFsOpen(char *fileName); EspFsFile *espFsOpen(char *fileName);
int espFsFlags(EspFsFile *fh); int espFsFlags(EspFsFile *fh);
int espFsSize(EspFsFile *fh);
int espFsRead(EspFsFile *fh, char *buff, int len); int espFsRead(EspFsFile *fh, char *buff, int len);
void espFsClose(EspFsFile *fh); void espFsClose(EspFsFile *fh);

@ -23,7 +23,6 @@ Esp8266 http server - core routines
#define DBG(format, ...) do { } while(0) #define DBG(format, ...) do { } while(0)
#endif #endif
//Max length of request head //Max length of request head
#define MAX_HEAD_LEN 1024 #define MAX_HEAD_LEN 1024
//Max amount of connections //Max amount of connections
@ -44,6 +43,7 @@ struct HttpdPriv {
char *sendBuff; // output buffer char *sendBuff; // output buffer
short headPos; // offset into header short headPos; // offset into header
short sendBuffLen; // offset into output buffer short sendBuffLen; // offset into output buffer
short sendBuffMax; // size of output buffer
short code; // http response code (only for logging) short code; // http response code (only for logging)
}; };
@ -226,6 +226,14 @@ int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *re
return 0; return 0;
} }
//Setup an output buffer
void ICACHE_FLASH_ATTR httpdSetOutputBuffer(HttpdConnData *conn, char *buff, short max)
{
conn->priv->sendBuff = buff;
conn->priv->sendBuffLen = 0;
conn->priv->sendBuffMax = max;
}
//Start the response headers. //Start the response headers.
void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) { void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) {
char buff[128]; char buff[128];
@ -277,9 +285,9 @@ int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) {
//Returns 1 for success, 0 for out-of-memory. //Returns 1 for success, 0 for out-of-memory.
int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len) { int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len) {
if (len<0) len = strlen(data); if (len<0) len = strlen(data);
if (conn->priv->sendBuffLen + len>MAX_SENDBUFF_LEN) { if (conn->priv->sendBuffLen + len>conn->priv->sendBuffMax) {
DBG("%sERROR! httpdSend full (%d of %d)\n", DBG("%sERROR! httpdSend full (%d of %d)\n",
connStr, conn->priv->sendBuffLen, MAX_SENDBUFF_LEN); connStr, conn->priv->sendBuffLen, conn->priv->sendBuffMax);
return 0; return 0;
} }
os_memcpy(conn->priv->sendBuff + conn->priv->sendBuffLen, data, len); os_memcpy(conn->priv->sendBuff + conn->priv->sendBuffLen, data, len);
@ -288,7 +296,7 @@ int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len)
} }
//Helper function to send any data in conn->priv->sendBuff //Helper function to send any data in conn->priv->sendBuff
static void ICACHE_FLASH_ATTR xmitSendBuff(HttpdConnData *conn) { void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn) {
if (conn->priv->sendBuffLen != 0) { if (conn->priv->sendBuffLen != 0) {
sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen); sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen);
if (status != 0) { if (status != 0) {
@ -307,13 +315,12 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) {
if (conn == NULL) return; // aborted connection if (conn == NULL) return; // aborted connection
char sendBuff[MAX_SENDBUFF_LEN]; char sendBuff[MAX_SENDBUFF_LEN];
conn->priv->sendBuff = sendBuff; httpdSetOutputBuffer(conn, sendBuff, sizeof(sendBuff));
conn->priv->sendBuffLen = 0;
if (conn->cgi == NULL) { //Marked for destruction? if (conn->cgi == NULL) { //Marked for destruction?
//os_printf("Closing 0x%p/0x%p->0x%p\n", arg, conn->conn, conn); //os_printf("Closing 0x%p/0x%p->0x%p\n", arg, conn->conn, conn);
espconn_disconnect(conn->conn); // we will get a disconnect callback espconn_disconnect(conn->conn); // we will get a disconnect callback
return; //No need to call xmitSendBuff. return; //No need to call httpdFlush.
} }
int r = conn->cgi(conn); //Execute cgi fn. int r = conn->cgi(conn); //Execute cgi fn.
@ -324,7 +331,7 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) {
DBG("%sERROR! Bad CGI code %d\n", connStr, r); DBG("%sERROR! Bad CGI code %d\n", connStr, r);
conn->cgi = NULL; //mark for destruction. conn->cgi = NULL; //mark for destruction.
} }
xmitSendBuff(conn); httpdFlush(conn);
} }
static const char *httpNotFoundHeader = "HTTP/1.0 404 Not Found\r\nConnection: close\r\n" static const char *httpNotFoundHeader = "HTTP/1.0 404 Not Found\r\nConnection: close\r\n"
@ -366,7 +373,7 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
//generate a built-in 404 to handle this. //generate a built-in 404 to handle this.
DBG("%s%s not found. 404!\n", connStr, conn->url); DBG("%s%s not found. 404!\n", connStr, conn->url);
httpdSend(conn, httpNotFoundHeader, -1); httpdSend(conn, httpNotFoundHeader, -1);
xmitSendBuff(conn); httpdFlush(conn);
conn->cgi = NULL; //mark for destruction. conn->cgi = NULL; //mark for destruction.
if (conn->post) conn->post->len = 0; // skip any remaining receives if (conn->post) conn->post->len = 0; // skip any remaining receives
return; return;
@ -378,12 +385,12 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
r = conn->cgi(conn); r = conn->cgi(conn);
if (r == HTTPD_CGI_MORE) { if (r == HTTPD_CGI_MORE) {
//Yep, it's happy to do so and has more data to send. //Yep, it's happy to do so and has more data to send.
xmitSendBuff(conn); httpdFlush(conn);
return; return;
} }
else if (r == HTTPD_CGI_DONE) { else if (r == HTTPD_CGI_DONE) {
//Yep, it's happy to do so and already is done sending data. //Yep, it's happy to do so and already is done sending data.
xmitSendBuff(conn); httpdFlush(conn);
conn->cgi = NULL; //mark for destruction. conn->cgi = NULL; //mark for destruction.
if (conn->post) conn->post->len = 0; // skip any remaining receives if (conn->post) conn->post->len = 0; // skip any remaining receives
return; return;
@ -485,8 +492,7 @@ static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short
if (conn == NULL) return; // aborted connection if (conn == NULL) return; // aborted connection
char sendBuff[MAX_SENDBUFF_LEN]; char sendBuff[MAX_SENDBUFF_LEN];
conn->priv->sendBuff = sendBuff; httpdSetOutputBuffer(conn, sendBuff, sizeof(sendBuff));
conn->priv->sendBuffLen = 0;
//This is slightly evil/dirty: we abuse conn->post->len as a state variable for where in the http communications we are: //This is slightly evil/dirty: we abuse conn->post->len as a state variable for where in the http communications we are:
//<0 (-1): Post len unknown because we're still receiving headers //<0 (-1): Post len unknown because we're still receiving headers

@ -59,10 +59,12 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen);
int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen); int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen);
void ICACHE_FLASH_ATTR httpdInit(HttpdBuiltInUrl *fixedUrls, int port); void ICACHE_FLASH_ATTR httpdInit(HttpdBuiltInUrl *fixedUrls, int port);
const char *httpdGetMimetype(char *url); const char *httpdGetMimetype(char *url);
void ICACHE_FLASH_ATTR httpdSetOutputBuffer(HttpdConnData *conn, char *buff, short max);
void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code); void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code);
void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const char *val); void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const char *val);
void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn); void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn);
int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *ret, int retLen); int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *ret, int retLen);
int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len); int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len);
void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn);
#endif #endif

Loading…
Cancel
Save