| 1 | --[[ |
|---|
| 2 | LuCI - Lua Development Framework |
|---|
| 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 | local date = require "luci.http.protocol.date" |
|---|
| 23 | |
|---|
| 24 | local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber |
|---|
| 25 | |
|---|
| 26 | module "luci.httpclient" |
|---|
| 27 | |
|---|
| 28 | function chunksource(sock, buffer) |
|---|
| 29 | buffer = buffer or "" |
|---|
| 30 | return function() |
|---|
| 31 | local output |
|---|
| 32 | local _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n") |
|---|
| 33 | while not count and #buffer <= 1024 do |
|---|
| 34 | local newblock, code = sock:recv(1024 - #buffer) |
|---|
| 35 | if not newblock then |
|---|
| 36 | return nil, code |
|---|
| 37 | end |
|---|
| 38 | buffer = buffer .. newblock |
|---|
| 39 | _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n") |
|---|
| 40 | end |
|---|
| 41 | count = tonumber(count, 16) |
|---|
| 42 | if not count then |
|---|
| 43 | return nil, -1, "invalid encoding" |
|---|
| 44 | elseif count == 0 then |
|---|
| 45 | return nil |
|---|
| 46 | elseif count + 2 <= #buffer - endp then |
|---|
| 47 | output = buffer:sub(endp+1, endp+count) |
|---|
| 48 | buffer = buffer:sub(endp+count+3) |
|---|
| 49 | return output |
|---|
| 50 | else |
|---|
| 51 | output = buffer:sub(endp+1, endp+count) |
|---|
| 52 | buffer = "" |
|---|
| 53 | if count - #output > 0 then |
|---|
| 54 | local remain, code = sock:recvall(count-#output) |
|---|
| 55 | if not remain then |
|---|
| 56 | return nil, code |
|---|
| 57 | end |
|---|
| 58 | output = output .. remain |
|---|
| 59 | count, code = sock:recvall(2) |
|---|
| 60 | else |
|---|
| 61 | count, code = sock:recvall(count+2-#buffer+endp) |
|---|
| 62 | end |
|---|
| 63 | if not count then |
|---|
| 64 | return nil, code |
|---|
| 65 | end |
|---|
| 66 | return output |
|---|
| 67 | end |
|---|
| 68 | end |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | function request_to_buffer(uri, options) |
|---|
| 73 | local source, code, msg = request_to_source(uri, options) |
|---|
| 74 | local output = {} |
|---|
| 75 | |
|---|
| 76 | if not source then |
|---|
| 77 | return nil, code, msg |
|---|
| 78 | end |
|---|
| 79 | |
|---|
| 80 | source, code = ltn12.pump.all(source, (ltn12.sink.table(output))) |
|---|
| 81 | |
|---|
| 82 | if not source then |
|---|
| 83 | return nil, code |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | return table.concat(output) |
|---|
| 87 | end |
|---|
| 88 | |
|---|
| 89 | function request_to_source(uri, options) |
|---|
| 90 | local status, response, buffer, sock = request_raw(uri, options) |
|---|
| 91 | if not status then |
|---|
| 92 | return status, response, buffer |
|---|
| 93 | elseif status ~= 200 and status ~= 206 then |
|---|
| 94 | return nil, status, response |
|---|
| 95 | end |
|---|
| 96 | |
|---|
| 97 | if response.headers["Transfer-Encoding"] == "chunked" then |
|---|
| 98 | return chunksource(sock, buffer) |
|---|
| 99 | else |
|---|
| 100 | return ltn12.source.cat(ltn12.source.string(buffer), sock:blocksource()) |
|---|
| 101 | end |
|---|
| 102 | end |
|---|
| 103 | |
|---|
| 104 | -- |
|---|
| 105 | -- GET HTTP-resource |
|---|
| 106 | -- |
|---|
| 107 | function request_raw(uri, options) |
|---|
| 108 | options = options or {} |
|---|
| 109 | local pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)") |
|---|
| 110 | if not host then |
|---|
| 111 | return nil, -1, "unable to parse URI" |
|---|
| 112 | end |
|---|
| 113 | |
|---|
| 114 | if pr ~= "http" and pr ~= "https" then |
|---|
| 115 | return nil, -2, "protocol not supported" |
|---|
| 116 | end |
|---|
| 117 | |
|---|
| 118 | port = #port > 0 and port or (pr == "https" and 443 or 80) |
|---|
| 119 | path = #path > 0 and path or "/" |
|---|
| 120 | |
|---|
| 121 | options.depth = options.depth or 10 |
|---|
| 122 | local headers = options.headers or {} |
|---|
| 123 | local protocol = options.protocol or "HTTP/1.1" |
|---|
| 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 | end |
|---|
| 155 | |
|---|
| 156 | if type(options.body) == "string" then |
|---|
| 157 | headers["Content-Length"] = headers["Content-Length"] or #options.body |
|---|
| 158 | headers["Content-Type"] = headers["Content-Type"] or |
|---|
| 159 | "application/x-www-form-urlencoded" |
|---|
| 160 | options.method = options.method or "POST" |
|---|
| 161 | end |
|---|
| 162 | |
|---|
| 163 | -- Assemble message |
|---|
| 164 | local message = {(options.method or "GET") .. " " .. path .. " " .. protocol} |
|---|
| 165 | |
|---|
| 166 | for k, v in pairs(headers) do |
|---|
| 167 | if type(v) == "string" or type(v) == "number" then |
|---|
| 168 | message[#message+1] = k .. ": " .. v |
|---|
| 169 | elseif type(v) == "table" then |
|---|
| 170 | for i, j in ipairs(v) do |
|---|
| 171 | message[#message+1] = k .. ": " .. j |
|---|
| 172 | end |
|---|
| 173 | end |
|---|
| 174 | end |
|---|
| 175 | |
|---|
| 176 | if options.cookies then |
|---|
| 177 | for _, c in ipairs(options.cookies) do |
|---|
| 178 | local cdo = c.flags.domain |
|---|
| 179 | local cpa = c.flags.path |
|---|
| 180 | if (cdo == host or cdo == "."..host or host:sub(-#cdo) == cdo) |
|---|
| 181 | and (cpa == "/" or cpa .. "/" == path:sub(#cpa+1)) |
|---|
| 182 | and (not c.flags.secure or pr == "https") |
|---|
| 183 | then |
|---|
| 184 | message[#message+1] = "Cookie: " .. c.key .. "=" .. c.value |
|---|
| 185 | end |
|---|
| 186 | end |
|---|
| 187 | end |
|---|
| 188 | |
|---|
| 189 | message[#message+1] = "" |
|---|
| 190 | message[#message+1] = "" |
|---|
| 191 | |
|---|
| 192 | -- Send request |
|---|
| 193 | sock:sendall(table.concat(message, "\r\n")) |
|---|
| 194 | |
|---|
| 195 | if type(options.body) == "string" then |
|---|
| 196 | sock:sendall(options.body) |
|---|
| 197 | end |
|---|
| 198 | |
|---|
| 199 | -- Create source and fetch response |
|---|
| 200 | local linesrc = sock:linesource() |
|---|
| 201 | local line, code, error = linesrc() |
|---|
| 202 | |
|---|
| 203 | if not line then |
|---|
| 204 | return nil, code, error |
|---|
| 205 | end |
|---|
| 206 | |
|---|
| 207 | local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)") |
|---|
| 208 | |
|---|
| 209 | if not protocol then |
|---|
| 210 | return nil, -3, "invalid response magic: " .. line |
|---|
| 211 | end |
|---|
| 212 | |
|---|
| 213 | local response = {status = line, headers = {}, code = 0, cookies = {}} |
|---|
| 214 | |
|---|
| 215 | line = linesrc() |
|---|
| 216 | while line and line ~= "" do |
|---|
| 217 | local key, val = line:match("^([%w-]+)%s?:%s?(.*)") |
|---|
| 218 | if key and key ~= "Status" then |
|---|
| 219 | if type(response[key]) == "string" then |
|---|
| 220 | response.headers[key] = {response.headers[key], val} |
|---|
| 221 | elseif type(response[key]) == "table" then |
|---|
| 222 | response.headers[key][#response.headers[key]+1] = val |
|---|
| 223 | else |
|---|
| 224 | response.headers[key] = val |
|---|
| 225 | end |
|---|
| 226 | end |
|---|
| 227 | line = linesrc() |
|---|
| 228 | end |
|---|
| 229 | |
|---|
| 230 | if not line then |
|---|
| 231 | return nil, -4, "protocol error" |
|---|
| 232 | end |
|---|
| 233 | |
|---|
| 234 | -- Parse cookies |
|---|
| 235 | if response.headers["Set-Cookie"] then |
|---|
| 236 | local cookies = response.headers["Set-Cookie"] |
|---|
| 237 | for _, c in ipairs(type(cookies) == "table" and cookies or {cookies}) do |
|---|
| 238 | local cobj = cookie_parse(c) |
|---|
| 239 | cobj.flags.path = cobj.flags.path or path:match("(/.*)/?[^/]*") |
|---|
| 240 | if not cobj.flags.domain or cobj.flags.domain == "" then |
|---|
| 241 | cobj.flags.domain = host |
|---|
| 242 | response.cookies[#response.cookies+1] = cobj |
|---|
| 243 | else |
|---|
| 244 | local hprt, cprt = {}, {} |
|---|
| 245 | |
|---|
| 246 | -- Split hostnames and save them in reverse order |
|---|
| 247 | for part in host:gmatch("[^.]*") do |
|---|
| 248 | table.insert(hprt, 1, part) |
|---|
| 249 | end |
|---|
| 250 | for part in cobj.flags.domain:gmatch("[^.]*") do |
|---|
| 251 | table.insert(cprt, 1, part) |
|---|
| 252 | end |
|---|
| 253 | |
|---|
| 254 | local valid = true |
|---|
| 255 | for i, part in ipairs(cprt) do |
|---|
| 256 | -- If parts are different and no wildcard |
|---|
| 257 | if hprt[i] ~= part and #part ~= 0 then |
|---|
| 258 | valid = false |
|---|
| 259 | break |
|---|
| 260 | -- Wildcard on invalid position |
|---|
| 261 | elseif hprt[i] ~= part and #part == 0 then |
|---|
| 262 | if i ~= #cprt or (#hprt ~= i and #hprt+1 ~= i) then |
|---|
| 263 | valid = false |
|---|
| 264 | break |
|---|
| 265 | end |
|---|
| 266 | end |
|---|
| 267 | end |
|---|
| 268 | -- No TLD cookies |
|---|
| 269 | if valid and #cprt > 1 and #cprt[2] > 0 then |
|---|
| 270 | response.cookies[#response.cookies+1] = cobj |
|---|
| 271 | end |
|---|
| 272 | end |
|---|
| 273 | end |
|---|
| 274 | end |
|---|
| 275 | |
|---|
| 276 | -- Follow |
|---|
| 277 | response.code = tonumber(status) |
|---|
| 278 | if response.code and options.depth > 0 then |
|---|
| 279 | if response.code == 301 or response.code == 302 or response.code == 307 |
|---|
| 280 | and response.headers.Location then |
|---|
| 281 | local nexturi = response.headers.Location |
|---|
| 282 | if not nexturi:find("https?://") then |
|---|
| 283 | nexturi = pr .. "://" .. host .. ":" .. port .. nexturi |
|---|
| 284 | end |
|---|
| 285 | |
|---|
| 286 | options.depth = options.depth - 1 |
|---|
| 287 | sock:close() |
|---|
| 288 | |
|---|
| 289 | return request_raw(nexturi, options) |
|---|
| 290 | end |
|---|
| 291 | end |
|---|
| 292 | |
|---|
| 293 | return response.code, response, linesrc(true), sock |
|---|
| 294 | end |
|---|
| 295 | |
|---|
| 296 | function cookie_parse(cookiestr) |
|---|
| 297 | local key, val, flags = cookiestr:match("%s?([^=;]+)=?([^;]*)(.*)") |
|---|
| 298 | if not key then |
|---|
| 299 | return nil |
|---|
| 300 | end |
|---|
| 301 | |
|---|
| 302 | local cookie = {key = key, value = val, flags = {}} |
|---|
| 303 | for fkey, fval in flags:gmatch(";%s?([^=;]+)=?([^;]*)") do |
|---|
| 304 | fkey = fkey:lower() |
|---|
| 305 | if fkey == "expires" then |
|---|
| 306 | fval = date.to_unix(fval:gsub("%-", " ")) |
|---|
| 307 | end |
|---|
| 308 | cookie.flags[fkey] = fval |
|---|
| 309 | end |
|---|
| 310 | |
|---|
| 311 | return cookie |
|---|
| 312 | end |
|---|
| 313 | |
|---|
| 314 | function cookie_create(cookie) |
|---|
| 315 | local cookiedata = {cookie.key .. "=" .. cookie.value} |
|---|
| 316 | |
|---|
| 317 | for k, v in pairs(cookie.flags) do |
|---|
| 318 | if k == "expires" then |
|---|
| 319 | v = date.to_http(v):gsub(", (%w+) (%w+) (%w+) ", ", %1-%2-%3 ") |
|---|
| 320 | end |
|---|
| 321 | cookiedata[#cookiedata+1] = k .. ((#v > 0) and ("=" .. v) or "") |
|---|
| 322 | end |
|---|
| 323 | |
|---|
| 324 | return table.concat(cookiedata, "; ") |
|---|
| 325 | end |
|---|