summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-24 12:36:37 +0200
committerclsr <clsr@clsr.net>2017-08-24 12:36:37 +0200
commit28398c213bc6a798108421f79659e7d97ca9dbc2 (patch)
tree28739971acb8caa1986c5ffc21a9d8c90ff5d440
parent282efe276ffbc2dbf5f6b7230d85667edc606cb5 (diff)
downloadcn-http-28398c213bc6a798108421f79659e7d97ca9dbc2.tar.gz
cn-http-28398c213bc6a798108421f79659e7d97ca9dbc2.zip
Remove race condition with template funcsv0.1.3
-rw-r--r--cnhttp.go54
1 files changed, 34 insertions, 20 deletions
diff --git a/cnhttp.go b/cnhttp.go
index 3e3f431..8305de3 100644
--- a/cnhttp.go
+++ b/cnhttp.go
@@ -69,7 +69,7 @@ func escapeURL(urlStr string) string {
urlStr = anchorHTTPSchema + urlStr[len(anchorCNPSchema):]
}
var buf bytes.Buffer
- anchorTemplate.Execute(&buf, urlStr)
+ _ = anchorTemplate.Execute(&buf, urlStr)
urlStr = buf.String()
urlStr = urlStr[len(anchorPrefix) : len(urlStr)-len(anchorSuffix)]
if isCNP {
@@ -163,7 +163,10 @@ func (srv *server) handleCNP(w http.ResponseWriter, r *http.Request, path string
return
}
- r.ParseForm()
+ err = r.ParseForm()
+ if err != nil {
+ srv.handleError(w, err)
+ }
_, preq := r.Form["req"]
_, phdr := r.Form["hdr"]
_, presp := r.Form["resp"]
@@ -202,7 +205,7 @@ func (srv *server) cnpToWeb(w http.ResponseWriter, r *http.Request, req *cnp.Req
if resp.Type() == "text/cnm" {
w.Header().Set("Content-Type", "text/plain")
}
- io.Copy(w, resp.Body)
+ _, _ = io.Copy(w, resp.Body)
}
case cnp.IntentNotModified:
w.WriteHeader(http.StatusNotModified)
@@ -426,6 +429,33 @@ func (srv *server) cnpCNMToHTML(w http.ResponseWriter, r *http.Request, req *cnp
if host == "" {
host = req.Host()
}
+
+ tmpl, err := templates.Clone()
+ if err != nil {
+ srv.handleError(w, err)
+ return
+ }
+
+ err = srv.addTemplateFuncs(tmpl, req, resp).ExecuteTemplate(&buf, "page.html", cnmPage{
+ URL: u.String(),
+ Req: breq.String(),
+ Resp: bresp.String(),
+ Doc: doc,
+ Netcat: shellquote.Join("echo", hdrs) + " | " + shellquote.Join("nc", host, port),
+ Site: st,
+ Toc: tocSection{Children: genToc(doc.Content)},
+ Highlight: srv.highlighting,
+ Browser: srv.host == "",
+ })
+ if err != nil {
+ srv.handleError(w, err)
+ return
+ }
+
+ _, _ = io.Copy(w, &buf)
+}
+
+func (srv *server) addTemplateFuncs(tmpl *template.Template, req *cnp.Request, resp *cnp.Response) *template.Template {
sections := []string{}
sanchors := map[string]bool{}
lanchors := map[string]bool{}
@@ -438,7 +468,7 @@ func (srv *server) cnpCNMToHTML(w http.ResponseWriter, r *http.Request, req *cnp
}
return template.URL(strings.Join(secs, "/"))
}
- err = templates.Funcs(map[string]interface{}{
+ return tmpl.Funcs(map[string]interface{}{
"inc": func(s string) string { sections = append(sections, s); return "" },
"dec": func() string { sections = sections[:len(sections)-1]; return "" },
"depth": func() int {
@@ -472,23 +502,7 @@ func (srv *server) cnpCNMToHTML(w http.ResponseWriter, r *http.Request, req *cnp
u, _ := srv.linkToURL(req, s)
return u
},
- }).ExecuteTemplate(&buf, "page.html", cnmPage{
- URL: u.String(),
- Req: breq.String(),
- Resp: bresp.String(),
- Doc: doc,
- Netcat: shellquote.Join("echo", hdrs) + " | " + shellquote.Join("nc", host, port),
- Site: st,
- Toc: tocSection{Children: genToc(doc.Content)},
- Highlight: srv.highlighting,
- Browser: srv.host == "",
})
- if err != nil {
- srv.handleError(w, err)
- return
- }
-
- io.Copy(w, &buf)
}
func (srv *server) fmtToHTML(req *cnp.Request, text cnmfmt.Text) template.HTML {