On Thursday, 24 July 2014 at 13:09:53 UTC, Sean Campbell wrote:
I'm trying to add gzip compression to a HTTP server i wrote in
D. here is the code that dose the gzip encoding.
I know zlib gzip works for http, I used it in my cgi.d
if(gzipResponse && acceptsGzip && isAll) {
auto c = new Compress(HeaderFormat.gzip);
// want gzip
auto data = c.compress(t);
data ~= c.flush();
t = data;
}
But your http server is buggy in a lot of ways. It doesn't reply
to curl and doesn't keep the connection open to issue manual
requests.
Among the bugs I see looking at it quickly:
server.d getRequestHeaderFieldValue, you don't check if epos is
-1. If it is, you should return null or something instead of
trying to use it - the connection will hang because of an
out-of-bounds array read killing the handler.
You also wrote:
if ((Info.modGzip) &
(indexOf(client.getRequestHeaderFieldValue("Accept-Encoding"),"gzip")
!= -1)){
Notice the & instead of &&. That's in fspipedserver.d.
Finally, you write client.client.send... which never sent the
headers back to the client, so it didn't know you were gzipping!
Change that to client.sendData (and change sendData in server.d
to take "in void[]" instead of "void[]") and then it sends the
headers and seems to work by my eyeballing.
if ((Info.modGzip) &
(indexOf(client.getRequestHeaderFieldValue("Accept-Encoding"),"gzip")
!= -1)){
writeln("gzip");
auto gzip = new Compress(HeaderFormat.gzip);
client.addToResponseHeader("Content-Encoding: gzip");
client.sendHeader("200 ok");
while (0 < (filestream.readBlock(readbuffer))){
client.client.send(gzip.compress(readbuffer));
}
client.client.send(gzip.flush());
delete gzip;
delete filestream;
}
but when i test it Firefox, internet explorer and chrome says
that the encoding or compression is bad. why? the data is
compressed with gzip.
the rest of the code is available at
http://sourceforge.net/p/netspark/netsparkb1/ci/master/tree/
Git
git clone git://git.code.sf.net/p/netspark/netsparkb1
netspark-netsparkb1