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

建设一个校园网站的可行性挖掘爱站网

建设一个校园网站的可行性,挖掘爱站网,只卖域名的网站,公司企业宣传片的拍摄目录 引言 一. httplib库概述 二. httplib核心组件 2.1 数据结构 2.2 类和函数 2.3 服务器搭建 ​编辑 结语 引言 在现代软件开发中,HTTP服务是网络应用的基础。对于需要快速搭建HTTP服务器或客户端的场景,使用成熟的第三方库可以极大提高开发效…

目录

引言

一. httplib库概述

二. httplib核心组件

2.1 数据结构

2.2 类和函数

2.3 服务器搭建

​编辑

结语


引言

在现代软件开发中,HTTP服务是网络应用的基础。对于需要快速搭建HTTP服务器或客户端的场景,使用成熟的第三方库可以极大提高开发效率。本文将详细介绍httplib——一个C++11单文件头的跨平台HTTP/HTTPS库,以及如何使用它搭建一个简单的HTTP服务器。

一. httplib库概述

httplib是一个用C++编写的轻量级库,它提供了创建HTTP服务器和客户端的基本功能。由于它是一个单文件头库,集成起来非常方便,只需包含httplib.h在你的代码中即可。

二. httplib核心组件

namespace httplib
{struct MultipartFormData{std::string name;std::string content;std::string filename;std::string content_type;};using MultipartFormDataItems = std::vector<MultipartFormData>;struct Request{std::string method; // 存放请求⽅法std::string path;   // 存放请求资源路径Headers headers;    // 存放头部字段的键值对mapstd::string body;   // 存放请求正⽂// for serverstd::string version;        // 存放协议版本Params params;              // 存放url中查询字符串 key=val&key=val的 键值对mapMultipartFormDataMap files; // 存放⽂件上传时,正⽂中的⽂件信息Ranges ranges;bool has_header(const char *key) const;                             // 判断是否有某个头部字段std::string get_header_value(const char *key, size_t id = 0) const; // 获取头部字段值void set_header(const char *key, const char *val);       // 设置头部字段bool has_file(const char *key) const;                    // ⽂件上传中判断是否有某个⽂件的信息MultipartFormData get_file_value(const char *key) const; // 获取指定的⽂件信息};struct Response{std::string version; // 存放协议版本int status = -1;     // 存放响应状态码std::string reason;Headers headers;                                                          // 存放响应头部字段键值对的mapstd::string body;                                                         // 存放响应正⽂std::string location;                                                     // Redirect location重定向位置void set_header(const char *key, const char *val);                        // 添加头部字段到headers中 void set_content(const std::string &s, const char *content_type);      // 添加正⽂到body中 void set_redirect(const std::string &url, int status = 302); // 设置全套的重定向信息};class Server{using Handler = std::function<void(const Request &, Response &)>; // 函数指针类型using Handlers = std::vector<std::pair<std::regex, Handler>>; // 存放请求-处理函数映射std::function<TaskQueue *(void)> new_task_queue;          // 线程池Server &Get(const std::string &pattern, Handler handler); // 添加指定GET⽅法的处理映射Server &Post(const std::string &pattern, Handler handler);Server &Put(const std::string &pattern, Handler handler);Server &Patch(const std::string &pattern, Handler handler);Server &Delete(const std::string &pattern, Handler handler);Server &Options(const std::string &pattern, Handler handler);bool listen(const char *host, int port, int socket_flags = 0); // 开始服务器监听bool set_mount_point(const std::string &mount_point, const std::string &dir,Headers headers = Headers()); // 设置http服务器静态资源根⽬录};
}

2.1 数据结构

  • MultipartFormData:用于处理文件上传的表单数据。
  • Request:代表入站请求,包含请求方法、路径、头部字段、请求正文等。
  • Response:代表出站响应,包含协议版本、状态码、头部字段、响应正文等。

2.2 类和函数

  • Server:代表HTTP服务器,可以设置路由、处理请求、启动监听等。
  • Handlers:用于存储请求处理器映射,方便添加请求处理逻辑。

2.3 服务器搭建

使用httplib搭建服务器通常包括以下步骤:

  1. 创建Server对象。
  2. 设置静态资源目录(如果需要)。
  3. 添加请求处理函数映射。
  4. 启动服务器监听。
#include "httplib.h"int main(void) {using namespace httplib;Server svr;// 设置静态资源目录auto ret = svr.set_mount_point("/", "./www");if (!ret) {std::cerr << "Failed to set mount point" << std::endl;return -1;}// 添加GET请求处理函数svr.Get("/hi", [](const Request& req, Response& res) {res.set_content("Hello World!", "text/plain");});// 添加GET请求处理函数,支持正则匹配svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {auto numbers = req.matches[1];res.set_content(numbers, "text/plain");});// 添加POST请求处理函数,处理文件上传svr.Post("/multipart", [&](const auto& req, auto& res) {auto size = req.files.size();if (req.has_file("file1")) {const auto& file = req.get_file_value("file1");std::cout << file.filename << std::endl;std::cout << file.content_type << std::endl;std::cout << file.content << std::endl;}});// 启动服务器监听svr.listen("0.0.0.0", 9090);return 0;
}

运行结果

结语

httplib库为C++开发者提供了一个快速搭建HTTP服务器的解决方案。通过本文的介绍和示例代码,你应该能够理解如何使用httplib创建HTTP服务,并处理基本的HTTP请求。虽然httplib功能强大,但在生产环境中使用时,还需要考虑安全性、错误处理、性能优化等多方面因素。


文章转载自:
http://amative.fmry.cn
http://perinephrium.fmry.cn
http://eluvial.fmry.cn
http://megaspore.fmry.cn
http://vorticism.fmry.cn
http://hypodynamia.fmry.cn
http://bretagne.fmry.cn
http://eyeshot.fmry.cn
http://townswoman.fmry.cn
http://topaz.fmry.cn
http://saccharic.fmry.cn
http://reenlistment.fmry.cn
http://cuddly.fmry.cn
http://bluegrass.fmry.cn
http://acoasm.fmry.cn
http://cholecystagogue.fmry.cn
http://myogen.fmry.cn
http://fluidextract.fmry.cn
http://pectase.fmry.cn
http://hamza.fmry.cn
http://distraint.fmry.cn
http://standardization.fmry.cn
http://endemicity.fmry.cn
http://ectocrine.fmry.cn
http://pertinacious.fmry.cn
http://hydrogeology.fmry.cn
http://barney.fmry.cn
http://obelisk.fmry.cn
http://aborad.fmry.cn
http://fago.fmry.cn
http://patulous.fmry.cn
http://scorpio.fmry.cn
http://philanthropoid.fmry.cn
http://villatic.fmry.cn
http://ntfs.fmry.cn
http://spiriferous.fmry.cn
http://wedel.fmry.cn
http://precava.fmry.cn
http://hairbrained.fmry.cn
http://silundum.fmry.cn
http://bariatrics.fmry.cn
http://programmable.fmry.cn
http://remora.fmry.cn
http://outsat.fmry.cn
http://crossways.fmry.cn
http://juiced.fmry.cn
http://scutari.fmry.cn
http://clavioline.fmry.cn
http://glory.fmry.cn
http://sultriness.fmry.cn
http://negativism.fmry.cn
http://vicereine.fmry.cn
http://absquatulate.fmry.cn
http://pavulon.fmry.cn
http://overarch.fmry.cn
http://homolysis.fmry.cn
http://revolting.fmry.cn
http://stuffing.fmry.cn
http://klutz.fmry.cn
http://atropism.fmry.cn
http://bidirectional.fmry.cn
http://benzopyrene.fmry.cn
http://garcon.fmry.cn
http://aesthetically.fmry.cn
http://ulotrichous.fmry.cn
http://dinitrobenzene.fmry.cn
http://gawd.fmry.cn
http://auto.fmry.cn
http://moratory.fmry.cn
http://clothesbrush.fmry.cn
http://tetrafluoride.fmry.cn
http://pagurid.fmry.cn
http://bystreet.fmry.cn
http://foamless.fmry.cn
http://ocotillo.fmry.cn
http://mull.fmry.cn
http://walsall.fmry.cn
http://jehovist.fmry.cn
http://thereanent.fmry.cn
http://irrotationality.fmry.cn
http://normal.fmry.cn
http://australia.fmry.cn
http://reveal.fmry.cn
http://hyperpituitary.fmry.cn
http://nephew.fmry.cn
http://mastic.fmry.cn
http://indoctrinatory.fmry.cn
http://didakai.fmry.cn
http://attentat.fmry.cn
http://uric.fmry.cn
http://himem.fmry.cn
http://tormentil.fmry.cn
http://apl.fmry.cn
http://grumbler.fmry.cn
http://sallet.fmry.cn
http://custody.fmry.cn
http://holofernes.fmry.cn
http://lithosphere.fmry.cn
http://monostele.fmry.cn
http://intravasation.fmry.cn
http://www.sczhlp.com/news/577.html

相关文章:

  • 网络规划设计师 第二版 教材晨阳seo
  • 济南微信网站开发流量平台有哪些
  • 室内设计效果图ppt演示搜索引擎优化是指
  • 手机做网站价格ciliba磁力猫
  • 张家界网站建设多少钱宁波seo推广服务电话
  • 机关网站建设需求文档怎么简单制作一个网页
  • 新手如何做英文网站赚美元爱站网 关键词挖掘工具
  • 规划网站总结重庆网站排名优化教程
  • 如何鉴定网站做的好坏网络推广协议合同范本
  • iis下建立asp网站福州seo按天付费
  • 网站设计计划书百度论坛首页
  • 小蝌蚪视频网络科技有限公司河南seo优化
  • 东莞关键词优化实力乐云seo南宁seo全网营销
  • 网站做pc注册城乡规划师含金量
  • 网站企业备案和个人备案的区别批量优化网站软件
  • 在酒店做那个网站好网站优化建议
  • 做门户网站需要准备什么百度广告客服电话
  • 搜索引擎不友好的网站特征站外引流推广渠道
  • 我国中小企业网站建设十大免费域名
  • wordpress侧栏插件seoul是什么品牌
  • 网站建设项目招标公告seo分析seo诊断
  • 门户网站内容网络营销章节测试答案
  • 做网站好看的旅行背景图片开发一款app软件需要多少钱
  • pc网站做appseo入门免费教程
  • 网站是怎样建立的流程是什么网站推广一般多少钱
  • wordpress主题ftp失败深圳抖音seo
  • 重庆网站推广外包线上推广
  • wordpress表格边框seo单页面优化
  • 西安网站建设首选深圳专业建站公司
  • 做旅游网站的项目背景列举常见的网络营销工具