format.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "encoding/xml"
  6. "io"
  7. "unicode"
  8. rxml "miniflux.app/v2/internal/reader/xml"
  9. )
  10. // List of feed formats.
  11. const (
  12. FormatRDF = "rdf"
  13. FormatRSS = "rss"
  14. FormatAtom = "atom"
  15. FormatJSON = "json"
  16. FormatUnknown = "unknown"
  17. )
  18. // DetectFeedFormat tries to guess the feed format from input data.
  19. func DetectFeedFormat(r io.ReadSeeker) (string, string) {
  20. r.Seek(0, io.SeekStart)
  21. defer r.Seek(0, io.SeekStart)
  22. if isJSON, err := detectJSONFormat(r); err == nil && isJSON {
  23. return FormatJSON, ""
  24. }
  25. r.Seek(0, io.SeekStart)
  26. decoder := rxml.NewXMLDecoder(r)
  27. for {
  28. token, _ := decoder.Token()
  29. if token == nil {
  30. break
  31. }
  32. if element, ok := token.(xml.StartElement); ok {
  33. switch element.Name.Local {
  34. case "rss":
  35. return FormatRSS, ""
  36. case "feed":
  37. for _, attr := range element.Attr {
  38. if attr.Name.Local == "version" && attr.Value == "0.3" {
  39. return FormatAtom, "0.3"
  40. }
  41. }
  42. return FormatAtom, "1.0"
  43. case "RDF":
  44. return FormatRDF, ""
  45. }
  46. }
  47. }
  48. return FormatUnknown, ""
  49. }
  50. // detectJSONFormat checks if the reader contains JSON by reading until it finds
  51. // the first non-whitespace character or reaches EOF/error.
  52. func detectJSONFormat(r io.ReadSeeker) (bool, error) {
  53. const bufferSize = 32
  54. buffer := make([]byte, bufferSize)
  55. for {
  56. n, err := r.Read(buffer)
  57. if n == 0 {
  58. if err == io.EOF {
  59. return false, nil // No non-whitespace content found
  60. }
  61. return false, err
  62. }
  63. if len(buffer) < n {
  64. panic("unreachable") // bounds check hint to compiler
  65. }
  66. // Check each byte in the buffer
  67. for i := range n {
  68. ch := buffer[i]
  69. // Skip whitespace characters (space, tab, newline, carriage return, etc.)
  70. if unicode.IsSpace(rune(ch)) {
  71. continue
  72. }
  73. // First non-whitespace character determines if it's JSON
  74. return ch == '{', nil
  75. }
  76. // If we've read less than bufferSize, we've reached EOF
  77. if n < bufferSize {
  78. return false, nil
  79. }
  80. }
  81. }