Sending Webmentions #10

Merged
h4kor merged 18 commits from webmention into master 2022-09-06 19:49:00 +00:00
2 changed files with 36 additions and 9 deletions
Showing only changes of commit 5596b578f4 - Show all commits

View File

@ -151,16 +151,26 @@ func (OwlHtmlParser) GetWebmentionEndpoint(resp *http.Response) (string, error)
requestUrl := resp.Request.URL
// Check link headers
for _, link := range resp.Header["Link"] {
if strings.Contains(link, "rel=\"webmention\"") || strings.Contains(link, "rel=webmention") {
link := strings.Split(link, ";")[0]
link = strings.Trim(link, "<>")
linkUrl, err := url.Parse(link)
if err != nil {
return "", err
for _, linkHeader := range resp.Header["Link"] {
linkHeaderParts := strings.Split(linkHeader, ",")
for _, linkHeaderPart := range linkHeaderParts {
linkHeaderPart = strings.TrimSpace(linkHeaderPart)
params := strings.Split(linkHeaderPart, ";")
if len(params) != 2 {
continue
}
for _, param := range params[1:] {
param = strings.TrimSpace(param)
if strings.Contains(param, "webmention") {
link := strings.Split(params[0], ";")[0]
link = strings.Trim(link, "<>")
linkUrl, err := url.Parse(link)
if err != nil {
return "", err
}
return requestUrl.ResolveReference(linkUrl).String(), nil
}
}
return requestUrl.ResolveReference(linkUrl).String(), nil
}
}

View File

@ -103,6 +103,23 @@ func TestGetWebmentionEndpointLinkHeader(t *testing.T) {
}
}
func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) {
html := []byte("")
parser := &owl.OwlHtmlParser{}
resp := constructResponse(html)
resp.Header = http.Header{
"Link": []string{"<https://webmention.rocks/test/19/webmention/error>; rel=\"other\", <https://webmention.rocks/test/19/webmention>; rel=\"webmention\""},
}
endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "https://webmention.rocks/test/19/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "https://webmention.rocks/test/19/webmention", endpoint)
}
}
func TestGetWebmentionEndpointRelativeLink(t *testing.T) {
html := []byte("<link rel=\"webmention\" href=\"/webmention\" />")
parser := &owl.OwlHtmlParser{}