mirror of
https://gitee.com/TarsCloud/TarsCpp.git
synced 2024-12-22 22:16:38 +08:00
support server cookie
This commit is contained in:
parent
6eb5d4732d
commit
886ad3b919
@ -704,6 +704,24 @@ bool ServantHandle::processDye(const TarsCurrentPtr ¤t, string& dyeingKey)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool ServantHandle::processCookie(const TarsCurrentPtr ¤t, 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 ¤t)
|
||||
{
|
||||
/*是否允许检查合法性*/
|
||||
@ -809,6 +827,17 @@ void ServantHandle::handleTarsProtocol(const TarsCurrentPtr ¤t)
|
||||
{
|
||||
dyeSwitch.enableDyeing(dyeingKey);
|
||||
}
|
||||
|
||||
//处理cookie
|
||||
string cookie;
|
||||
TarsCookieOp cookieOp;
|
||||
if (processCookie(current, cookie))
|
||||
{
|
||||
cookieOp.setCookie(cookie);
|
||||
current->setCookie(cookie);
|
||||
}
|
||||
|
||||
|
||||
#ifdef _USE_OPENTRACKING
|
||||
//处理tracking信息
|
||||
processTracking(current);
|
||||
|
@ -233,6 +233,9 @@ string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY";
|
||||
|
||||
string ServantProxy::STATUS_TRACK_KEY = "STATUS_TRACK_KEY";
|
||||
|
||||
string ServantProxy::STATUS_COOKIE = "STATUS_COOKIE";
|
||||
|
||||
|
||||
////////////////////////////////////
|
||||
ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum)
|
||||
: _communicator(pCommunicator)
|
||||
@ -779,6 +782,8 @@ void ServantProxy::tars_invoke_async(char cPacketType,
|
||||
|
||||
checkDye(msg->request);
|
||||
|
||||
checkCookie(msg->request);
|
||||
|
||||
invoke(msg, bCoro);
|
||||
}
|
||||
|
||||
@ -812,6 +817,8 @@ shared_ptr<ResponsePacket> ServantProxy::tars_invoke(char cPacketType,
|
||||
|
||||
checkDye(msg->request);
|
||||
|
||||
checkCookie(msg->request);
|
||||
|
||||
invoke(msg);
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -186,6 +186,12 @@ protected:
|
||||
*/
|
||||
bool processDye(const TarsCurrentPtr ¤t, string& dyeingKey);
|
||||
|
||||
/**
|
||||
* 处理cookie
|
||||
*
|
||||
*/
|
||||
bool processCookie(const TarsCurrentPtr ¤t, string &cookie);
|
||||
|
||||
/**
|
||||
* 检查set调用合法性
|
||||
*
|
||||
|
@ -151,6 +151,12 @@ public:
|
||||
#ifdef _USE_OPENTRACKING
|
||||
std::unordered_map<std::string, std::string> _trackInfoMap;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* cookie
|
||||
*/
|
||||
bool _hasCookie; // 是否包含cookie
|
||||
map<string, string> _cookie; // cookie内容
|
||||
};
|
||||
|
||||
|
||||
@ -406,6 +412,8 @@ public:
|
||||
|
||||
static string STATUS_TRACK_KEY; //track信息
|
||||
|
||||
static string STATUS_COOKIE; //cookie信息
|
||||
|
||||
/**
|
||||
* 缺省的同步调用超时时间
|
||||
* 超时后不保证消息不会被服务端处理
|
||||
@ -716,6 +724,13 @@ private:
|
||||
* @param req
|
||||
*/
|
||||
void checkDye(RequestPacket& req);
|
||||
|
||||
/**
|
||||
* 检查是否需要设置cookie
|
||||
* @param req
|
||||
*/
|
||||
void checkCookie(RequestPacket &req);
|
||||
|
||||
private:
|
||||
friend class ObjectProxy;
|
||||
friend class AdapterProxy;
|
||||
|
85
servant/servant/TarsCookie.h
Normal file
85
servant/servant/TarsCookie.h
Normal 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
|
@ -223,6 +223,22 @@ public:
|
||||
*/
|
||||
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:
|
||||
|
||||
friend class ServantHandle;
|
||||
@ -297,6 +313,11 @@ protected:
|
||||
* 设置额外返回的内容
|
||||
*/
|
||||
map<std::string, std::string> _responseContext;
|
||||
|
||||
/**
|
||||
* cookie
|
||||
*/
|
||||
map<string, string> _cookie;
|
||||
};
|
||||
//////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
@ -44,4 +44,10 @@ module tars
|
||||
8 optional string sResultDesc;
|
||||
9 optional map<string, string> context;
|
||||
};
|
||||
|
||||
// Cookie, 在服务调用链上透传
|
||||
struct Cookie
|
||||
{
|
||||
1 optional map<string, string> cookie;
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user