1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

make upload callback packets aligned to defined size

having this a multiple of 512 bytes helps writing to SDcard
2048 looks reasonable and fast, but could be lowered if too much
This commit is contained in:
ficeto 2015-05-13 01:09:39 +03:00
parent 8774b6ed48
commit c0fdd09132
2 changed files with 26 additions and 44 deletions

View File

@ -327,6 +327,15 @@ void ESP8266WebServer::_parseArguments(String data) {
} }
void ESP8266WebServer::_uploadWriteByte(uint8_t b){
if(_currentUpload.buflen == HTTP_UPLOAD_BUFLEN){
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.buflen = 0;
}
_currentUpload.buf[_currentUpload.buflen++] = b;
}
void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
#ifdef DEBUG #ifdef DEBUG
@ -428,41 +437,25 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
uint8_t argByte = client.read(); uint8_t argByte = client.read();
readfile: readfile:
while(argByte != 0x0D){ while(argByte != 0x0D){
_currentUpload.buf[_currentUpload.buflen++] = argByte; _uploadWriteByte(argByte);
if(_currentUpload.buflen == 1460){
#ifdef DEBUG
DEBUG_OUTPUT.println("Write File: 1460");
#endif
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.buflen = 0;
}
argByte = client.read(); argByte = client.read();
} }
argByte = client.read(); argByte = client.read();
if(argByte == 0x0A){ if(argByte == 0x0A){
#ifdef DEBUG
DEBUG_OUTPUT.print("Write File: ");
DEBUG_OUTPUT.println(_currentUpload.buflen);
#endif
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.buflen = 0;
argByte = client.read(); argByte = client.read();
if((char)argByte != '-'){ if((char)argByte != '-'){
//continue reading the file //continue reading the file
_currentUpload.buf[_currentUpload.buflen++] = 0x0D; _uploadWriteByte(0x0D);
_currentUpload.buf[_currentUpload.buflen++] = 0x0A; _uploadWriteByte(0x0A);
goto readfile; goto readfile;
} else { } else {
argByte = client.read(); argByte = client.read();
if((char)argByte != '-'){ if((char)argByte != '-'){
//continue reading the file //continue reading the file
_currentUpload.buf[_currentUpload.buflen++] = 0x0D; _uploadWriteByte(0x0D);
_currentUpload.buf[_currentUpload.buflen++] = 0x0A; _uploadWriteByte(0x0A);
_currentUpload.buf[_currentUpload.buflen++] = (uint8_t)('-'); _uploadWriteByte((uint8_t)('-'));
goto readfile; goto readfile;
} }
} }
@ -471,7 +464,10 @@ readfile:
client.readBytes(endBuf, boundary.length()); client.readBytes(endBuf, boundary.length());
if(strstr((const char*)endBuf, (const char*)(boundary.c_str())) != NULL){ if(strstr((const char*)endBuf, (const char*)(boundary.c_str())) != NULL){
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.status = UPLOAD_FILE_END; _currentUpload.status = UPLOAD_FILE_END;
if(_fileUploadHandler) _fileUploadHandler();
#ifdef DEBUG #ifdef DEBUG
DEBUG_OUTPUT.print("End File: "); DEBUG_OUTPUT.print("End File: ");
DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(_currentUpload.filename);
@ -480,7 +476,6 @@ readfile:
DEBUG_OUTPUT.print(" Size: "); DEBUG_OUTPUT.print(" Size: ");
DEBUG_OUTPUT.println(_currentUpload.size); DEBUG_OUTPUT.println(_currentUpload.size);
#endif #endif
if(_fileUploadHandler) _fileUploadHandler();
line = client.readStringUntil(0x0D); line = client.readStringUntil(0x0D);
client.readStringUntil(0x0A); client.readStringUntil(0x0A);
if(line == "--"){ if(line == "--"){
@ -491,33 +486,17 @@ readfile:
} }
continue; continue;
} else { } else {
_currentUpload.buf[_currentUpload.buflen++] = 0x0D; _uploadWriteByte(0x0D);
_currentUpload.buf[_currentUpload.buflen++] = 0x0A; _uploadWriteByte(0x0A);
uint32_t i = 0; uint32_t i = 0;
while(i < boundary.length()){ while(i < boundary.length()){
_currentUpload.buf[_currentUpload.buflen++] = endBuf[i++]; _uploadWriteByte(endBuf[i++]);
if(_currentUpload.buflen == 1460){
#ifdef DEBUG
DEBUG_OUTPUT.println("Write File: 1460");
#endif
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.buflen = 0;
}
} }
argByte = client.read(); argByte = client.read();
goto readfile; goto readfile;
} }
} else { } else {
_currentUpload.buf[_currentUpload.buflen++] = 0x0D; _uploadWriteByte(0x0D);
if(_currentUpload.buflen == 1460){
#ifdef DEBUG
DEBUG_OUTPUT.println("Write File: 1460");
#endif
if(_fileUploadHandler) _fileUploadHandler();
_currentUpload.size += _currentUpload.buflen;
_currentUpload.buflen = 0;
}
goto readfile; goto readfile;
} }
break; break;

View File

@ -29,6 +29,8 @@
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE }; enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE };
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END }; enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
#define HTTP_UPLOAD_BUFLEN 2048
typedef struct { typedef struct {
HTTPUploadStatus status; HTTPUploadStatus status;
String filename; String filename;
@ -36,7 +38,7 @@ typedef struct {
String type; String type;
size_t size; size_t size;
size_t buflen; size_t buflen;
uint8_t buf[1460]; uint8_t buf[HTTP_UPLOAD_BUFLEN];
} HTTPUpload; } HTTPUpload;
class ESP8266WebServer class ESP8266WebServer
@ -78,6 +80,7 @@ protected:
static const char* _responseCodeToString(int code); static const char* _responseCodeToString(int code);
static void _appendHeader(String& response, const char* name, const char* value); static void _appendHeader(String& response, const char* name, const char* value);
void _parseForm(WiFiClient& client, String boundary, uint32_t len); void _parseForm(WiFiClient& client, String boundary, uint32_t len);
void _uploadWriteByte(uint8_t b);
struct RequestHandler; struct RequestHandler;
struct RequestArgument { struct RequestArgument {