Changeset 51 in code for trunk/morty.go


Ignore:
Timestamp:
Nov 29, 2016, 10:35:59 AM (9 years ago)
Author:
alex
Message:

[fix] URI fragment are not encoded in the mortyurl but are encoded as usual fragment so the browser can use them.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r50 r51  
    285285        }
    286286
    287         if bytes.Contains(contentType, []byte("xhtml")) {
    288                 ctx.SetContentType("text/html; charset=UTF-8")
    289         } else {
    290                 ctx.SetContentType(fmt.Sprintf("%s; charset=UTF-8", contentInfo[0]))
    291         }
     287        ctx.SetContentType(fmt.Sprintf("%s; charset=UTF-8", contentInfo[0]))
    292288
    293289        switch {
     
    629625                return "", nil
    630626        }
     627
    631628        // TODO check malicious data: - e.g. data:script
    632629        if strings.HasPrefix(uri, "data:") {
     
    634631        }
    635632
    636         if len(uri) > 0 && uri[0] == '#' {
    637                 return uri, nil
    638         }
    639 
     633        // parse the uri
    640634        u, err := url.Parse(uri)
    641635        if err != nil {
    642636                return "", err
    643637        }
     638
     639        // get the fragment (with the prefix "#")
     640        fragment := ""
     641        if len(u.Fragment) > 0 {
     642                fragment = "#" + u.Fragment
     643        }
     644
     645        // reset the fragment: it is not included in the mortyurl
     646        u.Fragment = ""
     647
     648        // merge the URI with the document URI
    644649        u = mergeURIs(rc.BaseURL, u)
    645650
     651        // simple internal link ?
     652        // some web pages describe the whole link https://same:auth@same.host/same.path?same.query#new.fragment
     653        if u.Scheme == rc.BaseURL.Scheme &&
     654                ((u.User == nil && rc.BaseURL.User == nil) || (u.User.String() == rc.BaseURL.User.String())) &&
     655                u.Host == rc.BaseURL.Host &&
     656                u.Path == rc.BaseURL.Path &&
     657                u.RawQuery == rc.BaseURL.RawQuery {
     658                // the fragment is the only difference between the document URI and the uri parameter
     659                return fragment, nil
     660        }
     661
     662        // return full URI and fragment (if not empty)
    646663        uri = u.String()
    647664
    648665        if rc.Key == nil {
    649                 return fmt.Sprintf("./?mortyurl=%s", url.QueryEscape(uri)), nil
    650         }
    651 
    652         return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s", hash(uri, rc.Key), url.QueryEscape(uri)), nil
     666                return fmt.Sprintf("./?mortyurl=%s%s", url.QueryEscape(uri), fragment), nil
     667        }
     668        return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s%s", hash(uri, rc.Key), url.QueryEscape(uri), fragment), nil
    653669}
    654670
Note: See TracChangeset for help on using the changeset viewer.