forked from tokers/lua-resty-http2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.lua
72 lines (56 loc) · 1.47 KB
/
example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- Copyright (C) Alex Zhang
package.path = "./lib/?.lua;;"
local http2 = require "resty.http2"
local arg = arg
local exit = os.exit
local error = error
local print = print
local pairs = pairs
local host = arg[1]
local port = tonumber(arg[2])
if not host or not port then
error("invalid host or port")
end
local sock = ngx.socket.tcp()
local ok, err = sock:connect(host, port)
if not ok then
print("failed to connect ", host, ":", port, ": ", err)
exit(1)
end
local headers = {
{ name = ":authority", value = "tokers.com" },
{ name = ":method", value = "GET" },
{ name = ":path", value = "/index.html" },
{ name = ":scheme", value = "http" },
{ name = "accept-encoding", value = "gzip" },
{ name = "user-agent", value = "example/client" },
}
local on_headers_reach = function(ctx, headers)
print("received HEADERS frame:")
for k, v in pairs(headers) do
print(k, ": ", v)
end
end
local on_data_reach = function(ctx, data)
print("received DATA frame:")
print(data)
end
local opts = {
ctx = sock,
recv = sock.receive,
send = sock.send,
preread_size = 1024,
max_concurrent_stream = 100,
}
local client, err = http2.new(opts)
if not client then
print("failed to create HTTP/2 client: ", err)
exit(1)
end
local ok, err = client:request(headers, nil, on_headers_reach,
on_data_reach)
if not ok then
print("client:request() failed: ", err)
exit(1)
end
sock:close()