Hello!
2013/3/21 wgm.china:
> 我用ab -p "c:\temp\1.txt" http://192.168.1.101/u1" 压lua-resty-upload
> 时,每次后台都报错,不知道是不是ab的用法有问题?我写一个html 通过post提交文件正常。
>
ab 的 -p 命令并不是使用 multipart 格式进行 POST 表单上传。其实,它只是把你指定的文件直接作为请求体 POST
出去,我们可以做一个下面这个小试验来确认这一点。
1. 首先创建一个文件 hello.txt,其内容为 hello world:
$ echo hello world > hello.txt
2. 然后运行 nc 命令监听本地的一个端口,比如 1234:
$ nc -l 1234
3. 使用 ab -p 上传给 hello.txt:
$ ab -c1 -n1 -p hello.txt localhost:1234/
4. 在 nc 运行的终端上可以得到 ab 发出的原始 HTTP 请求:
$ nc -l 1234
POST / HTTP/1.0
Content-length: 12
Content-type: text/plain
Host: localhost:1234
User-Agent: ApacheBench/2.3
Accept: */*
hello world
我们看到,并不是合法的 multipart 表单上传请求。
> 2013/03/21 22:43:27 [error] 20720#0: *6533 lua entry thread aborted: runtime
> error: /usr/local/lua/upload.lua:9: attempt to index local 'form' (a nil
> value)
> stack traceback:
> coroutine 0:
>
这个错误是因为 upload:new() 方法调用返回的第一个值 form 是 nil 所致。而我的 lua-resty-upload
库文档中给的示例代码在这里并没有进行恰当的出错处理。将示例代码中的这一行
local form, err = upload:new(chunk_size)
的后面加上下面这个检查:
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(500)
end
这样你再用 ab -p 请求时,就会在 nginx 错误日志文件中看到下面这个更加友好的错误消息了:
failed to new upload: no boundary defined in Content-Type
显然,ab -p 发起的请求头 Content-Type 是 text/plain,并没有定义 boundary 选项的值。
在使用 ab 来摸似表单上传时,需要自己准备 multipart 格式的请求体(你可以如上面演示的那样通过 nc 直接拦截你的浏览器发出的
multipart POST 请求),然后使用 -p 同时再加 -T 选项来运行 ab. -T 选项用于指定 Content-Type
的值,注意需要在那里指定相应的 boundary 选项参数,例如:
ab -p /path/to/body -T "multipart/form-data;
boundary=---------------------------820127721219505131303151179" url
Best regards,
-agentzh
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 openresty@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html