expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.16k stars 15.6k forks source link

req.accepts doesn't play well with content type containing dots #2240

Closed honzajavorek closed 10 years ago

honzajavorek commented 10 years ago

I just encountered this behavior:

console.log req.headers['accept'] is 'vnd.apiblueprint.parseresult.raw+yaml'
# true

console.log req.accepts 'vnd.apiblueprint.parseresult.raw+yaml'
# false

...while following works nicely:

console.log req.headers['accept'] is 'application/hal+json'
# true

console.log req.accepts 'application/hal+json'
# application/hal+json

I suspect that req.accepts doesn't play well with dots in custom content types. I tried the same thing in separate code snippet using only accepts, but that worked O_o

accepts = require 'accepts'
accept = accepts
    headers:
        'Accept': 'vnd.apiblueprint.parseresult.raw+yaml'
console.log accept.types 'vnd.apiblueprint.parseresult.raw+yaml'
$ npm install accepts
$ coffee test.coffee
vnd.apiblueprint.parseresult.raw+yaml

I don't understand it. From my POW it seems to be an express' issue.

dougwilson commented 10 years ago

vnd.apiblueprint.parseresult.raw+yaml is not a valid mime type; it's missing the type (format is {type}/{subtype}. It looks like that is only a subtype.

Here is a good example from a RFC on the format: http://tools.ietf.org/html/rfc2045#section-5.1 you'll see the format must have a type and subtype.

dougwilson commented 10 years ago

And if you were wondering about Accept-specific headers, you can see in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html that vnd.apiblueprint.parseresult.raw+yaml is also an invalid format for the Accept header, because once again, you need a / in there. Acceptable values for the header are */*, {type}/* or {type}/{subtype}.

honzajavorek commented 10 years ago

facepalm Thank you so much for pointing it out. The type is so long I didn't really notice the first part is missing. It's definitely a mistake on my side. Sorry for filing an issue.