improved header parsing
This commit is contained in:
parent
de017b86f9
commit
5596b578f4
|
@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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{}
|
||||
|
|
Loading…
Reference in New Issue