2016-12-06 18:00:24 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-12-06 16:38:19 +00:00
|
|
|
"os"
|
2016-12-06 18:00:24 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/cmd"
|
2018-02-14 20:39:11 +00:00
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib"
|
2018-02-15 15:01:19 +00:00
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib/httpflags"
|
2018-10-28 14:08:20 +00:00
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib/serve"
|
2016-12-06 18:00:24 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/accounting"
|
2017-10-27 19:56:31 +00:00
|
|
|
"github.com/ncw/rclone/vfs"
|
|
|
|
"github.com/ncw/rclone/vfs/vfsflags"
|
2016-12-06 18:00:24 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-02-15 15:01:19 +00:00
|
|
|
httpflags.AddFlags(Command.Flags())
|
2017-10-27 19:56:31 +00:00
|
|
|
vfsflags.AddFlags(Command.Flags())
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "http remote:path",
|
|
|
|
Short: `Serve the remote over HTTP.`,
|
|
|
|
Long: `rclone serve http implements a basic web server to serve the remote
|
|
|
|
over HTTP. This can be viewed in a web browser or you can make a
|
|
|
|
remote of type http read from it.
|
|
|
|
|
|
|
|
You can use the filter flags (eg --include, --exclude) to control what
|
|
|
|
is served.
|
|
|
|
|
|
|
|
The server will log errors. Use -v to see access logs.
|
|
|
|
|
|
|
|
--bwlimit will be respected for file transfers. Use --stats to
|
|
|
|
control the stats printing.
|
2018-02-14 20:39:11 +00:00
|
|
|
` + httplib.Help + vfs.Help,
|
2016-12-06 18:00:24 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f := cmd.NewFsSrc(args)
|
|
|
|
cmd.Run(false, true, command, func() error {
|
2019-06-13 09:51:16 +00:00
|
|
|
s := newServer(f, &httpflags.Opt)
|
2018-11-01 17:16:31 +00:00
|
|
|
err := s.Serve()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Wait()
|
2016-12-06 18:00:24 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
// server contains everything to run the server
|
|
|
|
type server struct {
|
2018-11-01 17:16:31 +00:00
|
|
|
*httplib.Server
|
2018-02-14 20:39:11 +00:00
|
|
|
f fs.Fs
|
|
|
|
vfs *vfs.VFS
|
2017-10-27 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
func newServer(f fs.Fs, opt *httplib.Options) *server {
|
2018-02-14 20:39:11 +00:00
|
|
|
mux := http.NewServeMux()
|
2019-06-13 09:51:16 +00:00
|
|
|
s := &server{
|
2018-11-01 17:16:31 +00:00
|
|
|
Server: httplib.NewServer(mux, opt),
|
|
|
|
f: f,
|
|
|
|
vfs: vfs.New(f, &vfsflags.Opt),
|
2017-10-27 19:56:31 +00:00
|
|
|
}
|
2019-06-13 09:51:16 +00:00
|
|
|
mux.HandleFunc("/", s.handler)
|
2017-10-27 19:56:31 +00:00
|
|
|
return s
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
// Serve runs the http server in the background.
|
2018-11-01 17:16:31 +00:00
|
|
|
//
|
2019-06-13 09:51:16 +00:00
|
|
|
// Use s.Close() and s.Wait() to shutdown server
|
|
|
|
func (s *server) Serve() error {
|
2018-11-01 17:16:31 +00:00
|
|
|
err := s.Server.Serve()
|
2018-04-04 13:56:26 +00:00
|
|
|
if err != nil {
|
2018-11-01 17:16:31 +00:00
|
|
|
return err
|
2018-04-04 13:56:26 +00:00
|
|
|
}
|
2018-11-01 17:16:31 +00:00
|
|
|
fs.Logf(s.f, "Serving on %s", s.URL())
|
|
|
|
return nil
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
// handler reads incoming requests and dispatches them
|
|
|
|
func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
2016-12-06 18:00:24 +00:00
|
|
|
if r.Method != "GET" && r.Method != "HEAD" {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:56:31 +00:00
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
2017-10-24 22:17:04 +00:00
|
|
|
w.Header().Set("Server", "rclone/"+fs.Version)
|
|
|
|
|
2016-12-06 18:00:24 +00:00
|
|
|
urlPath := r.URL.Path
|
|
|
|
isDir := strings.HasSuffix(urlPath, "/")
|
|
|
|
remote := strings.Trim(urlPath, "/")
|
|
|
|
if isDir {
|
|
|
|
s.serveDir(w, r, remote)
|
|
|
|
} else {
|
|
|
|
s.serveFile(w, r, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveDir serves a directory index at dirRemote
|
2019-06-13 09:51:16 +00:00
|
|
|
func (s *server) serveDir(w http.ResponseWriter, r *http.Request, dirRemote string) {
|
2016-12-06 18:00:24 +00:00
|
|
|
// List the directory
|
2017-10-27 19:56:31 +00:00
|
|
|
node, err := s.vfs.Stat(dirRemote)
|
|
|
|
if err == vfs.ENOENT {
|
2016-12-06 18:00:24 +00:00
|
|
|
http.Error(w, "Directory not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:56:31 +00:00
|
|
|
if !node.IsDir() {
|
|
|
|
http.Error(w, "Not a directory", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := node.(*vfs.Dir)
|
|
|
|
dirEntries, err := dir.ReadDirAll()
|
|
|
|
if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2017-10-27 19:56:31 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2018-10-28 14:08:20 +00:00
|
|
|
// Make the entries for display
|
2018-12-23 00:16:50 +00:00
|
|
|
directory := serve.NewDirectory(dirRemote, s.HTMLTemplate)
|
2017-10-27 19:56:31 +00:00
|
|
|
for _, node := range dirEntries {
|
2018-10-28 14:08:20 +00:00
|
|
|
directory.AddEntry(node.Path(), node.IsDir())
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 14:08:20 +00:00
|
|
|
directory.Serve(w, r)
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serveFile serves a file object at remote
|
2019-06-13 09:51:16 +00:00
|
|
|
func (s *server) serveFile(w http.ResponseWriter, r *http.Request, remote string) {
|
2017-10-27 19:56:31 +00:00
|
|
|
node, err := s.vfs.Stat(remote)
|
|
|
|
if err == vfs.ENOENT {
|
2016-12-06 18:00:24 +00:00
|
|
|
fs.Infof(remote, "%s: File not found", r.RemoteAddr)
|
|
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(remote, w, "Failed to find file", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:56:31 +00:00
|
|
|
if !node.IsFile() {
|
|
|
|
http.Error(w, "Not a file", http.StatusNotFound)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-18 11:47:21 +00:00
|
|
|
entry := node.DirEntry()
|
|
|
|
if entry == nil {
|
|
|
|
http.Error(w, "Can't open file being written", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := entry.(fs.Object)
|
2017-10-27 19:56:31 +00:00
|
|
|
file := node.(*vfs.File)
|
2016-12-06 18:00:24 +00:00
|
|
|
|
|
|
|
// Set content length since we know how long the object is
|
2017-10-27 19:56:31 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(node.Size(), 10))
|
2016-12-06 18:00:24 +00:00
|
|
|
|
|
|
|
// Set content type
|
2019-06-17 08:34:30 +00:00
|
|
|
mimeType := fs.MimeType(r.Context(), obj)
|
2016-12-06 18:00:24 +00:00
|
|
|
if mimeType == "application/octet-stream" && path.Ext(remote) == "" {
|
|
|
|
// Leave header blank so http server guesses
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If HEAD no need to read the object since we have set the headers
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the object
|
2017-12-06 16:38:19 +00:00
|
|
|
in, err := file.Open(os.O_RDONLY)
|
2016-12-06 18:00:24 +00:00
|
|
|
if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(remote, w, "Failed to open file", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := in.Close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(remote, "Failed to close file: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Account the transfer
|
2018-01-12 16:30:54 +00:00
|
|
|
accounting.Stats.Transferring(remote)
|
|
|
|
defer accounting.Stats.DoneTransferring(remote, true)
|
2017-10-27 19:56:31 +00:00
|
|
|
// FIXME in = fs.NewAccount(in, obj).WithBuffer() // account the transfer
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2017-10-27 19:56:31 +00:00
|
|
|
// Serve the file
|
|
|
|
http.ServeContent(w, r, remote, node.ModTime(), in)
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|