Exposing new methods:

- sent response from request
- response code
- response headers
And fixed missing response when a handler forget to respond (501)
This commit is contained in:
Mathieu Carbou
2024-09-13 23:33:50 +02:00
parent cadd61898b
commit 4b03e071b4
3 changed files with 22 additions and 8 deletions

View File

@@ -85,7 +85,15 @@ void setup() {
WiFi.softAP("esp-captive"); WiFi.softAP("esp-captive");
#endif #endif
// curl -v -X GET http://192.168.4.1/handler-not-sending-response
server.on("/handler-not-sending-response", HTTP_GET, [](AsyncWebServerRequest* request) {
// handler forgot to send a response to the client => 501 Not Implemented
});
///////////////////////////////////////////////////////////////////////
// Request header manipulations // Request header manipulations
///////////////////////////////////////////////////////////////////////
// curl -v -X GET -H "x-remove-me: value" http://192.168.4.1/headers // curl -v -X GET -H "x-remove-me: value" http://192.168.4.1/headers
server.on("/headers", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/headers", HTTP_GET, [](AsyncWebServerRequest* request) {
Serial.printf("Request Headers:\n"); Serial.printf("Request Headers:\n");
@@ -106,6 +114,8 @@ void setup() {
request->send(200); request->send(200);
}); });
///////////////////////////////////////////////////////////////////////
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send(200, "text/plain", "Hello, world"); request->send(200, "text/plain", "Hello, world");
}); });

View File

@@ -195,6 +195,9 @@ class AsyncWebServerRequest {
AsyncWebServerResponse* _response; AsyncWebServerResponse* _response;
ArDisconnectHandler _onDisconnectfn; ArDisconnectHandler _onDisconnectfn;
// response is sent
bool _sent = false;
String _temp; String _temp;
uint8_t _parseState; uint8_t _parseState;
@@ -308,6 +311,7 @@ class AsyncWebServerRequest {
void redirect(const String& url) { return redirect(url.c_str()); }; void redirect(const String& url) { return redirect(url.c_str()); };
void send(AsyncWebServerResponse* response); void send(AsyncWebServerResponse* response);
const AsyncWebServerResponse* sentResponse() const { return _response; }
void send(int code, const char* contentType = asyncsrv::empty, const char* content = asyncsrv::empty, AwsTemplateProcessor callback = nullptr) { send(beginResponse(code, contentType, content, callback)); } void send(int code, const char* contentType = asyncsrv::empty, const char* content = asyncsrv::empty, AwsTemplateProcessor callback = nullptr) { send(beginResponse(code, contentType, content, callback)); }
void send(int code, const String& contentType, const String& content = emptyString, AwsTemplateProcessor callback = nullptr) { send(beginResponse(code, contentType, content, callback)); } void send(int code, const String& contentType, const String& content = emptyString, AwsTemplateProcessor callback = nullptr) { send(beginResponse(code, contentType, content, callback)); }
@@ -647,6 +651,7 @@ class AsyncWebServerResponse {
AsyncWebServerResponse(); AsyncWebServerResponse();
virtual ~AsyncWebServerResponse(); virtual ~AsyncWebServerResponse();
virtual void setCode(int code); virtual void setCode(int code);
int code() const { return _code; }
virtual void setContentLength(size_t len); virtual void setContentLength(size_t len);
void setContentType(const String& type) { setContentType(type.c_str()); } void setContentType(const String& type) { setContentType(type.c_str()); }
virtual void setContentType(const char* type); virtual void setContentType(const char* type);
@@ -656,6 +661,7 @@ class AsyncWebServerResponse {
bool addHeader(const String& name, long value, bool replaceExisting = true) { return addHeader(name.c_str(), value, replaceExisting); } bool addHeader(const String& name, long value, bool replaceExisting = true) { return addHeader(name.c_str(), value, replaceExisting); }
virtual bool removeHeader(const char* name); virtual bool removeHeader(const char* name);
virtual const AsyncWebHeader* getHeader(const char* name) const; virtual const AsyncWebHeader* getHeader(const char* name) const;
const std::list<AsyncWebHeader>& getHeaders() const { return _headers; }
#ifndef ESP8266 #ifndef ESP8266
[[deprecated("Use instead: _assembleHead(String& buffer, uint8_t version)")]] [[deprecated("Use instead: _assembleHead(String& buffer, uint8_t version)")]]

View File

@@ -123,7 +123,6 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
} }
} }
if (!_isPlainPost) { if (!_isPlainPost) {
// check if authenticated before calling the body
if (_handler) if (_handler)
_handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength); _handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
_parsedLength += len; _parsedLength += len;
@@ -139,11 +138,10 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
} }
if (_parsedLength == _contentLength) { if (_parsedLength == _contentLength) {
_parseState = PARSE_REQ_END; _parseState = PARSE_REQ_END;
// check if authenticated before calling handleRequest and request auth instead
if (_handler) if (_handler)
_handler->handleRequest(this); _handler->handleRequest(this);
else if (!_sent)
send(501); send(501, T_text_plain, "Handler did not handle the request");
} }
} }
break; break;
@@ -498,7 +496,6 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last) {
_params.emplace_back(_itemName, _itemValue, true); _params.emplace_back(_itemName, _itemValue, true);
} else { } else {
if (_itemSize) { if (_itemSize) {
// check if authenticated before calling the upload
if (_handler) if (_handler)
_handler->handleUpload(this, _itemFilename, _itemSize - _itemBufferIndex, _itemBuffer, _itemBufferIndex, true); _handler->handleUpload(this, _itemFilename, _itemSize - _itemBufferIndex, _itemBuffer, _itemBufferIndex, true);
_itemBufferIndex = 0; _itemBufferIndex = 0;
@@ -571,15 +568,14 @@ void AsyncWebServerRequest::_parseLine() {
String response(T_HTTP_100_CONT); String response(T_HTTP_100_CONT);
_client->write(response.c_str(), response.length()); _client->write(response.c_str(), response.length());
} }
// check handler for authentication
if (_contentLength) { if (_contentLength) {
_parseState = PARSE_REQ_BODY; _parseState = PARSE_REQ_BODY;
} else { } else {
_parseState = PARSE_REQ_END; _parseState = PARSE_REQ_END;
if (_handler) if (_handler)
_handler->handleRequest(this); _handler->handleRequest(this);
else if (!_sent)
send(501); send(501, T_text_plain, "Handler did not handle the request");
} }
} else } else
_parseReqHeader(); _parseReqHeader();
@@ -717,6 +713,7 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse* response) {
if (_response == NULL) { if (_response == NULL) {
_client->close(true); _client->close(true);
_onDisconnect(); _onDisconnect();
_sent = true;
return; return;
} }
if (!_response->_sourceValid()) { if (!_response->_sourceValid()) {
@@ -726,6 +723,7 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse* response) {
} else { } else {
_client->setRxTimeout(0); _client->setRxTimeout(0);
_response->_respond(this); _response->_respond(this);
_sent = true;
} }
} }