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
  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.GetMaster(), 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. if n.Cookie != cookie {
  69. glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
  70. w.WriteHeader(http.StatusNotFound)
  71. return
  72. }
  73. if n.LastModified != 0 {
  74. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  75. if r.Header.Get("If-Modified-Since") != "" {
  76. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  77. if t.Unix() >= int64(n.LastModified) {
  78. w.WriteHeader(http.StatusNotModified)
  79. return
  80. }
  81. }
  82. }
  83. }
  84. etag := n.Etag()
  85. if inm := r.Header.Get("If-None-Match"); inm == etag {
  86. w.WriteHeader(http.StatusNotModified)
  87. return
  88. }
  89. w.Header().Set("Etag", etag)
  90. if n.HasPairs() {
  91. pairMap := make(map[string]string)
  92. err = json.Unmarshal(n.Pairs, &pairMap)
  93. if err != nil {
  94. glog.V(0).Infoln("Unmarshal pairs error:", err)
  95. }
  96. for k, v := range pairMap {
  97. w.Header().Set(k, v)
  98. }
  99. }
  100. if vs.tryHandleChunkedFile(n, filename, w, r) {
  101. return
  102. }
  103. if n.NameSize > 0 && filename == "" {
  104. filename = string(n.Name)
  105. if ext == "" {
  106. ext = path.Ext(filename)
  107. }
  108. }
  109. mtype := ""
  110. if n.MimeSize > 0 {
  111. mt := string(n.Mime)
  112. if !strings.HasPrefix(mt, "application/octet-stream") {
  113. mtype = mt
  114. }
  115. }
  116. if ext != ".gz" {
  117. if n.IsGzipped() {
  118. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  119. w.Header().Set("Content-Encoding", "gzip")
  120. } else {
  121. if n.Data, err = operation.UnGzipData(n.Data); err != nil {
  122. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  123. }
  124. }
  125. }
  126. }
  127. rs := conditionallyResizeImages(bytes.NewReader(n.Data), ext, r)
  128. if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
  129. glog.V(2).Infoln("response write error:", e)
  130. }
  131. }
  132. func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string, w http.ResponseWriter, r *http.Request) (processed bool) {
  133. if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
  134. return false
  135. }
  136. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  137. if e != nil {
  138. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  139. return false
  140. }
  141. if fileName == "" && chunkManifest.Name != "" {
  142. fileName = chunkManifest.Name
  143. }
  144. ext := path.Ext(fileName)
  145. mType := ""
  146. if chunkManifest.Mime != "" {
  147. mt := chunkManifest.Mime
  148. if !strings.HasPrefix(mt, "application/octet-stream") {
  149. mType = mt
  150. }
  151. }
  152. w.Header().Set("X-File-Store", "chunked")
  153. chunkedFileReader := &operation.ChunkedFileReader{
  154. Manifest: chunkManifest,
  155. Master: vs.GetMaster(),
  156. }
  157. defer chunkedFileReader.Close()
  158. rs := conditionallyResizeImages(chunkedFileReader, ext, r)
  159. if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
  160. glog.V(2).Infoln("response write error:", e)
  161. }
  162. return true
  163. }
  164. func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  165. rs := originalDataReaderSeeker
  166. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
  167. width, height := 0, 0
  168. if r.FormValue("width") != "" {
  169. width, _ = strconv.Atoi(r.FormValue("width"))
  170. }
  171. if r.FormValue("height") != "" {
  172. height, _ = strconv.Atoi(r.FormValue("height"))
  173. }
  174. rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, r.FormValue("mode"))
  175. }
  176. return rs
  177. }
  178. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  179. totalSize, e := rs.Seek(0, 2)
  180. if mimeType == "" {
  181. if ext := path.Ext(filename); ext != "" {
  182. mimeType = mime.TypeByExtension(ext)
  183. }
  184. }
  185. if mimeType != "" {
  186. w.Header().Set("Content-Type", mimeType)
  187. }
  188. if filename != "" {
  189. contentDisposition := "inline"
  190. if r.FormValue("dl") != "" {
  191. if dl, _ := strconv.ParseBool(r.FormValue("dl")); dl {
  192. contentDisposition = "attachment"
  193. }
  194. }
  195. w.Header().Set("Content-Disposition", contentDisposition+`; filename="`+fileNameEscaper.Replace(filename)+`"`)
  196. }
  197. w.Header().Set("Accept-Ranges", "bytes")
  198. if r.Method == "HEAD" {
  199. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  200. return nil
  201. }
  202. rangeReq := r.Header.Get("Range")
  203. if rangeReq == "" {
  204. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  205. if _, e = rs.Seek(0, 0); e != nil {
  206. return e
  207. }
  208. _, e = io.Copy(w, rs)
  209. return e
  210. }
  211. //the rest is dealing with partial content request
  212. //mostly copy from src/pkg/net/http/fs.go
  213. ranges, err := parseRange(rangeReq, totalSize)
  214. if err != nil {
  215. http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
  216. return nil
  217. }
  218. if sumRangesSize(ranges) > totalSize {
  219. // The total number of bytes in all the ranges
  220. // is larger than the size of the file by
  221. // itself, so this is probably an attack, or a
  222. // dumb client. Ignore the range request.
  223. return nil
  224. }
  225. if len(ranges) == 0 {
  226. return nil
  227. }
  228. if len(ranges) == 1 {
  229. // RFC 2616, Section 14.16:
  230. // "When an HTTP message includes the content of a single
  231. // range (for example, a response to a request for a
  232. // single range, or to a request for a set of ranges
  233. // that overlap without any holes), this content is
  234. // transmitted with a Content-Range header, and a
  235. // Content-Length header showing the number of bytes
  236. // actually transferred.
  237. // ...
  238. // A response to a request for a single range MUST NOT
  239. // be sent using the multipart/byteranges media type."
  240. ra := ranges[0]
  241. w.Header().Set("Content-Length", strconv.FormatInt(ra.length, 10))
  242. w.Header().Set("Content-Range", ra.contentRange(totalSize))
  243. w.WriteHeader(http.StatusPartialContent)
  244. if _, e = rs.Seek(ra.start, 0); e != nil {
  245. return e
  246. }
  247. _, e = io.CopyN(w, rs, ra.length)
  248. return e
  249. }
  250. // process multiple ranges
  251. for _, ra := range ranges {
  252. if ra.start > totalSize {
  253. http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
  254. return nil
  255. }
  256. }
  257. sendSize := rangesMIMESize(ranges, mimeType, totalSize)
  258. pr, pw := io.Pipe()
  259. mw := multipart.NewWriter(pw)
  260. w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
  261. sendContent := pr
  262. defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
  263. go func() {
  264. for _, ra := range ranges {
  265. part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
  266. if e != nil {
  267. pw.CloseWithError(e)
  268. return
  269. }
  270. if _, e = rs.Seek(ra.start, 0); e != nil {
  271. pw.CloseWithError(e)
  272. return
  273. }
  274. if _, e = io.CopyN(part, rs, ra.length); e != nil {
  275. pw.CloseWithError(e)
  276. return
  277. }
  278. }
  279. mw.Close()
  280. pw.Close()
  281. }()
  282. if w.Header().Get("Content-Encoding") == "" {
  283. w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
  284. }
  285. w.WriteHeader(http.StatusPartialContent)
  286. _, e = io.CopyN(w, sendContent, sendSize)
  287. return e
  288. }