From 02aba328c1f1206ca07957db8c588e839af0f66a Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 9 Dec 2017 16:43:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=B7=A8=E5=9F=9F=E8=AE=BF?= =?UTF-8?q?=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 15 +++++++++++---- src/Http/HttpSession.h | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 636b88e9..72684837 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -424,19 +424,20 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){ ///////////////////是否断开本链接/////////////////////// static uint32_t reqCnt = mINI::Instance()[Config::Http::kMaxReqCount].as(); bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) && ( ++m_iReqCnt < reqCnt); + auto Origin = m_parser["Origin"]; /////////////////////异步回复Invoker/////////////////////////////// weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - HttpResponseInvoker invoker = [weakSelf,bClose](const string &codeOut, const KeyValue &headerOut, const string &contentOut){ + HttpResponseInvoker invoker = [weakSelf,bClose,Origin](const string &codeOut, const KeyValue &headerOut, const string &contentOut){ auto strongSelf = weakSelf.lock(); if(!strongSelf) { return; } - strongSelf->async([weakSelf,bClose,codeOut,headerOut,contentOut]() { + strongSelf->async([weakSelf,bClose,codeOut,headerOut,contentOut,Origin]() { auto strongSelf = weakSelf.lock(); if(!strongSelf) { return; } - strongSelf->responseDelay(bClose,codeOut,headerOut,contentOut); + strongSelf->responseDelay(Origin,bClose,codeOut,headerOut,contentOut); if(bClose){ strongSelf->shutdown(); } @@ -463,12 +464,18 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_POST() { emitHttpEvent(true); return Http_success; } -void HttpSession::responseDelay(bool bClose,const string &codeOut,const KeyValue &headerOut, const string &contentOut){ +void HttpSession::responseDelay(const string &Origin,bool bClose, + const string &codeOut,const KeyValue &headerOut, + const string &contentOut){ if(codeOut.empty()){ sendNotFound(bClose); return; } auto headerOther=makeHttpHeader(bClose,contentOut.size(),"text/plain"); + if(!Origin.empty()){ + headerOther["Access-Control-Allow-Origin"] = Origin; + headerOther["Access-Control-Allow-Credentials"] = "true"; + } const_cast(headerOut).insert(headerOther.begin(), headerOther.end()); sendResponse(codeOut.data(), headerOut, contentOut); } diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index c41edfc6..a46037a1 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -81,7 +81,9 @@ private: inline void sendNotFound(bool bClose); inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent); inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html"); - void responseDelay(bool bClose,const string &codeOut,const KeyValue &headerOut, const string &contentOut); + void responseDelay(const string &Origin,bool bClose, + const string &codeOut,const KeyValue &headerOut, + const string &contentOut); }; } /* namespace Http */