Skip to content

Commit fe006cc

Browse files
author
Ben Ye
authored
always parse request post form (#85)
* always parse request post form Signed-off-by: Ben Ye <[email protected]> * add parseForm when req body modified Signed-off-by: Ben Ye <[email protected]>
1 parent fa02066 commit fe006cc

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

injectproxy/routes.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,19 @@ func (r *routes) enforceLabel(h http.HandlerFunc) http.Handler {
211211
}
212212
req.URL.RawQuery = q.Encode()
213213
// Remove the proxy label from the PostForm.
214-
if req.PostForm.Get(r.label) != "" {
215-
req.PostForm.Del(r.label)
216-
newBody := req.PostForm.Encode()
217-
// We are replacing request body, close previous one (req.FormValue ensures it is read fully and not nil).
218-
_ = req.Body.Close()
219-
req.Body = ioutil.NopCloser(strings.NewReader(newBody))
220-
req.ContentLength = int64(len(newBody))
214+
if req.Method == http.MethodPost {
215+
if err := req.ParseForm(); err != nil {
216+
http.Error(w, fmt.Sprintf("Failed to parse the PostForm: %v", err), http.StatusInternalServerError)
217+
return
218+
}
219+
if req.PostForm.Get(r.label) != "" {
220+
req.PostForm.Del(r.label)
221+
newBody := req.PostForm.Encode()
222+
// We are replacing request body, close previous one (req.FormValue ensures it is read fully and not nil).
223+
_ = req.Body.Close()
224+
req.Body = ioutil.NopCloser(strings.NewReader(newBody))
225+
req.ContentLength = int64(len(newBody))
226+
}
221227
}
222228

223229
h.ServeHTTP(w, req)
@@ -356,6 +362,9 @@ func (r *routes) matcher(w http.ResponseWriter, req *http.Request) {
356362
}
357363
req.URL.RawQuery = q.Encode()
358364
if req.Method == http.MethodPost {
365+
if err := req.ParseForm(); err != nil {
366+
return
367+
}
359368
q = req.PostForm
360369
if err := injectMatcher(q, matcher); err != nil {
361370
return

0 commit comments

Comments
 (0)