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.

265 lines
7.3 KiB

9 years ago
9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
9 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "mime"
  9. "net/http"
  10. "net/url"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/images"
  17. "github.com/chrislusf/seaweedfs/weed/operation"
  18. "github.com/chrislusf/seaweedfs/weed/stats"
  19. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  20. "github.com/chrislusf/seaweedfs/weed/util"
  21. )
  22. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  23. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  24. stats.VolumeServerRequestCounter.WithLabelValues("get").Inc()
  25. start := time.Now()
  26. defer func() { stats.VolumeServerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }()
  27. n := new(needle.Needle)
  28. vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
  29. if !vs.maybeCheckJwtAuthorization(r, vid, fid, false) {
  30. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  31. return
  32. }
  33. volumeId, err := needle.NewVolumeId(vid)
  34. if err != nil {
  35. glog.V(2).Infof("parsing vid %s: %v", r.URL.Path, err)
  36. w.WriteHeader(http.StatusBadRequest)
  37. return
  38. }
  39. err = n.ParsePath(fid)
  40. if err != nil {
  41. glog.V(2).Infof("parsing fid %s: %v", r.URL.Path, err)
  42. w.WriteHeader(http.StatusBadRequest)
  43. return
  44. }
  45. // glog.V(4).Infoln("volume", volumeId, "reading", n)
  46. hasVolume := vs.store.HasVolume(volumeId)
  47. _, hasEcVolume := vs.store.FindEcVolume(volumeId)
  48. if !hasVolume && !hasEcVolume {
  49. if !vs.ReadRedirect {
  50. glog.V(2).Infoln("volume is not local:", err, r.URL.Path)
  51. w.WriteHeader(http.StatusNotFound)
  52. return
  53. }
  54. lookupResult, err := operation.Lookup(vs.GetMaster(), volumeId.String())
  55. glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
  56. if err == nil && len(lookupResult.Locations) > 0 {
  57. u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].PublicUrl))
  58. u.Path = fmt.Sprintf("%s/%s,%s", u.Path, vid, fid)
  59. arg := url.Values{}
  60. if c := r.FormValue("collection"); c != "" {
  61. arg.Set("collection", c)
  62. }
  63. u.RawQuery = arg.Encode()
  64. http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
  65. } else {
  66. glog.V(2).Infoln("lookup error:", err, r.URL.Path)
  67. w.WriteHeader(http.StatusNotFound)
  68. }
  69. return
  70. }
  71. cookie := n.Cookie
  72. var count int
  73. if hasVolume {
  74. count, err = vs.store.ReadVolumeNeedle(volumeId, n)
  75. } else if hasEcVolume {
  76. count, err = vs.store.ReadEcShardNeedle(volumeId, n)
  77. }
  78. // glog.V(4).Infoln("read bytes", count, "error", err)
  79. if err != nil || count < 0 {
  80. glog.V(0).Infof("read %s isNormalVolume %v error: %v", r.URL.Path, hasVolume, err)
  81. w.WriteHeader(http.StatusNotFound)
  82. return
  83. }
  84. if n.Cookie != cookie {
  85. glog.V(0).Infof("request %s with cookie:%x expected:%x from %s agent %s", r.URL.Path, cookie, n.Cookie, r.RemoteAddr, r.UserAgent())
  86. w.WriteHeader(http.StatusNotFound)
  87. return
  88. }
  89. if n.LastModified != 0 {
  90. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  91. if r.Header.Get("If-Modified-Since") != "" {
  92. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  93. if t.Unix() >= int64(n.LastModified) {
  94. w.WriteHeader(http.StatusNotModified)
  95. return
  96. }
  97. }
  98. }
  99. }
  100. if inm := r.Header.Get("If-None-Match"); inm == "\""+n.Etag()+"\"" {
  101. w.WriteHeader(http.StatusNotModified)
  102. return
  103. }
  104. setEtag(w, n.Etag())
  105. if n.HasPairs() {
  106. pairMap := make(map[string]string)
  107. err = json.Unmarshal(n.Pairs, &pairMap)
  108. if err != nil {
  109. glog.V(0).Infoln("Unmarshal pairs error:", err)
  110. }
  111. for k, v := range pairMap {
  112. w.Header().Set(k, v)
  113. }
  114. }
  115. if vs.tryHandleChunkedFile(n, filename, ext, w, r) {
  116. return
  117. }
  118. if n.NameSize > 0 && filename == "" {
  119. filename = string(n.Name)
  120. if ext == "" {
  121. ext = filepath.Ext(filename)
  122. }
  123. }
  124. mtype := ""
  125. if n.MimeSize > 0 {
  126. mt := string(n.Mime)
  127. if !strings.HasPrefix(mt, "application/octet-stream") {
  128. mtype = mt
  129. }
  130. }
  131. if ext != ".gz" {
  132. if n.IsCompressed() {
  133. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  134. if _, _, _, shouldResize := shouldResizeImages(ext, r); shouldResize {
  135. if n.Data, err = util.DecompressData(n.Data); err != nil {
  136. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  137. }
  138. } else {
  139. if util.IsGzippedContent(n.Data) {
  140. w.Header().Set("Content-Encoding", "gzip")
  141. }
  142. }
  143. } else {
  144. if n.Data, err = util.DecompressData(n.Data); err != nil {
  145. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  146. }
  147. }
  148. }
  149. }
  150. rs := conditionallyResizeImages(bytes.NewReader(n.Data), ext, r)
  151. if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
  152. glog.V(2).Infoln("response write error:", e)
  153. }
  154. }
  155. func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
  156. if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
  157. return false
  158. }
  159. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  160. if e != nil {
  161. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  162. return false
  163. }
  164. if fileName == "" && chunkManifest.Name != "" {
  165. fileName = chunkManifest.Name
  166. }
  167. if ext == "" {
  168. ext = filepath.Ext(fileName)
  169. }
  170. mType := ""
  171. if chunkManifest.Mime != "" {
  172. mt := chunkManifest.Mime
  173. if !strings.HasPrefix(mt, "application/octet-stream") {
  174. mType = mt
  175. }
  176. }
  177. w.Header().Set("X-File-Store", "chunked")
  178. chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster())
  179. defer chunkedFileReader.Close()
  180. rs := conditionallyResizeImages(chunkedFileReader, ext, r)
  181. if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
  182. glog.V(2).Infoln("response write error:", e)
  183. }
  184. return true
  185. }
  186. func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  187. rs := originalDataReaderSeeker
  188. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  189. if shouldResize {
  190. rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
  191. }
  192. return rs
  193. }
  194. func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
  195. if len(ext) > 0 {
  196. ext = strings.ToLower(ext)
  197. }
  198. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
  199. if r.FormValue("width") != "" {
  200. width, _ = strconv.Atoi(r.FormValue("width"))
  201. }
  202. if r.FormValue("height") != "" {
  203. height, _ = strconv.Atoi(r.FormValue("height"))
  204. }
  205. }
  206. mode = r.FormValue("mode")
  207. shouldResize = width > 0 || height > 0
  208. return
  209. }
  210. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  211. totalSize, e := rs.Seek(0, 2)
  212. if mimeType == "" {
  213. if ext := filepath.Ext(filename); ext != "" {
  214. mimeType = mime.TypeByExtension(ext)
  215. }
  216. }
  217. if mimeType != "" {
  218. w.Header().Set("Content-Type", mimeType)
  219. }
  220. w.Header().Set("Accept-Ranges", "bytes")
  221. if r.Method == "HEAD" {
  222. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  223. return nil
  224. }
  225. adjustHeadersAfterHEAD(w, r, filename)
  226. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  227. if _, e = rs.Seek(offset, 0); e != nil {
  228. return e
  229. }
  230. _, e = io.CopyN(writer, rs, size)
  231. return e
  232. })
  233. return nil
  234. }