diff options
author | Roland Reichwein <mail@reichwein.it> | 2021-09-24 11:02:21 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2021-09-24 11:02:21 +0200 |
commit | 9a40db34cd48b776023e3558a855458fa4f9d264 (patch) | |
tree | b7458e4702fa01a73adc78c73563590cb8d684f3 /libcommon | |
parent | 108dec000f813e1ba54dfc2a59a9dfe028cca414 (diff) |
webserver version 1.14: Bugfix: URL decode in static files
Diffstat (limited to 'libcommon')
-rw-r--r-- | libcommon/Makefile | 3 | ||||
-rw-r--r-- | libcommon/url.cpp | 32 | ||||
-rw-r--r-- | libcommon/url.h | 6 |
3 files changed, 40 insertions, 1 deletions
diff --git a/libcommon/Makefile b/libcommon/Makefile index d3d781d..ddff9fc 100644 --- a/libcommon/Makefile +++ b/libcommon/Makefile @@ -10,7 +10,8 @@ PROGSRC=\ file.cpp \ mime.cpp \ stringutil.cpp \ - tempfile.cpp + tempfile.cpp \ + url.cpp SRC=$(PROGSRC) diff --git a/libcommon/url.cpp b/libcommon/url.cpp new file mode 100644 index 0000000..5baf603 --- /dev/null +++ b/libcommon/url.cpp @@ -0,0 +1,32 @@ +#include "url.h" + +std::string urlDecode(std::string s) +{ + std::string result; + + size_t pos = 0; + while (pos < s.size()) { + char c {s[pos]}; + if (c == '+') { + result += ' '; + } else if (c == '%' && pos + 2 < s.size()) { + try { + int i = stoi(s.substr(pos + 1, 2), 0, 16); + if (i < 0 || i > 255) + return result; + + result += static_cast<char>(i); + } catch (...) { + return result; + } + + pos += 2; + } else { + result += c; + } + pos++; + } + + return result; +} + diff --git a/libcommon/url.h b/libcommon/url.h new file mode 100644 index 0000000..bd60616 --- /dev/null +++ b/libcommon/url.h @@ -0,0 +1,6 @@ +#pragma once + +#include <string> + +std::string urlDecode(std::string s); + |