From e90319a1f26251fc46616e6b259d2dbf5918b5d5 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 11 Mar 2020 20:58:41 +0800 Subject: [PATCH] =?UTF-8?q?http=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81OPTIONS=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 16 ++++++++++++++-- src/Http/HttpSession.h | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index d9144d11..478e564e 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -53,20 +53,32 @@ HttpSession::~HttpSession() { TraceP(this); } +void HttpSession::Handle_Req_OPTIONS(int64_t &content_len){ + KeyValue header; + header.emplace("Allow","GET, POST, OPTIONS"); + header.emplace("Access-Control-Allow-Origin","*"); + header.emplace("Access-Control-Allow-Credentials","true"); + header.emplace("Access-Control-Request-Methods","GET, POST, OPTIONS"); + header.emplace("Access-Control-Request-Headers","Accept,Accept-Language,Content-Language,Content-Type"); + sendResponse( "200 OK" , true, nullptr,header); +} + int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) { typedef void (HttpSession::*HttpCMDHandle)(int64_t &); static unordered_map s_func_map; static onceToken token([]() { s_func_map.emplace("GET",&HttpSession::Handle_Req_GET); s_func_map.emplace("POST",&HttpSession::Handle_Req_POST); - }, nullptr); + s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS); + }, nullptr); _parser.Parse(header); urlDecode(_parser); string cmd = _parser.Method(); auto it = s_func_map.find(cmd); if (it == s_func_map.end()) { - sendResponse("403 Forbidden", true); + WarnL << "不支持该命令:" << cmd; + sendResponse("405 Not Allowed", true); return 0; } diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 0d9ce18e..05a9777b 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -108,7 +108,9 @@ protected: private: void Handle_Req_GET(int64_t &content_len); void Handle_Req_POST(int64_t &content_len); - bool checkLiveFlvStream(const function &cb = nullptr); + void Handle_Req_OPTIONS(int64_t &content_len); + + bool checkLiveFlvStream(const function &cb = nullptr); bool checkWebSocket(); bool emitHttpEvent(bool doInvoke); void urlDecode(Parser &parser);