* Check if the given string is suitable to be used as an HTTP header name. * * @param name The value to check for validity * @return true if the argument is a valid header name, false otherwise */
| 119 | * @return true if the argument is a valid header name, false otherwise |
| 120 | */ |
| 121 | bool HttpUtility::IsValidHeaderName(std::string_view name) |
| 122 | { |
| 123 | /* |
| 124 | * Derived from the following syntax definition in RFC9110: |
| 125 | * |
| 126 | * field-name = token |
| 127 | * token = 1*tchar |
| 128 | * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 129 | * ALPHA = %x41-5A / %x61-7A ; A-Z / a-z |
| 130 | * DIGIT = %x30-39 ; 0-9 |
| 131 | * |
| 132 | * References: |
| 133 | * - https://datatracker.ietf.org/doc/html/rfc9110#section-5.1 |
| 134 | * - https://datatracker.ietf.org/doc/html/rfc9110#appendix-A |
| 135 | * - https://www.rfc-editor.org/rfc/rfc5234#appendix-B.1 |
| 136 | */ |
| 137 | |
| 138 | return !name.empty() && std::all_of(name.begin(), name.end(), [](char c) { |
| 139 | switch (c) { |
| 140 | case '!': case '#': case '$': case '%': case '&': case '\'': case '*': case '+': |
| 141 | case '-': case '.': case '^': case '_': case '`': case '|': case '~': |
| 142 | return true; |
| 143 | default: |
| 144 | return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Check if the given string is suitable to be used as an HTTP header value. |