1 --- elinks/src/document/session.h Wed May 15 06:40:09 2002
2 +++ elinks_with_gzip/src/document/session.h Mon May 13 13:55:41 2002
5 #ifndef EL__DOCUMENT_SESSION_H
6 #define EL__DOCUMENT_SESSION_H
11 +#include <openssl/ssl.h>
17 /* We need to declare these first :/. Damn cross-dependencies. */
19 --- elinks/src/lowlevel/sched.c Wed May 15 06:40:15 2002
20 +++ elinks_with_gzip/src/lowlevel/sched.c Mon May 13 21:42:33 2002
28 #include <openssl/ssl.h>
40 send_connection_info(c);
50 --- elinks/src/lowlevel/sched.h Wed May 15 06:40:15 2002
51 +++ elinks_with_gzip/src/lowlevel/sched.h Mon May 13 13:41:12 2002
54 #ifndef EL__LOWLEVEL_SCHED_H
55 #define EL__LOWLEVEL_SCHED_H
61 +#include <openssl/ssl.h>
66 #include "links.h" /* tcount, list_head */
67 #include "document/cache.h"
68 #include "lowlevel/ttime.h"
80 --- elinks/src/protocol/http/http.c Wed May 15 06:40:22 2002
81 +++ elinks_with_gzip/src/protocol/http/http.c Wed May 15 06:32:28 2002
84 #include <openssl/ssl.h>
93 #include "protocol/url.h"
94 #include "util/base64.h"
95 #include "util/blacklist.h"
96 +#include "util/compress.h"
98 struct http_connection_info {
99 enum blacklist_flags bl_flags;
105 if (c->info && !((struct http_connection_info *)c->info)->close
107 && (!c->ssl) /* We won't keep alive ssl connections */
111 add_to_str(&hdr, &l, "Accept: */*\r\n");
114 + add_to_str(&hdr, &l, "Accept-Encoding: gzip\r\n");
116 if (!accept_charset) {
117 unsigned char *cs, *ac;
121 if (info->length >= 0 && info->length < l) l = info->length;
123 - if (add_fragment(c->cache, c->from, rb->data, l) == 1) c->tries = 0;
128 + unsigned char *data = decompress_gzip(&c->z, rb->data, l, &dl);
130 + setcstate(c, S_OUT_OF_MEM);
131 + abort_connection(c);
134 + if (add_fragment(c->cache, c->from, data, dl) == 1) c->tries = 0;
140 + if (add_fragment(c->cache, c->from, rb->data, l) == 1) c->tries = 0;
144 if (info->length >= 0) info->length -= l;
146 kill_buffer_data(rb, l);
147 if (!info->length && !rb->close) {
150 int l = info->chunk_remaining;
151 if (l > rb->len) l = rb->len;
153 - if (add_fragment(c->cache, c->from, rb->data, l) == 1) c->tries = 0;
154 info->chunk_remaining -= l;
159 + unsigned char *data = decompress_gzip(&c->z, rb->data, l, &dl);
161 + setcstate(c, S_OUT_OF_MEM);
162 + abort_connection(c);
165 + if (add_fragment(c->cache, c->from, data, dl) == 1) c->tries = 0;
171 + if (add_fragment(c->cache, c->from, rb->data, l) == 1) c->tries = 0;
174 kill_buffer_data(rb, l);
175 if (!info->chunk_remaining && rb->len >= 1) {
176 if (rb->data[0] == 10) kill_buffer_data(rb, 1);
178 if (!e->last_modified && (d = parse_http_header(e->head, "Date", NULL)))
179 e->last_modified = d;
180 if (info->length == -1 || (version < 11 && info->close)) rb->close = 1;
182 + d = parse_http_header(e->head, "Content-Encoding", NULL);
185 + if (!strcasecmp(d, "gzip") || !strcasecmp(d, "x-gzip")) c->gzip = 1;
189 read_http_data(c, rb);
192 --- elinks/src/util/compress.c Wed May 15 06:40:24 2002
193 +++ elinks_with_gzip/src/util/compress.c Wed May 15 06:37:29 2002
195 #include "util/compress.h"
200 z_mem_alloc(void *opaque, int items, int size)
209 struct decoding_handlers {
210 @@ -111,74 +109,162 @@
211 gzclose((gzFile *) stream->data);
215 -static unsigned char *
216 -decompress_gzip(unsigned char *stream, int cur_size, int *new_size)
219 - char *stream_pos = stream;
220 - char method, flags;
224 +struct decoding_handlers gzip_handlers = {
230 - output = mem_alloc(cur_size * 4);
231 - if (!output) return stream;
233 +#define ASCII_FLAG 0x01
234 +#define HEAD_CRC 0x02
235 +#define EXTRA_FIELD 0x04
236 +#define ORIG_NAME 0x08
237 +#define COMMENT 0x10
238 +#define RESERVED 0xE0
241 - z.zalloc = (alloc_func) z_mem_alloc;
242 - z.zfree = z_mem_free;
243 - z.next_in = stream_pos;
244 - z.next_out = output;
245 - z.avail_out = size = cur_size * 4;
246 - z.avail_in = cur_size + 1;
247 +static z_streamp gzip_init(unsigned char *buf_old, int l) {
251 + unsigned char method;
252 + unsigned char flags;
253 + unsigned char *buf = buf_old;
258 + if (buf[0] != 0x1f || buf[1] != 0x8b) return NULL;
263 + if (method != Z_DEFLATED || (flags & RESERVED) != 0) return NULL;
265 - /* XXX: Why -15? --pasky */
266 - ret = inflateInit2(&z, -15);
267 +/* Comments are borrowed from gzio.c - zlib */
268 +/* Discard time, xflags and OS code: */
272 + if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
273 + len = 2 + buf[0] + (buf[1] << 8);
277 + if (l <= 0) return NULL;
279 + if ((flags & ORIG_NAME) != 0) {/* skip the original file name */
280 + len = strlen(buf) + 1;
284 + if (l <= 0) return NULL;
286 + if ((flags & COMMENT) != 0) {/* skip the .gz file comment */
287 + len = strlen(buf) + 1;
291 + if (l <= 0) return NULL;
293 - while (ret == Z_OK) {
295 + if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
299 + if (l <= 0) return NULL;
301 - ret = inflate(&z, Z_SYNC_FLUSH);
302 +/* initialization of z_stream */
303 + z = (z_streamp)mem_alloc(sizeof(z_stream));
304 + if (!z) return NULL;
307 + z->zalloc = (alloc_func)z_mem_alloc;
308 + z->zfree = (free_func)z_mem_free;
311 +/* windowBits is passed < 0 to tell that there is no zlib header.
312 + * Note that in this case inflate *requires* an extra "dummy" byte
313 + * after the compressed stream in order to complete decompression and
314 + * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
315 + * present after the compressed stream.
317 + ret = inflateInit2(z, -WMAXBITS);
318 + if (ret == Z_OK) return z;
324 - if (ret == Z_STREAM_END) {
326 - *new_size = (int) z.total_out;
327 - output = mem_realloc(output, z.total_out);
331 +#define OUTPUT_BUFFER_SIZE 65536
336 +unsigned char *decompress_gzip(z_streamp *z, unsigned char *buf, int l, int *dl)
338 + unsigned char *output;
344 + *z = gzip_init(buf, l);
351 + (*z)->next_in = buf;
352 + (*z)->avail_in = l;
355 - size += cur_size * 4;
357 - output_new = mem_realloc(output, size);
362 + (*z)->total_out = 0L;
363 + cur_size = OUTPUT_BUFFER_SIZE;
364 + output = mem_alloc(cur_size); /* output will be freed in http_read_data */
373 + (*z)->next_out = output;
374 + (*z)->avail_out = 65536;
376 + ret = inflate(*z, Z_SYNC_FLUSH);
377 + while (ret == Z_OK) {
378 + if (!(*z)->avail_in) {
379 + *dl = (int)(*z)->total_out;
383 - output = output_new;
384 - z.avail_out += cur_size * 4;
385 - z.next_out = output + z.total_out;
387 + new_size = cur_size + OUTPUT_BUFFER_SIZE;
388 + output = mem_realloc(output, new_size);
397 + (*z)->next_out = output + cur_size; /* assume that z->avail_out == 0 */
398 + (*z)->avail_out = OUTPUT_BUFFER_SIZE;
399 + cur_size = new_size;
400 + ret = inflate(*z, Z_SYNC_FLUSH);
404 + if (ret == Z_STREAM_END) *dl = (int)(*z)->total_out;
405 + else { /* something went wrong */
419 -struct decoding_handlers gzip_handlers = {
427 --- elinks/src/util/compress.h Wed May 15 06:40:24 2002
428 +++ elinks_with_gzip/src/util/compress.h Wed May 15 01:35:11 2002
430 #ifndef EL__UTIL_COMPRESS_H
431 #define EL__UTIL_COMPRESS_H
433 +#ifdef HAVE_CONFIG_H
440 enum stream_encoding {
444 struct stream_encoded *open_encoded(int, enum stream_encoding);
445 int read_encoded(struct stream_encoded *, unsigned char *, int);
446 void close_encoded(struct stream_encoded *);
448 +unsigned char *decompress_gzip(z_streamp *, unsigned char *, int, int *);