From 225d5977ff642aafed84720429ff0caab02695e4 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 3 May 2015 12:12:18 +0100 Subject: [PATCH] fastcgi: allow more request types. --- middleware/fastcgi/fastcgi.go | 13 ++++++++++--- middleware/fastcgi/fcgiclient.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 7bb241b9..7d3837a0 100644 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -85,14 +85,21 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) return http.StatusBadGateway, err } - // TODO: Allow more methods (requires refactoring fcgiclient first...) var resp *http.Response + contentLength, _ := strconv.Atoi(r.Header.Get("Content-Length")) switch r.Method { + case "HEAD": + resp, err = fcgi.Head(env) case "GET": resp, err = fcgi.Get(env) case "POST": - l, _ := strconv.Atoi(r.Header.Get("Content-Length")) - resp, err = fcgi.Post(env, r.Header.Get("Content-Type"), r.Body, l) + 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 } diff --git a/middleware/fastcgi/fcgiclient.go b/middleware/fastcgi/fcgiclient.go index 9a337ab6..a1cd35e5 100644 --- a/middleware/fastcgi/fcgiclient.go +++ b/middleware/fastcgi/fcgiclient.go @@ -407,6 +407,15 @@ func (this *FCGIClient) Get(p map[string]string) (resp *http.Response, err error return this.Request(p, nil) } +// Head issues a HEAD request to the fcgi responder. +func (this *FCGIClient) Head(p map[string]string) (resp *http.Response, err error) { + + p["REQUEST_METHOD"] = "HEAD" + p["CONTENT_LENGTH"] = "0" + + return this.Request(p, nil) +} + // Get issues a Post request to the fcgi responder. with request body // in the format that bodyType specified func (this *FCGIClient) Post(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { @@ -424,6 +433,30 @@ func (this *FCGIClient) Post(p map[string]string, bodyType string, body io.Reade return this.Request(p, body) } +// Put issues a PUT request to the fcgi responder. +func (this *FCGIClient) Put(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { + + p["REQUEST_METHOD"] = "PUT" + + return this.Post(p, bodyType, body, l) +} + +// Patch issues a PATCH request to the fcgi responder. +func (this *FCGIClient) Patch(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { + + p["REQUEST_METHOD"] = "PATCH" + + return this.Post(p, bodyType, body, l) +} + +// Delete issues a DELETE request to the fcgi responder. +func (this *FCGIClient) Delete(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) { + + p["REQUEST_METHOD"] = "DELETE" + + return this.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 (this *FCGIClient) PostForm(p map[string]string, data url.Values) (resp *http.Response, err error) {