本文介绍了如何正确解析传入的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用WinSck创建了一个C ++应用程序,该应用程序实现了一个小型(仅处理一些我需要的功能)HTTP服务器.它用于使用http请求与外界进行通信.它可以工作,但是有时由于解析失败而导致请求处理不正确.现在,我非常确定请求是正确构成的,因为它们是通过主要的网络浏览器(例如firefox/chrome或perl/C#(具有http模块/dll)发送的)发送的.

i've created an C++ application using WinSck, which has a small (handles just a few features which i need) http server implemented. This is used to communicate with the outside world using http requests. It works, but sometimes the requests are not handled correctly, because the parsing fails. Now i'm quite sure that the requests are correctly formed, since they are sent by major web browsers like firefox/chrome or perl/C# (which have http modules/dll's).

经过一些调试后,我发现问题实际上出在接收消息中.如果消息不只包含一部分(在一个 recv()调用中未读取),则有时解析会失败.我已经进行了许多尝试来解决这个问题,但是似乎没有什么足够可靠的.

After some debugging i found out that the problem is in fact in receiving the message. When the message comes in more than just one part (it is not read in one recv() call) then sometimes the parsing fails. I have gone through numerous tries on how to resolve this, but nothing seems to be reliable enough.

我现在要做的是读入数据,直到找到表示标题结尾的"\ r \ n \ r \ n" 序列.如果 WSAGetLastError()在发现这样的序列之前报告了除10035(连接关闭/失败)以外的其他信息,我将丢弃该消息.当我知道我拥有整个标头时,我将其解析并查找有关主体长度的信息.但是,我不确定此信息是否为强制性的(我认为不是),如果没有此类信息,我该怎么办-这意味着将没有尸体吗?另一个问题是我不知道是否应该在正文之后寻找"\ r \ n \ r \ n" (如果其长度大于零).

What i do now is that i read in data until i find "\r\n\r\n" sequence which indicates end of header. If WSAGetLastError() reports something else than 10035 (connection closed/failed) before such a sequence is found i discard the message. When i know i have the whole header i parse it and look for information about the body length. However i'm not sure if this information is mandatory (i think not) and what should i do if there is no such information - does it mean there will be no body? Another problem is that i do not know if i should look for a "\r\n\r\n" after the body (if its length is greater than zero).

有人知道如何可靠地解析http消息吗?

Does anybody know how to reliably parse a http message?

注意:我知道那里有http服务器的实现.我出于各种原因想要自己的东西.是的,重新发明轮子是不好的,我也知道.

Note: i know there are implementations of http servers out there. I want my own for various reasons. And yes, reinventing the wheel is bad, i know that too.

推荐答案

您可以尝试查看他们的代码,以了解他们如何处理HTTP消息.

You could try looking at their code to see how they handle a HTTP message.

或者您可以查看规范,其中有字段.显然,只有越野车浏览器会在最后发送其他CRLF.

Or you could look at the spec, there's message length fields you should use. Only buggy browsers send additional CRLFs at the end, apparently.

这篇关于如何正确解析传入的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:57
查看更多