-
Notifications
You must be signed in to change notification settings - Fork 6
/
tunnelling.go
119 lines (106 loc) · 3 KB
/
tunnelling.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
package restli
import (
"bytes"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
)
func EncodeTunnelledQuery(httpMethod, query string, body []byte) (newBody []byte, headers http.Header) {
headers = http.Header{}
headers.Add(MethodOverrideHeader, httpMethod)
if len(body) > 0 {
multiPartBody := &bytes.Buffer{}
// Since bytes.Buffer never returns an error when written to, all errors returned by mpWriter are ignored
mpWriter := multipart.NewWriter(multiPartBody)
w, _ := mpWriter.CreatePart(textproto.MIMEHeader{ContentTypeHeader: {FormUrlEncodedContentType}})
_, _ = w.Write([]byte(query))
w, _ = mpWriter.CreatePart(textproto.MIMEHeader{ContentTypeHeader: {ApplicationJsonContentType}})
_, _ = w.Write(body)
_ = mpWriter.Close()
newBody = multiPartBody.Bytes()
headers.Add(ContentTypeHeader, mime.FormatMediaType(MultipartMixedContentType, map[string]string{
MultipartBoundary: mpWriter.Boundary(),
}))
} else {
newBody = []byte(query)
headers.Add(ContentTypeHeader, FormUrlEncodedContentType)
}
return newBody, headers
}
func DecodeTunnelledQuery(req *http.Request) (err error) {
getAndDeleteHeader := func(h string) string {
v := req.Header.Get(h)
if v == "" {
return ""
}
req.Header.Del(h)
return v
}
tunnelledMethod := getAndDeleteHeader(MethodOverrideHeader)
if req.Method != http.MethodPost || tunnelledMethod == "" {
return nil
}
req.Method = tunnelledMethod
if req.URL.RawQuery != "" {
return fmt.Errorf("go-restli: Invalid request, cannot specify the %q header with a non-empty query",
MethodOverrideHeader)
}
body := new(bytes.Buffer)
_, err = io.Copy(body, req.Body)
if err != nil {
return err
}
err = req.Body.Close()
if err != nil {
return err
}
req.Body = nil
mediaType, params, err := mime.ParseMediaType(getAndDeleteHeader(ContentTypeHeader))
switch mediaType {
case FormUrlEncodedContentType:
req.URL.RawQuery = body.String()
req.RequestURI = req.URL.RequestURI()
req.Body = http.NoBody
return nil
case MultipartMixedContentType:
r := multipart.NewReader(body, params[MultipartBoundary])
for {
part, err := r.NextPart()
if err == io.EOF {
break
}
if err != nil {
return err
}
contentType := part.Header.Get(ContentTypeHeader)
switch contentType {
case FormUrlEncodedContentType:
query, _ := io.ReadAll(part)
req.URL.RawQuery = string(query)
case ApplicationJsonContentType:
buf := new(bytes.Buffer)
_, err = io.Copy(buf, part)
if err != nil {
return err
}
req.Body = io.NopCloser(buf)
req.Header.Set(ContentTypeHeader, ApplicationJsonContentType)
default:
return fmt.Errorf("go-restli: Unknown tunnelled %s: %q", ContentTypeHeader, contentType)
}
}
if req.URL.RawQuery == "" {
return fmt.Errorf("go-restli: No query specified in tunneled query request")
}
req.RequestURI = req.URL.RequestURI()
if req.Body == nil {
return fmt.Errorf("go-restli: No body specified in tunneled query request")
}
return nil
default:
return nil
}
}