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,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<string, HttpCMDHandle> 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;
}

View File

@ -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<void()> &cb = nullptr);
void Handle_Req_OPTIONS(int64_t &content_len);
bool checkLiveFlvStream(const function<void()> &cb = nullptr);
bool checkWebSocket();
bool emitHttpEvent(bool doInvoke);
void urlDecode(Parser &parser);