mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-09-30 00:00:56 +02:00
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:
@@ -85,7 +85,15 @@ void setup() {
|
||||
WiFi.softAP("esp-captive");
|
||||
#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
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
// curl -v -X GET -H "x-remove-me: value" http://192.168.4.1/headers
|
||||
server.on("/headers", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
Serial.printf("Request Headers:\n");
|
||||
@@ -106,6 +114,8 @@ void setup() {
|
||||
request->send(200);
|
||||
});
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
request->send(200, "text/plain", "Hello, world");
|
||||
});
|
||||
|
@@ -195,6 +195,9 @@ class AsyncWebServerRequest {
|
||||
AsyncWebServerResponse* _response;
|
||||
ArDisconnectHandler _onDisconnectfn;
|
||||
|
||||
// response is sent
|
||||
bool _sent = false;
|
||||
|
||||
String _temp;
|
||||
uint8_t _parseState;
|
||||
|
||||
@@ -308,6 +311,7 @@ class AsyncWebServerRequest {
|
||||
void redirect(const String& url) { return redirect(url.c_str()); };
|
||||
|
||||
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 String& contentType, const String& content = emptyString, AwsTemplateProcessor callback = nullptr) { send(beginResponse(code, contentType, content, callback)); }
|
||||
@@ -647,6 +651,7 @@ class AsyncWebServerResponse {
|
||||
AsyncWebServerResponse();
|
||||
virtual ~AsyncWebServerResponse();
|
||||
virtual void setCode(int code);
|
||||
int code() const { return _code; }
|
||||
virtual void setContentLength(size_t len);
|
||||
void setContentType(const String& type) { setContentType(type.c_str()); }
|
||||
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); }
|
||||
virtual bool removeHeader(const char* name);
|
||||
virtual const AsyncWebHeader* getHeader(const char* name) const;
|
||||
const std::list<AsyncWebHeader>& getHeaders() const { return _headers; }
|
||||
|
||||
#ifndef ESP8266
|
||||
[[deprecated("Use instead: _assembleHead(String& buffer, uint8_t version)")]]
|
||||
|
@@ -123,7 +123,6 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
|
||||
}
|
||||
}
|
||||
if (!_isPlainPost) {
|
||||
// check if authenticated before calling the body
|
||||
if (_handler)
|
||||
_handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
|
||||
_parsedLength += len;
|
||||
@@ -139,11 +138,10 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len) {
|
||||
}
|
||||
if (_parsedLength == _contentLength) {
|
||||
_parseState = PARSE_REQ_END;
|
||||
// check if authenticated before calling handleRequest and request auth instead
|
||||
if (_handler)
|
||||
_handler->handleRequest(this);
|
||||
else
|
||||
send(501);
|
||||
if (!_sent)
|
||||
send(501, T_text_plain, "Handler did not handle the request");
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -498,7 +496,6 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last) {
|
||||
_params.emplace_back(_itemName, _itemValue, true);
|
||||
} else {
|
||||
if (_itemSize) {
|
||||
// check if authenticated before calling the upload
|
||||
if (_handler)
|
||||
_handler->handleUpload(this, _itemFilename, _itemSize - _itemBufferIndex, _itemBuffer, _itemBufferIndex, true);
|
||||
_itemBufferIndex = 0;
|
||||
@@ -571,15 +568,14 @@ void AsyncWebServerRequest::_parseLine() {
|
||||
String response(T_HTTP_100_CONT);
|
||||
_client->write(response.c_str(), response.length());
|
||||
}
|
||||
// check handler for authentication
|
||||
if (_contentLength) {
|
||||
_parseState = PARSE_REQ_BODY;
|
||||
} else {
|
||||
_parseState = PARSE_REQ_END;
|
||||
if (_handler)
|
||||
_handler->handleRequest(this);
|
||||
else
|
||||
send(501);
|
||||
if (!_sent)
|
||||
send(501, T_text_plain, "Handler did not handle the request");
|
||||
}
|
||||
} else
|
||||
_parseReqHeader();
|
||||
@@ -717,6 +713,7 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse* response) {
|
||||
if (_response == NULL) {
|
||||
_client->close(true);
|
||||
_onDisconnect();
|
||||
_sent = true;
|
||||
return;
|
||||
}
|
||||
if (!_response->_sourceValid()) {
|
||||
@@ -726,6 +723,7 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse* response) {
|
||||
} else {
|
||||
_client->setRxTimeout(0);
|
||||
_response->_respond(this);
|
||||
_sent = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user