on master help sign in
Put User-Agent and Accept-Encoding on all HTTP requests
ccbc53cEvan Czaplicki
builder/src/Reporting/Task/Http.hs+19−1
@@ -18,8 +18,10 @@ import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
18import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, readMVar)
18import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, readMVar)
19import Control.Exception (Exception, SomeException, catch)
19import Control.Exception (Exception, SomeException, catch)
20import Control.Monad (forever, join, replicateM_, void)
20import Control.Monad (forever, join, replicateM_, void)
21+import qualified Data.ByteString.Char8 as BS
21import qualified Network.HTTP as Http (urlEncodeVars)
22import qualified Network.HTTP as Http (urlEncodeVars)
22import qualified Network.HTTP.Client as Http
23import qualified Network.HTTP.Client as Http
24+import qualified Network.HTTP.Types.Header as Http (hAcceptEncoding, hUserAgent)
23
25
24import qualified Elm.Compiler as Compiler
26import qualified Elm.Compiler as Compiler
25import qualified Elm.Package as Pkg
27import qualified Elm.Package as Pkg
@@ -170,7 +172,7 @@ fetchSafe url manager handler =
170fetchUnsafe :: String -> Http.Manager -> Handler a -> IO (Either Exit.Exit a)
172fetchUnsafe :: String -> Http.Manager -> Handler a -> IO (Either Exit.Exit a)
171fetchUnsafe url manager handler =
173fetchUnsafe url manager handler =
172 do request <- Http.parseUrlThrow url
174 do request <- Http.parseUrlThrow url
173− result <- handler request manager
175+ result <- handler (addHeaders request) manager
174 case result of
176 case result of
175 Right value ->
177 Right value ->
176 return (Right value)
178 return (Right value)
@@ -182,3 +184,19 @@ fetchUnsafe url manager handler =
182handleAnyError :: (Exception e) => String -> e -> IO (Either Exit.Exit a)
184handleAnyError :: (Exception e) => String -> e -> IO (Either Exit.Exit a)
183handleAnyError url exception =
185handleAnyError url exception =
184 return $ Left $ Exit.BadHttp url $ E.Unknown $ show exception
186 return $ Left $ Exit.BadHttp url $ E.Unknown $ show exception
187+
188+
189+addHeaders :: Http.Request -> Http.Request
190+addHeaders request =
191+ request
192+ { Http.requestHeaders =
193+ [ (Http.hUserAgent, userAgent)
194+ , (Http.hAcceptEncoding, "gzip")
195+ ]
196+ }
197+
198+
199+{-# NOINLINE userAgent #-}
200+userAgent :: BS.ByteString
201+userAgent =
202+ BS.pack ("elm/" ++ Pkg.versionToString Compiler.version)