mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-09-26 21:40:57 +02:00
Add WithCertificate helper function
This commit is contained in:
parent
f093bf567f
commit
f09b859370
21
server.go
21
server.go
@ -3,6 +3,7 @@ package gmi
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
@ -288,6 +289,26 @@ func GoneHandler() Handler {
|
||||
return HandlerFunc(Gone)
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if len(req.TLS.PeerCertificates) == 0 {
|
||||
CertificateRequired(rw, req)
|
||||
return
|
||||
}
|
||||
cert := req.TLS.PeerCertificates[0]
|
||||
f(cert)
|
||||
}
|
||||
|
||||
// CertificateHandler returns a simple handler that requests a certificate from
|
||||
// clients if they did not provide one, and calls f with the first certificate
|
||||
// if they did.
|
||||
func CertificateHandler(f func(*x509.Certificate)) Handler {
|
||||
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
|
||||
WithCertificate(rw, req, f)
|
||||
})
|
||||
}
|
||||
|
||||
// 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
|
||||
|
Loading…
Reference in New Issue
Block a user