如何解决来自网络服务器的无效 HTTP 响应通过调制解调器上的端口转发
我目前正在构建一个嵌入式设备,该设备使用开源轻量级 IP 包 (lwIP) 为使用 html 文件和 http 请求的客户端托管静态网络服务器。 lwIP 与普通的网络服务器有些不同,因为它是裸机并配置为通过 NOSYS 声明提供这些文件。
在一个极其简单的场景中,我尝试托管此文件 (index.html
):
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
</body>
</html>
当我直接连接到嵌入式设备时,我会从网络服务器获得有效的标头,如下所示:
Host: 192.168.168.10
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip,deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
我可以查看页面没问题。
但是,如果我尝试通过端口转发到嵌入式设备的调制解调器,在 Firefox 上尝试查看 index.html
时会出现以下错误:
The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range.
The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.
奇怪的是标头仍然相同(只是不同的 IP 和端口):
Host: 192.168.2.31:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: text/html,deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
并且响应数据仍然与上面的 index.html
相同。
我不明白是什么导致了错误,因为内容类型是在标签中指定的。
解决方法
您粘贴的是浏览器发送到服务器的 request 标头。问题在于嵌入式服务器发回的 response 标头。特别是 Content-Type 标头不正确。它应该看起来像:
Content-Type: text/plain; charset=us-ascii
如果您看不到将它们添加到嵌入式应用程序的何处,您可以在页面中使用 <meta http-equiv="Content-Type" content="...">
解决此问题。
您可以尝试将 <meta charset="UTF-8">
放在 HTML 文档的 <head>
中,它可能会解决问题。
此问题已通过使用 Tom Vs 对服务器的 curl -v
建议解决。这给出了 Received HTTP/0.9 when not allowed
的响应。
查看 lwipopts.h
中 LWIP 的配置时,LWIP_HTTPD_DYNAMIC_HEADERS
被设置为 0。这应该设置为 1 以允许从 lwIP 生成标头并设置给用户。>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。