jshttp / type-is

Infer the content-type of a request.
MIT License
226 stars 32 forks source link

Wildcard "*/*" should match empty content type #17

Closed CJex closed 9 years ago

CJex commented 9 years ago

If the response doesn't have a Content-Type header, it should still pass */* type check.

dougwilson commented 9 years ago

Thanks for the request, but for your case, where you only want to know if there is a body and do not care about the type, you want to just use typeis.hasBody(req) and not the main export.

I see the method is not listed on the readme, so I will get it added.

CJex commented 9 years ago

Thanks very much. I forgot to refer its usage in body-parser : https://github.com/expressjs/body-parser/blob/master/lib/types/text.js#L72

So in this case bodyParser.raw({type:'*/*'}) will fail if the response doesn't have a Content-Type header.
I think fix it in type-is lib is more natural.

dougwilson commented 9 years ago

You need to do the following for body-parser module:

bodyParser.raw({ type: function () { return true } })

As the type argument accepts a function (https://github.com/expressjs/body-parser/issues/76#issuecomment-72795371).

If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value.

No change will be made to this module, I'm sorry.

*/* is a structured match, which means a type with any type and any subtype. Having no sub type and no type is not the same, which is specifically why the / is included and not part of the *; you can think even say it's similar to a regular expression like /.*\/.*/, which as you can see, doesn't match an empty string, as there still has to be a literal / in the string somewhere at minimum.