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.

421 lines
12 KiB

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