packages feed

Ketchup 0.3.1 → 0.4.0

raw patch · 61 files changed

+97/−50 lines, 61 filesdep −regex-pcre-builtinbinary-addedPVP ok

version bump matches the API change (PVP)

Dependencies removed: regex-pcre-builtin

API changes (from Hackage documentation)

+ Ketchup.Routing: instance Show Arguments
+ Ketchup.Routing: match :: ByteString -> Matcher
+ Ketchup.Routing: prefix :: ByteString -> Matcher
+ Ketchup.Utils: subBS :: Int -> Int -> ByteString -> ByteString
- Ketchup.Routing: route :: [(ByteString, Route)] -> Handler
+ Ketchup.Routing: route :: [(Matcher, Route)] -> Handler

Files

.git/COMMIT_EDITMSG view
@@ -1,1 +1,15 @@-Version bump for cabal+Remove unneded comments+# Please enter the commit message for your changes. Lines starting+# with '#' will be ignored, and an empty message aborts the commit.+# On branch master+# Your branch is up-to-date with 'origin/master'.+#+# Changes to be committed:+#	modified:   Ketchup/Routing.hs+#+# Untracked files:+#	.gitignore~+#	Ketchup.cabal~+#	example.core+#	gendocs.sh~+#
.git/FETCH_HEAD view
@@ -1,2 +1,2 @@-b83c2375bba608232746064adfdb067cf57e5d80		branch 'master' of github.com:Hamcha/ketchup-4ac44b6241517d43310da25b086e175b3b9c0b49	not-for-merge	branch 'gh-pages' of github.com:Hamcha/ketchup+85b4c8d2256c11c570cacf64895353c0b9bd374f		branch 'master' of github.com:Hamcha/ketchup+4c2353910734c89633d869f0ffccf8f565c93221	not-for-merge	branch 'gh-pages' of github.com:Hamcha/ketchup
.git/ORIG_HEAD view
@@ -1,1 +1,1 @@-b83c2375bba608232746064adfdb067cf57e5d80+85b4c8d2256c11c570cacf64895353c0b9bd374f
.git/index view

binary file changed (1136 → 1136 bytes)

.git/logs/HEAD view
@@ -26,3 +26,5 @@ 0f03ed98a2eba1beb8db11eb86edb34b2321a261 0fd36c65d057f7ce244a4e9a335d698f075530e6 Hamcha <zikyky@gmail.com> 1404079677 +0200	commit: Version bump 0fd36c65d057f7ce244a4e9a335d698f075530e6 694dea2e0fc5c15b1ef710cee6f953b1afb623bd Hamcha <zikyky@gmail.com> 1404079963 +0200	commit: Promote Auth :D 694dea2e0fc5c15b1ef710cee6f953b1afb623bd 695dee0b1313795711418347aedd65f5bcbca02f Hamcha <zikyky@gmail.com> 1407086956 +0200	commit: Version bump for cabal+695dee0b1313795711418347aedd65f5bcbca02f 83ac9915be5493cf66f1f96de88b5edce6c39929 Hamcha <zikyky@gmail.com> 1407714950 +0200	commit: Doesn't work, but it's a start, will work on it tomorrow+83ac9915be5493cf66f1f96de88b5edce6c39929 85b4c8d2256c11c570cacf64895353c0b9bd374f Hamcha <zikyky@gmail.com> 1407715002 +0200	commit: Remove unneded comments
.git/logs/refs/heads/master view
@@ -26,3 +26,5 @@ 0f03ed98a2eba1beb8db11eb86edb34b2321a261 0fd36c65d057f7ce244a4e9a335d698f075530e6 Hamcha <zikyky@gmail.com> 1404079677 +0200	commit: Version bump 0fd36c65d057f7ce244a4e9a335d698f075530e6 694dea2e0fc5c15b1ef710cee6f953b1afb623bd Hamcha <zikyky@gmail.com> 1404079963 +0200	commit: Promote Auth :D 694dea2e0fc5c15b1ef710cee6f953b1afb623bd 695dee0b1313795711418347aedd65f5bcbca02f Hamcha <zikyky@gmail.com> 1407086956 +0200	commit: Version bump for cabal+695dee0b1313795711418347aedd65f5bcbca02f 83ac9915be5493cf66f1f96de88b5edce6c39929 Hamcha <zikyky@gmail.com> 1407714950 +0200	commit: Doesn't work, but it's a start, will work on it tomorrow+83ac9915be5493cf66f1f96de88b5edce6c39929 85b4c8d2256c11c570cacf64895353c0b9bd374f Hamcha <zikyky@gmail.com> 1407715002 +0200	commit: Remove unneded comments
.git/logs/refs/remotes/origin/gh-pages view
@@ -1,1 +1,2 @@ 0000000000000000000000000000000000000000 4ac44b6241517d43310da25b086e175b3b9c0b49 Hamcha <zikyky@gmail.com> 1402364070 +0200	pull: storing head+4ac44b6241517d43310da25b086e175b3b9c0b49 4c2353910734c89633d869f0ffccf8f565c93221 Hamcha <zikyky@gmail.com> 1411044679 +0200	pull: fast-forward
.git/logs/refs/remotes/origin/master view
@@ -23,3 +23,5 @@ f61d5b2caadecbdcf54d2d0ad7b131ca2adcdc28 0fd36c65d057f7ce244a4e9a335d698f075530e6 Hamcha <zikyky@gmail.com> 1404079685 +0200	update by push 0fd36c65d057f7ce244a4e9a335d698f075530e6 694dea2e0fc5c15b1ef710cee6f953b1afb623bd Hamcha <zikyky@gmail.com> 1404079969 +0200	update by push 694dea2e0fc5c15b1ef710cee6f953b1afb623bd 695dee0b1313795711418347aedd65f5bcbca02f Hamcha <zikyky@gmail.com> 1407086961 +0200	update by push+695dee0b1313795711418347aedd65f5bcbca02f 83ac9915be5493cf66f1f96de88b5edce6c39929 Hamcha <zikyky@gmail.com> 1407714965 +0200	update by push+83ac9915be5493cf66f1f96de88b5edce6c39929 85b4c8d2256c11c570cacf64895353c0b9bd374f Hamcha <zikyky@gmail.com> 1407715015 +0200	update by push
+ .git/objects/04/27491eaa020186e459934ed69cdb7d808501cd view
@@ -0,0 +1,4 @@+x”ßOÛ0Ç÷Ì_aü0à!ñJ…4ÖÄ+L Á@´Mˆ×1M¨k{±Óšö¿ïl(Ý&M}jz¾ûÜÝ×çI=B½ƒƒþ›lûørX|¿:A•›JtuûñülˆpBÈ×þãâ};-.ÎQ/}‡Š†)[»Z+&	9ù‚®œ3™Ïç鼟êfLŠk²ð¬žî>·™–®Ä4ùS©lþJïðð0{WÁJšM…cP¥3‰øÑÖ³µrB¹¤XÿrìÄÂO ^±Æ+—ߟ’÷š¹ÚIA3ÒýÊZMPՈ‡k.˜J¹µ5BæØº¥¶Âaä C!:Ǘ>$`-ojãm84ÃÊRóIÒºZ¦€[‰~d3}¡-¿hÖEÿϐlûnx|TÝmÍkUêyª•Ô¬D9zh÷÷v÷ÐOÃÆâì»{ƒ_ƒ-BîïéÖKUérI³²ž¡ºÌ±a|Q‰?
ÔÔJÄ%³p'^(>zËT¨dM3¸† #©—Óî2lFtæ½V=AdÕûÌG¾¸’VÒÌ<eæÌ„žÄÔ¸%¦oÕȚAFp¡î—⻇¢žÚ™ê²•"‘µu`]bzΡF`ù”¾Dk˜+]z\£eªÒLS!a¶¤dƂBH+.k>iÐã±7"è¿»Q;{˜~ŽW­ÙäôÚèõ+m+=ïô%¼ÊO"wÄä¨uU§ugJ½)г=Ëþ_ÀaÕª‰(טusì)¼Ðuh°m޼Öð–Ôx­Òκ9öÆ1Wó5j4n½…WoטÁö2}¸²øfûõ„?hí³4ôªÙå¢D£%zž•í[1;R†Üm ‚éiÜE>7š‰ÆúU±ŸöúéþÊ{"q'„Ióø
+ .git/objects/04/a409a115341e8d88fbe81afe993e824ed5a755 view

binary file changed (absent → 874 bytes)

+ .git/objects/0b/1732fd47a5b9008302f3cf2cc815b4c3fcbb03 view

binary file changed (absent → 321 bytes)

+ .git/objects/0e/f0bb4acb2a054328faea81aaa1b6ef08ab4324 view

binary file changed (absent → 1451 bytes)

+ .git/objects/2b/9c68df7936d7398cd61b39686392d66ff8f1c1 view

binary file changed (absent → 1391 bytes)

+ .git/objects/31/0e7b443f14a92b46059c540620e2e3ec9eeecf view

binary file changed (absent → 918 bytes)

+ .git/objects/33/d1dadf72d9a1c6217c5a6c7992dcdaaa343742 view

binary file changed (absent → 161 bytes)

+ .git/objects/34/f107591ec1e00bba2a7bee5238bddbb4832bdb view

binary file changed (absent → 1159 bytes)

+ .git/objects/39/7b16f4af94e0b04d7e6c7063332150ee9113e1 view

binary file changed (absent → 208 bytes)

+ .git/objects/4c/2353910734c89633d869f0ffccf8f565c93221 view
@@ -0,0 +1,2 @@+x•ÎA+1@Q×=Eö‚$MǶ âÒx€˜T´vëBOï\Áí‡_[­cOiÕçR@%R$ÝJæ„å2äA‰Ng$JÈçÄAÝ$syv`62±Kô–eqž¢²Õ˜³75áÀ1x'ï~k3¥êM`÷ïŸûçp­2>6Úê(`À˜#zX£GtK]Özù¹Ódҋ5}Ao€v?¹C$
+ .git/objects/5c/7dd6782bd7103baeb041d69fd3e17410ab46dd view

binary file changed (absent → 1552 bytes)

+ .git/objects/63/50f4f119a8325852c5279f96ae5b0c34cce55f view

binary file changed (absent → 1142 bytes)

+ .git/objects/6c/fe3c139d7fb98d3057452404339c790f2aa1d3 view

binary file changed (absent → 603 bytes)

+ .git/objects/70/15cd0af9b0fc60257cff8d057cee6a475f44d8 view

binary file changed (absent → 456 bytes)

+ .git/objects/83/ac9915be5493cf66f1f96de88b5edce6c39929 view
@@ -0,0 +1,1 @@+x•ŽMjÃ0F»Ö)f—EBýE6”ÒE½†4–aË*ò“œ¾"7Èö{¼ÇGµ”Ì ¬ýà#`N«4çm*iJŠh6҉B@-þ|‹+Ãy´SŒÝÒR»Ñ:)t·ã4m²ùžþηÚà׺yøzæù1?~®Åçå“jùiÐ9iF‹pD…(úÚ¯q|K—·õÀ°×6Ÿ Ü26ð°±o|‚=/ËB];®¥¶VwñI“Qu
+ .git/objects/85/b4c8d2256c11c570cacf64895353c0b9bd374f view
@@ -0,0 +1,2 @@+x•ŽÍjÃ0{ÖSì=¤Õ%¥Çœûëõ§Ä¤²‹«Ò§¯ó¹Ì0º¶6wbæ·¾TrÈ:fܨ.&¸¦h™‚›²+؛oÙ°tÊ^´GÄP¼Ö”ª«%MÈyŒ˜I})\ŒÜûuÝè,M¯B§¿ùö¸=>.M毣®í\°ÃࢵLË֚îk/Iæmýݗ&zFöÏóíFË
+ .git/objects/87/66065959fca5de5327f875dbb67559f69b52b6 view
@@ -0,0 +1,3 @@+x•’]oÓ0†¹î¯ð|µ]$^5!QêD*éÐ݇F*@Ó.\Ûk¼:vˆOšVˆÿŽmPѾïsìsÎJۏÏß¾¡'óÛ,ÿ~w‰+(5º[~X|ʎùz‘2ÏçèÛU~½@ãøå53N²†iB.o0Â@õž¶mãö"¶õšä÷dXã`–¼rÆNioWj㒿PƓɤ7©d"¥¥æ_	U$4j›à̐¢|_IŒx¿K0Ȑ@Ÿ"^°ÚIH–ùÇèF$¥ @˔’aÖÊlPQ˧[.™‰¹sÕR'ØÁ^KWH	0€;AçNðm°tXÇkUr5÷ŸaBX¾‰P:~ö¸Wîg¶e½Ö‹ô«”îè¡'Ù|–ÏF­2¶±5Ú2ôÔêNÏÐϊ­åŸŸžMMG„<>¦£— ¤Ïâʊ=R"Á¥2Ê?B¨m¿µ¢Ñ2Òʁ?­×ÌùÊpV>N¯»{GI•ÒF§T«?’²»ñ&_.“Ÿ%𢩢YE+áSÀ굯.™ò¬A%ÌgB«ÿ"fEc6R„š£¸W¾«S;ÅQÌ{ëÀ¬¾uÐÅý?ˆí%GQ—¾YÝAh§xa’ÐÄwCOù)”:ý
æ{.
+ .git/objects/a3/3f00a997926762956b66487bc763c7500b5912 view

binary file changed (absent → 529 bytes)

+ .git/objects/ad/296ab5c62662ad2f55c20cd75483c7014e3a9e view

binary file changed (absent → 1002 bytes)

+ .git/objects/c4/41d386e187efdfa3b1992b4bd4406bd9a2ad0d view

binary file changed (absent → 984 bytes)

+ .git/objects/c8/298c807614715a3dd52fe3fddcd74b42d8dd51 view

binary file changed (absent → 1169 bytes)

+ .git/objects/ca/7171c6a9384a3af595c1110538b011803b834c view

binary file changed (absent → 727 bytes)

+ .git/objects/cc/746e214c400d625c17bb704964d2490009dcd6 view

binary file changed (absent → 554 bytes)

+ .git/objects/da/b38cfd353bbdc462641543658ea8acd3eac19d view

binary file changed (absent → 928 bytes)

+ .git/objects/e5/afddd93fed2971b278890973cea1740b8011a2 view

binary file changed (absent → 207 bytes)

+ .git/objects/e5/dc6abf9873d9eaf80c4e5d308832b23e5b18cd view

binary file changed (absent → 1028 bytes)

+ .git/objects/e8/a9b100db71a644cc6cf513f26b5f44a42d17ab view

binary file changed (absent → 972 bytes)

+ .git/objects/ec/a1b33fcd1c0373e31d108a81dca8be042fd89c view

binary file changed (absent → 672 bytes)

+ .git/objects/f6/e848cb8cee1bc156a72f65022e443a068cae23 view

binary file changed (absent → 321 bytes)

+ .git/objects/f8/b79cfde7e99e07a48a906d6b0f43517cdee2b3 view

binary file changed (absent → 507 bytes)

+ .git/objects/ff/6a9f5d733cd98051951968bb686e9766cf0a44 view

binary file changed (absent → 960 bytes)

.git/refs/heads/master view
@@ -1,1 +1,1 @@-695dee0b1313795711418347aedd65f5bcbca02f+85b4c8d2256c11c570cacf64895353c0b9bd374f
.git/refs/remotes/origin/gh-pages view
@@ -1,1 +1,1 @@-4ac44b6241517d43310da25b086e175b3b9c0b49+4c2353910734c89633d869f0ffccf8f565c93221
.git/refs/remotes/origin/master view
@@ -1,1 +1,1 @@-695dee0b1313795711418347aedd65f5bcbca02f+85b4c8d2256c11c570cacf64895353c0b9bd374f
Ketchup.cabal view
@@ -1,5 +1,5 @@ Name:           Ketchup
-Version:        0.3.1
+Version:        0.4.0
 Cabal-Version:  >= 1.6
 Build-Type:     Simple
 License:        MIT
@@ -18,7 +18,7 @@     .
     * Basic httpd functionality (IPV4 only for now)
     .
-    * Regexp routing with parameters (ie. \`/user/:name/(.\*)\`)
+    * Regexp routing with parameters (ie. \`/user/:name/?\`)
     .
     * Static file handler (ie. \`static \".\"\` as route/handler)
     .
@@ -29,8 +29,6 @@     The core parts of Ketchup are all depedency free, that includes Ketchup.Httpd and Ketchup.Utils (and to a certain extent, Ketchup.Chunked).
     The dependencies required are for the following modules:
     .
-    * Ketchup.Routing (uses regex-pcre-builtin, This can ben modified to use builtin POSIX regexp)
-    .
     * Ketchup.Static (uses mime-types)
     .
     * Ketchup.Auth (uses base64-bytestring)
@@ -57,7 +55,6 @@     directory           <1.3,
     mime-types          <0.2,
     network             <2.5,
-    regex-pcre-builtin  <1.0,
     text                <2.0
 
 Source-Repository head
− Ketchup/Auth.hi

binary file changed (2110 → absent bytes)

− Ketchup/Auth.o

binary file changed (26408 → absent bytes)

− Ketchup/Chunked.hi

binary file changed (7619 → absent bytes)

− Ketchup/Chunked.o

binary file changed (28672 → absent bytes)

− Ketchup/Httpd.hi

binary file changed (4273 → absent bytes)

− Ketchup/Httpd.o

binary file changed (42152 → absent bytes)

− Ketchup/Routing.hi

binary file changed (3472 → absent bytes)

Ketchup/Routing.hs view
@@ -2,6 +2,8 @@ 
 module Ketchup.Routing
 ( Route      (..)
+, match
+, prefix
 , route
 , useHandler
 ) where
@@ -10,47 +12,56 @@ import           Ketchup.Httpd
 import           Ketchup.Utils
 import           Network
-import qualified Text.Regex.PCRE as R
 
-type Route = Socket -> HTTPRequest -> (B.ByteString -> Maybe B.ByteString) -> IO ()
-
+type Route   = Socket -> HTTPRequest -> (B.ByteString -> Maybe B.ByteString) -> IO ()
+type Matcher = B.ByteString -> (Bool, Arguments)
+data Arguments = None | Parameters [(B.ByteString, B.ByteString)]
+    deriving Show
 -- |Router function
 -- Takes a list of routes and iterates through them for every requeust
-route :: [(B.ByteString, Route)] -- ^ Routes
+route :: [(Matcher, Route)] -- ^ Routes
          -> Handler
 route []         handle request = sendNotFound handle
 route (r:routes) handle request
-    | match (uri request) (fst r) = (snd r) handle request $
-                                        params (uri request) (fst r)
-    | otherwise                   = route routes handle request
+    | isMatch   = (snd r) handle request (get params)
+    | otherwise = route routes handle request
+    where
+    isMatch = fst matched
+    params  = snd matched
+    matched = (fst r) (uri request)
 
+
 -- |Wrap a handler in a route
 -- Lets you use a handler (no parameters) as a route
 useHandler :: Handler -> Route
 useHandler handler hnd req params = handler hnd req
 
-match :: B.ByteString -> B.ByteString -> Bool
-match url template =
-    and $ zipWith compare urlparts tmpparts
+match :: B.ByteString -> Matcher
+match template url =
+    (isMatch, Parameters params)
     where
-    compare x y
-        | x == y                  = True
-        | or [B.null y, B.null x] = False
-        | B.head y == ':'         = True
-        | x R.=~ y                = True
-        | otherwise               = False
+    isMatch  = fst parsed
+    params   = snd parsed
+    parsed   = parse urlparts temparts []
     urlparts = B.split '/' url
-    tmpparts = B.split '/' template
+    temparts = B.split '/' template
 
-params :: B.ByteString -> B.ByteString -> B.ByteString -> Maybe B.ByteString
-params url template name =
-    lookup name list
-    where
-    list = filter (not . B.null . fst) $
-               zipWith retrieve urlparts tmpparts
-    retrieve x y
-        | or [B.null y, B.null x] = ("","")
-        | B.head y == ':'         = (B.tail y, x)
-        | otherwise               = ("","")
-    urlparts = B.split '/' url
-    tmpparts = B.split '/' template+parse :: [B.ByteString]
+      -> [B.ByteString]
+      -> [(B.ByteString, B.ByteString)]
+      -> (Bool, [(B.ByteString, B.ByteString)])
+parse []      []       params = (True, params)
+parse (u:url) []       params = (False, [])
+parse []      (t:temp) params = (False, [])
+parse (u:url) (t:temp) params
+    | B.length t < 1  = parse url temp params
+    | B.head t == ':' = parse url temp ((B.tail t, u) : params)
+    | u == t          = parse url temp params
+    | otherwise       = (False, [])
+
+prefix :: B.ByteString -> Matcher
+prefix urlPrefix url = (B.isPrefixOf urlPrefix url, None)
+
+get :: Arguments -> B.ByteString -> Maybe B.ByteString
+get (Parameters params) x = lookup x params
+get None x                = Nothing
− Ketchup/Routing.o

binary file changed (30512 → absent bytes)

− Ketchup/Static.hi

binary file changed (2100 → absent bytes)

− Ketchup/Static.o

binary file changed (15600 → absent bytes)

− Ketchup/Utils.hi

binary file changed (18874 → absent bytes)

Ketchup/Utils.hs view
@@ -1,14 +1,15 @@ {-# LANGUAGE OverloadedStrings #-}
 
 module Ketchup.Utils
-( trim
-, breakBS
+( breakBS
 , fallback
 , parseBody
-, sendReply
 , sendBadRequest
 , sendNotFound
+, sendReply
 , statusMsg
+, subBS
+, trim
 ) where
 
 import qualified Data.ByteString.Char8 as B
@@ -87,10 +88,15 @@     second = B.drop (B.length delimiter) $ snd broke
     broke  = B.breakSubstring delimiter source
 
+-- |Get a substring of a ByteString
+subBS :: Int -> Int -> B.ByteString -> B.ByteString
+subBS start length = B.take length . B.drop start
+
 -- |Parse a URL-encoded Request
 parseBody :: B.ByteString -> [(B.ByteString, B.ByteString)]
 parseBody body = map (breakBS "=") $ B.split '&' body
 
+-- |Maybe a -> a with fallback (if Nothing)
 fallback :: Maybe a -> a -> a
 fallback (Just value) fallback = value
 fallback Nothing      fallback = fallback
− Ketchup/Utils.o

binary file changed (79464 → absent bytes)

− example

file too large to diff

− example.hi

binary file changed (23780 → absent bytes)

example.hs view
@@ -32,12 +32,12 @@     print $ parseBody $ body req
     sendReply hnd 200 [("Content-Type", ["text/html"])] "OK!"
 
-router = route [ ("/"            , useHandler $ handle     )
-               , ("/greet/:user" , greet                   )
-               , ("/chunk"       , useHandler $ chunked    )
-               , ("/post"        , useHandler $ post       )
-               , ("/Ketchup/(.*)", useHandler $ static "." )
-               , ("/auth"        , useHandler $ basicAuth [("a","b")] "test" handle )
+router = route [ (match  "/greet/:user" , greet                   )
+               , (prefix "/chunk/"      , useHandler $ chunked    )
+               , (match  "/post"        , useHandler $ post       )
+               , (prefix "/Ketchup/"    , useHandler $ static "." )
+               , (match  "/auth"        , useHandler $ basicAuth [("a","b")] "test" handle )
+               , (match  "/"            , useHandler $ handle     )
                ]
 
 main = do listenHTTP "*" 8080 router
− example.o

binary file changed (76544 → absent bytes)