chmorgan / libesphttpd

Libesphttpd - web server for ESP8266 / ESP32
Mozilla Public License 2.0
128 stars 45 forks source link

how to remove a double-slash in url #30

Closed phatpaul closed 6 years ago

phatpaul commented 6 years ago

Some modern webservers will automatically ignore a double-slash in the URL. (i.e. http://192.168.1.79//min.json is equivalent to http://192.168.1.79/min.json) (see https://webmasters.stackexchange.com/questions/8354/what-does-the-double-slash-mean-in-urls )

How can I have the esphttpd server ignore the double-slash? (without sending a redirect to the browser)

chmorgan commented 6 years ago

It should be possible to update the libesphttpd code to ignore the repeated slashes but I can't say I know where to look at updating that. Maybe someone else using the library can help out? Or you can submit a PR if you figure it out.

tidklaas commented 6 years ago

Dammit, I'm supposed to be working... Well, let's call it an early morning warm-up exercise.

diff --git a/core/httpd.c b/core/httpd.c
index b46eb00..ebfa38d 100644
--- a/core/httpd.c
+++ b/core/httpd.c
@@ -684,6 +684,15 @@ static CallbackStatus ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData
         } else {
             conn->getArgs=NULL;
         }
+
+        // Remove multiple slashes in URL path.
+        while((e = strstr(conn->url, "//")) != NULL){
+            // Move string starting at second slash one place to the left.
+            // Use strlen() on the first slash so the '\0' will be copied too.
+            ESP_LOGD(TAG, "Cleaning up URL path: %s", conn->url);
+            memmove(e, e + 1, strlen(e));
+            ESP_LOGD(TAG, "Cleaned URL path: %s", conn->url);
+        }
     } else if (strncasecmp(h, "Connection:", 11)==0) {
         i=11;
         //Skip trailing spaces

I do not know if altering the URL will break other things or confuse some clients, so maybe this should be a configurable compile time option. I will prepare a proper pull request tonight.

Tido

phatpaul commented 6 years ago

Thanks Tido! That did the trick. Now I'm able to interface with an industrial device that (incorrectly?) requests files with multiple slashes. I guess you should get to work now ;-)

chmorgan commented 6 years ago

@phatpaul @tidklaas's change has been merged into master, closing this issue.