2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2014-12-14 16:20:21 +08:00
|
|
|
"github.com/mcqueenorama/weed-fs/go/glog"
|
|
|
|
"github.com/mcqueenorama/weed-fs/go/operation"
|
|
|
|
"github.com/mcqueenorama/weed-fs/go/stats"
|
|
|
|
"github.com/mcqueenorama/weed-fs/go/storage"
|
|
|
|
"github.com/mcqueenorama/weed-fs/go/util"
|
2013-12-02 17:37:36 +08:00
|
|
|
)
|
|
|
|
|
2014-03-26 04:46:59 +08:00
|
|
|
var serverStats *stats.ServerStats
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
serverStats = stats.NewServerStats()
|
|
|
|
go serverStats.Start()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) (err error) {
|
|
|
|
var bytes []byte
|
|
|
|
if r.FormValue("pretty") != "" {
|
|
|
|
bytes, err = json.MarshalIndent(obj, "", " ")
|
|
|
|
} else {
|
|
|
|
bytes, err = json.Marshal(obj)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
callback := r.FormValue("callback")
|
|
|
|
if callback == "" {
|
2014-06-14 09:16:03 +08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2013-12-02 17:37:36 +08:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
} else {
|
2014-06-14 09:16:03 +08:00
|
|
|
w.Header().Set("Content-Type", "application/javascript")
|
2013-12-02 17:37:36 +08:00
|
|
|
if _, err = w.Write([]uint8(callback)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write([]uint8("(")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, string(bytes))
|
|
|
|
if _, err = w.Write([]uint8(")")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper for writeJson - just logs errors
|
|
|
|
func writeJsonQuiet(w http.ResponseWriter, r *http.Request, obj interface{}) {
|
|
|
|
if err := writeJson(w, r, obj); err != nil {
|
|
|
|
glog.V(0).Infof("error writing JSON %s: %s", obj, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func writeJsonError(w http.ResponseWriter, r *http.Request, err error) {
|
2014-04-16 00:09:40 +08:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2013-12-02 17:37:36 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["error"] = err.Error()
|
|
|
|
writeJsonQuiet(w, r, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func debug(params ...interface{}) {
|
2014-03-31 02:28:04 +08:00
|
|
|
glog.V(4).Infoln(params)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func secure(whiteList []string, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if len(whiteList) == 0 {
|
|
|
|
f(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
if err == nil {
|
|
|
|
for _, ip := range whiteList {
|
|
|
|
if ip == host {
|
|
|
|
f(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"error": "No write permisson from " + host})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if r.Method != "POST" {
|
|
|
|
m["error"] = "Only submit via POST!"
|
|
|
|
writeJsonQuiet(w, r, m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("parsing upload file...")
|
2014-09-21 03:38:59 +08:00
|
|
|
fname, data, mimeType, isGzipped, lastModified, _, pe := storage.ParseUpload(r)
|
2013-12-02 17:37:36 +08:00
|
|
|
if pe != nil {
|
|
|
|
writeJsonError(w, r, pe)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("assigning file id for", fname)
|
2014-09-21 03:38:59 +08:00
|
|
|
assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"), r.FormValue("collection"), r.FormValue("ttl"))
|
2013-12-02 17:37:36 +08:00
|
|
|
if ae != nil {
|
|
|
|
writeJsonError(w, r, ae)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
url := "http://" + assignResult.PublicUrl + "/" + assignResult.Fid
|
|
|
|
if lastModified != 0 {
|
|
|
|
url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("upload file to store", url)
|
|
|
|
uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType)
|
|
|
|
if err != nil {
|
|
|
|
writeJsonError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m["fileName"] = fname
|
|
|
|
m["fid"] = assignResult.Fid
|
|
|
|
m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
|
|
|
|
m["size"] = uploadResult.Size
|
|
|
|
writeJsonQuiet(w, r, m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:41:34 +08:00
|
|
|
func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
|
|
|
|
r.ParseForm()
|
|
|
|
fids := r.Form["fid"]
|
2014-04-16 00:09:40 +08:00
|
|
|
ret, err := operation.DeleteFiles(masterUrl, fids)
|
|
|
|
if err != nil {
|
|
|
|
writeJsonError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, ret)
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
|
|
|
|
switch strings.Count(path, "/") {
|
|
|
|
case 3:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid, filename = parts[1], parts[2], parts[3]
|
|
|
|
ext = filepath.Ext(filename)
|
|
|
|
case 2:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid = parts[1], parts[2]
|
|
|
|
dotIndex := strings.LastIndex(fid, ".")
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = fid[dotIndex:]
|
|
|
|
fid = fid[0:dotIndex]
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
sepIndex := strings.LastIndex(path, "/")
|
|
|
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
vid, isVolumeIdOnly = path[sepIndex+1:], true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
|
|
vid = path[sepIndex+1 : commaIndex]
|
|
|
|
fid = path[commaIndex+1:]
|
|
|
|
ext = ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
fid = path[commaIndex+1 : dotIndex]
|
|
|
|
ext = path[dotIndex:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-04-14 14:41:34 +08:00
|
|
|
|
2014-03-26 04:46:59 +08:00
|
|
|
func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
2014-03-27 04:22:27 +08:00
|
|
|
m["Counters"] = serverStats
|
2014-03-26 04:46:59 +08:00
|
|
|
writeJsonQuiet(w, r, m)
|
|
|
|
}
|
2014-03-27 04:22:27 +08:00
|
|
|
|
2014-03-26 04:46:59 +08:00
|
|
|
func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
2014-03-27 04:22:27 +08:00
|
|
|
m["Memory"] = stats.MemStat()
|
2014-03-26 04:46:59 +08:00
|
|
|
writeJsonQuiet(w, r, m)
|
|
|
|
}
|