jshttp / content-type

Create and parse HTTP Content-Type header
MIT License
131 stars 25 forks source link

What happens if there is no encoding specified? #8

Closed loong closed 8 years ago

loong commented 8 years ago

What happens if there is no encoding specified? What happens if the encoding is invalid?

dougwilson commented 8 years ago

Hi @mindworker, thanks for the question! I'm not 100% sure what it means, though, as this module has no arguments, parameters, or just anything in the code called encoding, so I don't know what answer t give you. Can you provide more context to your question?

loong commented 8 years ago

Hi @dougwilson! Sorry that I'm wasn't clear enough. With encoding I mean the charset e.g. UTF-8. I wonder if there is the case if the charset is not defined in the content-type at all. Or what happens if the content-type is set with an invalid charset e.g. a typo of UTF-9. Can this happen? And how does package react to this?

Thanks!

dougwilson commented 8 years ago

Ah, @mindworker, that makes sense! So this module just parses a Content-Type header. As long as it is syntactically correct, the header will parse. The parameters defined vary from type to type, and as far as the definition of the Content-Type header itself goes, it does not care at all about parameters, only that the string is syntactically correct.

For your specific questions:

What happens if there is no encoding specified?

Then it won't be in the parameters object. You can use the node executable to run your own examples as well:

$ node -pe 'require("content-type").parse("foo/bar")'
ContentType { parameters: {}, type: 'foo/bar' }

What happens if the encoding is invalid?

Al this module cares about is if it is syntactically correct. The contents of any given parameter is of no concern to this module, because this module does not have a dictionary of which content types actually have a defined semantic for the charset parameter, or even what the acceptable values are (for example, some types only allow you to specify UTF charsets, but others don't care if you specify others. Example:

$ node -pe 'require("content-type").parse("foo/bar; charset=UTF-9")'
ContentType { parameters: { charset: 'UTF-9' }, type: 'foo/bar' }

I hope that helps!