-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.cpp
51 lines (45 loc) · 1.62 KB
/
header.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "http/header.h"
#include "http/http_utils.h"
namespace Next::Http {
Header::Header(const std::string &key, const std::string &value)
: key_(key), value_(value) {}
Header::Header(std::string &&key, std::string &&value)
: key_(std::move(key)), value_(std::move(value)) {}
Header::Header(const std::string &line) {
auto tokens = Spilt(line, COLON);
if (tokens.size() < 2) {
valid_ = false;
return;
}
key_ = std::move(tokens[0]);
tokens.erase(std::begin(tokens));
std::string value = (tokens.size() == 1) ? tokens[0] : Join(tokens, COLON);
value_ = std::move(value);
}
Header::Header(Header &&other) noexcept
: key_(std::move(other.key_)), value_(std::move(other.value_)),
valid_(other.valid_) {}
auto Header::operator=(Header &&other) -> Header & {
key_ = std::move(other.key_);
value_ = std::move(other.value_);
valid_ = other.valid_;
return *this;
}
auto Header::IsValid() const -> bool { return valid_; }
auto Header::GetKey() const -> std::string { return key_; }
auto Header::GetValue() const -> std::string { return value_; }
void Header::SetValue(const std::string &new_value) noexcept {
value_ = new_value;
}
auto Header::Serialize() const -> std::string {
return key_ + COLON + value_ + CRLF;
}
auto operator<<(std::ostream &os, const Header &header) -> std::ostream & {
os << "HTTP Header contains:" << std::endl;
os << "Key: " << header.GetKey() << std::endl;
os << "Value: " << header.GetValue() << std::endl;
os << "IsValid: " << ((header.IsValid()) ? "True" : "False") << std::endl;
os << "Serialize to: " << header.Serialize();
return os;
}
} // namespace Next::Http