当前位置: 首页 > news >正文

workflow系列教程(5-1)HTTP Server

往期教程

如果觉得写的可以,请给一个点赞+关注支持一下

观看之前请先看,往期的博客教程,否则这篇博客没办法看懂

  • workFlow c++异步网络库编译教程与简介

  • C++异步网络库workflow入门教程(1)HTTP任务

  • C++异步网络库workflow系列教程(2)redis任务

  • workflow系列教程(3)Series串联任务流

  • workflow系列教程(4)Parallel并联任务流

创建与启动http server

本示例里,我们采用http server的默认参数。创建和启动过程非常简单。

  • process为设置的异步回调函数
  • server.start(port):中port参数表示监听的端口号
WFHttpServer server(process);
if (server.start(port) == 0)
{pause();server.stop();
}

start()接口有好几个重载函数,在WFServer.h里,可以看到如下一些接口:

class WFServerBase
{
public:/* To start TCP server. */int start(unsigned short port);int start(int family, unsigned short port);int start(const char *host, unsigned short port);int start(int family, const char *host, unsigned short port);int start(const struct sockaddr *bind_addr, socklen_t addrlen);/* To start an SSL server */int start(unsigned short port, const char *cert_file, const char *key_file);int start(int family, unsigned short port,const char *cert_file, const char *key_file);int start(const char *host, unsigned short port,const char *cert_file, const char *key_file);int start(int family, const char *host, unsigned short port,const char *cert_file, const char *key_file);int start(const struct sockaddr *bind_addr, socklen_t addrlen,const char *cert_file, const char *key_file);/* For graceful restart or multi-process server. */int serve(int listen_fd);int serve(int listen_fd, const char *cert_file, const char *key_file);/* Get the listening address. Used when started a server on a random port. */int get_listen_addr(struct sockaddr *addr, socklen_t *addrlen) const;
};

这些接口都比较好理解。任何一个start函数,当端口号为0时,将使用随机端口。此时用户可能需要在server启动完成之后通过get_listen_addr获得实际监听地址。

http echo server的业务逻辑

我们看到在构造http server的时候,传入了一个process参数,这也是一个std::function,定义如下:

using http_process_t = std::function<void (WFHttpTask *)>;
using WFHttpServer = WFServer<protocol::HttpRequest, protocol::HttpResponse>;template<>
WFHttpServer::WFServer(http_process_t proc) :WFServerBase(&HTTP_SERVER_PARAMS_DEFAULT),process(std::move(proc))
{
}

其实这个http_proccess_t和的http_callback_t类型是完全一样的。都是处理一个WFHttpTask。
对server来讲,我们的目标就是根据request,填写好response。
同样我们用一个普通函数实现process。逐条读出request的http header写入html页面。

void process(WFHttpTask *server_task)
{protocol::HttpRequest *req = server_task->get_req();protocol::HttpResponse *resp = server_task->get_resp();long seq = server_task->get_task_seq();protocol::HttpHeaderCursor cursor(req);std::string name;std::string value;char buf[8192];int len;/* Set response message body. */resp->append_output_body_nocopy("<html>", 6);len = snprintf(buf, 8192, "<p>%s %s %s</p>", req->get_method(),req->get_request_uri(), req->get_http_version());resp->append_output_body(buf, len);while (cursor.next(name, value)){len = snprintf(buf, 8192, "<p>%s: %s</p>", name.c_str(), value.c_str());resp->append_output_body(buf, len);}resp->append_output_body_nocopy("</html>", 7);/* Set status line if you like. */resp->set_http_version("HTTP/1.1");resp->set_status_code("200");resp->set_reason_phrase("OK");resp->add_header_pair("Content-Type", "text/html");resp->add_header_pair("Server", "Sogou WFHttpServer");if (seq == 9) /* no more than 10 requests on the same connection. */resp->add_header_pair("Connection", "close");struct sockaddr_in addr;socklen_t len = sizeof(addr);serverTask->get_peer_addr((sockaddr *)&addr,&len);if(addr.sin_family == AF_INET){fprintf(stderr,"sin_family:AF_INET\n");fprintf(stderr,"ip:%s\n", inet_ntoa(addr.sin_addr));fprintf(stderr,"port:%d\n",ntohs(addr.sin_port));}
}
  • set_http_version设置http版本
  • set_status_code设置状态码
  • set_reason_phrase设置响应dui’x

大多数HttpMessage相关操作之前已经介绍过了,在这里唯一的一个新操作是append_output_body()。
显然让用户生成完整的http body再传给我们并不太高效。用户只需要调用append接口,把离散的数据一块块扩展到message里就可以了。
append_output_body()操作会把数据复制走,另一个带_nocopy后缀的接口会直接引用指针,使用时需要注意不可以指向局部变量。
相关几个调用在HttpMessage.h可以看到其声明:

class HttpMessage
{
public:bool append_output_body(const void *buf, size_t size);bool append_output_body_nocopy(const void *buf, size_t size);...bool append_output_body(const std::string& buf);
};

再次强调,使用append_output_body_nocopy()接口时,buf指向的数据的生命周期至少需要延续到task的callback。
函数中另外一个变量seq,通过server_task->get_task_seq()得到,表示该请求是当前连接上的第几次请求,从0开始计。
程序中,完成10次请求之后就强行关闭连接,于是:

    if (seq == 9) /* no more than 10 requests on the same connection. */resp->add_header_pair("Connection", "close");

关闭连接还可以通过task->set_keep_alive()接口来完成,但对于http协议,还是推荐使用设置header的方式。
这个示例中,因为返回的页面很小,我们没有关注回复成功与否。

http://www.lryc.cn/news/264310.html

相关文章:

  • php-使用wangeditor实现富文本(完成图片上传)-npm
  • mysql查看数据库中所有的表的建表语句
  • 【Axure RP9】实现登入效验及实现左侧菜单栏跳转各页面
  • 76. 最小覆盖子串。优化官方题解!
  • 在国产GPU寒武纪MLU上快速上手Pytorch使用指南
  • 重生奇迹MU觉醒战士攻略
  • 美颜技术详解:深入了解视频美颜SDK的工作机制
  • 3D模型格式转换工具如何实现高性能数据转换?请看CAE系统开发实例!
  • 多级缓存:亿级流量的缓存方案
  • C语言——高精度乘法
  • 为什么C语言没有被C++所取代呢?
  • 基于Spring的枚举类+策略模式设计(以实现多种第三方支付功能为例)
  • 基于Linphone android sdk开发Android软话机
  • [论文分享]TimeDRL:多元时间序列的解纠缠表示学习
  • 分享一个好看的vs主题
  • 什么是云呼叫中心?
  • 还在用nvm?来试试更快的node版本管理工具——fnm
  • 【Hadoop精讲】HDFS详解
  • 企业需要哪些数字化管理系统?
  • 【vue】开发常见问题及解决方案
  • 飞天使-k8s知识点3-卸载yum 安装的k8s
  • ZooKeeper 集群搭建
  • Meson:现代的构建系统
  • 【大模型AIGC系列课程 5-2】视觉-语言大模型原理
  • 震惊!难怪别人家的孩子越来越聪明,原来竟是因为它
  • Linux操作系统(UMASK+SUID+SGID+STICK)
  • Java 中单例模式的常见实现方式
  • 【C语言】自定义类型之联合和枚举
  • 使用Mosquitto/python3进行MQTT连接
  • JavaWeb笔记之前端开发HTML