http-directory 0.1.0 → 0.1.1
raw patch · 4 files changed
+49/−8 lines, 4 filesdep +http-datedep +timePVP ok
version bump matches the API change (PVP)
Dependencies added: http-date, time
API changes (from Hackage documentation)
+ Network.HTTP.Directory: httpLastModified :: Manager -> String -> IO (Maybe UTCTime)
Files
- CHANGELOG.md +3/−0
- README.md +9/−0
- http-directory.cabal +5/−2
- src/Network/HTTP/Directory.hs +32/−6
CHANGELOG.md view
@@ -2,5 +2,8 @@ `http-directory` uses [PVP Versioning](https://pvp.haskell.org). +## 0.1.1+- add httpLastModified+ ## 0.1.0 - initial release with httpDirectory, httpFileSize, and httpRedirect(s)
README.md view
@@ -7,3 +7,12 @@ [](https://travis-ci.org/juhp/http-directory) A simple library for reading http directories.++It uses http-client for http transport, and+html-conduit and xml-conduit to parse the html for links.++The library is intended for listing the files in http file directories,+but since http directories are just html pages it can actually be used+to list the links (href's) on any html webpage.++See the haddock documentation for usage.
http-directory.cabal view
@@ -1,10 +1,11 @@ cabal-version: 1.18 name: http-directory-version: 0.1.0+version: 0.1.1 synopsis: http directory listing library description: Library for listing http directories of files (links).- Also it can check the size of files, and url redirects.+ Also it can check the size and modtime of files,+ and for url redirects. homepage: https://github.com/juhp/http-directory bug-reports: https://github.com/juhp/http-directory/issues license: MIT@@ -32,8 +33,10 @@ bytestring, html-conduit, http-client,+ http-date, http-types, text,+ time, xml-conduit if impl(ghc<8.0) Build-depends: semigroups
src/Network/HTTP/Directory.hs view
@@ -6,17 +6,22 @@ @ import Network.HTTP.Directory import qualified Data.Text as T+import Network.HTTP.Client (newManager)+import Network.HTTP.Client.TLS (tlsManagerSettings) main = do- files <- httpDirectory "https://example.com/some/dir/"+ mgr <- newManager tlsManagerSettings+ files <- httpDirectory mgr "https://example.com/some/dir/" mapM_ T.putStrLn files- httpFileSize (head files) >>= print+ httpFileSize mgr (head files) >>= print+ httpLastModified mgr (head files) >>= print @ -} module Network.HTTP.Directory ( httpDirectory, httpFileSize,+ httpLastModified, httpRedirect, httpRedirects ) where@@ -29,10 +34,12 @@ import qualified Data.ByteString.Char8 as B import Data.Maybe import Data.Text (Text)+import Data.Time.Clock (UTCTime) import Network.HTTP.Client (hrRedirects, httpLbs, httpNoBody, Manager, method, parseRequest, responseBody, responseHeaders, responseOpenHistory, responseStatus)+import Network.HTTP.Date (httpDateToUTC, parseHTTPDate) import Network.HTTP.Types (hContentLength, hLocation, statusCode) import Text.HTML.DOM (parseLBS)@@ -40,8 +47,11 @@ -- | List the file links (hrefs) in an http directory ----- Note if the directory (webpage) url is redirected you may need to use--- 'httpRedirect' to determine the actual final url prefix for relative links+-- Raises an error if the http request fails.+--+-- Note if the directory (webpage) url is redirected to a different path+-- you may need to use 'httpRedirect' to determine+-- the actual final url prefix for relative links -- (files). httpDirectory :: Manager -> String -> IO [Text] httpDirectory mgr url = do@@ -55,7 +65,9 @@ cursor = fromDocument doc return $ concatMap (attribute "href") $ cursor $// element "a" --- | Try to get the filesize (Content-Length) of an http file+-- | Try to get the filesize (Content-Length field) of an http file+--+-- Raises an error if the http request fails. httpFileSize :: Manager -> String -> IO (Maybe Integer) httpFileSize mgr url = do request <- parseRequest url@@ -66,8 +78,22 @@ let headers = responseHeaders response return $ read . B.unpack <$> lookup hContentLength headers +-- | Try to get the modification time (Last-Modified field) of an http file+--+-- Raises an error if the http request fails.+httpLastModified :: Manager -> String -> IO (Maybe UTCTime)+httpLastModified mgr url = do+ request <- parseRequest url+ response <- httpNoBody (request {method = "HEAD"}) mgr+ if statusCode (responseStatus response) /= 200+ then error $ show $ responseStatus response+ else do+ let headers = responseHeaders response+ mdate = lookup "Last-Modified" headers+ return $ httpDateToUTC <$> maybe Nothing parseHTTPDate mdate+ -- | Returns the list of http redirects for an url in reverse order--- (ie last redirect is first)+-- (ie last redirect is listed first) httpRedirects :: Manager -> String -> IO [B.ByteString] httpRedirects mgr url = do request <- parseRequest url