response.go 2.9 KB

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