Skip to content

Commit f105f52

Browse files
Merge pull request #415 from ESP32Async/cleanup
Code cleanup with regard to PR 414, operator overloads and default constructor for WebRequestMethodComposite
2 parents 40f886c + 06ec7e4 commit f105f52

7 files changed

Lines changed: 41 additions & 28 deletions

File tree

examples/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ void setup() {
101101

102102
server.begin();
103103

104-
WebRequestMethodComposite composite1 = WebRequestMethod::HTTP_GET | WebRequestMethod::HTTP_POST;
105-
WebRequestMethodComposite composite2 = WebRequestMethod::HTTP_GET | WebRequestMethod::HTTP_POST;
106-
WebRequestMethodComposite composite3 = WebRequestMethod::HTTP_HEAD | WebRequestMethod::HTTP_OPTIONS;
104+
WebRequestMethodComposite composite1 = AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST;
105+
WebRequestMethodComposite composite2 = AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST;
106+
WebRequestMethodComposite composite3 = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS;
107107
WebRequestMethodComposite composite4 = composite3;
108-
WebRequestMethodComposite composite5 = WebRequestMethod::HTTP_GET;
108+
WebRequestMethodComposite composite5 = AsyncWebRequestMethod::HTTP_GET;
109109

110-
assert(composite1.matches(WebRequestMethod::HTTP_GET));
111-
assert(composite1.matches(WebRequestMethod::HTTP_POST));
112-
assert(!composite1.matches(WebRequestMethod::HTTP_HEAD));
113-
assert(!composite3.matches(WebRequestMethod::HTTP_GET));
110+
assert(composite1.matches(AsyncWebRequestMethod::HTTP_GET));
111+
assert(composite1.matches(AsyncWebRequestMethod::HTTP_POST));
112+
assert(!composite1.matches(AsyncWebRequestMethod::HTTP_HEAD));
113+
assert(!composite3.matches(AsyncWebRequestMethod::HTTP_GET));
114114

115115
assert(composite1 == composite2);
116116
assert(composite3 == composite4);

examples/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ void setup() {
104104

105105
server.begin();
106106

107-
WebRequestMethodComposite composite1 = WebRequestMethod::HTTP_GET | WebRequestMethod::HTTP_POST;
108-
WebRequestMethodComposite composite2 = WebRequestMethod::HTTP_GET | WebRequestMethod::HTTP_POST;
109-
WebRequestMethodComposite composite3 = WebRequestMethod::HTTP_HEAD | WebRequestMethod::HTTP_OPTIONS;
107+
WebRequestMethodComposite composite1 = AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST;
108+
WebRequestMethodComposite composite2 = AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST;
109+
WebRequestMethodComposite composite3 = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS;
110110
WebRequestMethodComposite composite4 = composite3;
111-
WebRequestMethodComposite composite5 = WebRequestMethod::HTTP_GET;
111+
WebRequestMethodComposite composite5 = AsyncWebRequestMethod::HTTP_GET;
112112

113-
assert(composite1.matches(WebRequestMethod::HTTP_GET));
114-
assert(composite1.matches(WebRequestMethod::HTTP_POST));
115-
assert(!composite1.matches(WebRequestMethod::HTTP_HEAD));
116-
assert(!composite3.matches(WebRequestMethod::HTTP_GET));
113+
assert(composite1.matches(AsyncWebRequestMethod::HTTP_GET));
114+
assert(composite1.matches(AsyncWebRequestMethod::HTTP_POST));
115+
assert(!composite1.matches(AsyncWebRequestMethod::HTTP_HEAD));
116+
assert(!composite3.matches(AsyncWebRequestMethod::HTTP_GET));
117117

118118
assert(composite1 == composite2);
119119
assert(composite3 == composite4);

src/AsyncEventSource.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,27 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient *client) {
368368
if (!client) {
369369
return;
370370
}
371+
372+
if (_connectcb) {
373+
_connectcb(client);
374+
}
375+
371376
#ifdef ESP32
372377
std::lock_guard<std::recursive_mutex> lock(_client_queue_lock);
373378
#endif
379+
374380
_clients.emplace_back(client);
375-
if (_connectcb) {
376-
_connectcb(client);
377-
}
378381

379382
_adjust_inflight_window();
380383
}
381384

382385
void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient *client) {
383-
#ifdef ESP32
384-
std::lock_guard<std::recursive_mutex> lock(_client_queue_lock);
385-
#endif
386386
if (_disconnectcb) {
387387
_disconnectcb(client);
388388
}
389+
#ifdef ESP32
390+
std::lock_guard<std::recursive_mutex> lock(_client_queue_lock);
391+
#endif
389392
for (auto i = _clients.begin(); i != _clients.end(); ++i) {
390393
if (i->get() == client) {
391394
_clients.erase(i);

src/AsyncJson.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <ESPAsyncWebServer.h>
77
#include "ChunkPrint.h"
88

9+
#include <utility>
10+
911
#if ASYNC_JSON_SUPPORT == 1
1012

1113
#if ARDUINOJSON_VERSION_MAJOR == 6
@@ -104,7 +106,7 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
104106
#endif
105107

106108
void setMethod(WebRequestMethodComposite method) {
107-
_method = method;
109+
_method = std::move(method);
108110
}
109111
void setMaxContentLength(int maxContentLength) {
110112
_maxContentLength = maxContentLength;

src/ESPAsyncWebServer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,26 @@ class WebRequestMethodComposite {
154154
constexpr WebRequestMethodComposite(uint32_t m) : mask(m){};
155155

156156
public:
157+
// Default constructor: by default, matches nothing
158+
constexpr WebRequestMethodComposite() : mask(0){};
159+
157160
// Constructor: allows implicit conversion from WebRequestMethod
158161
constexpr WebRequestMethodComposite(WebRequestMethod m) : mask(static_cast<uint32_t>(m)){};
159162

160163
// Combine composites
161-
constexpr inline WebRequestMethodComposite operator|(WebRequestMethodComposite r) const {
164+
constexpr inline WebRequestMethodComposite operator|(const WebRequestMethodComposite &r) const {
162165
return WebRequestMethodComposite(mask | r.mask);
163166
};
164167

165168
// == operator for composite
166-
constexpr inline bool operator==(WebRequestMethodComposite r) const {
169+
constexpr inline bool operator==(const WebRequestMethodComposite &r) const {
167170
return mask == r.mask;
168171
};
169172

173+
constexpr inline bool operator!=(const WebRequestMethodComposite &r) const {
174+
return mask != r.mask;
175+
};
176+
170177
// Check for a match
171178
constexpr inline bool matches(WebRequestMethod m) const {
172179
return mask & static_cast<uint32_t>(m);

src/WebHandlerImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <time.h>
88

99
#include <string>
10+
#include <utility>
1011

1112
class AsyncStaticWebHandler : public AsyncWebHandler {
1213
using File = fs::File;
@@ -65,7 +66,7 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {
6566
AsyncCallbackWebHandler() : _uri(), _method(AsyncWebRequestMethod::HTTP_ALL), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
6667
void setUri(AsyncURIMatcher uri);
6768
void setMethod(WebRequestMethodComposite method) {
68-
_method = method;
69+
_method = std::move(method);
6970
}
7071
void onRequest(ArRequestHandlerFunction fn) {
7172
_onRequest = fn;

src/WebServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ AsyncCallbackWebHandler &AsyncWebServer::on(
158158
) {
159159
AsyncCallbackWebHandler *handler = new AsyncCallbackWebHandler();
160160
handler->setUri(std::move(uri));
161-
handler->setMethod(method);
161+
handler->setMethod(std::move(method));
162162
handler->onRequest(onRequest);
163163
handler->onUpload(onUpload);
164164
handler->onBody(onBody);
@@ -169,7 +169,7 @@ AsyncCallbackWebHandler &AsyncWebServer::on(
169169
#if ASYNC_JSON_SUPPORT == 1
170170
AsyncCallbackJsonWebHandler &AsyncWebServer::on(AsyncURIMatcher uri, WebRequestMethodComposite method, ArJsonRequestHandlerFunction onBody) {
171171
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler(std::move(uri), onBody);
172-
handler->setMethod(method);
172+
handler->setMethod(std::move(method));
173173
addHandler(handler);
174174
return *handler;
175175
}

0 commit comments

Comments
 (0)