Beast 1.69.0 随笔

网络上出现频率最高的example的问题

  • 在1.69.0中没有以下的类型
  1. tcp_stream
  2. boost_front_handler

request和response body类型

  1. 首先request和response都属于message,message主要有header和body
    其中header主要就是http基本信息,如GET/POST 方法等

  2. message本身是个template,主要有一下几个类型

  • http::empty_body
    顾名思义就是没有body的message,一般用于不含有pay_load GET方法的request
    例子:
namespace http = boost::beast::http;
http::request<http::empty_body> req_;
req_.version(11);  // http1.1
req_.method(http::verb::get); // get method
req_.target("/"); // 一个URL host:port 后面的部分,如 http://127.0.0.1:80/abc/123 , 那么 target = /abc/123
req_.set(http::field::host, "127.0.0.1");
req_.set(http::field::user_agent,BOOST_BEAST_VERSION_STRING);
  • http::string_body
namespace http = boost::beast::http;
http::string_body::value_type body;
body = "some message";
http::response<http::string_body> res{http::status::not_found, 11};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.body() = std::move(body);
res.prepare_payload();
  • http::file_body
namespace http = boost::beast::http;
http::file_body::value_type body;
beast::error_code ec;
body.open("path", beast::file_mode::scan, ec);
http::response<http::file_body> res{http::status::not_found, 11};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.body() = std::move(body);
res.prepare_payload();
  • http::buffer_body
  • http::span_body
    看源码都是连续内存buffer的body

field

field主要是指http header中各种字段如content-type等
所有的field请参见beast源码中 include/boost/beast/http/field.hpp

  • 设置方法
http::request<http::string_body> req;
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.set(http::field::content_type, "text/html");
req.set(http::field::accept_encoding, "gzip, deflate");
  • 查找和获取
if (req.find(http::field::content_type) != req.end() )
{
  // find the field
  boost::string_view = req.at(http::field::content_type);
 // 或者使用[]操作符
}
  • 插入
req.insert(http::field::content_type, "text/html");
  • 删除
req.erase(http::field::content_type);
  • 清空所有
req.clear();

参考beast 1.70.0 中的chat-multi的修改(未加入websocket)

Github代码
请在listen.cpp的67行修改自己http的root文件夹目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值