前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >nginx嵌入lua 解析get和post请求

nginx嵌入lua 解析get和post请求

作者头像
友儿
发布2022-09-11 17:36:56
发布2022-09-11 17:36:56
1.9K0
举报
文章被收录于专栏:友儿
nginx.conf文件
代码语言:javascript
复制
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    # 设置纯 Lua 扩展库的搜寻路径(';;' 是默认路径)
    #lua_package_path "/data/www/code/nginx+lua/config/lua_p/?.lua;;";
    # 设置 C 编写的 Lua 扩展模块的搜寻路径(也可以用 ';;')
    #lua_package_cpath "/data/www/code/nginx+lua/config/lua_p_c/?.so;;";

    server {
       listen 8080;
       location /getPost {
            default_type 'text/html';
            #lua_code_cache off; # 关闭lua缓存 修改content_by_lua_file引入文件不用重启nginx就会生效
            content_by_lua_file   ./config/lua/getPost.lua;  # 引入lua脚本
            charset utf-8;
       }
    }
}
getPost.lua文件

说明post请求enctype属性

  • enctype:规定了form表单在发送到服务器时候编码方式,它有如下的三个值。
  • application/x-www-form-urlencoded:默认的编码方式。但是在用文本的传输和MP3等大型文件的时候,使用这种编码就显得 效率低下。
  • text/plain:纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码。
  • multipart/form-data:指定传输数据为二进制类型,比如图片、mp3、文件。
代码语言:javascript
复制
local function explode(_str,seperator)
  local pos, arr = 0, {}
  for st, sp in
  function() return string.find( _str, seperator, pos, true ) end
  do
      table.insert( arr, string.sub( _str, pos, st-1 ));
      pos = sp + 1;
  end
  table.insert( arr, string.sub( _str, pos ));
  return arr;
end

local args = {};
local file_args = {};
local is_have_file_param = false;
local function init_form_args()
  local cjson = require "cjson";
  local headers = ngx.req.get_headers();
  local method = ngx.var.request_method;
--    ngx.say(headers["content-type"]);
--    ngx.say(string.sub(headers["content-type"],1, 20));
--    ngx.say(string.sub(headers["content-type"],1, 20) == "multipart/form-data;");
  if "GET" == method then
      args = ngx.req.get_uri_args();
  elseif "POST" == method then
      ngx.req.read_body();
      if string.sub(headers["content-type"], 1, 34) == "application/x-www-form-urlencoded;" then --判断是否是multipart/form-data类型的表单
          args = ngx.req.get_post_args();
      elseif string.sub(headers["content-type"],1, 16) == "application/json;" then --判断是否是multipart/form-data类型的表单
          local json = ngx.req.get_body_data();
          if json ~= nil then
              args = cjson.decode(json);
          end
      elseif string.sub(headers["content-type"],1, 20) == "multipart/form-data;" then --判断是否是multipart/form-data类型的表单
          is_have_file_param = true;
          content_type = headers["content-type"];
          body_data = ngx.req.get_body_data();--body_data可是符合http协议的请求体,不是普通的字符串
          --请求体的size大于nginx配置里的client_body_buffer_size,则会导致请求体被缓冲到磁盘临时文件里,client_body_buffer_size默认是8k或者16k
          if not body_data then
              local datafile = ngx.req.get_body_file()
              if not datafile then
                  error_code = 1
                  error_msg = "no request body found"
              else
                  local fh, err = io.open(datafile, "r")
                  if not fh then
                      error_code = 2
                      error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err)
                  else
                      fh:seek("set")
                      body_data = fh:read("*a")
                      fh:close()
                      if body_data == "" then
                          error_code = 3
                          error_msg = "request body is empty"
                      end
                  end
              end
          end
          local new_body_data = {}
          --确保取到请求体的数据
          if not error_code then
              local boundary = "--" .. string.sub(headers["content-type"],31); -- 分割符
--                ngx.say(boundary);
              local body_data_table = explode(tostring(body_data),boundary);   -- 按分隔符分割
--                ngx.say(body_data_table);
              local first_string = table.remove(body_data_table,1);
              local last_string = table.remove(body_data_table);
              for i,v in ipairs(body_data_table) do
                  local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"')
                  if not start_pos then--普通参数
                      local t = explode(v,"\r\n\r\n");
--                        ngx.say(t[1]);
--                        ngx.say(t[2]);
                      local temp_param_name = string.sub(t[1],41,-2);
                      local temp_param_value = string.sub(t[2],1,-3);
                      args[temp_param_name] = temp_param_value;
                  else--文件类型的参数,capture是参数名称,capture2是文件名
                      file_args[capture] = capture2;
                      table.insert(new_body_data,v);
                  end
              end
              table.insert(new_body_data,1,first_string);
              table.insert(new_body_data,last_string);
              --去掉app_key,app_secret等几个参数,把业务级别的参数传给内部的API
              body_data = table.concat(new_body_data,boundary);--body_data可是符合http协议的请求体,不是普通的字符串
          end
      else
          args = ngx.req.get_post_args();
      end
  end

end
init_form_args();
for key, value in pairs(args) do
  ngx.say("key:"..tostring(key).."      value:"..tostring(value));
end
for key, value in pairs(file_args) do
  ngx.say("key:"..tostring(key).."      value:"..tostring(value));
end
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • nginx.conf文件
  • getPost.lua文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档