http.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local util = require "luci.util"
  5. local coroutine = require "coroutine"
  6. local table = require "table"
  7. local lhttp = require "lucihttp"
  8. local L, table, ipairs, pairs, type, error = _G.L, table, ipairs, pairs, type, error
  9. module "luci.http"
  10. HTTP_MAX_CONTENT = 1024*100 -- 100 kB maximum content size
  11. function close()
  12. L.http:close()
  13. end
  14. function content()
  15. return L.http:content()
  16. end
  17. function formvalue(name, noparse)
  18. return L.http:formvalue(name, noparse)
  19. end
  20. function formvaluetable(prefix)
  21. return L.http:formvaluetable(prefix)
  22. end
  23. function getcookie(name)
  24. return L.http:getcookie(name)
  25. end
  26. -- or the environment table itself.
  27. function getenv(name)
  28. return L.http:getenv(name)
  29. end
  30. function setfilehandler(callback)
  31. return L.http:setfilehandler(callback)
  32. end
  33. function header(key, value)
  34. L.http:header(key, value)
  35. end
  36. function prepare_content(mime)
  37. L.http:prepare_content(mime)
  38. end
  39. function source()
  40. return L.http.input
  41. end
  42. function status(code, message)
  43. L.http:status(code, message)
  44. end
  45. -- This function is as a valid LTN12 sink.
  46. -- If the content chunk is nil this function will automatically invoke close.
  47. function write(content, src_err)
  48. if src_err then
  49. error(src_err)
  50. end
  51. return L.print(content)
  52. end
  53. function splice(fd, size)
  54. coroutine.yield(6, fd, size)
  55. end
  56. function redirect(url)
  57. L.http:redirect(url)
  58. end
  59. function build_querystring(q)
  60. local s, n, k, v = {}, 1, nil, nil
  61. for k, v in pairs(q) do
  62. s[n+0] = (n == 1) and "?" or "&"
  63. s[n+1] = util.urlencode(k)
  64. s[n+2] = "="
  65. s[n+3] = util.urlencode(v)
  66. n = n + 4
  67. end
  68. return table.concat(s, "")
  69. end
  70. urldecode = util.urldecode
  71. urlencode = util.urlencode
  72. function write_json(x)
  73. L.printf('%J', x)
  74. end
  75. -- separated by "&". Tables are encoded as parameters with multiple values by
  76. -- repeating the parameter name with each value.
  77. function urlencode_params(tbl)
  78. local k, v
  79. local n, enc = 1, {}
  80. for k, v in pairs(tbl) do
  81. if type(v) == "table" then
  82. local i, v2
  83. for i, v2 in ipairs(v) do
  84. if enc[1] then
  85. enc[n] = "&"
  86. n = n + 1
  87. end
  88. enc[n+0] = lhttp.urlencode(k)
  89. enc[n+1] = "="
  90. enc[n+2] = lhttp.urlencode(v2)
  91. n = n + 3
  92. end
  93. else
  94. if enc[1] then
  95. enc[n] = "&"
  96. n = n + 1
  97. end
  98. enc[n+0] = lhttp.urlencode(k)
  99. enc[n+1] = "="
  100. enc[n+2] = lhttp.urlencode(v)
  101. n = n + 3
  102. end
  103. end
  104. return table.concat(enc, "")
  105. end
  106. context = {
  107. request = {
  108. formvalue = function(self, ...) return formvalue(...) end;
  109. formvaluetable = function(self, ...) return formvaluetable(...) end;
  110. content = function(self, ...) return content(...) end;
  111. getcookie = function(self, ...) return getcookie(...) end;
  112. setfilehandler = function(self, ...) return setfilehandler(...) end;
  113. message = L and L.http.message
  114. }
  115. }