format.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package parser // import "miniflux.app/v2/internal/reader/parser"
  4. import (
  5. "bytes"
  6. "encoding/xml"
  7. "errors"
  8. "io"
  9. "unicode"
  10. rxml "miniflux.app/v2/internal/reader/xml"
  11. )
  12. // List of feed formats.
  13. const (
  14. FormatRDF = "rdf"
  15. FormatRSS = "rss"
  16. FormatAtom = "atom"
  17. FormatJSON = "json"
  18. FormatUnknown = "unknown"
  19. )
  20. const maxTokensToConsider = uint(50)
  21. // DetectFeedFormat tries to guess the feed format from input data.
  22. func DetectFeedFormat(r io.ReadSeeker) (string, string) {
  23. r.Seek(0, io.SeekStart)
  24. defer r.Seek(0, io.SeekStart)
  25. if isJSON, err := detectJSONFormat(r); err == nil && isJSON {
  26. return FormatJSON, ""
  27. }
  28. r.Seek(0, io.SeekStart)
  29. decoder := rxml.NewXMLDecoder(r)
  30. processedTokens := uint(0)
  31. for {
  32. token, _ := decoder.Token()
  33. if token == nil || processedTokens == maxTokensToConsider {
  34. break
  35. }
  36. processedTokens += 1
  37. if element, ok := token.(xml.StartElement); ok {
  38. switch element.Name.Local {
  39. case "rss":
  40. return FormatRSS, ""
  41. case "feed":
  42. for _, attr := range element.Attr {
  43. if attr.Name.Local == "version" && attr.Value == "0.3" {
  44. return FormatAtom, "0.3"
  45. }
  46. }
  47. return FormatAtom, "1.0"
  48. case "RDF":
  49. return FormatRDF, ""
  50. }
  51. }
  52. }
  53. return FormatUnknown, ""
  54. }
  55. var utf8BOM = []byte{0xEF, 0xBB, 0xBF}
  56. // detectJSONFormat checks if the reader contains JSON by reading until it finds
  57. // the first non-whitespace character or reaches EOF/error.
  58. // A leading UTF-8 byte order mark is skipped, as allowed by RFC 8259.
  59. func detectJSONFormat(r io.ReadSeeker) (bool, error) {
  60. const bufferSize = 32
  61. buffer := make([]byte, bufferSize)
  62. for firstRead := true; ; firstRead = false {
  63. n, err := r.Read(buffer)
  64. if n == 0 {
  65. if errors.Is(err, io.EOF) {
  66. return false, nil // No non-whitespace content found
  67. }
  68. return false, err
  69. }
  70. if len(buffer) < n {
  71. panic("unreachable") // bounds check hint to compiler
  72. }
  73. start := 0
  74. if firstRead && bytes.HasPrefix(buffer[:n], utf8BOM) {
  75. start = len(utf8BOM)
  76. }
  77. // Check each byte in the buffer
  78. for i := start; i < n; i++ {
  79. ch := buffer[i]
  80. // Skip whitespace characters (space, tab, newline, carriage return, etc.)
  81. if unicode.IsSpace(rune(ch)) {
  82. continue
  83. }
  84. // First non-whitespace character determines if it's JSON
  85. return ch == '{', nil
  86. }
  87. // If we've read less than bufferSize, we've reached EOF
  88. if n < bufferSize {
  89. return false, nil
  90. }
  91. }
  92. }