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.

313 lines
8.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "io"
  5. "mime"
  6. "mime/multipart"
  7. "net/http"
  8. "net/url"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "encoding/json"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/images"
  16. "github.com/chrislusf/seaweedfs/weed/operation"
  17. "github.com/chrislusf/seaweedfs/weed/storage"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  19. )
  20. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  21. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  22. n := new(storage.Needle)
  23. vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
  24. volumeId, err := storage.NewVolumeId(vid)
  25. if err != nil {
  26. glog.V(2).Infoln("parsing error:", err, r.URL.Path)
  27. w.WriteHeader(http.StatusBadRequest)
  28. return
  29. }
  30. err = n.ParsePath(fid)
  31. if err != nil {
  32. glog.V(2).Infoln("parsing fid error:", err, r.URL.Path)
  33. w.WriteHeader(http.StatusBadRequest)
  34. return
  35. }
  36. glog.V(4).Infoln("volume", volumeId, "reading", n)
  37. if !vs.store.HasVolume(volumeId) {
  38. if !vs.ReadRedirect {
  39. glog.V(2).Infoln("volume is not local:", err, r.URL.Path)
  40. w.WriteHeader(http.StatusNotFound)
  41. return
  42. }
  43. lookupResult, err := operation.Lookup(vs.GetMasterNode(), volumeId.String())
  44. glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
  45. if err == nil && len(lookupResult.Locations) > 0 {
  46. u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].PublicUrl))
  47. u.Path = r.URL.Path
  48. arg := url.Values{}
  49. if c := r.FormValue("collection"); c != "" {
  50. arg.Set("collection", c)
  51. }
  52. u.RawQuery = arg.Encode()
  53. http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
  54. } else {
  55. glog.V(2).Infoln("lookup error:", err, r.URL.Path)
  56. w.WriteHeader(http.StatusNotFound)
  57. }
  58. return
  59. }
  60. cookie := n.Cookie
  61. count, e := vs.store.ReadVolumeNeedle(volumeId, n)
  62. glog.V(4).Infoln("read bytes", count, "error", e)
  63. if e != nil || count <= 0 {
  64. glog.V(0).Infoln("read error:", e, r.URL.Path)
  65. w.WriteHeader(http.StatusNotFound)
  66. return
  67. }
  68. defer n.ReleaseMemory()
  69. if n.Cookie != cookie {
  70. glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
  71. w.WriteHeader(http.StatusNotFound)
  72. return
  73. }
  74. if n.LastModified != 0 {
  75. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  76. if r.Header.Get("If-Modified-Since") != "" {
  77. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  78. if t.Unix() >= int64(n.LastModified) {
  79. w.WriteHeader(http.StatusNotModified)
  80. return
  81. }
  82. }
  83. }
  84. }
  85. etag := n.Etag()
  86. if inm := r.Header.Get("If-None-Match"); inm == etag {
  87. w.WriteHeader(http.StatusNotModified)
  88. return
  89. }
  90. w.Header().Set("Etag", etag)
  91. if n.HasPairs() {
  92. pairMap := make(map[string]string)
  93. err = json.Unmarshal(n.Pairs, &pairMap)
  94. if err != nil {
  95. glog.V(0).Infoln("Unmarshal pairs error:", err)
  96. }
  97. for k, v := range pairMap {
  98. w.Header().Set(k, v)
  99. }
  100. }
  101. if vs.tryHandleChunkedFile(n, filename, w, r) {
  102. return
  103. }
  104. if n.NameSize > 0 && filename == "" {
  105. filename = string(n.Name)
  106. if ext == "" {
  107. ext = path.Ext(filename)
  108. }
  109. }
  110. mtype := ""
  111. if n.MimeSize > 0 {
  112. mt := string(n.Mime)
  113. if !strings.HasPrefix(mt, "application/octet-stream") {
  114. mtype = mt
  115. }
  116. }
  117. if ext != ".gz" {
  118. if n.IsGzipped() {
  119. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  120. w.Header().Set("Content-Encoding", "gzip")
  121. } else {
  122. if n.Data, err = operation.UnGzipData(n.Data); err != nil {
  123. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  124. }
  125. }
  126. }
  127. }
  128. if ext == ".png" || ext == ".jpg" || ext == ".gif" {
  129. width, height := 0, 0
  130. if r.FormValue("width") != "" {
  131. width, _ = strconv.Atoi(r.FormValue("width"))
  132. }
  133. if r.FormValue("height") != "" {
  134. height, _ = strconv.Atoi(r.FormValue("height"))
  135. }
  136. n.Data, _, _ = images.Resized(ext, n.Data, width, height)
  137. }
  138. if e := writeResponseContent(filename, mtype, bytes.NewReader(n.Data), w, r); e != nil {
  139. glog.V(2).Infoln("response write error:", e)
  140. }
  141. }
  142. func (vs *VolumeServer) FaviconHandler(w http.ResponseWriter, r *http.Request) {
  143. data, err := images.Asset("favicon/favicon.ico")
  144. if err != nil {
  145. glog.V(2).Infoln("favicon read error:", err)
  146. return
  147. }
  148. if e := writeResponseContent("favicon.ico", "image/x-icon", bytes.NewReader(data), w, r); e != nil {
  149. glog.V(2).Infoln("response write error:", e)
  150. }
  151. }
  152. func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string, w http.ResponseWriter, r *http.Request) (processed bool) {
  153. if !n.IsChunkedManifest() {
  154. return false
  155. }
  156. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  157. if e != nil {
  158. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  159. return false
  160. }
  161. if fileName == "" && chunkManifest.Name != "" {
  162. fileName = chunkManifest.Name
  163. }
  164. mType := ""
  165. if chunkManifest.Mime != "" {
  166. mt := chunkManifest.Mime
  167. if !strings.HasPrefix(mt, "application/octet-stream") {
  168. mType = mt
  169. }
  170. }
  171. w.Header().Set("X-File-Store", "chunked")
  172. chunkedFileReader := &operation.ChunkedFileReader{
  173. Manifest: chunkManifest,
  174. Master: vs.GetMasterNode(),
  175. }
  176. defer chunkedFileReader.Close()
  177. if e := writeResponseContent(fileName, mType, chunkedFileReader, w, r); e != nil {
  178. glog.V(2).Infoln("response write error:", e)
  179. }
  180. return true
  181. }
  182. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  183. totalSize, e := rs.Seek(0, 2)
  184. if mimeType == "" {
  185. if ext := path.Ext(filename); ext != "" {
  186. mimeType = mime.TypeByExtension(ext)
  187. }
  188. }
  189. if mimeType != "" {
  190. w.Header().Set("Content-Type", mimeType)
  191. }
  192. if filename != "" {
  193. contentDisposition := "inline"
  194. if r.FormValue("dl") != "" {
  195. if dl, _ := strconv.ParseBool(r.FormValue("dl")); dl {
  196. contentDisposition = "attachment"
  197. }
  198. }
  199. w.Header().Set("Content-Disposition", contentDisposition+`; filename="`+fileNameEscaper.Replace(filename)+`"`)
  200. }
  201. w.Header().Set("Accept-Ranges", "bytes")
  202. if r.Method == "HEAD" {
  203. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  204. return nil
  205. }
  206. rangeReq := r.Header.Get("Range")
  207. if rangeReq == "" {
  208. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  209. if _, e = rs.Seek(0, 0); e != nil {
  210. return e
  211. }
  212. _, e = io.Copy(w, rs)
  213. return e
  214. }
  215. //the rest is dealing with partial content request
  216. //mostly copy from src/pkg/net/http/fs.go
  217. ranges, err := parseRange(rangeReq, totalSize)
  218. if err != nil {
  219. http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
  220. return nil
  221. }
  222. if sumRangesSize(ranges) > totalSize {
  223. // The total number of bytes in all the ranges
  224. // is larger than the size of the file by
  225. // itself, so this is probably an attack, or a
  226. // dumb client. Ignore the range request.
  227. return nil
  228. }
  229. if len(ranges) == 0 {
  230. return nil
  231. }
  232. if len(ranges) == 1 {
  233. // RFC 2616, Section 14.16:
  234. // "When an HTTP message includes the content of a single
  235. // range (for example, a response to a request for a
  236. // single range, or to a request for a set of ranges
  237. // that overlap without any holes), this content is
  238. // transmitted with a Content-Range header, and a
  239. // Content-Length header showing the number of bytes
  240. // actually transferred.
  241. // ...
  242. // A response to a request for a single range MUST NOT
  243. // be sent using the multipart/byteranges media type."
  244. ra := ranges[0]
  245. w.Header().Set("Content-Length", strconv.FormatInt(ra.length, 10))
  246. w.Header().Set("Content-Range", ra.contentRange(totalSize))
  247. w.WriteHeader(http.StatusPartialContent)
  248. if _, e = rs.Seek(ra.start, 0); e != nil {
  249. return e
  250. }
  251. _, e = io.CopyN(w, rs, ra.length)
  252. return e
  253. }
  254. // process multiple ranges
  255. for _, ra := range ranges {
  256. if ra.start > totalSize {
  257. http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
  258. return nil
  259. }
  260. }
  261. sendSize := rangesMIMESize(ranges, mimeType, totalSize)
  262. pr, pw := io.Pipe()
  263. mw := multipart.NewWriter(pw)
  264. w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
  265. sendContent := pr
  266. defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
  267. go func() {
  268. for _, ra := range ranges {
  269. part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
  270. if e != nil {
  271. pw.CloseWithError(e)
  272. return
  273. }
  274. if _, e = rs.Seek(ra.start, 0); e != nil {
  275. pw.CloseWithError(e)
  276. return
  277. }
  278. if _, e = io.CopyN(part, rs, ra.length); e != nil {
  279. pw.CloseWithError(e)
  280. return
  281. }
  282. }
  283. mw.Close()
  284. pw.Close()
  285. }()
  286. if w.Header().Get("Content-Encoding") == "" {
  287. w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
  288. }
  289. w.WriteHeader(http.StatusPartialContent)
  290. _, e = io.CopyN(w, sendContent, sendSize)
  291. return e
  292. }