mirror of
https://code.rocketnine.space/tslocum/gmitohtml.git
synced 2024-11-27 16:38:14 +01:00
Escape page content
This commit is contained in:
parent
e2232a8dc8
commit
a8700abe29
2 changed files with 14 additions and 8 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -20,6 +21,11 @@ var assetLock sync.Mutex
|
||||||
|
|
||||||
func rewriteURL(u string, loc *url.URL) string {
|
func rewriteURL(u string, loc *url.URL) string {
|
||||||
if daemonAddress != "" {
|
if daemonAddress != "" {
|
||||||
|
scheme := "gemini"
|
||||||
|
if strings.HasPrefix(loc.Path, "/file/") {
|
||||||
|
scheme = "file"
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(u, "gemini://") {
|
if strings.HasPrefix(u, "gemini://") {
|
||||||
return "http://" + daemonAddress + "/gemini/" + u[9:]
|
return "http://" + daemonAddress + "/gemini/" + u[9:]
|
||||||
} else if strings.HasPrefix(u, "file://") {
|
} else if strings.HasPrefix(u, "file://") {
|
||||||
|
@ -34,9 +40,9 @@ func rewriteURL(u string, loc *url.URL) string {
|
||||||
if u[0] != '/' {
|
if u[0] != '/' {
|
||||||
newPath = path.Join(loc.Path, u)
|
newPath = path.Join(loc.Path, u)
|
||||||
}
|
}
|
||||||
return "http://" + daemonAddress + "/gemini/" + loc.Host + newPath
|
return "http://" + daemonAddress + "/" + scheme + "/" + loc.Host + newPath
|
||||||
}
|
}
|
||||||
return "http://" + daemonAddress + "/gemini/" + u
|
return "http://" + daemonAddress + "/" + scheme + "/" + u
|
||||||
}
|
}
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
@ -68,7 +74,7 @@ func Convert(page []byte, u string) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
if preformatted {
|
if preformatted {
|
||||||
result = append(result, line...)
|
result = append(result, html.EscapeString(string(line))...)
|
||||||
result = append(result, []byte("\n")...)
|
result = append(result, []byte("\n")...)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -89,9 +95,9 @@ func Convert(page []byte, u string) []byte {
|
||||||
linkURL = split[0]
|
linkURL = split[0]
|
||||||
linkLabel = split[1]
|
linkLabel = split[1]
|
||||||
}
|
}
|
||||||
link := append([]byte(`<a href="`), rewriteURL(string(linkURL), parsedURL)...)
|
link := append([]byte(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
|
||||||
link = append(link, []byte(`">`)...)
|
link = append(link, []byte(`">`)...)
|
||||||
link = append(link, linkLabel...)
|
link = append(link, html.EscapeString(string(linkLabel))...)
|
||||||
link = append(link, []byte(`</a>`)...)
|
link = append(link, []byte(`</a>`)...)
|
||||||
result = append(result, link...)
|
result = append(result, link...)
|
||||||
result = append(result, []byte("<br>")...)
|
result = append(result, []byte("<br>")...)
|
||||||
|
@ -107,11 +113,11 @@ func Convert(page []byte, u string) []byte {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if heading > 0 {
|
if heading > 0 {
|
||||||
result = append(result, []byte(fmt.Sprintf("<h%d>%s</h%d>", heading, line[heading:], heading))...)
|
result = append(result, []byte(fmt.Sprintf("<h%d>%s</h%d>", heading, html.EscapeString(string(line[heading:])), heading))...)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
result = append(result, line...)
|
result = append(result, html.EscapeString(string(line))...)
|
||||||
result = append(result, []byte("<br>")...)
|
result = append(result, []byte("<br>")...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -117,7 +117,7 @@ func fetch(u string) ([]byte, []byte, error) {
|
||||||
errorPage := []byte(pageHeader)
|
errorPage := []byte(pageHeader)
|
||||||
errorPage = append(errorPage, []byte(fmt.Sprintf("Server sent unexpected header:<br><br><b>%s</b>", header))...)
|
errorPage = append(errorPage, []byte(fmt.Sprintf("Server sent unexpected header:<br><br><b>%s</b>", header))...)
|
||||||
errorPage = append(errorPage, []byte(pageFooter)...)
|
errorPage = append(errorPage, []byte(pageFooter)...)
|
||||||
return header, errorPage, nil
|
return header, fillTemplateVariables(errorPage, u, false), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.HasPrefix(header, []byte("20 text/html")) {
|
if bytes.HasPrefix(header, []byte("20 text/html")) {
|
||||||
|
|
Loading…
Reference in a new issue