forked from neex/http2smugl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse_set.go
73 lines (67 loc) · 1.56 KB
/
response_set.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
package main
import (
"bytes"
"fmt"
"sort"
)
type ResponseSet struct {
statuses map[string]struct{}
minLength, maxLength int
hasNonTimeouts bool
}
func (rs *ResponseSet) AccountResponse(response *HTTPMessage, isTimeout bool) {
length := 0
if response != nil {
length = len(response.Body)
}
if rs.statuses == nil {
rs.statuses = make(map[string]struct{})
rs.minLength = length
rs.maxLength = length
}
status := "<error>"
if response != nil {
responseStatus, ok := response.Headers.Get(":status")
if ok {
status = responseStatus
}
}
rs.hasNonTimeouts = rs.hasNonTimeouts || !isTimeout
rs.statuses[status] = struct{}{}
if rs.minLength > length {
rs.minLength = length
}
if rs.maxLength < length {
rs.maxLength = length
}
}
func (rs *ResponseSet) DistinguishableFrom(other *ResponseSet) bool {
if rs.statuses == nil || other.statuses == nil {
return true
}
statusSetsIntersect := false
for k := range rs.statuses {
if _, ok := other.statuses[k]; ok {
statusSetsIntersect = true
break
}
}
if !statusSetsIntersect {
return true
}
return rs.minLength > other.maxLength || rs.maxLength < other.minLength
}
func (rs *ResponseSet) String() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("statuses ")
var statuses []string
for s := range rs.statuses {
statuses = append(statuses, s)
}
sort.Strings(statuses)
_, _ = fmt.Fprintf(buf, "%v, %v <= size <= %v", statuses, rs.minLength, rs.maxLength)
return buf.String()
}
func (rs *ResponseSet) AllResponsesAreTimeouts() bool {
return !rs.hasNonTimeouts
}