From 600ee9a89fbba384b7aae883907b74ab3e14e4d7 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 31 Jan 2016 21:36:39 -0700 Subject: [PATCH] fastcgi: Accept any other methods as a POST-style request --- middleware/fastcgi/fastcgi.go | 10 +------- middleware/fastcgi/fcgiclient.go | 36 +++++++-------------------- middleware/fastcgi/fcgiclient_test.go | 2 +- 3 files changed, 11 insertions(+), 37 deletions(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 517505b6..5ecbdf8c 100755 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -85,16 +85,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) resp, err = fcgi.Get(env) case "OPTIONS": resp, err = fcgi.Options(env) - case "POST": - resp, err = fcgi.Post(env, r.Header.Get("Content-Type"), r.Body, contentLength) - case "PUT": - resp, err = fcgi.Put(env, r.Header.Get("Content-Type"), r.Body, contentLength) - case "PATCH": - resp, err = fcgi.Patch(env, r.Header.Get("Content-Type"), r.Body, contentLength) - case "DELETE": - resp, err = fcgi.Delete(env, r.Header.Get("Content-Type"), r.Body, contentLength) default: - return http.StatusMethodNotAllowed, nil + resp, err = fcgi.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength) } if resp.Body != nil { diff --git a/middleware/fastcgi/fcgiclient.go b/middleware/fastcgi/fcgiclient.go index 511a5219..05949ec8 100644 --- a/middleware/fastcgi/fcgiclient.go +++ b/middleware/fastcgi/fcgiclient.go @@ -484,11 +484,17 @@ func (c *FCGIClient) Options(p map[string]string) (resp *http.Response, err erro // Post issues a POST request to the fcgi responder. with request body // in the format that bodyType specified -func (c *FCGIClient) Post(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { +func (c *FCGIClient) Post(p map[string]string, method string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { + if p == nil { + p = make(map[string]string) + } + + p["REQUEST_METHOD"] = strings.ToUpper(method) if len(p["REQUEST_METHOD"]) == 0 || p["REQUEST_METHOD"] == "GET" { p["REQUEST_METHOD"] = "POST" } + p["CONTENT_LENGTH"] = strconv.Itoa(l) if len(bodyType) > 0 { p["CONTENT_TYPE"] = bodyType @@ -499,35 +505,11 @@ func (c *FCGIClient) Post(p map[string]string, bodyType string, body io.Reader, return c.Request(p, body) } -// Put issues a PUT request to the fcgi responder. -func (c *FCGIClient) Put(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { - - p["REQUEST_METHOD"] = "PUT" - - return c.Post(p, bodyType, body, l) -} - -// Patch issues a PATCH request to the fcgi responder. -func (c *FCGIClient) Patch(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { - - p["REQUEST_METHOD"] = "PATCH" - - return c.Post(p, bodyType, body, l) -} - -// Delete issues a DELETE request to the fcgi responder. -func (c *FCGIClient) Delete(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { - - p["REQUEST_METHOD"] = "DELETE" - - return c.Post(p, bodyType, body, l) -} - // PostForm issues a POST to the fcgi responder, with form // as a string key to a list values (url.Values) func (c *FCGIClient) PostForm(p map[string]string, data url.Values) (resp *http.Response, err error) { body := bytes.NewReader([]byte(data.Encode())) - return c.Post(p, "application/x-www-form-urlencoded", body, body.Len()) + return c.Post(p, "POST", "application/x-www-form-urlencoded", body, body.Len()) } // PostFile issues a POST to the fcgi responder in multipart(RFC 2046) standard, @@ -566,7 +548,7 @@ func (c *FCGIClient) PostFile(p map[string]string, data url.Values, file map[str return } - return c.Post(p, bodyType, buf, buf.Len()) + return c.Post(p, "POST", bodyType, buf, buf.Len()) } // Checks whether chunked is part of the encodings stack diff --git a/middleware/fastcgi/fcgiclient_test.go b/middleware/fastcgi/fcgiclient_test.go index 6ed37bb4..f48420cc 100644 --- a/middleware/fastcgi/fcgiclient_test.go +++ b/middleware/fastcgi/fcgiclient_test.go @@ -119,7 +119,7 @@ func sendFcgi(reqType int, fcgiParams map[string]string, data []byte, posts map[ if len(data) > 0 { length = len(data) rd := bytes.NewReader(data) - resp, err = fcgi.Post(fcgiParams, "", rd, rd.Len()) + resp, err = fcgi.Post(fcgiParams, "", "", rd, rd.Len()) } else if len(posts) > 0 { values := url.Values{} for k, v := range posts {