Rewrite relative RSS Bridge URL
This commit is contained in:
parent
ce32d181d5
commit
87c9ef6b48
1 changed files with 45 additions and 9 deletions
|
@ -6,10 +6,15 @@ package rssbridge // import "miniflux.app/integration/rssbridge"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultClientTimeout = 30 * time.Second
|
||||||
|
|
||||||
type Bridge struct {
|
type Bridge struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
BridgeMeta BridgeMeta `json:"bridgeMeta"`
|
BridgeMeta BridgeMeta `json:"bridgeMeta"`
|
||||||
|
@ -19,30 +24,61 @@ type BridgeMeta struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetectBridges(rssbridgeURL, websiteURL string) (bridgeResponse []Bridge, err error) {
|
func DetectBridges(rssBridgeURL, websiteURL string) ([]*Bridge, error) {
|
||||||
u, err := url.Parse(rssbridgeURL)
|
endpointURL, err := url.Parse(rssBridgeURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("RSS-Bridge: unable to parse bridge URL: %w", err)
|
||||||
}
|
}
|
||||||
values := u.Query()
|
|
||||||
|
values := endpointURL.Query()
|
||||||
values.Add("action", "findfeed")
|
values.Add("action", "findfeed")
|
||||||
values.Add("format", "atom")
|
values.Add("format", "atom")
|
||||||
values.Add("url", websiteURL)
|
values.Add("url", websiteURL)
|
||||||
u.RawQuery = values.Encode()
|
endpointURL.RawQuery = values.Encode()
|
||||||
|
|
||||||
response, err := http.Get(u.String())
|
slog.Debug("Detecting RSS bridges", slog.String("url", endpointURL.String()))
|
||||||
|
|
||||||
|
request, err := http.NewRequest(http.MethodGet, endpointURL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("RSS-Bridge: unable to excute request: %w", err)
|
return nil, fmt.Errorf("RSS-Bridge: unable to create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
||||||
|
|
||||||
|
response, err := httpClient.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("RSS-Bridge: unable to execute request: %w", err)
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
if response.StatusCode == http.StatusNotFound {
|
if response.StatusCode == http.StatusNotFound {
|
||||||
return
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.StatusCode > 400 {
|
if response.StatusCode > 400 {
|
||||||
return nil, fmt.Errorf("RSS-Bridge: unexpected status code %d", response.StatusCode)
|
return nil, fmt.Errorf("RSS-Bridge: unexpected status code %d", response.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bridgeResponse []*Bridge
|
||||||
if err := json.NewDecoder(response.Body).Decode(&bridgeResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&bridgeResponse); err != nil {
|
||||||
return nil, fmt.Errorf("RSS-Bridge: unable to decode bridge response: %w", err)
|
return nil, fmt.Errorf("RSS-Bridge: unable to decode bridge response: %w", err)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
for _, bridge := range bridgeResponse {
|
||||||
|
slog.Debug("Found RSS bridge",
|
||||||
|
slog.String("name", bridge.BridgeMeta.Name),
|
||||||
|
slog.String("url", bridge.URL),
|
||||||
|
)
|
||||||
|
|
||||||
|
if strings.HasPrefix(bridge.URL, "./") {
|
||||||
|
bridge.URL = rssBridgeURL + bridge.URL[2:]
|
||||||
|
|
||||||
|
slog.Debug("Rewrited relative RSS bridge URL",
|
||||||
|
slog.String("name", bridge.BridgeMeta.Name),
|
||||||
|
slog.String("url", bridge.URL),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bridgeResponse, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue