Ik probeer HTTP compressie toe te voegen aan mijn CGI scripts, maar het wil niet lukken. Wat doe ik fout?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| #include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <zlib.h>
#include "virtual_binary.h"
using namespace std;
Cvirtual_binary php_deflate_string(Cvirtual_binary s)
{
unsigned long cb_d = s.size() + (s.size() + 999) / 1000 + 12;
Cvirtual_binary d;
byte* w = d.write_start(10 + cb_d);
w[0] = 0x1f;
w[1] = 0x8b;
w[2] = Z_DEFLATED;
w[3] = w[4] = w[5] = w[6] = w[7] = w[8] = 0;
w[9] = 3;
w += 10;
compress(w, &cb_d, s.data(), s.size());
d.size(10 + cb_d);
return d;
}
int main()
{
string v = "<html>Hi!</html>";
/*
for (int i = 0; i < 16 << 10; i++)
v += "<!-- -->\n";
*/
string a_encoding = "gzip, deflate"; // HTTP_ACCEPT_ENCODING
/*
if (a_encoding.find("deflate") != string::npos)
coding = CODING_DEFLATE;
else
return s;
*/
Cvirtual_binary d(v.c_str(), v.size());
d = php_deflate_string(d);
cout << "Content-Encoding: deflate" << endl
// << "Content-Length: " << d.size() << endl
<< "Content-Type: text/html" << endl
<< "Vary: Accept-Encoding" << endl
<< endl;
cout.write(reinterpret_cast<const char*>(d.data()), d.size());
ofstream("gzip_out", ios::binary).write(reinterpret_cast<const char*>(d.data()), d.size());
return 0;
} |