encoding_opts.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_ENCODING_OPTS_HPP
  11. #define BOOST_URL_ENCODING_OPTS_HPP
  12. #include <boost/url/detail/config.hpp>
  13. namespace boost {
  14. namespace urls {
  15. /** Percent-encoding options
  16. These options are used to customize
  17. the behavior of algorithms which use
  18. percent escapes, such as encoding
  19. or decoding.
  20. @see
  21. @ref encode,
  22. @ref encoded_size,
  23. @ref pct_string_view.
  24. */
  25. struct encoding_opts
  26. {
  27. /** True if spaces encode to and from plus signs
  28. Although not prescribed by RFC 3986,
  29. many applications decode plus signs
  30. in URL queries as spaces. In particular,
  31. the form-urlencoded Media Type in HTML
  32. for submitting forms uses this convention.
  33. This option controls whether
  34. the PLUS character ("+") is used to
  35. represent the SP character (" ") when
  36. encoding or decoding.
  37. When this option is `true`, both the
  38. encoded SP ("%20") and the PLUS
  39. character ("+") represent a space (" ")
  40. when decoding. To represent a plus sign,
  41. its encoded form ("%2B") is used.
  42. The @ref encode and @ref encoded_size functions
  43. will encode spaces as plus signs when
  44. this option is `true`, regardless of the
  45. allowed character set. They will also
  46. encode plus signs as "%2B" when this
  47. option is `true`, regardless of the
  48. allowed character set.
  49. Note that when a URL is normalized,
  50. all unreserved percent-encoded characters are
  51. replaced with their unreserved equivalents.
  52. However, normalizing the URL query maintains
  53. the decoded and encoded "&=+" as they are
  54. because they might have different meanings.
  55. This behavior is not optional because
  56. normalization can only mitigate false
  57. negatives, but it should eliminate
  58. false positives.
  59. Making it optional would allow
  60. a false positive because there's
  61. at least one very relevant schema (HTTP)
  62. where a decoded or encoded "&=+" has different
  63. meanings and represents different resources.
  64. The same considerations apply to URL comparison
  65. algorithms in the library, as they treat URLs
  66. as if they were normalized.
  67. @par Specification
  68. @li <a href="https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1">
  69. application/x-www-form-urlencoded (w3.org)</a>
  70. @li <a href="https://datatracker.ietf.org/doc/html/rfc1866#section-8.2.1">
  71. The form-urlencoded Media Type (RFC 1866)</a>
  72. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2.2">
  73. Section 6.2.2.2. Percent-Encoding Normalization (RFC 3986)</a>
  74. */
  75. bool space_as_plus = false;
  76. /** True if hexadecimal digits are emitted as lower case
  77. By default, percent-encoding algorithms
  78. emit hexadecimal digits A through F as
  79. uppercase letters. When this option is
  80. `true`, lowercase letters are used.
  81. */
  82. bool lower_case = false;
  83. /** True if nulls are not allowed
  84. Normally all possible character values
  85. (from 0 to 255) are allowed, with reserved
  86. characters being replaced with escapes
  87. upon encoding. When this option is true,
  88. attempting to decode a null will result
  89. in an error.
  90. */
  91. bool disallow_null = false;
  92. /** Constructs an `encoding_opts` object with the specified options.
  93. @param space_as_plus If true, spaces will be encoded as plus signs.
  94. @param lower_case If true, hexadecimal digits will be emitted as lower case.
  95. @param disallow_null If true, null characters will not be allowed.
  96. */
  97. BOOST_CXX14_CONSTEXPR
  98. inline
  99. encoding_opts(
  100. bool const space_as_plus = false,
  101. bool const lower_case = false,
  102. bool const disallow_null = false) noexcept
  103. : space_as_plus(space_as_plus)
  104. , lower_case(lower_case)
  105. , disallow_null(disallow_null) {}
  106. };
  107. } // urls
  108. } // boost
  109. #endif