1
0
mirror of https://github.com/golang/go synced 2024-11-25 07:57:56 -07:00

http: don't always escape all reserved chars (fix build)

R=nigeltao, nigeltao_golang
CC=golang-dev
https://golang.org/cl/2206044
This commit is contained in:
Andrew Gerrand 2010-09-22 16:59:35 +10:00
parent d093bdbe7e
commit ad9042bdfa
2 changed files with 13 additions and 10 deletions

View File

@ -191,7 +191,7 @@ func (req *Request) Write(w io.Writer) os.Error {
uri := req.RawURL uri := req.RawURL
if uri == "" { if uri == "" {
uri = valueOrDefault(urlEscape(req.URL.Path, false), "/") uri = valueOrDefault(urlEscape(req.URL.Path, false, false), "/")
if req.URL.RawQuery != "" { if req.URL.RawQuery != "" {
uri += "?" + req.URL.RawQuery uri += "?" + req.URL.RawQuery
} }

View File

@ -54,15 +54,18 @@ func (e URLEscapeError) String() string {
// Return true if the specified character should be escaped when // Return true if the specified character should be escaped when
// appearing in a URL string, according to RFC 2396. // appearing in a URL string, according to RFC 2396.
func shouldEscape(c byte) bool { // When 'all' is true the full range of reserved characters are matched.
func shouldEscape(c byte, all bool) bool {
if c <= ' ' || c >= 0x7F { if c <= ' ' || c >= 0x7F {
return true return true
} }
switch c { switch c {
case '<', '>', '#', '%', '"', // RFC 2396 delims case '<', '>', '#', '%', '"', // RFC 2396 delims
'{', '}', '|', '\\', '^', '[', ']', '`', // RFC2396 unwise '{', '}', '|', '\\', '^', '[', ']', '`', // RFC2396 unwise
';', '/', '?', ':', '@', '&', '=', '+', '$', ',': // RFC 2396 reserved '?', '&', '=', '+': // RFC 2396 reserved
return true return true
case ';', '/', ':', '@', '$', ',': // RFC 2396 reserved
return all
} }
return false return false
} }
@ -188,13 +191,13 @@ func urlUnescape(s string, doPlus bool) (string, os.Error) {
} }
// URLEscape converts a string into URL-encoded form. // URLEscape converts a string into URL-encoded form.
func URLEscape(s string) string { return urlEscape(s, true) } func URLEscape(s string) string { return urlEscape(s, true, true) }
func urlEscape(s string, doPlus bool) string { func urlEscape(s string, doPlus, all bool) string {
spaceCount, hexCount := 0, 0 spaceCount, hexCount := 0, 0
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
c := s[i] c := s[i]
if shouldEscape(c) { if shouldEscape(c, all) {
if c == ' ' && doPlus { if c == ' ' && doPlus {
spaceCount++ spaceCount++
} else { } else {
@ -214,7 +217,7 @@ func urlEscape(s string, doPlus bool) string {
case c == ' ' && doPlus: case c == ' ' && doPlus:
t[j] = '+' t[j] = '+'
j++ j++
case shouldEscape(c): case shouldEscape(c, all):
t[j] = '%' t[j] = '%'
t[j+1] = "0123456789abcdef"[c>>4] t[j+1] = "0123456789abcdef"[c>>4]
t[j+2] = "0123456789abcdef"[c&15] t[j+2] = "0123456789abcdef"[c&15]
@ -394,16 +397,16 @@ func (url *URL) String() string {
if i := strings.Index(info, ":"); i >= 0 { if i := strings.Index(info, ":"); i >= 0 {
info = info[0:i] + ":******" info = info[0:i] + ":******"
} }
result += urlEscape(info, false) + "@" result += urlEscape(info, false, false) + "@"
} }
result += url.Host result += url.Host
} }
result += urlEscape(url.Path, false) result += urlEscape(url.Path, false, false)
if url.RawQuery != "" { if url.RawQuery != "" {
result += "?" + url.RawQuery result += "?" + url.RawQuery
} }
if url.Fragment != "" { if url.Fragment != "" {
result += "#" + urlEscape(url.Fragment, false) result += "#" + urlEscape(url.Fragment, false, false)
} }
return result return result
} }