http - 如何判断 HTTP 响应是否在 C 中终止

标签 http browser https mbed

我正在为我使用 mbedTLS for TLS 的嵌入式项目实现一个最小的 HTTPS 层和硬编码 HTTP header 以与 HTTPS 服务器通信。

它适用于普通网站。但到目前为止,我的实现通过检查读取的最后一个字节是否为 \n 来检测 HTTPS 响应的结束。

if( ret > 0 && output[len-1] == '\n' )
{
    ret = 0;
    output[len] = 0;
    break;
}

然而,由于显而易见的原因,这并不总是有效。我尝试了 openssl s_client,它的行为相同 - 如果 HTTP 响应以 \n 终止,则 s_client 在获取所有数据后立即返回。否则它会永远阻塞,等待更多数据。

真正的浏览器似乎能够正确处理这个问题。除了设置超时,我还能做些什么吗?

最佳答案

How to tell if an HTTP response terminates in C...
But so far my implementation detects the end of HTTPS response by checking if the last byte read is \n...
This, however, is not always working for obvious reason...

HTTP 调用 \r\n \n。参见 RFC 2616, Hypertext Transfer Protocol - HTTP/1.1和第 15 页:

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body (see appendix 19.3 for tolerant applications). The end-of-line marker within an entity-body is defined by its associated media type, as described in section 3.7.

CRLF = CR LF

现在,各种服务器发送的是完全不同的球赛。将出现重复的行尾标记、丢失的行尾标记和不正确的行尾标记。这是狂野的西部。

您可能想查看 HTTP 解析器的引用实现。如果是这样,请查看 libeventcURL的解析器以及它们如何维护它们的状态机。

关于http - 如何判断 HTTP 响应是否在 C 中终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423788/

相关文章:

browser - 在浏览器中渲染有向图

python - 如何在curl重定向上发送数据?

ssl - CNAMES 如何与 SSL 证书一起使用

Android 7.0 : 'javax.net.ssl.SSLHandshakeException: Connection closed by peer

json - 使用 Angular2 Promise 从 JSON 获取数据

node.js - 在 setTimeout 差异中关闭 node.js http 服务器

javascript - 使用原始 WebSocket 时如何知道消息是否已成功发送?

c# - 是否可以从 C# 控制台应用程序使用 HttpBrowserCapabilities?

python - 正则表达式捕捉网址

google-chrome - 禁用 Chrome 中的启动设置