Skip to content

Commit c822819

Browse files
committed
hackage-build: Port from HTTP to http-client-tls
Introduce support for TLS Hackage upstreams to the documentation builder.
1 parent 2b8e5f6 commit c822819

File tree

3 files changed

+164
-90
lines changed

3 files changed

+164
-90
lines changed

exes/BuildClient.hs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{-# LANGUAGE PatternGuards #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# LANGUAGE OverloadedStrings #-}
34
module Main (main) where
45

5-
import Network.HTTP hiding (password)
6-
import Network.Browser
6+
import Network.HTTP.Types.Header
7+
import Network.HTTP.Types.Status
78
import Network.URI (URI(..))
89
import Distribution.Client
910
import Distribution.Client.Cron (cron, rethrowSignalsAsExceptions,
@@ -26,6 +27,7 @@ import Control.Applicative as App
2627
import Control.Exception
2728
import Control.Monad
2829
import Control.Monad.Trans
30+
import qualified Data.ByteString.Char8 as BSS
2931
import qualified Data.ByteString.Lazy as BS
3032
import qualified Data.Map as M
3133

@@ -51,6 +53,7 @@ import Paths_hackage_server (version)
5153

5254
import Data.Aeson (eitherDecode, encode, parseJSON)
5355
import Data.Aeson.Types (parseEither)
56+
import Distribution.Server.Framework (resp)
5457

5558
data Mode = Help [String]
5659
| Init URI [URI]
@@ -878,20 +881,28 @@ uploadResults :: Verbosity -> BuildConfig -> DocInfo -> Maybe FilePath
878881
uploadResults verbosity config docInfo
879882
mdocsTarballFile buildReportFile buildLogFile testLogFile coverageFile installOk =
880883
httpSession verbosity "hackage-build" version $ do
881-
-- Make sure we authenticate to Hackage
882-
setAuthorityGen (provideAuthInfo (bc_srcURI config)
883-
(Just (bc_username config, bc_password config)))
884884
case mdocsTarballFile of
885885
Nothing -> return ()
886886
Just docsTarballFile ->
887887
putDocsTarball config docInfo docsTarballFile
888888

889889
putBuildFiles config docInfo buildReportFile buildLogFile testLogFile coverageFile installOk
890890

891+
withAuth :: BuildConfig -> Request -> Request
892+
withAuth config req =
893+
noRedirects $ applyBasicAuth (BSS.pack $ bc_username config) (BSS.pack $ bc_password config) req
894+
891895
putDocsTarball :: BuildConfig -> DocInfo -> FilePath -> HttpSession ()
892-
putDocsTarball config docInfo docsTarballFile =
893-
requestPUTFile (docInfoDocsURI config docInfo)
894-
"application/x-tar" (Just "gzip") docsTarballFile
896+
putDocsTarball config docInfo docsTarballFile = do
897+
body <- liftIO $ BS.readFile docsTarballFile
898+
req <- withAuth config <$> mkUploadRequest "PUT" uri mimetype mEncoding [] body
899+
runRequest req $ \rsp -> do
900+
rsp' <- responseReadBSL rsp
901+
checkStatus uri rsp'
902+
where
903+
uri = docInfoDocsURI config docInfo
904+
mimetype = "application/x-tar"
905+
mEncoding = Just "gzip"
895906

896907
putBuildFiles :: BuildConfig -> DocInfo -> Maybe FilePath
897908
-> FilePath -> Maybe FilePath -> Maybe FilePath -> Bool -> HttpSession ()
@@ -902,22 +913,17 @@ putBuildFiles config docInfo reportFile buildLogFile testLogFile coverageFile in
902913
coverageContent <- liftIO $ traverse readFile coverageFile
903914
let uri = docInfoReports config docInfo
904915
body = encode $ BR.BuildFiles reportContent (Just logContent) testContent coverageContent (not installOk)
905-
setAllowRedirects False
906-
(_, response) <- request Request {
907-
rqURI = uri,
908-
rqMethod = PUT,
909-
rqHeaders = [Header HdrContentType "application/json",
910-
Header HdrContentLength (show (BS.length body))],
911-
rqBody = body
912-
}
913-
case rspCode response of
914-
--TODO: fix server to not do give 303, 201 is more appropriate
915-
(3,0,3) -> return ()
916-
_ -> do checkStatus uri response
916+
let headers = [ (hAccept, BSS.pack "application/json") ]
917+
req <- withAuth config <$> mkUploadRequest (BSS.pack "PUT") uri "application/json" Nothing headers body
918+
runRequest req $ \rsp -> do
919+
case statusCode $ responseStatus rsp of
920+
--TODO: fix server to not do give 303, 201 is more appropriate
921+
303 -> return ()
922+
_ -> do rsp' <- responseReadBSL rsp
923+
checkStatus uri rsp'
917924
fail "Unexpected response from server."
918925

919926

920-
921927
-------------------------
922928
-- Command line handling
923929
-------------------------

hackage-server.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ library
409409
build-depends:
410410
, HStringTemplate ^>= 0.8
411411
, HTTP ^>= 4000.3.16 || ^>= 4000.4.1
412+
, http-client ^>= 0.7 && < 0.8
413+
, http-client-tls ^>= 0.3
414+
, http-types >= 0.10 && < 0.13
412415
, QuickCheck >= 2.14 && < 2.16
413416
, acid-state ^>= 0.16
414417
, async ^>= 2.2.1
@@ -454,6 +457,7 @@ library
454457
, stm ^>= 2.5.0
455458
, stringsearch ^>= 0.3.6.6
456459
, tagged ^>= 0.8.5
460+
, transformers ^>= 0.6
457461
, xhtml >= 3000.2.0.0 && < 3000.4
458462
, xmlgen ^>= 0.6
459463
, xss-sanitize ^>= 0.3.6
@@ -506,7 +510,7 @@ executable hackage-build
506510

507511
build-depends:
508512
-- version constraints inherited from hackage-server
509-
, HTTP
513+
, http-types
510514

511515
-- Runtime dependency only;
512516
-- TODO: we have no proper support for this kind of dependencies in cabal

0 commit comments

Comments
 (0)