gdritter repos shelob / 682b0b9
Rename Vriska->Shelob; start reorg Getty Ritter 7 years ago
18 changed file(s) with 334 addition(s) and 276 deletion(s). Collapse all Expand all
+0
-12
LICENSE less more
1 Copyright (c) 2017, Getty Ritter
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
8 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
10 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+0
-13
example/Main.hs less more
1 module Main where
2
3 import qualified Network.Vriska as HTTP
4 import qualified Network.Vriska.Types as HTTP
5 import Text.Show.Pretty
6
7 main :: IO ()
8 main = do
9 let req = HTTP.request HTTP.MGet "/index.html" [HTTP.Header "Host" "gdritter.com"]
10 conn <- HTTP.httpConnection "gdritter.com"
11 rs <- HTTP.makeRequest conn req
12 pPrint rs
13 return ()
1 Copyright (c) 2017, Getty Ritter
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
8 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
10 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 module Main where
2
3 import qualified Network.Shelob as HTTP
4 import qualified Network.Shelob.Types as HTTP
5 import Text.Show.Pretty
6
7 main :: IO ()
8 main = do
9 conn <- HTTP.httpConnection "gdritter.com"
10 (r0,r1,r2) <- HTTP.connect conn $ do
11 h <- HTTP.head "/index.html" [HTTP.Header "Host" "gdritter.com"]
12 a <- HTTP.get "/index.html" [HTTP.Header "Host" "gdritter.com"]
13 b <- HTTP.get "/matzo/matzo.html" [HTTP.Header "Host" "gdritter.com"]
14 return (h, a, b)
15 pPrint r0
16 pPrint r1
17 pPrint r2
18 return ()
1 name: shelob
2 version: 0.1.0.0
3 -- synopsis:
4 -- description:
5 license: BSD3
6 license-file: LICENSE
7 author: Getty Ritter <getty.ritter@gmail.com>
8 maintainer: Getty Ritter <getty.ritter@gmail.com>
9 copyright: ©2017 Getty Ritter
10 -- category:
11 build-type: Simple
12 cabal-version: >= 1.14
13
14 library
15 exposed-modules: Network.Shelob
16 , Network.Shelob.Connection
17 , Network.Shelob.Types
18 , Network.Shelob.Request
19 , Network.Shelob.Response
20 , Network.Shelob.Utils
21 hs-source-dirs: src
22 ghc-options: -Wall
23 build-depends: base >=4.7 && <4.10
24 , bytestring
25 , network
26 default-language: Haskell2010
27 default-extensions: OverloadedStrings,
28 ScopedTypeVariables
29
30 executable example
31 hs-source-dirs: example
32 main-is: Main.hs
33 ghc-options: -Wall
34 build-depends: base, shelob, pretty-show
35 default-language: Haskell2010
36 default-extensions: OverloadedStrings
1 {-# LANGUAGE RecordWildCards #-}
2
3 module Network.Shelob.Connection where
4
5 import qualified Data.ByteString as BS
6 import qualified Data.ByteString.Lazy as LBS
7 import qualified GHC.IO.Handle as H
8
9 import Network.Shelob.Types
10
11 handleToConnection :: H.Handle -> Connection
12 handleToConnection h = Connection { .. }
13 where connRead n = BS.hGet h n
14 connReadLine = BS.hGetLine h
15 connWrite bs = LBS.hPut h bs
16 connClose = H.hClose h
17
18 connReadContents :: Connection -> IO BS.ByteString
19 connReadContents conn = do
20 buf <- connRead conn 1024
21 if buf == ""
22 then connClose conn >> return buf
23 else BS.append buf `fmap` connReadContents conn
1 {-# LANGUAGE RecordWildCards #-}
2
3 module Network.Shelob.Request where
4
5 import Data.Monoid ((<>))
6 import qualified Data.ByteString.Lazy as B
7 import qualified Data.ByteString.Builder as B
8 import qualified Data.Foldable as F
9
10 import Network.Shelob.Types
11
12 crlf :: B.Builder
13 crlf = B.char7 '\13' <> B.char7 '\10'
14
15 pRequest :: Request -> B.ByteString
16 pRequest Request { .. } = B.toLazyByteString body
17 where body = pMethod requestMethod <> B.char7 ' ' <>
18 B.byteString requestPath <>
19 B.byteString " HTTP/1.1" <>
20 crlf <>
21 F.foldMap pHeader requestHeaders <>
22 crlf <>
23 B.byteString requestBody
24
25 pMethod :: Method -> B.Builder
26 pMethod m = B.byteString $ case m of
27 MGet -> "GET"
28 MHead -> "HEAD"
29 MPost -> "POST"
30 MPut -> "PUT"
31 MDelete -> "DELETE"
32 MTrace -> "TRACE"
33 MOptions -> "OPTIONS"
34 MConnect -> "CONNECT"
35 MPatch -> "PATCH"
36 MOther bs -> bs
37
38 pHeader :: Header -> B.Builder
39 pHeader (Header k v) =
40 B.byteString k <> B.char7 ':' <> B.char7 ' ' <>
41 B.byteString v <> crlf
1 module Network.Shelob.Response where
2
3 import qualified Data.ByteString as BS
4 import qualified Data.ByteString.Char8 as BS8
5
6 import Network.Shelob.Types
7
8 pCode :: BS.ByteString -> (Int, Int, Int)
9 pCode ln
10 | Just (a, as) <- BS.uncons ln
11 , Just (b, bs) <- BS.uncons as
12 , Just (c, "") <- BS.uncons bs
13 = ( fromIntegral (a - 48)
14 , fromIntegral (b - 48)
15 , fromIntegral (c - 48)
16 )
17 | otherwise = error "???"
18
19 readResponse :: Connection -> IO (Either HTTPError Response)
20 readResponse conn = do
21 ln <- connReadLine conn
22 case BS.split 32 ln of
23 (_version:code:_reason) -> do
24 let c = pCode code
25 headers <- readHeaders conn
26 body <- readBody conn headers
27 return (Right (Response headers c (Just body)))
28 _ -> return (Left HTTPError)
29
30 readHeaders :: Connection -> IO [Header]
31 readHeaders conn = do
32 ln <- connReadLine conn
33 let Just (ln', 13) = BS.unsnoc ln
34 if BS.null ln'
35 then return []
36 else do
37 let (key:vals) = BS.split 58 ln'
38 let val = BS.intercalate ":" vals
39 rs <- readHeaders conn
40 return (Header key (BS.tail val) : rs)
41
42 readBody :: Connection -> [Header] -> IO BS.ByteString
43 readBody conn hs
44 = case [ read (BS8.unpack value)
45 | Header name value <- hs
46 , name == "Content-Length"
47 ]
48 of
49 [ len ] -> connRead conn len
50 _ -> return ""
1 {-# LANGUAGE FlexibleInstances #-}
2 module Network.Shelob.Types where
3
4 import qualified Control.Exception as Ex
5 import qualified Data.ByteString as BS
6 import qualified Data.ByteString.Lazy as LBS
7 import Data.Typeable (Typeable)
8
9 data Method
10 = MGet
11 | MHead
12 | MPost
13 | MPut
14 | MDelete
15 | MTrace
16 | MOptions
17 | MConnect
18 | MPatch
19 | MOther BS.ByteString
20 deriving (Eq, Show)
21
22 data Header = Header BS.ByteString BS.ByteString deriving (Eq, Show)
23
24 data Request = Request
25 { requestMethod :: Method
26 , requestPath :: BS.ByteString
27 , requestHeaders :: [Header]
28 , requestBody :: BS.ByteString
29 } deriving (Eq, Show)
30
31 type ResponseCode = (Int, Int, Int)
32
33 data Response = Response
34 { responseHeaders :: [Header]
35 , responseCode :: ResponseCode
36 , responseBody :: Maybe BS.ByteString
37 } deriving (Eq, Show)
38
39 -- | This "Connection" abstraction allows us to build on top of
40 -- arbitrary other connection packages for our HTTP
41 data Connection = Connection
42 { connRead :: Int -> IO BS.ByteString
43 , connReadLine :: IO BS.ByteString
44 , connWrite :: LBS.ByteString -> IO ()
45 , connClose :: IO ()
46 }
47
48 data HTTPError = HTTPError deriving (Typeable, Show)
49
50 instance Ex.Exception HTTPError where
51 toException = undefined
52 fromException = undefined
53
54 class HTTPResult t where
55 fromResponse :: Either HTTPError Response -> t
56
57 instance HTTPResult r => HTTPResult (Either HTTPError r) where
58 fromResponse (Left err) = Left err
59 fromResponse (Right v) = Right (fromResponse (Right v))
60
61 instance HTTPResult r => HTTPResult (Maybe r) where
62 fromResponse (Left _) = Nothing
63 fromResponse (Right v) = Just (fromResponse (Right v))
64
65 instance HTTPResult Response where
66 fromResponse (Left err) = Ex.throw err
67 fromResponse (Right v) = v
1 module Network.Shelob.Utils where
1 {-# LANGUAGE DeriveFunctor #-}
2
3 module Network.Shelob where
4
5 import Control.Monad.IO.Class (MonadIO(..))
6 import qualified Data.ByteString as BS
7 import qualified Data.ByteString.Char8 as BS8
8 import qualified Network.Socket as Sock
9 import qualified System.IO as H
10
11 import Network.Shelob.Connection
12 import Network.Shelob.Request
13 import Network.Shelob.Response
14 import Network.Shelob.Types
15
16 newtype ConnM a = ConnM { runConnM :: Connection -> IO a }
17
18 instance Functor ConnM where
19 fmap f (ConnM g) = ConnM (\ c -> fmap f (g c))
20
21 instance Applicative ConnM where
22 pure x = ConnM (\ _ -> return x)
23 ConnM f <*> ConnM x = ConnM $ \ c -> do
24 f' <- f c
25 x' <- x c
26 return (f' x')
27
28 instance Monad ConnM where
29 ConnM x >>= f = ConnM $ \ c -> do
30 x' <- x c
31 runConnM (f x') c
32
33 instance MonadIO ConnM where
34 liftIO m = ConnM (\ _ -> m)
35
36 connect :: Connection -> ConnM a -> IO a
37 connect conn m = do
38 rs <- runConnM m conn
39 connClose conn
40 return rs
41
42 userAgent :: Header
43 userAgent = Header "User-Agent" "haskell/vriska"
44
45 request :: Method -> BS.ByteString -> [Header] -> Request
46 request m p hs = Request m p (hs' ++ hs) ""
47 where hs' = [ Header "Content-Length" "0"
48 , userAgent
49 ]
50
51 head :: BS.ByteString -> [Header] -> ConnM (Either HTTPError Response)
52 head p hs = ConnM (\ c -> makeRequest c (Request MHead p (hs' ++ hs) ""))
53 where hs' = [ Header "Content-Length" "0"
54 , userAgent
55 ]
56
57 get :: BS.ByteString -> [Header] -> ConnM (Either HTTPError Response)
58 get p hs = ConnM (\ c -> makeRequest c (Request MGet p (hs' ++ hs) ""))
59 where hs' = [ Header "Content-Length" "0"
60 , userAgent
61 ]
62
63 post :: BS.ByteString -> [Header] -> BS.ByteString -> ConnM (Either HTTPError Response)
64 post p hs body = ConnM (\ c -> makeRequest c (Request MPost p (hs' ++ hs) body))
65 where hs' = [ Header "Content-Length" (BS8.pack (show (BS.length body)))
66 , userAgent
67 ]
68
69 httpConnection :: String -> IO Connection
70 httpConnection host = do
71 addrInfo <- Sock.getAddrInfo Nothing (Just host) (Just "http")
72 case addrInfo of
73 [] -> error ("unable to look up " ++ host)
74 info:_ -> do
75 s <- Sock.socket (Sock.addrFamily info)
76 (Sock.addrSocketType info)
77 (Sock.addrProtocol info)
78 Sock.connect s (Sock.addrAddress info)
79 h <- Sock.socketToHandle s H.ReadWriteMode
80 return $ handleToConnection h
81
82 makeRequest :: Connection -> Request -> IO (Either HTTPError Response)
83 makeRequest conn req = do
84 let pReq = pRequest req
85 connWrite conn pReq
86 readResponse conn
+0
-22
src/Network/Vriska/Connection.hs less more
1 {-# LANGUAGE RecordWildCards #-}
2 module Network.Vriska.Connection where
3
4 import qualified Data.ByteString as BS
5 import qualified Data.ByteString.Lazy as LBS
6 import qualified GHC.IO.Handle as H
7
8 import Network.Vriska.Types
9
10 handleToConnection :: H.Handle -> Connection
11 handleToConnection h = Connection { .. }
12 where connRead n = BS.hGet h n
13 connReadLine = BS.hGetLine h
14 connWrite bs = LBS.hPut h bs
15 connClose = H.hClose h
16
17 connReadContents :: Connection -> IO BS.ByteString
18 connReadContents conn = do
19 buf <- connRead conn 1024
20 if buf == ""
21 then connClose conn >> return buf
22 else BS.append buf `fmap` connReadContents conn
+0
-40
src/Network/Vriska/Request.hs less more
1 {-# LANGUAGE RecordWildCards #-}
2 module Network.Vriska.Request where
3
4 import Data.Monoid ((<>))
5 import qualified Data.ByteString.Lazy as B
6 import qualified Data.ByteString.Builder as B
7 import qualified Data.Foldable as F
8
9 import Network.Vriska.Types
10
11 crlf :: B.Builder
12 crlf = B.char7 '\13' <> B.char7 '\10'
13
14 pRequest :: Request -> B.ByteString
15 pRequest Request { .. } = B.toLazyByteString body
16 where body = pMethod requestMethod <> B.char7 ' ' <>
17 B.byteString requestPath <>
18 B.byteString " HTTP/1.1" <>
19 crlf <>
20 F.foldMap pHeader requestHeaders <>
21 crlf <>
22 B.byteString requestBody
23
24 pMethod :: Method -> B.Builder
25 pMethod m = B.byteString $ case m of
26 MGet -> "GET"
27 MHead -> "HEAD"
28 MPost -> "POST"
29 MPut -> "PUT"
30 MDelete -> "DELETE"
31 MTrace -> "TRACE"
32 MOptions -> "OPTIONS"
33 MConnect -> "CONNECT"
34 MPatch -> "PATCH"
35 MOther bs -> bs
36
37 pHeader :: Header -> B.Builder
38 pHeader (Header k v) =
39 B.byteString k <> B.char7 ':' <> B.char7 ' ' <>
40 B.byteString v <> crlf
+0
-50
src/Network/Vriska/Response.hs less more
1 module Network.Vriska.Response where
2
3 import qualified Data.ByteString as BS
4 import qualified Data.ByteString.Char8 as BS8
5
6 import Network.Vriska.Types
7
8 pCode :: BS.ByteString -> (Int, Int, Int)
9 pCode ln
10 | Just (a, as) <- BS.uncons ln
11 , Just (b, bs) <- BS.uncons as
12 , Just (c, "") <- BS.uncons bs
13 = ( fromIntegral (a - 48)
14 , fromIntegral (b - 48)
15 , fromIntegral (c - 48)
16 )
17 | otherwise = error "???"
18
19 readResponse :: Connection -> IO (Either HTTPError Response)
20 readResponse conn = do
21 ln <- connReadLine conn
22 case BS.split 32 ln of
23 (_version:code:_reason) -> do
24 let c = pCode code
25 headers <- readHeaders conn
26 body <- readBody conn headers
27 return (Right (Response headers c (Just body)))
28 _ -> return (Left HTTPError)
29
30 readHeaders :: Connection -> IO [Header]
31 readHeaders conn = do
32 ln <- connReadLine conn
33 let Just (ln', 13) = BS.unsnoc ln
34 if BS.null ln'
35 then return []
36 else do
37 let (key:vals) = BS.split 58 ln'
38 let val = BS.intercalate ":" vals
39 rs <- readHeaders conn
40 return (Header key (BS.tail val) : rs)
41
42 readBody :: Connection -> [Header] -> IO BS.ByteString
43 readBody conn hs
44 = case [ read (BS8.unpack value)
45 | Header name value <- hs
46 , name == "Content-Length"
47 ]
48 of
49 [ len ] -> connRead conn len
50 _ -> return ""
+0
-67
src/Network/Vriska/Types.hs less more
1 {-# LANGUAGE FlexibleInstances #-}
2 module Network.Vriska.Types where
3
4 import qualified Control.Exception as Ex
5 import qualified Data.ByteString as BS
6 import qualified Data.ByteString.Lazy as LBS
7 import Data.Typeable (Typeable)
8
9 data Method
10 = MGet
11 | MHead
12 | MPost
13 | MPut
14 | MDelete
15 | MTrace
16 | MOptions
17 | MConnect
18 | MPatch
19 | MOther BS.ByteString
20 deriving (Eq, Show)
21
22 data Header = Header BS.ByteString BS.ByteString deriving (Eq, Show)
23
24 data Request = Request
25 { requestMethod :: Method
26 , requestPath :: BS.ByteString
27 , requestHeaders :: [Header]
28 , requestBody :: BS.ByteString
29 } deriving (Eq, Show)
30
31 type ResponseCode = (Int, Int, Int)
32
33 data Response = Response
34 { responseHeaders :: [Header]
35 , responseCode :: ResponseCode
36 , responseBody :: Maybe BS.ByteString
37 } deriving (Eq, Show)
38
39 -- | This "Connection" abstraction allows us to build on top of
40 -- arbitrary other connection packages for our HTTP
41 data Connection = Connection
42 { connRead :: Int -> IO BS.ByteString
43 , connReadLine :: IO BS.ByteString
44 , connWrite :: LBS.ByteString -> IO ()
45 , connClose :: IO ()
46 }
47
48 data HTTPError = HTTPError deriving (Typeable, Show)
49
50 instance Ex.Exception HTTPError where
51 toException = undefined
52 fromException = undefined
53
54 class HTTPResult t where
55 fromResponse :: Either HTTPError Response -> t
56
57 instance HTTPResult r => HTTPResult (Either HTTPError r) where
58 fromResponse (Left err) = Left err
59 fromResponse (Right v) = Right (fromResponse (Right v))
60
61 instance HTTPResult r => HTTPResult (Maybe r) where
62 fromResponse (Left _) = Nothing
63 fromResponse (Right v) = Just (fromResponse (Right v))
64
65 instance HTTPResult Response where
66 fromResponse (Left err) = Ex.throw err
67 fromResponse (Right v) = v
+0
-1
src/Network/Vriska/Utils.hs less more
1 module Network.Vriska.Utils where
+0
-35
src/Network/Vriska.hs less more
1 module Network.Vriska where
2
3 import qualified Data.ByteString as BS
4 import qualified Network.Socket as Sock
5 import qualified System.IO as H
6
7 import Network.Vriska.Connection
8 import Network.Vriska.Request
9 import Network.Vriska.Response
10 import Network.Vriska.Types
11
12 request :: Method -> BS.ByteString -> [Header] -> Request
13 request m p hs = Request m p (hs' ++ hs) ""
14 where hs' = [ Header "Content-Length" "0"
15 , Header "User-Agent" "haskell/vriska"
16 ]
17
18 httpConnection :: String -> IO Connection
19 httpConnection host = do
20 addrInfo <- Sock.getAddrInfo Nothing (Just host) (Just "http")
21 case addrInfo of
22 [] -> error ("unable to look up " ++ host)
23 info:_ -> do
24 s <- Sock.socket (Sock.addrFamily info)
25 (Sock.addrSocketType info)
26 (Sock.addrProtocol info)
27 Sock.connect s (Sock.addrAddress info)
28 h <- Sock.socketToHandle s H.ReadWriteMode
29 return $ handleToConnection h
30
31 makeRequest :: Connection -> Request -> IO (Either HTTPError Response)
32 makeRequest conn req = do
33 let pReq = pRequest req
34 connWrite conn pReq
35 readResponse conn
+0
-36
vriska.cabal less more
1 name: vriska
2 version: 0.1.0.0
3 -- synopsis:
4 -- description:
5 license: BSD3
6 license-file: LICENSE
7 author: Getty Ritter <getty.ritter@gmail.com>
8 maintainer: Getty Ritter <getty.ritter@gmail.com>
9 copyright: ©2017 Getty Ritter
10 -- category:
11 build-type: Simple
12 cabal-version: >= 1.14
13
14 library
15 exposed-modules: Network.Vriska
16 , Network.Vriska.Connection
17 , Network.Vriska.Types
18 , Network.Vriska.Request
19 , Network.Vriska.Response
20 , Network.Vriska.Utils
21 hs-source-dirs: src
22 ghc-options: -Wall
23 build-depends: base >=4.7 && <4.10
24 , bytestring
25 , network
26 default-language: Haskell2010
27 default-extensions: OverloadedStrings,
28 ScopedTypeVariables
29
30 executable example
31 hs-source-dirs: example
32 main-is: Main.hs
33 ghc-options: -Wall
34 build-depends: base, vriska, pretty-show
35 default-language: Haskell2010
36 default-extensions: OverloadedStrings