Skip to content

Commit

Permalink
server-rendered result page: fix q= escaping
Browse files Browse the repository at this point in the history
fixes #121
  • Loading branch information
stapelberg committed May 19, 2023
1 parent 115d6ad commit 955c194
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
12 changes: 12 additions & 0 deletions cmd/dcs-web/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"html/template"
"io/ioutil"
"log"
"net/url"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -53,6 +54,17 @@ func Init(tlsCertPath, tlsKeyPath, staticPath string) {
func loadTemplates() {
var err error
Templates = template.New("foo").Funcs(template.FuncMap{
"appendToQuery": func(unparsedURL, extra string) string {
u, err := url.Parse(unparsedURL)
if err != nil {
log.Printf("appendToQuery(%q) = %v", unparsedURL, err)
return unparsedURL
}
basequery := u.Query()
basequery.Set("q", basequery.Get("q")+extra)
u.RawQuery = basequery.Encode()
return u.String()
},
"eq": func(args ...interface{}) bool {
if len(args) == 0 {
return false
Expand Down
2 changes: 0 additions & 2 deletions cmd/dcs-web/serverrendered.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func renderPerPackage(w http.ResponseWriter, r *http.Request, queryid string, pa
pagination := updatePagination(page, pages, baseurl.String())

basequery.Del("perpkg")
basequery.Del("q")
// Ensure the filterurl has at least one parameter, so that we can
// always concatenate with the & sign.
basequery.Set("_", "_")
Expand Down Expand Up @@ -331,7 +330,6 @@ func Search(w http.ResponseWriter, r *http.Request) {
perpkgurl := baseurl.String()

basequery.Del("perpkg")
basequery.Del("q")
// Ensure the filterurl has at least one parameter, so that we can
// always concatenate with the & sign.
basequery.Set("_", "_")
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcs-web/templates/perpackage-results.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h2>Search Results by package for "{{.q}}"</h2>
<p>
<strong>Filter by package:</strong>
{{range $index, $package := .packages}}
<a href="{{$.filterurl}}&q={{$.q}}+package:{{$package}}">{{$package}}</a>,
<a href="{{ appendToQuery $.filterurl (printf " package:%s" $package ) }}">{{$package}}</a>,
{{end}}
</p>

Expand Down
2 changes: 1 addition & 1 deletion cmd/dcs-web/templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h2>Search Results for "{{.q}}"</h2>
<p>
<strong>Filter by package:</strong>
{{range $index, $package := .packages}}
<a href="{{$.filterurl}}&q={{$.q}}+package:{{$package}}">{{$package}}</a>,
<a href="{{ appendToQuery $.filterurl (printf " package:%s" $package ) }}">{{$package}}</a>,
{{end}}
</p>

Expand Down

0 comments on commit 955c194

Please sign in to comment.