Skip to content

Commit 5842ceb

Browse files
committed
Removed std::optional in favor of operator bool()
1 parent d7dd21b commit 5842ceb

3 files changed

Lines changed: 39 additions & 34 deletions

File tree

src/AsyncWebHeader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
#include <ESPAsyncWebServer.h>
55

6-
std::optional<AsyncWebHeader> AsyncWebHeader::parse(const char *data) {
6+
const AsyncWebHeader AsyncWebHeader::parse(const char *data) {
77
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers
88
// In HTTP/1.X, a header is a case-insensitive name followed by a colon, then optional whitespace which will be ignored, and finally by its value
99
if (!data) {
10-
return std::nullopt; // nullptr
10+
return AsyncWebHeader(); // nullptr
1111
}
1212
if (data[0] == '\0') {
13-
return std::nullopt; // empty string
13+
return AsyncWebHeader(); // empty string
1414
}
1515
if (strchr(data, '\n') || strchr(data, '\r')) {
16-
return std::nullopt; // Invalid header format
16+
return AsyncWebHeader(); // Invalid header format
1717
}
1818
char *colon = strchr(data, ':');
1919
if (!colon) {
20-
return std::nullopt; // separator not found
20+
return AsyncWebHeader(); // separator not found
2121
}
2222
if (colon == data) {
23-
return std::nullopt; // Header name cannot be empty
23+
return AsyncWebHeader(); // Header name cannot be empty
2424
}
2525
char *startOfValue = colon + 1; // Skip the colon
2626
// skip one optional whitespace after the colon

src/ESPAsyncWebServer.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <deque>
1313
#include <functional>
1414
#include <list>
15-
#include <optional>
1615
#include <unordered_map>
1716
#include <vector>
1817

@@ -136,6 +135,7 @@ class AsyncWebHeader {
136135
String _value;
137136

138137
public:
138+
AsyncWebHeader() {}
139139
AsyncWebHeader(const AsyncWebHeader &) = default;
140140
AsyncWebHeader(AsyncWebHeader &&) = default;
141141
AsyncWebHeader(const char *name, const char *value) : _name(name), _value(value) {}
@@ -145,7 +145,7 @@ class AsyncWebHeader {
145145
[[deprecated("Use AsyncWebHeader::parse(data) instead")]]
146146
#endif
147147
AsyncWebHeader(const String &data)
148-
: AsyncWebHeader(parse(data).value_or(AsyncWebHeader(emptyString, emptyString))){};
148+
: AsyncWebHeader(parse(data)) {};
149149

150150
AsyncWebHeader &operator=(const AsyncWebHeader &) = default;
151151

@@ -158,10 +158,15 @@ class AsyncWebHeader {
158158

159159
String toString() const;
160160

161-
static std::optional<AsyncWebHeader> parse(const String &data) {
161+
// returns true if the header is valid
162+
operator bool() const {
163+
return _name.length();
164+
}
165+
166+
static const AsyncWebHeader parse(const String &data) {
162167
return parse(data.c_str());
163168
}
164-
static std::optional<AsyncWebHeader> parse(const char *data);
169+
static const AsyncWebHeader parse(const char *data);
165170
};
166171

167172
/*
@@ -306,8 +311,9 @@ class AsyncWebServerRequest {
306311
RequestedConnectionType requestedConnType() const {
307312
return _reqconntype;
308313
}
309-
bool isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED)
310-
const;
314+
bool isExpectedRequestedConnType(
315+
RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED
316+
) const;
311317
bool isWebSocketUpgrade() const {
312318
return _method == HTTP_GET && isExpectedRequestedConnType(RCT_WS);
313319
}
@@ -495,7 +501,8 @@ class AsyncWebServerRequest {
495501
return beginResponse(code, contentType.c_str(), content, len, callback);
496502
}
497503
#ifndef ESP8266
498-
[[deprecated("Replaced by beginResponse(int code, const String& contentType, const char* content = asyncsrv::empty, AwsTemplateProcessor callback = nullptr)"
504+
[[deprecated(
505+
"Replaced by beginResponse(int code, const String& contentType, const char* content = asyncsrv::empty, AwsTemplateProcessor callback = nullptr)"
499506
)]]
500507
#endif
501508
AsyncWebServerResponse *beginResponse_P(int code, const String &contentType, PGM_P content, AwsTemplateProcessor callback = nullptr);

src/WebRequest.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -341,30 +341,28 @@ bool AsyncWebServerRequest::_parseReqHead() {
341341
}
342342

343343
bool AsyncWebServerRequest::_parseReqHeader() {
344-
std::optional<AsyncWebHeader> header = AsyncWebHeader::parse(_temp);
344+
AsyncWebHeader header = AsyncWebHeader::parse(_temp);
345345
if (header) {
346-
const String &name = header.value().name();
347-
const String &value = header.value().value();
348-
if (name.equalsIgnoreCase(T_Host)) {
349-
_host = value;
350-
} else if (name.equalsIgnoreCase(T_Content_Type)) {
351-
_contentType = value.substring(0, value.indexOf(';'));
352-
if (value.startsWith(T_MULTIPART_)) {
353-
_boundary = value.substring(value.indexOf('=') + 1);
346+
if (header.name().equalsIgnoreCase(T_Host)) {
347+
_host = header.value();
348+
} else if (header.name().equalsIgnoreCase(T_Content_Type)) {
349+
_contentType = header.value().substring(0, header.value().indexOf(';'));
350+
if (header.value().startsWith(T_MULTIPART_)) {
351+
_boundary = header.value().substring(header.value().indexOf('=') + 1);
354352
_boundary.replace(String('"'), String());
355353
_isMultipart = true;
356354
}
357-
} else if (name.equalsIgnoreCase(T_Content_Length)) {
358-
_contentLength = atoi(value.c_str());
359-
} else if (name.equalsIgnoreCase(T_EXPECT) && value.equalsIgnoreCase(T_100_CONTINUE)) {
355+
} else if (header.name().equalsIgnoreCase(T_Content_Length)) {
356+
_contentLength = atoi(header.value().c_str());
357+
} else if (header.name().equalsIgnoreCase(T_EXPECT) && header.value().equalsIgnoreCase(T_100_CONTINUE)) {
360358
_expectingContinue = true;
361-
} else if (name.equalsIgnoreCase(T_AUTH)) {
362-
int space = value.indexOf(' ');
359+
} else if (header.name().equalsIgnoreCase(T_AUTH)) {
360+
int space = header.value().indexOf(' ');
363361
if (space == -1) {
364-
_authorization = value;
362+
_authorization = header.value();
365363
_authMethod = AsyncAuthType::AUTH_OTHER;
366364
} else {
367-
String method = value.substring(0, space);
365+
String method = header.value().substring(0, space);
368366
if (method.equalsIgnoreCase(T_BASIC)) {
369367
_authMethod = AsyncAuthType::AUTH_BASIC;
370368
} else if (method.equalsIgnoreCase(T_DIGEST)) {
@@ -374,13 +372,13 @@ bool AsyncWebServerRequest::_parseReqHeader() {
374372
} else {
375373
_authMethod = AsyncAuthType::AUTH_OTHER;
376374
}
377-
_authorization = value.substring(space + 1);
375+
_authorization = header.value().substring(space + 1);
378376
}
379-
} else if (name.equalsIgnoreCase(T_UPGRADE) && value.equalsIgnoreCase(T_WS)) {
377+
} else if (header.name().equalsIgnoreCase(T_UPGRADE) && header.value().equalsIgnoreCase(T_WS)) {
380378
// WebSocket request can be uniquely identified by header: [Upgrade: websocket]
381379
_reqconntype = RCT_WS;
382-
} else if (name.equalsIgnoreCase(T_ACCEPT)) {
383-
String lowcase(value);
380+
} else if (header.name().equalsIgnoreCase(T_ACCEPT)) {
381+
String lowcase(header.value());
384382
lowcase.toLowerCase();
385383
#ifndef ESP8266
386384
const char *substr = std::strstr(lowcase.c_str(), T_text_event_stream);
@@ -392,7 +390,7 @@ bool AsyncWebServerRequest::_parseReqHeader() {
392390
_reqconntype = RCT_EVENT;
393391
}
394392
}
395-
_headers.emplace_back(std::move(header.value()));
393+
_headers.emplace_back(std::move(header));
396394
}
397395
#if defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) || defined(LIBRETINY)
398396
// Ancient PRI core does not have String::clear() method 8-()

0 commit comments

Comments
 (0)