response.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package http
  5. import "io"
  6. import "golang.org/x/net/html/charset"
  7. // Response wraps a server response.
  8. type Response struct {
  9. Body io.Reader
  10. StatusCode int
  11. EffectiveURL string
  12. LastModified string
  13. ETag string
  14. ContentType string
  15. }
  16. // HasServerFailure returns true if the status code represents a failure.
  17. func (r *Response) HasServerFailure() bool {
  18. return r.StatusCode >= 400
  19. }
  20. // IsModified returns true if the resource has been modified.
  21. func (r *Response) IsModified(etag, lastModified string) bool {
  22. if r.StatusCode == 304 {
  23. return false
  24. }
  25. if r.ETag != "" && r.ETag == etag {
  26. return false
  27. }
  28. if r.LastModified != "" && r.LastModified == lastModified {
  29. return false
  30. }
  31. return true
  32. }
  33. // NormalizeBodyEncoding make sure the body is encoded in UTF-8.
  34. func (r *Response) NormalizeBodyEncoding() (io.Reader, error) {
  35. return charset.NewReader(r.Body, r.ContentType)
  36. }