]> pd.if.org Git - zpackage/blobdiff - src/fetchurl.c
remove stray debug fprintf
[zpackage] / src / fetchurl.c
index 15bf1ecb3c2efdbbcd5475c2cfb97865740a63e4..5ff67ddb2ddf257b69122159251cdcb02690c1eb 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "tlse.h"
 
+#define DEBUG(lvl, ...) if (debuglevel >= lvl ) { fprintf(stderr, __VA_ARGS__); }
+
 struct tls_uri {
        char *scheme;
        char *userinfo;
@@ -55,6 +57,8 @@ static void hex(char *dst, uint8_t *src, size_t len) {
        }
 }
 
+static int debuglevel = 0;
+
 #if 0
 static void hexbin(uint8_t *dst, unsigned char *src, size_t len) {
        size_t i;
@@ -335,18 +339,26 @@ char *find_header(struct io *io, char *header, size_t *len) {
        *len = 0;
 
        hlen = strlen(header);
+
+       /* TODO can't do this, buffer may not be zero terminated */
        eoh = strstr(io->response.buffer, "\r\n\r\n");
        if (!eoh) {
                return 0;
        }
+
        soh = io->response.buffer;
        do {
+               /* skip the first line for some reason */
                soh = strstr(soh, "\r\n");
                if (soh == eoh) {
                        break;
                }
                soh += 2;
-               if (!memcmp(soh, header, hlen)) {
+               /* done if not enough room */
+               if (hlen > (size_t)(eoh - soh)) {
+                       break;
+               }
+               if (!strncasecmp(soh, header, hlen)) {
                        break;
                }
        } while (soh < eoh);
@@ -413,6 +425,7 @@ void parse_header(struct io *io) {
                case 302:
                case 303:
                case 307:
+                       DEBUG(1, "looking for Location header\n");
                        hval = find_header(io, "Location:", &hlen);
                        if (hval) {
                                io->redirect = strndup(hval, hlen);
@@ -461,7 +474,6 @@ ssize_t fill_buffer(struct io *io) {
                }
        }
 
-       fprintf(stderr, "filled %zd bytes\n", ret);
        return ret;
 }
 
@@ -728,8 +740,9 @@ int main(int ac, char *av[]) {
 
        ltc_mp = tfm_desc;
 
-       while ((option = getopt(ac, av, "o:OrIfz:np#RL:SkKU:")) != -1) {
+       while ((option = getopt(ac, av, "do:OrIfz:np#RL:SkKU:")) != -1) {
                switch (option) {
+                       case 'd': debuglevel++; break;
                        case 'o': outfile = optarg; break;
                        case 'O': calcoutfile = 1; break;
                        case 'S': printstatus = 1; head = 1; break;
@@ -842,6 +855,7 @@ int main(int ac, char *av[]) {
                if (!strcmp(uri.scheme, "https")) {
                        use_tls = 1;
 
+                       DEBUG(1, "creating tls context\n");
                        clientssl = tls_create_context(TLS_CLIENT, TLS_V12);
 
                        /* optionally, we can set a certificate validation
@@ -860,10 +874,14 @@ int main(int ac, char *av[]) {
                                        fprintf(stderr, "Error loading root certs\n");
                                        return 1;
                                }
+                               DEBUG(1, "verifying ssl cert via roots\n");
                                tls_set_verify(clientssl, verify_roots);
                        } else if (verifypolicy == 1) {
+                               DEBUG(1, "verifying ssl cert via first use\n");
                                tls_set_verify(clientssl, verify_first);
+                               DEBUG(1, "verified ssl cert via first use\n");
                        } else {
+                               DEBUG(1, "verifying ssl cert via trust\n");
                                tls_set_verify(clientssl, verify_trust);
                        }
 
@@ -872,9 +890,11 @@ int main(int ac, char *av[]) {
                                return -1;
                        }
                        tls_sni_set(clientssl, uri.host);
+                       DEBUG(1, "set sni to %s\n", uri.host);
                        clientssl->sync = 1;
                        io.tls = clientssl;
                        sockfd = open_tcp_connection(host, port);
+                       DEBUG(1, "opened tcp socket fd %d\n", sockfd);
                        if (sockfd < 0) {
                                perror("can't open connection");
                                exit(EXIT_FAILURE);
@@ -901,6 +921,7 @@ int main(int ac, char *av[]) {
                        exit(EXIT_FAILURE);
                }
 
+               DEBUG(1, "wrote http request\n");
                if (ret == -1) {
                        fprintf(stderr, "unable to write http request: %s\n", strerror(errno));
                        exit(EXIT_FAILURE);
@@ -914,12 +935,14 @@ int main(int ac, char *av[]) {
                                eoh = strstr(io.response.buffer, "\r\n\r\n");
                        }
                        if (!eoh) {
+                               DEBUG(1, "filling buffer\n");
                                ret = fill_buffer(&io);
                                if (ret <= 0) {
                                        break;
                                }
                        }
                } while (!eoh);
+               DEBUG(1, "got response\n");
 
                if (!eoh) {
                        /* never got (complete) header */
@@ -932,6 +955,7 @@ int main(int ac, char *av[]) {
                header_len = (size_t)(eoh - io.response.buffer) + 4;
 
                parse_header(&io);
+               DEBUG(1, "parsed response header, code %d\n", io.status_code);
 
                switch (io.status_code) {
                        case 304:
@@ -941,8 +965,10 @@ int main(int ac, char *av[]) {
                        case 302:
                        case 303:
                        case 307:
+                               DEBUG(1, "redirecting to %s\n", io.redirect);
                                free(url);
                                url = strdup(io.redirect);
+                               DEBUG(1, "redirecting to %s\n", url);
                                close(io.socket);
                                continue;
                                break;
@@ -958,6 +984,12 @@ int main(int ac, char *av[]) {
 
                if (head) {
                        io.response.len -= 2;
+                       write(out, io.response.buffer, io.response.len);
+                       break;
+               }
+
+               if (io.status_code == 304) {
+                       break;
                }
 
                if (outfile) {
@@ -974,11 +1006,6 @@ int main(int ac, char *av[]) {
                        }
                }
 
-               if (head) {
-                       write(out, io.response.buffer, io.response.len);
-                       break;
-               }
-
                if (raw) {
                        write(out, io.response.buffer, header_len);
                }
@@ -999,6 +1026,7 @@ int main(int ac, char *av[]) {
                }
 
                do {
+                       size_t before = io.received;
                        if (io.response.len) {
                                if (io.content_length && io.response.len + io.received > io.content_length) {
                                        io.response.len = io.content_length - io.received;
@@ -1012,7 +1040,7 @@ int main(int ac, char *av[]) {
 
                        if (progressbar) {
                                if (io.content_length) {
-                                       pdots(50, '.', total, total+ret,
+                                       pdots(50, '.', before, io.received,
                                                        io.content_length);
                                } else {
                                        putc('\r', stderr);
@@ -1052,9 +1080,9 @@ int main(int ac, char *av[]) {
 
        close(sockfd);
        if (progressbar && io.status_code == 200) {
-               if (total == io.content_length || io.content_length == 0) {
+               if (io.received == io.content_length || io.content_length == 0) {
                        fprintf(stderr, " done\n");
-               } else if (io.content_length != total) {
+               } else if (io.content_length != io.received) {
                        fprintf(stderr, "failed (%zu bytes read)\n", total);
                        io.status_code = 531; /* non official code */
                }