forked from connectrpc/conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_request.go
110 lines (99 loc) · 3.19 KB
/
raw_request.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
// Copyright 2023-2024 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package referenceclient
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"connectrpc.com/conformance/internal"
conformancev1 "connectrpc.com/conformance/internal/gen/proto/go/connectrpc/conformance/v1"
)
type rawRequestSender struct {
transport http.RoundTripper
rawRequest *conformancev1.RawHTTPRequest
}
func (r *rawRequestSender) RoundTrip(orig *http.Request) (*http.Response, error) {
switch r.rawRequest.Body.(type) {
case *conformancev1.RawHTTPRequest_Unary, *conformancev1.RawHTTPRequest_Stream, nil:
default:
return nil, fmt.Errorf("raw request has invalid body definition: %T", r.rawRequest.Body)
}
uri := r.rawRequest.Uri
if len(r.rawRequest.RawQueryParams) > 0 || len(r.rawRequest.EncodedQueryParams) > 0 {
reqURL, err := url.Parse(r.rawRequest.Uri)
if err != nil {
return nil, fmt.Errorf("raw request has invalid URI: %s: %w", r.rawRequest.Uri, err)
}
vals := reqURL.Query()
for _, param := range r.rawRequest.RawQueryParams {
vals[param.Name] = append(vals[param.Name], param.Value...)
}
for _, param := range r.rawRequest.EncodedQueryParams {
var buf bytes.Buffer
if err := internal.WriteRawMessageContents(param.Value, &buf); err != nil {
return nil, fmt.Errorf("raw request has invalid encoded query param %s: %w", param.Name, err)
}
var paramVal string
if param.Base64Encode {
paramVal = base64.URLEncoding.EncodeToString(buf.Bytes())
} else {
paramVal = buf.String()
}
vals[param.Name] = append(vals[param.Name], paramVal)
}
reqURL.RawQuery = vals.Encode()
uri = reqURL.String()
}
pipeReader, pipeWriter := io.Pipe()
req, err := http.NewRequestWithContext(
orig.Context(),
r.rawRequest.Verb,
fmt.Sprintf("%s://%s%s", orig.URL.Scheme, orig.URL.Host, uri),
pipeReader,
)
if err != nil {
if orig.Body != nil {
_ = orig.Body.Close()
}
return nil, err
}
internal.AddHeaders(r.rawRequest.Headers, req.Header)
if length, err := strconv.Atoi(req.Header.Get("Content-Length")); err == nil {
req.ContentLength = int64(length)
}
// Write the request body to the pipe.
go func() {
defer func() {
_ = pipeWriter.Close()
}()
switch contents := r.rawRequest.Body.(type) {
case *conformancev1.RawHTTPRequest_Unary:
_ = internal.WriteRawMessageContents(contents.Unary, pipeWriter)
case *conformancev1.RawHTTPRequest_Stream:
_ = internal.WriteRawStreamContents(contents.Stream, pipeWriter)
}
}()
// Drain the original request body and close it.
go func() {
defer func() {
_ = orig.Body.Close()
}()
_, _ = io.Copy(io.Discard, orig.Body)
}()
return r.transport.RoundTrip(req)
}