mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-09-20 03:11:34 +02:00
Add WithCertificate helper function
This commit is contained in:
parent
813a1b8957
commit
49f0063186
21
server.go
21
server.go
@ -3,6 +3,7 @@ package gmi
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -288,6 +289,26 @@ func GoneHandler() Handler {
|
|||||||
return HandlerFunc(Gone)
|
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.
|
// ServeMux is a Gemini request multiplexer.
|
||||||
// It matches the URL of each incoming request against a list of registered
|
// 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
|
// patterns and calls the handler for the pattern that most closesly matches
|
||||||
|
Loading…
Reference in New Issue
Block a user