-
-
Notifications
You must be signed in to change notification settings - Fork 814
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
Add CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. #14468
Add CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. #14468
Conversation
(Standard links)
|
3448e11
to
0adb38b
Compare
I altered the E2E-Matrix test in Jenkins to apply this as a patch when creating the builds and then did a manual run just against the Master Branch and got all green https://test.civicrm.org/job/CiviCRM-E2E-Matrix/333/ |
CRM/Utils/System/Base.php
Outdated
http_response_code($statusCode); | ||
} | ||
else { | ||
header('X-PHP-Response-Code: ' . $statusCode, TRUE, $statusCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the above is php 5.4+ what is this one?
CRM/Utils/System.php
Outdated
* Return an HTTP Response with appropriate content and status code set. | ||
* @param array $responseData | ||
*/ | ||
public static function sendResponse($responseData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
Right, so this is capturing the main goal of extracting that logic and creating a
CRM_Utils_System::sendResponse(...)
function. -
I think this is too conservative in refactoring-technique -- i.e. it uses (by happenstance) the "HTTP-response data-model" from
AssetBuilder
rather than an "HTTP-response data-model" that's designed for general HTTP expectations. In particular, note that it supports one setting for one HTTP header (mimeType
/Content-Type
) -- but HTTP end-points may need to send many other headers. There are also edge-casey bits in handling the HTTP data (like distinguishing the status-code and the status-reason; like case-sensitivity in header names) which we haven't specified. PSR-7 is not perfect, but it does address the edgy bits, and it's standard enough that we may eventually get some leverage out of it.Consider this signature:
function sendResponse(\Psr\Http\Message\ResponseInterface $response)
and usage:
CRM_Utils_System::sendResponse( new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'text/csv'], "ab,cd\n12,34\n") );
-
IMHO, the entire implementation of the
sendResponse()
function should live inCRM_Utils_System_Base
so that it can be overriden on a per-CMS basis. -
As long as the interface is based on PSR-7, I don't think there's much risk that we're going to be changing the signature/inputs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@totten i have updated now as per your sample code and i think it looks pretty sensible now
e80225e
to
d5ffdf7
Compare
@seamuslee001 jenkins wants a word with you |
f8310d5
to
9bea5a5
Compare
@eileenmcnaughton should be sorted now also ping again @totten |
CRM/Utils/System/Base.php
Outdated
http_response_code($response->getStatusCode()); | ||
} | ||
else { | ||
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $statusCode->getStatusCode()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we at least have a comment why we need this - if we are keeping support for php < 5.4 to reduce the riskiness of the PR can we flag that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it'd be fine to just use http_response_code()
and remove the conditional.
However, that's a minor cleanup - doesn't need to block this.
CRM/Utils/System/WordPress.php
Outdated
@@ -863,4 +863,23 @@ public function synchronizeUsers() { | |||
]; | |||
} | |||
|
|||
/** | |||
* Send an HTTP Response base on PSR HTTP RespnseInterface repsonse. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you need to fix this comment block it's worth mentioning that the phpdoc std is a blank line after the first 'short sentence describing the function'
Looks good to me - I made a couple of stylistic comments - hopefully @totten will give it another once over |
9bea5a5
to
bf85cd5
Compare
I did another manual run of the e2e-matrix with this patch applied and it passed for all distros we are testing https://test.civicrm.org/job/CiviCRM-E2E-Matrix/339/ |
CRM/Utils/System/WordPress.php
Outdated
// @todo should we remove this given we are no longer supporting < PHP 5.6 however keeping | ||
// in for the moment for compatability with the original AssetBuilder code. | ||
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $response->getStatusCode()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For WP, isn't status_header()
sufficient? (Guess the E2E tests would tell!) The http_response_code()
and header()
formulations feel redundant.
CRM/Utils/System/Base.php
Outdated
// in for the moment for compatability with the original AssetBuilder code. | ||
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $reponse->getStatusCode()); | ||
} | ||
CRM_Utils_System::setHttpHeader('Content-Type', $response->getHeaderLine('Content-Type')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, shouldn't this be more like:
foreach ($message->getHeaders() as $name => $values) {
foreach ($values as $value) {
CRM_Utils_System::setHttpHeader($name, $value);
}
}
// or
foreach ($response->getHeaders() as $name => $values) {
CRM_Utils_System::setHttpHeader($name, implode(', ', $values));
}
(Riffing on the docblock for MessageInterface::getHeaders()
-- but I'm not sure which formulation is better here.)
bf85cd5
to
ad0862b
Compare
@totten have updated now |
ad0862b
to
2131942
Compare
Confirmed on the latest patches it ran successfully on E2E https://test.civicrm.org/job/CiviCRM-E2E-Matrix/345/ |
Huzza! Looking good. I also did some Added a small cleanup. (CiviCRM Review Template WORD-1.2)
Merge on pass. |
…r_system based method to set HTTP Status code
81b216a
to
90f6c8d
Compare
…r_system based method to set HTTP Status code
Overview
This aims to ensure that there is a consistent response returned between Drupal/Backdrop/WordPress in regards to Asset Builder URLs, at the moment when a URL is not found Drupal/Backdrop return a 404 status which Guzzle can pick up but WordPress returns 200 because of internal WordPress 404 handling, this fixes it so that WordPress will also return status of 404 if we want it to
Before
Different Responses sent and E2E Unit Tests fail on WordPress
After
Consistent passing on all 3 CMSes
ping @totten @eileenmcnaughton @kcristiano