Moorad / youtube-video-downloader

200 stars 137 forks source link

Bug: Invalid character in header content ["Content-Disposition"] #25

Closed chrisVillanueva closed 4 years ago

chrisVillanueva commented 4 years ago

Hello.

I ran into an issue when testing a download. I encountered this server error:

Server Works !!! At port 4000
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Content-Disposition"]
    at ServerResponse.setHeader (_http_outgoing.js:473:3)

Any ideas on how to resolve this?

Thanks!

Moorad commented 4 years ago

Have you modified the code in any way? if you have can you show me that?

Be careful with res.header('Content-Disposition', `attachment; filename="${title}.mp3"`);, `attachment; filename="${title}.mp3"` is wrapped around with ticks ` not '

chrisVillanueva commented 4 years ago

Hi @Moorad

No, I have not modified the code. I pulled master and started server, then the client html.

Moorad commented 4 years ago

ok give me one min

Moorad commented 4 years ago

Alright I messed around a little bit and I was able to replicate the same error image The problem seems to be related to the title of the video. On line 23 where you have res.header('Content-Disposition', `attachment; filename="${title}.mp3"`); the name of the file is set to the title of the video. However, the filename cannot contain a character outside the ASCII range (for example an emoji) so if the video title contains a character outside the ASCII range it throws a TypeError.

This might look weird but if you add title = title.replace(/[^\x00-\x7F]/g, ""); (which removes anything non ASCII) above res.header('Content-Disposition', `attachment; filename="${title}.mp3"`); so the code looks something like

...
    title = info.player_response.videoDetails.title;
});

title = title.replace(/[^\x00-\x7F]/g, "");
res.header('Content-Disposition', `attachment; filename="${title}.mp3"`);
ytdl(url, {
    format: 'mp3',
    filter: 'audioonly',
...

it should do the trick

chrisVillanueva commented 4 years ago

Thanks @Moorad. I submitted a PR with the approach you mentioned.

Moorad commented 4 years ago

Great, thank you for the PR. I just merged it to master