From 438430ffc20e02790f67c19b5d080401fc5bc697 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 23 Dec 2022 06:42:00 -0800 Subject: [PATCH] add option to send specific accept header --- README.md | 1 + controllers/Parse.php | 3 ++- lib/XRay/Fetcher.php | 14 +++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d2c20d..1292146 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ In both cases, you can add an additional parameter to configure various options * `include_original` - Will also return the full document fetched * `target` - Specify a target URL, and XRay will first check if that URL is on the page, and only if it is, will continue to parse the page. This is useful when you're using XRay to verify an incoming webmention. * `expect=feed` - If you know the thing you are parsing is a feed, include this parameter which will avoid running the autodetection rules and will provide better results for some feeds. +* `accept` - (options: `html`, `json`, `activitypub`, `xml`) - Without this parameter, XRay sends a default `Accept` header to prioritize getting the most likely best result from a page. If you are parsing a page for a specific purpose and expect to find only one type of content (e.g. webmentions will probably only be from HTML pages), you can include this parameter to adjust the `Accept` header XRay sends. Additional parameters are supported when making requests that use the Twitter or GitHub API. See the Authentication section below for details. diff --git a/controllers/Parse.php b/controllers/Parse.php index 2cb8260..867bb5e 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -96,7 +96,8 @@ class Parse { $fields = [ 'twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret', 'github_access_token', - 'token' + 'token', + 'accept', ]; foreach($fields as $f) { if($v=$request->get($f)) diff --git a/lib/XRay/Fetcher.php b/lib/XRay/Fetcher.php index 09a5dcd..c4cdd07 100644 --- a/lib/XRay/Fetcher.php +++ b/lib/XRay/Fetcher.php @@ -66,7 +66,19 @@ class Fetcher { $headers = []; - $headers[] = 'Accept: application/mf2+json, application/activity+json, text/html, application/json, application/xml, text/xml'; + $accept = 'application/mf2+json, application/activity+json, text/html, application/json, application/xml, text/xml'; + if(isset($opts['accept'])) { + if($opts['accept'] == 'html') + $accept = 'text/html'; + if($opts['accept'] == 'json') + $accept = 'application/mf2+json, application/activity+json, application/json'; + if($opts['accept'] == 'activitypub') + $accept = 'application/activity+json'; + if($opts['accept'] == 'xml') + $accept = 'application/xml, text/xml'; + } + + $headers[] = 'Accept: '.$accept; if(isset($opts['token'])) $headers[] = 'Authorization: Bearer ' . $opts['token'];