检测HTTP头的一端与\\ r \\ n \\ r \\ n [英] detecting end of http header with \r\n\r\n

查看:1993
本文介绍了检测HTTP头的一端与\\ r \\ n \\ r \\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的recv我想要得到的HTTP头,所以我可以解析的内容长度。但是我无法检测线路中断。或者实际上我连检测线断线或将我第一次读入缓冲区始终是完整的头(假设我有一个足够长的缓冲)。

Using recv I want to get the http header so I can parse for a content length. However I'm having trouble detecting the line break. Or actually do I even have to detect line break or will the first time I read into the buffer always be the complete header (assuming I have a long enough buffer).

这是用C写的

编辑:在看一些相关的问题的事情之一,我担心的是,
...的\\ r \\ n时,头球破门的可能是由两个不同的调用被拉入您的缓冲区的recv(),这将prevent无法识别头球破门您的code。

edit: looking at some of the related questions one of the things I am worried about is "...the "\r\n" of the header break might be pulled into your buffer by two different calls to recv() which would prevent your code from recognizing the header break."

推荐答案

您应该重复调用的recv(),每次它给你的 X 字节你增加buffer-指针你给它由 X 字节(并降低它允许通过 X 字节还写CB)。你这样做,直到你的缓冲区要么包含一个 \\ r \\ n \\ r \\ n 或全满,在这种情况下,您只需关闭套接字,并从此忽略恶意的客户端上。缓冲器的大小应为约3000个字节。

You should call recv() repeatedly and each time it gives you x bytes you increase the buffer-pointer you give to it by x bytes (and decrease the cb it is allowed to write also by x bytes). You do this until your buffer either contains a \r\n\r\n or is completely full, in which case you just close the socket and ignore the malicious client from then on. Buffer-size should be about 3000 bytes.

但是:这忽略了你的服务器似乎是一个轮询服务器的一般问题。如果你有一些经验,你应该尽量让一个epoll的-服务器,而不是。

But: this ignores the general problem that your server seems to be a polling-server. If you have some experience you should try to make an epoll-server instead.

这篇关于检测HTTP头的一端与\\ r \\ n \\ r \\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆