From 6adc6ce9a6a0d671eaa673d333de3c9494066e08 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Tue, 19 Jun 2018 16:55:25 +0800 Subject: [PATCH] add Cookie class --- build.sh | 1 + lib/inc/drogon/Cookie.h | 44 +++++++++++++++++++++++++++++ lib/inc/drogon/HttpResponse.h | 3 ++ lib/src/Cookie.cc | 52 +++++++++++++++++++++++++++++++++++ lib/src/HttpResponseImpl.cc | 9 ++---- lib/src/HttpResponseImpl.h | 14 +++++++--- tests/CMakeLists.txt | 1 + tests/CookiesTest.cc | 14 ++++++++++ trantor | 2 +- 9 files changed, 128 insertions(+), 12 deletions(-) create mode 100755 lib/inc/drogon/Cookie.h create mode 100755 lib/src/Cookie.cc create mode 100755 tests/CookiesTest.cc diff --git a/build.sh b/build.sh index e1ce8abf..8f2e8a64 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,4 @@ +#!/bin/sh git submodule update --init mkdir build cd build diff --git a/lib/inc/drogon/Cookie.h b/lib/inc/drogon/Cookie.h new file mode 100755 index 00000000..badfd923 --- /dev/null +++ b/lib/inc/drogon/Cookie.h @@ -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 +#include + +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; + }; +} \ No newline at end of file diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index a5102629..9786048b 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include #include #include @@ -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; diff --git a/lib/src/Cookie.cc b/lib/src/Cookie.cc new file mode 100755 index 00000000..06b72dd7 --- /dev/null +++ b/lib/src/Cookie.cc @@ -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 +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; +} \ No newline at end of file diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index e1a0cc53..fea125d0 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -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"); diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h index e98cd612..964cdd62 100755 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -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 headers_; - std::map cookies_; + std::map cookies_; HttpStatusCode statusCode_; // FIXME: add http version Version v_; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a94e8507..14e41c52 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,5 @@ link_libraries(drogon trantor pthread) add_executable(cache_map_test CacheMapTest.cc) +add_executable(cookies_test CookiesTest.cc) diff --git a/tests/CookiesTest.cc b/tests/CookiesTest.cc new file mode 100755 index 00000000..7223d834 --- /dev/null +++ b/tests/CookiesTest.cc @@ -0,0 +1,14 @@ +#include +#include +int main() +{ + drogon::Cookie cookie1("test","1"); + std::cout<