/
get_first.go
119 lines (109 loc) · 3.48 KB
/
get_first.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
package check
import (
"github.com/google/uuid"
"github.com/nwtgck/piping-server-check/oneshot"
"io"
"net/http"
"net/http/httptrace"
"strings"
"time"
)
func get_first() Check {
return Check{
Name: getCheckName(),
run: func(config *Config, reporter RunCheckReporter) {
defer reporter.Close()
serverUrl, ok, stopServerIfNeed := prepareServerUrl(config, &reporter)
if !ok {
return
}
defer stopServerIfNeed()
postHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer postHttpClient.CloseIdleConnections()
getHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer getHttpClient.CloseIdleConnections()
path := "/" + uuid.NewString()
bodyString := "my message"
url := serverUrl + path
contentType := "text/plain"
getRespOneshot := oneshot.NewOneshot[*http.Response]()
getReqWroteRequestCh := make(chan struct{})
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
}
getReq = getReq.WithContext(httptrace.WithClientTrace(getReq.Context(), &httptrace.ClientTrace{
WroteRequest: func(info httptrace.WroteRequestInfo) {
getReqWroteRequestCh <- struct{}{}
},
}))
getResp, getOk := sendOrGetAndCheck(getHttpClient, getReq, config.Protocol, reporter)
if !getOk {
return
}
getRespOneshot.Send(getResp)
}()
if config.Protocol == ProtocolH3 {
// httptrace not supported: https://github.com/quic-go/quic-go/issues/3342
reporter.Report(RunCheckResult{Warnings: []ResultWarning{NewWarning("Sorry. Ensuring GET-request-first is not supported in HTTP/3", nil)}})
time.Sleep(config.GetReqWroteRequestWaitForH3)
} else {
// Wait for the GET request
select {
case <-getReqWroteRequestCh:
case _, ok := <-getRespOneshot.Channel():
if !ok {
return
}
}
}
postRespOneshot := oneshot.NewOneshot[*http.Response]()
go func() {
defer postRespOneshot.Done()
postReq, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create POST request", err)))
return
}
postReq.Header.Set("Content-Type", contentType)
postResp, postOk := sendOrGetAndCheck(postHttpClient, postReq, config.Protocol, reporter)
if !postOk {
return
}
postRespOneshot.Send(postResp)
}()
getResp, ok := respWithTimeout("", "GET", getRespOneshot, config.FixedLengthBodyGetTimeout, reporter)
if !ok {
return
}
checkContentTypeForwarding(getResp, contentType, reporter)
checkXRobotsTag(getResp, reporter)
bodyBytes, err := io.ReadAll(getResp.Body)
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to read up", err)))
return
}
if ok := checkCloseReceiverRespBody(getResp, reporter); !ok {
return
}
if string(bodyBytes) != bodyString {
reporter.Report(NewRunCheckResultWithOneError(NewError("message different", nil)))
return
}
// TODO: POST-timeout (already GET)
postResp, ok := <-postRespOneshot.Channel()
if !ok {
return
}
if ok := checkSenderRespReadUp(SubCheckNameTransferred, postResp, reporter); !ok {
return
}
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameTransferred})
checkTransferForReusePath(config, url, reporter)
return
},
}
}