/
httpResp.go
118 lines (95 loc) · 3.06 KB
/
httpResp.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
package v1alpha1
import (
"fmt"
"net/http"
)
//////////////////// RespProfile ////////////////
// Does not monitor Trailers
// Exposes ValueProfile interface
type RespProfile struct {
Headers HeadersProfile `json:"headers"`
MediaType MediaTypeProfile `json:"mediatype"` // "text/html"
ContentLength CountProfile `json:"contentlength"` // 0
}
func (profile *RespProfile) profileI(args ...interface{}) {
profile.Profile(args[0].(*http.Response))
}
func (profile *RespProfile) Profile(resp *http.Response) {
profile.Headers.Profile(resp.Header)
profile.MediaType.Profile(resp.Header.Get("Content-Type"))
length := resp.ContentLength
if length > 0 {
var log2length uint8
for length > 0 {
log2length++
length >>= 1
}
profile.ContentLength = CountProfile(log2length)
}
}
//////////////////// RespPile ////////////////
// Exposes ValuePile interface
type RespPile struct {
Headers HeadersPile `json:"headers"`
MediaType MediaTypePile `json:"mediatype"`
ContentLength CountPile `json:"contentlength"`
}
func (pile *RespPile) addI(valProfile ValueProfile) {
pile.Add(valProfile.(*RespProfile))
}
func (pile *RespPile) Add(profile *RespProfile) {
pile.Headers.Add(&profile.Headers)
pile.MediaType.Add(&profile.MediaType)
pile.ContentLength.Add(profile.ContentLength)
}
func (pile *RespPile) Clear() {
pile.Headers.Clear()
pile.MediaType.Clear()
pile.ContentLength.Clear()
}
func (pile *RespPile) mergeI(otherValProfile ValuePile) {
pile.Merge(otherValProfile.(*RespPile))
}
func (pile *RespPile) Merge(otherPile *RespPile) {
pile.Headers.Merge(&otherPile.Headers)
pile.MediaType.Merge(&otherPile.MediaType)
pile.ContentLength.Merge(otherPile.ContentLength)
}
//////////////////// RespConfig ////////////////
// Exposes ValueConfig interface
type RespConfig struct {
Headers HeadersConfig `json:"headers"`
MediaType MediaTypeConfig `json:"mediatype"`
ContentLength CountConfig `json:"contentlength"`
}
func (config *RespConfig) decideI(valProfile ValueProfile) string {
return config.Decide(valProfile.(*RespProfile))
}
func (config *RespConfig) Decide(profile *RespProfile) string {
if ret := config.Headers.Decide(&profile.Headers); ret != "" {
return fmt.Sprintf("Headers: %s", ret)
}
if ret := config.MediaType.Decide(&profile.MediaType); ret != "" {
return fmt.Sprintf("Media Type: %s", ret)
}
if ret := config.ContentLength.Decide(profile.ContentLength); ret != "" {
return fmt.Sprintf("Content Length: %s", ret)
}
return ""
}
func (config *RespConfig) learnI(valPile ValuePile) {
config.Learn(valPile.(*RespPile))
}
func (config *RespConfig) Learn(pile *RespPile) {
config.Headers.Learn(&pile.Headers)
config.MediaType.Learn(&pile.MediaType)
config.ContentLength.Learn(pile.ContentLength)
}
func (config *RespConfig) fuseI(otherValConfig ValueConfig) {
config.Fuse(otherValConfig.(*RespConfig))
}
func (config *RespConfig) Fuse(otherConfig *RespConfig) {
config.Headers.Fuse(&otherConfig.Headers)
config.MediaType.Fuse(&otherConfig.MediaType)
config.ContentLength.Fuse(otherConfig.ContentLength)
}