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.mzph.cn/news/233931.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Boto3按名字搜索AWS Image并返回Image的相关参数 (Python)

文章目录 小结问题及解决参考 小结 本文记录使用Python脚本和Boto3按名字搜索AWS Image并返回AWS Image的相关参数。 问题及解决 记得操作之前拿到相应的权限&#xff1a; export AWS_ACCESS_KEY_ID"xxxxxxxxxxxxxxxxxxxxxxxxxx"export AWS_SECRET_ACCESS_KEY&qu…

《Linux C编程实战》笔记:进程操作之ID,优先级

获得进程ID getpid函数 这个函数都用了很多次了&#xff0c;看一下定义和例子就行了 #include<sys/types.h> #include <unistd.h> pid_t getpid(void); 示例程序1 #include<cstdlib> #include<malloc.h> #include<cstring> #include <cs…

Tomcat (Linux系统)详解全集

点击标题进入对应模块学习&#xff0c;你也可以完全拿捏Tomcat&#xff01; 1 Tomcat及JDK下载安装&#xff08;Linux系统&#xff09; 2 Tomcat目录介绍 3 Tomcat的启动关闭及日志说明 4 完美解决Tomcat启动慢的三种方法 5 Tomcat管理功能使用 6 Tomcat主配置文件&#xff08;…

SSM整合实战(Spring、SpringMVC、MyBatis)

五、SSM整合实战 目录 一、SSM整合理解 1. 什么是SSM整合&#xff1f;2. SSM整合核心理解五连问&#xff01; 2.1 SSM整合涉及几个IoC容器&#xff1f;2.2 每个IoC容器盛放哪些组件&#xff1f;2.3 IoC容器之间是什么关系&#xff1f;2.4 需要几个配置文件和对应IoC容器关系&…

Python工程部署到Linux云服务器

安装python # 更新源 sudo yum install epel-release# 更新源 sudo yum update# 安装python3 sudo yum install python3# 验证 python3 -V安装pip # 安装 sudo yum install python3-pip# 升级 python3 -m pip install --upgrade pip安装virtualenv、virtualenvwrapper sudo …

Ubuntu中常用的基本操作指令及其功能

文件和目录操作&#xff1a; ls&#xff1a;列出当前目录下的文件和子目录。cd&#xff1a;切换目录。例如&#xff0c;cd /home/user 切换到 /home/user 目录。pwd&#xff1a;显示当前工作目录。mkdir&#xff1a;创建新目录。例如&#xff0c;mkdir new_directory 创建一个名…

2.vue学习(8-13)

文章目录 8.数据绑定9.el与data的2种写法10.理解mvvm11.object.defineProperty12. 理解数据代理13 vue中的数据代理 8.数据绑定 单向数据绑定就是我们学的v-bind的方式&#xff0c;vue对象变了&#xff0c;页面才变。但是页面变了&#xff0c;vue对象不会变。 双向数据绑定需要…

时序预测 | Python实现LSTM-Attention电力需求预测

时序预测 | Python实现LSTM-Attention电力需求预测 目录 时序预测 | Python实现LSTM-Attention电力需求预测预测效果基本描述程序设计参考资料预测效果 基本描述 该数据集因其每小时的用电量数据以及 TSO 对消耗和定价的相应预测而值得注意,从而可以将预期预测与当前最先进的行…

vue 学习笔记

生命周期 1&#xff09;定义&#xff1a;vue实例从创建到销毁的过程 2&#xff09;钩子函数 2.1&#xff09;beforeCreate&#xff1a;vue实例初始化之前调用&#xff0c;这个阶段vue实例刚刚在内存中创建&#xff0c;此时data和methods这些都没初始化好。 2.2&#xff09;Cre…

【算法】红黑树

一、红黑树介绍 红黑树是一种自平衡二叉查找树&#xff0c;是在计算机科学中用到的一种数据结构&#xff0c;典型的用途是实现关联数组。 红黑树是在1972年由Rudolf Bayer发明的&#xff0c;当时被称为平衡二叉B树&#xff08;symmetric binary B-trees&#xff09;。后来&am…

6.1 接口- java核心卷1

6.1 接口 任何实现Comparable接口的类都要包含compareTo方法&#xff0c;该方法参数为Object对象&#xff0c;返回整型数值 Array类的sort方法对Employee对象排序&#xff1a; 1.Employee类实现Comparable接口 2.重写compareTo方法&#xff0c;用Double.compare&#xff08;…

Go、Python、Java、JavaScript等语言的求余(取模)计算

余数符号规则&#xff1a; Go&#xff08;%&#xff09;&#xff1a; 余数与被除数符号一致 Java&#xff08;%&#xff09;&#xff1a; 余数与被除数符号一致 JavaScript&#xff08;%&#xff09;&#xff1a; 余数与被除数符号一致 Python&#xff08;%&#xff09;…

[GDI绘图]画笔CPen

CPen类 CPen画笔是一种用来画线及绘制有形边框的工具&#xff0c;用户可以指定它的颜色及厚度&#xff0c;并且可以指定它画实线、点线或虚线。 CPen类&#xff0c;该类封装了Windows图形设备接口&#xff08;GDI&#xff09;画笔&#xff0c;主要通过构造函数来创建绘图对象…

Educational Codeforces Round 160 (Rated for Div. 2)

Educational Codeforces Round 160 (Rated for Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) A. Rating Increase 题意&#xff1a;给定一个由数字字符组成的字符串&#xff0c;且无前导零&#xff0c;将其分割成ab两部分&#xff0c;b不能有前导零&#x…

DETR 【目标检测里程碑的任务】

paper with code - DETR 标题 End-to-End Object Detection with Transformers end-to-end 意味着去掉了NMS的操作&#xff08;生成很多的预测框&#xff0c;nms 去掉冗余的预测框&#xff09;。因为有了NMS &#xff0c;所以调参&#xff0c;训练都会多了一道工序&#xff0c…

Gemini 1.0:Google推出的全新AI模型,改变生成式人工智能领域的游戏规则!

Gemini 1.0&#xff1a;Google推出的全新AI模型&#xff0c;将改变生成式人工智能领域的游戏规则&#xff01; &#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; IT杂谈 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 …

Ubuntu18.04 上通过 jihu 镜像完成 ESP-IDF 编译环境搭建流程

为了解决国内开发者从 github 克隆 esp 相关仓库慢的问题&#xff0c;已将 esp-idf 和部分重要仓库及其关联的子模块镜像到了 jihu&#xff0c;这些仓库将自动从原始仓库进行同步。此篇博客用来阐述 Ubuntu18.04 上通过 jihu 镜像完成 ESP-IDF 编译环境搭建流程。 注&#xff1…

LeetCode Hot100 51.N皇后

题目&#xff1a; 按照国际象棋的规则&#xff0c;皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。 n 皇后问题 研究的是如何将 n 个皇后放置在 nn 的棋盘上&#xff0c;并且使皇后彼此之间不能相互攻击。 给你一个整数 n &#xff0c;返回所有不同的 n 皇后问题 的…

亚马逊鲲鹏系统:引领批量自动操作买家号先进技术

亚马逊&#xff0c;作为全球最大的电商平台之一&#xff0c;其独特的自动化批量操作一直是众多我追逐的焦点。深入了解其主要使用方法&#xff0c;通过批量导入无数个买家账户&#xff0c;借助最新的反指纹技术和国外代理IP的绑定&#xff0c;可以成功规遍亚马逊市场&#xff0…

TortoiseGit通过SSH连接配置,生成SSH密钥方法

生成SSH密钥&#xff1a; Win环境下命令(git ssh key是可以自定义命名的)&#xff1a; ssh-keygen -t ed25519 -C "git ssh key" && start "" "C:\Windows\notepad.exe" "C:\Users\%username%\.ssh\id_ed25519.pub" 打开cm…