From 23d793c9c21c9d75d86e34e59cc76e45dd593b3d Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 13 Jun 2019 11:45:13 +0800 Subject: [PATCH] =?UTF-8?q?http=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9Acookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpClient.cpp | 53 +++++++++++++++++++---------------------- src/Http/HttpCookie.cpp | 40 +++++++++++++++---------------- src/Http/HttpCookie.h | 9 ++++++- src/Rtsp/Rtsp.h | 18 +++++++++++--- 4 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 5846349f..8a23a8b2 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -263,42 +263,39 @@ void HttpClient::onResponseCompleted_l() { void HttpClient::checkCookie(HttpClient::HttpHeader &headers) { //Set-Cookie: IPTV_SERVER=8E03927B-CC8C-4389-BC00-31DBA7EC7B49;expires=Sun, Sep 23 2018 15:07:31 GMT;path=/index/api/ - auto it_set_cookie = headers.find("Set-Cookie"); - if(it_set_cookie == headers.end()){ - return; - } - auto key_val = Parser::parseArgs(it_set_cookie->second,";","="); + for(auto it_set_cookie = headers.find("Set-Cookie") ; it_set_cookie != headers.end() ; ++it_set_cookie ){ + auto key_val = Parser::parseArgs(it_set_cookie->second,";","="); + HttpCookie::Ptr cookie = std::make_shared(); + cookie->setHost(_lastHost); - HttpCookie::Ptr cookie = std::make_shared(); - cookie->setHost(_lastHost); + int index = 0; + auto arg_vec = split(it_set_cookie->second, ";"); + for (string &key_val : arg_vec) { + auto key = FindField(key_val.data(),NULL,"="); + auto val = FindField(key_val.data(),"=", NULL); - int index = 0; - auto arg_vec = split(it_set_cookie->second, ";"); - for (string &key_val : arg_vec) { - auto key = FindField(key_val.data(),NULL,"="); - auto val = FindField(key_val.data(),"=", NULL); + if(index++ == 0){ + cookie->setKeyVal(key,val); + continue; + } - if(index++ == 0){ - cookie->setKeyVal(key,val); - continue; + if(key == "path") { + cookie->setPath(val); + continue; + } + + if(key == "expires"){ + cookie->setExpires(val,headers["Date"]); + continue; + } } - if(key == "path") { - cookie->setPath(val); - continue; - } - - if(key == "expires"){ - cookie->setExpires(val,headers["Date"]); + if(!(*cookie)){ + //无效的cookie continue; } + HttpCookieStorage::Instance().set(cookie); } - - if(!(*cookie)){ - //无效的cookie - return; - } - HttpCookieStorage::Instance().set(cookie); } diff --git a/src/Http/HttpCookie.cpp b/src/Http/HttpCookie.cpp index 4dc0ff51..b68342df 100644 --- a/src/Http/HttpCookie.cpp +++ b/src/Http/HttpCookie.cpp @@ -80,7 +80,7 @@ void HttpCookieStorage::set(const HttpCookie::Ptr &cookie) { if(!cookie || !(*cookie)){ return; } - _all_cookie[cookie->_host][cookie->_path] = cookie; + _all_cookie[cookie->_host][cookie->_path][cookie->_key] = cookie; } vector HttpCookieStorage::get(const string &host, const string &path) { @@ -88,29 +88,27 @@ vector HttpCookieStorage::get(const string &host, const string lock_guard lck(_mtx_cookie); auto it = _all_cookie.find(host); if(it == _all_cookie.end()){ + //未找到该host相关记录 return ret; } - auto &path_cookie = it->second; - - auto lam = [&](const string &sub_path){ - auto it_cookie = path_cookie.find(sub_path); - if(it_cookie != path_cookie.end()){ - if(*(it_cookie->second)){ - ret.emplace_back(it_cookie->second); - }else{ - path_cookie.erase(it_cookie); - } + //遍历该host下所有path + for(auto &pr : it->second){ + if(path.find(pr.first) != 0){ + //这个path不匹配 + continue; } - }; - - - int pos = 0; - do{ - auto sub_path = path.substr(0,pos + 1); - lam(sub_path); - pos = path.find('/',1 + pos); - }while (pos != string::npos); - lam(path); + //遍历该path下的各个cookie + for(auto it_cookie = pr.second.begin() ; it_cookie != pr.second.end() ; ){ + if(!*(it_cookie->second)){ + //该cookie已经过期,移除之 + it_cookie = pr.second.erase(it_cookie); + continue; + } + //保存有效cookie + ret.emplace_back(it_cookie->second); + ++it_cookie; + } + } return ret; } diff --git a/src/Http/HttpCookie.h b/src/Http/HttpCookie.h index 69826455..8abdc053 100644 --- a/src/Http/HttpCookie.h +++ b/src/Http/HttpCookie.h @@ -30,12 +30,16 @@ #include #include #include +#include #include #include using namespace std; namespace mediakit { +/** + * http客户端cookie对象 + */ class HttpCookie { public: typedef std::shared_ptr Ptr; @@ -60,6 +64,9 @@ private: }; +/** + * http客户端cookie全局保存器 + */ class HttpCookieStorage{ public: ~HttpCookieStorage(){} @@ -69,7 +76,7 @@ public: private: HttpCookieStorage(){}; private: - unordered_map > _all_cookie; + unordered_map > > _all_cookie; mutex _mtx_cookie; }; diff --git a/src/Rtsp/Rtsp.h b/src/Rtsp/Rtsp.h index 2c7ffb4d..8da208be 100644 --- a/src/Rtsp/Rtsp.h +++ b/src/Rtsp/Rtsp.h @@ -122,7 +122,19 @@ struct StrCaseCompare { bool operator()(const string &__x, const string &__y) const { return strcasecmp(__x.data(), __y.data()) < 0; } }; -typedef map StrCaseMap; + +class StrCaseMap : public multimap{ +public: + StrCaseMap() = default; + ~StrCaseMap() = default; + string &operator[](const string &key){ + auto it = find(key); + if(it == end()){ + it = emplace(key,""); + } + return it->second; + } +}; class Parser { public: @@ -155,7 +167,7 @@ public: auto field = FindField(line.data(), NULL, ": "); auto value = FindField(line.data(), ": ", NULL); if (field.size() != 0) { - _mapHeaders[field] = value; + _mapHeaders.emplace(field,value); } } start = start + line.size() + 2; @@ -235,7 +247,7 @@ public: for (string &key_val : arg_vec) { auto key = FindField(key_val.data(), NULL, key_delim); auto val = FindField(key_val.data(), key_delim, NULL); - ret[key] = val; + ret.emplace(key,val); } return ret; }