1
0

Тайлбар байхгүй

hewei.it 852f697bbb optimize code 4 жил өмнө
base 22dd4abe67 update 4 жил өмнө
cert 7f2ba3920f add WITH_OPENSSL WITH_CURL 6 жил өмнө
cmake 97ab7fbb53 Done WebSocket 4 жил өмнө
consul 5ec98bae66 HV_EXPORT 5 жил өмнө
docs 97ab7fbb53 Done WebSocket 4 жил өмнө
echo-servers 4c85b7b6c3 hio_set_close_timeout 5 жил өмнө
etc 7c0817b7fc gin 5 жил өмнө
event 70d228f247 Impl WebSocketServer 4 жил өмнө
evpp 9f8e037390 fixbug: referenced local variable 4 жил өмнө
examples 80c144a7ec test wss 4 жил өмнө
html 6fe272c3e2 Add websocket_client.html 4 жил өмнө
http 852f697bbb optimize code 4 жил өмнө
misc c460e45491 update 6 жил өмнө
protocol 794194ef28 fix #41 4 жил өмнө
scripts acc25f2fa1 update 5 жил өмнө
unittest 794194ef28 fix #41 4 жил өмнө
utils f085d888f0 update 4 жил өмнө
.clang-format f085d888f0 update 4 жил өмнө
.gitignore 7272667d2e evpp: c++ classes of event 4 жил өмнө
.travis.yml a12be753c2 make evpp 4 жил өмнө
BUILD.md 92aa11a66c Add windows CI 5 жил өмнө
CMakeLists.txt e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
LICENSE 813b82ac76 add LICENSE 6 жил өмнө
Makefile 70d228f247 Impl WebSocketServer 4 жил өмнө
Makefile.in 7c339c47e2 filter-out *_test.c 4 жил өмнө
Makefile.vars 97ab7fbb53 Done WebSocket 4 жил өмнө
README.md 03bf80f037 update README 4 жил өмнө
TREE.md 7272667d2e evpp: c++ classes of event 4 жил өмнө
config.mk c54f841cbd WITH_PROTOCOL=no 5 жил өмнө
configure e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
getting_started.sh 599f087098 HttpMessage::String Data File Json 5 жил өмнө
hconfig.h e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
hconfig.h.in e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
hexport.h 27cfd65570 update 5 жил өмнө
hv.h a969840bce rm ununsed files 5 жил өмнө
readme_cn.md 03bf80f037 update README 4 жил өмнө

README.md

Build Status Platform

中文版

Intro

Like libevent, libev, and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler api and richer protocols.

Features

  • Cross-platform (Linux, Windows, Mac, Solaris)
  • EventLoop (IO, timer, idle)
  • TCP/UDP client/server
  • SSL/TLS support: WITH_OPENSSL or WITH_MBEDTLS
  • HTTP client/server (include https http1/x http2 grpc)
  • HTTP file service, indexof service, api service (support RESTful)
  • WebSocket client/server

Getting Started

run ./getting_started.sh:

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

HTTP

http server

see examples/http_server_test.cpp

#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;
}

http client

see examples/http_client_test.cpp

#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());
    }

    resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
    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());
    }

    return 0;
}

benchmark

# webbench (linux only)
make webbench
bin/webbench -c 2 -t 10 http://127.0.0.1:8080/
bin/webbench -k -c 2 -t 10 http://127.0.0.1:8080/

# sudo apt install apache2-utils
ab -c 100 -n 100000 http://127.0.0.1:8080/

# sudo apt install wrk
wrk -c 100 -t 4 -d 10s http://127.0.0.1:8080/

libhv(port:8080) vs nginx(port:80) libhv-vs-nginx.png

More examples

c version

c++ version

BUILD

see BUILD.md

lib

  • make libhv
  • sudo make install

examples

  • make examples

unittest

  • make unittest

compile options

compile WITH_OPENSSL

Enable SSL in libhv is so easy, just only two apis:

// init ssl_ctx, see base/hssl.h
hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param);

// enable ssl, see event/hloop.h
int hio_enable_ssl(hio_t* io);

https is the best example.

sudo apt install openssl libssl-dev # ubuntu
make clean
make WITH_OPENSSL=yes
# editor etc/httpd.conf => ssl = on
bin/httpd -s restart -d
bin/curl -v https://localhost:8080
curl -v https://localhost:8080 --insecure

compile WITH_CURL

make WITH_CURL=yes DEFINES="CURL_STATICLIB"

compile WITH_NGHTTP2

sudo apt install libnghttp2-dev # ubuntu
make clean
make WITH_NGHTTP2=yes
bin/httpd -d
bin/curl -v localhost:8080 --http2

other options

see config.mk

echo-servers

cd echo-servers
./build.sh
./benchmark.sh

echo-servers/benchmark
echo-servers