-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.lua
More file actions
211 lines (181 loc) · 4.66 KB
/
http.lua
File metadata and controls
211 lines (181 loc) · 4.66 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-- Copyright (C) 2013 Ivan Lin (ivan)
local strsub = string.sub
local tcp = ngx.socket.tcp
local insert = table.insert
local strlen = string.len
local escape_uri = ngx.escape_uri
local unescape_uri = ngx.unescape_uri
local strfind = string.find
local format = string.format
local concat = table.concat
local setmetatable = setmetatable
local error = error
local tonumber = tonumber
local type = type
module(...)
_VERSION = '0.10'
local STATE_CONNECTED = 1
local STATE_COMMAND_SENT = 2
local mt = { __index = _M }
local function parse_url(url)
local default_ports = {http = 80, ftp = 21, https = 443}
local _, _, scheme = strfind(url, "(.-)://")
local _, _, host, uri = strfind(url, "://([%a%d%.:]-)/(.*)")
if not host then
return nil, "that is not the links"
end
local _, _, host, port = strfind(host, "([%a%d%.]*):?(%d*)")
local _, len,query = strfind(uri, "%?(.*)")
port = tonumber(port) or default_ports[scheme]
local path = "/" .. uri
if _ then
path = "/" .. strsub(uri, 0, _ - 1)
end
return {scheme = scheme, host = host, port = port, path = path, query = query}
end
local function httprequest(self, url_parts, http_method, headers, body)
if type(headers) ~= "table" then
headers = {headers}
end
local url, sock = url_parts.path, self.sock
if url_parts.query then
url = url .. "?" .. url_parts.query
end
local host = url_parts.host
if (url_parts.scheme == "http" and url_parts.port ~= 80) and (url_parts.scheme == "https" and url_parts.port ~= 443) then
host = host .. ":" .. url_parts.port
end
insert(headers, 1, format("%s %s HTTP/1.0", http_method, url))
insert(headers, 3, "Host: " .. host)
insert(headers, "Accept: */*" )
insert(headers, "Accept-Language: zh-cn")
insert(headers, "Pragma: no-cache")
insert(headers, "Cache-Control: no-cache")
insert(headers, "Connection: Close")
if body then
insert(headers, "Content-length: " .. strlen(body))
end
insert(headers, "\r\n")
local bytes, err = sock:send(concat(headers, "\r\n"))
if not bytes then
return nil, err
end
local line, err, partail = sock:receive()
if not line then
return nil, err
end
local status = tonumber(strsub(line, 10, 13)) or 200
while true do
line, err, partail = sock:receive()
if line == "\r\n" or line == "" or line == "\n" then
break
end
if not line then
break
end
local _, _, key, val = strfind(line, "(%a+)%s*:%s*(%a+)")
if not key then
if key == "Location" or key == "URI" then
if not strfind(val, "://") then
local redirect = url_parts.scheme .. "://" .. url_parts.host .. ":" .. url_parts.port
if not strfind(val, "/") then
redirect = redirect .. "/" .. val
else
redirect = redirect .. val
end
return status, redirect
end
end
end
end
local str = ""
while true do2048
line, err, partail = sock:receive("*a")
if not line then
break
end
str = str .. line
end
return status, str
end
function new(self)
local sock, err = tcp()
if not sock then
return nil, err
end
return setmetatable({ sock = sock }, mt)
end
function set_timeout(self, timeout)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:settimeout(timeout)
end
function request(self, url, method, ...)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local res, err = parse_url(url)
if not res then
return nil, err
end
if res.scheme ~= "http" and res.scheme ~= "https" then
return nil, "Invalid protocol: " .. res.scheme
end
local ok, err = sock:connect(res.host, res.port)
if not ok then
return nil, "failed to connect: " .. err
end
return httprequest(self, res, method, ...)
end
function get(self, url, header)
local sock, err, result = self.sock
if not sock then
return nil, "not initialized"
end
while true do
local status, data = request(self, url, "GET", header)
if not status then
err = data
break
end
if status == 302 then
url = data
end
if status == 200 then
result = data
break
end
end
return result, err
end
function post(self, url, header, body)
local sock, err, result = self.sock
if not sock then
return nil, "not initialized"
end
while true do
local status, data = request(self, url, "POST", header, body)
if not status then
err = data
break
end
if status == 302 then
url = data
end
if status == 200 then
result = data
break
end
end
return result, err
end
function close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:close()
end