gen_tcp recv函数中的错误参数分割

huangapple go评论64阅读模式
英文:

bad argument in gen_tcp recv when split

问题

Hi,我正在阅读一个请求,我想要获取路径,但是当我运行这段代码

{:ok, line} = :gen_tcp.recv(socket, 0)
IO.inspect "************************* #{inspect line}"
[_server_name, verb, info_path, _version] = String.split(line)

其中 inspect 看起来正常,有 4 个参数

{:http_request, :GET, {:abs_path, "/gg"}, {1, 1}}

错误提示说:

{badarg,[{binary,split,
                 [{http_request,'GET',{abs_path,<<"gg">>},{1,1}},
                  [<<227,128,128>>,
                   <<226,129,159>>,
                   <<226,128,169>>,
                   <<226,128,168>>,
                   <<226,128,128>>,
                   <<226,128,129>>,
                   <<226,128,130>>,
                   <<226,128,131>>,
                   <<226,128,132>>,
                   <<226,128,133>>,
                   <<226,128,134>>,
                   <<226,128,136>>,
                   <<226,128,137>>,
                   <<226,128,138>>,
                   <<225,154,128>>,
                   <<194,133>>,
                   <<"\t">>,
                   <<"\n">>,
                   <<"\v">>,
                   <<"\f">>,
                   <<"\r">>],
                  [global,trim_all]],
                 []},

我不知道那些额外的参数是什么。

如果我尝试绑定整个行而不使用分割,我会收到

[_server_name, verb, info_path, _version] = line
{badmatch,{http_request,'GET',{abs_path,<<"gg">>},{1,1}}}
英文:

Hi I´m reading a request where I want to get the path, but when I run this code

    {:ok, line} = :gen_tcp.recv(socket, 0)
    IO.inspect &quot;************************* #{ inspect line} &quot;

    [_server_name, verb, info_path, _version] = String.split(line)

Where the inspect looks ok with 4 arguments

    {:http_request, :GET, {:abs_path, \&quot;/gg\&quot;}, {1, 1}}

The error said:

{badarg,[{binary,split,
                 [{http_request,&#39;GET&#39;,{abs_path,&lt;&lt;&quot;/gg&quot;&gt;&gt;},{1,1}},
                  [&lt;&lt;227,128,128&gt;&gt;,
                   &lt;&lt;226,129,159&gt;&gt;,
                   &lt;&lt;226,128,169&gt;&gt;,
                   &lt;&lt;226,128,168&gt;&gt;,
                   &lt;&lt;226,128,128&gt;&gt;,
                   &lt;&lt;226,128,129&gt;&gt;,
                   &lt;&lt;226,128,130&gt;&gt;,
                   &lt;&lt;226,128,131&gt;&gt;,
                   &lt;&lt;226,128,132&gt;&gt;,
                   &lt;&lt;226,128,133&gt;&gt;,
                   &lt;&lt;226,128,134&gt;&gt;,
                   &lt;&lt;226,128,136&gt;&gt;,
                   &lt;&lt;226,128,137&gt;&gt;,
                   &lt;&lt;226,128,138&gt;&gt;,
                   &lt;&lt;225,154,128&gt;&gt;,
                   &lt;&lt;194,133&gt;&gt;,
                   &lt;&lt;&quot; &quot;&gt;&gt;,&lt;&lt;&quot;\t&quot;&gt;&gt;,&lt;&lt;&quot;\n&quot;&gt;&gt;,&lt;&lt;&quot;\v&quot;&gt;&gt;,&lt;&lt;&quot;\f&quot;&gt;&gt;,&lt;&lt;&quot;\r&quot;&gt;&gt;],
                  [global,trim_all]],
                 []},

I don't know what are all those extra arguments of the line.

If I try just bind the line with every type without the split I receive

    [_server_name, verb, info_path, _version] = line


{{badmatch,{http_request,&#39;GET&#39;,{abs_path,&lt;&lt;&quot;/gg&quot;&gt;&gt;},{1,1}}},

答案1

得分: 2

如果我正确阅读:gen_tcp.recv的文档,您“应该”会收到一个HttpPacket,在您的情况下似乎是一个HttpRequest。由于请求的解析已为您处理,您“应该”能够简单地执行以下操作:

{:ok, line} = :gen_tcp.recv(socket, 0)
{_server_name, verb, info_path, _version} = line

注意: HttpRequest 使用{...}大括号而不是[...]方括号。

英文:

If I read the docs of :gen_tcp.recv correctly, you "should" receive a HttpPacket back, which in your case seems to be a HttpRequest. Since the parsing of the request is handled for you, you "should" be able to simply do:

{:ok, line} = :gen_tcp.recv(socket, 0)
{_server_name, verb, info_path, _version} = line

Note: The HttpRepuest uses {...} curly brackets and not [...] brackets.

huangapple
  • 本文由 发表于 2020年1月6日 19:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定