0

以下代码是 webmachine 资源的完整来源。预期的行为是流响应应该是 200,并且应该是一个指定长度的字符串,完全由字母“a”组成。

这个字符串确实是作为响应体返回的,但是状态码是 500。这怎么可能呢?

-module(dummy_binary_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init(Config)->
    {ok, Config}.

send_streamed_body(Remaining) ->
    PacketSize=1024,
    case Remaining of
        Partial when Partial =< PacketSize ->
            {string:chars($a,Partial),done};
        Full ->
            {string:chars($a,Full), fun() -> send_streamed_body(Remaining - PacketSize) end}
    end.

to_html(ReqData,State)->
    PathInfo = wrq:path_info(ReqData),
    {ok,SizeString} = dict:find(size,PathInfo),
    {Size,[]} = string:to_integer(SizeString),
    {true,wrq:set_resp_body({stream,send_streamed_body(Size)},ReqData),State}.
4

1 回答 1

0

返回值不正确。

适当的返回值是{{halt, 200}, wrq:set_resp_body({stream, send_streamed_body(Size)}, ReqData), State}.因为请求数据记录已使用默认代码 500 进行初始化。返回一个不会停止任何事情但实际上表示成功的“停止”值似乎有点违反直觉.

于 2013-05-03T13:15:01.207 回答