appview/router: fix url rewriter #798

merged
opened by oppi.li targeting master from op/vxnsylzpprzn

the appview rewrites urls of the form:

host.com/did-plc-foo/repo => host.com/did:plc:foo/repo
host.com/@handle.com/repo => host.com/handle.com/repo

however, the rewriter did not preserve query parameters or fragments:

host.com/@handle.com/repo?foo=bar => host.com/handle.com/repo?

this resulted in url rewrites being broken for git clones, which usees the "service" query parameter:

../repo/info/refs?service=git-upload-pack => ../repo/info/refs?

the new url rewriter simply takes the existing url and replaces the path component, thus preserving all other bits of the url.

Signed-off-by: oppiliappan me@oppi.li

Changed files
+10 -2
appview
state
+10 -2
appview/state/router.go
··· 57 if userutil.IsFlattenedDid(firstPart) { 58 unflattenedDid := userutil.UnflattenDid(firstPart) 59 redirectPath := strings.Join(append([]string{unflattenedDid}, pathParts[1:]...), "/") 60 - http.Redirect(w, r, "/"+redirectPath, http.StatusFound) 61 return 62 } 63 64 // if using a handle with @, rewrite to work without @ 65 if normalized := strings.TrimPrefix(firstPart, "@"); userutil.IsHandle(normalized) { 66 redirectPath := strings.Join(append([]string{normalized}, pathParts[1:]...), "/") 67 - http.Redirect(w, r, "/"+redirectPath, http.StatusFound) 68 return 69 } 70
··· 57 if userutil.IsFlattenedDid(firstPart) { 58 unflattenedDid := userutil.UnflattenDid(firstPart) 59 redirectPath := strings.Join(append([]string{unflattenedDid}, pathParts[1:]...), "/") 60 + 61 + redirectURL := *r.URL 62 + redirectURL.Path = "/" + redirectPath 63 + 64 + http.Redirect(w, r, redirectURL.String(), http.StatusFound) 65 return 66 } 67 68 // if using a handle with @, rewrite to work without @ 69 if normalized := strings.TrimPrefix(firstPart, "@"); userutil.IsHandle(normalized) { 70 redirectPath := strings.Join(append([]string{normalized}, pathParts[1:]...), "/") 71 + 72 + redirectURL := *r.URL 73 + redirectURL.Path = "/" + redirectPath 74 + 75 + http.Redirect(w, r, redirectURL.String(), http.StatusFound) 76 return 77 } 78