Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Gzip is working.

  • Loading branch information...
commit 88b4c7e75c5398e6d59f835830cd61b4d26db8ff 1 parent 1cd8f30
Sean MacLennan authored

Showing 3 changed files with 70 additions and 74 deletions. Show diff stats Hide diff stats

  1. +10 6 get-comics.h
  2. +0 2  http-get.c
  3. +60 66 http.c
16 get-comics.h
@@ -12,13 +12,18 @@
12 12 #include <sys/time.h>
13 13 #include <sys/poll.h>
14 14 #include <sys/socket.h>
15   -
16   -# ifdef WANT_GZIP
17   -# include <zlib.h>
18   -# endif
19 15 #endif
20 16 #include <sys/stat.h>
21 17
  18 +
  19 +#ifdef WANT_GZIP
  20 +#include <zlib.h>
  21 +#else
  22 +#define z_stream void
  23 +static inline int inflateEnd(void *strm) { return -1; }
  24 +#endif
  25 +
  26 +
22 27 #define HTTP_PORT 80
23 28 #define JSON_FILE "/usr/share/get-comics/comics.json"
24 29
@@ -55,6 +60,7 @@ struct log {
55 60 int failed;
56 61 };
57 62
  63 +
58 64 struct connection {
59 65 char *url;
60 66 char *host; /* filled by read_config */
@@ -79,10 +85,8 @@ struct connection {
79 85 int rlen;
80 86 char *curp; /* for chunking */
81 87 char *endp; /* for chunking */
82   -#ifdef WANT_GZIP
83 88 z_stream *zs; /* for gzip */
84 89 unsigned char *zs_buf; /* for gzip */
85   -#endif
86 90 int length; /* content length if available */
87 91 enum {
88 92 CS_NONE,
2  http-get.c
@@ -167,8 +167,6 @@ static void read_conn(struct connection *conn)
167 167 conn->rlen -= n;
168 168 *conn->endp = '\0';
169 169
170   - fputs(conn->curp, stdout);
171   -
172 170 if (conn->func && conn->func(conn))
173 171 fail_connection(conn);
174 172 } else if (n < 0)
126 http.c
@@ -34,10 +34,8 @@ int gotit;
34 34 static int read_file(struct connection *conn);
35 35 static int read_file_unsized(struct connection *conn);
36 36 static int read_file_chunked(struct connection *conn);
37   -#ifdef WANT_GZIP
38 37 static int gzip_init(struct connection *conn);
39 38 static int read_file_gzip(struct connection *conn);
40   -#endif
41 39
42 40
43 41 /* This is only for 2 stage comics and redirects */
@@ -63,6 +61,14 @@ int release_connection(struct connection *conn)
63 61
64 62 conn->connected = 0;
65 63
  64 + if (conn->zs) {
  65 + inflateEnd(conn->zs);
  66 + free(conn->zs);
  67 + conn->zs = NULL;
  68 + if (conn->zs_buf)
  69 + free(conn->zs_buf);
  70 + }
  71 +
66 72 return 0;
67 73 }
68 74
@@ -193,12 +199,14 @@ static void add_full_header(struct connection *conn)
193 199 "Accept-Language: en-us,en;q=0.5\r\n");
194 200 n += snprintf(conn->buf + n, BUFSIZE - n,
195 201 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
  202 +#endif
196 203
197 204 #ifdef WANT_GZIP
198 205 n += snprintf(conn->buf + n, BUFSIZE - n,
199 206 "Accept-Encoding: gzip, deflate\r\n");
200 207 #endif
201 208
  209 +#ifdef FULL_HEADER
202 210 n += snprintf(conn->buf + n, BUFSIZE - n,
203 211 "Connection: keep-alive\r\n");
204 212 #endif
@@ -447,15 +455,10 @@ int read_reply(struct connection *conn)
447 455 while (isspace(*p))
448 456 ++p;
449 457 if (strncmp(p, "gzip", 4) == 0) {
450   -#ifdef WANT_GZIP
451 458 if (verbose > 1)
452 459 printf("GZIP\n");
453 460 if (gzip_init(conn))
454 461 return 1;
455   -#else
456   - printf("No GZIP support\n");
457   - return 1;
458   -#endif
459 462 } else
460 463 printf("OH oh. %s: %s", conn->host, p);
461 464 }
@@ -515,10 +518,8 @@ int read_reply(struct connection *conn)
515 518 conn->cstate = CS_DIGITS;
516 519 NEXT_STATE(conn, read_file_chunked);
517 520 conn->length = 0; /* paranoia */
518   -#ifdef WANT_GZIP
519 521 } else if (conn->zs) {
520 522 NEXT_STATE(conn, read_file_gzip);
521   -#endif
522 523 } else if (conn->length == 0)
523 524 NEXT_STATE(conn, read_file_unsized);
524 525 else
@@ -724,15 +725,10 @@ static int gzip_init(struct connection *conn)
724 725 return 1;
725 726 }
726 727
727   - if (inflateInit2(conn->zs, 15 + 32)) {
728   - printf("inflateInit failed\n");
729   - free(conn->zs_buf);
730   - free(conn->zs);
731   - conn->zs = NULL;
  728 + /* Window size 15 is default for zlib. Adding 32 allows us to
  729 + * handle gzip format. */
  730 + if (inflateInit2(conn->zs, 15 + 32))
732 731 return 1;
733   - }
734   -
735   -// conn->zs->data_type = Z_TEXT;
736 732
737 733 return 0;
738 734 }
@@ -741,73 +737,71 @@ static int gzip_init(struct connection *conn)
741 737 static int read_file_gzip(struct connection *conn)
742 738 {
743 739 z_stream *zs = conn->zs;
744   - size_t bytes, zs_bytes;
  740 + size_t bytes;
745 741 int rc;
746 742
747 743 bytes = conn->endp - conn->curp;
748   - if (bytes > 0) {
749   - printf("==> next_in %p avail_in %u total_in %lu\n",
750   - zs->next_in, zs->avail_in, zs->total_in);
751   - printf(" next_out %p avail_out %u total_out %lu\n",
752   - zs->next_out, zs->avail_out, zs->total_out);
753   -
754   - zs->next_in = (unsigned char *)conn->curp;
755   - zs->avail_in = conn->endp - conn->curp;
756   -
757   - do {
758   - zs->next_out = conn->zs_buf;
759   - zs->avail_out = BUFSIZE;
760   -
  744 + if (bytes <= 0) {
  745 + printf("Read file problems %d for %s!\n", bytes, conn->url);
  746 + return 1;
  747 + }
761 748
762   - rc = inflate(zs, Z_SYNC_FLUSH);
  749 + zs->next_in = (unsigned char *)conn->curp;
  750 + zs->avail_in = bytes;
763 751
764   - printf("--> next_in %p avail_in %u total_in %lu\n",
765   - zs->next_in, zs->avail_in, zs->total_in);
766   - printf(" next_out %p avail_out %u total_out %lu\n",
767   - zs->next_out, zs->avail_out, zs->total_out);
768   - printf("rc %d\n", rc);
  752 + /* Inflate and write all output until we are done with the
  753 + * input buffer. */
  754 + /* SAM Should we do one write then switch to another state? */
  755 + do {
  756 + zs->next_out = conn->zs_buf;
  757 + zs->avail_out = BUFSIZE;
769 758
770   - if (rc == Z_BUF_ERROR)
771   - break; /* not fatal */
  759 + rc = inflate(zs, Z_SYNC_FLUSH);
772 760
773   - if (rc == Z_OK || rc == Z_STREAM_END) {
774   - zs_bytes = BUFSIZE - zs->avail_out;
  761 + switch (rc) {
  762 + case Z_BUF_ERROR:
  763 + break; /* not a problem */
775 764
776   - if (!write_output(conn, zs_bytes)) {
777   - printf("Write error\n");
778   - return 1;
779   - }
780   - } else {
781   -#if 0
782   - int fd = open("/tmp/http-dump", O_WRONLY|O_TRUNC|O_CREAT, 0666);
783   - write(fd, conn->curp, bytes);
784   - close(fd);
785   -#endif
786   - printf("Inflate failed: %d\n", rc);
  765 + case Z_OK:
  766 + case Z_STREAM_END:
  767 + if (!write_output(conn, BUFSIZE - zs->avail_out)) {
  768 + printf("Write error\n");
787 769 return 1;
788 770 }
789   - } while (zs->avail_out == 0);
790   -
  771 + break;
791 772
792   - conn->length -= bytes;
793   - if (conn->length <= 0) {
794   - if (verbose)
795   - printf("OK %s\n", conn->url);
796   - if (conn->regexp && !conn->matched)
797   - return process_html(conn);
798   - close_connection(conn);
799   - return 0;
  773 + default:
  774 + printf("Inflate failed: %d\n", rc);
  775 + return 1;
800 776 }
801   - } else {
802   - printf("Read file problems %d for %s!\n",
803   - bytes, conn->url);
804   - return 1;
  777 + } while (zs->avail_out == 0);
  778 +
  779 +
  780 + conn->length -= bytes;
  781 + if (conn->length <= 0 || rc == Z_STREAM_END) {
  782 + if (verbose)
  783 + printf("OK %s\n", conn->url);
  784 + if (conn->regexp && !conn->matched)
  785 + return process_html(conn);
  786 + close_connection(conn);
  787 + return 0;
805 788 }
806 789
807 790 conn->curp = conn->buf;
808 791 conn->rlen = conn->bufn;
809 792 return 0;
810 793 }
  794 +#else
  795 +static int gzip_init(struct connection *conn)
  796 +{
  797 + puts("Sorry, no GZIP support.");
  798 + return 1;
  799 +}
  800 +
  801 +static int read_file_gzip(struct connection *conn)
  802 +{
  803 + return 1;
  804 +}
811 805 #endif
812 806
813 807 /* State function */

0 comments on commit 88b4c7e

Please sign in to comment.
Something went wrong with that request. Please try again.