avoid auto escape in html template

╄→гoц情女王★ 提交于 2019-12-02 03:18:57

Using a value of template.URL is perfectly enough.

What you see is not HTML encoding of the given URL, what you see is the URL encoding of the opening and closing parenthesis in the (data) part. That is perfectly ok. %28 is the encoding of '(' (28 is the hexa code for the opening parenthesis character), and %29 is the ')' character.

The url http://myurl.com/(data)/aaa.jpg and http://myurl.com/%28data%29/aaa.jpg are one and the same.

The purpose of the URL encoding is so that URLs and values included in URLs (e.g. form parameters) can be safely transmitted.

URL Encoding

URLs can only be sent over the Internet using the ASCII character-set.

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

You can read more about URL encoding here: HTML URL Encoding Reference.

Testing it:

func rh(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, `<html><body>
<img src="http://localhost:8080/(data)/aaa.jpg">
<img src="http://localhost:8080/%28data%29/aaa.jpg">
</body></htm>`)
}

func imgh(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "aaa.jpg")
}

func main() {
    http.HandleFunc("/", rh)
    http.HandleFunc("/(data)/aaa.jpg", imgh)
    panic(http.ListenAndServe("localhost:8080", nil))
}

Now direct your browser to http://localhost:8080. It will display 2 images.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!