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.

66 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. MaxIdleConnsPerHost: 1024,
  15. }}
  16. }
  17. func (fs *FilerServer) proxyToVolumeServer(w http.ResponseWriter, r *http.Request, fileId string) {
  18. urlStrings, err := fs.filer.MasterClient.GetLookupFileIdFunction()(fileId)
  19. if err != nil {
  20. glog.Errorf("locate %s: %v", fileId, err)
  21. w.WriteHeader(http.StatusInternalServerError)
  22. return
  23. }
  24. if len(urlStrings) == 0 {
  25. w.WriteHeader(http.StatusNotFound)
  26. return
  27. }
  28. proxyReq, err := http.NewRequest(r.Method, urlStrings[rand.Intn(len(urlStrings))], r.Body)
  29. if err != nil {
  30. glog.Errorf("NewRequest %s: %v", urlStrings[0], err)
  31. w.WriteHeader(http.StatusInternalServerError)
  32. return
  33. }
  34. proxyReq.Header.Set("Host", r.Host)
  35. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  36. for header, values := range r.Header {
  37. for _, value := range values {
  38. proxyReq.Header.Add(header, value)
  39. }
  40. }
  41. proxyResponse, postErr := client.Do(proxyReq)
  42. if postErr != nil {
  43. glog.Errorf("post to filer: %v", postErr)
  44. w.WriteHeader(http.StatusInternalServerError)
  45. return
  46. }
  47. defer util.CloseResponse(proxyResponse)
  48. for k, v := range proxyResponse.Header {
  49. w.Header()[k] = v
  50. }
  51. w.WriteHeader(proxyResponse.StatusCode)
  52. io.Copy(w, proxyResponse.Body)
  53. }