C ++通过socket将压缩的html发送到浏览器

时间:2014-03-12 16:19:35

标签: c++ sockets webserver gzip

我正在构建Web服务器。我想发送gzip压缩页面,但我的代码有问题:

...
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中显示内容编码错误。怎么了 ?怎么解决?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。所有代码都可以,但我必须将Content-Encoding标题更改为“deflate”而不是gzipc!

相关问题