I would like use gzip encoding in a simple http server built on C. But I don't want to use zlib library. Please suggest how can I do this?
-
3Can you use other libraries which are similar to zlib, like zstd? Or are all libraries out of the question? And if so, what kind of answer do you expect, beyond "write it yourself"? – Jan 24 '18 at 01:48
-
Sorry, this is unclear. What, precisely, are your requirements? And based upon those requirements, what have you tried? – lurker Jan 24 '18 at 01:54
-
@duskwuff I don't wanna use any libraries. Isn't there anything by default In C standard library that let's us do encode strings and send via HTTP Response. – bingzy Jan 24 '18 at 01:58
-
@lurker I am supposed to encode a strip in gzip format and send it via HTTP Response. I wanna implement the encoding without using any external libraries. I have changed the Content-Encoding header to gzip, the browser says content-encoding error. – bingzy Jan 24 '18 at 02:01
-
@Amaresh the standard C library has not a gzip function. If you don't want to use a library for that, then I'm afraid you will have to write it yourself. – Pablo Jan 24 '18 at 02:10
-
@Amaresh just setting the content type in the header doesn't convert the content. The header field just indicates the format you intend to provide for the content. You have to convert the content yourself. If you can't use an external library to do so, then you'll have to write the conversion code yourself, for example, using the method described in the article linked in the answer that duskwuff gave. – lurker Jan 24 '18 at 12:18
2 Answers
If you can't (or refuse to) use libraries which implement gzip, the only remaining option will be to implement the DEFLATE algorithm (and a gzip wrapper around that) yourself. This is why zlib exists, after all -- the standard C library does not contain an implementation of this algorithm.
You could instead run the gzip utility to compress your data. However it would be more efficient to use the zlib library in your code directly that it would be to fork a job every time you want to respond to an http request.
Or you could write your own gzip-format compression code. Since it is not clear from your question why you would want to avoid using zlib, it is not clear that writing your own compressor would satisfy your needs. In any case, it would take far less time on your part to just use the zlib code, which is freely available for use in any code, commercial or not, without requiring even attribution.

- 101,978
- 13
- 118
- 158
-
No, I mean using the `pipe()` and `fork()` functions to run gzip from your program, or equivalent. – Mark Adler Jan 26 '18 at 21:01