aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradnano <[email protected]>2020-09-27 22:06:08 -0400
committeradnano <[email protected]>2020-09-27 22:06:08 -0400
commit554e0af32a9fa70a68fe887ade194cbae5643d60 (patch)
treead3b3939e8531878e49bef473e529077ff4b9a69
parentUse helper functions throughout (diff)
downloadgo-gemini-554e0af32a9fa70a68fe887ade194cbae5643d60.tar.xz
go-gemini-554e0af32a9fa70a68fe887ade194cbae5643d60.zip
Split Redirect into two functions
-rw-r--r--examples/auth/auth.go4
-rw-r--r--server.go77
2 files changed, 45 insertions, 36 deletions
diff --git a/examples/auth/auth.go b/examples/auth/auth.go
index 519c3e1..2ea5b78 100644
--- a/examples/auth/auth.go
+++ b/examples/auth/auth.go
@@ -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")
}
diff --git a/server.go b/server.go
index 2531ad0..b6fc341 100644
--- a/server.go
+++ b/server.go
@@ -199,57 +199,44 @@ 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")
+// 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)
}
-// GoneHandler returns a simple handler that responds to each request with
-// the status code Gone.
-func GoneHandler() Handler {
- return HandlerFunc(Gone)
+// InputHandler returns a simple handler that responds to each request with
+// a request for input.
+func InputHandler(prompt string) Handler {
+ return HandlerFunc(func(rw *ResponseWriter, req *Request) {
+ Input(rw, req, prompt)
+ })
}
// 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)
- }
+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, permanent bool) Handler {
+func RedirectHandler(url string) Handler {
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
- Redirect(rw, req, url, permanent)
+ Redirect(rw, req, url)
})
}
-// 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)
+// 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)
}
-// InputHandler returns a simple handler that responds to each request with
-// a request for input.
-func InputHandler(prompt string) Handler {
+// 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) {
- Input(rw, req, prompt)
+ PermanentRedirect(rw, req, url)
})
}
@@ -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