Example #1
0
func (ch CertificateHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if req.URL.Path == "/cert/" || req.URL.Path == "/cert/index.html" {
		w.Header().Set("Content-Type", "text/html")
		var s []string
		for k, _ := range ch.Cert {
			s = append(s, k)
		}
		t, err := template.New("show").Parse(CertListTemplate)
		options.FailIf(err, "can't parse template")
		err = t.Execute(w, s)
		options.FailIf(err, "can't execute template")
		return
	}
	var name string
	_, err := fmt.Sscanf(req.URL.Path, "/cert/%s", &name)
	if err != nil {
		http.NotFound(w, req)
		return
	}
	fmt.Printf("request for: %s\n", name)
	form := ""
	for _, s := range []string{"der", "pem", "txt", "html"} {
		if strings.HasSuffix(name, "."+s) {
			name = name[0 : len(name)-len(s)-1]
			form = s
			break
		}
	}
	cert := ch.Cert[name]
	if cert == nil {
		http.NotFound(w, req)
		return
	}
	chain := ch.CertChain(name)
	switch form {
	case "der":
		if cert.IsCA {
			w.Header().Set("Content-Type", "application/x-x509-ca-cert")
		} else {
			w.Header().Set("Content-Type", "application/x-x509-user-cert")
		}
		w.Write(cert.Raw)
	case "pem", "crt":
		w.Header().Set("Content-Type", "application/x-pem-file")
		for _, parent := range chain {
			s := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: parent.Raw})
			w.Write(s)
		}
	case "txt":
		w.Header().Set("Content-Type", "text/plain")
		out := indent.NewTextWriter(w, 2)
		for _, cert := range chain {
			x509txt.Dump(out, cert)
		}
	case "html":
		w.Header().Set("Content-Type", "text/html")
		s := ""
		for _, cert := range chain {
			s += x509txt.Html(cert)
		}
		t, err := template.New("show").Parse(CertTemplate)
		options.FailIf(err, "can't parse template")
		err = t.Execute(w, template.HTML(s))
		options.FailIf(err, "can't execute template")
	default:
		http.NotFound(w, req)
	}
}
Example #2
0
func String(cert *x509.Certificate) string {
	var b bytes.Buffer
	Dump(indent.NewTextWriter(&b, 80), cert)
	return b.String()
}