webapp: Use IP address, rather than localhost

since some systems may have configuration problems or other issues that
prevent web browsers from connecting to the right localhost IP for the
webapp.

Tested on both ipv4 and ipv6 localhost. Url for the latter looks like:
http://[::1]:50676
This commit is contained in:
Joey Hess 2013-01-09 23:17:52 -04:00
parent b78c71ca4c
commit 6f7ae84650
4 changed files with 18 additions and 12 deletions

View file

@ -35,7 +35,7 @@ import Git
import Yesod import Yesod
import Yesod.Static import Yesod.Static
import Network.Socket (PortNumber) import Network.Socket (SockAddr)
import Data.Text (pack, unpack) import Data.Text (pack, unpack)
thisThread :: String thisThread :: String
@ -67,14 +67,14 @@ webAppThread assistantdata urlrenderer noannex postfirstrun onstartup = thread $
( return $ httpDebugLogger app ( return $ httpDebugLogger app
, return app , return app
) )
runWebApp app' $ \port -> if noannex runWebApp app' $ \addr -> if noannex
then withTempFile "webapp.html" $ \tmpfile _ -> then withTempFile "webapp.html" $ \tmpfile _ ->
go port webapp tmpfile Nothing go addr webapp tmpfile Nothing
else do else do
let st = threadState assistantdata let st = threadState assistantdata
htmlshim <- runThreadState st $ fromRepo gitAnnexHtmlShim htmlshim <- runThreadState st $ fromRepo gitAnnexHtmlShim
urlfile <- runThreadState st $ fromRepo gitAnnexUrlFile urlfile <- runThreadState st $ fromRepo gitAnnexUrlFile
go port webapp htmlshim (Just urlfile) go addr webapp htmlshim (Just urlfile)
where where
thread = NamedThread thisThread thread = NamedThread thisThread
getreldir getreldir
@ -82,13 +82,13 @@ webAppThread assistantdata urlrenderer noannex postfirstrun onstartup = thread $
| otherwise = Just <$> | otherwise = Just <$>
(relHome =<< absPath (relHome =<< absPath
=<< runThreadState (threadState assistantdata) (fromRepo repoPath)) =<< runThreadState (threadState assistantdata) (fromRepo repoPath))
go port webapp htmlshim urlfile = do go addr webapp htmlshim urlfile = do
let url = myUrl webapp port let url = myUrl webapp addr
maybe noop (`writeFileProtected` url) urlfile maybe noop (`writeFileProtected` url) urlfile
writeHtmlShim "Starting webapp..." url htmlshim writeHtmlShim "Starting webapp..." url htmlshim
maybe noop (\a -> a url htmlshim) onstartup maybe noop (\a -> a url htmlshim) onstartup
myUrl :: WebApp -> PortNumber -> Url myUrl :: WebApp -> SockAddr -> Url
myUrl webapp port = unpack $ yesodRender webapp urlbase HomeR [] myUrl webapp addr = unpack $ yesodRender webapp urlbase HomeR []
where where
urlbase = pack $ "http://localhost:" ++ show port urlbase = pack $ "http://" ++ show addr

View file

@ -54,14 +54,14 @@ runBrowser url env = boolSystemEnv cmd [Param url] env
{- Binds to a socket on localhost, and runs a webapp on it. {- Binds to a socket on localhost, and runs a webapp on it.
- -
- An IO action can also be run, to do something with the port number, - An IO action can also be run, to do something with the address,
- such as start a web browser to view the webapp. - such as start a web browser to view the webapp.
-} -}
runWebApp :: Wai.Application -> (PortNumber -> IO ()) -> IO () runWebApp :: Wai.Application -> (SockAddr -> IO ()) -> IO ()
runWebApp app observer = do runWebApp app observer = do
sock <- localSocket sock <- localSocket
void $ forkIO $ runSettingsSocket defaultSettings sock app void $ forkIO $ runSettingsSocket defaultSettings sock app
observer =<< socketPort sock observer =<< getSocketName sock
{- Binds to a local socket, selecting any free port. {- Binds to a local socket, selecting any free port.
- -

3
debian/changelog vendored
View file

@ -6,6 +6,9 @@ git-annex (3.20130108) UNRELEASED; urgency=low
* drop: Suggest using git annex move when numcopies prevents dropping a file. * drop: Suggest using git annex move when numcopies prevents dropping a file.
* webapp: Repo switcher filters out repos that do not exist any more * webapp: Repo switcher filters out repos that do not exist any more
(or are on a drive that's not mounted). (or are on a drive that's not mounted).
* webapp: Use IP address, rather than localhost, since some systems may
have configuration problems or other issues that prevent web browsers
from connecting to the right localhost IP for the webapp.
-- Joey Hess <joeyh@debian.org> Tue, 08 Jan 2013 12:37:38 -0400 -- Joey Hess <joeyh@debian.org> Tue, 08 Jan 2013 12:37:38 -0400

View file

@ -25,3 +25,6 @@ I've tested this with:
* chromium * chromium
Iceweasel is the only one which correctly fell back to IPv4 and worked. Iceweasel is the only one which correctly fell back to IPv4 and worked.
> Ok, I've made it use the IP address in the URL. Ugly, but avoids
> whatever mess results in this behavior. [[done]] --[[Joey]]