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

Make file uploads using curl fail less often

This commit is contained in:
Ivan Grokhotkov 2015-08-18 23:40:33 +03:00
parent c355f626f2
commit 278c980ed8
2 changed files with 34 additions and 22 deletions

View File

@ -111,7 +111,7 @@ protected:
bool _parseRequest(WiFiClient& client); bool _parseRequest(WiFiClient& client);
void _parseArguments(String data); void _parseArguments(String data);
static const char* _responseCodeToString(int code); static const char* _responseCodeToString(int code);
void _parseForm(WiFiClient& client, String boundary, uint32_t len); bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
void _uploadWriteByte(uint8_t b); void _uploadWriteByte(uint8_t b);
uint8_t _uploadReadByte(WiFiClient& client); uint8_t _uploadReadByte(WiFiClient& client);
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);

View File

@ -24,8 +24,8 @@
#include "WiFiClient.h" #include "WiFiClient.h"
#include "ESP8266WebServer.h" #include "ESP8266WebServer.h"
// #define DEBUG #define DEBUG
#define DEBUG_OUTPUT Serial1 #define DEBUG_OUTPUT Serial
bool ESP8266WebServer::_parseRequest(WiFiClient& client) { bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
// Read the first line of HTTP request // Read the first line of HTTP request
@ -103,6 +103,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
} }
} else if (headerName == "Content-Length"){ } else if (headerName == "Content-Length"){
contentLength = headerValue.toInt(); contentLength = headerValue.toInt();
Serial.printf("Content-Length: %d\r\n", contentLength);
} }
} }
@ -131,7 +132,9 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
} }
_parseArguments(searchStr); _parseArguments(searchStr);
if (isForm){ if (isForm){
_parseForm(client, boundaryStr, contentLength); if (!_parseForm(client, boundaryStr, contentLength)) {
return false;
}
} }
} else { } else {
_parseArguments(searchStr); _parseArguments(searchStr);
@ -242,7 +245,7 @@ uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
return (uint8_t)res; return (uint8_t)res;
} }
void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
#ifdef DEBUG #ifdef DEBUG
DEBUG_OUTPUT.print("Parse Form: Boundary: "); DEBUG_OUTPUT.print("Parse Form: Boundary: ");
@ -251,7 +254,12 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
DEBUG_OUTPUT.println(len); DEBUG_OUTPUT.println(len);
#endif #endif
String line; String line;
line = client.readStringUntil('\r'); int retry = 0;
do {
line = client.readStringUntil('\r');
++retry;
} while (line.length() == 0 && retry < 3);
client.readStringUntil('\n'); client.readStringUntil('\n');
//start reading the form //start reading the form
if (line == ("--"+boundary)){ if (line == ("--"+boundary)){
@ -429,7 +437,11 @@ readfile:
} }
_currentArgCount = iarg; _currentArgCount = iarg;
if (postArgs) delete[] postArgs; if (postArgs) delete[] postArgs;
return true;
} }
#ifdef DEBUG
DEBUG_OUTPUT.print("Error: line: ");
DEBUG_OUTPUT.println(line);
#endif
return false;
} }