Use strings.Builder in Fingerprint

This commit is contained in:
Adnan Maolood 2020-10-28 15:13:31 -04:00
parent 7f0b1fa8a1
commit 34ae2a9066
3 changed files with 9 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt"
"io" "io"
"log" "log"
"os" "os"

9
fs.go
View File

@ -5,7 +5,6 @@ import (
"mime" "mime"
"os" "os"
"path" "path"
"path/filepath"
) )
func init() { func init() {
@ -25,14 +24,14 @@ type fsHandler struct {
} }
func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) { func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) {
path := path.Clean(r.URL.Path) p := path.Clean(r.URL.Path)
f, err := fsh.Open(path) f, err := fsh.Open(p)
if err != nil { if err != nil {
w.WriteStatus(StatusNotFound) w.WriteStatus(StatusNotFound)
return return
} }
// Detect mimetype // Detect mimetype
ext := filepath.Ext(path) ext := path.Ext(p)
mimetype := mime.TypeByExtension(ext) mimetype := mime.TypeByExtension(ext)
w.SetMimetype(mimetype) w.SetMimetype(mimetype)
// Copy file to response writer // Copy file to response writer
@ -71,7 +70,7 @@ func ServeFile(w *ResponseWriter, fs FS, name string) {
return return
} }
// Detect mimetype // Detect mimetype
ext := filepath.Ext(name) ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext) mimetype := mime.TypeByExtension(ext)
w.SetMimetype(mimetype) w.SetMimetype(mimetype)
// Copy file to response writer // Copy file to response writer

View File

@ -2,7 +2,6 @@ package gemini
import ( import (
"bufio" "bufio"
"bytes"
"crypto/sha512" "crypto/sha512"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
@ -162,14 +161,14 @@ func appendKnownHost(w io.Writer, hostname string, c certInfo) (int, error) {
// Fingerprint returns the SHA-512 fingerprint of the provided certificate. // Fingerprint returns the SHA-512 fingerprint of the provided certificate.
func Fingerprint(cert *x509.Certificate) string { func Fingerprint(cert *x509.Certificate) string {
sum512 := sha512.Sum512(cert.Raw) sum512 := sha512.Sum512(cert.Raw)
var buf bytes.Buffer var b strings.Builder
for i, f := range sum512 { for i, f := range sum512 {
if i > 0 { if i > 0 {
fmt.Fprintf(&buf, ":") b.WriteByte(':')
} }
fmt.Fprintf(&buf, "%02X", f) fmt.Fprintf(&b, "%02X", f)
} }
return buf.String() return b.String()
} }
// defaultKnownHostsPath returns the default known_hosts path. // defaultKnownHostsPath returns the default known_hosts path.