From dd6cbd7f818f8f94868beac5e53b59e6fe5583b2 Mon Sep 17 00:00:00 2001 From: Dingjun Date: Thu, 5 Jan 2017 18:17:16 +0800 Subject: [PATCH] use gofast implement to handle php script --- .gitignore | 1 + fastcgi.go | 115 ----------------------------------------------------- routers.go | 9 +++-- 3 files changed, 7 insertions(+), 118 deletions(-) delete mode 100644 fastcgi.go diff --git a/.gitignore b/.gitignore index 3c31137..00c2cff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ fileserver* gserver* +*swp diff --git a/fastcgi.go b/fastcgi.go deleted file mode 100644 index e3a6eee..0000000 --- a/fastcgi.go +++ /dev/null @@ -1,115 +0,0 @@ -package main - -import ( - "fmt" - "github.com/fangdingjun/gofast" - "log" - "net" - "net/http" - "os" - "path/filepath" - "regexp" - "strings" -) - -// FastCGI is a fastcgi client connection -type FastCGI struct { - Network string - Addr string - DocRoot string - URLPrefix string - //client gofast.Client -} - -// NewFastCGI creates a new FastCGI struct -func NewFastCGI(network, addr, docroot, urlPrefix string) (*FastCGI, error) { - u := strings.TrimRight(urlPrefix, "/") - return &FastCGI{network, addr, docroot, u}, nil -} - -var fcgiPathInfo = regexp.MustCompile(`^(.*?\.php)(.*)$`) - -// ServeHTTP implements http.Handler interface -func (f *FastCGI) ServeHTTP(w http.ResponseWriter, r *http.Request) { - f.FastCGIPass(w, r) -} - -// FastCGIPass pass the request to fastcgi socket -func (f *FastCGI) FastCGIPass(w http.ResponseWriter, r *http.Request) { - // make sure server not access the file out of document root - p1 := filepath.Clean(filepath.Join(f.DocRoot, r.URL.Path)) - p2 := filepath.Clean(f.DocRoot) - if !strings.HasPrefix(p1, p2) { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "invalid url") - return - } - - var scriptName, pathInfo, scriptFileName string - - conn, err := net.Dial(f.Network, f.Addr) - if err != nil { - log.Println(err) - w.WriteHeader(http.StatusBadGateway) - return - } - - defer conn.Close() - - client := gofast.NewClient(f.DocRoot, conn, 20) - - urlPath := r.URL.Path - if f.URLPrefix != "" { - urlPath = strings.Replace(r.URL.Path, f.URLPrefix, "", 1) - } - - p := fcgiPathInfo.FindStringSubmatch(urlPath) - - if len(p) < 2 { - if strings.HasSuffix(r.URL.Path, "/") { - // redirect to index.php - scriptName = filepath.Join(r.URL.Path, "index.php") - pathInfo = "" - scriptFileName = filepath.Join(f.DocRoot, urlPath, "index.php") - } else { - // serve static file in php directory - fn := filepath.Join(f.DocRoot, urlPath) - http.ServeFile(w, r, fn) - return - } - } else { - scriptName = p[1] - pathInfo = p[2] - scriptFileName = filepath.Join(f.DocRoot, scriptName) - } - - req := client.NewRequest(r) - - // set ourself path, prefix stripped - // req.Params["DOCUMENT_URI"] = scriptName - req.Params["SCRIPT_NAME"] = scriptName - req.Params["PHP_SELF"] = scriptName - req.Params["PATH_INFO"] = pathInfo - req.Params["SCRIPT_FILENAME"] = scriptFileName - - scheme := "http" - if r.TLS != nil { - scheme = "https" - } - - req.Params["REQUEST_SCHEME"] = scheme - - resp, err := client.Do(req) - if err != nil { - log.Println(err) - w.WriteHeader(http.StatusBadGateway) - return - } - - err = resp.WriteTo(w, os.Stderr) - if err != nil { - log.Println(err) - } - - resp.Close() -} diff --git a/routers.go b/routers.go index f5be881..79686d3 100644 --- a/routers.go +++ b/routers.go @@ -3,6 +3,7 @@ package main import ( "crypto/tls" "fmt" + "github.com/fangdingjun/gofast" "github.com/gorilla/mux" "log" "net" @@ -155,22 +156,24 @@ func registerUwsgiHandler(r rule, router *mux.Router) { } func registerFastCGIHandler(r rule, docroot string, router *mux.Router) { - var p string + var n, p string switch r.Target.Type { case "unix": + n = "unix" p = r.Target.Path case "tcp": + n = "tcp" p = fmt.Sprintf("%s:%d", r.Target.Host, r.Target.Port) default: fmt.Printf("invalid scheme: %s, only support unix, tcp", r.Target.Type) os.Exit(-1) } + + u := gofast.NewHandler(gofast.NewPHPFS(docroot), n, p) if r.IsRegex { m1 := myURLMatch{regexp.MustCompile(r.URLPrefix)} - u, _ := NewFastCGI(r.Target.Type, p, docroot, "") router.MatcherFunc(m1.match).Handler(u) } else { - u, _ := NewFastCGI(r.Target.Type, p, docroot, r.URLPrefix) router.PathPrefix(r.URLPrefix).Handler(u) } }