Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AsyncJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "AsyncJson.h"
#include "AsyncWebServerLogging.h"
using namespace AsyncWebRequestMethod;

#include <utility>

Expand Down
37 changes: 24 additions & 13 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
#include <RPAsyncTCP.h>
#include <HTTP_Method.h>
#include <http_parser.h>
#else
#error Platform not supported
#endif
Expand Down Expand Up @@ -80,11 +78,10 @@ class AsyncCallbackWebHandler;
class AsyncResponseStream;
class AsyncMiddlewareChain;

#if defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
typedef enum http_method WebRequestMethod;
#else
#ifndef WEBSERVER_H
typedef enum {
// Namespace for web request method defines
namespace AsyncWebRequestMethod {
// The long name here is because we sometimes include this in the global namespace
enum AsyncWebRequestMethodType {
HTTP_GET = 0b0000000000000001,
HTTP_POST = 0b0000000000000010,
HTTP_DELETE = 0b0000000000000100,
Expand All @@ -101,8 +98,23 @@ typedef enum {
HTTP_COPY = 0b0010000000000000,
HTTP_RESERVED = 0b0100000000000000,
HTTP_ANY = 0b0111111111111111,
} WebRequestMethod;
#endif
};
}; // namespace AsyncWebRequestMethod

typedef AsyncWebRequestMethod::AsyncWebRequestMethodType WebRequestMethod;
typedef uint16_t WebRequestMethodComposite;

// Type-safe helper functions for composite methods
extern inline WebRequestMethodComposite operator|(WebRequestMethodComposite l, WebRequestMethod r) {
return l | static_cast<WebRequestMethodComposite>(r);
};
extern inline WebRequestMethodComposite operator|(WebRequestMethod l, WebRequestMethod r) {
return static_cast<WebRequestMethodComposite>(l) | r;
};

#if !defined(ASYNCWEBSERVER_NO_GLOBAL_HTTP_METHODS)
// Import the method enum values to the global namespace
using namespace AsyncWebRequestMethod;
#endif

#ifndef HAVE_FS_FILE_OPEN_MODE
Expand All @@ -122,7 +134,6 @@ class FileOpenMode {
#define RESPONSE_TRY_AGAIN 0xFFFFFFFF
#define RESPONSE_STREAM_BUFFER_SIZE 1460

typedef uint16_t WebRequestMethodComposite;
typedef std::function<void(void)> ArDisconnectHandler;

/*
Expand Down Expand Up @@ -372,10 +383,10 @@ class AsyncWebServerRequest {
bool isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED)
const;
bool isWebSocketUpgrade() const {
return _method == HTTP_GET && isExpectedRequestedConnType(RCT_WS);
return _method == AsyncWebRequestMethod::HTTP_GET && isExpectedRequestedConnType(RCT_WS);
}
bool isSSE() const {
return _method == HTTP_GET && isExpectedRequestedConnType(RCT_EVENT);
return _method == AsyncWebRequestMethod::HTTP_GET && isExpectedRequestedConnType(RCT_EVENT);
}
bool isHTTP() const {
return isExpectedRequestedConnType(RCT_DEFAULT, RCT_HTTP);
Expand Down Expand Up @@ -1545,7 +1556,7 @@ class AsyncWebServer : public AsyncMiddlewareChain {
bool removeHandler(AsyncWebHandler *handler);

AsyncCallbackWebHandler &on(AsyncURIMatcher uri, ArRequestHandlerFunction onRequest) {
return on(std::move(uri), HTTP_ANY, onRequest);
return on(std::move(uri), AsyncWebRequestMethod::HTTP_ANY, onRequest);
}
AsyncCallbackWebHandler &on(
AsyncURIMatcher uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload = nullptr,
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void AsyncCorsMiddleware::run(AsyncWebServerRequest *request, ArMiddlewareNext n
// Origin header ? => CORS handling
if (request->hasHeader(asyncsrv::T_CORS_O)) {
// check if this is a preflight request => handle it and return
if (request->method() == HTTP_OPTIONS) {
if (request->method() == AsyncWebRequestMethod::HTTP_OPTIONS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why src/WebRequest.cpp and src/AsyncJson.cpp are updated to use the namespace while this cpp prefixes the constant with the namespace ? Wouldn't it be easier to just do that in all cpp files requiring these enums for consistency ?

// Bring in HTTP namespace
using namespace AsyncWebRequestMethod;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An excellent question! Best practices for C++ are to use fully qualified symbols where possible to avoid any possible risk of collisions should platform libraries change out from under you -- or at worst, apply using directives to individual functions. I got lazy. Thanks for calling me out. :)

AsyncWebServerResponse *response = request->beginResponse(200);
addCORSHeaders(request, response);
request->send(response);
Expand Down
2 changes: 1 addition & 1 deletion src/WebHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {
bool _isRegex;

public:
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
AsyncCallbackWebHandler() : _uri(), _method(AsyncWebRequestMethod::HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
void setUri(AsyncURIMatcher uri);
void setMethod(WebRequestMethodComposite method) {
_method = method;
Expand Down
2 changes: 1 addition & 1 deletion src/WebHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ AsyncStaticWebHandler &AsyncStaticWebHandler::setLastModified() {
}

bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request) const {
return request->isHTTP() && request->method() == HTTP_GET && request->url().startsWith(_uri) && _getFile(request);
return request->isHTTP() && request->method() == AsyncWebRequestMethod::HTTP_GET && request->url().startsWith(_uri) && _getFile(request);
}

bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest *request) const {
Expand Down
3 changes: 3 additions & 0 deletions src/WebRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include "./literals.h"

// Bring in HTTP namespace
using namespace AsyncWebRequestMethod;

static inline bool isParamChar(char c) {
return ((c) && ((c) != '{') && ((c) != '[') && ((c) != '&') && ((c) != '='));
}
Expand Down