summaryrefslogtreecommitdiffhomepage
path: root/libs/luci-lib-base/luasrc/http.lua
blob: 882b71c8f7fc4c92a1e51201aa7f5923f7396c3f (plain)
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
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
-- Licensed to the public under the Apache License 2.0.

local util  = require "luci.util"
local coroutine = require "coroutine"
local table = require "table"
local lhttp = require "lucihttp"

local L, table, ipairs, pairs, type, error = _G.L, table, ipairs, pairs, type, error

module "luci.http"

HTTP_MAX_CONTENT      = 1024*100		-- 100 kB maximum content size

function close()
	L.http:close()
end

function content()
	return L.http:content()
end

function formvalue(name, noparse)
	return L.http:formvalue(name, noparse)
end

function formvaluetable(prefix)
	return L.http:formvaluetable(prefix)
end

function getcookie(name)
	return L.http:getcookie(name)
end

-- or the environment table itself.
function getenv(name)
	return L.http:getenv(name)
end

function setfilehandler(callback)
	return L.http:setfilehandler(callback)
end

function header(key, value)
	L.http:header(key, value)
end

function prepare_content(mime)
	L.http:prepare_content(mime)
end

function source()
	return L.http.input
end

function status(code, message)
	L.http:status(code, message)
end

-- This function is as a valid LTN12 sink.
-- If the content chunk is nil this function will automatically invoke close.
function write(content, src_err)
	if src_err then
		error(src_err)
	end

	return L.print(content)
end

function splice(fd, size)
	coroutine.yield(6, fd, size)
end

function redirect(url)
	L.http:redirect(url)
end

function build_querystring(q)
	local s, n, k, v = {}, 1, nil, nil

	for k, v in pairs(q) do
		s[n+0] = (n == 1) and "?" or "&"
		s[n+1] = util.urlencode(k)
		s[n+2] = "="
		s[n+3] = util.urlencode(v)
		n = n + 4
	end

	return table.concat(s, "")
end

urldecode = util.urldecode

urlencode = util.urlencode

function write_json(x)
	L.printf('%J', x)
end

-- separated by "&". Tables are encoded as parameters with multiple values by
-- repeating the parameter name with each value.
function urlencode_params(tbl)
	local k, v
	local n, enc = 1, {}
	for k, v in pairs(tbl) do
		if type(v) == "table" then
			local i, v2
			for i, v2 in ipairs(v) do
				if enc[1] then
					enc[n] = "&"
					n = n + 1
				end

				enc[n+0] = lhttp.urlencode(k)
				enc[n+1] = "="
				enc[n+2] = lhttp.urlencode(v2)
				n = n + 3
			end
		else
			if enc[1] then
				enc[n] = "&"
				n = n + 1
			end

			enc[n+0] = lhttp.urlencode(k)
			enc[n+1] = "="
			enc[n+2] = lhttp.urlencode(v)
			n = n + 3
		end
	end

	return table.concat(enc, "")
end

context = {
	request = {
		formvalue      = function(self, ...) return formvalue(...)      end;
		formvaluetable = function(self, ...) return formvaluetable(...) end;
		content        = function(self, ...) return content(...)        end;
		getcookie      = function(self, ...) return getcookie(...)      end;
		setfilehandler = function(self, ...) return setfilehandler(...) end;
		message        = L and L.http.message
	}
}