@@ -124,11 +124,15 @@ func Run() error {
124
124
125
125
// if link specified on command line, resolve and exit
126
126
if flag .NArg () > 0 {
127
- destination , err := resolveLink (flag .Arg (0 ))
127
+ u , err := url . Parse (flag .Arg (0 ))
128
128
if err != nil {
129
129
log .Fatal (err )
130
130
}
131
- fmt .Println (destination )
131
+ dst , err := resolveLink (u )
132
+ if err != nil {
133
+ log .Fatal (err )
134
+ }
135
+ fmt .Println (dst .String ())
132
136
os .Exit (0 )
133
137
}
134
138
@@ -403,7 +407,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
403
407
http .Error (w , err .Error (), http .StatusInternalServerError )
404
408
return
405
409
}
406
- http .Redirect (w , r , target , http .StatusFound )
410
+ http .Redirect (w , r , target . String () , http .StatusFound )
407
411
}
408
412
409
413
// acceptHTML returns whether the request can accept a text/html response.
@@ -494,7 +498,7 @@ var expandFuncMap = texttemplate.FuncMap{
494
498
//
495
499
// If long does not include templates, the default behavior is to append
496
500
// env.Path to long.
497
- func expandLink (long string , env expandEnv ) (string , error ) {
501
+ func expandLink (long string , env expandEnv ) (* url. URL , error ) {
498
502
if ! strings .Contains (long , "{{" ) {
499
503
// default behavior is to append remaining path to long URL
500
504
if strings .HasSuffix (long , "/" ) {
@@ -505,19 +509,19 @@ func expandLink(long string, env expandEnv) (string, error) {
505
509
}
506
510
tmpl , err := texttemplate .New ("" ).Funcs (expandFuncMap ).Parse (long )
507
511
if err != nil {
508
- return "" , err
512
+ return nil , err
509
513
}
510
514
buf := new (bytes.Buffer )
511
515
if err := tmpl .Execute (buf , env ); err != nil {
512
- return "" , err
516
+ return nil , err
513
517
}
514
- long = buf .String ()
515
518
516
- _ , err = url .Parse (long )
519
+ u , err : = url .Parse (buf . String () )
517
520
if err != nil {
518
- return "" , err
521
+ return nil , err
519
522
}
520
- return long , nil
523
+
524
+ return u , nil
521
525
}
522
526
523
527
func devMode () bool { return * dev != "" }
@@ -740,22 +744,23 @@ func restoreLastSnapshot() error {
740
744
return bs .Err ()
741
745
}
742
746
743
- func resolveLink (link string ) (string , error ) {
744
- // if link specified as "go/name", trim "go" prefix.
745
- // Remainder will parse as URL with no scheme or host
746
- link = strings . TrimPrefix ( link , * hostname )
747
- u , err := url . Parse ( link )
748
- if err != nil {
749
- return "" , err
747
+ func resolveLink (link * url. URL ) (* url. URL , error ) {
748
+ path := link . Path
749
+
750
+ // if link was specified as "go/name", it will parse with no scheme or host.
751
+ // Trim "go" prefix from beginning of path.
752
+ if link . Host == "" {
753
+ path = strings . TrimPrefix ( path , * hostname )
750
754
}
751
- short , remainder , _ := strings .Cut (strings .TrimPrefix (u .RequestURI (), "/" ), "/" )
755
+
756
+ short , remainder , _ := strings .Cut (strings .TrimPrefix (path , "/" ), "/" )
752
757
l , err := db .Load (short )
753
758
if err != nil {
754
- return "" , err
759
+ return nil , err
755
760
}
756
761
dst , err := expandLink (l .Long , expandEnv {Now : time .Now ().UTC (), Path : remainder })
757
762
if err == nil {
758
- if u , uErr := url . Parse ( dst ); uErr == nil && ( u . Hostname () == "" || u . Hostname () == * hostname ) {
763
+ if dst . Host == "" || dst . Host == * hostname {
759
764
dst , err = resolveLink (dst )
760
765
}
761
766
}
0 commit comments