-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_first.go
144 lines (134 loc) · 5.07 KB
/
send_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
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
package check
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/nwtgck/piping-server-check/util"
"io"
"net/http"
"net/http/httptrace"
"strings"
"time"
)
func post_first() Check {
return Check{
Name: getCheckName(),
run: func(config *Config, runCheckResultCh chan<- RunCheckResult) {
sendFirstRun("POST", config, runCheckResultCh)
},
}
}
func put() Check {
return Check{
Name: getCheckName(),
run: func(config *Config, runCheckResultCh chan<- RunCheckResult) {
sendFirstRun("PUT", config, runCheckResultCh)
},
}
}
func sendFirstRun(sendMethod string, config *Config, runCheckResultCh chan<- RunCheckResult) {
defer close(runCheckResultCh)
serverUrl, ok, stopServerIfNeed := prepareServerUrl(config, runCheckResultCh)
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"
gettingCh := make(chan struct{}, 1)
// h3 does not support httptrace: https://github.com/quic-go/quic-go/issues/3342
var getWroteRequestNotForH3 bool
postRespCh := make(chan *http.Response, 1)
postFinished := make(chan struct{})
go func() {
defer func() { postFinished <- struct{}{} }()
postReq, err := http.NewRequest(sendMethod, url, strings.NewReader(bodyString))
if err != nil {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError("failed to create POST request", err))
return
}
postReq.Header.Set("Content-Type", contentType)
postResp, postOk := sendOrGetAndCheck(postHttpClient, postReq, config.Protocol, runCheckResultCh)
if !postOk {
return
}
// TODO: not subcheck in HTTP/1.0
if getWroteRequestNotForH3 {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSenderResponseBeforeReceiver, Warnings: []ResultWarning{NewWarning("sender's response header should be arrived before receiver's request", nil)}}
} else {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSenderResponseBeforeReceiver}
if config.Protocol == ProtocolH3 {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSamePathSenderRejection, Warnings: []ResultWarning{NewWarning("not supported in h3", nil)}}
} else {
ctx, cancel := context.WithCancel(context.Background())
go func() { <-gettingCh; cancel() }()
checkSenderConnected(ctx, config, sendMethod, url, runCheckResultCh)
}
}
postRespCh <- postResp
}()
select {
case <-postRespCh:
case <-time.After(config.SenderResponseBeforeReceiverTimeout):
}
gettingCh <- struct{}{}
getReq, err := http.NewRequest("GET", url, nil)
if err != nil {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError("failed to create GET request", err))
return
}
getReq = getReq.WithContext(httptrace.WithClientTrace(getReq.Context(), &httptrace.ClientTrace{
WroteRequest: func(info httptrace.WroteRequestInfo) {
getWroteRequestNotForH3 = true
},
}))
getResp, getOk := sendOrGetAndCheck(getHttpClient, getReq, config.Protocol, runCheckResultCh)
if !getOk {
return
}
checkContentTypeForwarding(getResp, contentType, runCheckResultCh)
checkXRobotsTag(getResp, runCheckResultCh)
bodyBytes, err := io.ReadAll(getResp.Body)
if err != nil {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError("failed to read up", err))
return
}
if string(bodyBytes) != bodyString {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError("message different", nil))
return
}
<-postFinished
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameTransferred}
checkTransferForReusePath(config, url, runCheckResultCh)
return
}
func checkSenderConnected(ctx context.Context, config *Config, sendMethod string, url string, runCheckResultCh chan<- RunCheckResult) {
sendHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer sendHttpClient.CloseIdleConnections()
pr, _ := io.Pipe()
sendReq, err := http.NewRequest(sendMethod, url, pr)
if err != nil {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSamePathSenderRejection, Errors: []ResultError{NewError(fmt.Sprintf("failed to create %s request", sendMethod), err)}}
return
}
sendReq = sendReq.WithContext(ctx)
sendResp, err := sendHttpClient.Do(sendReq)
if err != nil {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSamePathSenderRejection, Errors: []ResultError{NewError(fmt.Sprintf("failed to %s", sendMethod), err)}}
return
}
if resultErrors := checkProtocol(sendResp, config.Protocol); len(resultErrors) != 0 {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameProtocol, Errors: resultErrors}
}
if util.IsHttp4xxError(sendResp) {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSamePathSenderRejection}
} else {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameSamePathSenderRejection, Errors: []ResultError{NewError(fmt.Sprintf("expected 4xx status but found: %d", sendResp.StatusCode), nil)}}
}
}