func pageBadgePage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") id := strings.TrimSpace(r.FormValue("id")) if id != "" { doc, found := getDatabase().FindFullPackage(id) if !found { http.Error(w, fmt.Sprintf("Package %s not found!", id), http.StatusNotFound) return } badgeUrl := "http://go-search.org/badge?id=" + template.URLQueryEscaper(doc.Package) viewUrl := "http://go-search.org/view?id=" + template.URLQueryEscaper(doc.Package) htmlCode := fmt.Sprintf(`<a href="%s"><img src="%s" alt="GoSearch"></a>`, viewUrl, badgeUrl) mdCode := fmt.Sprintf(`[![GoSearch](%s)](%s)`, badgeUrl, viewUrl) if err := templates.ExecuteTemplate(w, "badgepage.html", struct { UIUtils gcse.HitInfo HTMLCode string MDCode string }{ HitInfo: doc, HTMLCode: htmlCode, MDCode: mdCode, }); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }
func Example_escape() { const s = `"Fran & Freddie's Diner" <*****@*****.**>` v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<*****@*****.**>`} fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.HTMLEscaper(v...)) fmt.Println(template.JSEscapeString(s)) template.JSEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.JSEscaper(v...)) fmt.Println(template.URLQueryEscaper(v...)) // Output: // "Fran & Freddie's Diner" <[email protected]> // "Fran & Freddie's Diner" <[email protected]> // "Fran & Freddie's Diner"32<[email protected]> // \"Fran & Freddie\'s Diner\" \[email protected]\x3E // \"Fran & Freddie\'s Diner\" \[email protected]\x3E // \"Fran & Freddie\'s Diner\"32\[email protected]\x3E // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E }
func pageUpdate(w http.ResponseWriter, r *http.Request) { id := strings.TrimSpace(r.FormValue("id")) if id != "" { c := appengine.NewContext(r) updateDocInfo(c, id) http.Redirect(w, r, "view?id="+template.URLQueryEscaper(id), 302) } }
func TestHTMLEscape(t *testing.T) { const s = `"Fran & Freddie's Diner" <*****@*****.**>` v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<*****@*****.**>`} fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprint(os.Stdout, "") fmt.Println(template.JSEscapeString(s)) fmt.Println(template.JSEscaper(v...)) fmt.Println(template.URLQueryEscaper(v...)) }
func pageView(w http.ResponseWriter, r *http.Request) { id := strings.TrimSpace(r.FormValue("id")) if id != "" { c := appengine.NewContext(r) ddb := NewCachedDocDB(c, "doc") var doc DocInfo err, exists := ddb.Get(id, &doc) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if !exists { fmt.Fprintf(w, `<html><body>No such entry!`) ent, _ := findCrawlingEntry(c, kindCrawlerPackage, id) if ent != nil { fmt.Fprintf(w, ` Scheduled to be crawled at %s`, ent.ScheduleTime.Format("2006-01-02 15:04:05")) } else { fmt.Fprintf(w, ` Not found yet!`) } fmt.Fprintf(w, ` Click to <a href="crawl?id=%s">crawl</a>.</body></html>`, template.URLQueryEscaper(id)) return } if doc.StarCount < 0 { doc.StarCount = 0 } var descHTML villa.ByteSlice godoc.ToHTML(&descHTML, doc.Description, nil) showReadme := len(doc.Description) < 10 && len(doc.ReadmeData) > 0 err = templates.ExecuteTemplate(w, "view.html", struct { DocInfo DescHTML template.HTML ShowReadme bool }{ DocInfo: doc, DescHTML: template.HTML(descHTML), ShowReadme: showReadme, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }
func writeHTML(ctx data.Entry, dirname string) error { filename := strings.Replace(template.URLQueryEscaper(ctx.Translations[0].Language+"_"+ctx.Translations[0].Words[0].Written+".html"), "%", "X", -1) file, err := os.Create(filename) defer file.Close() if err != nil { return err } showme := struct { Title string Body template.HTML }{ ctx.Translations[0].Words[0].Written + ": " + ctx.Wordtype, template.HTML(""), } for _, translation := range ctx.Translations { showme.Body += template.HTML("<div class=\"word\"><div id=\"" + translation.Language + "\" class=\"wordheader\">" + translation.Language + "</div>\n<div class=\"definitions\">") for _, word := range translation.Words { showme.Body += template.HTML("<div class=\"definition\">" + template.HTMLEscapeString(word.Written)) for _, sw := range word.Sources { showme.Body += template.HTML("<span class=\"source\">" + template.HTMLEscapeString(sw) + "</span>") } showme.Body += template.HTML("</div>") references = append(references, [3]string{translation.Language, word.Written, filename}) found := false firstletter, _ := utf8.DecodeRuneInString(strings.ToLower(word.Written)) for _, letter := range alphabet { if firstletter == letter { found = true } } if !found { alphabet = append(alphabet, firstletter) } } showme.Body += template.HTML("</div></div>") } err = tmpl.Execute(file, showme) if err != nil { return err } return nil }
/* http://xxx.xxx/?xxx=$text */ func Query(text interface{}) string { return template.URLQueryEscaper(fmt.Sprint(text)) }
func LoginUrlFor(endpoint string, values ...string) string { return beego.UrlFor("AuthController.Login", ":returnurl", template.URLQueryEscaper(beego.UrlFor(endpoint, values...))) }
func (a *Api) WUI(c *gin.Context) { smss, errcode := a.getSms(c) if errcode > 0 { if errcode == 404 { c.HTML(404, "index.tmpl", gin.H{ "NotFound": true, }) return } else { c.AbortWithStatus(errcode) } } var err error var start, limit int64 var sstart, slimit, q string q = c.Query("q") smscount := a.countSms(q) sstart = c.Query("start") slimit = c.Query("limit") if sstart == "" { start = 0 } else { start, err = strconv.ParseInt(sstart, 10, 64) if err != nil { stringWebError(c, 400, "start must be an integer") return } } if slimit == "" { limit = 50 } else { limit, err = strconv.ParseInt(slimit, 10, 64) if err != nil { stringWebError(c, 400, "limit must be an integer") return } } morePrev := start > 0 moreNext := (start + limit) < int64(smscount) if q != "" { q = fmt.Sprintf("&q=%s", template.URLQueryEscaper(q)) } log.Debugf("start: %v, limit %v, moreNext: %v, morePrev: %v, smsCount: %d", start, limit, moreNext, morePrev, smscount) for i := range smss { msg := smss[i].Message msg = template.HTMLEscapeString(msg) msg = strings.Replace(msg, "\n", "<br>", -1) smss[i].MessageHTML = template.HTML(msg) } c.HTML(200, "index.tmpl", gin.H{ "SMS": smss, "Count": smscount, "MorePrev": morePrev, "MoreNext": moreNext, "Limit": limit, "StartNext": start + limit, "StartPrev": start - limit, "Query": template.URL(q), }) return }
// EscapeURL escapes URLs using QueryEscapeString func EscapeURL(s string) string { return got.URLQueryEscaper(s) }
"ReviewGameCount": func(gameid string) (string, error) { count, err := Dal.GetReviewsGameCount(gameid) if err != nil { return "0", err } return strconv.Itoa(count), nil }, "VideoGameCount": func(gameid string) (string, error) { count, err := Dal.GetGameVideosCount(gameid) if err != nil { return "0", err } return strconv.Itoa(count), nil }, "URLQueryEscaper": func(s interface{}) (string, error) { return template.URLQueryEscaper(s), nil }, "JSONify": func(s interface{}) (string, error) { js, err := json.Marshal(s) return string(js), err }, "HTMLnewlines": func(s string) (template.HTML, error) { buf := bytes.NewBuffer(nil) lastind := 0 for ind := StringIndexOf(s, "\n", lastind); ind >= 0; ind = StringIndexOf(s, "\n", ind+1) { fmt.Fprintln(buf, template.HTMLEscapeString(s[lastind:ind])+"<br/>") lastind = ind } return template.HTML(buf.String()), nil }, }
/* ============================================================================================ */ func UrlEncode(str string) string { return template.URLQueryEscaper(str) }
// ScriptLink returns an html tag for a given file path func ScriptLink(name string) template.HTML { if !strings.HasSuffix(name, ".js") { name = name + ".js" } return template.HTML(fmt.Sprintf(scriptTemplate, template.URLQueryEscaper(name))) }
// StyleLink returns an html tag for a given file path func StyleLink(name string) template.HTML { if !strings.HasSuffix(name, ".css") { name = name + ".css" } return template.HTML(fmt.Sprintf(styleTemplate, template.URLQueryEscaper(name))) }