Skip to content

Commit

Permalink
Add hv::escapeURL, hv::escapeHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Dec 20, 2023
1 parent a5b3744 commit 0595007
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
22 changes: 22 additions & 0 deletions cpputil/hurl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,25 @@ const std::string& HUrl::dump() {
}
return url;
}

namespace hv {

std::string escapeHTML(const std::string& str) {
std::string ostr;
const char* p = str.c_str();
while (*p != '\0') {
switch (*p) {
case '<': ostr += "&lt;"; break;
case '>': ostr += "&gt;"; break;
case '&': ostr += "&amp;"; break;
case '\"': ostr += "&quot;"; break;
case '\'': ostr += "&apos;"; break;
// case ' ': ostr += "&nbsp;"; break;
default: ostr += *p; break;
}
++p;
}
return ostr;
}

}
13 changes: 10 additions & 3 deletions cpputil/hurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ class HV_EXPORT HUrl {
public:
static std::string escape(const std::string& str, const char* unescaped_chars = "");
static std::string unescape(const std::string& str);
static inline std::string escapeUrl(const std::string& url) {
return escape(url, ":/@?=&#+");
}

HUrl() : port(0) {}
~HUrl() {}
Expand All @@ -31,4 +28,14 @@ class HV_EXPORT HUrl {
std::string fragment;
};

namespace hv {

HV_INLINE std::string escapeURL(const std::string& url) {
return HUrl::escape(url, ":/@?=&#+");
}

HV_EXPORT std::string escapeHTML(const std::string& str);

} // end namespace hv

#endif // HV_URL_H_
2 changes: 1 addition & 1 deletion examples/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ int main(int argc, char* argv[]) {
req.method = HTTP_POST;
}
}
req.url = HUrl::escapeUrl(url);
req.url = hv::escapeURL(url);
req.http_cb = [](HttpMessage* res, http_parser_state state, const char* data, size_t size) {
if (state == HP_HEADERS_COMPLETE) {
if (verbose) {
Expand Down

0 comments on commit 0595007

Please sign in to comment.