support server cookie

This commit is contained in:
pingfang 2020-03-25 17:20:32 +08:00
parent 6eb5d4732d
commit 886ad3b919
7 changed files with 180 additions and 0 deletions

View File

@ -704,6 +704,24 @@ bool ServantHandle::processDye(const TarsCurrentPtr &current, string& dyeingKey)
return false; return false;
} }
bool ServantHandle::processCookie(const TarsCurrentPtr &current, map<string, string> &cookie)
{
map<string, string>::const_iterator cookieIt = current->getContext().find(ServantProxy::STATUS_COOKIE);
if (cookieIt != current->getContext().end())
{
TLOGINFO("[TARS] cookie:" << cookieIt->second << endl);
Cookie stCookie;
stCookie.readFromJsonString(cookieIt->second);
cookie = stCookie.cookie;
return true;
}
return false;
}
bool ServantHandle::checkValidSetInvoke(const TarsCurrentPtr &current) bool ServantHandle::checkValidSetInvoke(const TarsCurrentPtr &current)
{ {
/*是否允许检查合法性*/ /*是否允许检查合法性*/
@ -809,6 +827,17 @@ void ServantHandle::handleTarsProtocol(const TarsCurrentPtr &current)
{ {
dyeSwitch.enableDyeing(dyeingKey); dyeSwitch.enableDyeing(dyeingKey);
} }
//处理cookie
string cookie;
TarsCookieOp cookieOp;
if (processCookie(current, cookie))
{
cookieOp.setCookie(cookie);
current->setCookie(cookie);
}
#ifdef _USE_OPENTRACKING #ifdef _USE_OPENTRACKING
//处理tracking信息 //处理tracking信息
processTracking(current); processTracking(current);

View File

@ -233,6 +233,9 @@ string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY";
string ServantProxy::STATUS_TRACK_KEY = "STATUS_TRACK_KEY"; string ServantProxy::STATUS_TRACK_KEY = "STATUS_TRACK_KEY";
string ServantProxy::STATUS_COOKIE = "STATUS_COOKIE";
//////////////////////////////////// ////////////////////////////////////
ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum) ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum)
: _communicator(pCommunicator) : _communicator(pCommunicator)
@ -779,6 +782,8 @@ void ServantProxy::tars_invoke_async(char cPacketType,
checkDye(msg->request); checkDye(msg->request);
checkCookie(msg->request);
invoke(msg, bCoro); invoke(msg, bCoro);
} }
@ -812,6 +817,8 @@ shared_ptr<ResponsePacket> ServantProxy::tars_invoke(char cPacketType,
checkDye(msg->request); checkDye(msg->request);
checkCookie(msg->request);
invoke(msg); invoke(msg);
shared_ptr<ResponsePacket> rsp = msg->response; shared_ptr<ResponsePacket> rsp = msg->response;
@ -977,6 +984,17 @@ void ServantProxy::checkDye(RequestPacket& req)
} }
} }
void ServantProxy::checkCookie(RequestPacket& req)
{
//线程私有数据
ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
assert(pSptd != NULL);
if(pSptd->_hasCookie)
{
req.context[ServantProxy::STATUS_COOKIE] = pSptd->_cookie;
}
}
void ServantProxy::tars_endpoints(vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint) void ServantProxy::tars_endpoints(vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
{ {

View File

@ -186,6 +186,12 @@ protected:
*/ */
bool processDye(const TarsCurrentPtr &current, string& dyeingKey); bool processDye(const TarsCurrentPtr &current, string& dyeingKey);
/**
* cookie
*
*/
bool processCookie(const TarsCurrentPtr &current, string &cookie);
/** /**
* set调用合法性 * set调用合法性
* *

View File

@ -151,6 +151,12 @@ public:
#ifdef _USE_OPENTRACKING #ifdef _USE_OPENTRACKING
std::unordered_map<std::string, std::string> _trackInfoMap; std::unordered_map<std::string, std::string> _trackInfoMap;
#endif #endif
/**
* cookie
*/
bool _hasCookie; // 是否包含cookie
map<string, string> _cookie; // cookie内容
}; };
@ -406,6 +412,8 @@ public:
static string STATUS_TRACK_KEY; //track信息 static string STATUS_TRACK_KEY; //track信息
static string STATUS_COOKIE; //cookie信息
/** /**
* *
* *
@ -716,6 +724,13 @@ private:
* @param req * @param req
*/ */
void checkDye(RequestPacket& req); void checkDye(RequestPacket& req);
/**
* cookie
* @param req
*/
void checkCookie(RequestPacket &req);
private: private:
friend class ObjectProxy; friend class ObjectProxy;
friend class AdapterProxy; friend class AdapterProxy;

View File

@ -0,0 +1,85 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#ifndef __TARS_COOKIE_H__
#define __TARS_COOKIE_H__
/**
* cookie操作类
*/
class TarsCookieOp
{
public:
/**
*
*/
TarsCookieOp()
:_hasCookie(false)
{
}
/**
* cookie
*/
~TarsCookieOp()
{
if(_hasCookie)
{
ServantProxyThreadData * td = ServantProxyThreadData::getData();
assert(NULL != td);
if (td)
{
td->_hasCookie = false;
td->_cookie.clear();
}
_hasCookie = false;
}
}
/**
* cookie
*/
static const map<string, string> & getCookie()
{
ServantProxyThreadData * td = ServantProxyThreadData::getData();
assert(NULL != td);
return td->_cookie;
}
/**
* cookie
*/
void setCookie(const map<string, string> &cookie)
{
ServantProxyThreadData * td = ServantProxyThreadData::getData();
assert(NULL != td);
if(td)
{
td->_hasCookie = true;
td->_cookie = cookie;
}
_hasCookie = true;
}
protected:
bool _hasCookie;
};
#endif

View File

@ -223,6 +223,22 @@ public:
*/ */
void sendResponse(const char* buff, uint32_t len); void sendResponse(const char* buff, uint32_t len);
/**
* cookie
*/
void setCookie(const map<string, string> &cookie)
{
_cookie = cookie;
}
/**
* cookie
*/
const map<string, string> & getCookie()
{
return _cookie;
}
protected: protected:
friend class ServantHandle; friend class ServantHandle;
@ -297,6 +313,11 @@ protected:
* *
*/ */
map<std::string, std::string> _responseContext; map<std::string, std::string> _responseContext;
/**
* cookie
*/
map<string, string> _cookie;
}; };
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
} }

View File

@ -44,4 +44,10 @@ module tars
8 optional string sResultDesc; 8 optional string sResultDesc;
9 optional map<string, string> context; 9 optional map<string, string> context;
}; };
// Cookie, 在服务调用链上透传
struct Cookie
{
1 optional map<string, string> cookie;
};
}; };