在 Erlang 中读取收到的 HTTP 请求的正文
我一直在研究 Mochiweb,但在通过套接字接收请求时找不到读取正文的方法。
我不限于 Mochiweb,任何其他 HTTP 库都对我有好处。
我还尝试了 gen_tcp:listen(Port, [{packet, http}]),这样我可以在接收 HTTP 请求时读取正文/标头,但我必须手动处理响应并保持套接字打开以接受更多请求,因此我不想使用此解决方案。
我的目的是接收大型正文的请求,而不是等待收到完整正文才能开始阅读/处理它们。
I've been looking into Mochiweb, but I can't find a way to read the body while I'm receiving the request through the socket.
I'm not limited to Mochiweb, any other HTTP library would be good for me.
I also tried gen_tcp:listen(Port, [{packet, http}])
, this way I can read the body/headers while I'm receiving the HTTP request, but I must handle manually the responses and keeping the socket open for more requests, so I prefer not to use this solution.
My intention is to receive request with large bodies and not to wait to receive the full body in order to start reading/processing them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 mochiweb,您可以使用
折叠请求正文的块要求:stream_body/3
。它需要一个块处理函数作为第二个参数。该处理程序的调用方式为
{ChunkSize, BinaryData}
以及从套接字接收到的每个块的状态。示例(检索块的[反向]列表):
With mochiweb you can fold over chunks of the request body using
Req:stream_body/3
.It expects a chunk handler function as the second argument. This handler is called with
{ChunkSize, BinaryData}
and your state for every chunk, as it is received from the socket.Example (retrieving a [reversed] list of chunks):