8000 [pull] main from miniflux:main by pull[bot] · Pull Request #341 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/reader/json/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ func (j *JSONAdapter) BuildFeed(baseURL string) *model.Feed {

for _, item := range j.jsonFeed.Items {
entry := model.NewEntry()

// Populate the entry language. Per the JSON Feed spec, an item
// declares a language only when it differs from the primary
// language of the feed.
entry.Language = language.Normalize(item.Language)
if entry.Language == "" {
entry.Language = feed.Language
}

for _, itemURL := range []string{item.URL, item.ExternalURL} {
if itemURL = strings.TrimSpace(itemURL); itemURL == "" {
Expand Down
6 changes: 3 additions & 3 deletions internal/reader/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ func TestParseItemWithLanguage(t *testing.T) {
}
}

func TestParseItemWithoutLanguage(t *testing.T) {
func TestParseItemWithoutLanguageInheritsFeedLanguage(t *testing.T) {
data := `{
"version": "https://jsonfeed.org/version/1.1",
"title": "Example",
Expand All @@ -1199,7 +1199,7 @@ func TestParseItemWithoutLanguage(t *testing.T) {
t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
}

if feed.Entries[0].Language != "" {
t.Errorf("Expected empty entry language, got: %q", feed.Entries[0].Language)
if feed.Entries[0].Language != "en-us" {
t.Errorf("Expected entry to inherit feed language, got: %q", feed.Entries[0].Language)
}
}
5 changes: 5 additions & 0 deletions internal/reader/rdf/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func (r *rdfAdapter) buildFeed(baseURL string) *model.Feed {
entry.Author = sanitizer.StripTags(r.rdf.Channel.DublinCoreCreator)
}

// Populate the entry language, falling back to the channel
// language: items are part of the channel's content.
entry.Language = language.Normalize(item.DublinCoreLanguage)
if entry.Language == "" {
entry.Language = feed.Language
}

feed.Entries = append(feed.Entries, entry)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/reader/rdf/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,8 @@ func TestParseFeedWithChannelLanguage(t *testing.T) {
t.Fatalf(`Unexpected entry count, got: %d`, len(feed.Entries))
}

if feed.Entries[0].Language != "" {
t.Errorf(`Expected empty entry language, got: %q`, feed.Entries[0].Language)
if feed.Entries[0].Language != "en-us" {
t.Errorf(`Expected entry to inherit channel language, got: %q`, feed.Entries[0].Language)
}
}

Expand Down
11 changes: 11 additions & 0 deletions internal/reader/rss/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func (r *rssAdapter) buildFeed(baseURL string) *model.Feed {
Language: language.Normalize(r.rss.Channel.Language),
}

// Hybrid feeds declare the channel language with <dc:language>
// instead of <language>.
if feed.Language == "" {
feed.Language = language.Normalize(r.rss.Channel.DublinCoreLanguage)
}

// Ensure the Site URL is absolute.
if absoluteSiteURL, err := urllib.ResolveToAbsoluteURL(baseURL, feed.SiteURL); err == nil {
feed.SiteURL = absoluteSiteURL
Expand Down Expand Up @@ -115,7 +121,12 @@ func (r *rssAdapter) buildFeed(baseURL string) *model.Feed {
entry.Author = findFeedAuthor(&r.rss.Channel)
}

// Populate the entry language, falling back to the channel
// language: items are part of the channel's content.
entry.Language = language.Normalize(item.DublinCoreLanguage)
if entry.Language == "" {
entry.Language = feed.Language
}

// Generate the entry hash.
//
Expand Down
30 changes: 27 additions & 3 deletions internal/reader/rss/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,31 @@ func TestParseItemWithDublinCoreLanguage(t *testing.T) {
}
}

func TestParseItemWithoutDublinCoreLanguage(t *testing.T) {
func TestParseFeedWithDublinCoreChannelLanguage(t *testing.T) {
data := `<?xml version="1.0" 8000 encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Example</title>
<link>https://example.org/</link>
<dc:language>fr-FR</dc:language>
<item>
<title>Item</title>
<link>https://example.org/item</link>
</item>
</channel>
</rss>`

feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
if err != nil {
t.Fatal(err)
}

if feed.Language != "fr-fr" {
t.Errorf("Incorrect feed language, got: %q", feed.Language)
}
}

func TestParseItemWithoutDublinCoreLanguageInheritsChannelLanguage(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
Expand All @@ -2406,7 +2430,7 @@ func TestParseItemWithoutDublinCoreLanguage(t *testing.T) {
t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
}

if feed.Entries[0].Language != "" {
t.Errorf("Expected empty entry language, got: %q", feed.Entries[0].Language)
if feed.Entries[0].Language != "en-us" {
t.Errorf("Expected entry to inherit channel language, got: %q", feed.Entries[0].Language)
}
}
4 changes: 4 additions & 0 deletions internal/reader/rss/rss.go
8BD0
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type rssChannel struct {
// You may also use values defined by the W3C: https://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes.
Language string `xml:"rss language"`

// Hybrid feeds declare the channel language with <dc:language>
// instead of <language>.
dublincore.DublinCoreChannelElement

// Copyright is a string indicating the copyright.
Copyright string `xml:"rss copyRight"`

Expand Down
7 changes: 7 additions & 0 deletions internal/template/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func (e *Engine) Render(name string, data map[string]any) []byte {
panic("The template " + name + " does not exists.")
}

// Clone the template so the per-request, language-specific functions below
// are bound on a private copy. The shared template stored in e.templates is
// only ever cloned (never executed directly), so concurrent requests no
// longer race on its function map, which previously could cause a response
// to be rendered with another concurrent request's language.
tpl = template.Must(tpl.Clone())

printer := locale.NewPrinter(data["language"].(string))

// Functions that need to be declared at runtime.
Expand Down
62 changes: 62 additions & 0 deletions internal/template/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package template // import "miniflux.app/v2/internal/template"

import (
"bytes"
"sync"
"testing"
)

// TestRenderConcurrency renders the same template concurrently in different
// languages. Because Render binds per-request, language-specific functions
// ("t", "plural", "elapsed") onto the template, doing so on a shared template
// while other requests execute it corrupts the output: a request can be served
// another request's language. Each concurrent render must match the output of
// the equivalent sequential render for its language.
func TestRenderConcurrency(t *testing.T) {
engine := NewEngine("")
engine.ParseTemplates()

languages := []string{"en_US", "fr_FR", "de_DE", "es_ES", "pt_BR", "ru_RU", "zh_CN", "it_IT"}

newData := func(language string) map[string]any {
return map[string]any{"language": language, "theme": "system_serif"}
}

// Establish the expected output for each language sequentially.
expected := make(map[string][]byte, len(languages))
for _, language := range languages {
expected[language] = engine.Render("offline.html", newData(language))
}

const iterations = 300

var wg sync.WaitGroup
var mu sync.Mutex
mismatches := make(map[string]int)

for i := 0; i < iterations; i++ {
language := languages[i%len(languages)]
wg.Add(1)
go func(language string) {
defer wg.Done()
got := engine.Render("offline.html", newData(language))
if !bytes.Equal(got, expected[language]) {
mu.Lock()
mismatches[language]++
mu.Unlock()
}
}(language)
}
wg.Wait()

if len(mismatches) > 0 {
total := 0
for _, n := range mismatches {
total += n
}
t.Fatalf("concurrent Render produced wrong output for %d/%d requests (wrong-language translations); per-language mismatches: %v", total, iterations, mismatches)
}
}
Loading
0