Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloadで途中で切断された場合に正常終了してしまう #1020

Closed
mei23 opened this issue Apr 10, 2020 · 9 comments · Fixed by #1729
Closed

downloadで途中で切断された場合に正常終了してしまう #1020

mei23 opened this issue Apr 10, 2020 · 9 comments · Fixed by #1729
Labels
🐛Bug あらまあバグだわ ⚙️Server サーバーだわ

Comments

@mei23
Copy link
Owner

mei23 commented Apr 10, 2020

💡 Summary

import * as net from  'net';

const server = net.createServer(c => {
	console.log(`client connected: ${c.remoteAddress}:${c.remotePort}`);

	c.on('data', data => {
		console.log(data + ' from ' + c.remoteAddress + ':' + c.remotePort);
		c.write(`HTTP/1.0 200 OK\r\n`);
		c.write(`Content-Length: 10\r\n`);
		c.write(`\r\n`);
		c.write(`aaa`);
		c.destroy();
	});

	c.on('end', () => {
		console.log('client disconnected');
	});
}).listen(3331);

console.log('listening on port 3331');

みたいな10バイトあるはずが3バイトしか送らないで切断しても
3バイトのファイルをDLして正常終了してしまう。

note-fetchだけではなくrequestのバージョンでも起こる

🙂 Expected Behavior

☹️ Actual Behavior

📝 Steps to Reproduce

📌 Environment

@u1-liquid
Copy link

https://tools.ietf.org/html/rfc7230#section-3.4

A message that uses a valid Content-Length is incomplete if the size of the message body received (in octets) is less than the value given by Content-Length. A response that has neither chunked transfer coding nor Content-Length is terminated by closure of the connection and, thus, is considered complete regardless of the number of message body octets received, provided that the header section was received intact.

えー

@mei23
Copy link
Owner Author

mei23 commented Apr 10, 2020

↑たぶん特に変な事は書いてないです

1

Content-Length: 10

だったら10バイト受信したら正常終了すればいい

2

Content-Length: なし
Transfer-Encoding: chunked

だったら応答終了をマークするchunkを受信できたら正常終了すればいい

3

Content-Length: なし
Transfer-Encoding: なし

なら接続断を検知したら正常終了とすればいい

って意味でほとんどのサーバーは1, 2のいずれかなので問題ないです。


今問題になってるのは、
1, 2 であきらかに受信したものが足りなくても、response stream で error イベントが上がらないので、enexcepted closeが検知できないという点です。

@mei23
Copy link
Owner Author

mei23 commented Apr 10, 2020

これ requestでもnode-fetchでもgotでもaxiosでもsuperagentでも起きるので、ハンドル無理かも。

@mei23
Copy link
Owner Author

mei23 commented Apr 10, 2020

HTTP/1.0で送り出すようにすればContent-Length付けてくれる可能性が高くなるので、
それでContent-LengthあったらDL後に比較するとかすればましになるかも。

@mei23
Copy link
Owner Author

mei23 commented Apr 11, 2020

とりあえずこんな感じ?
https://github.com/mei23/newreq/blob/6362fa1dfe97d07ee95f4ac967aa18c28a666f40/src/dl.ts#L1

HTTP/1.0を指定する方法はなさそうだった

@mei23
Copy link
Owner Author

mei23 commented Apr 11, 2020

とりあえずContent-Lengthが取得できたら比較することに
8ae4911
6599903

@mei23
Copy link
Owner Author

mei23 commented Apr 11, 2020

Node http で、socket, request, response の error インベントlistenしてもerrorなんて起きなかった。
自分でsocket喋るしかない?

@mei23
Copy link
Owner Author

mei23 commented Oct 12, 2020

Node http で aborted イベントをListenすればいいみたい
https://github.com/mei23/newreq/blob/bcd2d9bbef757d80f524e0334e111a0539ee19be/src/http.ts#L51

でも request も node-fetch も対応してない様子

@mei23
Copy link
Owner Author

mei23 commented Oct 12, 2020

gotがちゃんとしてるみたい

@mei23 mei23 mentioned this issue Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛Bug あらまあバグだわ ⚙️Server サーバーだわ
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants