1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +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

@ -1,8 +1,8 @@
/* /*
Parsing.cpp - HTTP request parsing. Parsing.cpp - HTTP request parsing.
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -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
@ -43,7 +43,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
#endif #endif
return false; return false;
} }
String methodStr = req.substring(0, addr_start); String methodStr = req.substring(0, addr_start);
String url = req.substring(addr_start + 1, addr_end); String url = req.substring(addr_start + 1, addr_end);
String searchStr = ""; String searchStr = "";
@ -53,7 +53,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
url = url.substring(0, hasSearch); url = url.substring(0, hasSearch);
} }
_currentUri = url; _currentUri = url;
HTTPMethod method = HTTP_GET; HTTPMethod method = HTTP_GET;
if (methodStr == "POST") { if (methodStr == "POST") {
method = HTTP_POST; method = HTTP_POST;
@ -65,7 +65,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
method = HTTP_PATCH; method = HTTP_PATCH;
} }
_currentMethod = method; _currentMethod = method;
#ifdef DEBUG #ifdef DEBUG
DEBUG_OUTPUT.print("method: "); DEBUG_OUTPUT.print("method: ");
DEBUG_OUTPUT.print(methodStr); DEBUG_OUTPUT.print(methodStr);
@ -103,9 +103,10 @@ 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);
} }
} }
if (!isForm){ if (!isForm){
if (searchStr != "") searchStr += '&'; if (searchStr != "") searchStr += '&';
//some clients send headers first and data after (like we do) //some clients send headers first and data after (like we do)
@ -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,8 +245,8 @@ 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: ");
DEBUG_OUTPUT.print(boundary); DEBUG_OUTPUT.print(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)){
@ -263,7 +271,7 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
String argType; String argType;
String argFilename; String argFilename;
bool argIsFile = false; bool argIsFile = false;
line = client.readStringUntil('\r'); line = client.readStringUntil('\r');
client.readStringUntil('\n'); client.readStringUntil('\n');
if (line.startsWith("Content-Disposition")){ if (line.startsWith("Content-Disposition")){
@ -314,11 +322,11 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
DEBUG_OUTPUT.println(argValue); DEBUG_OUTPUT.println(argValue);
DEBUG_OUTPUT.println(); DEBUG_OUTPUT.println();
#endif #endif
RequestArgument& arg = postArgs[postArgsLen++]; RequestArgument& arg = postArgs[postArgsLen++];
arg.key = argName; arg.key = argName;
arg.value = argValue; arg.value = argValue;
if (line == ("--"+boundary+"--")){ if (line == ("--"+boundary+"--")){
#ifdef DEBUG #ifdef DEBUG
DEBUG_OUTPUT.println("Done Parsing POST"); DEBUG_OUTPUT.println("Done Parsing POST");
@ -346,7 +354,7 @@ readfile:
_uploadWriteByte(argByte); _uploadWriteByte(argByte);
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
} }
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
if (argByte == 0x0A){ if (argByte == 0x0A){
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
@ -365,10 +373,10 @@ readfile:
goto readfile; goto readfile;
} }
} }
uint8_t endBuf[boundary.length()]; uint8_t endBuf[boundary.length()];
client.readBytes(endBuf, boundary.length()); client.readBytes(endBuf, boundary.length());
if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if (strstr((const char*)endBuf, boundary.c_str()) != NULL){
if (_fileUploadHandler) _fileUploadHandler(); if (_fileUploadHandler) _fileUploadHandler();
_currentUpload.totalSize += _currentUpload.currentSize; _currentUpload.totalSize += _currentUpload.currentSize;
@ -412,7 +420,7 @@ readfile:
} }
} }
} }
int iarg; int iarg;
int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount; int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount;
for (iarg = 0; iarg < totalArgs; iarg++){ for (iarg = 0; iarg < totalArgs; iarg++){
@ -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;
} }