【lua】LWT HttpdModule

要使用httpd模块,需要在脚本开头添加:  

require "httpd"

  httpd.pairs(apr_table)

  用以遍历apr_table

for key, value in httpd.pairs(t) do body end

  例:

 1 require "httpd"
 2 local table = { "uri", "protocol", "hostname", "path", "path_info", "args",
 3                 "method", "filename", "filedir", "user", "auth_type",
 4                 "local_ip", "remote_ip" }
 5 
 6 httpd.set_content_type("text/plain; charset=utf8")
 7 
 8 httpd.write("Hello Lua World
")
 9 httpd.write("----------------------------------
")
10 
11 for mm, key in ipairs(table) do
12         httpd.write(mm .. "->" .. key .. "
")
13 end
14 
15 httpd.write("----------------------------------")

  httpd.set_status (status)

    设置一个介于100到599的值作为HTTP响应的状态。

  httpd.set_content_type (content_type)

    将HTTP响应的内容类型设置为指定值。

httpd.set_content_type("text/html; charset=utf-8");

  httpd.add_header (name, value [, err_header])

     添加HTTP响应头,值是表示该头的值的字符串。

  httpd.add_cookie (name [, value [, expires [, path [, domain [, secure [, httponly]]]]]])

    添加cookie  

  httpd.write_template (filename [, flags [, file]])

    根据指定的文件模版渲染网页

    如果模版被解析,则下面的指令将被模版引擎解释:

  httpd.escape_uri (string)

      相对于uri的转义字符序列

  httpd.escape_xml (string)

      相对于xml的转义字符串序列

      

  httpd.escape_js (string)

      相对于js的转义字符串序列

       

    httpd.input

      用来读取HTTP请求数据流,和Lua的input类似

    httpd.output

      用来输出HTTP响应数据流,和Lua的output类似

  httpd.read(...)

      相当于httpd.input:read(....)

  httpd.write(...)

      相当于httpd.output:write(...)

  httpd.debug (message)

      将消息记录写入HTTP server debug级别的日志内

  httpd.notice (message)

      将消息记录写入HTTP server notice级别的日志内

  httpd.err (message) 

      将消息记录写入HTTP server err级别的日志内

  httpd.redirect (request, uri, status)

      设置Location头信息并返回状态码,当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址,这个跳转只有浏览器知道,不管体内容里有没有东西,用户都看不到。     

return httpd.redirect(request, "/next.lua", status)

  httpd.dump (value)

      将value写入至HTTP响应主体、HTML内、函数处理的Lua表中、ARP表或者递归

原文地址:https://www.cnblogs.com/linxiong945/p/4103727.html