nixio: Finetuning of TLS-support
[project/luci.git] / libs / httpclient / luasrc / httpclient.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require "nixio.util"
16 local nixio = require "nixio"
17
18 local ltn12 = require "luci.ltn12"
19 local util = require "luci.util"
20 local table = require "table"
21 local http = require "luci.http.protocol"
22
23 local type, pairs, tonumber, print = type, pairs, tonumber, print
24
25 module "luci.httpclient"
26
27 function chunksource(sock, buffer)
28 buffer = buffer or ""
29 return function()
30 local output
31 local _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
32 while not count and #buffer <= 1024 do
33 local newblock, code = sock:recv(1024 - #buffer)
34 if not newblock then
35 return nil, code
36 end
37 buffer = buffer .. newblock
38 _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
39 end
40 count = tonumber(count, 16)
41 if not count then
42 return nil, -1, "invalid encoding"
43 elseif count == 0 then
44 return nil
45 elseif count + 2 <= #buffer - endp then
46 output = buffer:sub(endp+1, endp+count)
47 buffer = buffer:sub(endp+count+3)
48 return output
49 else
50 output = buffer:sub(endp+1, endp+count)
51 buffer = ""
52 if count - #output > 0 then
53 local remain, code = sock:recvall(count-#output)
54 if not remain then
55 return nil, code
56 end
57 output = output .. remain
58 count, code = sock:recvall(2)
59 else
60 count, code = sock:recvall(count+2-#buffer+endp)
61 end
62 if not count then
63 return nil, code
64 end
65 return output
66 end
67 end
68 end
69
70
71 function request_to_buffer(uri, options)
72 local source, code, msg = request_to_source(uri, options)
73 local output = {}
74
75 if not source then
76 return nil, code, msg
77 end
78
79 source, code = ltn12.pump.all(source, (ltn12.sink.table(output)))
80
81 if not source then
82 return nil, code
83 end
84
85 return table.concat(output)
86 end
87
88 function request_to_source(uri, options)
89 local status, response, buffer, sock = request_raw(uri, options)
90 if not status then
91 return status, response, buffer
92 elseif status ~= 200 and status ~= 206 then
93 return nil, status, response
94 end
95
96 if response["Transfer-Encoding"] == "chunked" then
97 return chunksource(sock, buffer)
98 else
99 return ltn12.source.cat(ltn12.source.string(buffer), sock:blocksource())
100 end
101 end
102
103 --
104 -- GET HTTP-resource
105 --
106 function request_raw(uri, options)
107 options = options or {}
108 local pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)")
109 if not host then
110 return nil, -1, "unable to parse URI"
111 end
112
113 if pr ~= "http" and pr ~= "https" then
114 return nil, -2, "protocol not supported"
115 end
116
117 port = #port > 0 and port or (pr == "https" and "443" or "80")
118 path = #path > 0 and path or "/"
119
120 options.depth = options.depth or 10
121 local headers = options.headers or {}
122 local protocol = options.protocol or "HTTP/1.1"
123 local method = options.method or "GET"
124 headers["User-Agent"] = headers["User-Agent"] or "LuCI httpclient 0.1"
125
126 if headers.Connection == nil then
127 headers.Connection = "close"
128 end
129
130 local sock, code, msg = nixio.connect(host, port)
131 if not sock then
132 return nil, code, msg
133 end
134
135 sock:setsockopt("socket", "sndtimeo", options.sndtimeo or 15)
136 sock:setsockopt("socket", "rcvtimeo", options.rcvtimeo or 15)
137
138 if pr == "https" then
139 local tls = options.tls_context or nixio.tls()
140 sock = tls:create(sock)
141 local stat, code, error = sock:connect()
142 if not stat then
143 return stat, code, error
144 end
145 end
146
147 -- Pre assemble fixes
148 if protocol == "HTTP/1.1" then
149 headers.Host = headers.Host or host
150 end
151
152 if type(options.body) == "table" then
153 options.body = http.urlencode_params(options.body)
154 headers["Content-Type"] = headers["Content-Type"] or
155 "application/x-www-form-urlencoded"
156 end
157
158 if type(options.body) == "string" then
159 headers["Content-Length"] = headers["Content-Length"] or #options.body
160 end
161
162 -- Assemble message
163 local message = {method .. " " .. path .. " " .. protocol}
164
165 for k, v in pairs(headers) do
166 if v then
167 message[#message+1] = k .. ": " .. v
168 end
169 end
170 message[#message+1] = ""
171 message[#message+1] = ""
172
173 -- Send request
174 sock:sendall(table.concat(message, "\r\n"))
175
176 if type(options.body) == "string" then
177 sock:sendall(options.body)
178 end
179
180 -- Create source and fetch response
181 local linesrc = sock:linesource()
182 local line, code, error = linesrc()
183
184 if not line then
185 return nil, code, error
186 end
187
188 local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)")
189
190 if not protocol then
191 return nil, -3, "invalid response magic: " .. line
192 end
193
194 local response = {Status=line}
195
196 line = linesrc()
197 while line and line ~= "" do
198 local key, val = line:match("^([%w-]+)%s?:%s?(.*)")
199 if key and key ~= "Status" then
200 response[key] = val
201 end
202 line = linesrc()
203 end
204
205 if not line then
206 return nil, -4, "protocol error"
207 end
208
209 -- Follow
210 local code = tonumber(status)
211 if code and options.depth > 0 then
212 if code == 301 or code == 302 or code == 307 and response.Location then
213 local nexturi = response.Location
214 if not nexturi:find("https?://") then
215 nexturi = pr .. "://" .. host .. ":" .. port .. nexturi
216 end
217
218 options.depth = options.depth - 1
219 sock:close()
220
221 return request_raw(nexturi, options)
222 end
223 end
224
225 return code, response, linesrc(true), sock
226 end