diff options
| author | adnano <[email protected]> | 2020-09-28 02:05:37 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-28 02:05:37 -0400 |
| commit | 70c5d8b9cea707b20a9f5aa1b0888776326d55be (patch) | |
| tree | 6636c23265f8762f438c55753a40d31f44c836fc /server.go | |
| parent | Use WithCertificate helper in auth example (diff) | |
| download | go-gemini-70c5d8b9cea707b20a9f5aa1b0888776326d55be.tar.xz go-gemini-70c5d8b9cea707b20a9f5aa1b0888776326d55be.zip | |
Add WithInput helper functions
Diffstat (limited to 'server.go')
| -rw-r--r-- | server.go | 74 |
1 files changed, 48 insertions, 26 deletions
@@ -213,6 +213,42 @@ func InputHandler(prompt string) Handler { }) } +// WithInput either responds to the request with StatusInput if no input +// is provided, or calls f with the input when provided. +func WithInput(rw *ResponseWriter, req *Request, prompt string, f func(string)) { + input := req.URL.RawQuery + if input == "" { + Input(rw, req, prompt) + return + } + f(input) +} + +// Sensitive responds to the request with a request for sensitive input +// using the given prompt. +func SensitiveInput(rw *ResponseWriter, req *Request, prompt string) { + rw.WriteHeader(StatusSensitiveInput, prompt) +} + +// SensitiveInputHandler returns a simpler handler that responds to each request +// with a request for sensitive input. +func SensitiveInputHandler(prompt string) Handler { + return HandlerFunc(func(rw *ResponseWriter, req *Request) { + SensitiveInput(rw, req, prompt) + }) +} + +// WithSensitiveInput either responds to the request with StatusSensitiveInput +// if no input is provided, or calls f with the input when provided. +func WithSensitiveInput(rw *ResponseWriter, req *Request, prompt string, f func(string)) { + input := req.URL.RawQuery + if input == "" { + SensitiveInput(rw, req, prompt) + return + } + f(input) +} + // Redirect replies to the request with a redirect to the given URL. func Redirect(rw *ResponseWriter, req *Request, url string) { rw.WriteHeader(StatusRedirect, url) @@ -241,32 +277,6 @@ func PermanentRedirectHandler(url string) Handler { }) } -// Sensitive responds to the request with a request for sensitive input -// using the given prompt. -func SensitiveInput(rw *ResponseWriter, req *Request, prompt string) { - rw.WriteHeader(StatusSensitiveInput, prompt) -} - -// SensitiveInputHandler returns a simpler handler that responds to each request -// with a request for sensitive input. -func SensitiveInputHandler(prompt string) Handler { - return HandlerFunc(func(rw *ResponseWriter, req *Request) { - SensitiveInput(rw, req, prompt) - }) -} - -// CertificateRequired responds to the request with the CertificateRequired -// status code. -func CertificateRequired(rw *ResponseWriter, req *Request) { - rw.WriteHeader(StatusCertificateRequired, "Certificate required") -} - -// CertificateNotAuthorized responds to the request with -// the CertificateNotAuthorized status code. -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") @@ -289,6 +299,18 @@ func GoneHandler() Handler { return HandlerFunc(Gone) } +// CertificateRequired responds to the request with the CertificateRequired +// status code. +func CertificateRequired(rw *ResponseWriter, req *Request) { + rw.WriteHeader(StatusCertificateRequired, "Certificate required") +} + +// CertificateNotAuthorized responds to the request with +// the CertificateNotAuthorized status code. +func CertificateNotAuthorized(rw *ResponseWriter, req *Request) { + rw.WriteHeader(StatusCertificateNotAuthorized, "Certificate not authorized") +} + // WithCertificate responds with CertificateRequired if the client did not // provide a certificate, and calls f with the first ceritificate if they did. func WithCertificate(rw *ResponseWriter, req *Request, f func(*x509.Certificate)) { |