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.

67 lines
1.4 KiB

  1. package weed_server
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "io"
  6. "math/rand"
  7. "net/http"
  8. )
  9. var (
  10. client *http.Client
  11. )
  12. func init() {
  13. client = &http.Client{Transport: &http.Transport{
  14. MaxIdleConns: 1024,
  15. MaxIdleConnsPerHost: 1024,
  16. }}
  17. }
  18. func (fs *FilerServer) proxyToVolumeServer(w http.ResponseWriter, r *http.Request, fileId string) {
  19. urlStrings, err := fs.filer.MasterClient.GetLookupFileIdFunction()(fileId)
  20. if err != nil {
  21. glog.Errorf("locate %s: %v", fileId, err)
  22. w.WriteHeader(http.StatusInternalServerError)
  23. return
  24. }
  25. if len(urlStrings) == 0 {
  26. w.WriteHeader(http.StatusNotFound)
  27. return
  28. }
  29. proxyReq, err := http.NewRequest(r.Method, urlStrings[rand.Intn(len(urlStrings))], r.Body)
  30. if err != nil {
  31. glog.Errorf("NewRequest %s: %v", urlStrings[0], err)
  32. w.WriteHeader(http.StatusInternalServerError)
  33. return
  34. }
  35. proxyReq.Header.Set("Host", r.Host)
  36. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  37. for header, values := range r.Header {
  38. for _, value := range values {
  39. proxyReq.Header.Add(header, value)
  40. }
  41. }
  42. proxyResponse, postErr := client.Do(proxyReq)
  43. if postErr != nil {
  44. glog.Errorf("post to filer: %v", postErr)
  45. w.WriteHeader(http.StatusInternalServerError)
  46. return
  47. }
  48. defer util.CloseResponse(proxyResponse)
  49. for k, v := range proxyResponse.Header {
  50. w.Header()[k] = v
  51. }
  52. w.WriteHeader(proxyResponse.StatusCode)
  53. io.Copy(w, proxyResponse.Body)
  54. }