Permalink
Browse files

cabalized

  • Loading branch information...
1 parent 72e8130 commit a30da2d7303c557191485270591456ee1e57a8ed @snoyberg committed Sep 21, 2010
Showing with 88 additions and 5 deletions.
  1. +2 −0 .gitignore
  2. +25 −0 LICENSE
  3. +23 −3 Network/HTTP/Enumerator.hs
  4. +4 −1 Http.hs → Network/HTTP/Enumerator/HttpParser.hs
  5. +33 −0 http-enumerator.cabal
  6. +1 −1 test.hs
View
2 .gitignore
@@ -0,0 +1,2 @@
+*.swp
+dist
View
25 LICENSE
@@ -0,0 +1,25 @@
+The following license covers this documentation, and the source code, except
+where otherwise indicated.
+
+Copyright 2010, Michael Snoyman. 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.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "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 HOLDERS 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.
View
26 Network/HTTP/Enumerator.hs
@@ -9,6 +9,7 @@ module Network.HTTP.Enumerator
, parseUrl
, httpLbs
, simpleHttp
+ , withHttpEnumerator
) where
import qualified OpenSSL.Session as SSL
@@ -19,8 +20,7 @@ import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as S8
import Data.Enumerator hiding (head, map, break)
import qualified Data.Enumerator as E
-import Http
-import Safe
+import Network.HTTP.Enumerator.HttpParser
import Control.Exception (throwIO, Exception)
import Control.Arrow (first)
import Data.Char (toLower)
@@ -31,6 +31,20 @@ import Data.Typeable (Typeable)
import Data.Word (Word8)
import Data.Bits
import Data.Maybe (fromMaybe)
+import OpenSSL
+
+-- | The OpenSSL library requires some initialization of variables to be used,
+-- and therefore you must call 'withOpenSSL' before using any of its functions.
+-- As this library uses OpenSSL, you must use 'withOpenSSL' as well. (As a side
+-- note, you'll also want to use the withSocketsDo function for network
+-- activity.)
+--
+-- To future-proof this package against switching to different SSL libraries,
+-- we re-export 'withOpenSSL' under this name. You can call this function as
+-- early as you like; in fact, simply wrapping the do block of your main
+-- function is probably best.
+withHttpEnumerator :: IO a -> IO a
+withHttpEnumerator = withOpenSSL
getSocket :: String -> Int -> IO Socket
getSocket host' port' = do
@@ -90,6 +104,7 @@ data Request = Request
, requestBody :: L.ByteString
, method :: S.ByteString
}
+ deriving Show
data Response a = Response
{ statusCode :: Int
@@ -98,7 +113,7 @@ data Response a = Response
}
http :: Request -> Iteratee S.ByteString IO a -> IO (Response a)
-http (Request {..}) bodyIter = do
+http req@(Request {..}) bodyIter = do
let h' = S8.unpack host
res <- (if secure then withOpenSslConn else withSocketConn) h' port go
case res of
@@ -282,3 +297,8 @@ httpLbs = flip http (L.fromChunks `fmap` consume)
simpleHttp :: String -> IO (Response L.ByteString)
simpleHttp url = parseUrl url >>= httpLbs
+
+readMay :: Read a => String -> Maybe a
+readMay s = case reads s of
+ [] -> Nothing
+ (x, _):_ -> Just x
View
5 Http.hs → Network/HTTP/Enumerator/HttpParser.hs
@@ -1,5 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}
-module Http where
+module Network.HTTP.Enumerator.HttpParser
+ ( iterHeaders
+ , iterChunks
+ ) where
import Prelude hiding (take)
import Data.Attoparsec
View
33 http-enumerator.cabal
@@ -0,0 +1,33 @@
+name: http-enumerator
+version: 0.0.0
+license: BSD3
+license-file: LICENSE
+author: Michael Snoyman <michael@snoyman.com>
+maintainer: Michael Snoyman <michael@snoyman.com>
+synopsis: HTTP client package with enumerator interface and HTTPS support.
+description:
+ This package uses attoparsec for parsing the actual contents of the HTTP connection. The only gotcha is the withHttpEnumerator function, otherwise should do exactly what you expect.
+category: Web
+stability: Experimental
+cabal-version: >= 1.6
+build-type: Simple
+homepage: http://github.com/snoyberg/http-enumerator
+
+library
+ build-depends: base >= 4 && < 5
+ , bytestring >= 0.9.1.4 && < 0.10
+ , transformers >= 0.2 && < 0.3
+ , HsOpenSSL >= 0.8 && < 0.9
+ , failure >= 0.1 && < 0.2
+ , enumerator >= 0.4 && < 0.5
+ , network >= 2.2.1.7 && < 2.3
+ , network-bytestring >= 0.1.3 && < 0.2
+ , attoparsec >= 0.8.0.2 && < 0.9
+ , attoparsec-enumerator >= 0.2 && < 0.3
+ exposed-modules: Network.HTTP.Enumerator
+ other-modules: Network.HTTP.Enumerator.HttpParser
+ ghc-options: -Wall
+
+source-repository head
+ type: git
+ location: git://github.com/snoyberg/http-enumerator.git
View
2 test.hs
@@ -9,7 +9,7 @@ import Data.Enumerator (consume, Iteratee)
import System.Environment (getArgs)
main :: IO ()
-main = withSocketsDo $ withOpenSSL $ do
+main = withSocketsDo $ withHttpEnumerator $ do
let _req1 = Request
{ host = "localhost"
, port = 80

0 comments on commit a30da2d

Please sign in to comment.