Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to Accept parser #119

Merged
merged 3 commits into from
Oct 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions wai-extra/Network/Wai/Parse.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,19 +58,25 @@ breakDiscard w s =
parseHttpAccept :: S.ByteString -> [S.ByteString] parseHttpAccept :: S.ByteString -> [S.ByteString]
parseHttpAccept = map fst parseHttpAccept = map fst
. sortBy (rcompare `on` snd) . sortBy (rcompare `on` snd)
. map grabQ . map (addSpecificity . grabQ)
. S.split 44 -- comma . S.split 44 -- comma
where where
rcompare :: Double -> Double -> Ordering rcompare :: (Double,Int) -> (Double,Int) -> Ordering
rcompare = flip compare rcompare = flip compare
addSpecificity (s, q) =
-- Prefer higher-specificity types
let semicolons = S.count 0x3B s
stars = S.count 0x2A s
in (s, (q, semicolons - stars))
grabQ s = grabQ s =
let (s', q) = breakDiscard 59 s -- semicolon -- Stripping all spaces may be too harsh.
(_, q') = breakDiscard 61 q -- equals sign -- Maybe just strip either side of semicolon?
in (trimWhite s', readQ $ trimWhite q') let (s', q) = S.breakSubstring ";q=" (S.filter (/=0x20) s) -- 0x20 is space
q' = S.takeWhile (/=0x3B) (S.drop 3 q) -- 0x3B is semicolon
in (s', readQ q')
readQ s = case reads $ S8.unpack s of readQ s = case reads $ S8.unpack s of
(x, _):_ -> x (x, _):_ -> x
_ -> 1.0 _ -> 1.0
trimWhite = S.dropWhile (== 32) -- space


-- | Store uploaded files in memory -- | Store uploaded files in memory
lbsBackEnd :: Monad m => ignored1 -> ignored2 -> Sink S.ByteString m L.ByteString lbsBackEnd :: Monad m => ignored1 -> ignored2 -> Sink S.ByteString m L.ByteString
Expand Down
4 changes: 2 additions & 2 deletions wai-extra/test/WaiExtraTest.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ caseParseQueryStringQM = do


caseParseHttpAccept :: Assertion caseParseHttpAccept :: Assertion
caseParseHttpAccept = do caseParseHttpAccept = do
let input = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c" let input = "text/plain; q=0.5, text/html;charset=utf-8, text/*;q=0.8;ext=blah, text/x-dvi; q=0.8, text/x-c"
expected = ["text/html", "text/x-c", "text/x-dvi", "text/plain"] expected = ["text/html;charset=utf-8", "text/x-c", "text/x-dvi", "text/*", "text/plain"]
expected @=? parseHttpAccept input expected @=? parseHttpAccept input


parseRequestBody' :: BackEnd L.ByteString parseRequestBody' :: BackEnd L.ByteString
Expand Down