volume_server_handlers_read.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "mime"
  10. "net/http"
  11. "net/url"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. "sync/atomic"
  16. "time"
  17. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  18. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  19. "github.com/seaweedfs/seaweedfs/weed/filer"
  20. "github.com/seaweedfs/seaweedfs/weed/glog"
  21. "github.com/seaweedfs/seaweedfs/weed/images"
  22. "github.com/seaweedfs/seaweedfs/weed/operation"
  23. "github.com/seaweedfs/seaweedfs/weed/stats"
  24. "github.com/seaweedfs/seaweedfs/weed/storage"
  25. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  26. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  27. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  28. "github.com/seaweedfs/seaweedfs/weed/util"
  29. )
  30. const reqIsProxied = "proxied"
  31. var fileNameEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`)
  32. func NotFound(w http.ResponseWriter) {
  33. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorGetNotFound).Inc()
  34. w.WriteHeader(http.StatusNotFound)
  35. }
  36. func InternalError(w http.ResponseWriter) {
  37. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorGetInternal).Inc()
  38. w.WriteHeader(http.StatusInternalServerError)
  39. }
  40. func (vs *VolumeServer) proxyReqToTargetServer(w http.ResponseWriter, r *http.Request) {
  41. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  42. volumeId, err := needle.NewVolumeId(vid)
  43. if err != nil {
  44. glog.V(2).Infof("parsing vid %s: %v", r.URL.Path, err)
  45. w.WriteHeader(http.StatusBadRequest)
  46. return
  47. }
  48. lookupResult, err := operation.LookupVolumeId(vs.GetMaster, vs.grpcDialOption, volumeId.String())
  49. if err != nil || len(lookupResult.Locations) <= 0 {
  50. glog.V(0).Infoln("lookup error:", err, r.URL.Path)
  51. NotFound(w)
  52. return
  53. }
  54. var tragetUrl *url.URL
  55. location := fmt.Sprintf("%s:%d", vs.store.Ip, vs.store.Port)
  56. for _, loc := range lookupResult.Locations {
  57. if !strings.Contains(loc.Url, location) {
  58. rawURL, _ := util_http.NormalizeUrl(loc.Url)
  59. tragetUrl, _ = url.Parse(rawURL)
  60. break
  61. }
  62. }
  63. if tragetUrl == nil {
  64. stats.VolumeServerHandlerCounter.WithLabelValues(stats.EmptyReadProxyLoc).Inc()
  65. glog.Errorf("failed lookup target host is empty locations: %+v, %s", lookupResult.Locations, location)
  66. NotFound(w)
  67. return
  68. }
  69. if vs.ReadMode == "proxy" {
  70. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ReadProxyReq).Inc()
  71. // proxy client request to target server
  72. r.URL.Host = tragetUrl.Host
  73. r.URL.Scheme = tragetUrl.Scheme
  74. r.URL.Query().Add(reqIsProxied, "true")
  75. request, err := http.NewRequest(http.MethodGet, r.URL.String(), nil)
  76. if err != nil {
  77. glog.V(0).Infof("failed to instance http request of url %s: %v", r.URL.String(), err)
  78. InternalError(w)
  79. return
  80. }
  81. for k, vv := range r.Header {
  82. for _, v := range vv {
  83. request.Header.Add(k, v)
  84. }
  85. }
  86. response, err := util_http.GetGlobalHttpClient().Do(request)
  87. if err != nil {
  88. stats.VolumeServerHandlerCounter.WithLabelValues(stats.FailedReadProxyReq).Inc()
  89. glog.V(0).Infof("request remote url %s: %v", r.URL.String(), err)
  90. InternalError(w)
  91. return
  92. }
  93. defer util_http.CloseResponse(response)
  94. // proxy target response to client
  95. for k, vv := range response.Header {
  96. if k == "Server" {
  97. continue
  98. }
  99. for _, v := range vv {
  100. w.Header().Add(k, v)
  101. }
  102. }
  103. w.WriteHeader(response.StatusCode)
  104. buf := mem.Allocate(128 * 1024)
  105. defer mem.Free(buf)
  106. io.CopyBuffer(w, response.Body, buf)
  107. return
  108. } else {
  109. // redirect
  110. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ReadRedirectReq).Inc()
  111. tragetUrl.Path = fmt.Sprintf("%s/%s,%s", tragetUrl.Path, vid, fid)
  112. arg := url.Values{}
  113. if c := r.FormValue("collection"); c != "" {
  114. arg.Set("collection", c)
  115. }
  116. arg.Set(reqIsProxied, "true")
  117. tragetUrl.RawQuery = arg.Encode()
  118. http.Redirect(w, r, tragetUrl.String(), http.StatusMovedPermanently)
  119. return
  120. }
  121. }
  122. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  123. n := new(needle.Needle)
  124. vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
  125. if !vs.maybeCheckJwtAuthorization(r, vid, fid, false) {
  126. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  127. return
  128. }
  129. volumeId, err := needle.NewVolumeId(vid)
  130. if err != nil {
  131. glog.V(2).Infof("parsing vid %s: %v", r.URL.Path, err)
  132. w.WriteHeader(http.StatusBadRequest)
  133. return
  134. }
  135. err = n.ParsePath(fid)
  136. if err != nil {
  137. glog.V(2).Infof("parsing fid %s: %v", r.URL.Path, err)
  138. w.WriteHeader(http.StatusBadRequest)
  139. return
  140. }
  141. // glog.V(4).Infoln("volume", volumeId, "reading", n)
  142. hasVolume := vs.store.HasVolume(volumeId)
  143. _, hasEcVolume := vs.store.FindEcVolume(volumeId)
  144. if !hasVolume && !hasEcVolume {
  145. if vs.ReadMode == "local" {
  146. glog.V(0).Infoln("volume is not local:", err, r.URL.Path)
  147. NotFound(w)
  148. return
  149. }
  150. vs.proxyReqToTargetServer(w, r)
  151. return
  152. }
  153. cookie := n.Cookie
  154. readOption := &storage.ReadOption{
  155. ReadDeleted: r.FormValue("readDeleted") == "true",
  156. HasSlowRead: vs.hasSlowRead,
  157. ReadBufferSize: vs.readBufferSizeMB * 1024 * 1024,
  158. }
  159. var count int
  160. var memoryCost types.Size
  161. readOption.AttemptMetaOnly, readOption.MustMetaOnly = shouldAttemptStreamWrite(hasVolume, ext, r)
  162. onReadSizeFn := func(size types.Size) {
  163. memoryCost = size
  164. atomic.AddInt64(&vs.inFlightDownloadDataSize, int64(memoryCost))
  165. }
  166. if hasVolume {
  167. count, err = vs.store.ReadVolumeNeedle(volumeId, n, readOption, onReadSizeFn)
  168. } else if hasEcVolume {
  169. count, err = vs.store.ReadEcShardNeedle(volumeId, n, onReadSizeFn)
  170. }
  171. defer func() {
  172. atomic.AddInt64(&vs.inFlightDownloadDataSize, -int64(memoryCost))
  173. if vs.concurrentDownloadLimit != 0 {
  174. vs.inFlightDownloadDataLimitCond.Broadcast()
  175. }
  176. }()
  177. if err != nil && err != storage.ErrorDeleted && hasVolume {
  178. glog.V(4).Infof("read needle: %v", err)
  179. // start to fix it from other replicas, if not deleted and hasVolume and is not a replicated request
  180. }
  181. // glog.V(4).Infoln("read bytes", count, "error", err)
  182. if err != nil || count < 0 {
  183. glog.V(3).Infof("read %s isNormalVolume %v error: %v", r.URL.Path, hasVolume, err)
  184. if err == storage.ErrorNotFound || err == storage.ErrorDeleted || errors.Is(err, erasure_coding.NotFoundError) {
  185. NotFound(w)
  186. } else {
  187. InternalError(w)
  188. }
  189. return
  190. }
  191. if n.Cookie != cookie {
  192. 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())
  193. NotFound(w)
  194. return
  195. }
  196. if n.LastModified != 0 {
  197. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  198. if r.Header.Get("If-Modified-Since") != "" {
  199. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  200. if t.Unix() >= int64(n.LastModified) {
  201. w.WriteHeader(http.StatusNotModified)
  202. return
  203. }
  204. }
  205. }
  206. }
  207. if inm := r.Header.Get("If-None-Match"); inm == "\""+n.Etag()+"\"" {
  208. w.WriteHeader(http.StatusNotModified)
  209. return
  210. }
  211. SetEtag(w, n.Etag())
  212. if n.HasPairs() {
  213. pairMap := make(map[string]string)
  214. err = json.Unmarshal(n.Pairs, &pairMap)
  215. if err != nil {
  216. glog.V(0).Infoln("Unmarshal pairs error:", err)
  217. }
  218. for k, v := range pairMap {
  219. w.Header().Set(k, v)
  220. }
  221. }
  222. if vs.tryHandleChunkedFile(n, filename, ext, w, r) {
  223. return
  224. }
  225. if n.NameSize > 0 && filename == "" {
  226. filename = string(n.Name)
  227. if ext == "" {
  228. ext = filepath.Ext(filename)
  229. }
  230. }
  231. mtype := ""
  232. if n.MimeSize > 0 {
  233. mt := string(n.Mime)
  234. if !strings.HasPrefix(mt, "application/octet-stream") {
  235. mtype = mt
  236. }
  237. }
  238. if n.IsCompressed() {
  239. _, _, _, shouldResize := shouldResizeImages(ext, r)
  240. _, _, _, _, shouldCrop := shouldCropImages(ext, r)
  241. if shouldResize || shouldCrop {
  242. if n.Data, err = util.DecompressData(n.Data); err != nil {
  243. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  244. }
  245. // } else if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") && util.IsZstdContent(n.Data) {
  246. // w.Header().Set("Content-Encoding", "zstd")
  247. } else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && util.IsGzippedContent(n.Data) {
  248. w.Header().Set("Content-Encoding", "gzip")
  249. } else {
  250. if n.Data, err = util.DecompressData(n.Data); err != nil {
  251. glog.V(0).Infoln("uncompress error:", err, r.URL.Path)
  252. }
  253. }
  254. }
  255. if !readOption.IsMetaOnly {
  256. rs := conditionallyCropImages(bytes.NewReader(n.Data), ext, r)
  257. rs = conditionallyResizeImages(rs, ext, r)
  258. if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
  259. glog.V(2).Infoln("response write error:", e)
  260. }
  261. } else {
  262. vs.streamWriteResponseContent(filename, mtype, volumeId, n, w, r, readOption)
  263. }
  264. }
  265. func shouldAttemptStreamWrite(hasLocalVolume bool, ext string, r *http.Request) (shouldAttempt bool, mustMetaOnly bool) {
  266. if !hasLocalVolume {
  267. return false, false
  268. }
  269. if len(ext) > 0 {
  270. ext = strings.ToLower(ext)
  271. }
  272. if r.Method == http.MethodHead {
  273. return true, true
  274. }
  275. _, _, _, shouldResize := shouldResizeImages(ext, r)
  276. _, _, _, _, shouldCrop := shouldCropImages(ext, r)
  277. if shouldResize || shouldCrop {
  278. return false, false
  279. }
  280. return true, false
  281. }
  282. func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
  283. if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
  284. return false
  285. }
  286. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  287. if e != nil {
  288. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  289. return false
  290. }
  291. if fileName == "" && chunkManifest.Name != "" {
  292. fileName = chunkManifest.Name
  293. }
  294. if ext == "" {
  295. ext = filepath.Ext(fileName)
  296. }
  297. mType := ""
  298. if chunkManifest.Mime != "" {
  299. mt := chunkManifest.Mime
  300. if !strings.HasPrefix(mt, "application/octet-stream") {
  301. mType = mt
  302. }
  303. }
  304. w.Header().Set("X-File-Store", "chunked")
  305. chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster(context.Background()), vs.grpcDialOption)
  306. defer chunkedFileReader.Close()
  307. rs := conditionallyCropImages(chunkedFileReader, ext, r)
  308. rs = conditionallyResizeImages(rs, ext, r)
  309. if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
  310. glog.V(2).Infoln("response write error:", e)
  311. }
  312. return true
  313. }
  314. func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  315. rs := originalDataReaderSeeker
  316. if len(ext) > 0 {
  317. ext = strings.ToLower(ext)
  318. }
  319. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  320. if shouldResize {
  321. rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
  322. }
  323. return rs
  324. }
  325. func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
  326. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" {
  327. if r.FormValue("width") != "" {
  328. width, _ = strconv.Atoi(r.FormValue("width"))
  329. }
  330. if r.FormValue("height") != "" {
  331. height, _ = strconv.Atoi(r.FormValue("height"))
  332. }
  333. }
  334. mode = r.FormValue("mode")
  335. shouldResize = width > 0 || height > 0
  336. return
  337. }
  338. func conditionallyCropImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  339. rs := originalDataReaderSeeker
  340. if len(ext) > 0 {
  341. ext = strings.ToLower(ext)
  342. }
  343. x1, y1, x2, y2, shouldCrop := shouldCropImages(ext, r)
  344. if shouldCrop {
  345. var err error
  346. rs, err = images.Cropped(ext, rs, x1, y1, x2, y2)
  347. if err != nil {
  348. glog.Errorf("Cropping images error: %s", err)
  349. }
  350. }
  351. return rs
  352. }
  353. func shouldCropImages(ext string, r *http.Request) (x1, y1, x2, y2 int, shouldCrop bool) {
  354. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
  355. if r.FormValue("crop_x1") != "" {
  356. x1, _ = strconv.Atoi(r.FormValue("crop_x1"))
  357. }
  358. if r.FormValue("crop_y1") != "" {
  359. y1, _ = strconv.Atoi(r.FormValue("crop_y1"))
  360. }
  361. if r.FormValue("crop_x2") != "" {
  362. x2, _ = strconv.Atoi(r.FormValue("crop_x2"))
  363. }
  364. if r.FormValue("crop_y2") != "" {
  365. y2, _ = strconv.Atoi(r.FormValue("crop_y2"))
  366. }
  367. }
  368. shouldCrop = x1 >= 0 && y1 >= 0 && x2 > x1 && y2 > y1
  369. return
  370. }
  371. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  372. totalSize, e := rs.Seek(0, 2)
  373. if mimeType == "" {
  374. if ext := filepath.Ext(filename); ext != "" {
  375. mimeType = mime.TypeByExtension(ext)
  376. }
  377. }
  378. if mimeType != "" {
  379. w.Header().Set("Content-Type", mimeType)
  380. }
  381. w.Header().Set("Accept-Ranges", "bytes")
  382. AdjustPassthroughHeaders(w, r, filename)
  383. if r.Method == http.MethodHead {
  384. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  385. return nil
  386. }
  387. return ProcessRangeRequest(r, w, totalSize, mimeType, func(offset int64, size int64) (filer.DoStreamContent, error) {
  388. return func(writer io.Writer) error {
  389. if _, e = rs.Seek(offset, 0); e != nil {
  390. return e
  391. }
  392. _, e = io.CopyN(writer, rs, size)
  393. return e
  394. }, nil
  395. })
  396. }
  397. func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) {
  398. totalSize := int64(n.DataSize)
  399. if mimeType == "" {
  400. if ext := filepath.Ext(filename); ext != "" {
  401. mimeType = mime.TypeByExtension(ext)
  402. }
  403. }
  404. if mimeType != "" {
  405. w.Header().Set("Content-Type", mimeType)
  406. }
  407. w.Header().Set("Accept-Ranges", "bytes")
  408. AdjustPassthroughHeaders(w, r, filename)
  409. if r.Method == http.MethodHead {
  410. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  411. return
  412. }
  413. ProcessRangeRequest(r, w, totalSize, mimeType, func(offset int64, size int64) (filer.DoStreamContent, error) {
  414. return func(writer io.Writer) error {
  415. return vs.store.ReadVolumeNeedleDataInto(volumeId, n, readOption, writer, offset, size)
  416. }, nil
  417. })
  418. }