Giter Site home page Giter Site logo

ithewei / libhv Goto Github PK

View Code? Open in Web Editor NEW
6.3K 134.0 1.2K 6.24 MB

🔥 比libevent/libuv/asio更易用的网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket/MQTT client/server.

Home Page: https://github.com/ithewei/libhv/wiki

License: BSD 3-Clause "New" or "Revised" License

C++ 32.20% C 58.78% Makefile 1.47% HTML 0.21% Shell 1.62% CMake 4.72% Python 0.23% Starlark 0.77%
https http-client http-server websocket websocket-client websocket-server webserver epoll curl openssl

libhv's People

Contributors

alan0526 avatar charlielven avatar cismonx avatar endingly avatar fairylovell avatar firear avatar flaribbit avatar fnmrrice avatar heheda123123 avatar huangxiaodui avatar iricbing avatar ithewei avatar lyg1949 avatar ml-haha avatar mtdxc avatar northcitychen avatar pata00 avatar peter23 avatar pleafles avatar santalex avatar seewhy-chen avatar spiderpigpig avatar src-xiaojin avatar superbigyb avatar trofi avatar waruqi avatar xfmy avatar ynkan avatar yuanfeng1995 avatar zb353027 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libhv's Issues

libhv服务端我想发完本协议后直接关闭连接要怎样实现?

tcp/http/websocket我想发完本次消息后立即关闭本连接,tcp/websocket我就是想发完本消息后就关闭掉,http我是想就做成one-shot的服务,不要有一堆的其他关闭中的状态,很耗。请问如何实现呢?libevent/libuv都有这个坑,发完直接调close会出问题,要改中间封装层或者源码才行。

Crash error

write access violation exception error
(throw exception from _Mtx_lockX)

in the following codes:

HV_MEMCHECK;

HttpService router;
http_server_t server;
server.port = 8081;
server.service = &router;

http_server_run(&server);

I have built libhv in MSVC 2017 x64.

your last commit is worse than previous commit.
I hope you upload new commit after test is done.
I will give up to use this library.

Help, I'm new!

Hi, I'm new to GitHub and I want to start helping out in some project. Could someone be kind enough to just let me know how the code is compiled into one URL and how are they connected here in Github. I understand how the pull requests, branches, issues section, and a little bit of markdown work ( because I took the intro course from the lab ), but I don't get how all this code thrown in the code tab compiles/connects as one and contributes to building the software. Final question: is github only for building websites? Or how and where is the software built here used and applied elsewhere.
If this is not a project/server that encourages new guys then I deeply apologise and appreciate your time.
also, what is the main purpose of this project?
Thanks

error occurred with WITH_NGHTTP2=yes

Hi, an error occurred when compile with 'WITH_NGHTTP2=yes', something like:

http/Http2Parser.cpp: In member function ‘virtual int Http2Parser::SubmitRequest(HttpRequest*)’:
http/Http2Parser.cpp:202:43: error: ‘class HttpRequest’ has no member named ‘https’
202 | nvs.push_back(make_nv(":scheme", req->https ? "https" : "http"));
| ^~~~~
make[1]: *** [: http/Http2Parser.o] Error 1
make[1]: Leaving directory '/path/to/libhv'
make: *** [Makefile:56: libhv] Error 2

加上-DWITH_OPENSSL=ON,使用cmake编译,提示ssl ca_file verify failed!

mkdir build

cd build

cmake .. -DWITH_OPENSSL=ON

cmake --build .

使用上面的cmake编译之后
./examples/bin/httpd
Load confile [/home/lyh/http/libhv/build/etc/httpd.conf] failed: 1030

将/etc/httpd.conf复制到该路径下可以解决

然后出现
ssl ca_file verify failed!
请问我已经打开了-DWITH_OPENSSL=ON,为什么还是显示ssl ca_file verify failed!?

我关闭-DWITH_OPENSSL=OFF的时候,能够正常启动
./examples/bin/httpd

qt project, windows platform, libhv crash

今天(2020/11/18)上午的master分支代码,编译成dll(release),引入到qt项目中,启动服务后,访问/hello崩溃。

代码如下:

`
int http_api_hello(HttpRequest* req, HttpResponse* res)
{
res->body = "hello";
return 0;
}

HttpService service;
service.AddApi("/hello", HTTP_GET, http_api_hello);

http_server_t server;
server.port = 8082;
server.service = &service;
http_server_run(&server);
`

环境:
qt creator 4.11.0
qt 5.9.9 build Desktop_Qt_5_9_9_MSVC2015_32bit
windows 10

报错:
C4_2 J$5P8T7UAH}N0BLHGI

callstack
TV_ 293 $NU%P3$EI TSN)9

Cross compile

按照Uasge提示的,使用 make all TARGET=libhv CROSS_COMPILE=arm-hisiv400-linux- 跨平台编译
编译失败
编译日志.txt

WITH_CURL compile error

使用 make WITH_CURL=yes DEFINES="CURL_STATICLIB"
之后报错

http/client/http_client.cpp: 在函数‘int __http_client_send(http_client_t*, HttpRequest*, HttpResponse*)’中:
http/client/http_client.cpp:201:60: 错误:‘CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE’在此作用域中尚未声明
         curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
                                                            ^
make[1]: *** [http/client/http_client.o] 错误 1
make[1]: 离开目录“/root/libhv”
make: *** [libhv] 错误 2

使用 cmake -DWITH_CURL=ON ..

-- Installing: /root/libhv/build/include/hv/http_client.h
CMake Error at examples/CMakeLists.txt:48 (target_link_libraries):
  Target "curl" of type EXECUTABLE may not be linked into another target.
  One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
  executables with the ENABLE_EXPORTS property set.


CMake Error at examples/CMakeLists.txt:53 (target_link_libraries):
  Target "curl" of type EXECUTABLE may not be linked into another target.
  One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
  executables with the ENABLE_EXPORTS property set.


-- Configuring incomplete, errors occurred!
See also "/root/libhv/build/CMakeFiles/CMakeOutput.log".
See also "/root/libhv/build/CMakeFiles/CMakeError.log".

windows mingw compile hsocket.c error

#hscoket.h
static inline const char* sockaddr_ip(sockaddr_u* addr, char* ip, int len) {
if (addr->sa.sa_family == AF_INET) {
return inet_ntop(AF_INET, &addr->sin.sin_addr, ip, len);
}
else if (addr->sa.sa_family == AF_INET6) {
return inet_ntop(AF_INET6, &addr->sin6.sin6_addr, ip, len);
}
return ip;
}

#hsocket.c
int Resolver(const char* host, sockaddr_u* addr) {
if (inet_pton(AF_INET, host, &addr->sin.sin_addr) == 1) {
addr->sa.sa_family = AF_INET; // host is ipv4, so easy ;)
return 0;
}

inet_ntop 返回值是 char 还是 int 呢

About benchmark

benchmark 是否能有更详细的案例? 不仅仅只有 echo 1个 benchmark

ulibc compile error

使用arm-hisiv500-linux不能编译通过 所以是ulibc的不支持是吧

utils/json.hpp: In static member function ‘static void nlohmann::detail::lexer<BasicJsonType, InputAdapterType>::strtof(float&, const char*, char**)’:
utils/json.hpp:6785:13: error: ‘strtof’ is not a member of ‘std’
f = std::strtof(str, endptr);

VS+vcpkg+cmake集成环境不能直接使用包的问题

  1. 在VS普通C/C++项目中,使用vcpkg下载安装后可以直接使用。
  2. 在VS创建的cmake项目中,不能直接使用cmake的指令find_package去添加vcpkg安装的包,需要手动用include_directories和其他的一些cmake指令去添加头文件和库才能够正常使用。

希望能简化cmake + vcpkg工程中的使用。

Add projects of VS

丢到vs里f5就能跑的项目是诱拐无知大学生的最好食材,对项目传播会有很大帮助,建议考虑一下。

How to pause/resume the read of hio?

Sometimes we do not want to receive data too fast. If delete hio_done(io) in the funtion hio_del, we can use hio_del(io,HV_READ) to pause, and use hio_read(io) to resume.

Can we pause/resume hio by this method?

Thanks.

libhv的http模块如何实现类似挂起的操作?

一个http请求到达服务A,服务A需要访问服务B获取数据后才能返回响应。假设服务B响应时间较长,在此期间服务A该怎么办?
如果阻塞等待B的回复,A就没法响应其他请求了,这不是我们想要的。
如果开多线程、多进程处理,这我感觉就没有使用IO多路复用的意义了。

window10+VS2017+cmake compile error

我在window10+VS2017+cmake平台上使用了
mkdir win64
cd win64
cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release
cmake --build .
编译,结果报错
fatal error C1083: Cannot open include file: 'stdatomic.h': No such file or directory
我需要有什么额外设置嘛?

请问对http2的支持怎么样?

你好,看了一下代码,好像包含了http2协议的解析。因为代码量不多,想问一下是已经实现了http2的全功能支持了吗?同一连接上多个stream都可以正确被处理吗?谢谢!

http_server_test coredump

在http_server_test.cpp 的ping接口中,实现N皇后算法(n=12)以模拟计算负载;
客户端使用jmeter发起压测;

客户端的请求正常结束时没有问题;
客户端的测试任务强行终止时,服务端会coredump。调用堆栈:

......
#12 0x00007ffff79608f0 in std::map<std::string, std::string, StringCaseLess, std::allocator<std::pair<std::string const, std::string> > >::clear (this=0x7ffe08000a20) at /usr/include/c++/4.8.2/bits/stl_map.h:789
#13 0x00007ffff7960072 in HttpMessage::Reset (this=0x7ffe08000a10) at /home/niuqiang/gitrepos/libhv/http/HttpMessage.h:87
#14 0x00007ffff796030a in HttpRequest::Reset (this=0x7ffe08000a10) at /home/niuqiang/gitrepos/libhv/http/HttpMessage.h:174
#15 0x00007ffff7991924 in HttpHandler::Reset (this=0x7ffe080009b0) at /home/niuqiang/gitrepos/libhv/http/server/HttpHandler.h:36
#16 0x00007ffff799094e in on_recv (io=0x7ffe08000750, _buf=0x7fff74002a50, readbytes=135) at /home/niuqiang/gitrepos/libhv/http/server/HttpServer.cpp:203
#17 0x00007ffff79570bc in __read_cb (io=0x7ffe08000750, buf=0x7fff74002a50, readbytes=135) at /home/niuqiang/gitrepos/libhv/event/nio.c:119
#18 0x00007ffff7957954 in nio_read (io=0x7ffe08000750) at /home/niuqiang/gitrepos/libhv/event/nio.c:378
#19 0x00007ffff7957b2e in hio_handle_events (io=0x7ffe08000750) at /home/niuqiang/gitrepos/libhv/event/nio.c:438
#20 0x00007ffff7954329 in hloop_process_pendings (loop=0x7fff740008c0) at /home/niuqiang/gitrepos/libhv/event/hloop.c:104
#21 0x00007ffff795454d in hloop_process_events (loop=0x7fff740008c0) at /home/niuqiang/gitrepos/libhv/event/hloop.c:160
#22 0x00007ffff7954e5a in hloop_run (loop=0x7fff740008c0) at /home/niuqiang/gitrepos/libhv/event/hloop.c:359
#23 0x00007ffff7990e6d in worker_fn (userdata=0x7fffffffd7e0) at /home/niuqiang/gitrepos/libhv/http/server/HttpServer.cpp:296
#24 0x00007ffff7694dc5 in start_thread () from /lib64/libpthread.so.0
#25 0x00007ffff679721d in clone () from /lib64/libc.so.6

工作安顿下来后,希望能一起开发。祝好~

file uploaded is compromised

I have tried to upload file by using postman or other c++ codes
file is saved into server
static int preprocessor(HttpRequest* req, HttpResponse* resp) {
req->ParseBody();

return 0;

}

router.preprocessor = preprocessor;
router.POST("/uploadReport", [&router](HttpRequest* req, HttpResponse* resp) {
req->SaveFormFile("report", "test.png");
return resp->String("pong");
});

but it is not opened.
I have compared by HEX viewer.
It has CR+LF even if original file has only LF.
Would you help me?
Best Regards

On the Windows 10 platform, the static library compiled with cmake is incorrectly linked

Windows平台
Visual Studio 15 2017
使用cmake编译出的静态库 : hv_static.lib
在尝试编译libhv_echo例子的时候报链接错误:

1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hloop_new,该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hloop_free,该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hloop_run,该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hio_setcb_read,该符号在函数 "void __cdecl on_accept(struct hio_s *)" (?on_accept@@YAXPAUhio_s@@@z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hio_setcb_close,该符号在函数 "void __cdecl on_accept(struct hio_s *)" (?on_accept@@YAXPAUhio_s@@@z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hio_read,该符号在函数 "void __cdecl on_accept(struct hio_s *)" (?on_accept@@YAXPAUhio_s@@@z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hio_write,该符号在函数 "void __cdecl on_recv(struct hio_s *,void *,int)" (?on_recv@@YAXPAUhio_s@@paxh@Z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 __imp__hloop_create_tcp_server,该符号在函数 _main 中被引用

Why not using new form of C compatibility headers ?

There are many C compatibility headers in the project , for example:

#include <assert.h> // for assert
#include <stddef.h> // for NULL
#include <stdlib.h> // for malloc,realloc,free
#include <string.h> // for memset,memmov

I'm confused why not using c prefix header, as follows :

#include <cassert> // for assert
#include <cstddef> // for NULL
#include <cstdlib> // for malloc,realloc,free
#include <cstring> // for memset,memmov

I'm a new c++ beginer , and I learn that we should use the second form, beacuse the xx.h is deprecated:

image

referece to: https://en.cppreference.com/w/cpp/header

SSL

Can SSL be supported?

Crash by memory error when getting POST multipart form data param

I have tried to get form data param from request on postman or other c++ codes

static int preprocessor(HttpRequest* req, HttpResponse* resp) {
req->ParseBody();

return 0;
}

router.preprocessor = preprocessor;
router.POST("/uploadReport", [&router](HttpRequest* req, HttpResponse* resp) {
std::string fileName = req->GetString("report_name") + ".png";
return req->SaveFormFile("report", fileName.c_str());
});

but server is crashed by memory error in line which gets fileName
Would you help me?
Best Regards

ssl

Is there any ssl client or server examples,please?

How to upgrade non-TLS connection to TLS connection?

Hi! How it would be possible with this library to upgrade non-TLS connection to TLS connection? For example MySQL and PostgreSQL would need first to negotiate with non-encrypted messages and after that start the TLS

With PostgreSQL connection the TLS initialization goes: https://www.postgresql.org/docs/8.4/protocol-flow.html. "To initiate an SSL-encrypted connection, the frontend initially sends an SSLRequest message rather than a StartupMessage. The server then responds with a single byte containing S or N, indicating that it is willing or unwilling to perform SSL, respectively."

With MySQL the case is just the same, you start with unencrypted communication: https://dev.mysql.com/doc/internals/en/ssl.html
https://dev.mysql.com/doc/internals/en/ssl-handshake.html
https://mariadb.com/kb/en/connection/

Thank you for all the help!

websocket server halt

我集成了libhv的websocket server到自己的应用里,对比windows版本的hv.dll和linux版本的libhv.so测试同样的场景,浏览器发起连接和断开连接在win10和centos7下websocket server都正常, 但当浏览器没有正常断开websocket连接,而直接跳转到其他页面的情况下,windows的websocket server工作正常,再重新用浏览器连接成功。centos7下的websocket server无法再用浏览器连接,只有重启应用服务才行。

Callback userdata

1 回调函数或者hio_t需要上下文
读完成/写完成/关闭回调函数也许应该增加一个上下文参数,以便回调函数可以对不同的hio_t进行不同的处理。或者,允许对 hio_t 设置用户数据,就像对 hloop_t 那样。至少,应该提供接口,获取 hio_t 关联到的 hloop_t ,然后再获取 hloop_t 的用户数据。
现在这样的回调函数,太简单,不方便编程。

2 读写失败时的回调问题
读写失败了,就不会调用读写完成回调函数,而是调用关闭回调函数。这太简单了,如果我需要在读写完成回调函数中进行资源释放,该怎么办?需要额外编码记住进行中的读写操作,在关闭回调函数中对没有完成的读写操作进行处理(还不知道读写失败的错误码),这增加了复杂性。

建议参考Windows完成端口的设计
(1) 每个 hio_t 可以关联一个上下文(CreateIoCompletionPort函数的第3个参数)
(2) 每个读写操作可以关联一个上下文(OVERLAPPED结构体)
(3) 无论读写是成功,还是出错完成,都要调用完成回调函数。

代码中是有 hoverlapped_s 类型的,能否开放出来,供用户使用?

我刚开始用 libhv,发现有上述问题,也许是对libhv不熟悉,使用方法错了,请指正。

FileCache::Open() BUG of reading file that contains \r\n in windows

In method FileCache::Open(): we open file int fd = open(filepath, O_RDONLY);, then read int nread = read(fd, fc->filebuf.base, fc->filebuf.len);.
BUT on windows, read() transforms \r\n to \n in TEXT mode! ref: https://docs.microsoft.com/zh-cn/cpp/c-runtime-library/reference/read?view=msvc-160
This makes nread less then fc->filebuf.len! Then server sends 404 to client at first GET and sends wrong file cache to client at next GET.

VS2019, x64

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.