Bryan O'Sullivan avatar Bryan O'Sullivan committed 88a7435

RFC2616 example

Comments (0)

Files changed (1)

examples/RFC2616.hs

+{-# LANGUAGE OverloadedStrings #-}
+module RFC2616 where
+
+import Data.ParserCombinators.Attoparsec.Incremental.Char8
+import Data.ByteString.Lazy.Char8 (ByteString)
+import qualified Data.ByteString.Lazy.Char8 as L
+import Data.ByteString.Char8 ()
+import Control.Applicative
+import Data.Char
+import Control.Monad
+import Prelude hiding (takeWhile)
+
+date = rfc1123Date -- <|> rfc850Date <|> asctimeDate
+
+oneOf :: Alternative f => [f a] -> f a
+oneOf = foldr (<|>) empty
+
+rfc1123Date =
+    liftA3 (,,) (wkday <* string ", ") (date <* char ' ') (time <* string " GMT") <?> "RFC1123 date"
+  where wkday = oneWord "Mon Tue Wed Thu Fri Sat Sun"
+        oneWord = oneOf . map string . L.words
+        date = liftA3 (,,) (d2 <* char ' ') (month <* char ' ') d4
+        month = oneWord "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
+        d2 = replicateM 2 (satisfy isDigit)
+        d4 = replicateM 4 (satisfy isDigit)
+
+time = liftA3 (,,) (d <* c) (d <* c) d <?> "time"
+    where d = replicateM 2 (satisfy isDigit)
+          c = char ':'
+
+eol = (char '\n' *> pure Nothing) <|> (string "\r\n" *> pure Nothing)
+
+header =
+    (,) <$> (takeWhile fieldChar <* char ':' <* skipWhile space)
+        <*> ((:) <$> tillEOL <*> many cont)
+    where tillEOL = takeTill (\c -> c == '\r' || c == '\n') <* eol
+          fieldChar c = c /= ':' && c >= '!' && c <= '~' 
+          cont = some (satisfy space) *> tillEOL
+          space c = c == ' ' || c == '\t'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.