在 Erlang 中解析二进制文件
如果我有以下二进制文件: <<"GET http://www.google.com HTTP/1.1">>
,如何将其拆分以便只能检索主机 (http://www.google.com
)?
我从以下内容开始:
get_host(<<$G, Rest/binary>>) -> get_host(休息);
get_host(<<$E, Rest/binary>>) ->; get_host(休息);
get_host(<<$T, Rest/binary>>) ->; get_host(Rest);
但我不知道如何从这里继续。我正在考虑反转 Rest
并从二进制文件的末尾重新开始。
If I have the following binary: <<"GET http://www.google.com HTTP/1.1">>
, how can I split it up so that I can retrieve only the host (http://www.google.com
) ?
I started with something like:
get_host(<<$G, Rest/binary>>) -> get_host(Rest);
get_host(<<$E, Rest/binary>>) -> get_host(Rest);
get_host(<<$T, Rest/binary>>) -> get_host(Rest);
but I am not sure how to go on from here. I was thinking of reversing Rest
and starting over from the end of the binary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您正在尝试为 HTTP 1.1 实现一个最小的解析器。这是一个解决方案,它遵循 HTTP 1.1 的规范并解析第一行一个http请求。在不知道您的具体情况的情况下,我在大多数情况下建议在简化的“分割二进制”或类似的之前使用通用的 HTTP 解析器。
It seems you're trying to implement a minimal parser for HTTP 1.1. This is one solution that does follow the specifications for HTTP 1.1 and parses the first line of a http request. Without knowing your specific situation I would in most cases recommend using a generic HTTP parser before a simplified "split binary" or similar.
我建议使用
erlang:decode_packet
来实现此目的,但为了展示如何完成此操作,这里有一对函数,它们可以去除前导的"GET "
然后返回所有内容到第一个空格(但如果没有空格则会崩溃)。基本上,我将每个不是空格的字节放入我的“累加器”中,当我找到空格时,我返回我的累加器。这是一个常见的技巧,在列表中更常见。 (对于列表,您需要将新项目放在列表的前面,并在末尾反转列表,以避免 O(N) 算法变成 O(N²),但这不是二进制文件需要。)
I would recommend
erlang:decode_packet
for this, but to show how it can be done, here is a pair of functions that strips the leading"GET "
and then returns everything up to the first space (but crashes if there is no space).Basically, I put each byte that is not a space into my "accumulator", and when I find the space I return my accumulator. This is a common trick that is more often seen with lists. (With lists you will want to put new items at the front of the list and reverse the list at the end, to avoid your O(N) algorithm turning into O(N²), but that is not needed for binaries.)
简单的答案(但可能不是您真正想要的)
The simple answer (but probably not what you really is asking for)