From 8cbe984ba420a696df363104ba29737a17c2d8f4 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Sun, 15 Dec 2019 17:06:48 +0530 Subject: [PATCH] Default {{.Host}} to https:// --- views.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/views.go b/views.go index be7f9b5..b18072a 100644 --- a/views.go +++ b/views.go @@ -20,6 +20,8 @@ import ( "github.com/pkg/errors" ) +const defaultScheme = "https" + // Plain text templates var textBaseTemplate *text.Template = text.Must(text.New("").Parse(string(MustAsset("templates/txt/base.txt.tmpl")))) var htmlBaseTemplate *html.Template = html.Must(html.New("").Parse(string(MustAsset("templates/html/base.html.tmpl")))) @@ -83,14 +85,6 @@ func mapExtend(m map[string]interface{}, key string, value interface{}) { m[key] = value } -func (rl *rushlink) resolveHost(r *http.Request) string { - rlHost := rl.Host() - if rlHost != nil { - return rlHost.String() - } - return r.Host -} - func (rl *rushlink) render(w http.ResponseWriter, r *http.Request, tmplName string, data map[string]interface{}) { contentType, err := resolveResponseContentType(r, []string{"text/plain", "text/html"}) if err != nil { @@ -157,6 +151,21 @@ func (rl *rushlink) renderInternalServerError(w http.ResponseWriter, r *http.Req rl.renderError(w, r, http.StatusInternalServerError, msg) } +// resolveHost constructs the `scheme://host` part of rushlinks public API. +// +// If the `--host` flag is set, it will return that URL. +// Otherwise, this function will return 'https://{Host}', where `{Host}` is +// the value provided by the client in the HTTP `Host` header. This value may +// be invalid, but it is impossible to handle this error (because we *cannot* +// know the real host). +func (rl *rushlink) resolveHost(r *http.Request) string { + rlHost := rl.Host() + if rlHost != nil { + return rlHost.String() + } + return defaultScheme + r.Host +} + // Try to resolve the preferred content-type for the response to this request. // // This is done by reading from the `types` argument. If one of them matches