Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fever-ch
GitHub Repository: fever-ch/go-google-sites-proxy
Path: blob/master/proxy/patch.go
508 views
1
package proxy
2
3
import (
4
"bytes"
5
"github.com/fever-ch/go-google-sites-proxy/blob"
6
"github.com/fever-ch/go-google-sites-proxy/common/config"
7
"github.com/fever-ch/go-google-sites-proxy/utils"
8
"regexp"
9
"strconv"
10
)
11
12
13
func newPatcher(site config.Site, context *siteContext) func(*Page) *Page {
14
var htmlRx, _ = regexp.Compile("text/html($|;.*)")
15
patchLinks := func(input *Page) *Page {
16
if !site.KeepLinks() && htmlRx.MatchString(input.Headers["Content-Type"]) {
17
return &Page{input.Code,
18
input.Headers,
19
blob.NewRawBlob(bytes.Replace(input.Blob.Raw(), []byte("\"/"+site.GRef()), []byte("\""), -1)),
20
input.OriginallyGziped}
21
}
22
return input
23
}
24
25
ep := int(utils.Epoch())
26
27
patchFavicon := func(input *Page) *Page {
28
if context.Favicon != nil && htmlRx.MatchString(input.Headers["Content-Type"]) {
29
return &Page{input.Code,
30
input.Headers,
31
blob.NewRawBlob(bytes.Replace(input.Blob.Raw(),
32
[]byte("<link rel=\"icon\" href=\"//ssl.gstatic.com/atari/images/favicon_2.ico\"/>"),
33
[]byte("<link rel=\"icon\" href=\"/favicon.ico?v="+strconv.Itoa(ep)+"\"/>"),
34
-1)),
35
36
input.OriginallyGziped}
37
}
38
return input
39
}
40
41
return func(input *Page) *Page {
42
return patchFavicon(patchLinks(input))
43
}
44
}
45
46