add Cookie class
This commit is contained in:
parent
f19fbd3dd2
commit
6adc6ce9a6
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
*
|
||||
* @file
|
||||
* @author An Tao
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a MIT license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <trantor/utils/Date.h>
|
||||
#include <string>
|
||||
|
||||
namespace drogon{
|
||||
class Cookie{
|
||||
public:
|
||||
Cookie(const std::string &key,const std::string &value):
|
||||
_key(key),
|
||||
_value(value)
|
||||
{}
|
||||
~Cookie(){}
|
||||
void setExpiresDate(const trantor::Date &date){_expiresDate=date;}
|
||||
void setHttpOnly(bool only){_httpOnly=only;}
|
||||
void setEnsure(bool ensure){_ensure=ensure;}
|
||||
void setDomain(const std::string &domain){_domain=domain;}
|
||||
void setPath(const std::string &path){_path=path;}
|
||||
const std::string cookieString() const;
|
||||
const std::string &key() const {return _key;}
|
||||
const std::string &value() const {return _value;}
|
||||
private:
|
||||
trantor::Date _expiresDate=trantor::Date();
|
||||
bool _httpOnly=true;
|
||||
bool _ensure=false;
|
||||
std::string _domain;
|
||||
std::string _path;
|
||||
std::string _key;
|
||||
std::string _value;
|
||||
};
|
||||
}
|
|
@ -27,6 +27,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <drogon/HttpViewData.h>
|
||||
#include <drogon/Cookie.h>
|
||||
#include <json/json.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
@ -99,6 +100,8 @@ namespace drogon
|
|||
|
||||
virtual void addCookie(const std::string& key, const std::string& value)=0;
|
||||
|
||||
virtual void addCookie(const Cookie &cookie)=0;
|
||||
|
||||
virtual void setBody(const std::string& body)=0;
|
||||
|
||||
virtual void setBody(std::string&& body)=0;
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
*
|
||||
* @file
|
||||
* @author An Tao
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright 2018, An Tao. All rights reserved.
|
||||
* Use of this source code is governed by a MIT license
|
||||
* that can be found in the License file.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
*/
|
||||
|
||||
#include <drogon/Cookie.h>
|
||||
using namespace drogon;
|
||||
const std::string Cookie::cookieString() const
|
||||
{
|
||||
std::string ret="Set-Cookie: ";
|
||||
ret.append(_key).append("= ").append(_value).append("; ");
|
||||
if(_expiresDate!=trantor::Date())
|
||||
{
|
||||
//add expiresDate string
|
||||
struct tm tm1=_expiresDate.tmStruct();
|
||||
char timeBuf[64];
|
||||
asctime_r(&tm1,timeBuf);
|
||||
std::string timeStr(timeBuf);
|
||||
std::string::size_type len=timeStr.length();
|
||||
timeStr =timeStr.substr(0,len-1)+" GMT";
|
||||
|
||||
ret.append("Expires= ").append(timeStr).append("; ");
|
||||
}
|
||||
if(!_domain.empty())
|
||||
{
|
||||
ret.append("Domain= ").append(_domain).append("; ");
|
||||
}
|
||||
if(!_path.empty())
|
||||
{
|
||||
ret.append("Path= ").append(_path).append("; ");
|
||||
}
|
||||
if(_ensure)
|
||||
{
|
||||
ret.append("Secure; ");
|
||||
}
|
||||
if(_httpOnly)
|
||||
{
|
||||
ret.append("HttpOnly; ");
|
||||
}
|
||||
ret.resize(ret.length()-2);//delete last semicolon
|
||||
ret.append("\r\n");
|
||||
return ret;
|
||||
}
|
|
@ -209,15 +209,10 @@ void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const
|
|||
output->append("\r\n");
|
||||
}
|
||||
if(cookies_.size() > 0) {
|
||||
output->append("Set-Cookie: ");
|
||||
for(auto it = cookies_.begin(); it != cookies_.end(); it++) {
|
||||
output->append(it->first);
|
||||
output->append("= ");
|
||||
output->append(it->second);
|
||||
output->append(";");
|
||||
|
||||
output->append(it->second.cookieString());
|
||||
}
|
||||
output->unwrite(1);//delete last ';'
|
||||
output->append("\r\n");
|
||||
}
|
||||
|
||||
output->append("\r\n");
|
||||
|
|
|
@ -150,7 +150,7 @@ namespace drogon
|
|||
cpos++;
|
||||
cookie_name=cookie_name.substr(cpos);
|
||||
std::string cookie_value = coo.substr(epos + 1);
|
||||
cookies_[cookie_name] = cookie_value;
|
||||
cookies_.insert(std::make_pair(cookie_name,Cookie(cookie_name,cookie_value)));
|
||||
}
|
||||
value=value.substr(pos+1);
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ namespace drogon
|
|||
cpos++;
|
||||
cookie_name=cookie_name.substr(cpos);
|
||||
std::string cookie_value = coo.substr(epos + 1);
|
||||
cookies_[cookie_name] = cookie_value;
|
||||
cookies_.insert(std::make_pair(cookie_name,Cookie(cookie_name,cookie_value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -173,8 +173,14 @@ namespace drogon
|
|||
|
||||
virtual void addCookie(const std::string& key, const std::string& value) override
|
||||
{
|
||||
cookies_[key] = value;
|
||||
cookies_.insert(std::make_pair(key,Cookie(key,value)));
|
||||
}
|
||||
|
||||
virtual void addCookie(const Cookie &cookie) override
|
||||
{
|
||||
cookies_.insert(std::make_pair(cookie.key(),cookie));
|
||||
}
|
||||
|
||||
virtual void setBody(const std::string& body) override
|
||||
{
|
||||
body_ = body;
|
||||
|
@ -217,7 +223,7 @@ namespace drogon
|
|||
|
||||
private:
|
||||
std::map<std::string, std::string> headers_;
|
||||
std::map<std::string, std::string> cookies_;
|
||||
std::map<std::string, Cookie> cookies_;
|
||||
HttpStatusCode statusCode_;
|
||||
// FIXME: add http version
|
||||
Version v_;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
link_libraries(drogon trantor pthread)
|
||||
|
||||
add_executable(cache_map_test CacheMapTest.cc)
|
||||
add_executable(cookies_test CookiesTest.cc)
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#include <drogon/Cookie.h>
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
drogon::Cookie cookie1("test","1");
|
||||
std::cout<<cookie1.cookieString();
|
||||
drogon::Cookie cookie2("test","2");
|
||||
cookie2.setEnsure(true);
|
||||
std::cout<<cookie2.cookieString();
|
||||
drogon::Cookie cookie3("test","3");
|
||||
cookie3.setDomain("drogon.org");
|
||||
cookie3.setExpiresDate(trantor::Date::date().after(3600*24));
|
||||
std::cout<<cookie3.cookieString();
|
||||
}
|
2
trantor
2
trantor
|
@ -1 +1 @@
|
|||
Subproject commit cda7f3d59b8d84eab4eb891bb43fc953cb1ed723
|
||||
Subproject commit cedf7790c698f3f6a844a8bf965831f69702dc42
|
Loading…
Reference in New Issue