Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 27, 2025
1 parent eff38b0 commit 08eb064
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 44 deletions.
2 changes: 1 addition & 1 deletion include/external/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class AsyncClient {
bool send(); // send all data added with the method above

// write equals add()+send()
size_t write(const char* data);
size_t write(std::string_view data);
size_t write(const char* data, size_t size, uint8_t apiflags = ASYNC_WRITE_FLAG_COPY); // only when canSend() == true

uint8_t state();
Expand Down
10 changes: 9 additions & 1 deletion include/external/AsyncWebServer/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ typedef enum {
RCT_MAX
} RequestedConnectionType;

enum HttpParseState {
PARSE_REQ_START,
PARSE_REQ_HEADERS,
PARSE_REQ_BODY,
PARSE_REQ_END,
PARSE_REQ_FAIL
};

typedef std::function<size_t(uint8_t*, size_t, size_t)> AwsResponseFiller;

class AsyncWebServerRequest {
Expand All @@ -143,7 +151,7 @@ class AsyncWebServerRequest {
ArDisconnectHandler _onDisconnectfn;

std::string _temp;
uint8_t _parseState;
HttpParseState _parseState;

HttpVersion _version;
HttpRequestMethod _method;
Expand Down
2 changes: 2 additions & 0 deletions include/external/AsyncWebServer/HttpRequestMethod.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cstdint>

enum HttpRequestMethod : uint16_t {
HTTP_GET = 0b0000000001,
HTTP_POST = 0b0000000010,
Expand Down
16 changes: 8 additions & 8 deletions include/external/AsyncWebServer/WebHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string>
#include "util/StringUtils.h"

#include "stddef.h"
#include <time.h>
#include <cstddef>
#include <string>

class AsyncStaticWebHandler : public AsyncWebHandler {
using File = fs::File;
Expand Down Expand Up @@ -95,17 +95,17 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {
std::string_view requestUrl = request->url();

// Match 1 ??
if (OpenShock::StringStartsWith(uri, "/*."sv)) {
if (OpenShock::StringHasPrefix(uri, "/*."sv)) {
uri = OpenShock::StringAfterLast(uri, '.');

return OpenShock::StringEndsWith(requestUrl, uri);
return OpenShock::StringHasSuffix(requestUrl, uri);
}

// Match 2 ????
if (OpenShock::StringEndsWith(uri, '*')) {
if (OpenShock::StringHasSuffix(uri, '*')) {
uri.remove_suffix(1);

return OpenShock::StringStartsWith(requestUrl, uri);
return OpenShock::StringHasPrefix(requestUrl, uri);
}

// Huh ??????
Expand All @@ -114,7 +114,7 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {
}

// Way to check if requestUrl ends with uri + "/" without any allocations
return requestUrl.length() > uri.length() && requestUrl[uri.length()] == '/' && OpenShock::StringStartsWith(requestUrl, uri);
return requestUrl.length() > uri.length() && requestUrl[uri.length()] == '/' && OpenShock::StringHasPrefix(requestUrl, uri);
}

virtual void handleRequest(AsyncWebServerRequest* request) override final
Expand Down
1 change: 1 addition & 0 deletions include/util/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ namespace OpenShock {
bool StringIEquals(std::string_view a, std::string_view b);
bool StringIContains(std::string_view haystack, std::string_view needle);
bool StringHasPrefixIC(std::string_view view, std::string_view prefix);
bool StringTryRemovePrefixIC(std::string_view& view, std::string_view prefix);

String StringToArduinoString(std::string_view view);
} // namespace OpenShock
6 changes: 3 additions & 3 deletions src/external/AsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,12 @@ bool AsyncClient::free()
return false;
}

size_t AsyncClient::write(const char* data)
size_t AsyncClient::write(std::string_view data)
{
if (data == NULL) {
if (data.empty()) {
return 0;
}
return write(data, strlen(data));
return write(data.data(), data.size());
}

size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags)
Expand Down
2 changes: 1 addition & 1 deletion src/external/AsyncWebServer/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ void AsyncWebSocketResponse::_respond(AsyncWebServerRequest* request)
return;
}
std::string out = _assembleHead(request->version());
request->client()->write(out.c_str(), out.size());
request->client()->write(out);
_state = RESPONSE_WAIT_ACK;
}

Expand Down
14 changes: 7 additions & 7 deletions src/external/AsyncWebServer/WebHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ AsyncStaticWebHandler::AsyncStaticWebHandler(std::string_view uri, FS& fs, std::
, _isDir(false)
{
// Ensure leading '/'
if (!OpenShock::StringStartsWith(uri, '/')) uri.remove_prefix(1);
if (!OpenShock::StringStartsWith(path, '/')) path.remove_prefix(1);
if (!OpenShock::StringHasPrefix(uri, '/')) uri.remove_prefix(1);
if (!OpenShock::StringHasPrefix(path, '/')) path.remove_prefix(1);

// Remove the trailing '/' so we can handle default file
// Notice that root will be "" not "/"
if (OpenShock::StringEndsWith(uri, '/')) uri.remove_suffix(1);
if (OpenShock::StringEndsWith(path, '/')) {
if (OpenShock::StringHasSuffix(uri, '/')) uri.remove_suffix(1);
if (OpenShock::StringHasSuffix(path, '/')) {
path.remove_suffix(1);

// If path ends with '/' we assume a hint that this is a directory to improve performance.
Expand Down Expand Up @@ -77,7 +77,7 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setSharedEtag(std::string_view eta

bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest* request)
{
if (request->method() != HTTP_GET || !OpenShock::StringStartsWith(request->url(), _uri) || !request->isExpectedRequestedConnType(RCT_DEFAULT, RCT_HTTP)) {
if (request->method() != HTTP_GET || !OpenShock::StringHasPrefix(request->url(), _uri) || !request->isExpectedRequestedConnType(RCT_DEFAULT, RCT_HTTP)) {
return false;
}
if (_getFile(request)) {
Expand All @@ -94,7 +94,7 @@ bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest* request)
std::string path(request->url().substr(_uri.length()));

// We can skip the file check and look for default if request is to the root of a directory or that request path ends with '/'
bool canSkipFileCheck = (_isDir && path.length() == 0) || OpenShock::StringEndsWith(path, '/');
bool canSkipFileCheck = (_isDir && path.length() == 0) || OpenShock::StringHasSuffix(path, '/');

path = _path + path;

Expand All @@ -105,7 +105,7 @@ bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest* request)
if (_default_file.length() == 0) return false;

// Try to add default file, ensure there is a trailing '/' ot the path.
if (!OpenShock::StringEndsWith(path, '/')) path.push_back('/');
if (!OpenShock::StringHasSuffix(path, '/')) path.push_back('/');
path += _default_file;

return _fileExists(request, path);
Expand Down
44 changes: 26 additions & 18 deletions src/external/AsyncWebServer/WebRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ static const std::string SharedEmptyString = std::string();

#define __is_param_char(c) ((c) && ((c) != '{') && ((c) != '[') && ((c) != '&') && ((c) != '='))

enum {
PARSE_REQ_START,
PARSE_REQ_HEADERS,
PARSE_REQ_BODY,
PARSE_REQ_END,
PARSE_REQ_FAIL
};

static bool httpTryBasicUriDecode(std::string_view uri, std::string& uri_out)
{
uri_out.clear();
Expand Down Expand Up @@ -115,7 +107,7 @@ static bool httpParseMethod(std::string_view str, HttpRequestMethod& method_out)
static bool httpParseHttpVersion(std::string_view str, HttpVersion& http_version_out)
{
using namespace std::string_view_literals;
if (!OpenShock::StringStartsWith(str, "HTTP/"sv)) {
if (!OpenShock::StringHasPrefix(str, "HTTP/"sv)) {
return false;
}

Expand Down Expand Up @@ -267,7 +259,7 @@ void AsyncWebServerRequest::_onData(void* buf, size_t len)
} else {
if (_parsedLength == 0) {
using namespace std::string_view_literals;
if (OpenShock::StringStartsWith(_contentType, "application/x-www-form-urlencoded"sv)) {
if (OpenShock::StringHasPrefix(_contentType, "application/x-www-form-urlencoded"sv)) {
_isPlainPost = true;
} else if (_contentType == "text/plain" && __is_param_char(((char*)buf)[0])) {
size_t i = 0;
Expand Down Expand Up @@ -448,7 +440,7 @@ bool AsyncWebServerRequest::_parseReqHeader(std::string_view header)
_host = pair.second;
} else if (OpenShock::StringIEquals(pair.first, "Content-Type"sv)) {
_contentType = OpenShock::StringBeforeFirst(pair.second, ';');
if (OpenShock::StringStartsWith(_contentType, "multipart/"sv)) {
if (OpenShock::StringHasPrefix(_contentType, "multipart/"sv)) {
_boundary = OpenShock::StringAfterFirst(pair.second, '=');
_boundary.replace("\"", ""); // TODO: Optimize this by merging with string_view assignment
_isMultipart = true;
Expand Down Expand Up @@ -500,7 +492,7 @@ void AsyncWebServerRequest::_parsePlainPostChar(uint8_t data)
std::string_view name = "body"sv;
std::string_view value = _temp;

if (!OpenShock::StringStartsWith(value, '{') && !OpenShock::StringStartsWith(value, '[')) {
if (!OpenShock::StringHasPrefix(value, '{') && !OpenShock::StringHasPrefix(value, '[')) {
size_t equals_pos = value.find('=');
if (equals_pos != std::string_view::npos) {
name = value.substr(0, equals_pos);
Expand Down Expand Up @@ -583,11 +575,27 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last)
} else if (_multiParseState == PARSE_HEADERS) {
if ((char)data != '\r' && (char)data != '\n') _temp += (char)data;
if ((char)data == '\n') {
if (_temp.length()) {
if (_temp.length() > 12 && _temp.substring(0, 12).equalsIgnoreCase("Content-Type")) {
_itemType = _temp.substring(14);
using namespace std::string_view_literals;
std::string_view view = _temp;
if (!view.empty()) {
if (OpenShock::StringTryRemovePrefixIC(view, "Content-Type:"sv)) {
_itemType = OpenShock::StringTrim(view);
_itemIsFile = true;
} else if (_temp.length() > 19 && _temp.substring(0, 19).equalsIgnoreCase("Content-Disposition")) {
} else if (OpenShock::StringTryRemovePrefixIC(view, "Content-Disposition:"sv)) {
for (const auto part : OpenShock::StringSplit(view, ';')) {
part = OpenShock::StringTrim(part);

size_t equalsPos = part.find('=');
if (equalsPos == std::string_view::npos) {
if (part != "form-data"sv) {
_multiParseState = PARSE_ERROR;
return;
}
} else {
std::string_view key = view.substr(0, equalsPos);
std::string_view value = view.substr(equalsPos + 1);
}
}
_temp = _temp.substring(_temp.indexOf(';') + 2);
while (_temp.indexOf(';') > 0) {
std::string name = _temp.substring(0, _temp.indexOf('='));
Expand Down Expand Up @@ -737,8 +745,8 @@ void AsyncWebServerRequest::_parseLine()
// end of headers
_server->_attachHandler(this);
if (_expectingContinue) {
const char* response = "HTTP/1.1 100 Continue\r\n\r\n";
_client->write(response, strlen(response));
using namespace std::string_view_literals;
_client->write("HTTP/1.1 100 Continue\r\n\r\n"sv);
}
// check handler for authentication
if (_contentLength) {
Expand Down
10 changes: 5 additions & 5 deletions src/external/AsyncWebServer/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,22 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest* request)
size_t space = request->client()->space();

if (!_contentLength && space >= out.size()) {
_writtenLength += request->client()->write(out.data(), out.size());
_writtenLength += request->client()->write(out);
_state = RESPONSE_WAIT_ACK;
return;
}

if (_contentLength && space >= out.size() + _contentLength) {
out += _content;
_writtenLength += request->client()->write(out.data(), out.size());
_writtenLength += request->client()->write(out);
_state = RESPONSE_WAIT_ACK;
return;
}

if (space && space < out.size()) {
_content.insert(_content.begin(), out.begin() + space, out.end());
_contentLength += out.size() - space;
_writtenLength += request->client()->write(out.data(), space);
_writtenLength += request->client()->write(out);
_state = RESPONSE_CONTENT;
return;
}
Expand All @@ -275,7 +275,7 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest* request)
out += std::string_view(_content).substr(0, shift);
_content.erase(0, shift);
_sentLength += shift;
_writtenLength += request->client()->write(out.c_str(), out.length());
_writtenLength += request->client()->write(out);
_state = RESPONSE_CONTENT;
return;
}
Expand Down Expand Up @@ -556,7 +556,7 @@ AsyncFileResponse::AsyncFileResponse(File content, std::string_view path, std::s

using namespace std::string_view_literals;

if (!download && OpenShock::StringEndsWith(content.name(), ".gz"sv) && !OpenShock::StringEndsWith(path, ".gz"sv)) {
if (!download && OpenShock::StringHasSuffix(content.name(), ".gz"sv) && !OpenShock::StringHasSuffix(path, ".gz"sv)) {
addHeader("Content-Encoding", "gzip");
_sendContentLength = true;
_chunked = false;
Expand Down
8 changes: 8 additions & 0 deletions src/util/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ bool OpenShock::StringHasPrefixIC(std::string_view view, std::string_view prefix
if (view.size() < prefix.size()) return false;
return StringIEquals(view.substr(0, prefix.size()), prefix);
}
bool OpenShock::StringTryRemovePrefixIC(std::string_view& view, std::string_view prefix)
{
if (!StringHasPrefixIC(view, prefix)) return false;

view.remove_prefix(prefix.size());

return true;
}

String OpenShock::StringToArduinoString(std::string_view view) {
return String(view.data(), view.size());
Expand Down

1 comment on commit 08eb064

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.3) reports: 2 file(s) not formatted
  • include/external/AsyncWebServer/HttpRequestMethod.h
  • src/util/StringUtils.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.