c++编写HTTP API服务端/客户端最简单的库,没有之一

libhv是c++编写HTTP API 服务端/客户端最简单的库,没有之一

具有以下特性:

  • 跨平台(Windows, Linux, Mac)
  • 支持https
  • 支持RESTful API
  • 支持application/jsonapplication/x-www-form-urlencodedmultipart/form-data
  • 内置web service文件服务和indexof service目录服务
  • http服务端类似golanggin
  • http客户端类似pythonrequests
  • 可扩展多进程/多线程模型

libhv简介

libhv是一个类似于libevent、libev、libuv的跨平台网络库,提供了更简单的接口和更丰富的协议。

项目地址:https://github.com/ithewei/libhv.git
码云镜像:https://gitee.com/ithewei/libhv.git
QQ技术交流群:739352073
libhv博客专栏:https://hewei.blog.csdn.net/category_9866493.html
libhv源码分析:https://blog.csdn.net/qu1993/category_10637982.html

http服务端最简版

examples/http_server_test
添加好path=>handlerAPI路由,设置监听端口,调用http_server_run即可

#include "HttpServer.h"

int main() {
    HttpService router;
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
    });

    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        static char data[] = "0123456789";
        return resp->Data(data, 10);
    });

    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
    });

    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
        resp->content_type = req->content_type;
        resp->body = req->body;
        return 200;
    });

    http_server_t server;
    server.port = 8080;
    server.service = &router;
    http_server_run(&server);
    return 0;
}
g++ -std=c++11 http_server_test.cpp -o http_server_test -Iinclude/hv -Llib -lhv

http客户端最简版

examples/http_client_test
用法类似pythonrequestsrequests::request一个接口即可完成请求响应

#include "requests.h"

int main() {
    auto resp = requests::get("http://www.example.com");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    auto resp2 = requests::post("127.0.0.1:8080/echo", "hello,world!");
    if (resp2 == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp2->status_code, resp2->status_message());
        printf("%s\n", resp2->body.c_str());
    }

    return 0;
}
g++ -std=c++11 http_client_test.cpp -o http_client_test -Iinclude/hv -Llib -lhv

http服务端完整版

examples/httpd

class Router {
public:
    static void Register(HttpService& router) {
        // preprocessor => Handler => postprocessor
        router.preprocessor = Handler::preprocessor;
        router.postprocessor = Handler::postprocessor;

        // curl -v http://ip:port/ping
        router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
            return resp->String("pong");
        });

        // curl -v http://ip:port/data
        router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
            static char data[] = "0123456789";
            return resp->Data(data, 10);
        });

        // curl -v http://ip:port/html/index.html
        router.GET("/html/index.html", [&router](HttpRequest* req, HttpResponse* resp) {
            return resp->File("html/index.html");
        });

        // curl -v http://ip:port/paths
        router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
            return resp->Json(router.Paths());
        });

        // curl -v http://ip:port/echo -d "hello,world!"
        router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
            resp->content_type = req->content_type;
            resp->body = req->body;
            return 200;
        });

        // wildcard *
        // curl -v http://ip:port/wildcard/any
        router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp){
            std::string str = req->path + " match /wildcard*";
            return resp->String(str);
        });

        // curl -v http://ip:port/sleep?t=3
        router.GET("/sleep", Handler::sleep);

        // curl -v http://ip:port/query?page_no=1\&page_size=10
        router.GET("/query", Handler::query);

        // Content-Type: application/x-www-form-urlencoded
        // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
        router.POST("/kv", Handler::kv);

        // Content-Type: application/json
        // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
        router.POST("/json", Handler::json);

        // Content-Type: multipart/form-data
        // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
        router.POST("/form", Handler::form);

        // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
        // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
        // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
        router.POST("/test", Handler::test);

        // Content-Type: application/grpc
        // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
        router.POST("/grpc", Handler::grpc);

        // RESTful API: /group/:group_name/user/:user_id
        // curl -v -X DELETE http://ip:port/group/test/user/123
        router.Delete("/group/:group_name/user/:user_id", Handler::restful);

        // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
        router.POST("/upload", Handler::upload);

        // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
        router.POST("/login", Handler::login);
    }
};

完整版演示了application/jsonapplication/x-www-form-urlencodedmultipart/form-dataRESTful API多进程/多线程

赶快git clone https://github.com/ithewei/libhv.git体验吧

git clone https://github.com/ithewei/libhv.git
cd libhv
make httpd curl

bin/httpd -h
bin/httpd -d
#bin/httpd -c etc/httpd.conf -s restart -d
ps aux | grep httpd

# http web service
bin/curl -v localhost:8080

# http indexof service
bin/curl -v localhost:8080/downloads/

# http api service
bin/curl -v localhost:8080/ping
bin/curl -v localhost:8080/echo -d "hello,world!"
bin/curl -v localhost:8080/query?page_no=1\&page_size=10
bin/curl -v localhost:8080/kv   -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
bin/curl -v localhost:8080/upload -F "file=@LICENSE"

bin/curl -v localhost:8080/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
bin/curl -v localhost:8080/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
bin/curl -v localhost:8080/test -F 'bool=1 int=123 float=3.14 string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/group/test/user/123

# webbench (linux only)
make webbench
bin/webbench -c 2 -t 60 localhost:8080
ithewei CSDN认证博客专家 c/c++ Qt libhv
编程之路,其路漫漫,吾将上下而求索
https://github.com/ithewei
https://hewei.blog.csdn.net
已标记关键词 清除标记
<div style="font-size:14px;"> 课程简介 </div> <div style="font-size:14px;"> 这是一门使用Java语言,SpringBoot框架,从0开发一个RESTful API应用,接近企业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识,99%代码为手写;因为这是项目课程;所以不会深入到源码讲解某个知识点,以及原理,但会粗略的讲解下基础原理;主要是讲解如何使用系统功能,流行的第三方框架,第三方服务,完成接近企业级项目,目的是让大家,学到真正的企业级项目开发技术。 </div> <div style="font-size:14px;"> <br /> </div> <div style="font-size:14px;"> 适用人群 </div> <div style="font-size:14px;"> 刚刚毕业的学生 </div> <div style="font-size:14px;"> 想提高职场竞争力 </div> <div style="font-size:14px;"> 想学从零开发SpringBoot项目 </div> <div style="font-size:14px;"> 想提升SpringBoot项目开发技术 </div> <div style="font-size:14px;"> 想学习SpringBoot项目架构技术 </div> <div style="font-size:14px;"> 想学习企业级项目开发技术 </div> <div style="font-size:14px;"> 就是想学习SpringBoot开发 </div> <div style="font-size:14px;"> <br /> </div> <div style="font-size:14px;"> 能学到什么 </div> <div style="font-size:14px;"> 从0开发一个类似企业级项目 </div> <div style="font-size:14px;"> 学会能做出市面上90%通用API </div> <div style="font-size:14px;"> 快速增加1到2年实际开发经验 </div> <div style="font-size:14px;"> 刚毕业学完后能找到满意的工作 </div> <p style="font-size:14px;"> 已经工作学完后最高涨薪30% </p> <p style="font-size:14px;"> <br /> </p> <p style="font-size:14px;"> <br /> </p> <div style="font-size:14px;"> 课程信息 </div> <div style="font-size:14px;"> 全课程目前是82章,155小时,每节视频都经过精心剪辑。 </div> <p style="font-size:14px;"> 在线学习分辨率最高1080P </p> <p style="font-size:14px;"> <br /> </p> <p style="font-size:14px;"> <br /> </p> <div style="font-size:14px;"> 课程知识点 </div> <div style="font-size:14px;"> 1~11章:学习方法,项目架构,编码规范,Postman使用方法,Git和Github版本控制 </div> <div style="font-size:14px;"> 12~16章:搭建开发环境,快速入门SpringBoot框架 </div> <div style="font-size:14px;"> 17~20章:快速入门MySQL数据 </div> <div style="font-size:14px;"> 21~30章:MyBatis,登录注册,找回密码,发送短信,发送邮件,企业级接口配置 </div> <div style="font-size:14px;"> 31~41章:实现歌单,歌单标签,音乐,列表分页,视频,评论,好友功能 </div> <div style="font-size:14px;"> 42~48章:阿里云OSS,话题,MyBatis-plus,应用监控 </div> <div style="font-size:14px;"> 49~53章:Redis使用,集成Redis,SpringCache,HTTP缓存 </div> <div style="font-size:14px;"> 54~58章:Elasticsearch使用,集成Elasticsearch,使用ES搜索 </div> <div style="font-size:14px;"> 59~61章:商城,集成支付宝SDK,支付宝支付 </div> <div style="font-size:14px;"> 62~64章:常用哈希和加密算法,接口加密和签名 </div> <div style="font-size:14px;"> 65~67章:实时挤掉用户,企业级项目测试环境,企业级接口文档 </div> <div style="font-size:14px;"> 68~69章:SpringBoot全站HTTPS,自签证书,申请免费证书 </div> <div style="font-size:14px;"> 70~73章:云MySQL数据,云Redis数据使用,轻量级应用部署环境,域名解析 </div> <div style="font-size:14px;"> 74~80章:Docker使用,生产级Kubernetes集群,域名解析,集群全站HTTPS </div> <div style="font-size:14px;"> 81~82章:增强和重构项目,课程总结,后续学习计划 </div>
相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页