欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


CentOS系统中安装配置Apache+PHP+MySQL环境
iis 7如何添加mime类型支持所有后缀名文件
什么情况下关闭win7系统网络防火墙最好
Linux系统中vim工具常用命令大全
Win7使用小马进行激活后黑屏怎么办?
三个步骤解决win7系统本地组策略打不开的问题
Windows Serve 2012 R2运行Hyper-V的五大理由
Linux下查看以及修改文件权限
Linux常用的进程管理和查看指令
linux下的lvs三种模式及配置
Nginx服务器中使用lua获取get或post参数
【 来源:网络 】【 点击:1 】 【 发布时间:2017_03_03 08:59:59 】

   使用ngx_lua模块(http://wiki.nginx.org/HttpLuaModule):

  local request_method = ngx.var.request_method

  local args = nil

  local param = nil

  local param2 = nil

  --获取参数的值

  if "GET" == request_method then

  args = ngx.req.get_uri_args()

  elseif "POST" == request_method then

  ngx.req.read_body()

  args = ngx.req.get_post_args()

  end

  param = args["param"]

  param2 = args["param2"]

  升级版(能处理content-type=multipart/form-data的表单):

  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 receive_headers = ngx.req.get_headers()

  local request_method = ngx.var.request_method

  if "GET" == request_method then

  args = ngx.req.get_uri_args()

  elseif "POST" == request_method then

  ngx.req.read_body()

  if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then--判断是否是multipart/form-data类型的表单

  is_have_file_param = true

  content_type = receive_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(receive_headers["content-type"],31)

  local body_data_table = explode(tostring(body_data),boundary)

  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,"rnrn")

  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

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站