Skip to content

Commit de75193

Browse files
committed
Handle large octet-stream
1 parent 9da60f4 commit de75193

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

Diff for: libraries/WebServer/src/Parsing.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
136136
String headerName;
137137
String headerValue;
138138
bool isForm = false;
139+
bool isRaw = false;
139140
bool isEncoded = false;
140141
//parse headers
141142
while(1){
@@ -158,6 +159,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
158159
using namespace mime;
159160
if (headerValue.startsWith(FPSTR(mimeTable[txt].mimeType))){
160161
isForm = false;
162+
} else if (headerValue.startsWith(FPSTR(mimeTable[none].mimeType))){
163+
isRaw = true;
161164
} else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){
162165
isForm = false;
163166
isEncoded = true;
@@ -173,7 +176,30 @@ bool WebServer::_parseRequest(WiFiClient& client) {
173176
}
174177
}
175178

176-
if (!isForm){
179+
if (isRaw && _currentHandler && _currentHandler->canRaw(_currentUri)){
180+
log_v("Parse raw");
181+
_currentRaw.reset(new HTTPRaw());
182+
_currentRaw->status = RAW_START;
183+
_currentRaw->totalSize = 0;
184+
_currentRaw->currentSize = 0;
185+
log_v("Start Raw");
186+
_currentHandler->raw(*this, _currentUri, *_currentRaw);
187+
_currentRaw->status = RAW_WRITE;
188+
189+
while (_currentRaw->totalSize < _clientContentLength) {
190+
_currentRaw->currentSize = client.readBytes(_currentRaw->buf, HTTP_RAW_BUFLEN);
191+
_currentRaw->totalSize += _currentRaw->currentSize;
192+
if (_currentRaw->currentSize == 0) {
193+
_currentRaw->status = RAW_ABORTED;
194+
_currentHandler->raw(*this, _currentUri, *_currentRaw);
195+
return false;
196+
}
197+
_currentHandler->raw(*this, _currentUri, *_currentRaw);
198+
}
199+
_currentRaw->status = RAW_END;
200+
_currentHandler->raw(*this, _currentUri, *_currentRaw);
201+
log_v("Finish Raw");
202+
} else if (!isForm) {
177203
size_t plainLength;
178204
char* plainBuf = readBytesWithTimeout(client, _clientContentLength, plainLength, HTTP_MAX_POST_WAIT);
179205
if (plainLength < _clientContentLength) {

Diff for: libraries/WebServer/src/WebServer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ void WebServer::handleClient() {
333333
_currentClient = WiFiClient();
334334
_currentStatus = HC_NONE;
335335
_currentUpload.reset();
336+
_currentRaw.reset();
336337
}
337338

338339
if (callYield) {

Diff for: libraries/WebServer/src/WebServer.h

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
3434
UPLOAD_FILE_ABORTED };
35+
enum HTTPRawStatus { RAW_START, RAW_WRITE, RAW_END, RAW_ABORTED };
3536
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
3637
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
3738

@@ -41,6 +42,10 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
4142
#define HTTP_UPLOAD_BUFLEN 1436
4243
#endif
4344

45+
#ifndef HTTP_RAW_BUFLEN
46+
#define HTTP_RAW_BUFLEN 1436
47+
#endif
48+
4449
#define HTTP_MAX_DATA_WAIT 5000 //ms to wait for the client to send the request
4550
#define HTTP_MAX_POST_WAIT 5000 //ms to wait for POST data to arrive
4651
#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
@@ -61,6 +66,15 @@ typedef struct {
6166
uint8_t buf[HTTP_UPLOAD_BUFLEN];
6267
} HTTPUpload;
6368

69+
typedef struct
70+
{
71+
HTTPRawStatus status;
72+
size_t totalSize; // content size
73+
size_t currentSize; // size of data currently in buf
74+
uint8_t buf[HTTP_UPLOAD_BUFLEN];
75+
void *data; // additional data
76+
} HTTPRaw;
77+
6478
#include "detail/RequestHandler.h"
6579

6680
namespace fs {
@@ -97,6 +111,7 @@ class WebServer
97111
HTTPMethod method() { return _currentMethod; }
98112
virtual WiFiClient client() { return _currentClient; }
99113
HTTPUpload& upload() { return *_currentUpload; }
114+
HTTPRaw& raw() { return *_currentRaw; }
100115

101116
String pathArg(unsigned int i); // get request path argument by number
102117
String arg(String name); // get request argument value by name
@@ -196,6 +211,7 @@ class WebServer
196211
RequestArgument* _postArgs;
197212

198213
std::unique_ptr<HTTPUpload> _currentUpload;
214+
std::unique_ptr<HTTPRaw> _currentRaw;
199215

200216
int _headerKeysCount;
201217
RequestArgument* _currentHeaders;

Diff for: libraries/WebServer/src/detail/RequestHandler.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ class RequestHandler {
99
virtual ~RequestHandler() { }
1010
virtual bool canHandle(HTTPMethod method, String uri) { (void) method; (void) uri; return false; }
1111
virtual bool canUpload(String uri) { (void) uri; return false; }
12+
virtual bool canRaw(String uri) { (void) uri; return false; }
1213
virtual bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) { (void) server; (void) requestMethod; (void) requestUri; return false; }
1314
virtual void upload(WebServer& server, String requestUri, HTTPUpload& upload) { (void) server; (void) requestUri; (void) upload; }
15+
virtual void raw(WebServer& server, String requestUri, HTTPRaw& raw) { (void) server; (void) requestUri; (void) raw; }
1416

1517
RequestHandler* next() { return _next; }
1618
void next(RequestHandler* r) { _next = r; }

Diff for: libraries/WebServer/src/detail/RequestHandlersImpl.h

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class FunctionRequestHandler : public RequestHandler {
3636

3737
return true;
3838
}
39+
bool canRaw(String requestUri) override {
40+
if (!_ufn || _method == HTTP_GET)
41+
return false;
42+
43+
return true;
44+
}
3945

4046
bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) override {
4147
(void) server;
@@ -53,6 +59,13 @@ class FunctionRequestHandler : public RequestHandler {
5359
_ufn();
5460
}
5561

62+
void raw(WebServer& server, String requestUri, HTTPRaw& raw) override {
63+
(void)server;
64+
(void)raw;
65+
if (canRaw(requestUri))
66+
_ufn();
67+
}
68+
5669
protected:
5770
WebServer::THandlerFunction _fn;
5871
WebServer::THandlerFunction _ufn;

0 commit comments

Comments
 (0)