Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Make it Optional to Save Response from the GET Request Task Routine #36926

Open
wants to merge 8 commits into
base: 5.2-dev
Choose a base branch
from
4 changes: 4 additions & 0 deletions administrator/language/en-GB/plg_task_requests.ini
Expand Up @@ -11,10 +11,14 @@ PLG_TASK_REQUESTS_LABEL_AUTH_HEADER="Authorization Header"
PLG_TASK_REQUESTS_LABEL_AUTH_KEY="Authorization Key"
PLG_TASK_REQUESTS_LABEL_REQUEST_TIMEOUT="Request Timeout"
PLG_TASK_REQUESTS_LABEL_REQUEST_URL="Request URL"
PLG_TASK_REQUESTS_LABEL_SAVE_RESPONSE="Save Response"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_DESC="Make GET requests to a server. Supports a custom timeout and authorization headers."
PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE="Request response code was: %1$d"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT="GET request failed or timed out."
PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT="Unable write output file!"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_NOT_SAVED="NOT_SAVED"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_ROUTINE_END_LOG_MESSAGE="GET return code is: %1$d. Processing Time: %2$.2f seconds"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_ROUTINE_OUTPUT="======= Task Output Body =======\n>URL: %1$s\n>Response Code: %2$d\n>Response: %3$s\n"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_SAVED="SAVED"
PLG_TASK_REQUESTS_TASK_GET_REQUEST_TITLE="GET Request"
PLG_TASK_REQUESTS_XML_DESCRIPTION="Job plugin to make GET requests to a server."
12 changes: 12 additions & 0 deletions plugins/task/requests/forms/get_requests.xml
Expand Up @@ -21,6 +21,18 @@
filter="int"
validate="number"
/>
<field
name="saveResponse"
type="radio"
label="PLG_TASK_REQUESTS_LABEL_SAVE_RESPONSE"
layout="joomla.form.field.radio.switcher"
default="0"
required="true"
filter="integer"
>
<option value="0">JDISABLED</option>
<option value="1">JENABLED</option>
</field>
<field
name="auth"
type="radio"
Expand Down
76 changes: 54 additions & 22 deletions plugins/task/requests/requests.php
Expand Up @@ -10,6 +10,7 @@
// Restrict direct access
defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Http\HttpFactory;
Expand Down Expand Up @@ -43,6 +44,12 @@ class PlgTaskRequests extends CMSPlugin implements SubscriberInterface
],
];

/**
* @var CMSApplication
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* @var boolean
* @since 4.1.0
Expand Down Expand Up @@ -80,12 +87,13 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
$id = $event->getTaskId();
$params = $event->getArgument('params');

$url = $params->url;
$timeout = $params->timeout;
$auth = (string) $params->auth ?? 0;
$authType = (string) $params->authType ?? '';
$authKey = (string) $params->authKey ?? '';
$headers = [];
$url = $params->url;
$timeout = $params->timeout;
$auth = ($params->auth ?? 0) === 1;
$authType = (string) $params->authType ?? '';
$authKey = (string) $params->authKey ?? '';
$saveResponse = ($params->saveResponse ?? 0) === 1;
$headers = [];

if ($auth && $authType && $authKey)
{
Expand All @@ -108,26 +116,20 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
$responseCode = $response->code;
$responseBody = $response->body;

// @todo this handling must be rethought and made safe. stands as a good demo right now.
$responseFilename = Path::clean(JPATH_ROOT . "/tmp/task_{$id}_response.html");
$responseSaved = false;

if (File::write($responseFilename, $responseBody))
{
$this->snapshot['output_file'] = $responseFilename;
$responseStatus = 'SAVED';
}
else
if ($saveResponse)
{
$this->logTask('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT', 'error');
$responseStatus = 'NOT_SAVED';
$this->saveResponse($id, $url, $responseCode, $responseBody);
$responseSaved = true;
}

$this->snapshot['output'] = <<< EOF
======= Task Output Body =======
> URL: $url
> Response Code: $responseCode
> Response: $responseStatus
EOF;
$responseStatus = (function() use ($responseSaved, $saveResponse) {
$status = $saveResponse && $responseSaved ? 'SAVED' : 'NOT_SAVED';
return Text::_("PLG_TASK_REQUESTS_TASK_GET_REQUEST_${status}");
})();

$this->snapshot['output'] = Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_OUTPUT', $responseCode, $responseStatus, $url);
ditsuke marked this conversation as resolved.
Show resolved Hide resolved

$this->logTask(Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE', $responseCode));

Expand All @@ -138,4 +140,34 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int

return TaskStatus::OK;
}

/**
* Save request response to task snapshot.
*
* @param integer $taskId ID of the task spawning the request routine.
* @param string $url Request URL.
* @param integer $responseCode Request response code.
* @param string $responseBody Response body.
*
* @return boolean Returns false on failure to save the response.
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
protected function saveResponse(int $taskId, string $url, int $responseCode, string $responseBody): bool
{
$tmpdir = $this->app->getConfig()->get('tmp_path');
$responseFilename = Path::check("${tmpdir}/task_{$taskId}_GET_response.html");

if (File::write($responseFilename, $responseBody))
{
$this->snapshot['output_file'] = $responseFilename;

return true;
}

$this->logTask('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT', 'error');

return false;
}
}