@@ -109,8 +109,7 @@ static std::string mongoose_eventToString(int event) {
109
109
110
110
static void dumpHttpMessage (struct http_message *pHttpMessage) {
111
111
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 );
114
113
}
115
114
116
115
/*
@@ -539,7 +538,7 @@ void WebServer::HTTPResponse::setStatus(int status) {
539
538
* @param [in] message The message representing the request.
540
539
*/
541
540
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 );
543
542
HTTPResponse httpResponse = HTTPResponse (mgConnection);
544
543
httpResponse.setRootPath (getRootPath ());
545
544
@@ -559,7 +558,7 @@ void WebServer::processRequest(struct mg_connection *mgConnection, struct http_m
559
558
// Because we reached here, it means that we did NOT match a handler. Now we want to attempt
560
559
// to retrieve the corresponding file content.
561
560
std::string filePath = httpResponse.getRootPath ();
562
- filePath += message->uri .p ;
561
+ filePath. append ( message->uri .p , message-> uri . len ) ;
563
562
ESP_LOGD (tag, " Opening file: %s" , filePath.c_str ());
564
563
FILE *file = fopen (filePath.c_str (), " r" );
565
564
if (file != nullptr ) {
@@ -586,17 +585,13 @@ void WebServer::processRequest(struct mg_connection *mgConnection, struct http_m
586
585
* @param [in] pathPattern The path pattern to be matched.
587
586
* @param [in] webServerRequestHandler The request handler to be called.
588
587
*/
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)) {
592
589
m_method = method;
593
590
m_pattern = std::regex (pathPattern);
594
591
m_requestHandler = webServerRequestHandler;
595
592
} // PathHandler
596
593
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)) {
600
595
m_method = std::move (method);
601
596
m_pattern = std::regex (pathPattern);
602
597
m_requestHandler = webServerRequestHandler;
@@ -614,7 +609,7 @@ WebServer::PathHandler::PathHandler(std::string&& method, const std::string& pat
614
609
615
610
bool WebServer::PathHandler::match (const char * method, size_t method_len, const char * path) {
8000
code>
616
611
// 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 ) {
618
613
return false ;
619
614
}
620
615
return std::regex_search (path, m_pattern);
@@ -646,7 +641,8 @@ WebServer::HTTPRequest::HTTPRequest(struct http_message* message) {
646
641
/* *
647
642
* @brief Get the body of the request.
648
643
* 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
650
646
* @return The body of the request.
651
647
*/
652
648
const char * WebServer::HTTPRequest::getBody () const {
@@ -667,7 +663,9 @@ size_t WebServer::HTTPRequest::getBodyLen() const {
667
663
/* *
668
664
* @brief Get the method of the request.
669
665
* 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.
671
669
*/
672
670
const char * WebServer::HTTPRequest::getMethod () const {
673
671
return m_message->method .p ;
@@ -686,7 +684,9 @@ size_t WebServer::HTTPRequest::getMethodLen() const {
686
684
/* *
687
685
* @brief Get the path of the request.
688
686
* 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.
690
690
* @return The path of the request.
691
691
*/
692
692
const char * WebServer::HTTPRequest::getPath () const {
@@ -777,7 +777,7 @@ std::map<std::string, std::string> WebServer::HTTPRequest::getQuery() const {
777
777
* @return A vector of the constituent parts of the path.
778
778
*/
779
779
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
781
781
std::vector<std::string> ret;
782
782
std::string pathPart;
783
783
while (std::getline (stream, pathPart, ' /' )) {
0 commit comments