Skip to content

Commit

Permalink
Don't reject on media type parameters
Browse files Browse the repository at this point in the history
Restify uses the negotiator module, which will fail to match media types
in the client's `accepts` header if there are parameters other than q
attached to them.  This results in 406 errors when something like
"Accepts: text/plain; charset=utf8" is sent.

Restify doesn't let you specify the charset as an acceptable parameter;
it strips that out of the server's list before checking.

This hack removes any charset specifiers from the accepts header.  It
should probably be removed at a later time, after the negotiator module
is fixed (see jshttp/negotiator#35)

I found this as swagger-ui's requests have charsets appended to the
accepts header, so it was failing with 406 errors.
  • Loading branch information
Doug Luce authored and dougluce committed Dec 20, 2015
1 parent 23763f7 commit 1c54081
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/api.ls
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require! {
'prelude-ls': {map}
fs
npid
'media-type'
contenttype
}

swagger =
Expand Down Expand Up @@ -118,15 +120,30 @@ web_proxy = (req, res, next) ->

swaggerJson = (req, res) ->
res.send JSON.stringify swagger
#
# This is because the negotiator module will balk if any media type
# parameter (except for q) doesn't explicitly match the server's
# allowed parameters. And those allowed parameters aren't allowed to
# have parameters specified.
#

cleanAccepts = (req, res, next) ->
types = []
for type in contenttype.splitContentTypes req.headers.accept
media = mediaType.fromString type
delete media.parameters.charset
types.push media.asString!
req.headers.accept = types.join ', '
next!

export init = (server, logobj) ->
logger = logobj
swagger.host = server.name
server.use setHeader
server.use cleanAccepts
server.use restify.bodyParser!
server.use restify.acceptParser server.acceptable
server.use restify.CORS!
server.use restify.fullResponse!

server.get /^(|\/|\/index.html|\/w.*)$/ web_proxy
commands.init!
makeroutes server, logger
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
"basho-riak-client": "http://kvs.io/w/riak-client.tgz",
"bunyan": "^1.3.5",
"bunyan-prettystream": "^0.1.3",
"contenttype": "^1.0.1",
"dtrace-provider": "^0.4.0",
"ipv6": "^3.1.1",
"ipware": "0.0.5",
"livescript": "^1.4.0",
"media-type": "^0.2.0",
"node-restify-swagger": "^0.1.8",
"node-restify-validation": "^1.0.4",
"npid": "^0.4.0",
Expand Down

0 comments on commit 1c54081

Please sign in to comment.