]> Cypherpunks repositories - gostls13.git/commitdiff
http: don't always escape all reserved chars (fix build)
authorAndrew Gerrand <adg@golang.org>
Wed, 22 Sep 2010 06:59:35 +0000 (16:59 +1000)
committerAndrew Gerrand <adg@golang.org>
Wed, 22 Sep 2010 06:59:35 +0000 (16:59 +1000)
R=nigeltao, nigeltao_golang
CC=golang-dev
https://golang.org/cl/2206044

src/pkg/http/request.go
src/pkg/http/url.go

index a602fd52c0e28fa44f51733f9cb7d29c36a128e2..56a930e45146b196268854521773889e888835bc 100644 (file)
@@ -191,7 +191,7 @@ func (req *Request) Write(w io.Writer) os.Error {
 
        uri := req.RawURL
        if uri == "" {
-               uri = valueOrDefault(urlEscape(req.URL.Path, false), "/")
+               uri = valueOrDefault(urlEscape(req.URL.Path, false, false), "/")
                if req.URL.RawQuery != "" {
                        uri += "?" + req.URL.RawQuery
                }
index 060619b8355ec5aef2290a553a57a01dac40b7fa..c1ede281123ea6aef15b7c8cb65ca7bbd81fac8d 100644 (file)
@@ -54,15 +54,18 @@ func (e URLEscapeError) String() string {
 
 // Return true if the specified character should be escaped when
 // 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 {
                return true
        }
        switch c {
        case '<', '>', '#', '%', '"', // RFC 2396 delims
                '{', '}', '|', '\\', '^', '[', ']', '`', // RFC2396 unwise
-               ';', '/', '?', ':', '@', '&', '=', '+', '$', ',': // RFC 2396 reserved
+               '?', '&', '=', '+': // RFC 2396 reserved
                return true
+       case ';', '/', ':', '@', '$', ',': // RFC 2396 reserved
+               return all
        }
        return false
 }
@@ -188,13 +191,13 @@ func urlUnescape(s string, doPlus bool) (string, os.Error) {
 }
 
 // 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
        for i := 0; i < len(s); i++ {
                c := s[i]
-               if shouldEscape(c) {
+               if shouldEscape(c, all) {
                        if c == ' ' && doPlus {
                                spaceCount++
                        } else {
@@ -214,7 +217,7 @@ func urlEscape(s string, doPlus bool) string {
                case c == ' ' && doPlus:
                        t[j] = '+'
                        j++
-               case shouldEscape(c):
+               case shouldEscape(c, all):
                        t[j] = '%'
                        t[j+1] = "0123456789abcdef"[c>>4]
                        t[j+2] = "0123456789abcdef"[c&15]
@@ -394,16 +397,16 @@ func (url *URL) String() string {
                        if i := strings.Index(info, ":"); i >= 0 {
                                info = info[0:i] + ":******"
                        }
-                       result += urlEscape(info, false) + "@"
+                       result += urlEscape(info, false, false) + "@"
                }
                result += url.Host
        }
-       result += urlEscape(url.Path, false)
+       result += urlEscape(url.Path, false, false)
        if url.RawQuery != "" {
                result += "?" + url.RawQuery
        }
        if url.Fragment != "" {
-               result += "#" + urlEscape(url.Fragment, false)
+               result += "#" + urlEscape(url.Fragment, false, false)
        }
        return result
 }