1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00

Add option to automatically follow redirects (#75)

Co-authored-by: makeworld
This commit is contained in:
Sotiris Papatheodorou 2020-08-28 01:55:42 +03:00 committed by GitHub
parent 580e87bcc9
commit 4452fcfec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 7 deletions

View File

@ -145,6 +145,7 @@ func Init() error {
}
viper.SetDefault("a-general.home", "gemini.circumlunar.space")
viper.SetDefault("a-general.auto_redirect", false)
viper.SetDefault("a-general.http", "default")
viper.SetDefault("a-general.search", "gus.guru/search")
viper.SetDefault("a-general.color", true)

View File

@ -16,6 +16,11 @@ var defaultConf = []byte(`# This is the default config file.
# Press Ctrl-H to access it
home = "gemini://gemini.circumlunar.space"
# Follow up to 5 Gemini redirects without prompting.
# A prompt is always shown after the 5th redirect and for redirects to protocols other than Gemini.
# If set to false, a prompt will be shown before following redirects.
auto_redirect = false
# What command to run to open a HTTP URL. Set to "default" to try to guess the browser,
# or set to "off" to not open HTTP URLs.
# If a command is set, than the URL will be added (in quotes) to the end of the command.

View File

@ -13,6 +13,11 @@
# Press Ctrl-H to access it
home = "gemini://gemini.circumlunar.space"
# Follow up to 5 Gemini redirects without prompting.
# A prompt is always shown after the 5th redirect and for redirects to protocols other than Gemini.
# If set to false, a prompt will be shown before following redirects.
auto_redirect = false
# What command to run to open a HTTP URL. Set to "default" to try to guess the browser,
# or set to "off" to not open HTTP URLs.
# If a command is set, than the URL will be added (in quotes) to the end of the command.

View File

@ -521,7 +521,7 @@ func Reload() {
go func(t *tab) {
cache.RemovePage(tabs[curTab].page.URL)
cache.RemoveFavicon(parsed.Host)
handleURL(t, t.page.URL) // goURL is not used bc history shouldn't be added to
handleURL(t, t.page.URL, 0) // goURL is not used bc history shouldn't be added to
if t == tabs[curTab] {
// Display the bottomBar state that handleURL set
t.applyBottomBar()

View File

@ -2,7 +2,7 @@ package display
// applyHist is a history.go internal function, to load a URL in the history.
func applyHist(t *tab) {
handleURL(t, t.history.urls[t.history.pos]) // Load that position in history
handleURL(t, t.history.urls[t.history.pos], 0) // Load that position in history
t.applyAll()
}

View File

@ -267,7 +267,7 @@ func handleFavicon(t *tab, host, old string) {
//
// It should be called in a goroutine.
func goURL(t *tab, u string) {
final, displayed := handleURL(t, u)
final, displayed := handleURL(t, u, 0)
if displayed {
t.addToHistory(final)
}
@ -289,7 +289,10 @@ func goURL(t *tab, u string) {
//
// The bottomBar is not actually changed in this func, except during loading.
// The func that calls this one should apply the bottomBar values if necessary.
func handleURL(t *tab, u string) (string, bool) {
//
// numRedirects is the number of redirects that resulted in the provided URL.
// It should typically be 0.
func handleURL(t *tab, u string, numRedirects int) (string, bool) {
defer App.Draw() // Just in case
// Save for resetting on error
@ -421,7 +424,7 @@ func handleURL(t *tab, u string) (string, bool) {
Error("Input Error", "URL for that input would be too long.")
return ret("", false)
}
return ret(handleURL(t, parsed.String()))
return ret(handleURL(t, parsed.String(), 0))
}
return ret("", false)
case 30:
@ -431,11 +434,22 @@ func handleURL(t *tab, u string) (string, bool) {
return ret("", false)
}
redir := parsed.ResolveReference(parsedMeta).String()
if YesNo("Follow redirect?\n" + redir) {
// Prompt before redirecting to non-Gemini protocol
redirect := false
if !strings.HasPrefix(redir, "gemini") {
if YesNo("Follow redirect to non-Gemini URL?\n" + redir) {
redirect = true
} else {
return ret("", false)
}
}
// Prompt before redirecting
autoRedirect := viper.GetBool("a-general.auto_redirect")
if redirect || (autoRedirect && numRedirects < 5) || YesNo("Follow redirect?\n"+redir) {
if res.Status == gemini.StatusRedirectPermanent {
go cache.AddRedir(u, redir)
}
return ret(handleURL(t, redir))
return ret(handleURL(t, redir, numRedirects+1))
}
return ret("", false)
case 40: