Skip to content

Commit

Permalink
Merge pull request #802 from UN-OCHA/develop
Browse files Browse the repository at this point in the history
Deploy 14-08-2024
  • Loading branch information
lazysoundsystem authored Aug 13, 2024
2 parents 40066c6 + 64e8a1f commit b3bd5dc
Show file tree
Hide file tree
Showing 93 changed files with 1,485 additions and 1,089 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/composer-update-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Run Composer Update with Config

on:
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Update The Thing with config
id: update-action-with-config
uses: UN-OCHA/actions/composer-update@OPS-10254-config-too
with:
aws_access_key_id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
github_access_token: ${{ secrets.PAT }}
patch_branch: ${{ github.head_ref || github.ref_name }}
patch_maintainers: ${{ secrets.DRUPAL_MAINTAINERS }}
slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
slack_channel_name: ${{ vars.SLACK_CHANNEL }}
108 changes: 108 additions & 0 deletions PATCHES/linkchecker-fallback-get-3334240.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
diff --git a/config/install/linkchecker.settings.yml b/config/install/linkchecker.settings.yml
index f3311437c5beb3072a353b5047d769850442f95b..c8f135bae3aba4c7a69394dc998334780138ee20 100644
--- a/config/install/linkchecker.settings.yml
+++ b/config/install/linkchecker.settings.yml
@@ -29,6 +29,7 @@ check:
error:
action_status_code_301: 0
action_status_code_404: 0
+ fallback_get_if_head_fails: true
ignore_response_codes: "200\n206\n302\n304\n401\n403"
impersonate_account: ''
logging:
diff --git a/config/schema/linkchecker.schema.yml b/config/schema/linkchecker.schema.yml
index 7b3ad78f875cbcb883190b180bcbeaf5b28051bf..3c4cd755482815392a87fdc641bbc5936984c0e5 100644
--- a/config/schema/linkchecker.schema.yml
+++ b/config/schema/linkchecker.schema.yml
@@ -79,6 +79,9 @@ linkchecker.settings:
action_status_code_404:
type: integer
label: 'Unpublish content on file not found error'
+ fallback_get_if_head_fails:
+ type: boolean
+ label: 'If HEAD request fails, attempt GET'
ignore_response_codes:
type: string
label: 'Do not treat these response codes as errors'
diff --git a/src/Form/LinkCheckerAdminSettingsForm.php b/src/Form/LinkCheckerAdminSettingsForm.php
index 73cafcff0e0d1048fc3b4376550502a30d92f207..d6571f338766bd929fd446d3dccceb2501b8eb81 100644
--- a/src/Form/LinkCheckerAdminSettingsForm.php
+++ b/src/Form/LinkCheckerAdminSettingsForm.php
@@ -388,6 +388,12 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase {
10 => $this->t('After ten file not found errors'),
],
];
+ $form['error']['linkchecker_fallback_get_if_head_fails'] = [
+ '#title' => $this->t('Use GET method if error returned with HEAD'),
+ '#description' => $this->t('Certain sites may return an error when using HEAD, but return OK when making a GET request. In case HEAD request returns error, a GET request would be attempted.'),
+ '#type' => 'checkbox',
+ '#default_value' => $config->get('error.fallback_get_if_head_fails'),
+ ];
$form['error']['linkchecker_ignore_response_codes'] = [
'#default_value' => $config->get('error.ignore_response_codes'),
'#type' => 'textarea',
@@ -480,6 +486,7 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase {
->set('check.useragent', $form_state->getValue('linkchecker_check_useragent'))
->set('error.action_status_code_301', $form_state->getValue('linkchecker_action_status_code_301'))
->set('error.action_status_code_404', $form_state->getValue('linkchecker_action_status_code_404'))
+ ->set('error.fallback_get_if_head_fails', $form_state->getValue('linkchecker_fallback_get_if_head_fails'))
->set('error.ignore_response_codes', $form_state->getValue('linkchecker_ignore_response_codes'))
->set('error.impersonate_account', $form_state->getValue('linkchecker_impersonate_account'))
->set('logging.level', $form_state->getValue('linkchecker_logging_level'))
diff --git a/src/LinkCheckerService.php b/src/LinkCheckerService.php
index e672546064af73737756e62370b2e660bcfe1064..0257f8042d4591f50dbba7c4b19e5c70feb9d8c3 100644
--- a/src/LinkCheckerService.php
+++ b/src/LinkCheckerService.php
@@ -193,6 +193,26 @@ class LinkCheckerService {
return $this->httpClient
->requestAsync($link->getRequestMethod(), $link->getUrl(), $options)
->then(function (ResponseInterface $response) use ($link, $uri) {
+ // Certain HEAD requests return error even though they are valid.
+ // If turned on, in case of error response to HEAD request, attempt
+ // a GET request as well.
+ $fallbackGetIfHeadFails = $this->linkcheckerSetting->get('error.fallback_get_if_head_fails');
+ $isHead = $link->getRequestMethod() === 'HEAD';
+ $isErrorResponse = !in_array($response->getStatusCode(), $this->getIgnoreResponseCodes());
+
+ if ($fallbackGetIfHeadFails && $isHead && $isErrorResponse) {
+ // It checks the link using GET method. After the check is finished,
+ // it resets the method to HEAD.
+ $link->setRequestMethod('GET')->save();
+ $this->check($link)
+ ->then(
+ function () use ($link) {
+ $link->setRequestMethod('HEAD')->save();
+ }
+ );
+ return NULL;
+ }
+
if (!empty($uri['fragment'])) {
$response = $response->withHeader('Fragment', $uri['fragment']);
}
@@ -204,6 +224,16 @@ class LinkCheckerService {
);
}

+ /**
+ * Extracts response codes which are not treated as broken links.
+ *
+ * @return string[]
+ * Array of response codes.
+ */
+ protected function getIgnoreResponseCodes() {
+ return preg_split('/(\r\n?|\n)/', $this->linkcheckerSetting->get('error.ignore_response_codes'));
+ }
+
/**
* Status code handling.
*
@@ -214,7 +244,7 @@ class LinkCheckerService {
* The link.
*/
protected function statusHandling(ResponseInterface $response, LinkCheckerLinkInterface $link) {
- $ignoreResponseCodes = preg_split('/(\r\n?|\n)/', $this->linkcheckerSetting->get('error.ignore_response_codes'));
+ $ignoreResponseCodes = $this->getIgnoreResponseCodes();

$error = $response->getReasonPhrase();
if (!isset($error)) {
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
],
"require": {
"php": "8.*",
"bower-asset/dragula": "^3.7",
"composer/installers": "^2",
"cweagans/composer-patches": "^1.7",
"drupal/address": "^2",
Expand All @@ -45,7 +46,6 @@
"drupal/core-composer-scaffold": "^10",
"drupal/core-project-message": "^10",
"drupal/core-recommended": "^10",
"drupal/csp": "^1.16",
"drupal/csv_serialization": "^4.0",
"drupal/dashboards": "^2.1",
"drupal/date_recur": "^3.2",
Expand All @@ -58,7 +58,7 @@
"drupal/group": "^2.0.0-rc1",
"drupal/imageapi_optimize_binaries": "^1.0@beta",
"drupal/imageapi_optimize_webp": "^2.0",
"drupal/imagemagick": "^3.7",
"drupal/imagemagick": "^4",
"drupal/inline_entity_form": "^3@RC",
"drupal/jquery_ui_dialog": "^2.0",
"drupal/layout_paragraphs": "^2",
Expand All @@ -78,7 +78,7 @@
"drupal/select2": "^1.13",
"drupal/seven": "^1.0@alpha",
"drupal/social_auth_hid": "^3.0",
"drupal/stage_file_proxy": "^2.1",
"drupal/stage_file_proxy": "^3",
"drupal/subgroup": "^2.0.0-beta1",
"drupal/theme_switcher": "^2",
"drupal/token_or": "^2.1",
Expand Down
Loading

0 comments on commit b3bd5dc

Please sign in to comment.