【问题标题】:C++ send compressed html via socket to browserC ++通过套接字将压缩的html发送到浏览器
【发布时间】:2014-03-12 16:19:34
【问题描述】:

我正在构建网络服务器。我想发送 gzipped html 页面,但我的代码有问题:

...
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Encoding: gzip\r\n"
"Connection: keep-alive\r\n"
"Server: michal\r\n"
"Set-Cookie: nazwa=test\r\n"
"Date: Mon, 24 Feb 2014 11:39:26 GMT\r\n"
"Vary: Accept-Encoding\r\n\r\n";
std::string body =
"<html><body><h1>It works!</h1>"
"<p>This is the default web page for this server.</p>"
"<p>The web server software is running but no content has been added, yet.</p>"
"<form method='post' action='/' enctype='multipart/form-data' ><input type='file' name='pliczek1'/><input type='submit' name='sub' value='sender' /><input type='checkbox' name='add[]' value='100001_used' ><input type='hidden' name='hidd' value='testowy hiddenik' /><input type='checkbox' name='add[]' value='100002_used' ><textarea name='txtform'>tekstowe poleąś</textarea></form>"
"</body></html>\r\n\r\n";

...

std::string responseStr = response;
responseStr += compress_string(body);
std::cout << responseStr;
write(client_fd, responseStr.c_str(), responseStr.length());
close(client_fd);

我正在使用 compress_string 函数:http://pastebin.com/kFGgvVbF 没有 compress_string 和 Content-Encoding 一切正常,所以压缩/发送/标头有问题。页面未在浏览器中加载。在 FF 中显示内容编码错误。怎么了 ?如何解决?

【问题讨论】:

  • 您忽略了请求和响应的样子。它们符合您的期望吗?你怎么知道你的浏览器甚至接受 gzip 编码?您知道该压缩例程是否甚至可以输出正确的 gzip 吗?
  • 浏览器接受 gzip(我在标题中看到)。如何检查压缩是否输出正确的 gzip ?我想在浏览器中查看
  • 好吧,一种方法是使用gzip 并比较输出。那个压缩程序对我来说看起来很粗略。使用 zlib 或其他更成熟的东西可能会更好。
  • 我找到了解决方案!内容编码必须设置为:放气而不是 gzip !感谢您的 cmets

标签: c++ sockets webserver gzip


【解决方案1】:

我找到了解决方案。代码的一切都可以,但我不得不将 Content-Encoding 标头更改为“deflate”而不是 gzipc!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2015-04-22
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多