From 20e62b103ac9bb2b36fed7aa0185e1ccfca3bc4c Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 12 Jun 2019 18:55:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E8=BF=BD?= =?UTF-8?q?=E8=B8=AA=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/config.cpp | 1 + src/Common/config.h | 8 +++++++- src/Http/HttpSession.cpp | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 0e19df23..0789c82a 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -58,6 +58,7 @@ const string kBroadcastMediaChanged = "kBroadcastMediaChanged"; const string kBroadcastRecordMP4 = "kBroadcastRecordMP4"; const string kBroadcastHttpRequest = "kBroadcastHttpRequest"; const string kBroadcastHttpAccess = "kBroadcastHttpAccess"; +const string kBroadcastTrackHttpClient = "kBroadcastTrackHttpClient"; const string kBroadcastOnGetRtspRealm = "kBroadcastOnGetRtspRealm"; const string kBroadcastOnRtspAuth = "kBroadcastOnRtspAuth"; const string kBroadcastMediaPlayed = "kBroadcastMediaPlayed"; diff --git a/src/Common/config.h b/src/Common/config.h index f5b1c05b..709d5380 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -79,10 +79,16 @@ extern const string kBroadcastRecordMP4; extern const string kBroadcastHttpRequest; #define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,TcpSession &sender -//收到http 访问文件或目录的广播 +//收到http 访问文件或目录的广播,通过该事件控制访问http目录的权限 extern const string kBroadcastHttpAccess; #define BroadcastHttpAccessArgs const Parser &parser,const MediaInfo &args,const string &path,const bool &is_dir,const HttpSession::HttpAccessPathInvoker &invoker,TcpSession &sender +//追踪用户事件,如果http客户端不支持cookie,ip端口又一直变,那么可以根据url参数来追踪用户, +//从而减少kBroadcastHttpAccess事件触发的次数。 +extern const string kBroadcastTrackHttpClient; +#define BroadcastTrackHttpClientArgs const Parser &parser,string &uid,TcpSession &sender + + //该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证 extern const string kBroadcastOnGetRtspRealm; #define BroadcastOnGetRtspRealmArgs const MediaInfo &args,const RtspSession::onGetRealm &invoker,TcpSession &sender diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7720327e..fe7ca521 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -325,10 +325,18 @@ inline string HttpSession::getClientUid(){ //该ip端口只能有一个cookie,不能重复获取cookie, //目的是为了防止我们让客户端设置cookie,但是客户端不支持cookie导致一直重复生成cookie //判断是否为同一个用户还可以根据url相关字段,但是这个跟具体业务逻辑相关,在这里不便实现 - //如果一个http客户端不支持cookie并且一直变换端口号,那么会导致服务器无法追踪该用户,从而导致一直触发事件并且一直生成cookie - return StrPrinter << get_peer_ip() << ":" << get_peer_port(); + //如果一个http客户端不支持cookie并且一直变换端口号,那么可能会导致服务器无法追踪该用户,从而导致一直触发事件并且一直生成cookie + string uid = StrPrinter << get_peer_ip() << ":" << get_peer_port(); + //所以我们通过kBroadcastTrackHttpClient事件来让业务逻辑自行决定根据url参数追踪用户 + NoticeCenter::Instance().emitEventNoCopy(Broadcast::kBroadcastTrackHttpClient,_parser,uid,*this); + return uid; } inline void HttpSession::canAccessPath(const string &path_in,bool is_dir,const function &callback_in){ + if(NoticeCenter::Instance().listenerSize(Broadcast::kBroadcastHttpAccess) == 0){ + //该事件无人监听,那么就不做cookie查找这样费时的操作 + callback_in(true, nullptr); + return; + } auto path = path_in; replace(const_cast(path),"//","/");