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")) {
187 + d = parse_http_header(e->head, "Content-Type", NULL);
189 + if (!strncasecmp(d, "text", 4)) c->gzip = 1;
195 read_http_data(c, rb);
198 --- elinks/src/util/compress.c Wed May 15 06:40:24 2002
199 +++ elinks_with_gzip/src/util/compress.c Wed May 15 06:37:29 2002
201 #include "util/compress.h"
206 z_mem_alloc(void *opaque, int items, int size)
215 struct decoding_handlers {
216 @@ -111,74 +109,162 @@
217 gzclose((gzFile *) stream->data);
221 -static unsigned char *
222 -decompress_gzip(unsigned char *stream, int cur_size, int *new_size)
225 - char *stream_pos = stream;
226 - char method, flags;
230 +struct decoding_handlers gzip_handlers = {
236 - output = mem_alloc(cur_size * 4);
237 - if (!output) return stream;
239 +#define ASCII_FLAG 0x01
240 +#define HEAD_CRC 0x02
241 +#define EXTRA_FIELD 0x04
242 +#define ORIG_NAME 0x08
243 +#define COMMENT 0x10
244 +#define RESERVED 0xE0
247 - z.zalloc = (alloc_func) z_mem_alloc;
248 - z.zfree = z_mem_free;
249 - z.next_in = stream_pos;
250 - z.next_out = output;
251 - z.avail_out = size = cur_size * 4;
252 - z.avail_in = cur_size + 1;
253 +static z_streamp gzip_init(unsigned char *buf_old, int l) {
257 + unsigned char method;
258 + unsigned char flags;
259 + unsigned char *buf = buf_old;
264 + if (buf[0] != 0x1f || buf[1] != 0x8b) return NULL;
269 + if (method != Z_DEFLATED || (flags & RESERVED) != 0) return NULL;
271 - /* XXX: Why -15? --pasky */
272 - ret = inflateInit2(&z, -15);
273 +/* Comments are borrowed from gzio.c - zlib */
274 +/* Discard time, xflags and OS code: */
278 + if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
279 + len = 2 + buf[0] + (buf[1] << 8);
283 + if (l <= 0) return NULL;
285 + if ((flags & ORIG_NAME) != 0) {/* skip the original file name */
286 + len = strlen(buf) + 1;
290 + if (l <= 0) return NULL;
292 + if ((flags & COMMENT) != 0) {/* skip the .gz file comment */
293 + len = strlen(buf) + 1;
297 + if (l <= 0) return NULL;
299 - while (ret == Z_OK) {
301 + if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
305 + if (l <= 0) return NULL;
307 - ret = inflate(&z, Z_SYNC_FLUSH);
308 +/* initialization of z_stream */
309 + z = (z_streamp)mem_alloc(sizeof(z_stream));
310 + if (!z) return NULL;
313 + z->zalloc = (alloc_func)z_mem_alloc;
314 + z->zfree = (free_func)z_mem_free;
317 +/* windowBits is passed < 0 to tell that there is no zlib header.
318 + * Note that in this case inflate *requires* an extra "dummy" byte
319 + * after the compressed stream in order to complete decompression and
320 + * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
321 + * present after the compressed stream.
323 + ret = inflateInit2(z, -WMAXBITS);
324 + if (ret == Z_OK) return z;
330 - if (ret == Z_STREAM_END) {
332 - *new_size = (int) z.total_out;
333 - output = mem_realloc(output, z.total_out);
337 +#define OUTPUT_BUFFER_SIZE 65536
342 +unsigned char *decompress_gzip(z_streamp *z, unsigned char *buf, int l, int *dl)
344 + unsigned char *output;
350 + *z = gzip_init(buf, l);
357 + (*z)->next_in = buf;
358 + (*z)->avail_in = l;
361 - size += cur_size * 4;
363 - output_new = mem_realloc(output, size);
368 + (*z)->total_out = 0L;
369 + cur_size = OUTPUT_BUFFER_SIZE;
370 + output = mem_alloc(cur_size); /* output will be freed in http_read_data */
379 + (*z)->next_out = output;
380 + (*z)->avail_out = 65536;
382 + ret = inflate(*z, Z_SYNC_FLUSH);
383 + while (ret == Z_OK) {
384 + if (!(*z)->avail_in) {
385 + *dl = (int)(*z)->total_out;
389 - output = output_new;
390 - z.avail_out += cur_size * 4;
391 - z.next_out = output + z.total_out;
393 + new_size = cur_size + OUTPUT_BUFFER_SIZE;
394 + output = mem_realloc(output, new_size);
403 + (*z)->next_out = output + cur_size; /* assume that z->avail_out == 0 */
404 + (*z)->avail_out = OUTPUT_BUFFER_SIZE;
405 + cur_size = new_size;
406 + ret = inflate(*z, Z_SYNC_FLUSH);
410 + if (ret == Z_STREAM_END) *dl = (int)(*z)->total_out;
411 + else { /* something went wrong */
425 -struct decoding_handlers gzip_handlers = {
433 --- elinks/src/util/compress.h Wed May 15 06:40:24 2002
434 +++ elinks_with_gzip/src/util/compress.h Wed May 15 01:35:11 2002
436 #ifndef EL__UTIL_COMPRESS_H
437 #define EL__UTIL_COMPRESS_H
439 +#ifdef HAVE_CONFIG_H
446 enum stream_encoding {
450 struct stream_encoded *open_encoded(int, enum stream_encoding);
451 int read_encoded(struct stream_encoded *, unsigned char *, int);
452 void close_encoded(struct stream_encoded *);
454 +unsigned char *decompress_gzip(z_streamp *, unsigned char *, int, int *);