C++ 能够响应Http 请求的开源库有哪些?

如题,最好支持windows 和 Linux。 如果没有,是不是应该在程序中建立Server处理远程发来的http请求?
关注者
124
被浏览
295,532

13 个回答

c++ web framework 很少

随着 c++ 热度升温,c++ 在人工智能 自然语言处理 加快应用。

最近一款国产 paozhu c++ web framework 问世

写业务速度跟脚步语言一样速度,主要是写业务代码优雅,方便 后面附 CURD 例子

  1. 自带 json 内置 decode encode 支持,支持标准 json 输出,支持 emoji 编码
  2. 支持多域名网站
  3. 支持多域名 ssl 服务端
  4. 支持 http1.1、http2 协议
  5. 支持 websocket 服务端,
  6. 框架自带 websocket 推送,支持定时推送到 webscoket 客户端
  7. 支持同步 httpclient get post
  8. 框架自带 ORM, 使用链接池方式,目前支持 mysql
  9. 框架自带线程池,和用户代码运行的线程池
  10. 框架使用 asio 自带的协程
  11. 框架特色是 I/O 使用协程池 运行使用线程池
  12. 框架支持普通文件 gzip br
  13. 框架解析 URL 和 POST, 解析结果类似 PHP GET POST 方式获取内容
  14. 集成 sendmail
  15. 生成二维码 (qrcode), 需要 gd、qrencode 库

目前支持mac linux

具体可以看官方github



#include "orm.h"
#include <chrono>
#include <thread>
#include "md5.h"
#include "func.h"
#include "httppeer.h"
#include "testcurd.h"
namespace http
   std::string articlelogin(std::shared_ptr<httppeer> peer)
      // step1  show login page
      peer->view("login/login");
      return "";
   std::string articleloginpost(std::shared_ptr<httppeer> peer)
      // step2
      // get login/login post field
      httppeer &client = peer->getpeer();
      std::string username = client.post["username"].to_string();
      std::string password = client.post["password"].to_string();
      auto users = orm::cms::User();
      std::string md5string;
         md5string = md5(password);
         users.where("name=", username).whereAnd("password=", md5string).limit(1).fetch();
         // view orm create sql
         // client<<"<p>"<<users.sqlstring<<"</p>";
         if (users.getUserid() > 0)
            // save session,other page get  int userid= client.session["userid"].to_int();
            client.session["userid"] = users.getUserid();
            client.save_session();
            client.goto_url("/cms/list");
            return "";
            client.goto_url("/cms/login",3,"用户名或密码错误!");
            return "";
      catch (std::exception &e)
         client << "<p>" << e.what() << "</p>";
         return "";
      return "";
   std::string articlelist(std::shared_ptr<httppeer> peer)
      // step3 content list
      httppeer &client = peer->getpeer();
      int userid = client.session["userid"].to_int();
      if (userid == 0)
         // client.goto_url("/cms/login");
         client.val["msg"] = "<a href=\"/cms/login\">Please login </a>";
      auto articles = orm::cms::Article();
      int page = client.get["page"].to_int();
      if (page < 0)
         page = 0;
      page = page * 20;
      articles.where("isopen=1").order(" aid desc ").limit(page, 20).fetch();
      // 也可以直接返回OBJ_VALUE 对象; 不过正常业务会要处理下结果集
      // You can also return the OBJ_VALUE object directly; but normal business process will need to process the result set
      client.val["list"].set_array();
      if (articles.size() > 0)
         for (auto &bb : articles)
            OBJ_ARRAY item;
            item["aid"] = bb.aid;
            item["title"] = bb.title;
            item["createtime"] = bb.createtime;
            item["summary"] = bb.summary;
            // client<<"<p><a href=\"/cms/show?id="<<bb.aid<<"\">"<<bb.title<<"</a>  "<<bb.createtime<<" </p>";
            client.val["list"].push(std::move(item));
      peer->view("cms/list");
      return "";
   std::string articleshow(std::shared_ptr<httppeer> peer)
      // step4
      httppeer &client = peer->getpeer();
      auto articles = orm::cms::Article();
      int aid = client.get["id"].to_int();
      articles.where("isopen=1").where(" aid=", aid).limit(1).fetch();
      client.val["title"] = articles.getTitle();
      client.val["content"] = articles.getContent();
      peer->view("cms/show");
      return "";
   std::string articleedit(std::shared_ptr<httppeer> peer)
      // same the show content
      httppeer &client = peer->getpeer();
      auto articles = orm::cms::Article();
      int aid = client.get["id"].to_int();
      articles.where("isopen=1").where(" aid=", aid).limit(1).fetch();
      client.val["title"] = articles.getTitle();
      client.val["content"] = html_encode(articles.getRefContent());
      client.val["aid"] = articles.getAid();
      peer->view("cms/edit");
      return "";
   std::string articleeditpost(std::shared_ptr<httppeer> peer)
      httppeer &client = peer->getpeer();
      std::string title = client.post["title"].to_string();
      std::string content = client.post["content"].to_string();
      unsigned int aid = client.post["aid"].to_int();
      auto articles = orm::cms::Article();
      // articles.where("isopen=1").where(" aid=",aid).limit(1).fetch();
      // articles.data.aid=aid;
      // articles.data.title=title;
      // articles.setAid(aid);
      articles.setTitle(title);
      // articles.setTitle("直接标题");
      articles.setContent(content);
      articles.where(" aid=", aid);
      int effectnum = 0;
         effectnum = articles.update("title,content");
      catch (std::exception &e)
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      if (effectnum > 0)
         client.goto_url("/cms/list", 3, "内容已经更新");
         return "";
         client.goto_url("/cms/list", 3, "更新出错(error)");
         return "";
      return "";
   std::string articleadd(std::shared_ptr<httppeer> peer)
      httppeer &client = peer->getpeer();
      peer->view("cms/add");
      return "";
   std::string articleaddpost(std::shared_ptr<httppeer> peer)
      httppeer &client = peer->getpeer();
      std::string title = client.post["title"].to_string();
      std::string content = client.post["content"].to_string();
      unsigned int aid = 0;
      auto articles = orm::cms::Article();
      // articles.data.aid=aid;
      // articles.data.title=title;
      // articles.setAid(aid);
      articles.setIsopen(1);
      articles.setCreatetime(date("%Y-%m-%d %X")); // Y-m-d H:i:s
      articles.setAddtime(timeid());               // unix timestamp
      articles.setAddip(client.client_ip);    // client ip
      articles.setTitle(title);
      // articles.setTitle("直接标题");
      articles.setContent(content);
      int effectnum = 0;
         effectnum = articles.save();
         aid = articles.getAid();
         client << "<p>新(new)id " << aid << " 或 新(new)id " << effectnum << "</p>";
      catch (std::exception &e)
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      if (effectnum > 0)
         client.goto_url("/cms/list", 3, "内容已经添加");
         return "";
         client.goto_url("/cms/list", 3, "添加出错(error)");
         return "";
      return "";
   std::string articledelete(std::shared_ptr<httppeer> peer)
      httppeer &client = peer->getpeer();
      unsigned int aid = client.get["id"].to_int();
      auto articles = orm::cms::Article();
      //  可以先查询是否存在或有权限之类
      // articles.where("isopen=1").where(" aid=",aid).limit(1).fetch();
      int effectnum = 0;
         effectnum = articles.remove(aid);
      catch (std::exception &e)
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      if (effectnum > 0)
         client.goto_url("/cms/list", 3, "内容已经删除");
         return "";
         client.goto_url("/cms/list", 3, "删除出错(error)");
         return "";
      return "";
}

更看官方 controller 例子