Add WithCertificate helper function

This commit is contained in:
adnano 2020-09-28 01:10:36 -04:00
parent f093bf567f
commit f09b859370

View File

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