From 83c2fbfb85a6b47983ef94cff240dd1660b59495 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Tue, 10 Aug 2021 14:25:42 -0700 Subject: [PATCH] ignore content-type encoding when choosing a parser for topic data Now it will properly strip any encoding from content-type header before comparing against available parsers. --- src/link-helper.js | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/link-helper.js b/src/link-helper.js index 1f3cd09..0517dec 100644 --- a/src/link-helper.js +++ b/src/link-helper.js @@ -41,40 +41,46 @@ class LinkHelper { // Add Link headers first, as they take priority over link elements in body. const linkHeader = getHeader(headers, Enum.Header.Link); const links = []; - try { - links.push(...parseLinkHeader(linkHeader)); - } catch (e) { - if (e instanceof ParseSyntaxError) { - this.logger.debug(_scope, 'failed to parse link header, bad syntax', { error: e, linkHeader }); - } else { - this.logger.error(_scope, 'failed to parse link header', { error: e, linkHeader }); + if (linkHeader) { + try { + links.push(...parseLinkHeader(linkHeader)); + } catch (e) { + if (e instanceof ParseSyntaxError) { + this.logger.debug(_scope, 'failed to parse link header, bad syntax', { error: e, linkHeader }); + } else { + this.logger.error(_scope, 'failed to parse link header', { error: e, linkHeader }); + } } } - const contentType = getHeader(headers, Enum.Header.ContentType); - let bodyLinks = []; - switch (contentType) { - case Enum.ContentType.ApplicationAtom: - case Enum.ContentType.ApplicationRDF: - case Enum.ContentType.ApplicationRSS: - case Enum.ContentType.ApplicationXML: - case Enum.ContentType.TextXML: { - bodyLinks = await this.linksFromFeedBody(url, body); - break; - } + if (contentType) { + const [contentTypeBase, _contentTypeEncoding] = contentType.split(/; +/); + let bodyLinks = []; + switch (contentTypeBase) { + case Enum.ContentType.ApplicationAtom: + case Enum.ContentType.ApplicationRDF: + case Enum.ContentType.ApplicationRSS: + case Enum.ContentType.ApplicationXML: + case Enum.ContentType.TextXML: { + bodyLinks = await this.linksFromFeedBody(url, body); + break; + } - case Enum.ContentType.TextHTML: - bodyLinks = this.linksFromHTMLBody(body); - break; + case Enum.ContentType.TextHTML: + bodyLinks = this.linksFromHTMLBody(body); + break; - default: - this.logger.debug(_scope, 'no parser for content type', { contentType }); + default: + this.logger.debug(_scope, 'no parser for content type', { contentType }); + } + links.push(...bodyLinks); } - links.push(...bodyLinks); // Fetch all hub relation targets from headers, resolving relative URIs. const hubs = LinkHelper.locateHubTargets(links).map((link) => this.absoluteURI(link, url)); + this.logger.debug(_scope, 'valid hubs for url', { url, hubs }); + return hubs.includes(this.selfUrl); } -- 2.45.2