mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 20:47:08 +08:00
支持跨域访问
This commit is contained in:
parent
15afeb7f61
commit
02aba328c1
@ -424,19 +424,20 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){
|
|||||||
///////////////////是否断开本链接///////////////////////
|
///////////////////是否断开本链接///////////////////////
|
||||||
static uint32_t reqCnt = mINI::Instance()[Config::Http::kMaxReqCount].as<uint32_t>();
|
static uint32_t reqCnt = mINI::Instance()[Config::Http::kMaxReqCount].as<uint32_t>();
|
||||||
bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) && ( ++m_iReqCnt < reqCnt);
|
bool bClose = (strcasecmp(m_parser["Connection"].data(),"close") == 0) && ( ++m_iReqCnt < reqCnt);
|
||||||
|
auto Origin = m_parser["Origin"];
|
||||||
/////////////////////异步回复Invoker///////////////////////////////
|
/////////////////////异步回复Invoker///////////////////////////////
|
||||||
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
|
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(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();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf) {
|
if(!strongSelf) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strongSelf->async([weakSelf,bClose,codeOut,headerOut,contentOut]() {
|
strongSelf->async([weakSelf,bClose,codeOut,headerOut,contentOut,Origin]() {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf) {
|
if(!strongSelf) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strongSelf->responseDelay(bClose,codeOut,headerOut,contentOut);
|
strongSelf->responseDelay(Origin,bClose,codeOut,headerOut,contentOut);
|
||||||
if(bClose){
|
if(bClose){
|
||||||
strongSelf->shutdown();
|
strongSelf->shutdown();
|
||||||
}
|
}
|
||||||
@ -463,12 +464,18 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_POST() {
|
|||||||
emitHttpEvent(true);
|
emitHttpEvent(true);
|
||||||
return Http_success;
|
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()){
|
if(codeOut.empty()){
|
||||||
sendNotFound(bClose);
|
sendNotFound(bClose);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto headerOther=makeHttpHeader(bClose,contentOut.size(),"text/plain");
|
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<KeyValue &>(headerOut).insert(headerOther.begin(), headerOther.end());
|
const_cast<KeyValue &>(headerOut).insert(headerOther.begin(), headerOther.end());
|
||||||
sendResponse(codeOut.data(), headerOut, contentOut);
|
sendResponse(codeOut.data(), headerOut, contentOut);
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,9 @@ private:
|
|||||||
inline void sendNotFound(bool bClose);
|
inline void sendNotFound(bool bClose);
|
||||||
inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
|
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");
|
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 */
|
} /* namespace Http */
|
||||||
|
Loading…
Reference in New Issue
Block a user