-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
189 lines (175 loc) · 6.91 KB
/
helpers.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
package check
import (
"fmt"
"github.com/nwtgck/piping-server-check/oneshot"
"io"
"net/http"
"strings"
"time"
)
func checkProtocol(resp *http.Response, expectedProto Protocol) []ResultError {
var resultErrors []ResultError
var versionOk bool
switch expectedProto {
case ProtocolHttp1_0, ProtocolHttp1_0_tls:
versionOk = resp.Proto == "HTTP/1.0"
case ProtocolHttp1_1, ProtocolHttp1_1_tls:
versionOk = resp.Proto == "HTTP/1.1"
case ProtocolH2, ProtocolH2c:
versionOk = resp.Proto == "HTTP/2.0"
case ProtocolH3:
versionOk = resp.Proto == "HTTP/3.0"
}
if !versionOk {
resultErrors = append(resultErrors, NewError(fmt.Sprintf("expected %s but %s", expectedProto, resp.Proto), nil))
}
shouldUseTls := protocolUsesTls(expectedProto)
if shouldUseTls && resp.TLS == nil {
resultErrors = append(resultErrors, NewError("should use TLS but not used", nil))
}
if !shouldUseTls && resp.TLS != nil {
resultErrors = append(resultErrors, NewError("should not use TLS but used", nil))
}
return resultErrors
}
func sendOrGetAndCheck(httpClient *http.Client, req *http.Request, protocol Protocol, reporter RunCheckReporter) (*http.Response, bool) {
resp, err := httpClient.Do(req)
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError(fmt.Sprintf("failed to %s", req.Method), err)))
return nil, false
}
if resultErrors := checkProtocol(resp, protocol); len(resultErrors) != 0 {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameProtocol, Errors: resultErrors})
}
if resp.StatusCode != 200 {
reporter.Report(NewRunCheckResultWithOneError(ResultError{Message: fmt.Sprintf("expected status=200 but status=%d found", resp.StatusCode)}))
return nil, false
}
return resp, true
}
func checkContentTypeForwarding(getResp *http.Response, expectedContentType string, reporter RunCheckReporter) {
receivedContentType := getResp.Header.Get("Content-Type")
if receivedContentType == expectedContentType {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameContentTypeForwarding})
} else {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameContentTypeForwarding, Errors: []ResultError{{Message: fmt.Sprintf("Content-Type should be %s but found %s", expectedContentType, receivedContentType)}}})
}
}
func checkContentDispositionForwarding(getResp *http.Response, expectedContentDisposition string, reporter RunCheckReporter) {
receivedContentDisposition := getResp.Header.Get("Content-Disposition")
if receivedContentDisposition == expectedContentDisposition {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameContentDispositionForwarding})
} else {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameContentDispositionForwarding, Errors: []ResultError{{Message: fmt.Sprintf("Content-Disposition should be %s but found %s", expectedContentDisposition, receivedContentDisposition)}}})
}
}
func checkXRobotsTag(getResp *http.Response, reporter RunCheckReporter) {
receivedXRobotsTag := getResp.Header.Get("X-Robots-Tag")
if receivedXRobotsTag == "none" {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameXRobotsTagNone})
} else {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameXRobotsTagNone, Warnings: []ResultWarning{{Message: fmt.Sprintf("X-Robots-Tag: none is recommeded but found '%+v'", receivedXRobotsTag)}}})
}
}
func checkTransferForReusePath(config *Config, url string, reporter RunCheckReporter) {
getHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer getHttpClient.CloseIdleConnections()
postHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer postHttpClient.CloseIdleConnections()
bodyString := "message for reuse"
getRespOneshot := oneshot.NewOneshot[*http.Response]()
go func() {
defer getRespOneshot.Done()
getReq, err := http.NewRequest("GET", url, nil)
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create GET request", err)))
return
}
getResp, getOk := sendOrGetAndCheck(getHttpClient, getReq, config.Protocol, reporter)
if !getOk {
return
}
getRespOneshot.Send(getResp)
}()
postRespOneshot := oneshot.NewOneshot[*http.Response]()
go func() {
defer postRespOneshot.Done()
postReq, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("failed to create POST request", err)}})
return
}
postResp, postOk := sendOrGetAndCheck(getHttpClient, postReq, config.Protocol, reporter)
if !postOk {
return
}
postRespOneshot.Send(postResp)
}()
select {
case _, ok := <-getRespOneshot.Channel():
if !ok {
return
}
case _, ok := <-postRespOneshot.Channel():
if !ok {
return
}
}
getResp, ok := respWithTimeout(SubCheckNameReusePath, "GET", getRespOneshot, config.FixedLengthBodyGetTimeout, reporter)
if !ok {
return
}
bodyBytes, err := io.ReadAll(getResp.Body)
if err != nil {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("failed to read up", err)}})
return
}
if ok := checkCloseReceiverRespBody(getResp, reporter); !ok {
return
}
if string(bodyBytes) != bodyString {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("message different", nil)}})
return
}
// TODO: POST-timeout (already GET)
postResp, ok := <-postRespOneshot.Channel()
if !ok {
return
}
if ok := checkSenderRespReadUp(SubCheckNameReusePath, postResp, reporter); !ok {
return
}
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameReusePath})
}
func respWithTimeout(subcheckName string /* empty string OK */, methodName string, respOneshot *oneshot.Oneshot[*http.Response], timeout time.Duration, reporter RunCheckReporter) (*http.Response, bool) {
var resp *http.Response
var ok bool
select {
case resp, ok = <-respOneshot.Channel():
if !ok {
return nil, false
}
return resp, true
case <-time.After(timeout):
reporter.Report(RunCheckResult{SubCheckName: subcheckName, Errors: []ResultError{NewError(fmt.Sprintf("failed to receive a %s response in %v", methodName, timeout), nil)}})
return nil, false
}
}
func checkSenderRespReadUp(subcheckName string /* empty string OK */, resp *http.Response, reporter RunCheckReporter) bool {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
reporter.Report(RunCheckResult{SubCheckName: subcheckName, Errors: []ResultError{NewError("failed to read sender response body", err)}})
return false
}
if err := resp.Body.Close(); err != nil {
reporter.Report(RunCheckResult{SubCheckName: subcheckName, Errors: []ResultError{NewError("failed to close sender response body", err)}})
return false
}
return true
}
func checkCloseReceiverRespBody(resp *http.Response, reporter RunCheckReporter) bool {
if err := resp.Body.Close(); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to close receiver response body", err)))
return false
}
return true
}