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.

326 lines
9.2 KiB

10 years ago
5 years ago
6 years ago
5 years ago
10 years ago
5 years ago
4 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "mime/multipart"
  8. "net/http"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "google.golang.org/grpc"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/operation"
  16. "github.com/chrislusf/seaweedfs/weed/stats"
  17. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  19. "github.com/gorilla/mux"
  20. statik "github.com/rakyll/statik/fs"
  21. _ "github.com/chrislusf/seaweedfs/weed/statik"
  22. )
  23. var serverStats *stats.ServerStats
  24. var startTime = time.Now()
  25. var statikFS http.FileSystem
  26. func init() {
  27. serverStats = stats.NewServerStats()
  28. go serverStats.Start()
  29. statikFS, _ = statik.New()
  30. }
  31. func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
  32. var bytes []byte
  33. if obj != nil {
  34. if r.FormValue("pretty") != "" {
  35. bytes, err = json.MarshalIndent(obj, "", " ")
  36. } else {
  37. bytes, err = json.Marshal(obj)
  38. }
  39. }
  40. if err != nil {
  41. return
  42. }
  43. if httpStatus >= 400 {
  44. glog.V(0).Infof("response method:%s URL:%s with httpStatus:%d and JSON:%s",
  45. r.Method, r.URL.String(), httpStatus, string(bytes))
  46. }
  47. callback := r.FormValue("callback")
  48. if callback == "" {
  49. w.Header().Set("Content-Type", "application/json")
  50. w.WriteHeader(httpStatus)
  51. if httpStatus == http.StatusNotModified {
  52. return
  53. }
  54. _, err = w.Write(bytes)
  55. } else {
  56. w.Header().Set("Content-Type", "application/javascript")
  57. w.WriteHeader(httpStatus)
  58. if httpStatus == http.StatusNotModified {
  59. return
  60. }
  61. if _, err = w.Write([]uint8(callback)); err != nil {
  62. return
  63. }
  64. if _, err = w.Write([]uint8("(")); err != nil {
  65. return
  66. }
  67. fmt.Fprint(w, string(bytes))
  68. if _, err = w.Write([]uint8(")")); err != nil {
  69. return
  70. }
  71. }
  72. return
  73. }
  74. // wrapper for writeJson - just logs errors
  75. func writeJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
  76. if err := writeJson(w, r, httpStatus, obj); err != nil {
  77. glog.V(0).Infof("error writing JSON status %d: %v", httpStatus, err)
  78. glog.V(1).Infof("JSON content: %+v", obj)
  79. }
  80. }
  81. func writeJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
  82. m := make(map[string]interface{})
  83. m["error"] = err.Error()
  84. writeJsonQuiet(w, r, httpStatus, m)
  85. }
  86. func debug(params ...interface{}) {
  87. glog.V(4).Infoln(params...)
  88. }
  89. func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string, grpcDialOption grpc.DialOption) {
  90. m := make(map[string]interface{})
  91. if r.Method != "POST" {
  92. writeJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
  93. return
  94. }
  95. debug("parsing upload file...")
  96. pu, pe := needle.ParseUpload(r, 256*1024*1024)
  97. if pe != nil {
  98. writeJsonError(w, r, http.StatusBadRequest, pe)
  99. return
  100. }
  101. debug("assigning file id for", pu.FileName)
  102. r.ParseForm()
  103. count := uint64(1)
  104. if r.FormValue("count") != "" {
  105. count, pe = strconv.ParseUint(r.FormValue("count"), 10, 32)
  106. if pe != nil {
  107. writeJsonError(w, r, http.StatusBadRequest, pe)
  108. return
  109. }
  110. }
  111. ar := &operation.VolumeAssignRequest{
  112. Count: count,
  113. DataCenter: r.FormValue("dataCenter"),
  114. Rack: r.FormValue("rack"),
  115. Replication: r.FormValue("replication"),
  116. Collection: r.FormValue("collection"),
  117. Ttl: r.FormValue("ttl"),
  118. DiskType: r.FormValue("disk"),
  119. }
  120. assignResult, ae := operation.Assign(masterUrl, grpcDialOption, ar)
  121. if ae != nil {
  122. writeJsonError(w, r, http.StatusInternalServerError, ae)
  123. return
  124. }
  125. url := "http://" + assignResult.Url + "/" + assignResult.Fid
  126. if pu.ModifiedTime != 0 {
  127. url = url + "?ts=" + strconv.FormatUint(pu.ModifiedTime, 10)
  128. }
  129. debug("upload file to store", url)
  130. uploadResult, err := operation.UploadData(url, pu.FileName, false, pu.Data, pu.IsGzipped, pu.MimeType, pu.PairMap, assignResult.Auth)
  131. if err != nil {
  132. writeJsonError(w, r, http.StatusInternalServerError, err)
  133. return
  134. }
  135. m["fileName"] = pu.FileName
  136. m["fid"] = assignResult.Fid
  137. m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
  138. m["size"] = pu.OriginalDataSize
  139. m["eTag"] = uploadResult.ETag
  140. writeJsonQuiet(w, r, http.StatusCreated, m)
  141. return
  142. }
  143. func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
  144. switch strings.Count(path, "/") {
  145. case 3:
  146. parts := strings.Split(path, "/")
  147. vid, fid, filename = parts[1], parts[2], parts[3]
  148. ext = filepath.Ext(filename)
  149. case 2:
  150. parts := strings.Split(path, "/")
  151. vid, fid = parts[1], parts[2]
  152. dotIndex := strings.LastIndex(fid, ".")
  153. if dotIndex > 0 {
  154. ext = fid[dotIndex:]
  155. fid = fid[0:dotIndex]
  156. }
  157. default:
  158. sepIndex := strings.LastIndex(path, "/")
  159. commaIndex := strings.LastIndex(path[sepIndex:], ",")
  160. if commaIndex <= 0 {
  161. vid, isVolumeIdOnly = path[sepIndex+1:], true
  162. return
  163. }
  164. dotIndex := strings.LastIndex(path[sepIndex:], ".")
  165. vid = path[sepIndex+1 : commaIndex]
  166. fid = path[commaIndex+1:]
  167. ext = ""
  168. if dotIndex > 0 {
  169. fid = path[commaIndex+1 : dotIndex]
  170. ext = path[dotIndex:]
  171. }
  172. }
  173. return
  174. }
  175. func statsHealthHandler(w http.ResponseWriter, r *http.Request) {
  176. m := make(map[string]interface{})
  177. m["Version"] = util.Version()
  178. writeJsonQuiet(w, r, http.StatusOK, m)
  179. }
  180. func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
  181. m := make(map[string]interface{})
  182. m["Version"] = util.Version()
  183. m["Counters"] = serverStats
  184. writeJsonQuiet(w, r, http.StatusOK, m)
  185. }
  186. func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
  187. m := make(map[string]interface{})
  188. m["Version"] = util.Version()
  189. m["Memory"] = stats.MemStat()
  190. writeJsonQuiet(w, r, http.StatusOK, m)
  191. }
  192. func handleStaticResources(defaultMux *http.ServeMux) {
  193. defaultMux.Handle("/favicon.ico", http.FileServer(statikFS))
  194. defaultMux.Handle("/seaweedfsstatic/", http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
  195. }
  196. func handleStaticResources2(r *mux.Router) {
  197. r.Handle("/favicon.ico", http.FileServer(statikFS))
  198. r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
  199. }
  200. func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
  201. if filename != "" {
  202. contentDisposition := "inline"
  203. if r.FormValue("dl") != "" {
  204. if dl, _ := strconv.ParseBool(r.FormValue("dl")); dl {
  205. contentDisposition = "attachment"
  206. }
  207. }
  208. w.Header().Set("Content-Disposition", contentDisposition+`; filename="`+fileNameEscaper.Replace(filename)+`"`)
  209. }
  210. }
  211. func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64, httpStatusCode int) error) {
  212. rangeReq := r.Header.Get("Range")
  213. if rangeReq == "" {
  214. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  215. if err := writeFn(w, 0, totalSize, 0); err != nil {
  216. http.Error(w, err.Error(), http.StatusInternalServerError)
  217. return
  218. }
  219. return
  220. }
  221. //the rest is dealing with partial content request
  222. //mostly copy from src/pkg/net/http/fs.go
  223. ranges, err := parseRange(rangeReq, totalSize)
  224. if err != nil {
  225. http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
  226. return
  227. }
  228. if sumRangesSize(ranges) > totalSize {
  229. // The total number of bytes in all the ranges
  230. // is larger than the size of the file by
  231. // itself, so this is probably an attack, or a
  232. // dumb client. Ignore the range request.
  233. return
  234. }
  235. if len(ranges) == 0 {
  236. return
  237. }
  238. if len(ranges) == 1 {
  239. // RFC 2616, Section 14.16:
  240. // "When an HTTP message includes the content of a single
  241. // range (for example, a response to a request for a
  242. // single range, or to a request for a set of ranges
  243. // that overlap without any holes), this content is
  244. // transmitted with a Content-Range header, and a
  245. // Content-Length header showing the number of bytes
  246. // actually transferred.
  247. // ...
  248. // A response to a request for a single range MUST NOT
  249. // be sent using the multipart/byteranges media type."
  250. ra := ranges[0]
  251. w.Header().Set("Content-Length", strconv.FormatInt(ra.length, 10))
  252. w.Header().Set("Content-Range", ra.contentRange(totalSize))
  253. err = writeFn(w, ra.start, ra.length, http.StatusPartialContent)
  254. if err != nil {
  255. http.Error(w, err.Error(), http.StatusInternalServerError)
  256. return
  257. }
  258. return
  259. }
  260. // process multiple ranges
  261. for _, ra := range ranges {
  262. if ra.start > totalSize {
  263. http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
  264. return
  265. }
  266. }
  267. sendSize := rangesMIMESize(ranges, mimeType, totalSize)
  268. pr, pw := io.Pipe()
  269. mw := multipart.NewWriter(pw)
  270. w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
  271. sendContent := pr
  272. defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
  273. go func() {
  274. for _, ra := range ranges {
  275. part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
  276. if e != nil {
  277. pw.CloseWithError(e)
  278. return
  279. }
  280. if e = writeFn(part, ra.start, ra.length, 0); e != nil {
  281. pw.CloseWithError(e)
  282. return
  283. }
  284. }
  285. mw.Close()
  286. pw.Close()
  287. }()
  288. if w.Header().Get("Content-Encoding") == "" {
  289. w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
  290. }
  291. w.WriteHeader(http.StatusPartialContent)
  292. if _, err := io.CopyN(w, sendContent, sendSize); err != nil {
  293. http.Error(w, "Internal Error", http.StatusInternalServerError)
  294. return
  295. }
  296. }