Enable users to subclass WFHttpServerTask

This commit is contained in:
Xie Han
2023-05-11 20:47:28 +08:00
parent a22b7aca6f
commit 77a3ced876
6 changed files with 78 additions and 49 deletions
+1
View File
@@ -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 = [
+1
View File
@@ -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
+15 -42
View File
@@ -804,48 +804,29 @@ WFHttpTask *WFTaskFactory::create_http_task(const ParsedURI& uri,
/**********Server**********/
class WFHttpServerTask : public WFServerTask<HttpRequest, HttpResponse>
void WFHttpServerTask::handle(int state, int error)
{
public:
WFHttpServerTask(CommService *service,
std::function<void (WFHttpTask *)>& 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<void (WFHttpTask *)>& process)
{
return new WFHttpServerTask(service, process);
}
+2 -2
View File
@@ -796,8 +796,8 @@ class WFMySQLServerTask : public WFServerTask<MySQLRequest, MySQLResponse>
{
public:
WFMySQLServerTask(CommService *service,
std::function<void (WFMySQLTask *)>& process):
WFServerTask(service, WFGlobal::get_scheduler(), process)
std::function<void (WFMySQLTask *)>& proc):
WFServerTask(service, WFGlobal::get_scheduler(), proc)
{}
protected:
+50
View File
@@ -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<protocol::HttpRequest,
protocol::HttpResponse>
{
private:
using TASK = WFNetworkTask<protocol::HttpRequest, protocol::HttpResponse>;
public:
WFHttpServerTask(CommService *service, std::function<void (TASK *)>& 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
+9 -5
View File
@@ -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<REQ, RESP>::create_client_task(TransportType type,
template<class REQ, class RESP>
WFNetworkTask<REQ, RESP> *
WFNetworkTaskFactory<REQ, RESP>::create_server_task(CommService *service,
std::function<void (WFNetworkTask<REQ, RESP> *)>& process)
std::function<void (WFNetworkTask<REQ, RESP> *)>& proc)
{
return new WFServerTask<REQ, RESP>(service, WFGlobal::get_scheduler(),
process);
return new WFServerTask<REQ, RESP>(service, WFGlobal::get_scheduler(), proc);
}
/**********Server Factory**********/
@@ -557,9 +557,13 @@ class WFServerTaskFactory
{
public:
static WFHttpTask *create_http_task(CommService *service,
std::function<void (WFHttpTask *)>& process);
std::function<void (WFHttpTask *)>& proc)
{
return new WFHttpServerTask(service, proc);
}
static WFMySQLTask *create_mysql_task(CommService *service,
std::function<void (WFMySQLTask *)>& process);
std::function<void (WFMySQLTask *)>& proc);
};
/************Go Task Factory************/