Skip to content

Commit

Permalink
Enable users to subclass WFHttpServerTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed May 11, 2023
1 parent 9bca11b commit 2aada38
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
1 change: 1 addition & 0 deletions CMakeLists_Headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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
Expand Down
57 changes: 15 additions & 42 deletions src/factory/HttpTaskImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -1029,11 +1010,3 @@ WFHttpTask *__new_https_server_session(long long seq, CommConnection *conn,
return new WFHttpsServerTask(service, process);
}

/**********Server Factory**********/

WFHttpTask *WFServerTaskFactory::create_http_task(CommService *service,
std::function<void (WFHttpTask *)>& process)
{
return new WFHttpServerTask(service, process);
}

4 changes: 2 additions & 2 deletions src/factory/MySQLTaskImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 50 additions & 0 deletions src/factory/WFHttpServerTask.h
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
*/

#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

14 changes: 9 additions & 5 deletions src/factory/WFTaskFactory.inl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "WFTaskError.h"
#include "EndpointParams.h"
#include "WFNameService.h"
#include "WFHttpServerTask.h"

class __WFDynamicTask : public WFDynamicTask
{
Expand Down Expand Up @@ -544,10 +545,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**********/
Expand All @@ -556,9 +556,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************/
Expand Down

0 comments on commit 2aada38

Please sign in to comment.