web: Add IPv6 and hostname support

This commit introduces IPv6 support (and thus closes #1145).
It also allows using local hostnames as a parameter for --host.
For this, multiple things needed to be changed:

 - checkWebOpts is dropped, as the supplied parameter is checked later
 by Network.Socket.getAddrInfo
 - defbaseurl needs to check if : is used in the host, as this indicates
 the usage of an IPv6 address. In this case, the host needs to be
 wrapped in [] for the base URL
 - To allow opening such a modified base URL, runHostPortFullUrl is used
 instead of runhostPortUrl, as it allows opening arbitrary URLs instead
 of a path prefixed with http://127.0.0.1

As checking the host for validity is postponed until the webserver tries
to start, an invalid hostname leads to an exception caused by
Network.Socket.getAddrInfo.
This is still fine, as hledger-web won't start in an undefined state, but
will terminate with a nonzero exit code.
This commit is contained in:
Amarandus 2020-01-14 22:18:05 +01:00 committed by Simon Michael
parent 392d0d0a31
commit 76ffaca466
6 changed files with 12 additions and 12 deletions

View File

@ -16,7 +16,7 @@ import qualified Data.Text as T
import Network.Socket import Network.Socket
import Network.Wai (Application) import Network.Wai (Application)
import Network.Wai.Handler.Warp (runSettings, runSettingsSocket, defaultSettings, setHost, setPort) import Network.Wai.Handler.Warp (runSettings, runSettingsSocket, defaultSettings, setHost, setPort)
import Network.Wai.Handler.Launch (runHostPortUrl) import Network.Wai.Handler.Launch (runHostPortFullUrl)
import Prelude hiding (putStrLn) import Prelude hiding (putStrLn)
import System.Directory (removeFile) import System.Directory (removeFile)
import System.Exit (exitSuccess, exitFailure) import System.Exit (exitSuccess, exitFailure)
@ -106,5 +106,5 @@ web opts j = do
putStrLn "Opening web browser..." putStrLn "Opening web browser..."
hFlush stdout hFlush stdout
-- exits after 2m of inactivity (hardcoded) -- exits after 2m of inactivity (hardcoded)
Network.Wai.Handler.Launch.runHostPortUrl h p "" app Network.Wai.Handler.Launch.runHostPortFullUrl h p u app

View File

@ -45,7 +45,10 @@ defport = 5000
defbaseurl :: String -> Int -> String defbaseurl :: String -> Int -> String
defbaseurl host port = defbaseurl host port =
"http://" ++ host ++ if port /= 80 then ":" ++ show port else "" if ':' `elem` host then
"http://[" ++ host ++ "]" ++ if port /= 80 then ":" ++ show port else ""
else
"http://" ++ host ++ if port /= 80 then ":" ++ show port else ""
-- Static setting below. Changing these requires a recompile -- Static setting below. Changing these requires a recompile

View File

@ -158,11 +158,7 @@ rawOptsToWebOpts rawopts =
stripTrailingSlash = reverse . dropWhile (== '/') . reverse -- yesod don't like it stripTrailingSlash = reverse . dropWhile (== '/') . reverse -- yesod don't like it
checkWebOpts :: WebOpts -> WebOpts checkWebOpts :: WebOpts -> WebOpts
checkWebOpts wopts = do checkWebOpts = id
let h = host_ wopts
if any (`notElem` (".0123456789" :: String)) h
then usageError $ "--host requires an IP address, not " ++ show h
else wopts
getHledgerWebOpts :: IO WebOpts getHledgerWebOpts :: IO WebOpts
getHledgerWebOpts = do getHledgerWebOpts = do

View File

@ -1,10 +1,10 @@
cabal-version: 1.12 cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.31.2. -- This file has been generated from package.yaml by hpack version 0.32.0.
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
-- --
-- hash: cbf332de4147396a82198ccf5173db10b775ed99f9830e89aea475bfd4da5a7f -- hash: 0376acf91d14e3868fa58b399419538104d2187ad41552aa62bd24ca675274b3
name: hledger-web name: hledger-web
version: 1.16.99 version: 1.16.99
@ -188,7 +188,7 @@ library
, wai , wai
, wai-cors , wai-cors
, wai-extra , wai-extra
, wai-handler-launch >=1.3 , wai-handler-launch >=3.0.3
, warp , warp
, yaml , yaml
, yesod >=1.4 && <1.7 , yesod >=1.4 && <1.7

View File

@ -131,7 +131,7 @@ library:
- utf8-string - utf8-string
- wai - wai
- wai-extra - wai-extra
- wai-handler-launch >=1.3 - wai-handler-launch >=3.0.3
- wai-cors - wai-cors
- warp - warp
- yaml - yaml

View File

@ -18,3 +18,4 @@ extra-deps:
# for hledger: # for hledger:
# for hledger-ui: # for hledger-ui:
# for hledger-web: # for hledger-web:
- wai-handler-launch-3.0.3