Default {{.Host}} to https://

This commit is contained in:
Daan Sprenkels 2019-12-15 17:06:48 +05:30
parent 0bffde1dc1
commit 8cbe984ba4
1 changed files with 17 additions and 8 deletions

View File

@ -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