Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 21323aa

Browse files
committed
Breaking: Renamed Middleware classes to not conflict with Arduino Core 3.1.1 which now has Middleware support
1 parent fd7dbdd commit 21323aa

File tree

7 files changed

+65
-65
lines changed

7 files changed

+65
-65
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
2222
- [`AsyncWebSocketMessageBuffer` and `makeBuffer()`](#asyncwebsocketmessagebuffer-and-makebuffer)
2323
- [How to replace a response](#how-to-replace-a-response)
2424
- [How to use Middleware](#how-to-use-middleware)
25-
- [How to use authentication with AuthenticationMiddleware](#how-to-use-authentication-with-authenticationmiddleware)
25+
- [How to use authentication with AsyncAuthenticationMiddleware](#how-to-use-authentication-with-authenticationmiddleware)
2626
- [Migration to Middleware to improve performance and memory usage](#migration-to-middleware-to-improve-performance-and-memory-usage)
2727
- [Original Documentation](#original-documentation)
2828

@@ -270,24 +270,24 @@ AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlew
270270
**Here are the list of available middlewares:**
271271

272272
- `AsyncMiddlewareFunction`: can convert a lambda function (`ArMiddlewareCallback`) to a middleware
273-
- `AuthenticationMiddleware`: to handle basic/digest authentication globally or per handler
274-
- `AuthorizationMiddleware`: to handle authorization globally or per handler
275-
- `CorsMiddleware`: to handle CORS preflight request globally or per handler
276-
- `HeaderFilterMiddleware`: to filter out headers from the request
277-
- `HeaderFreeMiddleware`: to only keep some headers from the request, and remove the others
273+
- `AsyncAuthenticationMiddleware`: to handle basic/digest authentication globally or per handler
274+
- `AsyncAuthorizationMiddleware`: to handle authorization globally or per handler
275+
- `AsyncCorsMiddleware`: to handle CORS preflight request globally or per handler
276+
- `AsyncHeaderFilterMiddleware`: to filter out headers from the request
277+
- `AsyncHeaderFreeMiddleware`: to only keep some headers from the request, and remove the others
278278
- `LoggerMiddleware`: to log requests globally or per handler with the same pattern as curl. Will also record request processing time
279-
- `RateLimitMiddleware`: to limit the number of requests on a windows of time globally or per handler
279+
- `AsyncRateLimitMiddleware`: to limit the number of requests on a windows of time globally or per handler
280280

281-
## How to use authentication with AuthenticationMiddleware
281+
## How to use authentication with AsyncAuthenticationMiddleware
282282

283283
Do not use the `setUsername()` and `setPassword()` methods on the hanlders anymore.
284284
They are deprecated.
285285
These methods were causing a copy of the username and password for each handler, which is not efficient.
286286

287-
Now, you can use the `AuthenticationMiddleware` to handle authentication globally or per handler.
287+
Now, you can use the `AsyncAuthenticationMiddleware` to handle authentication globally or per handler.
288288

289289
```c++
290-
AuthenticationMiddleware authMiddleware;
290+
AsyncAuthenticationMiddleware authMiddleware;
291291

292292
// [...]
293293

@@ -309,10 +309,10 @@ myHandler.addMiddleware(&authMiddleware); // add authentication to a specific ha
309309

310310
## Migration to Middleware to improve performance and memory usage
311311

312-
- `AsyncEventSource.authorizeConnect(...)` => do not use this method anymore: add a common `AuthorizationMiddleware` to the handler or server, and make sure to add it AFTER the `AuthenticationMiddleware` if you use authentication.
313-
- `AsyncWebHandler.setAuthentication(...)` => do not use this method anymore: add a common `AuthenticationMiddleware` to the handler or server
312+
- `AsyncEventSource.authorizeConnect(...)` => do not use this method anymore: add a common `AsyncAuthorizationMiddleware` to the handler or server, and make sure to add it AFTER the `AsyncAuthenticationMiddleware` if you use authentication.
313+
- `AsyncWebHandler.setAuthentication(...)` => do not use this method anymore: add a common `AsyncAuthenticationMiddleware` to the handler or server
314314
- `ArUploadHandlerFunction` and `ArBodyHandlerFunction` => these callbacks receiving body data and upload and not calling anymore the authentication code for performance reasons.
315-
These callbacks can be called multiple times during request parsing, so this is up to the user to now call the `AuthenticationMiddleware.allowed(request)` if needed and ideally when the method is called for the first time.
315+
These callbacks can be called multiple times during request parsing, so this is up to the user to now call the `AsyncAuthenticationMiddleware.allowed(request)` if needed and ideally when the method is called for the first time.
316316
These callbacks are also not triggering the whole middleware chain since they are not part of the request processing workflow (they are not the final handler).
317317

318318

docs/index.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
2222
- [`AsyncWebSocketMessageBuffer` and `makeBuffer()`](#asyncwebsocketmessagebuffer-and-makebuffer)
2323
- [How to replace a response](#how-to-replace-a-response)
2424
- [How to use Middleware](#how-to-use-middleware)
25-
- [How to use authentication with AuthenticationMiddleware](#how-to-use-authentication-with-authenticationmiddleware)
25+
- [How to use authentication with AsyncAuthenticationMiddleware](#how-to-use-authentication-with-authenticationmiddleware)
2626
- [Migration to Middleware to improve performance and memory usage](#migration-to-middleware-to-improve-performance-and-memory-usage)
2727
- [Original Documentation](#original-documentation)
2828

@@ -270,24 +270,24 @@ AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlew
270270
**Here are the list of available middlewares:**
271271

272272
- `AsyncMiddlewareFunction`: can convert a lambda function (`ArMiddlewareCallback`) to a middleware
273-
- `AuthenticationMiddleware`: to handle basic/digest authentication globally or per handler
274-
- `AuthorizationMiddleware`: to handle authorization globally or per handler
275-
- `CorsMiddleware`: to handle CORS preflight request globally or per handler
276-
- `HeaderFilterMiddleware`: to filter out headers from the request
277-
- `HeaderFreeMiddleware`: to only keep some headers from the request, and remove the others
273+
- `AsyncAuthenticationMiddleware`: to handle basic/digest authentication globally or per handler
274+
- `AsyncAuthorizationMiddleware`: to handle authorization globally or per handler
275+
- `AsyncCorsMiddleware`: to handle CORS preflight request globally or per handler
276+
- `AsyncHeaderFilterMiddleware`: to filter out headers from the request
277+
- `AsyncHeaderFreeMiddleware`: to only keep some headers from the request, and remove the others
278278
- `LoggerMiddleware`: to log requests globally or per handler with the same pattern as curl. Will also record request processing time
279-
- `RateLimitMiddleware`: to limit the number of requests on a windows of time globally or per handler
279+
- `AsyncRateLimitMiddleware`: to limit the number of requests on a windows of time globally or per handler
280280

281-
## How to use authentication with AuthenticationMiddleware
281+
## How to use authentication with AsyncAuthenticationMiddleware
282282

283283
Do not use the `setUsername()` and `setPassword()` methods on the hanlders anymore.
284284
They are deprecated.
285285
These methods were causing a copy of the username and password for each handler, which is not efficient.
286286

287-
Now, you can use the `AuthenticationMiddleware` to handle authentication globally or per handler.
287+
Now, you can use the `AsyncAuthenticationMiddleware` to handle authentication globally or per handler.
288288

289289
```c++
290-
AuthenticationMiddleware authMiddleware;
290+
AsyncAuthenticationMiddleware authMiddleware;
291291

292292
// [...]
293293

@@ -309,10 +309,10 @@ myHandler.addMiddleware(&authMiddleware); // add authentication to a specific ha
309309

310310
## Migration to Middleware to improve performance and memory usage
311311

312-
- `AsyncEventSource.authorizeConnect(...)` => do not use this method anymore: add a common `AuthorizationMiddleware` to the handler or server, and make sure to add it AFTER the `AuthenticationMiddleware` if you use authentication.
313-
- `AsyncWebHandler.setAuthentication(...)` => do not use this method anymore: add a common `AuthenticationMiddleware` to the handler or server
312+
- `AsyncEventSource.authorizeConnect(...)` => do not use this method anymore: add a common `AsyncAuthorizationMiddleware` to the handler or server, and make sure to add it AFTER the `AsyncAuthenticationMiddleware` if you use authentication.
313+
- `AsyncWebHandler.setAuthentication(...)` => do not use this method anymore: add a common `AsyncAuthenticationMiddleware` to the handler or server
314314
- `ArUploadHandlerFunction` and `ArBodyHandlerFunction` => these callbacks receiving body data and upload and not calling anymore the authentication code for performance reasons.
315-
These callbacks can be called multiple times during request parsing, so this is up to the user to now call the `AuthenticationMiddleware.allowed(request)` if needed and ideally when the method is called for the first time.
315+
These callbacks can be called multiple times during request parsing, so this is up to the user to now call the `AsyncAuthenticationMiddleware.allowed(request)` if needed and ideally when the method is called for the first time.
316316
These callbacks are also not triggering the whole middleware chain since they are not part of the request processing workflow (they are not the final handler).
317317

318318

examples/SimpleServer/SimpleServer.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,27 @@ AsyncWebSocket ws("/ws");
110110
/////////////////////////////////////////////////////////////////////////////////////////////////////
111111

112112
// log incoming requests
113-
LoggingMiddleware requestLogger;
113+
AsyncLoggingMiddleware requestLogger;
114114

115115
// CORS
116-
CorsMiddleware cors;
116+
AsyncCorsMiddleware cors;
117117

118118
// maximum 5 requests per 10 seconds
119-
RateLimitMiddleware rateLimit;
119+
AsyncRateLimitMiddleware rateLimit;
120120

121121
// filter out specific headers from the incoming request
122-
HeaderFilterMiddleware headerFilter;
122+
AsyncHeaderFilterMiddleware headerFilter;
123123

124124
// remove all headers from the incoming request except the ones provided in the constructor
125-
HeaderFreeMiddleware headerFree;
125+
AsyncHeaderFreeMiddleware headerFree;
126126

127127
// basicAuth
128-
AuthenticationMiddleware basicAuth;
129-
AuthenticationMiddleware basicAuthHash;
128+
AsyncAuthenticationMiddleware basicAuth;
129+
AsyncAuthenticationMiddleware basicAuthHash;
130130

131131
// simple digest authentication
132-
AuthenticationMiddleware digestAuth;
133-
AuthenticationMiddleware digestAuthHash;
132+
AsyncAuthenticationMiddleware digestAuth;
133+
AsyncAuthenticationMiddleware digestAuthHash;
134134

135135
// complex authentication which adds request attributes for the next middlewares and handler
136136
AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlewareNext next) {
@@ -145,7 +145,7 @@ AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlew
145145
request->getResponse()->addHeader("X-Rate-Limit", "200");
146146
});
147147

148-
AuthorizationMiddleware authz([](AsyncWebServerRequest* request) { return request->getAttribute("role") == "staff"; });
148+
AsyncAuthorizationMiddleware authz([](AsyncWebServerRequest* request) { return request->getAttribute("role") == "staff"; });
149149

150150
int wsClients = 0;
151151

src/AsyncEventSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void AsyncEventSourceClient::set_max_inflight_bytes(size_t value) {
332332
/* AsyncEventSource */
333333

334334
void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb) {
335-
AuthorizationMiddleware* m = new AuthorizationMiddleware(401, cb);
335+
AsyncAuthorizationMiddleware* m = new AsyncAuthorizationMiddleware(401, cb);
336336
m->_freeOnRemoval = true;
337337
addMiddleware(m);
338338
}

src/ESPAsyncWebServer.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,12 @@ class AsyncWebServerRequest {
285285
void setHandler(AsyncWebHandler* handler) { _handler = handler; }
286286

287287
#ifndef ESP8266
288-
[[deprecated("All headers are now collected. Use removeHeader(name) or HeaderFreeMiddleware if you really need to free some headers.")]]
288+
[[deprecated("All headers are now collected. Use removeHeader(name) or AsyncHeaderFreeMiddleware if you really need to free some headers.")]]
289289
#endif
290290
void addInterestingHeader(__unused const char* name) {
291291
}
292292
#ifndef ESP8266
293-
[[deprecated("All headers are now collected. Use removeHeader(name) or HeaderFreeMiddleware if you really need to free some headers.")]]
293+
[[deprecated("All headers are now collected. Use removeHeader(name) or AsyncHeaderFreeMiddleware if you really need to free some headers.")]]
294294
#endif
295295
void addInterestingHeader(__unused const String& name) {
296296
}
@@ -557,8 +557,8 @@ class AsyncMiddlewareChain {
557557
std::list<AsyncMiddleware*> _middlewares;
558558
};
559559

560-
// AuthenticationMiddleware is a middleware that checks if the request is authenticated
561-
class AuthenticationMiddleware : public AsyncMiddleware {
560+
// AsyncAuthenticationMiddleware is a middleware that checks if the request is authenticated
561+
class AsyncAuthenticationMiddleware : public AsyncMiddleware {
562562
public:
563563
void setUsername(const char* username);
564564
void setPassword(const char* password);
@@ -601,11 +601,11 @@ class AuthenticationMiddleware : public AsyncMiddleware {
601601
};
602602

603603
using ArAuthorizeFunction = std::function<bool(AsyncWebServerRequest* request)>;
604-
// AuthorizationMiddleware is a middleware that checks if the request is authorized
605-
class AuthorizationMiddleware : public AsyncMiddleware {
604+
// AsyncAuthorizationMiddleware is a middleware that checks if the request is authorized
605+
class AsyncAuthorizationMiddleware : public AsyncMiddleware {
606606
public:
607-
AuthorizationMiddleware(ArAuthorizeFunction authorizeConnectHandler) : _code(403), _authz(authorizeConnectHandler) {}
608-
AuthorizationMiddleware(int code, ArAuthorizeFunction authorizeConnectHandler) : _code(code), _authz(authorizeConnectHandler) {}
607+
AsyncAuthorizationMiddleware(ArAuthorizeFunction authorizeConnectHandler) : _code(403), _authz(authorizeConnectHandler) {}
608+
AsyncAuthorizationMiddleware(int code, ArAuthorizeFunction authorizeConnectHandler) : _code(code), _authz(authorizeConnectHandler) {}
609609

610610
void run(AsyncWebServerRequest* request, ArMiddlewareNext next) { return _authz && !_authz(request) ? request->send(_code) : next(); }
611611

@@ -615,7 +615,7 @@ class AuthorizationMiddleware : public AsyncMiddleware {
615615
};
616616

617617
// remove all headers from the incoming request except the ones provided in the constructor
618-
class HeaderFreeMiddleware : public AsyncMiddleware {
618+
class AsyncHeaderFreeMiddleware : public AsyncMiddleware {
619619
public:
620620
void keep(const char* name) { _toKeep.push_back(name); }
621621
void unKeep(const char* name) { _toKeep.erase(std::remove(_toKeep.begin(), _toKeep.end(), name), _toKeep.end()); }
@@ -627,7 +627,7 @@ class HeaderFreeMiddleware : public AsyncMiddleware {
627627
};
628628

629629
// filter out specific headers from the incoming request
630-
class HeaderFilterMiddleware : public AsyncMiddleware {
630+
class AsyncHeaderFilterMiddleware : public AsyncMiddleware {
631631
public:
632632
void filter(const char* name) { _toRemove.push_back(name); }
633633
void unFilter(const char* name) { _toRemove.erase(std::remove(_toRemove.begin(), _toRemove.end(), name), _toRemove.end()); }
@@ -639,7 +639,7 @@ class HeaderFilterMiddleware : public AsyncMiddleware {
639639
};
640640

641641
// curl-like logging of incoming requests
642-
class LoggingMiddleware : public AsyncMiddleware {
642+
class AsyncLoggingMiddleware : public AsyncMiddleware {
643643
public:
644644
void setOutput(Print& output) { _out = &output; }
645645
void setEnabled(bool enabled) { _enabled = enabled; }
@@ -653,7 +653,7 @@ class LoggingMiddleware : public AsyncMiddleware {
653653
};
654654

655655
// CORS Middleware
656-
class CorsMiddleware : public AsyncMiddleware {
656+
class AsyncCorsMiddleware : public AsyncMiddleware {
657657
public:
658658
void setOrigin(const char* origin) { _origin = origin; }
659659
void setMethods(const char* methods) { _methods = methods; }
@@ -674,7 +674,7 @@ class CorsMiddleware : public AsyncMiddleware {
674674
};
675675

676676
// Rate limit Middleware
677-
class RateLimitMiddleware : public AsyncMiddleware {
677+
class AsyncRateLimitMiddleware : public AsyncMiddleware {
678678
public:
679679
void setMaxRequests(size_t maxRequests) { _maxRequests = maxRequests; }
680680
void setWindowSize(uint32_t seconds) { _windowSizeMillis = seconds * 1000; }
@@ -727,7 +727,7 @@ class AsyncWebRewrite {
727727
class AsyncWebHandler : public AsyncMiddlewareChain {
728728
protected:
729729
ArRequestFilterFunction _filter = nullptr;
730-
AuthenticationMiddleware* _authMiddleware = nullptr;
730+
AsyncAuthenticationMiddleware* _authMiddleware = nullptr;
731731

732732
public:
733733
AsyncWebHandler() {}

0 commit comments

Comments
 (0)