http.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Copyright (C) 2017-2018 Marcus Rohrmoser, http://purl.mro.name/ShaarliGo
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package main
  18. import (
  19. "compress/gzip"
  20. "io"
  21. "log"
  22. "net/http"
  23. "net/url"
  24. "strings"
  25. "time"
  26. )
  27. func contains(haystack []string, needle string) bool {
  28. for _, s := range haystack {
  29. if needle == s {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. func HttpGetBody(url *url.URL, timeout time.Duration) (io.Reader, error) {
  36. defer un(trace(strings.Join([]string{"HttpGetBody", url.String()}, " ")))
  37. client := &http.Client{Timeout: timeout}
  38. req, _ := http.NewRequest("GET", url.String(), nil)
  39. req.Header.Set("Accept-Encoding", "gzip, deflate")
  40. req.Header.Set("User-Agent", strings.Join([]string{myselfNamespace, version}, ""))
  41. if resp, err := client.Do(req); nil == resp && nil != err {
  42. return nil, err
  43. } else {
  44. encs := resp.Header["Content-Encoding"]
  45. switch {
  46. case contains(encs, "gzip"), contains(encs, "deflate"):
  47. return gzip.NewReader(resp.Body)
  48. case 0 == len(encs):
  49. // NOP
  50. default:
  51. log.Printf("Strange compression: %s\n", encs)
  52. }
  53. return resp.Body, err
  54. }
  55. }