-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.go
More file actions
392 lines (335 loc) · 11.3 KB
/
Copy pathintegration.go
File metadata and controls
392 lines (335 loc) · 11.3 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package http2
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
)
// HTTP2Transport implements http.RoundTripper interface for HTTP/2 communication.
// It allows using standard http.Client with HTTP/2 transport layer.
type HTTP2Transport struct {
client *Client
closed bool
}
// NewHTTP2Transport creates a new HTTP/2 transport instance.
func NewHTTP2Transport(address string) (*HTTP2Transport, error) {
logConnection("creating_transport", address, map[string]interface{}{
"protocol": "HTTP/2.0",
})
client, err := NewClient(address)
if err != nil {
logError(err, "transport_creation_failed", map[string]interface{}{
"address": address,
})
return nil, fmt.Errorf("failed to create HTTP/2 client: %w", err)
}
logConnection("transport_created", address, map[string]interface{}{
"status": "success",
})
return &HTTP2Transport{
client: client,
closed: false,
}, nil
}
// RoundTrip implements the http.RoundTripper interface.
// It converts standard http.Request to HTTP/2 format, sends it over HTTP/2 connection,
// and converts the response back to standard http.Response format.
func (t *HTTP2Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.closed {
logError(fmt.Errorf("transport closed"), "roundtrip_failed", map[string]interface{}{
"url": req.URL.String(),
})
return nil, fmt.Errorf("transport is closed")
}
logRequest(req.Method, req.URL.Path, req.Host, nil)
// Convert standard http.Request to internal http2.Request format
http2Req, err := t.convertRequest(req)
if err != nil {
logError(err, "request_conversion_failed", map[string]interface{}{
"method": req.Method,
"url": req.URL.String(),
})
return nil, fmt.Errorf("failed to convert request: %w", err)
}
// Send request over HTTP/2 connection
http2Resp, err := t.client.SendRequest(http2Req)
if err != nil {
logError(err, "http2_request_failed", map[string]interface{}{
"method": req.Method,
"url": req.URL.String(),
})
return nil, fmt.Errorf("HTTP/2 request failed: %w", err)
}
logResponse(http2Resp.StatusCode, http2Resp.Headers, len(http2Resp.Body))
// Convert internal http2.Response back to standard http.Response
httpResp, err := t.convertResponse(http2Resp, req)
if err != nil {
logError(err, "response_conversion_failed", map[string]interface{}{
"status_code": http2Resp.StatusCode,
})
return nil, fmt.Errorf("failed to convert response: %w", err)
}
return httpResp, nil
}
// Close terminates the HTTP/2 transport and underlying connection.
func (t *HTTP2Transport) Close() error {
if !t.closed {
t.closed = true
return t.client.Close()
}
return nil
}
// convertRequest converts a standard http.Request to internal http2.Request format.
// This includes extracting HTTP/2 pseudo-headers and filtering connection-specific headers.
func (t *HTTP2Transport) convertRequest(req *http.Request) (*Request, error) {
// Extract HTTP method, default to GET if empty
method := strings.ToUpper(req.Method)
if method == "" {
method = MethodGET
}
logger.Debug().
Str("event", "request_conversion").
Str("method", method).
Str("url", req.URL.String()).
Msg("Converting HTTP request to HTTP/2")
// Construct request path including query parameters
path := req.URL.Path
if path == "" {
path = "/"
}
if req.URL.RawQuery != "" {
path += "?" + req.URL.RawQuery
}
// Extract authority (host:port) from request
authority := req.Host
if authority == "" && req.URL.Host != "" {
authority = req.URL.Host
}
// Extract scheme, default to http if not specified
scheme := req.URL.Scheme
if scheme == "" {
scheme = SchemeHTTP
}
// Convert HTTP/1.1 headers to HTTP/2 format
headers := make(map[string]string)
for name, values := range req.Header {
// HTTP/2 header names must be lowercase per RFC 7540
headerName := strings.ToLower(name)
// Skip connection-specific headers as per RFC 7540 Section 8.1.2.2
if isConnectionSpecific(headerName) {
continue
}
// Join multiple header values with comma separator
if len(values) > 0 {
headers[headerName] = strings.Join(values, ", ")
}
}
// Read request body if present
var body []byte
if req.Body != nil {
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("failed to read request body: %w", err)
}
body = bodyBytes
req.Body.Close()
// Restore body for potential retries
req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}
http2Req := &Request{
Method: method,
Path: path,
Authority: authority,
Scheme: scheme,
Headers: headers,
Body: body,
}
return http2Req, nil
}
// convertResponse converts internal http2.Response to standard http.Response format.
// This includes parsing status codes and converting headers back to HTTP/1.1 format.
func (t *HTTP2Transport) convertResponse(resp *Response, originalReq *http.Request) (*http.Response, error) {
logger.Debug().
Str("event", "response_conversion").
Int("status_code", resp.StatusCode).
Int("body_length", len(resp.Body)).
Msg("Converting HTTP/2 response to HTTP")
// Parse HTTP status code from response
statusCode := resp.StatusCode
if statusCode == 0 && resp.Status != "" {
// Attempt to parse status code from status string
if code, err := strconv.Atoi(resp.Status); err == nil {
statusCode = code
} else {
statusCode = 200 // Default to OK if parsing fails
}
}
// Generate standard HTTP status text
statusText := http.StatusText(statusCode)
if statusText == "" {
statusText = "Unknown"
}
// Convert HTTP/2 headers back to HTTP/1.1 format
httpHeaders := make(http.Header)
for name, value := range resp.Headers {
// Skip HTTP/2 pseudo-headers (starting with ':')
if strings.HasPrefix(name, ":") {
continue
}
// Split comma-separated header values back into individual entries
values := strings.Split(value, ", ")
for _, v := range values {
httpHeaders.Add(name, strings.TrimSpace(v))
}
}
// Create response body reader
var body io.ReadCloser
if len(resp.Body) > 0 {
body = io.NopCloser(bytes.NewReader(resp.Body))
} else {
body = io.NopCloser(strings.NewReader(""))
}
// Construct standard http.Response
httpResp := &http.Response{
Status: fmt.Sprintf("%d %s", statusCode, statusText),
StatusCode: statusCode,
Proto: "HTTP/2.0",
ProtoMajor: 2,
ProtoMinor: 0,
Header: httpHeaders,
Body: body,
ContentLength: int64(len(resp.Body)),
Request: originalReq,
}
return httpResp, nil
}
// isConnectionSpecific checks if a header is connection-specific and should be
// excluded from HTTP/2 requests per RFC 7540 Section 8.1.2.2.
func isConnectionSpecific(headerName string) bool {
// Headers that must not be included in HTTP/2 requests
connectionHeaders := map[string]bool{
"connection": true,
"keep-alive": true,
"proxy-connection": true,
"transfer-encoding": true,
"upgrade": true,
}
return connectionHeaders[headerName]
}
// HTTP2Client wraps the standard http.Client with HTTP/2 transport.
// It provides a drop-in replacement for http.Client that uses HTTP/2 protocol.
type HTTP2Client struct {
*http.Client
transport *HTTP2Transport
}
// NewHTTP2Client creates a new HTTP client with HTTP/2 transport.
// The address parameter specifies the target server (host:port).
func NewHTTP2Client(address string) (*HTTP2Client, error) {
transport, err := NewHTTP2Transport(address)
if err != nil {
return nil, err
}
client := &HTTP2Client{
Client: &http.Client{
Transport: transport,
},
transport: transport,
}
return client, nil
}
// Close terminates the HTTP/2 client and underlying connections.
func (c *HTTP2Client) Close() error {
return c.transport.Close()
}
// ConvertHTTPRequest converts a standard http.Request to internal http2.Request format.
// This is a standalone utility function for request conversion.
func ConvertHTTPRequest(req *http.Request) (*Request, error) {
t := &HTTP2Transport{} // Temporary instance for method access
return t.convertRequest(req)
}
// ConvertHTTPResponse converts internal http2.Response to standard http.Response format.
// This is a standalone utility function for response conversion.
func ConvertHTTPResponse(resp *Response, originalReq *http.Request) (*http.Response, error) {
t := &HTTP2Transport{} // Temporary instance for method access
return t.convertResponse(resp, originalReq)
}
// SendHTTPRequest sends a standard http.Request over HTTP/2 and returns http.Response.
// This method allows using standard HTTP request/response types with HTTP/2 transport.
func (c *Client) SendHTTPRequest(req *http.Request) (*http.Response, error) {
// Convert standard http.Request to internal format
http2Req, err := ConvertHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("failed to convert request: %w", err)
}
// Send request over HTTP/2 connection
http2Resp, err := c.SendRequest(http2Req)
if err != nil {
return nil, fmt.Errorf("HTTP/2 request failed: %w", err)
}
// Convert response back to standard format
httpResp, err := ConvertHTTPResponse(http2Resp, req)
if err != nil {
return nil, fmt.Errorf("failed to convert response: %w", err)
}
return httpResp, nil
}
// Helper functions for creating HTTP requests
// NewHTTPRequest creates a new http.Request with the specified method, URL, and body.
func NewHTTPRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
return http.NewRequest(method, urlStr, body)
}
// NewHTTPRequestWithContext creates a new http.Request with context for timeout/cancellation.
func NewHTTPRequestWithContext(ctx context.Context, method, urlStr string, body io.Reader) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, urlStr, body)
}
// NewGetRequest creates a GET request for the specified URL.
func NewGetRequest(urlStr string) (*http.Request, error) {
return http.NewRequest("GET", urlStr, nil)
}
// NewPostRequest creates a POST request with JSON content type and body.
func NewPostRequest(urlStr string, jsonBody []byte) (*http.Request, error) {
req, err := http.NewRequest("POST", urlStr, bytes.NewReader(jsonBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
// NewPostFormRequest creates a POST request with form-encoded data.
func NewPostFormRequest(urlStr string, formData url.Values) (*http.Request, error) {
req, err := http.NewRequest("POST", urlStr, strings.NewReader(formData.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
// Convenience methods for common HTTP operations
// DoGet performs a GET request to the specified URL and returns the response.
func (c *Client) DoGet(urlStr string) (*http.Response, error) {
req, err := NewGetRequest(urlStr)
if err != nil {
return nil, err
}
return c.SendHTTPRequest(req)
}
// DoPost performs a POST request with JSON body to the specified URL.
func (c *Client) DoPost(urlStr string, jsonBody []byte) (*http.Response, error) {
req, err := NewPostRequest(urlStr, jsonBody)
if err != nil {
return nil, err
}
return c.SendHTTPRequest(req)
}
// DoPostForm performs a POST request with form-encoded data to the specified URL.
func (c *Client) DoPostForm(urlStr string, formData url.Values) (*http.Response, error) {
req, err := NewPostFormRequest(urlStr, formData)
if err != nil {
return nil, err
}
return c.SendHTTPRequest(req)
}