Split Redirect into two functions

This commit is contained in:
adnano 2020-09-27 22:06:08 -04:00
parent 599c5bb131
commit 554e0af32a
2 changed files with 52 additions and 43 deletions

View File

@ -81,7 +81,7 @@ func login(rw *gmi.ResponseWriter, req *gmi.Request) {
sessions[fingerprint] = &session{
username: username,
}
gmi.Redirect(rw, req, "/login/password", false)
gmi.Redirect(rw, req, "/login/password")
}
} else {
gmi.CertificateRequired(rw, req)
@ -102,7 +102,7 @@ func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) {
expected := logins[session.username].password
if password == expected {
session.authorized = true
gmi.Redirect(rw, req, "/profile", false)
gmi.Redirect(rw, req, "/profile")
} else {
gmi.SensitiveInput(rw, req, "Wrong password. Try again")
}

View File

@ -199,47 +199,6 @@ type Handler interface {
Serve(*ResponseWriter, *Request)
}
// NotFound replies to the request with the NotFound status code.
func NotFound(rw *ResponseWriter, req *Request) {
rw.WriteHeader(StatusNotFound, "Not found")
}
// NotFoundHandler returns a simple handler that responds to each request with
// the status code NotFound.
func NotFoundHandler() Handler {
return HandlerFunc(NotFound)
}
// Gone replies to the request with the Gone status code.
func Gone(rw *ResponseWriter, req *Request) {
rw.WriteHeader(StatusGone, "Gone")
}
// GoneHandler returns a simple handler that responds to each request with
// the status code Gone.
func GoneHandler() Handler {
return HandlerFunc(Gone)
}
// Redirect replies to the request with a redirect to the given url.
// If permanent is true, Redirect will respond with a permanent redirect.
func Redirect(rw *ResponseWriter, req *Request, url string, permanent bool) {
if permanent {
rw.WriteHeader(StatusRedirectPermanent, url)
} else {
rw.WriteHeader(StatusRedirect, url)
}
}
// RedirectHandler returns a simple handler that responds to each request with
// a redirect to the given URL.
// If permanent is true, the handler will respond with a permanent redirect.
func RedirectHandler(url string, permanent bool) Handler {
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
Redirect(rw, req, url, permanent)
})
}
// Input responds to the request with a request for input using the given prompt.
func Input(rw *ResponseWriter, req *Request, prompt string) {
rw.WriteHeader(StatusInput, prompt)
@ -253,6 +212,34 @@ func InputHandler(prompt string) Handler {
})
}
// Redirect replies to the request with a redirect to the given url.
func Redirect(rw *ResponseWriter, req *Request, url string) {
rw.WriteHeader(StatusRedirect, url)
}
// RedirectHandler returns a simple handler that responds to each request with
// a redirect to the given URL.
// If permanent is true, the handler will respond with a permanent redirect.
func RedirectHandler(url string) Handler {
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
Redirect(rw, req, url)
})
}
// PermanentRedirect replies to the request with a permanent redirect to the given URL.
func PermanentRedirect(rw *ResponseWriter, req *Request, url string) {
rw.WriteHeader(StatusRedirectPermanent, url)
}
// PermanentRedirectHandler returns a simple handler that responds to each request with
// a redirect to the given URL.
// If permanent is true, the handler will respond with a permanent redirect.
func PermanentRedirectHandler(url string) Handler {
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
PermanentRedirect(rw, req, url)
})
}
// Sensitive responds to the request with a request for sensitive input
// using the given prompt.
func SensitiveInput(rw *ResponseWriter, req *Request, prompt string) {
@ -279,6 +266,28 @@ func CertificateNotAuthorized(rw *ResponseWriter, req *Request) {
rw.WriteHeader(StatusCertificateNotAuthorized, "Certificate not authorized")
}
// NotFound replies to the request with the NotFound status code.
func NotFound(rw *ResponseWriter, req *Request) {
rw.WriteHeader(StatusNotFound, "Not found")
}
// NotFoundHandler returns a simple handler that responds to each request with
// the status code NotFound.
func NotFoundHandler() Handler {
return HandlerFunc(NotFound)
}
// Gone replies to the request with the Gone status code.
func Gone(rw *ResponseWriter, req *Request) {
rw.WriteHeader(StatusGone, "Gone")
}
// GoneHandler returns a simple handler that responds to each request with
// the status code Gone.
func GoneHandler() Handler {
return HandlerFunc(Gone)
}
// ServeMux is a Gemini request multiplexer.
// It matches the URL of each incoming request against a list of registered
// patterns and calls the handler for the pattern that most closesly matches