Skip to content

Commit 172083f

Browse files
committed
Handle large octet-stream
1 parent 654aead commit 172083f

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

libraries/WebServer/src/Parsing.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ bool WebServer::_parseRequest(NetworkClient& 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(NetworkClient& 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(NetworkClient& 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) {

libraries/WebServer/src/WebServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ void WebServer::handleClient() {
438438
_currentClient = NetworkClient();
439439
_currentStatus = HC_NONE;
440440
_currentUpload.reset();
441+
_currentRaw.reset();
441442
}
442443

443444
if (callYield) {

libraries/WebServer/src/WebServer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
3535
UPLOAD_FILE_ABORTED };
36+
enum HTTPRawStatus { RAW_START, RAW_WRITE, RAW_END, RAW_ABORTED };
3637
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
3738
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH, OTHER_AUTH };
3839

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

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

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

6882
namespace fs {
@@ -128,6 +142,7 @@ class WebServer
128142
HTTPMethod method() { return _currentMethod; }
129143
virtual NetworkClient & client() { return _currentClient; }
130144
HTTPUpload& upload() { return *_currentUpload; }
145+
HTTPRaw& raw() { return *_currentRaw; }
131146

132147
String pathArg(unsigned int i); // get request path argument by number
133148
String arg(String name); // get request argument value by name
@@ -232,6 +247,7 @@ class WebServer
232247
RequestArgument* _postArgs;
233248

234249
std::unique_ptr<HTTPUpload> _currentUpload;
250+
std::unique_ptr<HTTPRaw> _currentRaw;
235251

236252
int _headerKeysCount;
237253
RequestArgument* _currentHeaders;

libraries/WebServer/src/detail/RequestHandler.h

Lines changed: 2 additions & 0 deletions
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; }

libraries/WebServer/src/detail/RequestHandlersImpl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class FunctionRequestHandler : public RequestHandler {
3838

3939
return true;
4040
}
41+
bool canRaw(String requestUri) override {
42+
if (!_ufn || _method == HTTP_GET)
43+
return false;
44+
45+
return true;
46+
}
4147

4248
bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) override {
4349
(void) server;
@@ -55,6 +61,13 @@ class FunctionRequestHandler : public RequestHandler {
5561
_ufn();
5662
}
5763

64+
void raw(WebServer& server, String requestUri, HTTPRaw& raw) override {
65+
(void)server;
66+
(void)raw;
67+
if (canRaw(requestUri))
68+
_ufn();
69+
}
70+
5871
protected:
5972
WebServer::THandlerFunction _fn;
6073
WebServer::THandlerFunction _ufn;

0 commit comments

Comments
 (0)