package main
import (
"log"
"net/http"
)
func main() {
// invalid method called "bad"
req, err := http.NewRequest("bad", "https://www.google.com", nil)
if err != nil {
log.Printf("E! got err: %v", err)
} else {
log.Printf("I! request method: %s", req.Method)
}
}
https://play.golang.org/p/NM8_4pkN5uM
错误在这里没有,有人可以解释一下吗?
谢谢!
答案 0 :(得分:6)
错误 不被视为错误的http方法。
任何非零长度的字符串都包含来自的字符!#$%& * + - 。0123456789ABCDEFGHIJKLMNOPQRSTUWVXYZ ^ _abcdefghijklmnopqrstuvwxyz |〜被视为有效
以下是用于验证方法的函数
func validMethod(method string) bool {
/*
Method = "OPTIONS" ; Section 9.2
| "GET" ; Section 9.3
| "HEAD" ; Section 9.4
| "POST" ; Section 9.5
| "PUT" ; Section 9.6
| "DELETE" ; Section 9.7
| "TRACE" ; Section 9.8
| "CONNECT" ; Section 9.9
| extension-method
extension-method = token
token = 1*<any CHAR except CTLs or separators>
*/
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
}