From: Justin Wind Date: Tue, 10 Aug 2021 21:25:42 +0000 (-0700) Subject: ignore content-type encoding when choosing a parser for topic data X-Git-Tag: v1.1.1^2~1 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=83c2fbfb85a6b47983ef94cff240dd1660b59495;p=websub-hub 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. --- 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); }