diff --git a/BUILD b/BUILD index e995c734..6d98911d 100644 --- a/BUILD +++ b/BUILD @@ -93,6 +93,7 @@ cc_library( 'src/protocol/HttpMessage.h', 'src/protocol/HttpUtil.h', 'src/protocol/http_parser.h', + 'src/factory/WFHttpServerTask.h', 'src/server/WFHttpServer.h', ], includes = [ diff --git a/CMakeLists_Headers.txt b/CMakeLists_Headers.txt index 396efcc0..22e44b33 100644 --- a/CMakeLists_Headers.txt +++ b/CMakeLists_Headers.txt @@ -90,6 +90,7 @@ set(INCLUDE_HEADERS src/factory/WFOperator.h src/factory/WFResourcePool.h src/factory/WFMessageQueue.h + src/factory/WFHttpServerTask.h src/nameservice/WFNameService.h src/nameservice/WFDnsResolver.h src/nameservice/WFServiceGovernance.h diff --git a/src/factory/HttpTaskImpl.cc b/src/factory/HttpTaskImpl.cc index 474bb12a..d44c5324 100644 --- a/src/factory/HttpTaskImpl.cc +++ b/src/factory/HttpTaskImpl.cc @@ -804,48 +804,29 @@ WFHttpTask *WFTaskFactory::create_http_task(const ParsedURI& uri, /**********Server**********/ -class WFHttpServerTask : public WFServerTask +void WFHttpServerTask::handle(int state, int error) { -public: - WFHttpServerTask(CommService *service, - std::function& process): - WFServerTask(service, WFGlobal::get_scheduler(), process), - req_is_alive_(false), - req_has_keep_alive_header_(false) - {} - -protected: - virtual void handle(int state, int error) + if (state == WFT_STATE_TOREPLY) { - if (state == WFT_STATE_TOREPLY) + req_is_alive_ = this->req.is_keep_alive(); + if (req_is_alive_ && this->req.has_keep_alive_header()) { - req_is_alive_ = this->req.is_keep_alive(); - if (req_is_alive_ && this->req.has_keep_alive_header()) - { - HttpHeaderCursor req_cursor(&this->req); - struct HttpMessageHeader header; + HttpHeaderCursor req_cursor(&this->req); + struct HttpMessageHeader header; - header.name = "Keep-Alive"; - header.name_len = strlen("Keep-Alive"); - req_has_keep_alive_header_ = req_cursor.find(&header); - if (req_has_keep_alive_header_) - { - req_keep_alive_.assign((const char *)header.value, - header.value_len); - } + header.name = "Keep-Alive"; + header.name_len = strlen("Keep-Alive"); + req_has_keep_alive_header_ = req_cursor.find(&header); + if (req_has_keep_alive_header_) + { + req_keep_alive_.assign((const char *)header.value, + header.value_len); } } - - this->WFServerTask::handle(state, error); } - virtual CommMessageOut *message_out(); - -private: - bool req_is_alive_; - bool req_has_keep_alive_header_; - std::string req_keep_alive_; -}; + this->WFServerTask::handle(state, error); +} CommMessageOut *WFHttpServerTask::message_out() { @@ -955,11 +936,3 @@ CommMessageOut *WFHttpServerTask::message_out() return this->WFServerTask::message_out(); } -/**********Server Factory**********/ - -WFHttpTask *WFServerTaskFactory::create_http_task(CommService *service, - std::function& process) -{ - return new WFHttpServerTask(service, process); -} - diff --git a/src/factory/MySQLTaskImpl.cc b/src/factory/MySQLTaskImpl.cc index 9ae42458..2c74fe11 100644 --- a/src/factory/MySQLTaskImpl.cc +++ b/src/factory/MySQLTaskImpl.cc @@ -796,8 +796,8 @@ class WFMySQLServerTask : public WFServerTask { public: WFMySQLServerTask(CommService *service, - std::function& process): - WFServerTask(service, WFGlobal::get_scheduler(), process) + std::function& proc): + WFServerTask(service, WFGlobal::get_scheduler(), proc) {} protected: diff --git a/src/factory/WFHttpServerTask.h b/src/factory/WFHttpServerTask.h new file mode 100644 index 00000000..67eedb6e --- /dev/null +++ b/src/factory/WFHttpServerTask.h @@ -0,0 +1,50 @@ +/* + Copyright (c) 2023 Sogou, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Authors: Xie Han (xiehan@sogou-inc.com) +*/ + +#ifndef _WFHTTPSERVERTASK_H_ +#define _WFHTTPSERVERTASK_H_ + +#include "HttpMessage.h" +#include "WFTask.h" +#include "WFGlobal.h" + +class WFHttpServerTask : public WFServerTask +{ +private: + using TASK = WFNetworkTask; + +public: + WFHttpServerTask(CommService *service, std::function& proc) : + WFServerTask(service, WFGlobal::get_scheduler(), proc), + req_is_alive_(false), + req_has_keep_alive_header_(false) + {} + +protected: + virtual void handle(int state, int error); + virtual CommMessageOut *message_out(); + +private: + bool req_is_alive_; + bool req_has_keep_alive_header_; + std::string req_keep_alive_; +}; + +#endif + diff --git a/src/factory/WFTaskFactory.inl b/src/factory/WFTaskFactory.inl index 479c19f5..666ee118 100644 --- a/src/factory/WFTaskFactory.inl +++ b/src/factory/WFTaskFactory.inl @@ -37,6 +37,7 @@ #include "WFTaskError.h" #include "EndpointParams.h" #include "WFNameService.h" +#include "WFHttpServerTask.h" class __WFDynamicTask : public WFDynamicTask { @@ -545,10 +546,9 @@ WFNetworkTaskFactory::create_client_task(TransportType type, template WFNetworkTask * WFNetworkTaskFactory::create_server_task(CommService *service, - std::function *)>& process) + std::function *)>& proc) { - return new WFServerTask(service, WFGlobal::get_scheduler(), - process); + return new WFServerTask(service, WFGlobal::get_scheduler(), proc); } /**********Server Factory**********/ @@ -557,9 +557,13 @@ class WFServerTaskFactory { public: static WFHttpTask *create_http_task(CommService *service, - std::function& process); + std::function& proc) + { + return new WFHttpServerTask(service, proc); + } + static WFMySQLTask *create_mysql_task(CommService *service, - std::function& process); + std::function& proc); }; /************Go Task Factory************/