http-pony-transformer-startline (empty) → 0.1.0.0
raw patch · 8 files changed
+172/−0 lines, 8 filesdep +attoparsecdep +basedep +bytestringsetup-changed
Dependencies added: attoparsec, base, bytestring, http-types, lens
Files
- ChangeLog.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- http-pony-transformer-startline.cabal +33/−0
- src/Network/HTTP/Pony/Transformer/StartLine.hs +42/−0
- src/Network/HTTP/Pony/Transformer/StartLine/Builder.hs +18/−0
- src/Network/HTTP/Pony/Transformer/StartLine/Parser.hs +31/−0
- src/Network/HTTP/Pony/Transformer/StartLine/Type.hs +11/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for http-pony-transformer-startline++## 0.1.0.0 -- 2016-09-22++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2016, Jinjing Wang++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * 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.++ * Neither the name of Jinjing Wang nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++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+OWNER 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ http-pony-transformer-startline.cabal view
@@ -0,0 +1,33 @@+-- Initial http-pony-transformer-startline.cabal generated by cabal init. +-- For further documentation, see http://haskell.org/cabal/users-guide/++name: http-pony-transformer-startline+version: 0.1.0.0+synopsis: transform HTTP startlines to tuples+-- description: +license: BSD3+license-file: LICENSE+author: Jinjing Wang+maintainer: nfjinjing@gmail.com+-- copyright: +category: Network+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Network.HTTP.Pony.Transformer.StartLine+ , Network.HTTP.Pony.Transformer.StartLine.Type+ , Network.HTTP.Pony.Transformer.StartLine.Parser+ , Network.HTTP.Pony.Transformer.StartLine.Builder+ -- other-modules: + -- other-extensions: FlexibleContexts, OverloadedStrings++ build-depends: base >=4.9 && <4.10+ , attoparsec >=0.13+ , bytestring >=0.10+ , http-types >= 0.9.1+ , lens >= 4.14++ hs-source-dirs: src+ default-language: Haskell2010
+ src/Network/HTTP/Pony/Transformer/StartLine.hs view
@@ -0,0 +1,42 @@+-- |++{-# LANGUAGE FlexibleContexts #-}++module Network.HTTP.Pony.Transformer.StartLine where++import Control.Lens (over, ASetter, _1, _2, view+ , Field1, Field2, set, Getting)+import Data.Attoparsec.ByteString (parse, eitherResult)+import Data.ByteString (ByteString)+import qualified Network.HTTP.Types as HTTP++import qualified Network.HTTP.Pony.Transformer.StartLine.Builder as Builder+import qualified Network.HTTP.Pony.Transformer.StartLine.Parser as Parser+import Network.HTTP.Pony.Transformer.StartLine.Type (RequestURI, RequestLine, ResponseLine)+++-- Here f and f' should really be the same Lens from the Request into the StartLine+startLineWith :: (Monoid a, Monad f) => Getting ByteString s ByteString+ -> ASetter s t a1 RequestLine+ -> ASetter s1 a ResponseLine ByteString+ -> (t -> f s1)+ -> s+ -> f a+startLineWith f f' g app request = do+ let requestLine = view f request+ case eitherResult (parse Parser.requestLineTokens requestLine) of+ Left _ -> pure mempty+ Right r -> do+ response <- app (set f' r request)+ pure (over g Builder.response response)+++startLine :: ( Field1 s3 b2 ByteString ByteString+ , Field1 s3 b ByteString (HTTP.Method, RequestURI, HTTP.HttpVersion)+ , Field1 s2 b1 ResponseLine ByteString+ , Field1 s1 a s2 b1+ , Field1 s s s3 b2+ , Field1 s t s3 b+ , Monoid a+ , Monad f) => (t -> f s1) -> s -> f a+startLine = startLineWith (_1 . _1) (_1 . _1) (_1 . _1)
+ src/Network/HTTP/Pony/Transformer/StartLine/Builder.hs view
@@ -0,0 +1,18 @@+{-# LANGUAGE OverloadedStrings #-}++module Network.HTTP.Pony.Transformer.StartLine.Builder where++import qualified Network.HTTP.Types as HTTP+import qualified Data.ByteString.Char8 as B+import Data.ByteString (ByteString)+import Data.Semigroup ((<>))++import Network.HTTP.Pony.Transformer.StartLine.Type (ResponseLine)++response :: ResponseLine -> ByteString+response (version, (HTTP.Status code message)) =+ B.pack (show version)+ <> " "+ <> B.pack (show code)+ <> " "+ <> message
+ src/Network/HTTP/Pony/Transformer/StartLine/Parser.hs view
@@ -0,0 +1,31 @@+{-# LANGUAGE OverloadedStrings #-}++module Network.HTTP.Pony.Transformer.StartLine.Parser where+++import Data.Attoparsec.ByteString (Parser)+import qualified Data.Attoparsec.ByteString.Char8 as Char+import Data.ByteString.Char8 (ByteString)+import qualified Data.ByteString.Char8 as B+import Data.Char (ord)+import qualified Network.HTTP.Types as HTTP++import Network.HTTP.Pony.Transformer.StartLine.Type (RequestLine)++requestLineTokens :: Parser RequestLine+requestLineTokens = do+ method <- Char.takeTill (== ' ')+ Char.char ' '+ requestURI <- Char.takeTill (== ' ')+ Char.char ' '+ Char.string "HTTP/"++ let minusZero = (+) (- ord '0')+ charToInt = minusZero . ord+ digit = fmap charToInt Char.digit++ major <- digit+ Char.char '.'+ minor <- digit++ pure (method, requestURI, HTTP.HttpVersion major minor)
+ src/Network/HTTP/Pony/Transformer/StartLine/Type.hs view
@@ -0,0 +1,11 @@+-- | ++module Network.HTTP.Pony.Transformer.StartLine.Type where++import Data.ByteString (ByteString)+import qualified Network.HTTP.Types as HTTP++type RequestURI = ByteString++type RequestLine = (HTTP.Method, RequestURI, HTTP.HttpVersion)+type ResponseLine = (HTTP.HttpVersion, HTTP.Status)