tinyrelayfile
tinyrelay/tinyclient/prefix.go/

tinyclient/prefix.go raw
1package tinyclient
2
3import (
4 "regexp"
5 "strings"
6
7 "golang.org/x/net/html"
8)
9
10var rootURLAttribute = regexp.MustCompile(`\s(?:href|src|fx-action|action|data|fallback)="(/[^\"]*)"`)
11
12// injectBase scopes root-relative template URLs without changing scripts or
13// literal content. The signer bridge scopes its requests with localPath.
14func injectBase(source, base string) string {
15 if base == "" {
16 return source
17 }
18 var out strings.Builder
19 out.Grow(len(source))
20 tokens := html.NewTokenizer(strings.NewReader(source))
21 for {
22 kind := tokens.Next()
23 raw := string(tokens.Raw())
24 if kind == html.StartTagToken || kind == html.SelfClosingTagToken {
25 raw = rootURLAttribute.ReplaceAllStringFunc(raw, func(attribute string) string {
26 start := strings.IndexByte(attribute, '"') + 1
27 path := attribute[start : len(attribute)-1]
28 if strings.HasPrefix(path, "//") || path == base || strings.HasPrefix(path, base+"/") {
29 return attribute
30 }
31 return attribute[:start] + base + path + `"`
32 })
33 }
34 out.WriteString(raw)
35 if kind == html.ErrorToken {
36 return out.String()
37 }
38 }
39}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs