]> pd.if.org Git - zpackage/blobdiff - src/fetchurl.c
switch to blake2
[zpackage] / src / fetchurl.c
index 2f144d6dba72b72638a5aca6573fd13836845d68..6d5958848ecdb9c2e9091824841cefa92b4229f3 100644 (file)
@@ -15,7 +15,6 @@
 #include <fcntl.h>
 
 #include "tlse.h"
-#define MARK fprintf(stderr, "%s %s:%d\n", __FILE__, __func__, __LINE__)
 
 struct tls_uri {
        char *scheme;
@@ -23,9 +22,12 @@ struct tls_uri {
        char *host;
        char *port;
        char *path;
+       char *encoded_path;
        char *query;
+       char *encoded_query;
        char *fragment;
 };
+
 int tls_parse_uri(char *, struct tls_uri *);
 void tls_free_uri(struct tls_uri *);
 
@@ -207,6 +209,11 @@ int verify_first(struct TLSContext *context, struct TLSCertificate **chain, int
                }
 
                int match = (memcmp(certhash, fp, 64) == 0); 
+               if (!match) {
+                       fprintf(stderr, "host %s certificate changed\n", host);
+                       fprintf(stderr, "was %.64s\n", fp);
+                       fprintf(stderr, "now %.64s\n", certhash);
+               }
 
                close(trustdb);
                tls_buffer_free(&tbuf);
@@ -579,6 +586,7 @@ int main(int ac, char *av[]) {
        struct tls_buffer request;
        char lmtime[80];
        char *eoh = 0;
+       char *user_agent = 0;
        size_t total = 0;
        size_t header_len;
        char *url = 0;
@@ -587,13 +595,14 @@ int main(int ac, char *av[]) {
 
        ltc_mp = tfm_desc;
 
-       while ((option = getopt(ac, av, "o:OrIfz:np#R:SkK")) != -1) {
+       while ((option = getopt(ac, av, "o:OrIfz:np#R:SkKU:")) != -1) {
                switch (option) {
                        case 'o': outfile = optarg; break;
                        case 'O': calcoutfile = 1; break;
                        case 'S': printstatus = 1; head = 1; break;
                        case 'k': verifypolicy = 0; break;
                        case 'K': verifypolicy = 2; break;
+                       case 'U': user_agent = optarg; break;
                        case 'I': head = 1;
                        case 'r': raw = 1; break;
                        case 'f': failsilent = 1; break;
@@ -676,10 +685,18 @@ int main(int ac, char *av[]) {
                } else {
                        tls_buffer_append(&request, "GET ", 4);
                }
-               tls_buffer_append(&request, uri.path, strlen(uri.path));
+               tls_buffer_append(&request, uri.encoded_path, strlen(uri.encoded_path));
+               if (uri.encoded_query) {
+                       tls_buffer_append(&request, "?", 1);
+                       tls_buffer_append(&request, uri.encoded_query, strlen(uri.encoded_query));
+               }
                tls_buffer_append(&request, " HTTP/1.1\r\n", 11);
 
                append_header(&request, "Host", host);
+               if (user_agent) {
+                       append_header(&request, "User-Agent", user_agent);
+               }
+               append_header(&request, "Accept", "*/*");
                append_header(&request, "Connection", "close");
                if (lmfile) {
                        append_header(&request, "If-Modified-Since", lmtime);
@@ -755,6 +772,7 @@ int main(int ac, char *av[]) {
 
                io.socket = sockfd;
 
+               eoh = 0;
                do {
                        if (io.response.len >= 4) {
                                eoh = strstr(io.response.buffer, "\r\n\r\n");
@@ -768,8 +786,10 @@ int main(int ac, char *av[]) {
                } while (!eoh);
 
                if (!eoh) {
-                       /* never got (complet) header */
-                       fprintf(stderr, "incomplete response to %s\n", av[optind]);
+                       /* never got (complete) header */
+                       fprintf(stderr, "incomplete response (ret = %zd) to %s\n", ret, url);
+                       fprintf(stderr, "have:\n");
+                       fwrite(io.response.buffer, io.response.len, 1, stderr);
                        exit(EXIT_FAILURE);
                }
 
@@ -778,6 +798,7 @@ int main(int ac, char *av[]) {
 
                switch (io.status_code) {
                        case 304:
+                               progressbar = 0;
                                break;
                        case 301:
                        case 302:
@@ -826,11 +847,8 @@ int main(int ac, char *av[]) {
                                        pdots(50, '.', total, total+ret,
                                                        io.content_length);
                                } else {
-                                       int old = total / 1000000;
-                                       int new = (total+ret)/1000000;
-                                       while (old < new) {
-                                               putc('.',stderr);
-                                       }
+                                       putc('\r', stderr);
+                                       fprintf(stderr, "%zu", total+ret);
                                }
                                total += ret;
                        }