NetServices: move token validation to header for reuse elsewhere
Change-Id: I2dbed5cfbd387eaf6ed7cf38c73a7db865a87771
This commit is contained in:
@@ -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_
|
||||
Reference in New Issue
Block a user