http: download interrompido continua de onde parou
Um <arquivo>.parte que ja existe vai no pedido como "Range: bytes=N-": com 206 o download anexa ao .parte; com 200 (servidor sem Range) recomeca; com 416 apaga o .parte e tenta do zero. O .parte nao e mais apagado num erro de transferencia, para a proxima tentativa continuar. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51f55e60ff
commit
829bc67142
2 files changed
+58
-11
No files matched your search
+32
-2
@@ -24,10 +24,12 @@ static int fails = 0;
|
||||
|
||||
/* -- servidor local ------------------------------------------------------- */
|
||||
static int s_port;
|
||||
static int s_range_from = -1; /* o servidor recebeu "Range: bytes=N-" */
|
||||
static const char* BIG = "0123456789ABCDEFGHIJ0123456789ABCDEFGHIJ0123456789ABCDEFGHIJ0123456789ABCDEFGHIJ0123456789ABCDEFGHIJ";
|
||||
|
||||
static void* server(void* arg) {
|
||||
int ls = *(int*)arg;
|
||||
for (int k = 0; k < 6; k++) {
|
||||
for (int k = 0; k < 7; k++) {
|
||||
int c = accept(ls, NULL, NULL);
|
||||
if (c < 0) break;
|
||||
char req[2048] = "";
|
||||
@@ -45,7 +47,16 @@ static void* server(void* arg) {
|
||||
}
|
||||
const char* resp;
|
||||
char buf[512];
|
||||
if (strstr(req, "GET /chunk ")) resp = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nola, \r\n6\r\nmundo!\r\n0\r\n\r\n";
|
||||
if (strstr(req, "GET /grande ")) {
|
||||
const char* rg = strstr(req, "Range: bytes=");
|
||||
int from = rg ? atoi(rg + 13) : 0;
|
||||
if (rg) s_range_from = from;
|
||||
size_t len = strlen(BIG) - (size_t)from;
|
||||
snprintf(buf, sizeof(buf), rg ? "HTTP/1.1 206 Partial Content\r\nContent-Length: %zu\r\n\r\n%s"
|
||||
: "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s", len, BIG + from);
|
||||
resp = buf;
|
||||
}
|
||||
else if (strstr(req, "GET /chunk ")) resp = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nola, \r\n6\r\nmundo!\r\n0\r\n\r\n";
|
||||
else if (strstr(req, "GET /redir ")) resp = "HTTP/1.1 302 Found\r\nLocation: /len\r\nContent-Length: 0\r\n\r\n";
|
||||
else if (strstr(req, "GET /len ")) resp = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\ncom tamanho!";
|
||||
else if (strstr(req, "POST /eco ")) {
|
||||
@@ -130,6 +141,25 @@ int main(void) {
|
||||
char txt[64];
|
||||
tb_value_to_text(con->outputs[0], txt, sizeof(txt));
|
||||
CHECK(!strcmp(txt, "conexão tcp aberta"), "a conexão aparece como \"conexão tcp aberta\"");
|
||||
/* download que continua: o .parte tem os 40 primeiros bytes */
|
||||
{
|
||||
FILE* pf = fopen("/tmp/tb_http_resume.bin.parte", "wb");
|
||||
fwrite(BIG, 1, 40, pf); fclose(pf);
|
||||
remove("/tmp/tb_http_resume.bin");
|
||||
snprintf(url, sizeof(url), "http://127.0.0.1:%d/grande", s_port);
|
||||
TinyBlocks_Block* dl = blk(rt, "Http.Download", url, "/tmp/tb_http_resume.bin");
|
||||
tb_block_eval(dl, rt);
|
||||
TinyBlocks_Block* dn = blk(rt, "Http.Done", "/tmp/tb_http_resume.bin", NULL);
|
||||
for (int w = 0; w < 100; w++) {
|
||||
dn->dirty = true; tb_block_eval(dn, rt);
|
||||
if (dn->outputs[0].as.integer) break;
|
||||
struct timespec ts = { 0, 20000000 }; nanosleep(&ts, NULL);
|
||||
}
|
||||
char got[256] = "";
|
||||
FILE* f = fopen("/tmp/tb_http_resume.bin", "rb");
|
||||
if (f) { size_t k = fread(got, 1, sizeof(got) - 1, f); got[k] = 0; fclose(f); }
|
||||
CHECK(s_range_from == 40 && !strcmp(got, BIG), "download interrompido continua (Range: bytes=40-, 206)");
|
||||
}
|
||||
pthread_join(th, NULL);
|
||||
|
||||
/* com internet */
|
||||
|
||||
Reference in new issue
Block a user