You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.6 KiB

  1. package weed_server
  2. import (
  3. "bytes"
  4. "code.google.com/p/weed-fs/go/glog"
  5. "code.google.com/p/weed-fs/go/operation"
  6. "code.google.com/p/weed-fs/go/stats"
  7. "code.google.com/p/weed-fs/go/storage"
  8. "code.google.com/p/weed-fs/go/util"
  9. "encoding/json"
  10. "fmt"
  11. "net"
  12. "net/http"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. )
  17. var serverStats *stats.ServerStats
  18. func init() {
  19. serverStats = stats.NewServerStats()
  20. go serverStats.Start()
  21. }
  22. func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) (err error) {
  23. w.Header().Set("Content-Type", "application/javascript")
  24. var bytes []byte
  25. if r.FormValue("pretty") != "" {
  26. bytes, err = json.MarshalIndent(obj, "", " ")
  27. } else {
  28. bytes, err = json.Marshal(obj)
  29. }
  30. if err != nil {
  31. return
  32. }
  33. callback := r.FormValue("callback")
  34. if callback == "" {
  35. _, err = w.Write(bytes)
  36. } else {
  37. if _, err = w.Write([]uint8(callback)); err != nil {
  38. return
  39. }
  40. if _, err = w.Write([]uint8("(")); err != nil {
  41. return
  42. }
  43. fmt.Fprint(w, string(bytes))
  44. if _, err = w.Write([]uint8(")")); err != nil {
  45. return
  46. }
  47. }
  48. return
  49. }
  50. // wrapper for writeJson - just logs errors
  51. func writeJsonQuiet(w http.ResponseWriter, r *http.Request, obj interface{}) {
  52. if err := writeJson(w, r, obj); err != nil {
  53. glog.V(0).Infof("error writing JSON %s: %s", obj, err.Error())
  54. }
  55. }
  56. func writeJsonError(w http.ResponseWriter, r *http.Request, err error) {
  57. w.WriteHeader(http.StatusInternalServerError)
  58. m := make(map[string]interface{})
  59. m["error"] = err.Error()
  60. writeJsonQuiet(w, r, m)
  61. }
  62. func debug(params ...interface{}) {
  63. glog.V(4).Infoln(params)
  64. }
  65. func secure(whiteList []string, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  66. return func(w http.ResponseWriter, r *http.Request) {
  67. if len(whiteList) == 0 {
  68. f(w, r)
  69. return
  70. }
  71. host, _, err := net.SplitHostPort(r.RemoteAddr)
  72. if err == nil {
  73. for _, ip := range whiteList {
  74. if ip == host {
  75. f(w, r)
  76. return
  77. }
  78. }
  79. }
  80. writeJsonQuiet(w, r, map[string]interface{}{"error": "No write permisson from " + host})
  81. }
  82. }
  83. func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  84. m := make(map[string]interface{})
  85. if r.Method != "POST" {
  86. m["error"] = "Only submit via POST!"
  87. writeJsonQuiet(w, r, m)
  88. return
  89. }
  90. debug("parsing upload file...")
  91. fname, data, mimeType, isGzipped, lastModified, pe := storage.ParseUpload(r)
  92. if pe != nil {
  93. writeJsonError(w, r, pe)
  94. return
  95. }
  96. debug("assigning file id for", fname)
  97. assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"), r.FormValue("collection"))
  98. if ae != nil {
  99. writeJsonError(w, r, ae)
  100. return
  101. }
  102. url := "http://" + assignResult.PublicUrl + "/" + assignResult.Fid
  103. if lastModified != 0 {
  104. url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
  105. }
  106. debug("upload file to store", url)
  107. uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType)
  108. if err != nil {
  109. writeJsonError(w, r, err)
  110. return
  111. }
  112. m["fileName"] = fname
  113. m["fid"] = assignResult.Fid
  114. m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
  115. m["size"] = uploadResult.Size
  116. writeJsonQuiet(w, r, m)
  117. return
  118. }
  119. func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  120. r.ParseForm()
  121. fids := r.Form["fid"]
  122. ret, err := operation.DeleteFiles(masterUrl, fids)
  123. if err != nil {
  124. writeJsonError(w, r, err)
  125. return
  126. }
  127. writeJsonQuiet(w, r, ret)
  128. }
  129. func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
  130. switch strings.Count(path, "/") {
  131. case 3:
  132. parts := strings.Split(path, "/")
  133. vid, fid, filename = parts[1], parts[2], parts[3]
  134. ext = filepath.Ext(filename)
  135. case 2:
  136. parts := strings.Split(path, "/")
  137. vid, fid = parts[1], parts[2]
  138. dotIndex := strings.LastIndex(fid, ".")
  139. if dotIndex > 0 {
  140. ext = fid[dotIndex:]
  141. fid = fid[0:dotIndex]
  142. }
  143. default:
  144. sepIndex := strings.LastIndex(path, "/")
  145. commaIndex := strings.LastIndex(path[sepIndex:], ",")
  146. if commaIndex <= 0 {
  147. vid, isVolumeIdOnly = path[sepIndex+1:], true
  148. return
  149. }
  150. dotIndex := strings.LastIndex(path[sepIndex:], ".")
  151. vid = path[sepIndex+1 : commaIndex]
  152. fid = path[commaIndex+1:]
  153. ext = ""
  154. if dotIndex > 0 {
  155. fid = path[commaIndex+1 : dotIndex]
  156. ext = path[dotIndex:]
  157. }
  158. }
  159. return
  160. }
  161. func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
  162. m := make(map[string]interface{})
  163. m["Version"] = util.VERSION
  164. m["Counters"] = serverStats
  165. writeJsonQuiet(w, r, m)
  166. }
  167. func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
  168. m := make(map[string]interface{})
  169. m["Version"] = util.VERSION
  170. m["Memory"] = stats.MemStat()
  171. writeJsonQuiet(w, r, m)
  172. }