Commit 2068c873 by xiongziliang

完善跨域支持

parent d01fc3a3
...@@ -134,6 +134,9 @@ int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) { ...@@ -134,6 +134,9 @@ int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) {
return 0; return 0;
} }
//跨域
_origin = _parser["Origin"];
//默认后面数据不是content而是header //默认后面数据不是content而是header
int64_t content_len = 0; int64_t content_len = 0;
auto &fun = it->second; auto &fun = it->second;
...@@ -775,6 +778,11 @@ inline HttpSession::KeyValue HttpSession::makeHttpHeader(bool bClose, int64_t iC ...@@ -775,6 +778,11 @@ inline HttpSession::KeyValue HttpSession::makeHttpHeader(bool bClose, int64_t iC
if(iContentSize > 0){ if(iContentSize > 0){
headerOut.emplace("Content-Length", StrPrinter<<iContentSize<<endl); headerOut.emplace("Content-Length", StrPrinter<<iContentSize<<endl);
} }
if(!_origin.empty()){
headerOut.emplace("Access-Control-Allow-Origin",_origin);
headerOut.emplace("Access-Control-Allow-Credentials", "true");
}
return headerOut; return headerOut;
} }
...@@ -802,20 +810,19 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){ ...@@ -802,20 +810,19 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount); GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt); bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
auto Origin = _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,Origin](const string &codeOut, const KeyValue &headerOut, const string &contentOut){ HttpResponseInvoker invoker = [weakSelf,bClose](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,Origin]() { strongSelf->async([weakSelf,bClose,codeOut,headerOut,contentOut]() {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {
return; return;
} }
strongSelf->responseDelay(Origin,bClose,codeOut,headerOut,contentOut); strongSelf->responseDelay(bClose,codeOut,headerOut,contentOut);
if(bClose){ if(bClose){
strongSelf->shutdown(SockException(Err_shutdown,"Connection: close")); strongSelf->shutdown(SockException(Err_shutdown,"Connection: close"));
} }
...@@ -906,19 +913,15 @@ inline void HttpSession::Handle_Req_POST(int64_t &content_len) { ...@@ -906,19 +913,15 @@ inline void HttpSession::Handle_Req_POST(int64_t &content_len) {
} }
//有后续content数据要处理,暂时不关闭连接 //有后续content数据要处理,暂时不关闭连接
} }
void HttpSession::responseDelay(const string &Origin,bool bClose, void HttpSession::responseDelay(bool bClose,
const string &codeOut,const KeyValue &headerOut, const string &codeOut,
const KeyValue &headerOut,
const string &contentOut){ 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";
}
for (auto &pr : headerOther){ for (auto &pr : headerOther){
//添加默认http头,默认http头不能覆盖用户自定义的头 //添加默认http头,默认http头不能覆盖用户自定义的头
const_cast<KeyValue &>(headerOut).emplace(pr.first,pr.second); const_cast<KeyValue &>(headerOut).emplace(pr.first,pr.second);
......
...@@ -112,8 +112,7 @@ private: ...@@ -112,8 +112,7 @@ 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(const string &Origin, void responseDelay(bool bClose,
bool bClose,
const string &codeOut, const string &codeOut,
const KeyValue &headerOut, const KeyValue &headerOut,
const string &contentOut); const string &contentOut);
...@@ -139,6 +138,7 @@ private: ...@@ -139,6 +138,7 @@ private:
*/ */
inline string getClientUid(); inline string getClientUid();
private: private:
string _origin;
Parser _parser; Parser _parser;
Ticker _ticker; Ticker _ticker;
uint32_t _iReqCnt = 0; uint32_t _iReqCnt = 0;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论