// DirectoryHandler returns a handler that serves files from a directory tree.
// The directory is specified by a slash separated path relative to the static
// server's Dir field.
765512
func (ss *StaticServer) DirectoryHandler(prefix, dirName string) http.Handler {
	if !strings.HasSuffix(prefix, "/") {
		prefix += "/"
	}
	idBase := dirName
	dirName = ss.resolve(dirName)
	return &staticHandler{
		ss: ss,
		id: func(p string) string {
			if !strings.HasPrefix(p, prefix) {
				return "."
			}
			return path.Join(idBase, p[len(prefix):])
		},
		open: func(p string) (io.ReadCloser, int64, string, error) {
			if !strings.HasPrefix(p, prefix) {
				return nil, 0, "", errors.New("request url does not match directory prefix")
			}
			p = p[len(prefix):]
			return ss.openFile(filepath.Join(dirName, filepath.FromSlash(p)))
		},
	}
}
138304
func DirectoryValidator(input string) (interface{}, error) {
	if input == "" {
		return "", nil
	}

	path, err := PathValidator(input)
	if err != nil {
		return nil, err
	}
	pathStr := path.(string)

	info, err := os.Stat(pathStr)
	if err != nil {
		return nil, err
	}

	if !info.IsDir() {
		return nil, fmt.Errorf("%s is not a directory", pathStr)
	}

	return pathStr, nil
}
606271
func SymbolHandler() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		Symbol(ctx)
		return nil
	}
}
126157
func (r *Router) IndexHandler(w http.ResponseWriter, _ *http.Request, _ Params) {
	WriteJSON(w, r.Index())
}
124125
func (c *Static) processDir(fullPath, basePath string) (args map[string]interface{}, err error) {
	dirName := fpath.Base(fullPath)
	args = map[string]interface{}{"dirName": dirName}
	// Walk the folder showing up and down links
	dirFiles := []model.FileInformation{}
	symLinkPath, e := fpath.EvalSymlinks(fullPath)
	if e != nil {
		return args, e
	}

	// Get directory contents
	files, e := ioutil.ReadDir(fullPath)
	if e != nil {
		return nil, e
	}

	if fullPath != basePath {
		fileInfo := model.FileInformation{Icon: UP_DIR_ICON, Name: c.Message("static\\parent directory"), Relative: "../"}
		dirFiles = append(dirFiles, fileInfo)
	}
	for _, f := range files {
		fileInfo := model.FileInformation{Name: f.Name()}
		if f.IsDir() {
			fileInfo.Icon = DIR_ICON
			// Check that it is not a symnlink
			realFullPath, _ := fpath.EvalSymlinks(fpath.Join(fullPath, f.Name()))
			if strings.HasPrefix(realFullPath, symLinkPath) {
				// Valid to drill into
				fileInfo.Relative = f.Name() + "/"
			}
		} else {
			fileInfo.Icon = FILE_ICON
			size := "bytes"
			divider := int64(1)
			fileInfo.Size = f.Size()
			for x := 0; fileInfo.Size > byteSizeList[x][1].(int64); x++ {
				size = byteSizeList[x][0].(string)
				divider = byteSizeList[x][1].(int64)
			}
			fileInfo.Size = fileInfo.Size / divider
			fileInfo.SizeType = size
			fileInfo.NiceSize = fmt.Sprintf("%0.1d %s", fileInfo.Size, size)
			fileInfo.Relative = fileInfo.Name
		}
		modified := f.ModTime()
		fileInfo.Modified = &modified
		dirFiles = append(dirFiles, fileInfo)
	}
	args["content"] = dirFiles
	args["count"] = len(dirFiles)
	args["dateformat"] = revel.Config.StringDefault("static.dateformat", "2006-01-02 15:04:05 MST")
	return
}
270222
func (gr *guiRouter) ensureFileHandler(h func(gh *guiHandler, w http.ResponseWriter, req *http.Request)) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		rootDir, hash, err := gr.ensureFiles(req)
		if err != nil {
			// Note that ensureFiles also checks that the model UUID is valid.
			if err := sendError(w, err); err != nil {
				logger.Errorf("%v", err)
			}
			return
		}
		qhash := req.URL.Query().Get(":hash")
		if qhash != "" && qhash != hash {
			if err := sendError(w, errors.NotFoundf("resource with %q hash", qhash)); err != nil {
				logger.Errorf("%v", err)
			}
			return
		}
		gh := &guiHandler{
			ctxt:     gr.ctxt,
			rootDir:  rootDir,
			basePath: guiURLPathPrefix,
			hash:     hash,
		}
		h(gh, w, req)
	})
}
887039
func (s *storageRESTServer) AppendFileHandler(w http.ResponseWriter, r *http.Request) {
	if !s.IsValid(w, r) {
		return
	}
	vars := mux.Vars(r)
	volume := vars[storageRESTVolume]
	filePath := vars[storageRESTFilePath]

	buf := make([]byte, r.ContentLength)
	_, err := io.ReadFull(r.Body, buf)
	if err != nil {
		s.writeErrorResponse(w, err)
		return
	}
	err = s.storage.AppendFile(volume, filePath, buf)
	if err != nil {
		s.writeErrorResponse(w, err)
	}
}
899894
func MakeHandler(fn HandlerFunc) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
		// ensure that neither proxies nor browsers cache http traffic
		SetNoCacheHeaders(w.Header())

		out, err := fn(w, r, p)
		if err != nil {
			trace.WriteError(w, err)
			return
		}
		if out != nil {
			roundtrip.ReplyJSON(w, http.StatusOK, out)
		}
	}
}
858871
func TCPRedirectHandler(opts ...HandlerOption) Handler {
	h := &tcpRedirectHandler{}
	h.Init(opts...)

	return h
}
980954
func (mr *MountableRouter) Handler(method string, path string, handler http.Handler) {
	mr.Delegate.Handler(method, libpath.Join(mr.Prefix, path), handler)
}
791413
func (filebox *Filebox) AccessDir(dirPath string, roles ...string) *Dir {
	return &Dir{DirPath: filepath.Join(filebox.BaseDir, dirPath), Roles: roles, Filebox: filebox}
}
202305
func FileServe(dir string) http.HandlerFunc {
	fs := http.FileServer(http.Dir(dir))

	return func(w http.ResponseWriter, r *http.Request) {
		r.URL.Path = Param(r.Context(), "filepath")
		fs.ServeHTTP(w, r)
	}
}
156696
func (c *folderSummaryService) foldersToHandle() []string {
	// We only recalculate summaries if someone is listening to events
	// (a request to /rest/events has been made within the last
	// pingEventInterval).

	c.lastEventReqMut.Lock()
	last := c.lastEventReq
	c.lastEventReqMut.Unlock()
	if time.Since(last) > minSummaryInterval {
		return nil
	}

	c.foldersMut.Lock()
	res := make([]string, 0, len(c.folders))
	for folder := range c.folders {
		res = append(res, folder)
		delete(c.folders, folder)
	}
	c.foldersMut.Unlock()
	return res
}
376779
func (client *storageRESTClient) ListDir(volume, dirPath string, count int, leafFile string) (entries []string, err error) {
	values := make(url.Values)
	values.Set(storageRESTVolume, volume)
	values.Set(storageRESTDirPath, dirPath)
	values.Set(storageRESTCount, strconv.Itoa(count))
	values.Set(storageRESTLeafFile, leafFile)
	respBody, err := client.call(storageRESTMethodListDir, values, nil, -1)
	if err != nil {
		return nil, err
	}
	defer http.DrainBody(respBody)
	err = gob.NewDecoder(respBody).Decode(&entries)
	return entries, err
}
1260145
func (watcher *RecursiveWatcher) addDirectory(dir string) {
	err := watcher.WatchFlags(dir, fsnotify.FSN_ALL)
	if err != nil {
		log.Println("Error watching directory: ", dir, err)
	}
}
1082772
func (f *FastHTTPServer) RequestHandler(ctx *fasthttp.RequestCtx) {
	// This section is called if the developer has added custom mux to the app
	if f.HasAppMux && f.handleAppMux(ctx) {
		return
	}
	f.handleMux(ctx)
}
611045
func rootHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path == "/" {
		// 跳转到 /index.html
		w.Header().Set("Location", "/index.html")
		http.Error(w, "", 301)
	} else {
		http.Error(w, "404 Not Found", 404)
	}
}