// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package main import ( "bytes" "fmt" "html/template" "log" "net/http" "regexp" "strings" "github.com/davecgh/go-spew/spew" "github.com/stapelberg/qrbill" ) var ( fieldNameRe = regexp.MustCompile(`
( )*([^:]+):`) stringLiteralRe = regexp.MustCompile(`"([^"]*)"`) parenRe = regexp.MustCompile(`\(([^)]+)\) `) ) var tmpl = template.Must(template.New("").Parse(` QR Bill HTML Debug Page

URL parameters

Parameter Value
&criban= {{ .Criban }}
&crname= {{ .Crname }}
&craddr1= {{ .Craddr1 }}
&craddr2= {{ .Craddr2 }}
&crpost= {{ .Crpost }}
&crcity= {{ .Crcity }}
&crcountry= {{ .Crcountry }}
&udname= {{ .Udname }}
&udaddr1= {{ .Udaddr1 }}
&udaddr2= {{ .Udaddr2 }}
&udpost= {{ .Udpost }}
&udcity= {{ .Udcity }}
&udcountry= {{ .Udcountry }}
&message= {{ .Message }}
`)) func debugHTML(w http.ResponseWriter, r *http.Request, prefix string, qrch *qrbill.QRCH) { w.Header().Add("Content-Type", "text/html; charset=utf-8") var buf bytes.Buffer err := tmpl.Execute(&buf, struct { Criban string Crname string Craddr1 string Craddr2 string Crpost string Crcity string Crcountry string Udname string Udaddr1 string Udaddr2 string Udpost string Udcity string Udcountry string Message string }{ Criban: r.FormValue("criban"), Crname: r.FormValue("crname"), Craddr1: r.FormValue("craddr1"), Craddr2: r.FormValue("craddr2"), Crpost: r.FormValue("crpost"), Crcity: r.FormValue("crcity"), Crcountry: r.FormValue("crcountry"), Udname: r.FormValue("udname"), Udaddr1: r.FormValue("udaddr1"), Udaddr2: r.FormValue("udaddr2"), Udpost: r.FormValue("udpost"), Udcity: r.FormValue("udcity"), Udcountry: r.FormValue("udcountry"), Message: r.FormValue("message"), }) if err != nil { log.Printf("%s %s", prefix, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "%s", buf.String()) spew := func(vars ...interface{}) string { sp := spew.Sdump(vars...) sp = strings.ReplaceAll(sp, "\n", "
") sp = strings.ReplaceAll(sp, " ", " ") sp = parenRe.ReplaceAllString(sp, "") sp = stringLiteralRe.ReplaceAllStringFunc(sp, func(stringLiteral string) string { return `` + stringLiteral + "" }) sp = fieldNameRe.ReplaceAllStringFunc(sp, func(fieldName string) string { return `` + fieldName + "" }) return sp } fmt.Fprintf(w, `

input

%s
`, spew(qrch)) fmt.Fprintf(w, `

validated

%s
`, spew(qrch.Validate())) r.URL.Path = "/qr" v := r.URL.Query() v.Set("format", "png") r.URL.RawQuery = v.Encode() fmt.Fprintf(w, `

QR Bill

`, r.URL.String()) }