aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
757 stars 127 forks source link

Missing Content-Length in HEAD response leads to invalid request range #6

Closed patrickjuchli closed 8 years ago

patrickjuchli commented 8 years ago

I'm using the library in the browser and I'd like to see the tags of MP3 files. Doing so leads to the following error:

GET http://localhost:3001/sound/stella.mp3 416 (Requested Range Not Satisfiable)

This happens because the requested range is [NaN, NaN]. After a quick first debug session I think this is caused by the size of the file being reported as NaN. In the XHRFileReader you send a HEAD request in order to get the file size using Content-Length in the response header. Not all servers return Content-Length after a HEAD request, though. See RFC 7231:

The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request had been a GET, except that the payload header fields (Section 3.3) MAY be omitted.

SOULD, not MUST. In my case, there is no such information and thus the rest fails. I "verified" this by replacing the parseInt() expression with a constant number. The line in question is:

self._size = parseInt(xhr.getResponseHeader("Content-Length"), 10);

How should this be fixed correctly? Why do you need the file size? (I don't know much about ID3 tags, hence these questions)

aadsm commented 8 years ago

Thank you for pointing this out, I didn't realize it's a SHOULD for a HEAD request. A way to fix it is to replace the HEAD request with a GET using a Range request. This is needed to read tags that are located at the end of the file.

aadsm commented 8 years ago

I've just updated the npm package, can you please give it a try?

patrickjuchli commented 8 years ago

Works great, I'm using the config to disable HEAD requests at all. Many thanks!