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.

261 lines
6.7 KiB

9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "io"
  5. "io/ioutil"
  6. "mime"
  7. "mime/multipart"
  8. "net/http"
  9. "net/url"
  10. "path"
  11. "strconv"
  12. "strings"
  13. "github.com/chrislusf/seaweedfs/weed/filer2"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  18. path := r.URL.Path
  19. if strings.HasSuffix(path, "/") && len(path) > 1 {
  20. path = path[:len(path)-1]
  21. }
  22. entry, err := fs.filer.FindEntry(context.Background(), filer2.FullPath(path))
  23. if err != nil {
  24. if path == "/" {
  25. fs.listDirectoryHandler(w, r)
  26. return
  27. }
  28. glog.V(1).Infof("Not found %s: %v", path, err)
  29. w.WriteHeader(http.StatusNotFound)
  30. return
  31. }
  32. if entry.IsDirectory() {
  33. if fs.option.DisableDirListing {
  34. w.WriteHeader(http.StatusMethodNotAllowed)
  35. return
  36. }
  37. fs.listDirectoryHandler(w, r)
  38. return
  39. }
  40. if len(entry.Chunks) == 0 {
  41. glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
  42. w.WriteHeader(http.StatusNoContent)
  43. return
  44. }
  45. w.Header().Set("Accept-Ranges", "bytes")
  46. if r.Method == "HEAD" {
  47. w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
  48. w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
  49. setEtag(w, filer2.ETag(entry.Chunks))
  50. return
  51. }
  52. if len(entry.Chunks) == 1 {
  53. fs.handleSingleChunk(w, r, entry)
  54. return
  55. }
  56. fs.handleMultipleChunks(w, r, entry)
  57. }
  58. func (fs *FilerServer) handleSingleChunk(w http.ResponseWriter, r *http.Request, entry *filer2.Entry) {
  59. fileId := entry.Chunks[0].FileId
  60. urlString, err := fs.filer.MasterClient.LookupFileId(fileId)
  61. if err != nil {
  62. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", fileId, err)
  63. w.WriteHeader(http.StatusNotFound)
  64. return
  65. }
  66. if fs.option.RedirectOnRead {
  67. http.Redirect(w, r, urlString, http.StatusFound)
  68. return
  69. }
  70. u, _ := url.Parse(urlString)
  71. q := u.Query()
  72. for key, values := range r.URL.Query() {
  73. for _, value := range values {
  74. q.Add(key, value)
  75. }
  76. }
  77. u.RawQuery = q.Encode()
  78. request := &http.Request{
  79. Method: r.Method,
  80. URL: u,
  81. Proto: r.Proto,
  82. ProtoMajor: r.ProtoMajor,
  83. ProtoMinor: r.ProtoMinor,
  84. Header: r.Header,
  85. Body: r.Body,
  86. Host: r.Host,
  87. ContentLength: r.ContentLength,
  88. }
  89. glog.V(3).Infoln("retrieving from", u)
  90. resp, do_err := util.Do(request)
  91. if do_err != nil {
  92. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  93. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  94. return
  95. }
  96. defer func() {
  97. io.Copy(ioutil.Discard, resp.Body)
  98. resp.Body.Close()
  99. }()
  100. for k, v := range resp.Header {
  101. w.Header()[k] = v
  102. }
  103. if entry.Attr.Mime != "" {
  104. w.Header().Set("Content-Type", entry.Attr.Mime)
  105. }
  106. w.WriteHeader(resp.StatusCode)
  107. io.Copy(w, resp.Body)
  108. }
  109. func (fs *FilerServer) handleMultipleChunks(w http.ResponseWriter, r *http.Request, entry *filer2.Entry) {
  110. mimeType := entry.Attr.Mime
  111. if mimeType == "" {
  112. if ext := path.Ext(entry.Name()); ext != "" {
  113. mimeType = mime.TypeByExtension(ext)
  114. }
  115. }
  116. if mimeType != "" {
  117. w.Header().Set("Content-Type", mimeType)
  118. }
  119. setEtag(w, filer2.ETag(entry.Chunks))
  120. totalSize := int64(filer2.TotalSize(entry.Chunks))
  121. rangeReq := r.Header.Get("Range")
  122. if rangeReq == "" {
  123. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  124. if err := fs.writeContent(w, entry, 0, int(totalSize)); err != nil {
  125. http.Error(w, err.Error(), http.StatusInternalServerError)
  126. return
  127. }
  128. return
  129. }
  130. //the rest is dealing with partial content request
  131. //mostly copy from src/pkg/net/http/fs.go
  132. ranges, err := parseRange(rangeReq, totalSize)
  133. if err != nil {
  134. http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
  135. return
  136. }
  137. if sumRangesSize(ranges) > totalSize {
  138. // The total number of bytes in all the ranges
  139. // is larger than the size of the file by
  140. // itself, so this is probably an attack, or a
  141. // dumb client. Ignore the range request.
  142. return
  143. }
  144. if len(ranges) == 0 {
  145. return
  146. }
  147. if len(ranges) == 1 {
  148. // RFC 2616, Section 14.16:
  149. // "When an HTTP message includes the content of a single
  150. // range (for example, a response to a request for a
  151. // single range, or to a request for a set of ranges
  152. // that overlap without any holes), this content is
  153. // transmitted with a Content-Range header, and a
  154. // Content-Length header showing the number of bytes
  155. // actually transferred.
  156. // ...
  157. // A response to a request for a single range MUST NOT
  158. // be sent using the multipart/byteranges media type."
  159. ra := ranges[0]
  160. w.Header().Set("Content-Length", strconv.FormatInt(ra.length, 10))
  161. w.Header().Set("Content-Range", ra.contentRange(totalSize))
  162. w.WriteHeader(http.StatusPartialContent)
  163. err = fs.writeContent(w, entry, ra.start, int(ra.length))
  164. if err != nil {
  165. http.Error(w, err.Error(), http.StatusInternalServerError)
  166. return
  167. }
  168. return
  169. }
  170. // process multiple ranges
  171. for _, ra := range ranges {
  172. if ra.start > totalSize {
  173. http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
  174. return
  175. }
  176. }
  177. sendSize := rangesMIMESize(ranges, mimeType, totalSize)
  178. pr, pw := io.Pipe()
  179. mw := multipart.NewWriter(pw)
  180. w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
  181. sendContent := pr
  182. defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
  183. go func() {
  184. for _, ra := range ranges {
  185. part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
  186. if e != nil {
  187. pw.CloseWithError(e)
  188. return
  189. }
  190. if e = fs.writeContent(part, entry, ra.start, int(ra.length)); e != nil {
  191. pw.CloseWithError(e)
  192. return
  193. }
  194. }
  195. mw.Close()
  196. pw.Close()
  197. }()
  198. if w.Header().Get("Content-Encoding") == "" {
  199. w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
  200. }
  201. w.WriteHeader(http.StatusPartialContent)
  202. if _, err := io.CopyN(w, sendContent, sendSize); err != nil {
  203. http.Error(w, "Internal Error", http.StatusInternalServerError)
  204. return
  205. }
  206. }
  207. func (fs *FilerServer) writeContent(w io.Writer, entry *filer2.Entry, offset int64, size int) error {
  208. chunkViews := filer2.ViewFromChunks(entry.Chunks, offset, size)
  209. fileId2Url := make(map[string]string)
  210. for _, chunkView := range chunkViews {
  211. urlString, err := fs.filer.MasterClient.LookupFileId(chunkView.FileId)
  212. if err != nil {
  213. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  214. return err
  215. }
  216. fileId2Url[chunkView.FileId] = urlString
  217. }
  218. for _, chunkView := range chunkViews {
  219. urlString := fileId2Url[chunkView.FileId]
  220. _, err := util.ReadUrlAsStream(urlString, chunkView.Offset, int(chunkView.Size), func(data []byte) {
  221. w.Write(data)
  222. })
  223. if err != nil {
  224. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  225. return err
  226. }
  227. }
  228. return nil
  229. }