From 13b0cc6b833f458b2ce8a6f5eb56e24f45e2eed1 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 28 May 2011 18:31:24 -0400 Subject: [PATCH] Add backend support for downloading files --- src/paths.go | 6 ++++++ src/server.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/paths.go b/src/paths.go index 617d5a9..522b3f6 100644 --- a/src/paths.go +++ b/src/paths.go @@ -41,6 +41,12 @@ func checkInJail(the_path string) bool { return true } +func IsValid(path string) (bool, string) { + path = canonicalizePath(path) + _, err := os.Lstat(path) + return err == nil && checkInJail(path), path +} + func List(the_path string) (files vector.StringVector, err os.Error) { full_path := canonicalizePath(the_path) if !checkInJail(full_path) { diff --git a/src/server.go b/src/server.go index 8d8adf3..3ff4705 100644 --- a/src/server.go +++ b/src/server.go @@ -84,6 +84,18 @@ func serviceHandler(response http.ResponseWriter, request *http.Request) { } okResponse(response, data) } + case "download": + valid, fullPath := paths.IsValid(request.FormValue("path")) + if valid { + info, _ := os.Lstat(fullPath) // Error is already checked by |valid|. + if info.IsDirectory() { + errorResponse(response, "File is a directory") + return + } + http.ServeFile(response, request, fullPath) + } else { + errorResponse(response, "Invalid path") + } default: fmt.Printf("Invalid action: '%s'\n", request.FormValue("action")) errorResponse(response, "Unhandled action") -- 2.43.5