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.

365 lines
11 KiB

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