// serve http with php script(cgi mode) func Cgi(w http.ResponseWriter, r *http.Request, phpBin string, scriptFileName string) { handler := new(cgi.Handler) handler.Path = phpBin handler.Env = append(handler.Env, "REDIRECT_STATUS=CGI") handler.Env = append(handler.Env, "SCRIPT_FILENAME="+scriptFileName) handler.ServeHTTP(w, r) }
/** 调用CGI程序的Handler */ func handleCgi(out http.ResponseWriter, request *http.Request) { u := (*request).URL var hdl cgi.Handler hdl.Path = "./" + u.Path hdl.Root = "/cgi/" hdl.Dir = "." hdl.ServeHTTP(out, request) }
func CgiHandler(config map[string]string) http.Handler { h := new(cgi.Handler) h.Path = mustGet(config, "path") h.Root = mustGet(config, "mount") h.Dir = tryGet(config, "dir", "") h.Args = getSlice(config, "args") h.Env = getSlice(config, "env") h.InheritEnv = getSlice(config, "inherit") h.PathLocationHandler = http.DefaultServeMux return h }
func bundleCgiHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool { segments := strings.Split(req.URL.Path, "/") cgiProgram := segments[3] // This is to try to prevent someone from trying to execute arbitrary commands (e.g. ../../../bash) if strings.Index(cgiProgram, ".") != -1 { return false } // Check the bin directories of the gopaths to find a command that matches // the command specified here. cmd := "" for _, srcDir := range srcDirs { c := filepath.Join(srcDir, "../bin/"+cgiProgram) _, err := os.Stat(c) if err == nil { cmd = c break } } if cmd != "" { logger.Printf("GODEV CGI CALL: %v\n", cmd) handler := cgi.Handler{} handler.Path = cmd handler.Args = []string{"-godev"} handler.Logger = logger handler.InheritEnv = []string{"PATH", "GOPATH"} // TODO Add GOCERTFILE, GOKEYFILE, ... handler.ServeHTTP(writer, req) return true } else { logger.Printf("GODEV CGI MISS: %v\n", cgiProgram) } return false }
// Serve a CGI application 'path', stripping 'prefix' from the URL being // requested func CGIServer(path, prefix string) Component { handler := new(cgi.Handler) handler.Path = path handler.Root = prefix return NewHandlerComponent(handler) }