volume_server_handlers_read.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. "sync/atomic"
  15. "time"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  17. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  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. _, _, _, shouldResize := shouldResizeImages(ext, r)
  193. _, _, _, _, shouldCrop := shouldCropImages(ext, r)
  194. if shouldResize || shouldCrop {
  195. if n.Data, err = util.DecompressData(n.Data); err != nil {
  196. glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
  197. }
  198. // } else if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") && util.IsZstdContent(n.Data) {
  199. // w.Header().Set("Content-Encoding", "zstd")
  200. } else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && util.IsGzippedContent(n.Data) {
  201. w.Header().Set("Content-Encoding", "gzip")
  202. } else {
  203. if n.Data, err = util.DecompressData(n.Data); err != nil {
  204. glog.V(0).Infoln("uncompress error:", err, r.URL.Path)
  205. }
  206. }
  207. }
  208. if !readOption.IsMetaOnly {
  209. rs := conditionallyCropImages(bytes.NewReader(n.Data), ext, r)
  210. rs = conditionallyResizeImages(rs, ext, r)
  211. if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
  212. glog.V(2).Infoln("response write error:", e)
  213. }
  214. } else {
  215. vs.streamWriteResponseContent(filename, mtype, volumeId, n, w, r, readOption)
  216. }
  217. }
  218. func shouldAttemptStreamWrite(hasLocalVolume bool, ext string, r *http.Request) (shouldAttempt bool, mustMetaOnly bool) {
  219. if !hasLocalVolume {
  220. return false, false
  221. }
  222. if len(ext) > 0 {
  223. ext = strings.ToLower(ext)
  224. }
  225. if r.Method == "HEAD" {
  226. return true, true
  227. }
  228. _, _, _, shouldResize := shouldResizeImages(ext, r)
  229. _, _, _, _, shouldCrop := shouldCropImages(ext, r)
  230. if shouldResize || shouldCrop {
  231. return false, false
  232. }
  233. return true, false
  234. }
  235. func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
  236. if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
  237. return false
  238. }
  239. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  240. if e != nil {
  241. glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
  242. return false
  243. }
  244. if fileName == "" && chunkManifest.Name != "" {
  245. fileName = chunkManifest.Name
  246. }
  247. if ext == "" {
  248. ext = filepath.Ext(fileName)
  249. }
  250. mType := ""
  251. if chunkManifest.Mime != "" {
  252. mt := chunkManifest.Mime
  253. if !strings.HasPrefix(mt, "application/octet-stream") {
  254. mType = mt
  255. }
  256. }
  257. w.Header().Set("X-File-Store", "chunked")
  258. chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster(), vs.grpcDialOption)
  259. defer chunkedFileReader.Close()
  260. rs := conditionallyCropImages(chunkedFileReader, ext, r)
  261. rs = conditionallyResizeImages(rs, ext, r)
  262. if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
  263. glog.V(2).Infoln("response write error:", e)
  264. }
  265. return true
  266. }
  267. func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  268. rs := originalDataReaderSeeker
  269. if len(ext) > 0 {
  270. ext = strings.ToLower(ext)
  271. }
  272. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  273. if shouldResize {
  274. rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
  275. }
  276. return rs
  277. }
  278. func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
  279. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" {
  280. if r.FormValue("width") != "" {
  281. width, _ = strconv.Atoi(r.FormValue("width"))
  282. }
  283. if r.FormValue("height") != "" {
  284. height, _ = strconv.Atoi(r.FormValue("height"))
  285. }
  286. }
  287. mode = r.FormValue("mode")
  288. shouldResize = width > 0 || height > 0
  289. return
  290. }
  291. func conditionallyCropImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
  292. rs := originalDataReaderSeeker
  293. if len(ext) > 0 {
  294. ext = strings.ToLower(ext)
  295. }
  296. x1, y1, x2, y2, shouldCrop := shouldCropImages(ext, r)
  297. if shouldCrop {
  298. var err error
  299. rs, err = images.Cropped(ext, rs, x1, y1, x2, y2)
  300. if err != nil {
  301. glog.Errorf("Cropping images error: %s", err)
  302. }
  303. }
  304. return rs
  305. }
  306. func shouldCropImages(ext string, r *http.Request) (x1, y1, x2, y2 int, shouldCrop bool) {
  307. if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
  308. if r.FormValue("crop_x1") != "" {
  309. x1, _ = strconv.Atoi(r.FormValue("crop_x1"))
  310. }
  311. if r.FormValue("crop_y1") != "" {
  312. y1, _ = strconv.Atoi(r.FormValue("crop_y1"))
  313. }
  314. if r.FormValue("crop_x2") != "" {
  315. x2, _ = strconv.Atoi(r.FormValue("crop_x2"))
  316. }
  317. if r.FormValue("crop_y2") != "" {
  318. y2, _ = strconv.Atoi(r.FormValue("crop_y2"))
  319. }
  320. }
  321. shouldCrop = x1 >= 0 && y1 >= 0 && x2 > x1 && y2 > y1
  322. return
  323. }
  324. func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
  325. totalSize, e := rs.Seek(0, 2)
  326. if mimeType == "" {
  327. if ext := filepath.Ext(filename); ext != "" {
  328. mimeType = mime.TypeByExtension(ext)
  329. }
  330. }
  331. if mimeType != "" {
  332. w.Header().Set("Content-Type", mimeType)
  333. }
  334. w.Header().Set("Accept-Ranges", "bytes")
  335. adjustPassthroughHeaders(w, r, filename)
  336. if r.Method == "HEAD" {
  337. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  338. return nil
  339. }
  340. return processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  341. if _, e = rs.Seek(offset, 0); e != nil {
  342. return e
  343. }
  344. _, e = io.CopyN(writer, rs, size)
  345. return e
  346. })
  347. }
  348. func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) {
  349. totalSize := int64(n.DataSize)
  350. if mimeType == "" {
  351. if ext := filepath.Ext(filename); ext != "" {
  352. mimeType = mime.TypeByExtension(ext)
  353. }
  354. }
  355. if mimeType != "" {
  356. w.Header().Set("Content-Type", mimeType)
  357. }
  358. w.Header().Set("Accept-Ranges", "bytes")
  359. adjustPassthroughHeaders(w, r, filename)
  360. if r.Method == "HEAD" {
  361. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  362. return
  363. }
  364. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  365. return vs.store.ReadVolumeNeedleDataInto(volumeId, n, readOption, writer, offset, size)
  366. })
  367. }