-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_first_byte_by_byte_streaming.go
155 lines (145 loc) · 4.79 KB
/
post_first_byte_by_byte_streaming.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
package check
import (
"fmt"
"github.com/google/uuid"
"github.com/nwtgck/piping-server-check/oneshot"
"golang.org/x/exp/slices"
"io"
"net/http"
"time"
)
func post_first_byte_by_byte_streaming() Check {
return Check{
Name: getCheckName(),
run: func(config *Config, reporter RunCheckReporter) {
defer reporter.Close()
if slices.Contains([]Protocol{ProtocolHttp1_0, ProtocolHttp1_0_tls}, config.Protocol) {
// Skip because HTTP/1.0 has not chunked encoding
return
}
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()
url := serverUrl + path
postRespOneshot := oneshot.NewOneshot[*http.Response]()
pr, pw := io.Pipe()
go func() {
defer postRespOneshot.Done()
postReq, err := http.NewRequest("POST", url, pr)
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create request", err)))
return
}
postResp, postOk := sendOrGetAndCheck(postHttpClient, postReq, config.Protocol, reporter)
if !postOk {
return
}
postRespOneshot.Send(postResp)
// Need to send one byte to GET
if _, err := pw.Write([]byte{0}); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to send request body", err)))
return
}
}()
select {
case _, ok := <-postRespOneshot.Channel():
if !ok {
return
}
case <-time.After(config.SenderResponseBeforeReceiverTimeout):
}
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 request", err)))
return
}
getResp, getOk := sendOrGetAndCheck(getHttpClient, getReq, config.Protocol, reporter)
if !getOk {
return
}
getRespOneshot.Send(getResp)
}()
var getResp *http.Response
select {
case getResp, ok = <-getRespOneshot.Channel():
if !ok {
return
}
case <-time.After(config.GetResponseReceivedTimeout):
reporter.Report(NewRunCheckResultWithOneError(NewError(fmt.Sprintf("failed to get receiver's response in %s", config.GetResponseReceivedTimeout), nil)))
return
}
firstByteChecked := make(chan struct{}, 1)
go func() {
var buff [1]byte
if _, err := io.ReadFull(getResp.Body, buff[:]); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to read GET response body", err)))
return
}
if buff[0] != 0 {
reporter.Report(NewRunCheckResultWithOneError(NewError("different first byte of body", nil)))
return
}
firstByteChecked <- struct{}{}
}()
select {
case <-firstByteChecked:
case <-time.After(config.FirstByteCheckTimeout):
reporter.Report(NewRunCheckResultWithOneError(NewError(fmt.Sprintf("failed to get first byte in %s", config.FirstByteCheckTimeout), nil)))
return
}
var buff [1]byte
for i := 1; i < 256; i++ {
writeBytes := []byte{byte(i)}
if _, err := pw.Write(writeBytes); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to send request body", err)))
return
}
if _, err := io.ReadFull(getResp.Body, buff[:]); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to read GET response body", err)))
return
}
if byte(i) != buff[0] {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameTransferred, Errors: []ResultError{NewError(fmt.Sprintf("different body: i=%d", i), nil)}})
return
}
}
if err := pw.Close(); err != nil {
reporter.Report(RunCheckResult{Errors: []ResultError{NewError("failed to close sending body", err)}})
return
}
n, err := getResp.Body.Read(buff[:])
if n != 0 {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameTransferred, Errors: []ResultError{NewError(fmt.Sprintf("expected to read 0 bytes but %d", n), err)}})
return
}
if err != io.EOF {
reporter.Report(RunCheckResult{SubCheckName: SubCheckNameTransferred, Errors: []ResultError{NewError("expected to get EOF", err)}})
return
}
if ok := checkCloseReceiverRespBody(getResp, reporter); !ok {
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})
return
},
}
}