diff options
Diffstat (limited to 'src/pkg/http/url.go')
-rw-r--r-- | src/pkg/http/url.go | 158 |
1 files changed, 79 insertions, 79 deletions
diff --git a/src/pkg/http/url.go b/src/pkg/http/url.go index ddf0ba986..ca8c344be 100644 --- a/src/pkg/http/url.go +++ b/src/pkg/http/url.go @@ -8,19 +8,19 @@ package http import ( - "os"; - "strconv"; - "strings"; + "os" + "strconv" + "strings" ) // URLError reports an error and the operation and URL that caused it. type URLError struct { - Op string; - URL string; - Error os.Error; + Op string + URL string + Error os.Error } -func (e *URLError) String() string { return e.Op + " " + e.URL + ": " + e.Error.String() } +func (e *URLError) String() string { return e.Op + " " + e.URL + ": " + e.Error.String() } func ishex(c byte) bool { switch { @@ -31,7 +31,7 @@ func ishex(c byte) bool { case 'A' <= c && c <= 'F': return true } - return false; + return false } func unhex(c byte) byte { @@ -43,7 +43,7 @@ func unhex(c byte) byte { case 'A' <= c && c <= 'F': return c - 'A' + 10 } - return 0; + return 0 } type URLEscapeError string @@ -59,12 +59,12 @@ func shouldEscape(c byte) bool { return true } switch c { - case '<', '>', '#', '%', '"', // RFC 2396 delims - '{', '}', '|', '\\', '^', '[', ']', '`', // RFC2396 unwise - '?', '&', '=', '+': // RFC 2396 reserved in path + case '<', '>', '#', '%', '"', // RFC 2396 delims + '{', '}', '|', '\\', '^', '[', ']', '`', // RFC2396 unwise + '?', '&', '=', '+': // RFC 2396 reserved in path return true } - return false; + return false } // URLUnescape unescapes a URL-encoded string, @@ -73,23 +73,23 @@ func shouldEscape(c byte) bool { // by two hexadecimal digits. func URLUnescape(s string) (string, os.Error) { // Count %, check that they're well-formed. - n := 0; - hasPlus := false; + n := 0 + hasPlus := false for i := 0; i < len(s); { switch s[i] { case '%': - n++; + n++ if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) { - s = s[i:]; + s = s[i:] if len(s) > 3 { s = s[0:3] } - return "", URLEscapeError(s); + return "", URLEscapeError(s) } - i += 3; + i += 3 case '+': - hasPlus = true; - i++; + hasPlus = true + i++ default: i++ } @@ -99,32 +99,32 @@ func URLUnescape(s string) (string, os.Error) { return s, nil } - t := make([]byte, len(s)-2*n); - j := 0; + t := make([]byte, len(s)-2*n) + j := 0 for i := 0; i < len(s); { switch s[i] { case '%': - t[j] = unhex(s[i+1])<<4 | unhex(s[i+2]); - j++; - i += 3; + t[j] = unhex(s[i+1])<<4 | unhex(s[i+2]) + j++ + i += 3 case '+': - t[j] = ' '; - j++; - i++; + t[j] = ' ' + j++ + i++ default: - t[j] = s[i]; - j++; - i++; + t[j] = s[i] + j++ + i++ } } - return string(t), nil; + return string(t), nil } // URLEscape converts a string into URL-encoded form. func URLEscape(s string) string { - spaceCount, hexCount := 0, 0; + spaceCount, hexCount := 0, 0 for i := 0; i < len(s); i++ { - c := s[i]; + c := s[i] if shouldEscape(c) { if c == ' ' { spaceCount++ @@ -138,24 +138,24 @@ func URLEscape(s string) string { return s } - t := make([]byte, len(s)+2*hexCount); - j := 0; + t := make([]byte, len(s)+2*hexCount) + j := 0 for i := 0; i < len(s); i++ { switch c := s[i]; { case c == ' ': - t[j] = '+'; - j++; + t[j] = '+' + j++ case shouldEscape(c): - t[j] = '%'; - t[j+1] = "0123456789abcdef"[c>>4]; - t[j+2] = "0123456789abcdef"[c&15]; - j += 3; + t[j] = '%' + t[j+1] = "0123456789abcdef"[c>>4] + t[j+2] = "0123456789abcdef"[c&15] + j += 3 default: - t[j] = s[i]; - j++; + t[j] = s[i] + j++ } } - return string(t); + return string(t) } // A URL represents a parsed URL (technically, a URI reference). @@ -168,15 +168,15 @@ func URLEscape(s string) string { // Note, the reason for using wire format for the query is that it needs // to be split into key/value pairs before decoding. type URL struct { - Raw string; // the original string - Scheme string; // scheme - RawPath string; // //[userinfo@]host/path[?query][#fragment] - Authority string; // [userinfo@]host - Userinfo string; // userinfo - Host string; // host - Path string; // /path - RawQuery string; // query - Fragment string; // fragment + Raw string // the original string + Scheme string // scheme + RawPath string // //[userinfo@]host/path[?query][#fragment] + Authority string // [userinfo@]host + Userinfo string // userinfo + Host string // host + Path string // /path + RawQuery string // query + Fragment string // fragment } // Maybe rawurl is of the form scheme:path. @@ -184,7 +184,7 @@ type URL struct { // If so, return scheme, path; else return "", rawurl. func getscheme(rawurl string) (scheme, path string, err os.Error) { for i := 0; i < len(rawurl); i++ { - c := rawurl[i]; + c := rawurl[i] switch { case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z': // do nothing @@ -196,14 +196,14 @@ func getscheme(rawurl string) (scheme, path string, err os.Error) { if i == 0 { return "", "", os.ErrorString("missing protocol scheme") } - return rawurl[0:i], rawurl[i+1:], nil; + return rawurl[0:i], rawurl[i+1:], nil default: // we have encountered an invalid character, // so there is no valid scheme return "", rawurl, nil } } - return "", rawurl, nil; + return "", rawurl, nil } // Maybe s is of the form t c u. @@ -215,10 +215,10 @@ func split(s string, c byte, cutc bool) (string, string) { if cutc { return s[0:i], s[i+1:] } - return s[0:i], s[i:]; + return s[0:i], s[i:] } } - return s, ""; + return s, "" } // TODO(rsc): The BUG comment is supposed to appear in the godoc output @@ -233,18 +233,18 @@ func split(s string, c byte, cutc bool) (string, string) { // (Web browsers strip #fragment before sending the URL to a web server.) func ParseURL(rawurl string) (url *URL, err os.Error) { if rawurl == "" { - err = os.ErrorString("empty url"); - goto Error; + err = os.ErrorString("empty url") + goto Error } - url = new(URL); - url.Raw = rawurl; + url = new(URL) + url.Raw = rawurl // split off possible leading "http:", "mailto:", etc. - var path string; + var path string if url.Scheme, path, err = getscheme(rawurl); err != nil { goto Error } - url.RawPath = path; + url.RawPath = path // RFC 2396: a relative URI (no scheme) has a ?query, // but absolute URIs only have query if path begins with / @@ -279,32 +279,32 @@ func ParseURL(rawurl string) (url *URL, err os.Error) { goto Error } if strings.Index(url.Scheme, "%") >= 0 { - err = os.ErrorString("hexadecimal escape in scheme"); - goto Error; + err = os.ErrorString("hexadecimal escape in scheme") + goto Error } if strings.Index(url.Host, "%") >= 0 { - err = os.ErrorString("hexadecimal escape in host"); - goto Error; + err = os.ErrorString("hexadecimal escape in host") + goto Error } - return url, nil; + return url, nil Error: - return nil, &URLError{"parse", rawurl, err}; + return nil, &URLError{"parse", rawurl, err} } // ParseURLReference is like ParseURL but allows a trailing #fragment. func ParseURLReference(rawurlref string) (url *URL, err os.Error) { // Cut off #frag. - rawurl, frag := split(rawurlref, '#', true); + rawurl, frag := split(rawurlref, '#', true) if url, err = ParseURL(rawurl); err != nil { return nil, err } if url.Fragment, err = URLUnescape(frag); err != nil { return nil, &URLError{"parse", rawurl, err} } - return url, nil; + return url, nil } // String reassembles url into a valid URL string. @@ -313,23 +313,23 @@ func ParseURLReference(rawurlref string) (url *URL, err os.Error) { // the String method consults Scheme, Path, Host, Userinfo, // RawQuery, and Fragment, but not Raw, RawPath or Authority. func (url *URL) String() string { - result := ""; + result := "" if url.Scheme != "" { result += url.Scheme + ":" } if url.Host != "" || url.Userinfo != "" { - result += "//"; + result += "//" if url.Userinfo != "" { result += URLEscape(url.Userinfo) + "@" } - result += url.Host; + result += url.Host } - result += URLEscape(url.Path); + result += URLEscape(url.Path) if url.RawQuery != "" { result += "?" + url.RawQuery } if url.Fragment != "" { result += "#" + URLEscape(url.Fragment) } - return result; + return result } |