Changeset 57 in code for trunk/morty.go


Ignore:
Timestamp:
Dec 4, 2016, 10:58:41 AM (9 years ago)
Author:
asciimoo
Message:

Merge pull request #39 from dalf/comment

[fix] ignore all comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r56 r57  
    632632                return "", nil
    633633        }
     634
    634635        // TODO check malicious data: - e.g. data:script
    635636        if strings.HasPrefix(uri, "data:") {
     
    637638        }
    638639
    639         if len(uri) > 0 && uri[0] == '#' {
    640                 return uri, nil
    641         }
    642 
     640        // parse the uri
    643641        u, err := url.Parse(uri)
    644642        if err != nil {
    645643                return "", err
    646644        }
     645
     646        // get the fragment (with the prefix "#")
     647        fragment := ""
     648        if len(u.Fragment) > 0 {
     649                fragment = "#" + u.Fragment
     650        }
     651
     652        // reset the fragment: it is not included in the mortyurl
     653        u.Fragment = ""
     654
     655        // merge the URI with the document URI
    647656        u = mergeURIs(rc.BaseURL, u)
    648657
     658        // simple internal link ?
     659        // some web pages describe the whole link https://same:auth@same.host/same.path?same.query#new.fragment
     660        if u.Scheme == rc.BaseURL.Scheme &&
     661                (rc.BaseURL.User == nil || (u.User != nil && u.User.String() == rc.BaseURL.User.String())) &&
     662                u.Host == rc.BaseURL.Host &&
     663                u.Path == rc.BaseURL.Path &&
     664                u.RawQuery == rc.BaseURL.RawQuery {
     665                // the fragment is the only difference between the document URI and the uri parameter
     666                return fragment, nil
     667        }
     668
     669        // return full URI and fragment (if not empty)
    649670        uri = u.String()
    650671
    651672        if rc.Key == nil {
    652                 return fmt.Sprintf("./?mortyurl=%s", url.QueryEscape(uri)), nil
    653         }
    654 
    655         return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s", hash(uri, rc.Key), url.QueryEscape(uri)), nil
     673                return fmt.Sprintf("./?mortyurl=%s%s", url.QueryEscape(uri), fragment), nil
     674        }
     675        return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s%s", hash(uri, rc.Key), url.QueryEscape(uri), fragment), nil
    656676}
    657677
Note: See TracChangeset for help on using the changeset viewer.