HandleHttpLocation.go 788 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. func GetHttpLocation(Url string) string {
  8. resp, err := http.Get(Url)
  9. if err != nil {
  10. fmt.Println("Error:", err)
  11. return ""
  12. }
  13. defer resp.Body.Close()
  14. fmt.Println("Headers for", url)
  15. if resp.Request == nil || resp.Request.Response == nil || resp.Request.Response.Header == nil {
  16. return ""
  17. }
  18. locationHeader := resp.Request.Response.Header["Location"][0]
  19. if len(locationHeader) > 0 {
  20. return locationHeader
  21. }
  22. return ""
  23. }
  24. func RefreshHttpLocation(Url string) string {
  25. defer func() {
  26. if r := recover(); r != nil {
  27. go RefreshHttpLocation(*url)
  28. }
  29. }()
  30. for {
  31. time.Sleep(60 * time.Second)
  32. location := GetHttpLocation(Url)
  33. if len(location) > 0 {
  34. TargetUrl = location
  35. } else {
  36. TargetUrl = *url
  37. }
  38. }
  39. }