2 LuCI - Lua Configuration Interface
4 Copyright 2009 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
16 local nixio = require "nixio"
18 local ltn12 = require "luci.ltn12"
19 local util = require "luci.util"
20 local table = require "table"
21 local http = require "luci.http.protocol"
23 local type, pairs, tonumber, print = type, pairs, tonumber, print
25 module "luci.httpclient"
27 function chunksource(sock, buffer)
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)
37 buffer = buffer .. newblock
38 _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
40 count = tonumber(count, 16)
42 return nil, -1, "invalid encoding"
43 elseif count == 0 then
45 elseif count + 2 <= #buffer - endp then
46 output = buffer:sub(endp+1, endp+count)
47 buffer = buffer:sub(endp+count+3)
50 output = buffer:sub(endp+1, endp+count)
52 if count - #output > 0 then
53 local remain, code = sock:recvall(count-#output)
57 output = output .. remain
58 count, code = sock:recvall(2)
60 count, code = sock:recvall(count+2-#buffer+endp)
71 function request_to_buffer(uri, options)
72 local source, code, msg = request_to_source(uri, options)
79 source, code = ltn12.pump.all(source, (ltn12.sink.table(output)))
85 return table.concat(output)
88 function request_to_source(uri, options)
89 local status, response, buffer, sock = request_raw(uri, options)
91 return status, response, buffer
92 elseif status ~= 200 and status ~= 206 then
93 return nil, status, response
96 if response["Transfer-Encoding"] == "chunked" then
97 return chunksource(sock, buffer)
99 return ltn12.source.cat(ltn12.source.string(buffer), sock:blocksource())
106 function request_raw(uri, options)
107 options = options or {}
108 local pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)")
110 return nil, -1, "unable to parse URI"
114 return nil, -2, "protocol not supported"
117 port = #port > 0 and port or "80"
118 path = #path > 0 and path or "/"
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"
126 if headers.Connection == nil then
127 headers.Connection = "close"
130 local sock, code, msg = nixio.connect(host, port)
132 return nil, code, msg
135 sock:setsockopt("socket", "sndtimeo", options.sndtimeo or 15)
136 sock:setsockopt("socket", "rcvtimeo", options.rcvtimeo or 15)
138 -- Pre assemble fixes
139 if protocol == "HTTP/1.1" then
140 headers.Host = headers.Host or host
143 if type(options.body) == "table" then
144 options.body = http.urlencode_params(options.body)
145 headers["Content-Type"] = headers["Content-Type"] or
146 "application/x-www-form-urlencoded"
149 if type(options.body) == "string" then
150 headers["Content-Length"] = headers["Content-Length"] or #options.body
154 local message = {method .. " " .. path .. " " .. protocol}
156 for k, v in pairs(headers) do
158 message[#message+1] = k .. ": " .. v
161 message[#message+1] = ""
162 message[#message+1] = ""
165 sock:sendall(table.concat(message, "\r\n"))
167 if type(options.body) == "string" then
168 sock:sendall(options.body)
171 -- Create source and fetch response
172 local linesrc = sock:linesource()
173 local line, code, error = linesrc()
176 return nil, code, error
179 local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)")
182 return nil, -3, "invalid response magic: " .. line
185 local response = {Status=line}
188 while line and line ~= "" do
189 local key, val = line:match("^([%w-]+)%s?:%s?(.*)")
190 if key and key ~= "Status" then
197 return nil, -4, "protocol error"
201 local code = tonumber(status)
202 if code and options.depth > 0 then
203 if code == 301 or code == 302 or code == 307 and response.Location then
204 local nexturi = response.Location
205 if not nexturi:find("https?://") then
206 nexturi = pr .. "://" .. host .. ":" .. port .. nexturi
209 options.depth = options.depth - 1
212 return request_raw(nexturi, options)
216 return code, response, linesrc(true), sock