为什么我的 URL.Path 语句没有被击中
Why aren't my URL.Path statements getting hit
下面是我用来展示我的 html 模板的函数。我最近开始在我的博客页面上工作,由于某种原因,我的第一个和第二个 "else if" 语句没有被点击。 :
func handleRequest(w http.ResponseWriter, r *http.Request) {
templates := populateTemplates()
// present home.html if the request url is "/"
if r.URL.Path == "/" {
t := templates.Lookup("home.html")
t.Execute(w, nil)
} else if r.URL.Path == "blog/" {
posts := getPosts()
t := template.New("blog.html")
t, _ = t.ParseFiles("blog.html")
t.Execute(w, posts)
return
} else if r.URL.Path == "blog/*" {
f := "blog/" + r.URL.Path[1:] + ".md"
fileread, _ := ioutil.ReadFile(f)
lines := strings.Split(string(fileread), "\n")
status := string(lines[0])
title := string(lines[1])
date := string(lines[2])
summary := string(lines[3])
body := strings.Join(lines[4:len(lines)], "\n")
htmlBody := template.HTML(blackfriday.MarkdownCommon([]byte(body)))
post := Post{status, title, date, summary, htmlBody, r.URL.Path[1:]}
t := template.New("_post.html")
t, _ = t.ParseFiles("_post.html")
t.Execute(w, post)
} else {
requestedFile := r.URL.Path[1:]
t := templates.Lookup(requestedFile + ".html")
if t != nil {
err := t.Execute(w, nil)
if err != nil {
log.Println(err)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}
假设问题出在博客网址上,您可以试试这个:
if r.URL.Path == "/" {
...
} else if r.URL.Path == "/blog" {
...
} else if strings.HasPrefix(r.URL.Path,"/blog/") {
....
} else {
我希望 templates.Lookup 不进行任何文件系统调用,大概它只是在地图中查找它们。
此外,不要这样做:
f := "blog/" + r.URL.Path[1:] + ".md"
fileread, _ := ioutil.ReadFile(f)
如果 r.URL.Path[1:] 是“../mysecretfile”并且您在那里有匹配的文件,会发生什么情况?使用外部字符串而不用 path.Clean 或类似的方法清理它们不是一个好主意。
下面是我用来展示我的 html 模板的函数。我最近开始在我的博客页面上工作,由于某种原因,我的第一个和第二个 "else if" 语句没有被点击。 :
func handleRequest(w http.ResponseWriter, r *http.Request) {
templates := populateTemplates()
// present home.html if the request url is "/"
if r.URL.Path == "/" {
t := templates.Lookup("home.html")
t.Execute(w, nil)
} else if r.URL.Path == "blog/" {
posts := getPosts()
t := template.New("blog.html")
t, _ = t.ParseFiles("blog.html")
t.Execute(w, posts)
return
} else if r.URL.Path == "blog/*" {
f := "blog/" + r.URL.Path[1:] + ".md"
fileread, _ := ioutil.ReadFile(f)
lines := strings.Split(string(fileread), "\n")
status := string(lines[0])
title := string(lines[1])
date := string(lines[2])
summary := string(lines[3])
body := strings.Join(lines[4:len(lines)], "\n")
htmlBody := template.HTML(blackfriday.MarkdownCommon([]byte(body)))
post := Post{status, title, date, summary, htmlBody, r.URL.Path[1:]}
t := template.New("_post.html")
t, _ = t.ParseFiles("_post.html")
t.Execute(w, post)
} else {
requestedFile := r.URL.Path[1:]
t := templates.Lookup(requestedFile + ".html")
if t != nil {
err := t.Execute(w, nil)
if err != nil {
log.Println(err)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}
假设问题出在博客网址上,您可以试试这个:
if r.URL.Path == "/" {
...
} else if r.URL.Path == "/blog" {
...
} else if strings.HasPrefix(r.URL.Path,"/blog/") {
....
} else {
我希望 templates.Lookup 不进行任何文件系统调用,大概它只是在地图中查找它们。
此外,不要这样做:
f := "blog/" + r.URL.Path[1:] + ".md"
fileread, _ := ioutil.ReadFile(f)
如果 r.URL.Path[1:] 是“../mysecretfile”并且您在那里有匹配的文件,会发生什么情况?使用外部字符串而不用 path.Clean 或类似的方法清理它们不是一个好主意。