NetServices: move token validation to header for reuse elsewhere

Change-Id: I2dbed5cfbd387eaf6ed7cf38c73a7db865a87771
This commit is contained in:
Niels Sascha Reedijk
2022-02-20 13:10:20 +00:00
parent a8003a7030
commit cb67e34887
2 changed files with 45 additions and 23 deletions
@@ -12,34 +12,14 @@
#include <ctype.h>
#include <utility>
#include "HttpPrivate.h"
using namespace BPrivate::Network;
// #pragma mark -- utilities
/*!
\brief Validate whether the string conforms to a HTTP token value
RFC 7230 section 3.2.6 determines that valid tokens for the header name are:
!#$%&'*+=.^_`|~, any digits or alpha.
\returns \c true if the string is valid, or \c false if it is not.
*/
static inline bool
validate_token_string(const std::string_view& string)
{
for (auto it = string.cbegin(); it < string.cend(); it++) {
if (*it <= 31 || *it == 127 || *it == '(' || *it == ')' || *it == '<' || *it == '>'
|| *it == '@' || *it == ',' || *it == ';' || *it == '\\' || *it == '"'
|| *it == '/' || *it == '[' || *it == ']' || *it == '?' || *it == '='
|| *it == '{' || *it == '}' || *it == ' ')
return false;
}
return true;
}
/*!
\brief Validate whether the string is a valid HTTP header value
@@ -269,7 +249,7 @@ BHttpFields::Field::Field(const std::string_view& name, const std::string_view&
BHttpFields::Field::Field(const std::string_view& name, const std::string_view& value, bool borrowed)
: fName(name), fValue(value)
{
if (name.length() == 0 || !validate_token_string(name))
if (name.length() == 0 || !validate_http_token_string(name))
throw BHttpFields::InvalidInput(__PRETTY_FUNCTION__, fName);
if (value.length() == 0 || !validate_value_string(value))
throw BHttpFields::InvalidInput(__PRETTY_FUNCTION__, BString(value.data(), value.length()));
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _B_HTTP_PRIVATE_H_
#define _B_HTTP_PRIVATE_H_
#include <string_view>
namespace BPrivate {
namespace Network {
/*!
\brief Validate whether the string conforms to a HTTP token value
RFC 7230 section 3.2.6 determines that valid tokens for the header name are:
!#$%&'*+=.^_`|~, any digits or alpha.
\returns \c true if the string is valid, or \c false if it is not.
*/
static inline bool
validate_http_token_string(const std::string_view& string)
{
for (auto it = string.cbegin(); it < string.cend(); it++) {
if (*it <= 31 || *it == 127 || *it == '(' || *it == ')' || *it == '<' || *it == '>'
|| *it == '@' || *it == ',' || *it == ';' || *it == '\\' || *it == '"'
|| *it == '/' || *it == '[' || *it == ']' || *it == '?' || *it == '='
|| *it == '{' || *it == '}' || *it == ' ')
return false;
}
return true;
}
} // namespace Network
} // namespace BPrivate
#endif // _B_HTTP_PRIVATE_H_