Line data Source code
1 : #include "cookie_store.hpp"
2 :
3 : namespace thinger::http{
4 :
5 2078 : cookie_store::cookie_store() = default;
6 :
7 2078 : cookie_store::~cookie_store() = default;
8 :
9 46 : bool cookie_store::update_from_headers(const headers& hdrs){
10 46 : bool updated = false;
11 46 : std::vector<std::string> cookie_headers = hdrs.get_headers_with_key(header::set_cookie);
12 46 : for(const auto& header_value : cookie_headers){
13 0 : cookie c = cookie::parse(header_value);
14 0 : if(c.is_valid()){
15 0 : cookies_[c.get_name()] = c;
16 0 : updated = true;
17 : }
18 0 : }
19 46 : return updated;
20 46 : }
21 :
22 8 : std::string cookie_store::get_cookie_string() const{
23 8 : std::string result;
24 20 : for(const auto& [name, c] : cookies_){
25 12 : if(!result.empty()){
26 6 : result += "; ";
27 : }
28 12 : result += c.get_name();
29 12 : result += "=";
30 12 : result += c.get_value();
31 : }
32 8 : return result;
33 0 : }
34 :
35 32 : void cookie_store::set_cookie(const cookie& c) {
36 32 : if(c.is_valid()){
37 28 : cookies_[c.get_name()] = c;
38 : }
39 32 : }
40 :
41 26 : void cookie_store::set_cookie(const std::string& name, const std::string& value) {
42 26 : set_cookie(cookie(name, value));
43 26 : }
44 :
45 6 : std::optional<cookie> cookie_store::get_cookie(const std::string& name) const {
46 6 : auto it = cookies_.find(name);
47 6 : if(it != cookies_.end()){
48 4 : return it->second;
49 : }
50 2 : return std::nullopt;
51 : }
52 :
53 10 : bool cookie_store::has_cookie(const std::string& name) const {
54 10 : return cookies_.find(name) != cookies_.end();
55 : }
56 :
57 4 : void cookie_store::remove_cookie(const std::string& name) {
58 4 : cookies_.erase(name);
59 4 : }
60 :
61 2 : void cookie_store::clear() {
62 2 : cookies_.clear();
63 2 : }
64 :
65 8 : size_t cookie_store::size() const {
66 8 : return cookies_.size();
67 : }
68 :
69 12 : bool cookie_store::empty() const {
70 12 : return cookies_.empty();
71 : }
72 :
73 : }
|