volume_server_handlers_read.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package weed_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  8. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  9. "io"
  10. "mime"
  11. "net/http"
  12. "net/url"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "sync/atomic"
  17. "time"
  18. "github.com/seaweedfs/seaweedfs/weed/glog"
  19. "github.com/seaweedfs/seaweedfs/weed/images"
  20. "github.com/seaweedfs/seaweedfs/weed/operation"
  21. "github.com/seaweedfs/seaweedfs/weed/storage"
  22. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  23. "github.com/seaweedfs/seaweedfs/weed/util"
  24. )
  25. var fileNameEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`)
  26. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  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.ReadMode == "local" {
  50. glog.V(0).Infoln("volume is not local:", err, r.URL.Path)
  51. w.WriteHeader(http.StatusNotFound)
  52. return
  53. }
  54. lookupResult, err := operation.LookupVolumeId(vs.GetMaster, vs.grpcDialOption, volumeId.String())
  55. glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
  56. if err != nil || len(lookupResult.Locations) <= 0 {
  57. glog.V(0).Infoln("lookup error:", err, r.URL.Path)
  58. w.WriteHeader(http.StatusNotFound)
  59. return
  60. }
  61. if vs.ReadMode == "proxy" {
  62. // proxy client request to target server
  63. u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].Url))
  64. r.URL.Host = u.Host
  65. r.URL.Scheme = u.Scheme
  66. request, err := http.NewRequest("GET", r.URL.String(), nil)
  67. if err != nil {
  68. glog.V(0).Infof("failed to instance http request of url %s: %v", r.URL.String(), err)
  69. w.WriteHeader(http.StatusInternalServerError)
  70. return
  71. }
  72. for k, vv := range r.Header {
  73. for _, v := range vv {
  74. request.Header.Add(k, v)
  75. }
  76. }
  77. response, err := client.Do(request)
  78. if err != nil {
  79. glog.V(0).Infof("request remote url %s: %v", r.URL.String(), err)
  80. w.WriteHeader(http.StatusInternalServerError)
  81. return
  82. }
  83. defer util.CloseResponse(response)
  84. // proxy target response to client
  85. for k, vv := range response.Header {
  86. for _, v := range vv {
  87. w.Header().Add(k, v)
  88. }
  89. }
  90. w.WriteHeader(response.StatusCode)
  91. buf := mem.Allocate(128 * 1024)
  92. defer mem.Free(buf)
  93. io.CopyBuffer(w, response.Body, buf)
  94. return
  95. } else {
  96. // redirect
  97. u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].PublicUrl))
  98. u.Path = fmt.Sprintf("%s/%s,%s", u.Path, vid, fid)
  99. arg := url.Values{}
  100. if c := r.FormValue("collection"); c != "" {
  101. arg.Set("collection", c)
  102. }
  103. u.RawQuery = arg.Encode()
  104. http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
  105. return
  106. }
  107. }
  108. cookie := n.Cookie
  109. readOption := &storage.ReadOption{
  110. ReadDeleted: r.FormValue("readDeleted") == "true",
  111. HasSlowRead: vs.hasSlowRead,
  112. ReadBufferSize: vs.readBufferSizeMB * 1024 * 1024,
  113. }
  114. var count int
  115. var memoryCost types.Size
  116. readOption.AttemptMetaOnly, readOption.MustMetaOnly = shouldAttemptStreamWrite(hasVolume, ext, r)
  117. onReadSizeFn := func(size types.Size) {
  118. memoryCost = size
  119. atomic.AddInt64(&vs.inFlightDownloadDataSize, int64(memoryCost))
  120. }
  121. if hasVolume {
  122. count, err = vs.store.ReadVolumeNeedle(volumeId, n, readOption, onReadSizeFn)
  123. } else if hasEcVolume {
  124. count, err = vs.store.ReadEcShardNeedle(volumeId, n, onReadSizeFn)
  125. }
  126. defer func() {
  127. atomic.AddInt64(&vs.inFlightDownloadDataSize, -int64(memoryCost))
  128. vs.inFlightDownloadDataLimitCond.Signal()
  129. }()
  130. if err != nil && err != storage.ErrorDeleted && hasVolume {
  131. glog.V(4).Infof("read needle: %v", err)
  132. // start to fix it from other replicas, if not deleted and hasVolume and is not a replicated request
  133. }
  134. // glog.V(4).Infoln("read bytes", count, "error", err)
  135. if err != nil || count < 0 {
  136. glog.V(3).Infof("read %s isNormalVolume %v error: %v", r.URL.Path, hasVolume, err)
  137. if err == storage.ErrorNotFound || err == storage.ErrorDeleted {
  138. w.WriteHeader(http.StatusNotFound)
  139. } else {
  140. w.WriteHeader(http.StatusInternalServerError)
  141. }
  142. return
  143. }
  144. if n.Cookie != cookie {
  145. 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())
  146. w.WriteHeader(http.StatusNotFound)
  147. return
  148. }
  149. if n.LastModified != 0 {
  150. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  151. if r.Header.Get("If-Modified-Since") != "" {
  152. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  153. if t.Unix() >= int64(n.LastModified) {
  154. w.WriteHeader(http.StatusNotModified)
  155. return
  156. }
  157. }
  158. }
  159. }
  160. if inm := r.Header.Get("If-None-Match"); inm == "\""+n.Etag()+"\"" {
  161. w.WriteHeader(http.StatusNotModified)
  162. return
  163. }
  164. setEtag(w, n.Etag())
  165. if n.HasPairs() {
  166. pairMap := make(map[string]string)
  167. err = json.Unmarshal(n.Pairs, &pairMap)
  168. if err != nil {
  169. glog.V(0).Infoln("Unmarshal pairs error:", err)
  170. }
  171. for k, v := range pairMap {
  172. w.Header().Set(k, v)
  173. }
  174. }
  175. if vs.tryHandleChunkedFile(n, filename, ext, w, r) {
  176. return
  177. }
  178. if n.NameSize > 0 && filename == "" {
  179. filename = string(n.Name)
  180. if ext == "" {
  181. ext = filepath.Ext(filename)
  182. }
  183. }
  184. mtype := ""
  185. if n.MimeSize > 0 {
  186. mt := string(n.Mime)
  187. if !strings.HasPrefix(mt, "application/octet-stream") {
  188. mtype = mt
  189. }
  190. }
  191. if n.IsCompressed() {
  192. if _, _, _, shouldResize := shouldResizeImages(ext, r); shouldResize {
  193. if n.Data, err = util.DecompressData(n.Data); err != nil {
  194. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  195. }
  196. // } else if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") && util.IsZstdContent(n.Data) {
  197. // w.Header().Set("Content-Encoding", "zstd")
  198. } else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && util.IsGzippedContent(n.Data) {
  199. w.Header().Set("Content-Encoding", "gzip")
  200. } else {
  201. if n.Data, err = util.DecompressData(n.Data); err != nil {
  202. glog.V(0).Infoln("uncompress error:", err, r.URL.Path)
  203. }
  204. }
  205. }
  206. if !readOption.IsMetaOnly {
  207. rs := conditionallyResizeImages(bytes.NewReader(n.Data), ext, r)
  208. if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
  209. glog.V(2).Infoln("response write error:", e)
  210. }
  211. } else {
  212. vs.streamWriteResponseContent(filename, mtype, volumeId, n, w, r, readOption)
  213. }
  214. }
  215. func shouldAttemptStreamWrite(hasLocalVolume bool, ext string, r *http.Request) (shouldAttempt bool, mustMetaOnly bool) {
  216. if !hasLocalVolume {
  217. return false, false
  218. }
  219. if len(ext) > 0 {
  220. ext = strings.ToLower(ext)
  221. }
  222. if r.Method == "HEAD" {
  223. return true, true
  224. }
  225. _, _, _, shouldResize := shouldResizeImages(ext, r)
  226. if shouldResize {
  227. return false, false
  228. }
  229. return true, false
  230. }
  231. func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
  232. if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
  233. return false
  234. }
  235. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  236. if e != nil {
  237. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  238. return false
  239. }
  240. if fileName == "" && chunkManifest.Name != "" {
  241. fileName = chunkManifest.Name
  242. }
  243. if ext == "" {
  244. ext = filepath.Ext(fileName)
  245. }
  246. mType := ""
  247. if chunkManifest.Mime != "" {
  248. mt := chunkManifest.Mime
  249. if !strings.HasPrefix(mt, "application/octet-stream") {
  250. mType = mt
  251. }
  252. }
  253. w.Header().Set("X-File-Store", "chunked")
  254. chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster(), vs.grpcDialOption)
  255. defer chunkedFileReader.Close()
  256. rs := conditionallyResizeImages(chunkedFileReader, ext, r)
  257. if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
  258. glog.V(2).Infoln("response write error:", e)
  259. }
  260. return true
  261. }
  262. func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  263. rs := originalDataReaderSeeker
  264. if len(ext) > 0 {
  265. ext = strings.ToLower(ext)
  266. }
  267. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  268. if shouldResize {
  269. rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
  270. }
  271. return rs
  272. }
  273. func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
  274. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" {
  275. if r.FormValue("width") != "" {
  276. width, _ = strconv.Atoi(r.FormValue("width"))
  277. }
  278. if r.FormValue("height") != "" {
  279. height, _ = strconv.Atoi(r.FormValue("height"))
  280. }
  281. }
  282. mode = r.FormValue("mode")
  283. shouldResize = width > 0 || height > 0
  284. return
  285. }
  286. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  287. totalSize, e := rs.Seek(0, 2)
  288. if mimeType == "" {
  289. if ext := filepath.Ext(filename); ext != "" {
  290. mimeType = mime.TypeByExtension(ext)
  291. }
  292. }
  293. if mimeType != "" {
  294. w.Header().Set("Content-Type", mimeType)
  295. }
  296. w.Header().Set("Accept-Ranges", "bytes")
  297. adjustPassthroughHeaders(w, r, filename)
  298. if r.Method == "HEAD" {
  299. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  300. return nil
  301. }
  302. return processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  303. if _, e = rs.Seek(offset, 0); e != nil {
  304. return e
  305. }
  306. _, e = io.CopyN(writer, rs, size)
  307. return e
  308. })
  309. }
  310. func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) {
  311. totalSize := int64(n.DataSize)
  312. if mimeType == "" {
  313. if ext := filepath.Ext(filename); ext != "" {
  314. mimeType = mime.TypeByExtension(ext)
  315. }
  316. }
  317. if mimeType != "" {
  318. w.Header().Set("Content-Type", mimeType)
  319. }
  320. w.Header().Set("Accept-Ranges", "bytes")
  321. adjustPassthroughHeaders(w, r, filename)
  322. if r.Method == "HEAD" {
  323. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  324. return
  325. }
  326. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  327. return vs.store.ReadVolumeNeedleDataInto(volumeId, n, readOption, writer, offset, size)
  328. })
  329. }