http服务器支持OPTIONS命令

This commit is contained in:
xiongziliang 2020-03-11 20:58:41 +08:00
parent 2b592780ad
commit e90319a1f2
2 changed files with 17 additions and 3 deletions

View File

@ -53,12 +53,23 @@ HttpSession::~HttpSession() {
TraceP(this); 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) { int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) {
typedef void (HttpSession::*HttpCMDHandle)(int64_t &); typedef void (HttpSession::*HttpCMDHandle)(int64_t &);
static unordered_map<string, HttpCMDHandle> s_func_map; static unordered_map<string, HttpCMDHandle> s_func_map;
static onceToken token([]() { static onceToken token([]() {
s_func_map.emplace("GET",&HttpSession::Handle_Req_GET); s_func_map.emplace("GET",&HttpSession::Handle_Req_GET);
s_func_map.emplace("POST",&HttpSession::Handle_Req_POST); s_func_map.emplace("POST",&HttpSession::Handle_Req_POST);
s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS);
}, nullptr); }, nullptr);
_parser.Parse(header); _parser.Parse(header);
@ -66,7 +77,8 @@ int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) {
string cmd = _parser.Method(); string cmd = _parser.Method();
auto it = s_func_map.find(cmd); auto it = s_func_map.find(cmd);
if (it == s_func_map.end()) { if (it == s_func_map.end()) {
sendResponse("403 Forbidden", true); WarnL << "不支持该命令:" << cmd;
sendResponse("405 Not Allowed", true);
return 0; return 0;
} }

View File

@ -108,6 +108,8 @@ protected:
private: private:
void Handle_Req_GET(int64_t &content_len); void Handle_Req_GET(int64_t &content_len);
void Handle_Req_POST(int64_t &content_len); void Handle_Req_POST(int64_t &content_len);
void Handle_Req_OPTIONS(int64_t &content_len);
bool checkLiveFlvStream(const function<void()> &cb = nullptr); bool checkLiveFlvStream(const function<void()> &cb = nullptr);
bool checkWebSocket(); bool checkWebSocket();
bool emitHttpEvent(bool doInvoke); bool emitHttpEvent(bool doInvoke);