take proper RSS/Atom Content-Type headers into account

master
Wouter Groeneveld 8 months ago
parent d0eddc3047
commit f57a53e1b1
  1. 1
      .gitignore
  2. 1
      .idea/go-jamming.iml
  3. 21
      app/webmention/send/discoverer.go
  4. 11
      app/webmention/send/discoverer_test.go

1
.gitignore vendored

@ -16,3 +16,4 @@ go-jamming
vangen
*.sublime-workspace
.idea/*

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />

@ -4,6 +4,7 @@ import (
"brainbaking.com/go-jamming/rest"
"fmt"
"github.com/rs/zerolog/log"
"net/http"
"net/url"
"regexp"
"strings"
@ -25,14 +26,32 @@ var (
"feed",
"feed/index.xml",
}
possibleContentTypes = []string{
"application/rss+xml",
"application/atom+xml",
"application/xml",
"text/xml",
"application/rdf+xml",
}
)
func isContentTypeFeedCompatible(header http.Header) bool {
cType := header.Get("Content-Type")
for _, possibleType := range possibleContentTypes {
if strings.Contains(cType, possibleType) {
return true
}
}
return false
}
func (sndr *Sender) discoverRssFeed(domain string) (string, error) {
for _, endpt := range possibleFeedEndpoints {
feedUrl := fmt.Sprintf("https://%s/%s", domain, endpt)
resp, err := sndr.RestClient.Head(feedUrl)
if err != nil || !rest.IsStatusOk(resp) || resp.Header.Get("Content-Type") != "text/xml" {
if err != nil || !rest.IsStatusOk(resp) || !isContentTypeFeedCompatible(resp.Header) {
continue
}

@ -10,6 +10,17 @@ import (
"testing"
)
func TestDiscoverRssFeedE2EBrainbaking(t *testing.T) {
var sender = &Sender{
RestClient: &rest.HttpClient{},
}
link, err := sender.discoverRssFeed("brainbaking.com")
expectedUrl := "https://brainbaking.com/index.xml"
assert.Nil(t, err)
assert.Truef(t, strings.HasPrefix(link, expectedUrl), "should start with %s, but was %s", expectedUrl, link)
}
func TestDiscoverMentionEndpointE2EWithRedirect(t *testing.T) {
t.Skip("Skipping TestDiscoverE2EWithRedirect, webmention.rocks is slow.")
var sender = &Sender{

Loading…
Cancel
Save