diff options
| -rw-r--r-- | cnhttp.go | 54 | 
1 files changed, 34 insertions, 20 deletions
| @@ -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 { |