diff --git a/src/util.c b/src/util.c
index 8441b584ff199ffd81472539a59acce5d0a18f42..1d44c07b609cb61433f554f3b5dc3714f2365e48 100644
--- a/src/util.c
+++ b/src/util.c
@@ -9,6 +9,7 @@ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <unistd.h>
#include "util.h"
void
@@ -83,10 +84,13 @@ }
fprintf(out, "Downloading %s to %s\n", url, path);
char buf[BUFSIZ];
for (int n = 1; n > 0;) {
- n = br_sslio_read(&resp.body, buf, sizeof(buf));
- if (n == -1) {
- fprintf(stderr, "Error: read\n");
- return 1;
+ if (resp.sc) {
+ n = br_sslio_read(&resp.body, buf, BUFSIZ);
+ } else {
+ n = read(resp.fd, buf, BUFSIZ);
+ }
+ if (n < 0) {
+ break;
}
ssize_t w = 0;
while (w < (ssize_t)n) {