/
source.go
198 lines (156 loc) · 3.99 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package module
import (
"crypto/sha256"
"encoding/base64"
"github.com/andybalholm/cascadia"
"github.com/pkg/errors"
"golang.org/x/net/html"
"io"
"net/http"
"strconv"
"strings"
"time"
)
type WebSource struct {
id *string
method *string
url *string
body *string
responseStatusCode *int
responseHeaders *http.Header
responseContent *string
cachedUntil *time.Time
loadTime *time.Duration
}
type WebComponent struct {
source *WebSource
name *string
headers *http.Header
content *html.Node
scripts []*html.Node
stylesheets []*html.Node
}
func newSource(method *string, url *string, body *string) *WebSource {
result := new(WebSource)
result.method = method
result.url = url
result.body = body
result.id = result.calculateId()
return result
}
func (s *WebSource) load(c ComposeContext) error {
ti := time.Now()
c.logCompositionDebug("composition fetching remote", s.url, s.method)
requestBody := ""
if s.body != nil {
requestBody = *s.body
}
bodyReader := strings.NewReader(requestBody)
request, err := http.NewRequest(*s.method, *s.url, bodyReader)
if err != nil {
return err
}
c.handoverRequestHeader(request.Header)
response, err := c.httpClient.Do(request)
if err != nil {
return err
}
data, err := io.ReadAll(response.Body)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
dataString := string(data)
s.responseStatusCode = &response.StatusCode
s.responseHeaders = &response.Header
s.responseContent = &dataString
duration := ti.Sub(time.Now())
s.loadTime = &duration
s.cachedUntil = s.calculateCachedUntil()
return nil
}
func (s *WebSource) calculateCachedUntil() *time.Time {
prefix := "max-age="
value := s.responseHeaders.Get("Cache-Control")
if strings.HasPrefix(value, prefix) {
number, err := strconv.Atoi(strings.ReplaceAll(value, prefix, ""))
if err == nil {
result := time.Now()
result = result.Add(time.Duration(number) * time.Second)
return &result
}
}
return nil
}
func (s *WebSource) calculateId() *string {
hasher := sha256.New()
if s.method != nil {
hasher.Write([]byte(*s.method))
}
if s.url != nil {
hasher.Write([]byte("-"))
hasher.Write([]byte(*s.url))
}
if s.body != nil {
hasher.Write([]byte("-"))
hasher.Write([]byte(*s.body))
}
hash := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return &hash
}
func (s *WebSource) getWebComponent(name *string) (*WebComponent, error) {
doc, err := parseString(s.responseContent)
if err != nil {
return nil, err
}
var componentNode *html.Node
divs := cascadia.MustCompile("div").MatchAll(doc)
for _, div := range divs {
url := attr(div, AttributeUrlKey, nil)
componentName := attr(div, AttributeNameKey, nil)
if url == nil && componentName != nil && *componentName == *name {
componentNode = div
}
}
if componentNode == nil {
return nil, errors.Errorf("Component %s not found", *name)
}
component := new(WebComponent)
component.name = name
component.source = s
component.headers = s.responseHeaders
component.content = componentNode
head := cascadia.MustCompile("head").MatchFirst(doc)
if head != nil {
component.stylesheets = cascadia.MustCompile("link").MatchAll(head)
}
body := cascadia.MustCompile("body").MatchFirst(doc)
if body != nil {
component.scripts = cascadia.MustCompile("script").MatchAll(body)
}
return component, nil
}
func (ctx *ComposeContext) handoverRequestHeader(header http.Header) {
for key, values := range ctx.httpRequest.Header {
if ctx.mustHandoverHeader(key) {
for _, value := range values {
if !containsHeaderValue(header, key, value) {
header.Add(key, value)
}
}
}
}
}
func containsHeaderValue(header http.Header, targetKey string, targetValue string) bool {
for key, values := range header {
for _, value := range values {
if strings.EqualFold(targetKey, key) && targetValue == value {
return true
}
}
}
return false
}