quote.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <util/generic/strbuf.h>
  3. #include <util/generic/string.h>
  4. //CGIEscape*:
  5. // ' ' converted to '+',
  6. // Some punctuation and chars outside [32, 126] range are converted to %xx
  7. // Use function CgiEscapeBufLen to determine number of characters needed for 'char* to' parameter.
  8. // Returns pointer to the end of the result string
  9. char* CGIEscape(char* to, const char* from);
  10. char* CGIEscape(char* to, const char* from, size_t len);
  11. inline char* CGIEscape(char* to, const TStringBuf from) {
  12. return CGIEscape(to, from.data(), from.size());
  13. }
  14. void CGIEscape(TString& url);
  15. TString CGIEscapeRet(const TStringBuf url);
  16. TString& AppendCgiEscaped(const TStringBuf value, TString& to);
  17. inline TStringBuf CgiEscapeBuf(char* to, const TStringBuf from) {
  18. return TStringBuf(to, CGIEscape(to, from.data(), from.size()));
  19. }
  20. inline TStringBuf CgiEscape(void* tmp, const TStringBuf s) {
  21. return CgiEscapeBuf(static_cast<char*>(tmp), s);
  22. }
  23. //CgiUnescape*:
  24. // Decodes '%xx' to bytes, '+' to space.
  25. // Use function CgiUnescapeBufLen to determine number of characters needed for 'char* to' parameter.
  26. // If pointer returned, then this is pointer to the end of the result string.
  27. char* CGIUnescape(char* to, const char* from);
  28. char* CGIUnescape(char* to, const char* from, size_t len);
  29. void CGIUnescape(TString& url);
  30. TString CGIUnescapeRet(const TStringBuf from);
  31. inline TStringBuf CgiUnescapeBuf(char* to, const TStringBuf from) {
  32. return TStringBuf(to, CGIUnescape(to, from.data(), from.size()));
  33. }
  34. inline TStringBuf CgiUnescape(void* tmp, const TStringBuf s) {
  35. return CgiUnescapeBuf(static_cast<char*>(tmp), s);
  36. }
  37. //Quote:
  38. // Is like CGIEscape, also skips encoding of user-supplied 'safe' characters.
  39. char* Quote(char* to, const char* from, const char* safe = "/");
  40. char* Quote(char* to, const TStringBuf s, const char* safe = "/");
  41. void Quote(TString& url, const char* safe = "/");
  42. //UrlEscape:
  43. // Can't be used for cgi parameters ('&' character is not escaped)!
  44. // escapes only '%' not followed by two hex-digits or if forceEscape set to ture,
  45. // and chars outside [32, 126] range.
  46. // Can't handle '\0'-chars in TString.
  47. char* UrlEscape(char* to, const char* from, bool forceEscape = false);
  48. void UrlEscape(TString& url, bool forceEscape = false);
  49. TString UrlEscapeRet(const TStringBuf from, bool forceEscape = false);
  50. //UrlUnescape:
  51. // '+' is NOT converted to space!
  52. // %xx converted to bytes, other characters are copied unchanged.
  53. char* UrlUnescape(char* to, TStringBuf from);
  54. void UrlUnescape(TString& url);
  55. TString UrlUnescapeRet(const TStringBuf from);
  56. //*BufLen: how much characters you should allocate for 'char* to' buffers.
  57. constexpr size_t CgiEscapeBufLen(const size_t len) noexcept {
  58. return 3 * len + 1;
  59. }
  60. constexpr size_t CgiUnescapeBufLen(const size_t len) noexcept {
  61. return len + 1;
  62. }