Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add default matching function, stubs for non-default matching, and pl…

…umbing
  • Loading branch information...
commit 8dab11dbb274b845dc61135cede6e78b440bc196 1 parent d2d3fb9
@benblank benblank authored
Showing with 21 additions and 3 deletions.
  1. +21 −3 src/Accept.coffee
View
24 src/Accept.coffee
@@ -67,7 +67,7 @@ parseStandard = (obj) ->
obj.value
# Parse custom `Accept` header field.
-parseHeaderField = (str, map, sort) ->
+parseHeaderField = (str, map, sort, match) ->
if not str?
return
@@ -82,10 +82,13 @@ parseHeaderField = (str, map, sort) ->
objects.sort sort
+ Object.defineProperty objects, "getBestMatch"
+ value: match ? getBestMatch
+
# Parse `Accept` header field.
parseAccept = (str) ->
str = str ? '*/*'
- parseHeaderField str, parseMediaType, sortMediaType
+ parseHeaderField str, parseMediaType, sortMediaType, getBestMediaMatch
# ## Sort functions
@@ -116,13 +119,28 @@ sortMediaType = (a, b) ->
return -1
0
+# ## Match functions
+
+# Get the best full-string match.
+getBestMatch = (candidates) ->
+ acceptable = (accepted for accepted in this when accepted in candidates)
+ acceptable[0] ? (candidates[0] if "*" in this)
+
+# Get the best language match, as per RFC 2616 section 14.4.
+getBestLanguageMatch = (candidates) ->
+ null
+
+# Get the best media-type match, as per RFC 2616 section 14.1.
+getBestMediaMatch = (candidates) ->
+ null
+
# Build middleware with parsers for several accept header fields.
middleware = (req, res, next) ->
req.accept =
types: parseAccept req.headers.accept
charsets: parseHeaderField req.headers['accept-charset']
encodings: parseHeaderField req.headers['accept-encoding']
- languages: parseHeaderField req.headers['accept-language']
+ languages: (parseHeaderField req.headers['accept-language'], null, null, getBestLanguageMatch)
ranges: parseHeaderField req.headers['accept-ranges']
next()
Please sign in to comment.
Something went wrong with that request. Please try again.