Unverified Commit b1fd9c8f authored by hasankandemir1993's avatar hasankandemir1993 Committed by GitHub

Update cookie.h

parent 61af27cb
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
#include <list>
#include <pistache/optional.h> #include <pistache/optional.h>
#include <pistache/http_defs.h> #include <pistache/http_defs.h>
...@@ -41,25 +43,46 @@ struct Cookie { ...@@ -41,25 +43,46 @@ struct Cookie {
class CookieJar { class CookieJar {
public: public:
using Storage = std::unordered_map<std::string, Cookie>; typedef std::unordered_map<std::string, Cookie> HashMapCookies; // "value" -> Cookie
typedef std::unordered_map<std::string, HashMapCookies> Storage; // "name" -> Hashmap("value" -> Cookie)
struct iterator : std::iterator<std::bidirectional_iterator_tag, Cookie> { struct iterator : std::iterator<std::bidirectional_iterator_tag, Cookie> {
iterator(const Storage::const_iterator& _iterator) iterator(const Storage::const_iterator& _iterator)
: it_(_iterator) : it_(_iterator)
{ } {
}
iterator(const Storage::const_iterator& _iterator, const Storage::const_iterator& end)
: it_(_iterator),end_(end)
{
if(it_ != end_) {
it_2 = it_->second.begin();
}
}
Cookie operator*() const { Cookie operator*() const {
return it_->second; return it_2->second; // return it_->second;
} }
iterator operator++() { iterator operator++() {
++it_2;
if(it_2 == it_->second.end()) {
++it_; ++it_;
return iterator(it_); if(it_ != end_)
it_2 = it_->second.begin();
}
return iterator(it_,end_);
} }
iterator operator++(int) { iterator operator++(int) {
iterator ret(it_); iterator ret(it_,end_);
++it_2;
if(it_2 == it_->second.end()) {
++it_; ++it_;
if(it_ != end_) // this check is important
it_2 = it_->second.begin();
}
return ret; return ret;
} }
...@@ -73,18 +96,22 @@ public: ...@@ -73,18 +96,22 @@ public:
private: private:
Storage::const_iterator it_; Storage::const_iterator it_;
HashMapCookies::const_iterator it_2;
Storage::const_iterator end_; // we need to know where main hashmap ends.
}; };
CookieJar(); CookieJar();
void add(const Cookie& cookie); void add(const Cookie& cookie);
void removeCookie(const std::string& name); // ADDED LATER
void addFromRaw(const char *str, size_t len); void addFromRaw(const char *str, size_t len);
Cookie get(const std::string& name) const; Cookie get(const std::string& name) const;
bool has(const std::string& name) const; bool has(const std::string& name) const;
iterator begin() const { iterator begin() const {
return iterator(cookies.begin()); return iterator(cookies.begin(),cookies.end());
} }
iterator end() const { iterator end() const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment