1
1
{-# LANGUAGE PatternGuards #-}
2
2
{-# LANGUAGE ScopedTypeVariables #-}
3
+ {-# LANGUAGE OverloadedStrings #-}
3
4
module Main (main ) where
4
5
5
- import Network.HTTP hiding ( password )
6
- import Network.Browser
6
+ import Network.HTTP.Types.Header
7
+ import Network.HTTP.Types.Status
7
8
import Network.URI (URI (.. ))
8
9
import Distribution.Client
9
10
import Distribution.Client.Cron (cron , rethrowSignalsAsExceptions ,
@@ -26,6 +27,7 @@ import Control.Applicative as App
26
27
import Control.Exception
27
28
import Control.Monad
28
29
import Control.Monad.Trans
30
+ import qualified Data.ByteString.Char8 as BSS
29
31
import qualified Data.ByteString.Lazy as BS
30
32
import qualified Data.Map as M
31
33
@@ -51,6 +53,7 @@ import Paths_hackage_server (version)
51
53
52
54
import Data.Aeson (eitherDecode , encode , parseJSON )
53
55
import Data.Aeson.Types (parseEither )
56
+ import Distribution.Server.Framework (resp )
54
57
55
58
data Mode = Help [String ]
56
59
| Init URI [URI ]
@@ -878,20 +881,28 @@ uploadResults :: Verbosity -> BuildConfig -> DocInfo -> Maybe FilePath
878
881
uploadResults verbosity config docInfo
879
882
mdocsTarballFile buildReportFile buildLogFile testLogFile coverageFile installOk =
880
883
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)))
884
884
case mdocsTarballFile of
885
885
Nothing -> return ()
886
886
Just docsTarballFile ->
887
887
putDocsTarball config docInfo docsTarballFile
888
888
889
889
putBuildFiles config docInfo buildReportFile buildLogFile testLogFile coverageFile installOk
890
890
891
+ withAuth :: BuildConfig -> Request -> Request
892
+ withAuth config req =
893
+ noRedirects $ applyBasicAuth (BSS. pack $ bc_username config) (BSS. pack $ bc_password config) req
894
+
891
895
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"
895
906
896
907
putBuildFiles :: BuildConfig -> DocInfo -> Maybe FilePath
897
908
-> FilePath -> Maybe FilePath -> Maybe FilePath -> Bool -> HttpSession ()
@@ -902,22 +913,17 @@ putBuildFiles config docInfo reportFile buildLogFile testLogFile coverageFile in
902
913
coverageContent <- liftIO $ traverse readFile coverageFile
903
914
let uri = docInfoReports config docInfo
904
915
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'
917
924
fail " Unexpected response from server."
918
925
919
926
920
-
921
927
-------------------------
922
928
-- Command line handling
923
929
-------------------------
0 commit comments