Skip to content

Commit

Permalink
Add function applyBasicAuth.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikd committed May 12, 2011
1 parent 9d38027 commit 346c166
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Network/HTTP/Enumerator.hs
Expand Up @@ -69,6 +69,7 @@ module Network.HTTP.Enumerator
, withManager
-- * Utility functions
, parseUrl
, applyBasicAuth
, semiParseUrl
, lbsIter
-- * Request bodies
Expand Down Expand Up @@ -112,6 +113,7 @@ import qualified Data.IORef as I
import Control.Applicative ((<$>))
import Data.Certificate.X509 (X509)
import Network.TLS.Extra (certificateVerifyChain, certificateVerifyDomain)
import qualified Data.ByteString.Base64 as B64

getSocket :: String -> Int -> IO NS.Socket
getSocket host' port' = do
Expand Down Expand Up @@ -192,6 +194,21 @@ data RequestBody m
| RequestBodyBuilder Int64 Blaze.Builder
| RequestBodyEnum Int64 (Enumerator Blaze.Builder m ())


-- | Add a Basic Auth header (with the specified user name and password) to the
-- given Request. Ignore error handling:
--
-- applyBasicAuth "user" "pass" $ fromJust $ HE.parseUrl url

applyBasicAuth :: S8.ByteString -> S8.ByteString -> Request m -> Request m
applyBasicAuth user passwd req =
req { requestHeaders = authHeader : requestHeaders req }
where
authHeader = (CI.mk "Authorization", basic)
basic = S8.append "Basic " (B64.encode $ S8.concat [ user, ":", passwd ])



-- | A simple representation of the HTTP response created by 'lbsIter'.
data Response = Response
{ statusCode :: Int
Expand Down

0 comments on commit 346c166

Please sign in to comment.