From b15cd86514aad2fd30dcbdeeb48c709526d0a3b8 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 16 Mar 2020 09:33:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EHTTP=20HEAD=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E7=9B=AE=E7=9A=84=E6=98=AF?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=B8=80=E4=BA=9B=E6=92=AD=E6=94=BE=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 20 ++++++++++---------- src/Http/HttpSession.h | 5 +++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 478e564e..a1bae44f 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -53,14 +53,11 @@ 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); +void HttpSession::Handle_Req_HEAD(int64_t &content_len){ + //暂时全部返回200 OK,因为HTTP GET存在按需生成流的操作,所以不能按照HTTP GET的流程返回 + //如果直接返回404,那么又会导致按需生成流的逻辑失效,所以HTTP HEAD在静态文件或者已存在资源时才有效 + //对于按需生成流的直播场景并不适用 + sendResponse("200 OK", true); } int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) { @@ -69,7 +66,7 @@ int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) { static onceToken token([]() { s_func_map.emplace("GET",&HttpSession::Handle_Req_GET); s_func_map.emplace("POST",&HttpSession::Handle_Req_POST); - s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS); + s_func_map.emplace("HEAD",&HttpSession::Handle_Req_HEAD); }, nullptr); _parser.Parse(header); @@ -268,8 +265,11 @@ bool HttpSession::checkLiveFlvStream(const function &cb){ return true; } - void HttpSession::Handle_Req_GET(int64_t &content_len) { + Handle_Req_GET_l(content_len, true); +} + +void HttpSession::Handle_Req_GET_l(int64_t &content_len, bool sendBody) { //先看看是否为WebSocket请求 if(checkWebSocket()){ content_len = -1; diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 05a9777b..072681d3 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -107,8 +107,9 @@ protected: void onWebSocketEncodeData(const Buffer::Ptr &buffer) override; private: void Handle_Req_GET(int64_t &content_len); - void Handle_Req_POST(int64_t &content_len); - void Handle_Req_OPTIONS(int64_t &content_len); + void Handle_Req_GET_l(int64_t &content_len, bool sendBody); + void Handle_Req_POST(int64_t &content_len); + void Handle_Req_HEAD(int64_t &content_len); bool checkLiveFlvStream(const function &cb = nullptr); bool checkWebSocket();