response.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 client // import "miniflux.app/http/client"
  5. import (
  6. "bytes"
  7. "io"
  8. "io/ioutil"
  9. "mime"
  10. "regexp"
  11. "strings"
  12. "unicode/utf8"
  13. "golang.org/x/net/html/charset"
  14. )
  15. var xmlEncodingRegex = regexp.MustCompile(`<\?xml(.*)encoding=["'](.+)["'](.*)\?>`)
  16. // Response wraps a server response.
  17. type Response struct {
  18. Body io.Reader
  19. StatusCode int
  20. EffectiveURL string
  21. LastModified string
  22. ETag string
  23. ContentType string
  24. ContentLength int64
  25. }
  26. // IsNotFound returns true if the resource doesn't exists anymore.
  27. func (r *Response) IsNotFound() bool {
  28. return r.StatusCode == 404 || r.StatusCode == 410
  29. }
  30. // IsNotAuthorized returns true if the resource require authentication.
  31. func (r *Response) IsNotAuthorized() bool {
  32. return r.StatusCode == 401
  33. }
  34. // HasServerFailure returns true if the status code represents a failure.
  35. func (r *Response) HasServerFailure() bool {
  36. return r.StatusCode >= 400
  37. }
  38. // IsModified returns true if the resource has been modified.
  39. func (r *Response) IsModified(etag, lastModified string) bool {
  40. if r.StatusCode == 304 {
  41. return false
  42. }
  43. if r.ETag != "" && r.ETag == etag {
  44. return false
  45. }
  46. if r.LastModified != "" && r.LastModified == lastModified {
  47. return false
  48. }
  49. return true
  50. }
  51. // EnsureUnicodeBody makes sure the body is encoded in UTF-8.
  52. //
  53. // If a charset other than UTF-8 is detected, we convert the document to UTF-8.
  54. // This is used by the scraper and feed readers.
  55. //
  56. // Do not forget edge cases:
  57. //
  58. // - Feeds with encoding specified only in Content-Type header and not in XML document
  59. // - Feeds with encoding specified in both places
  60. // - Feeds with encoding specified only in XML document and not in HTTP header
  61. // - Feeds with wrong encoding defined and already in UTF-8
  62. func (r *Response) EnsureUnicodeBody() (err error) {
  63. if r.ContentType != "" {
  64. mediaType, _, mediaErr := mime.ParseMediaType(r.ContentType)
  65. if mediaErr != nil {
  66. return mediaErr
  67. }
  68. // JSON feeds are always in UTF-8.
  69. if strings.Contains(mediaType, "json") {
  70. return
  71. }
  72. if strings.Contains(mediaType, "xml") {
  73. buffer, _ := ioutil.ReadAll(r.Body)
  74. r.Body = bytes.NewReader(buffer)
  75. // We ignore documents with encoding specified in XML prolog.
  76. // This is going to be handled by the XML parser.
  77. length := 1024
  78. if len(buffer) < 1024 {
  79. length = len(buffer)
  80. }
  81. if xmlEncodingRegex.Match(buffer[0:length]) {
  82. return
  83. }
  84. // If no encoding is specified in the XML prolog and
  85. // the document is valid UTF-8, nothing needs to be done.
  86. if utf8.Valid(buffer) {
  87. return
  88. }
  89. }
  90. }
  91. r.Body, err = charset.NewReader(r.Body, r.ContentType)
  92. return err
  93. }
  94. // String returns the response body as string.
  95. func (r *Response) String() string {
  96. bytes, _ := ioutil.ReadAll(r.Body)
  97. return string(bytes)
  98. }