/
get_cancel_get.go
172 lines (159 loc) · 4.95 KB
/
get_cancel_get.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
package check
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/nwtgck/piping-server-check/oneshot"
"io"
"net/http"
"net/http/httptrace"
"strings"
"time"
)
func get_cancel_get() 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()
getHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer getHttpClient.CloseIdleConnections()
path := "/" + uuid.NewString()
url := serverUrl + path
getFailed := false
canceledCh := make(chan struct{})
func() {
getReqWroteRequestCh := make(chan struct{})
getReq1, err := http.NewRequest("GET", url, nil)
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create GET request", err)))
getFailed = true
return
}
ctx, cancel := context.WithCancel(context.Background())
getReq1 = getReq1.WithContext(ctx)
getReq1 = getReq1.WithContext(httptrace.WithClientTrace(getReq1.Context(), &httptrace.ClientTrace{
WroteRequest: func(info httptrace.WroteRequestInfo) {
getReqWroteRequestCh <- struct{}{}
},
}))
go func() {
if config.Protocol == ProtocolH3 {
// httptrace not supported: https://github.com/quic-go/quic-go/issues/3342
reporter.Report(RunCheckResult{Warnings: []ResultWarning{NewWarning("Sorry. WroteRequest detection not supported in HTTP/3", nil)}})
<-time.After(config.GetReqWroteRequestWaitForH3)
} else {
<-getReqWroteRequestCh
}
// Difficult to detect whether server handles GET request
time.Sleep(config.WaitDurationBetweenReceiverWroteRequestAndCancel)
cancel()
canceledCh <- struct{}{}
}()
_, err = getHttpClient.Do(getReq1)
if errors.Is(err, context.Canceled) {
return
}
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to GET", err)))
getFailed = true
return
}
reporter.Report(NewRunCheckResultWithOneError(NewError("expected not to receive a response but GET response received", err)))
}()
if getFailed {
return
}
<-canceledCh
time.Sleep(config.WaitDurationAfterReceiverCancel)
checkTransferForGetCancelGet(config, url, reporter)
return
},
}
}
func checkTransferForGetCancelGet(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 := "my message"
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)
}()
postRespOneshot := oneshot.NewOneshot[*http.Response]()
go func() {
defer postRespOneshot.Done()
if config.Protocol == ProtocolH3 {
// httptrace not supported: https://github.com/quic-go/quic-go/issues/3342
reporter.Report(RunCheckResult{Warnings: []ResultWarning{NewWarning("Sorry. WroteRequest detection not supported in HTTP/3", nil)}})
<-time.After(config.GetReqWroteRequestWaitForH3)
} else {
<-getReqWroteRequestCh
}
postReq, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
reporter.Report(RunCheckResult{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 := <-getRespOneshot.Channel()
if !ok {
return
}
bodyBytes, err := io.ReadAll(getResp.Body)
if err != nil {
reporter.Report(RunCheckResult{Errors: []ResultError{NewError("failed to read up", err)}})
return
}
if ok := checkCloseReceiverRespBody(getResp, reporter); !ok {
return
}
if string(bodyBytes) != bodyString {
reporter.Report(RunCheckResult{Errors: []ResultError{NewError("message different", nil)}})
return
}
postResp, ok := <-postRespOneshot.Channel()
if !ok {
return
}
if ok := checkSenderRespReadUp("", postResp, reporter); !ok {
return
}
reporter.Report(RunCheckResult{})
}