From 73ed286309648260af92b1fd818e0e28d6991aed Mon Sep 17 00:00:00 2001 From: jungle-boogie Date: Wed, 27 Jan 2016 11:28:49 -0800 Subject: [PATCH 1/2] wrap lines to 80 also update copyright year. --- dist/README.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dist/README.txt b/dist/README.txt index 85baab0f..532e93f4 100644 --- a/dist/README.txt +++ b/dist/README.txt @@ -8,9 +8,11 @@ Source Code https://github.com/mholt/caddy -For instructions on using Caddy, please see the user guide on the website. For a list of what's new in this version, see CHANGES.txt. +For instructions on using Caddy, please see the user guide on the website. +For a list of what's new in this version, see CHANGES.txt. -If you have a question, bug report, or would like to contribute, please open an issue or submit a pull request on GitHub. Your contributions do not go unnoticed! +If you have a question, bug report, or would like to contribute, please open an +issue or submit a pull request on GitHub. Your contributions do not go unnoticed! For a good time, follow @mholt6 on Twitter. @@ -18,4 +20,4 @@ And thanks - you're awesome! --- -(c) 2015 Matthew Holt +(c) 2015 - 2016 Matthew Holt From 4d4ea94465c4a128855b7067a0b9a13110ab4ef9 Mon Sep 17 00:00:00 2001 From: Kevin Bowrin Date: Wed, 27 Jan 2016 19:00:08 -0500 Subject: [PATCH 2/2] Parse address from fastcgi directive, and pass results to fcgiclient Dial(). This allows scheme prefixes "tcp://" and "fastcgi://" in configuration. Fixes #540 --- middleware/fastcgi/fastcgi.go | 26 +++++++++++++++++++------ middleware/fastcgi/fastcgi_test.go | 31 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 middleware/fastcgi/fastcgi_test.go diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 21404c2e..517505b6 100755 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -70,7 +70,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } // Connect to FastCGI gateway - fcgi, err := getClient(&rule) + network, address := rule.parseAddress() + fcgi, err := Dial(network, address) if err != nil { return http.StatusBadGateway, err } @@ -128,15 +129,28 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) return h.Next.ServeHTTP(w, r) } -func getClient(r *Rule) (*FCGIClient, error) { - // check if unix socket or TCP +// parseAddress returns the network and address of r. +// The first string is the network, "tcp" or "unix", implied from the scheme and address. +// The second string is r.Address, with scheme prefixes removed. +// The two returned strings can be used as parameters to the Dial() function. +func (r Rule) parseAddress() (string, string) { + // check if address has tcp scheme explicitly set + if strings.HasPrefix(r.Address, "tcp://") { + return "tcp", r.Address[len("tcp://"):] + } + // check if address has fastcgi scheme explicity set + if strings.HasPrefix(r.Address, "fastcgi://") { + return "tcp", r.Address[len("fastcgi://"):] + } + // check if unix socket if trim := strings.HasPrefix(r.Address, "unix"); strings.HasPrefix(r.Address, "/") || trim { if trim { - r.Address = r.Address[len("unix:"):] + return "unix", r.Address[len("unix:"):] } - return Dial("unix", r.Address) + return "unix", r.Address } - return Dial("tcp", r.Address) + // default case, a plain tcp address with no scheme + return "tcp", r.Address } func writeHeader(w http.ResponseWriter, r *http.Response) { diff --git a/middleware/fastcgi/fastcgi_test.go b/middleware/fastcgi/fastcgi_test.go new file mode 100644 index 00000000..69ee02f3 --- /dev/null +++ b/middleware/fastcgi/fastcgi_test.go @@ -0,0 +1,31 @@ +package fastcgi + +import ( + "testing" +) + +func TestRuleParseAddress(t *testing.T) { + + getClientTestTable := []struct { + rule *Rule + expectednetwork string + expectedaddress string + }{ + {&Rule{Address: "tcp://172.17.0.1:9000"}, "tcp", "172.17.0.1:9000"}, + {&Rule{Address: "fastcgi://localhost:9000"}, "tcp", "localhost:9000"}, + {&Rule{Address: "172.17.0.15"}, "tcp", "172.17.0.15"}, + {&Rule{Address: "/my/unix/socket"}, "unix", "/my/unix/socket"}, + {&Rule{Address: "unix:/second/unix/socket"}, "unix", "/second/unix/socket"}, + } + + for _, entry := range getClientTestTable { + if actualnetwork, _ := entry.rule.parseAddress(); actualnetwork != entry.expectednetwork { + t.Errorf("Unexpected network for address string %v. Got %v, expected %v", entry.rule.Address, actualnetwork, entry.expectednetwork) + } + if _, actualaddress := entry.rule.parseAddress(); actualaddress != entry.expectedaddress { + t.Errorf("Unexpected parsed address for address string %v. Got %v, expected %v", entry.rule.Address, actualaddress, entry.expectedaddress) + } + + } + +}