lua-resty-reqargs
HTTP 请求参数和文件上传助手
$ opm get bungle/lua-resty-reqargs
lua-resty-reqargs
帮助检索 application/x-www-form-urlencoded
、multipart/form-data
和 application/json
请求参数。
概要
local get, post, files = require "resty.reqargs"()
if not get then
error(post)
end
-- Use get, post, and files...
安装
只需将 `reqargs.lua` 放置在您的 package.path
中的 resty
目录下。如果您使用的是 OpenResty,默认位置为 /usr/local/openresty/lualib/resty
。
使用 OpenResty 包管理器 (opm)
$ opm get bungle/lua-resty-reqargs
使用 LuaRocks
$ luarocks install lua-resty-reqargs
lua-resty-reqargs
的 LuaRocks 仓库位于 https://luarocks.org/modules/bungle/lua-resty-reqargs。
API
此模块只有一个函数,该函数通过 require 加载
local reqargs = require "resty.reqargs"
get, post, files regargs(options)
调用该函数 (reqargs
) 时,您可以向其传递 options
。这些选项将覆盖您可能在 Nginx 配置 (或默认值) 中定义的任何内容。您可以使用以下选项
{
tmp_dir = "/tmp",
timeout = 1000,
chunk_size = 4096,
max_get_args = 100,
mas_post_args = 100,
max_line_size = 512,
max_file_uploads = 10
}
此函数将返回三个 (3) 个返回值,它们被称为 get
、post
和 files
。这些是包含 (HTTP) 请求数据的 Lua 表。get
包含使用 ngx.req.get_uri_args 检索到的 HTTP 请求 GET 参数。post
包含使用 ngx.req.get_post_args 检索到的 HTTP 请求 POST 参数,或者在 application/json
(作为请求的 Content-Type 头部) 的情况下,它将读取请求主体并解码 JSON,然后 post
将包含作为 Lua 表呈现的解码 JSON 结构。最后一个返回值 files
包含所有上传的文件。files
返回值仅在实际上传文件且请求 Content-Type 设置为 multipart/form-data
时才包含数据。files
具有与 get
和 post
相同的键结构,但值以 Lua 表的形式呈现,看起来像这样 (想想 PHP 的 $_FILES
)
{
-- The name of the file upload form field (same as the key)
name = "photo",
-- The name of the file that the user selected for the upload
file = "cat.jpg",
-- The mimetype of the uploaded file
type = "image/jpeg"
-- The file size of the uploaded file (in bytes)
size = 123465
-- The location where the uploaded file was streamed
temp = "/tmp/????"
}
如果发生错误,此函数将返回 nil
、错误消息
。
Nginx 配置变量
您可以直接从 Nginx 配置中配置 lua-resty-reqargs
的几个方面,以下是您可以使用的配置值及其默认值
# the default is the system temp dir
set $reqargs_tmp_dir /tmp;
# see https://github.com/openresty/lua-resty-upload
set $reqargs_timeout 1000;
# see https://github.com/openresty/lua-resty-upload
set $reqargs_chunk_size 4096;
# see https://github.com/openresty/lua-nginx-module#ngxreqget_uri_args
set $reqargs_max_get_args 100;
# see https://github.com/openresty/lua-nginx-module#ngxreqget_post_args
set $reqargs_max_post_args 100;
# see https://github.com/openresty/lua-resty-upload
set $reqargs_max_line_size 512;
# the default is unlimited
set $reqargs_max_file_uploads 10;
变更
此模块每个版本的更改记录在 Changes.md 文件中。
许可证
lua-resty-reqargs
使用两条款 BSD 许可证。
Copyright (c) 2015 - 2017, Aapo Talvensaari
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES`
作者
Aapo Talvensaari (@bungle)
许可证
2bsd
依赖项
openresty/lua-resty-upload、openresty
版本
-
HTTP 请求参数和文件上传助手 2017-01-07 22:40:29
-
HTTP 请求参数和文件上传助手 2016-09-30 13:26:16