2014-03-12 55 views
0

我正在構建Web服務器。我想給gzip壓縮的HTML頁面,但事情是錯我的代碼:C++通過套接字發送壓縮的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和內容編碼一切正常的話,什麼問題是壓縮/發送/頭。瀏覽器中沒有加載頁面。在FF中顯示內容編碼的錯誤。怎麼了 ?如何解決它?

+0

你忽略了說什麼請求和響應的樣子。它們是否符合你的期望?你怎麼知道你的瀏覽器甚至接受gzip編碼?你知道這個壓縮程序是否能輸出正確的gzip嗎? –

+0

瀏覽器接受gzip(我在標題中看到它)。我如何檢查壓縮是否輸出正確的gzip?我想在瀏覽器中檢查它 – mitch

+0

那麼,一種方法是使用'gzip'並比較輸出。這個壓縮程序看起來很粗略。可能會更好地使用zlib或其他更成熟的東西。 –

回答

0

我找到了解決方案。所有代碼都可以,但是我必須將Content-Encoding標頭改爲「放氣」而不是gzipc!