http server 模块
上一章,已经介绍了http client
的模块的用法,这章主要介绍http server
模块。
server |
描述 |
JSONRPCS |
支持http,fifo, datagram |
MICROHTTPD |
依赖libmcrohttpd |
XHTTP |
提供http/1.0基础服务 |
XHTTP_PI |
通过kamailio的API提供数据库操作,依赖xhttp,libxml2 |
XHTTP_PROM |
为prometheus提供metrics数据 |
XHTTP_RPC |
通过http方式 提供rpc服务 |
JSONRPCS 模块
JSONRPCS支持三种传输方式: fifo, udp, http/https
, 依赖xhttp
.
配置参数
1
2
|
# 0,支持所有协议; 1,http;2,fifo;4,udp;8,tcp 可多个协议叠加
modparam("jsonrpcs", "transport", 1)
|
- http
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
listen=tcp:172.16.4.111:5361
tcp_accept_no_cl=yes
loadmodule "xhttp.so"
loadmodule "jsonrpcs.so"
modparam("jsonrpcs", "transport", 1)
event_route[xhttp:request] {
set_reply_close();
set_reply_no_connect();
if(src_ip!=127.0.0.1) {
xhttp_reply("403", "Forbidden", "text/html",
"<html><body>Not allowed from $si</body></html>");
exit;
}
if ($hu =~ "^/RPC") {
jsonrpc_dispatch();
exit;
}
xhttp_reply("200", "OK", "text/html",
"<html><body>Wrong URL $hu</body></html>");
exit;
}
|
监听一个tcp端口,tcp_accept_no_cl=yes
, 之后就可以往该tcp端口发送http请求了。
想要调用kamailio的内部函数,请求/RPC
路径。例如,查询注册用户数:
POST
:http://172.16.4.111:5461/RPC, body:
{ "jsonrpc":"2.0", "method":"stats.get_statistics", "params":["registered_users"], "id":"123234", "value":{ "test1":1111, "test2":"2222" } }
返回值:
{ "jsonrpc": "2.0", "result": [ "usrloc:registered_users = 1" ], "id": "123234" }
- fifo
1
2
3
|
modparam("jsonrpcs", "transport", 7)
modparam("jsonrpcs", "fifo_name", "/run/kamailio/kamailio_rpc.fifo")
modparam("jsonrpcs", "fifo_mode", 0660)
|
测试方式配置kamctlrc
文件,RPCFIFOPATH
和CTLENGINE
, 运行kamctl ul show
命令。
- udp
1
2
3
4
5
6
7
|
modparam("jsonrpcs", "transport", 4)
modparam("jsonrpcs", "dgram_socket", "/tmp/kamailio_rpc.sock")
modparam("jsonrpcs", "dgram_workers", 4)
modparam("jsonrpcs", "dgram_mode", 0600)
modparam("jsonrpcs", "dgram_group", 0)
modparam("jsonrpcs", "dgram_user", 0)
modparam("jsonrpcs", "dgram_timeout", 2000)
|
重要函数
- jsonrpc_dispatch()
用于处理jsonrpc请求, 并返回结果。
- jsonrpc_exec(cmd)
例如jsonrpc_exec("stats.get_statistics('registered_users')")
结果存储在$jsonrpl(key)
,例如$jsonrpl(code)
- the body is: $jsonrpl(body)
MICROHTTPD 模块
此MICROHTTPD依赖libmicrohttpd
库, 该库提供了http服务.
使用较为简单, 配置如下:
1
2
3
4
5
6
7
8
9
10
11
|
loadmodule "microhttpd.so"
modparam("microhttpd", "listen_addr", "127.0.0.1")
#默认8280
modparam("microhttpd", "listen_port", 8284)
event_route[microhttpd:request] {
xinfo("request: $mhttpd(method) - url: $mhttpd(url) - data: [$mhttpd(data)]\n");
mhttpd_reply("200", "OK", "text/html",
"<html><body>OK</body></html>");
}
|
这块测试post
请求带body
出现没有响应的问题, kamailio的版本是kamailio 5.8.5 (x86_64/linux)
已提bug到github。
XHTTP 模块
此模块通常和jsonrpcs
一起使用,具体的使用示例,参考jsonrpcs
模块。新增配置参数:
1
2
3
4
|
# 如果url匹配,跳过请求不处理
modparam("xhttp", "url_skip", "^/RPC2")
# 如果url匹配,处理请求
modparam("xhttp", "url_match", "^/sip/")
|
XHTTP_PI 模块
提供接口访问数据库,配置方式比较简单,请求/pi/dispatcher
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
tcp_accept_no_cl=yes
...
loadmodule "sl.so"
loadmodule "xhttp.so"
loadmodule "xhttp_pi.so"
...
modparam("xhttp_pi", "framework", "/usr/local/etc/kamailio/pi_framework.xml")
...
event_route[xhttp:request] {
$var(xhttp_root) = $(hu{s.substr,0,3});
if ($var(xhttp_root) == "/pi")
dispatch_xhttp_pi();
else
xhttp_reply("200", "OK", "text/html",
"<html><body>Wrong URL $hu</body></html>");
}
|
目前暂时未找到成功的测试示例。
XHTTP_PROM 模块
此模块依赖xhttp
模块,提供prometheus metrics数据. 此模块暂时不深入研究,后续研究监控时,再介绍。
XHTTP_RPC 模块
此模块依赖xhttp
模块,会调用rpc函数,相当于kamactl rpc xxx
配置方式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
tcp_accept_no_cl=yes
...
loadmodule "sl.so"
loadmodule "xhttp.so"
loadmodule "xhttp_rpc.so"
...
modparam("xhttp_rpc", "xhttp_rpc_root", "http_rpc")
...
event_route[xhttp:request] {
$var(xhttp_rpc_root) = $(hu{s.substr,0,9});
if ($var(xhttp_rpc_root) == "/http_rpc")
dispatch_xhttp_rpc();
else
xhttp_reply("200", "OK", "text/html",
"<html><body>Wrong URL $hu</body></html>");
}
|
Preview: