219
221
// scheme:opaque[?query][#fragment]
223
// Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
224
// A consequence is that it is impossible to tell which slashes in the Path were
225
// slashes in the raw URL and which were %2f. This distinction is rarely important,
226
// but when it is a client must use other routines to parse the raw URL or construct
227
// the parsed URL. For example, an HTTP server can consult req.RequestURI, and
228
// an HTTP client can use URL{Host: "example.com", Opaque: "//example.com/Go%2f"}
229
// instead of URL{Host: "example.com", Path: "/Go/"}.
221
230
type URL struct {
223
232
Opaque string // encoded opaque data
224
233
User *Userinfo // username and password information
234
Host string // host or host:port
227
236
RawQuery string // encoded query values, without '?'
228
237
Fragment string // fragment for references, without '#'
308
317
// Maybe s is of the form t c u.
309
318
// If so, return t, c u (or t, u if cutc == true).
310
319
// If not, return s, "".
311
func split(s string, c byte, cutc bool) (string, string) {
312
for i := 0; i < len(s); i++ {
315
return s[0:i], s[i+1:]
320
func split(s string, c string, cutc bool) (string, string) {
321
i := strings.Index(s, c)
326
return s[0:i], s[i+len(c):]
323
331
// Parse parses rawurl into a URL structure.
324
332
// The rawurl may be relative or absolute.
325
333
func Parse(rawurl string) (url *URL, err error) {
327
u, frag := split(rawurl, '#', true)
335
u, frag := split(rawurl, "#", true)
328
336
if url, err = parse(u, false); err != nil {
353
361
func parse(rawurl string, viaRequest bool) (url *URL, err error) {
364
if rawurl == "" && viaRequest {
357
365
err = errors.New("empty url")
362
375
// Split off possible leading "http:", "mailto:", etc.
363
376
// Cannot contain escaped characters.
364
377
if url.Scheme, rest, err = getscheme(rawurl); err != nil {
380
url.Scheme = strings.ToLower(url.Scheme)
368
rest, url.RawQuery = split(rest, '?', true)
382
rest, url.RawQuery = split(rest, "?", true)
370
384
if !strings.HasPrefix(rest, "/") {
371
385
if url.Scheme != "" {
382
if (url.Scheme != "" || !viaRequest) && strings.HasPrefix(rest, "//") && !strings.HasPrefix(rest, "///") {
396
if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
383
397
var authority string
384
authority, rest = split(rest[2:], '/', false)
398
authority, rest = split(rest[2:], "/", false)
385
399
url.User, url.Host, err = parseAuthority(authority)
414
428
user = User(userinfo)
416
username, password := split(userinfo, ':', true)
430
username, password := split(userinfo, ":", true)
417
431
if username, err = unescape(username, encodeUserPassword); err != nil {
428
442
// String reassembles the URL into a valid URL string.
429
443
func (u *URL) String() string {
430
// TODO: Rewrite to use bytes.Buffer
432
445
if u.Scheme != "" {
433
result += u.Scheme + ":"
446
buf.WriteString(u.Scheme)
435
449
if u.Opaque != "" {
450
buf.WriteString(u.Opaque)
438
if u.Host != "" || u.User != nil {
452
if u.Scheme != "" || u.Host != "" || u.User != nil {
453
buf.WriteString("//")
440
454
if u := u.User; u != nil {
441
result += u.String() + "@"
455
buf.WriteString(u.String())
458
if h := u.Host; h != "" {
445
result += escape(u.Path, encodePath)
462
buf.WriteString(escape(u.Path, encodePath))
447
464
if u.RawQuery != "" {
448
result += "?" + u.RawQuery
466
buf.WriteString(u.RawQuery)
450
468
if u.Fragment != "" {
451
result += "#" + escape(u.Fragment, encodeFragment)
470
buf.WriteString(escape(u.Fragment, encodeFragment))
456
475
// Values maps a string key to a list of values.
541
parts := make([]string, 0, len(v)) // will be large enough for most uses
542
for k, vs := range v {
565
keys := make([]string, 0, len(v))
567
keys = append(keys, k)
570
for _, k := range keys {
543
572
prefix := QueryEscape(k) + "="
544
573
for _, v := range vs {
545
parts = append(parts, prefix+QueryEscape(v))
577
buf.WriteString(prefix)
578
buf.WriteString(QueryEscape(v))
548
return strings.Join(parts, "&")
551
584
// resolvePath applies special path segments from refs and applies
552
// them to base, per RFC 2396.
553
func resolvePath(basepath string, refpath string) string {
554
base := strings.Split(basepath, "/")
555
refs := strings.Split(refpath, "/")
559
for idx, ref := range refs {
562
base[len(base)-1] = ""
564
newLen := len(base) - 1
585
// them to base, per RFC 3986.
586
func resolvePath(base, ref string) string {
590
} else if ref[0] != '/' {
591
i := strings.LastIndex(base, "/")
592
full = base[:i+1] + ref
600
src := strings.Split(full, "/")
601
for _, elem := range src {
607
dst = dst[:len(dst)-1]
568
base = base[0:newLen]
569
base[len(base)-1] = ""
571
if idx == 0 || base[len(base)-1] == "" {
572
base[len(base)-1] = ref
574
base = append(base, ref)
610
dst = append(dst, elem)
578
return strings.Join(base, "/")
613
if last := src[len(src)-1]; last == "." || last == ".." {
614
// Add final slash to the joined path.
615
dst = append(dst, "")
617
return "/" + strings.TrimLeft(strings.Join(dst, "/"), "/")
581
620
// IsAbs returns true if the URL is absolute.
597
636
// ResolveReference resolves a URI reference to an absolute URI from
598
// an absolute base URI, per RFC 2396 Section 5.2. The URI reference
637
// an absolute base URI, per RFC 3986 Section 5.2. The URI reference
599
638
// may be relative or absolute. ResolveReference always returns a new
600
639
// URL instance, even if the returned URL is identical to either the
601
640
// base or reference. If ref is an absolute URL, then ResolveReference
602
641
// ignores base and returns a copy of ref.
603
642
func (u *URL) ResolveReference(ref *URL) *URL {
644
if ref.Scheme == "" {
645
url.Scheme = u.Scheme
647
if ref.Scheme != "" || ref.Host != "" || ref.User != nil {
648
// The "absoluteURI" or "net_path" cases.
649
url.Path = resolvePath(ref.Path, "")
608
// relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
610
url.RawQuery = ref.RawQuery
611
url.Fragment = ref.Fragment
612
652
if ref.Opaque != "" {
613
url.Opaque = ref.Opaque
619
if ref.Host != "" || ref.User != nil {
620
// The "net_path" case.
624
if strings.HasPrefix(ref.Path, "/") {
625
// The "abs_path" case.
628
// The "rel_path" case.
629
path := resolvePath(u.Path, ref.Path)
630
if !strings.HasPrefix(path, "/") {
659
if ref.RawQuery == "" {
660
url.RawQuery = u.RawQuery
661
if ref.Fragment == "" {
662
url.Fragment = u.Fragment
666
// The "abs_path" or "rel_path" cases.
669
url.Path = resolvePath(u.Path, ref.Path)