Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 46eebdd

Browse files
tetiousme-no-dev
authored andcommitted
Allow "default" headers to be defined for all responses. (me-no-dev#230)
* Add DefaultHeaders singleton. * Add documentation for DefaultHeaders.
1 parent b681dbc commit 46eebdd

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ To use this library you might need to have the latest git versions of [ESP32](ht
7777
- [Setting up the server](#setting-up-the-server)
7878
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
7979
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
80-
80+
- [Adding default headers to all responses](#adding-default-headers)
8181
## Why should you care
8282
- Using asynchronous network means that you can handle more than one connection at the same time
8383
- You are called once the request is ready and parsed
@@ -1339,3 +1339,30 @@ Example of OTA code
13391339
});
13401340
13411341
```
1342+
1343+
### Adding Default Headers
1344+
1345+
In some cases, such as when working with CORS, or with some sort of custom authentication system,
1346+
you might need to define a header that should get added to all responses (including static, websocket and EventSource).
1347+
The DefaultHeaders singleton allows you to do this.
1348+
1349+
Example:
1350+
1351+
```arduino
1352+
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
1353+
webServer.begin();
1354+
```
1355+
1356+
*NOTE*: You will still need to respond to the OPTIONS method for CORS pre-flight in most cases. (unless you are only using GET)
1357+
1358+
This is one option:
1359+
1360+
```arduino
1361+
webServer.onNotFound([](AsyncWebServerRequest *request) {
1362+
if (request->method() == HTTP_OPTIONS) {
1363+
request->send(200);
1364+
} else {
1365+
request->send(404);
1366+
}
1367+
});
1368+
```

src/ESPAsyncWebServer.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,31 @@ class AsyncWebServer {
414414
void _rewriteRequest(AsyncWebServerRequest *request);
415415
};
416416

417+
class DefaultHeaders {
418+
using headers_t = LinkedList<AsyncWebHeader *>;
419+
headers_t _headers;
420+
421+
DefaultHeaders()
422+
:_headers(headers_t([](AsyncWebHeader *h){ delete h; }))
423+
{}
424+
public:
425+
using ConstIterator = headers_t::ConstIterator;
426+
427+
void addHeader(const String& name, const String& value){
428+
_headers.add(new AsyncWebHeader(name, value));
429+
}
430+
431+
ConstIterator begin() const { return _headers.begin(); }
432+
ConstIterator end() const { return _headers.end(); }
433+
434+
DefaultHeaders(DefaultHeaders const &) = delete;
435+
DefaultHeaders &operator=(DefaultHeaders const &) = delete;
436+
static DefaultHeaders &Instance() {
437+
static DefaultHeaders instance;
438+
return instance;
439+
}
440+
};
441+
417442
#include "WebResponseImpl.h"
418443
#include "WebHandlerImpl.h"
419444
#include "AsyncWebSocket.h"

src/WebResponses.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ AsyncWebServerResponse::AsyncWebServerResponse()
9494
, _ackedLength(0)
9595
, _writtenLength(0)
9696
, _state(RESPONSE_SETUP)
97-
{}
97+
{
98+
for(auto header: DefaultHeaders::Instance()) {
99+
_headers.add(new AsyncWebHeader(header->name(), header->value()));
100+
}
101+
}
98102

99103
AsyncWebServerResponse::~AsyncWebServerResponse(){
100104
_headers.free();

0 commit comments

Comments
 (0)