Skip to content

Commit

Permalink
Support no_proxy environment variables. snoyberg#140
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiddens committed Aug 7, 2015
1 parent 76bc8b6 commit 92535a0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions http-client/Network/HTTP/Client/Manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import qualified Data.ByteString.Lazy as L

import qualified Blaze.ByteString.Builder as Blaze

import Data.Char (toLower)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Read (decimal)
Expand Down Expand Up @@ -506,7 +507,8 @@ envHelper :: Text -> EnvHelper -> IO (Request -> Request)
envHelper name eh = do
env <- getEnvironment
let lenv = Map.fromList $ map (first $ T.toLower . T.pack) env
case lookup (T.unpack name) env <|> Map.lookup name lenv of
lookupEnvVar n = lookup (T.unpack n) env <|> Map.lookup n lenv
case lookupEnvVar name of
Nothing -> return noEnvProxy
Just "" -> return noEnvProxy
Just str -> do
Expand Down Expand Up @@ -541,9 +543,17 @@ envHelper name eh = do

Just $ (Proxy (S8.pack $ U.uriRegName auth) port, muserpass)
return $ \req ->
maybe id (uncurry applyBasicProxyAuth) muserpass
req { proxy = Just p }
if host req `hasDomainSuffixIn` lookupEnvVar "no_proxy"
then noEnvProxy req
else maybe id (uncurry applyBasicProxyAuth) muserpass
req { proxy = Just p }
where noEnvProxy = case eh of
EHFromRequest -> id
EHNoProxy -> \req -> req { proxy = Nothing }
EHUseProxy p -> \req -> req { proxy = Just p }
prefixed s | S8.head s == '.' = s
| otherwise = S8.cons '.' s
domainSuffixes no_proxy = [ prefixed $ S8.dropWhile (== ' ') suffix | suffix <- S8.split ',' (S8.pack (map toLower no_proxy)), not (S8.null suffix)]
hasDomainSuffixIn _ Nothing = False
hasDomainSuffixIn _ (Just "") = False
hasDomainSuffixIn host (Just no_proxy) = any (`S8.isSuffixOf` prefixed (S8.map toLower host)) (domainSuffixes no_proxy)

0 comments on commit 92535a0

Please sign in to comment.