From a9899873198ffc1124f89b13c24091ab913a5ef1 Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Tue, 8 Feb 2022 12:37:06 +0100 Subject: fall back to server after reaching maximum number of retries Use the HTTP header field: X-Accept-Redirect: off to ask for a direct download from the server. --- src/main.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 3e88308..7785150 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ struct tip_client { int fd; bool error; bool redirected; + bool server_only; struct timeval tv_start, tv_last; const char *payload; }; @@ -400,7 +401,10 @@ static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int ev case TIP_CLIENT_GET_HEADER: syslog(LOG_INFO, "connected to %s to fetch file %s\n", inet_ntoa(cli->addr.sin_addr), filename); - snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n\r\n", filename); + if (cli->server_only) + snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\nX-Accept-Redirect: off\r\n\r\n", filename); + else + snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n\r\n", filename); break; case TIP_CLIENT_HEAD_HEADER: syslog(LOG_INFO, "connected to %s to get file size of %s\n", @@ -581,6 +585,19 @@ int main(int argc, char *argv[]) ret = tip_client_request_file(&_cli, addr, filename); } while (ret > 0); + if (ret < 0) { + memset(&_cli, 0, sizeof(_cli)); + _cli.chunk_offset = chunk_size * k; + _cli.fd = fd; + _cli.server_only = true; + + do { + syslog(LOG_INFO, "Requesting file %s to server only\n", filename); + _cli.state = TIP_CLIENT_GET_HEADER; + ret = tip_client_request_file(&_cli, addr, filename); + } while (ret > 0); + } + if (ret < 0) goto err_max_retries; -- cgit v1.2.3-18-g5258