8000 Fix some bugs i created while removing mgStrToString · headcloudmonkey/esp32-snippets@32111fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 32111fb

Browse files
committed
Fix some bugs i created while removing mgStrToString
Everything works now
1 parent 4d7655a commit 32111fb

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

cpp_utils/WebServer.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ static std::string mongoose_eventToString(int event) {
109109

110110
static void dumpHttpMessage(struct http_message *pHttpMessage) {
111111
ESP_LOGD(tag, "HTTP Message");
112-
ESP_LOGD(tag, "Message: %s", pHttpMessage->message.p);
113-
ESP_LOGD(tag, "URI: %s", pHttpMessage->uri.p);
112+
ESP_LOGD(tag, "Message: %.*s", (int)pHttpMessage->uri.len, pHttpMessage->message.p);
114113
}
115114

116115
/*
@@ -539,7 +538,7 @@ void WebServer::HTTPResponse::setStatus(int status) {
539538
* @param [in] message The message representing the request.
540539
*/
541540
void WebServer::processRequest(struct mg_connection *mgConnection, struct http_message* message) {
542-
ESP_LOGD(tag, "WebServer::processRequest: Matching: %s", message->uri.p);
541+
ESP_LOGD(tag, "WebServer::processRequest: Matching: %.*s", (int)message->uri.len, message->uri.p);
543542
HTTPResponse httpResponse = HTTPResponse(mgConnection);
544543
httpResponse.setRootPath(getRootPath());
545544

@@ -559,7 +558,7 @@ void WebServer::processRequest(struct mg_connection *mgConnection, struct http_m
559558
// Because we reached here, it means that we did NOT match a handler. Now we want to attempt
560559
// to retrieve the corresponding file content.
561560
std::string filePath = httpResponse.getRootPath();
562-
filePath += message->uri.p;
561+
filePath.append(message->uri.p, message->uri.len);
563562
ESP_LOGD(tag, "Opening file: %s", filePath.c_str());
564563
FILE *file = fopen(filePath.c_str(), "r");
565564
if (file != nullptr) {
@@ -586,17 +585,13 @@ void WebServer::processRequest(struct mg_connection *mgConnection, struct http_m
586585
* @param [in] pathPattern The path pattern to be matched.
587586
* @param [in] webServerRequestHandler The request handler to be called.
588587
*/
589-
WebServer::PathHandler::PathHandler(const std::string& method, const std::string& pathPattern,
590-
void (* webServerRequestHandler)(WebServer::HTTPRequest* pHttpRequest,
591-
WebServer::HTTPResponse* pHttpResponse)) {
588+
WebServer::PathHandler::PathHandler(const std::string& method, const std::string& pathPattern, void (* webServerRequestHandler)(WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse)) {
592589
m_method = method;
593590
m_pattern = std::regex(pathPattern);
594591
m_requestHandler = webServerRequestHandler;
595592
} // PathHandler
596593

597-
WebServer::PathHandler::PathHandler(std::string&& method, const std::string& pathPattern,
598-
void (* webServerRequestHandler)(WebServer::HTTPRequest* pHttpRequest,
599-
WebServer::HTTPResponse* pHttpResponse)) {
594+
WebServer::PathHandler::PathHandler(std::string&& method, const std::string& pathPattern, void (* webServerRequestHandler)(WebServer::HTTPRequest* pHttpRequest, WebServer::HTTPResponse* pHttpResponse)) {
600595
m_method = std::move(method);
601596
m_pattern = std::regex(pathPattern);
602597
m_requestHandler = webServerRequestHandler;
@@ -614,7 +609,7 @@ WebServer::PathHandler::PathHandler(std::string&& method, const std::string& pat
614609

615610
bool WebServer::PathHandler::match(const char* method, size_t method_len, const char* path) {
616611
//ESP_LOGD(tag, "match: %s with %s", m_pattern.c_str(), path.c_str());
617-
if (method_len != m_method.length() || strcmp(method, m_method.c_str()) != 0) {
612+
if (method_len != m_method.length() || strncmp(method, m_method.c_str(), method_len) != 0) {
618613
return false;
619614
}
620615
return std::regex_search(path, m_pattern);
@@ -646,7 +641,8 @@ WebServer::HTTPRequest::HTTPRequest(struct http_message* message) {
646641
/**
647642
* @brief Get the body of the request.
648643
* When an HTTP request is either PUT or POST then it may contain a payload that is also
649-
* known as the body. This method returns that payload (if it exists).
644+
* known as the body. This method returns that payload (if it exists). Careful, because it's not a standard string
645+
* that is terminated by a null character, use the getBodyLen() function to determine the body length
650646
* @return The body of the request.
651647
*/
652648
const char* WebServer::HTTPRequest::getBody() const {
@@ -667,7 +663,9 @@ size_t WebServer::HTTPRequest::getBodyLen() const {
667663
/**
668664
* @brief Get the method of the request.
669665
* An HTTP request contains a request method which is one of GET, PUT, POST, etc.
670-
* @return The method of the request.
666+
* @return The method of the request. Careful, because it's not a standard string
667+
* that is terminated by a null character, use the getMethodLen() function to determine the method length
668+
* @return The body of the request.
671669
*/
672670
const char* WebServer::HTTPRequest::getMethod() const {
673671
return m_message->method.p;
@@ -686,7 +684,9 @@ size_t WebServer::HTTPRequest::getMethodLen() const {
686684
/**
687685
* @brief Get the path of the request.
688686
* The path of an HTTP request is the portion of the URL that follows the hostname/port pair
689-
* but does not include any query parameters.
687+
* but does not include any query parameters. Careful, because it's not a standard string
688+
* that is terminated by a null character, use the getPathLen() function to determine the path length
689+
* @return The body of the request.
690690
* @return The path of the request.
691691
*/
692692
const char* WebServer::HTTPRequest::getPath() const {
@@ -777,7 +777,7 @@ std::map<std::string, std::string> WebServer::HTTPRequest::getQuery() const {
777777
* @return A vector of the constituent parts of the path.
778778
*/
779779
std::vector<std::string> WebServer::HTTPRequest::pathSplit() const {
780-
std::istringstream stream(getPath());
780+
std::istringstream stream(std::string(getPath(), getPathLen())); // I don't know if there's a better istringstream constructor for this
781781
std::vector<std::string> ret;
782782
std::string pathPart;
783783
while(std::getline(stream, pathPart, '/')) {

0 commit comments

Comments
 (0)
0