Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dhall.Import refactorings #2646

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dhall-nixpkgs/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import Data.Maybe (mapMaybe)
import Data.Text (Text)
import Data.Void (Void)
import Dhall.Crypto (SHA256Digest (..))
import Dhall.Import (Status (..), stack)
import Dhall.Import (Status, stack)
import Dhall.Parser (Src)
import GHC.Generics (Generic)
import Lens.Micro (rewriteOf)
Expand Down
1 change: 1 addition & 0 deletions dhall/dhall.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ Library
Dhall.Normalize
Dhall.Parser.Combinators
Dhall.Pretty.Internal
Dhall.Settings
Dhall.Syntax
Dhall.Syntax.Binding
Dhall.Syntax.Chunks
Expand Down
43 changes: 19 additions & 24 deletions dhall/ghc-src/Dhall/Import/HTTP.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
Expand All @@ -9,11 +10,11 @@ module Dhall.Import.HTTP
) where

import Control.Exception (Exception)
import Control.Monad (join)
import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.State.Strict (StateT)
import Data.ByteString (ByteString)
import Data.CaseInsensitive (CI)
import Data.Dynamic (toDyn)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Text.Encoding (decodeUtf8)
import Dhall.Core
Expand All @@ -29,27 +30,35 @@ import Dhall.Core
, URL (..)
)
import Dhall.Import.Types
( Chained (..)
, HTTPHeader
, Manager
, OriginHeaders
, PrettyHttpException (..)
, Status (..)
)
import Dhall.Parser (Src)
import Dhall.URL (renderURL)
import Lens.Micro.Mtl (assign, use)
import System.Directory (getXdgDirectory, XdgDirectory(XdgConfig))
import System.FilePath (splitDirectories)


import Network.HTTP.Client (HttpException (..), HttpExceptionContent (..))

import qualified Control.Exception
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.ByteString.Lazy as ByteString.Lazy
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text
import qualified Data.Text.Encoding
import qualified Dhall.Import.Types
import qualified Dhall.Settings
import qualified Dhall.Util
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types

mkPrettyHttpException :: String -> HttpException -> PrettyHttpException
mkPrettyHttpException url ex =
PrettyHttpException (renderPrettyHttpException url ex) (toDyn ex)
PrettyHttpException (renderPrettyHttpException url ex) (Control.Exception.toException ex)

renderPrettyHttpException :: String -> HttpException -> String
renderPrettyHttpException _ (InvalidUrlException _ r) =
Expand Down Expand Up @@ -162,18 +171,9 @@ renderPrettyHttpException url (HttpExceptionRequest _ e) =

newManager :: StateT Status IO Manager
newManager = do
Status { _manager = oldManager, ..} <- State.get

case oldManager of
Nothing -> do
manager <- liftIO _newManager

State.put (Status { _manager = Just manager , ..})

return manager

Just manager ->
return manager
manager <- liftIO =<< use Dhall.Settings.newManager
assign Dhall.Settings.newManager (return manager)
return manager

data NotCORSCompliant = NotCORSCompliant
{ expectedOrigins :: [ByteString]
Expand Down Expand Up @@ -255,7 +255,7 @@ addHeaders originHeaders urlHeaders request =
request { HTTP.requestHeaders = (filterHeaders urlHeaders) <> perOriginHeaders }
where
origin = decodeUtf8 (HTTP.host request) <> ":" <> Text.pack (show (HTTP.port request))

perOriginHeaders = HashMap.lookupDefault [] origin originHeaders

filterHeaders = foldMap (filter (not . overridden))
Expand All @@ -269,10 +269,7 @@ addHeaders originHeaders urlHeaders request =
fetchFromHttpUrlBytes
:: URL -> Maybe [HTTPHeader] -> StateT Status IO ByteString
fetchFromHttpUrlBytes childURL mheaders = do
Status { _loadOriginHeaders } <- State.get

originHeaders <- _loadOriginHeaders

originHeaders <- join (use Dhall.Import.Types.loadOriginHeaders)
manager <- newManager

let childURLString = Text.unpack (renderURL childURL)
Expand All @@ -289,9 +286,7 @@ fetchFromHttpUrlBytes childURL mheaders = do

response <- liftIO (Control.Exception.handle handler io)

Status {..} <- State.get

case _stack of
use Dhall.Import.Types.stack >>= \case
-- We ignore the first import in the stack since that is the same import
-- as the `childUrl`
_ :| Chained parentImport : _ -> do
Expand Down
13 changes: 6 additions & 7 deletions dhall/ghcjs-src/Dhall/Import/Manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

For the GHC implementation the `Dhall.Import.Manager.Manager` type is a real
`Network.HTTP.Client.Manager` from the @http-client@ package. For the GHCJS
implementation the `Dhall.Import.Manager.Manager` type is a synonym for
@`Data.Void.Void`@ since GHCJS does not use a
`Network.HTTP.Client.Manager` for HTTP requests.
implementation the `Dhall.Import.Manager.Manager` type is a stub since GHCJS
does not use a `Network.HTTP.Client.Manager` for HTTP requests.
-}
module Dhall.Import.Manager
( -- * Manager
Expand All @@ -16,11 +15,11 @@ module Dhall.Import.Manager

{-| The GHCJS implementation does not require a `Network.HTTP.Client.Manager`

The purpose of this synonym is so that "Dhall.Import.Types" can import a
The purpose of this type is so that "Dhall.Import.Types" can import a
`Dhall.Import.Manager.Manager` type from "Dhall.Import.HTTP" that does the
correct thing for both the GHC and GHCJS implementations
correct thing for both the GHC and GHCJS implementations.
-}
type Manager = ()
data Manager = Manager

defaultNewManager :: IO Manager
defaultNewManager = pure ()
defaultNewManager = pure Manager
Loading
Loading