forked from armatys/hyperparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_response.lua
66 lines (59 loc) · 1.89 KB
/
test_response.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
local HP = require("ffi-hyperparser")
print("version: ", HP.version())
do
local req = "GET /index/?key=val&key2=val2 HTTP/1.1\r\nHost: www.example.com\r\nContent-Length: 12\r\n\r\nHello world!"
local parser = HP.createParser("REQUEST")
local nread, state, htbl = parser:process(req)
print(string.format("\n---- request, readed:%d, state:%d", nread, state))
for k, v in pairs(htbl) do
print(k .. ': ' .. tostring(v))
if type(v) == "table" then
for m, n in pairs(v) do
print("\t", m .. ": " .. n)
end
end
end
end
do
local res = [[HTTP/1.1 403 Forbidden
Date: Tue, 29 Jun 2021 15:12:36 GMT
Content-Type: text/html
Content-Length: 236
Connection: keep-alive
Server: web cache
Expires: Tue, 29 Jun 2021 15:12:36 GMT
X-Ser: BC22_dx-guangdong-zhuhai-16-cache-5
Cache-Control: no-cache,no-store,private
cdn-user-ip: 218.18.163.7
cdn-ip: 125.89.76.22
X-Cache-Remote: HIT
cdn-source: baishan
<html><head><title>ERROR: ACCESS DENIED</title></head><body><center><h1>ERROR: ACCESS DENIED</h1></center><hr>
<center>Tue, 29 Jun 2021 15:12:36 GMT (taikoo/BC22_dx-guangdong-zhuhai-16-cache-5)</center></BODY></HTML>
<!-- web cache -->
]]
local parser = HP.createParser("RESPONSE")
local nread, state, htbl = parser:process(res)
print(string.format("\n---- response readed:%d, state:%d", nread, state))
for k, v in pairs(htbl) do
print(k, v)
end
end
-- parse url
do
local url = "http://www.example.com:80/web/main?name=123&age=99#anchor"
local info = HP.parseURL(url)
print("\n---- req url info ----")
for k, v in pairs(info) do
print(k, v)
end
end
do
local url = "/web/index?key=val&key2=val2#anchor"
local info = HP.parseURL(url, true)
print("\n---- response url info ----")
for k, v in pairs(info) do
print(k, v)
end
end
--print("\nurl parsed:", uparser.schema, uparser.host, uparser.port, uparser.path, uparser.query, uparser.fragment, uparser.userinfo)