NetServices: Rewrite BHttpFields to use raw strings as underlying data storage
This change also drops the principle that fields with the same keys would be grouped together. This was initially inspired by Boost::Beast, but it means a lot of extra copying of data when adding/organizing the list, as well as inefficient querying on each add. Now that the design choice is to fully go for the raw string as underlying data storage, that choice is not necessary. In the future it may be able to emulate the grouping or retrieving of lists of values in the API, rather than as a fundamental principle of the data storage. Change-Id: I2667cfa38eb3b7b75393ee71fb038231a40b4193
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#define _B_HTTP_FIELDS_H_
|
||||
|
||||
#include <list>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@@ -42,19 +43,17 @@ public:
|
||||
bool operator==(const FieldName& other) const noexcept;
|
||||
|
||||
// Conversion
|
||||
operator BString() const;
|
||||
operator std::string_view() const;
|
||||
private:
|
||||
friend class BHttpFields;
|
||||
FieldName(const std::string_view& name);
|
||||
FieldName(BString name);
|
||||
FieldName(const FieldName& other);
|
||||
FieldName() noexcept;
|
||||
FieldName(const std::string_view& name) noexcept;
|
||||
FieldName(const FieldName& other) noexcept;
|
||||
FieldName(FieldName&&) noexcept;
|
||||
FieldName& operator=(const FieldName& other);
|
||||
FieldName& operator=(const FieldName& other) noexcept;
|
||||
FieldName& operator=(FieldName&&) noexcept;
|
||||
FieldName& operator=(BString name);
|
||||
|
||||
std::variant<std::string_view, BString> fName;
|
||||
std::string_view fName;
|
||||
};
|
||||
|
||||
class Field {
|
||||
@@ -62,6 +61,7 @@ public:
|
||||
// Constructors
|
||||
Field() noexcept;
|
||||
Field(const std::string_view& name, const std::string_view& value);
|
||||
Field(BString& field);
|
||||
Field(const Field& other);
|
||||
Field(Field&&) noexcept;
|
||||
|
||||
@@ -72,54 +72,56 @@ public:
|
||||
// Access Operators
|
||||
const FieldName& Name() const noexcept;
|
||||
std::string_view Value() const noexcept;
|
||||
std::string_view RawField() const noexcept;
|
||||
bool IsEmpty() const noexcept;
|
||||
|
||||
private:
|
||||
friend class BHttpFields;
|
||||
|
||||
Field(const std::string_view& name, const std::string_view& value, bool borrowed);
|
||||
Field(BString&& rawField);
|
||||
|
||||
std::optional<BString> fRawField;
|
||||
|
||||
FieldName fName;
|
||||
std::variant<std::string_view, BString>
|
||||
fValue;
|
||||
std::string_view fValue;
|
||||
};
|
||||
|
||||
// Type Aliases
|
||||
using ConstIterator = std::list<Field>::const_iterator;
|
||||
|
||||
// Constructors & Destructor
|
||||
BHttpFields();
|
||||
BHttpFields(std::initializer_list<Field> fields);
|
||||
BHttpFields(const BHttpFields& other);
|
||||
BHttpFields(BHttpFields&& other);
|
||||
~BHttpFields() noexcept;
|
||||
BHttpFields();
|
||||
BHttpFields(std::initializer_list<Field> fields);
|
||||
BHttpFields(const BHttpFields& other);
|
||||
BHttpFields(BHttpFields&& other);
|
||||
~BHttpFields() noexcept;
|
||||
|
||||
// Assignment operators
|
||||
BHttpFields& operator=(const BHttpFields&);
|
||||
BHttpFields& operator=(BHttpFields&&) noexcept;
|
||||
BHttpFields& operator=(const BHttpFields&);
|
||||
BHttpFields& operator=(BHttpFields&&) noexcept;
|
||||
|
||||
// Access list
|
||||
const Field& operator[](size_t index) const;
|
||||
const Field& operator[](size_t index) const;
|
||||
|
||||
// Modifiers
|
||||
void AddField(const std::string_view& name, const std::string_view& value);
|
||||
void AddFields(std::initializer_list<Field> fields);
|
||||
void RemoveField(const std::string_view& name) noexcept;
|
||||
void RemoveField(ConstIterator it) noexcept;
|
||||
void MakeEmpty() noexcept;
|
||||
void AddField(const std::string_view& name,
|
||||
const std::string_view& value);
|
||||
void AddField(BString& field);
|
||||
void AddFields(std::initializer_list<Field> fields);
|
||||
void RemoveField(const std::string_view& name) noexcept;
|
||||
void RemoveField(ConstIterator it) noexcept;
|
||||
void MakeEmpty() noexcept;
|
||||
|
||||
// Querying
|
||||
ConstIterator FindField(const std::string_view& name) const noexcept;
|
||||
size_t CountFields() const noexcept;
|
||||
ConstIterator FindField(const std::string_view& name) const noexcept;
|
||||
size_t CountFields() const noexcept;
|
||||
|
||||
// Range-based iteration
|
||||
ConstIterator begin() const noexcept;
|
||||
ConstIterator end() const noexcept;
|
||||
ConstIterator begin() const noexcept;
|
||||
ConstIterator end() const noexcept;
|
||||
|
||||
private:
|
||||
void _AddField(Field&& field);
|
||||
|
||||
std::list<Field> fFields;
|
||||
std::list<Field> fFields;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user