// It must not contain any bytes that require escaping during path encoding.
func validEncodedPath(s string) bool {
for i := 0; i < len(s); i++ {
- if s[i] != '%' && shouldEscape(s[i], encodePath) {
- return false
+ // RFC 3986, Appendix A.
+ // pchar = unreserved / pct-encoded / sub-delims / ":" / "@".
+ // shouldEscape is not quite compliant with the RFC,
+ // so we check the sub-delims ourselves and let
+ // shouldEscape handle the others.
+ switch s[i] {
+ case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '@':
+ // ok
+ case '[', ']':
+ // ok - not specified in RFC 3986 but left alone by modern browsers
+ case '%':
+ // ok - percent encoded, will decode
+ default:
+ if shouldEscape(s[i], encodePath) {
+ return false
+ }
}
}
return true
},
"",
},
- // worst case host
+ // worst case host, still round trips
{
"scheme://!$&'()*+,;=hello!:port/path",
&URL{
},
"",
},
+ // worst case path, still round trips
+ {
+ "http://host/!$&'()*+,;=:@[hello]",
+ &URL{
+ Scheme: "http",
+ Host: "host",
+ Path: "/!$&'()*+,;=:@[hello]",
+ RawPath: "/!$&'()*+,;=:@[hello]",
+ },
+ "",
+ },
+ // golang.org/issue/5684
+ {
+ "http://example.com/oid/[order_id]",
+ &URL{
+ Scheme: "http",
+ Host: "example.com",
+ Path: "/oid/[order_id]",
+ RawPath: "/oid/[order_id]",
+ },
+ "",
+ },
}
// more useful string for debugging than fmt's struct printer