Skip to content

Commit 276e12d

Browse files
committed
[proxy] fix stalled requests on seeking
and minor cache fix
1 parent c1d1be0 commit 276e12d

3 files changed

Lines changed: 56 additions & 14 deletions

File tree

build/server.js

Lines changed: 27 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/server/HttpServer.hx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,21 @@ class HttpServer {
409409
function proxyUrl(req:IncomingMessage, res:ServerResponse):Bool {
410410
final url = req.url.replace("/proxy?url=", "");
411411
final proxy = proxyRequest(url, req, res, proxyRes -> {
412-
final url = proxyRes.headers["location"] ?? return false;
413-
final proxy2 = proxyRequest(url, req, res, proxyRes -> false);
412+
final redirectUrl = proxyRes.headers["location"] ?? return false;
413+
final proxy2 = proxyRequest(redirectUrl, req, res, proxyRes -> false);
414414
if (proxy2 == null) {
415-
res.end('Proxy error: multiple redirects for url $url');
415+
if (!res.headersSent) {
416+
res.end('Proxy error: multiple redirects for url $redirectUrl');
417+
}
416418
return true;
417419
}
418420
req.pipe(proxy2);
421+
req.on("error", () -> proxy2.destroy());
419422
return true;
420423
});
421424
if (proxy == null) return false;
422425
req.pipe(proxy);
426+
req.on("error", () -> proxy.destroy());
423427
return true;
424428
}
425429

@@ -443,16 +447,34 @@ class HttpServer {
443447
};
444448
req.headers["referer"] = url.toString();
445449
req.headers["host"] = url.hostname;
450+
446451
final request = url.protocol == "https:" ? Https.request : Http.request;
447452
final proxy = request(options, proxyRes -> {
448-
if (cancelProxyRequest(proxyRes)) return;
453+
if (cancelProxyRequest(proxyRes)) {
454+
proxyRes.destroy();
455+
return;
456+
}
449457
proxyRes.headers["content-type"] = "application/octet-stream";
450458
res.writeHead(proxyRes.statusCode, proxyRes.headers);
451459
proxyRes.pipe(res);
460+
461+
// clean up when response ends
462+
proxyRes.on("end", () -> {
463+
if (!res.writableEnded) res.end();
464+
});
452465
});
466+
453467
proxy.on("error", err -> {
454-
res.end('Proxy error: ${url.href}');
468+
proxy.destroy();
469+
if (!res.headersSent) {
470+
res.end('Proxy error: ${url.href}');
471+
}
455472
});
473+
// clean up when client disconnects (seeking/abort)
474+
res.on("close", () -> {
475+
if (!proxy.destroyed) proxy.destroy();
476+
});
477+
456478
return proxy;
457479
}
458480

src/server/cache/YoutubeCache.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ class YoutubeCache {
198198
});
199199

200200
dlVideo.then((v) -> {
201-
final name = cache.findFile(n -> n.startsWith(inVideoName) && n.endsWith(".mp4")) ?? {
201+
final name = cache.findFile(n -> n.startsWith(inVideoName)) ?? {
202202
final err = 'Error: cannot find downloaded file with prefix $inVideoName';
203203
cache.logWithAdmins(client, err);
204+
cancelProgress(clientName);
204205
return;
205206
};
206207
FileSystem.rename('${cache.cacheDir}/$name', '${cache.cacheDir}/$outName');

0 commit comments

Comments
 (0)