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

Add CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. #14468

Merged

Conversation

seamuslee001
Copy link
Contributor

…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

@civibot
Copy link

civibot bot commented Jun 6, 2019

(Standard links)

@civibot civibot bot added the master label Jun 6, 2019
@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch from 3448e11 to 0adb38b Compare June 6, 2019 22:42
@seamuslee001
Copy link
Contributor Author

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/

http_response_code($statusCode);
}
else {
header('X-PHP-Response-Code: ' . $statusCode, TRUE, $statusCode);
Copy link
Contributor

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?

* Return an HTTP Response with appropriate content and status code set.
* @param array $responseData
*/
public static function sendResponse($responseData) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Right, so this is capturing the main goal of extracting that logic and creating a CRM_Utils_System::sendResponse(...) function.

  2. 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")
    );
  3. IMHO, the entire implementation of the sendResponse() function should live in CRM_Utils_System_Base so that it can be overriden on a per-CMS basis.

  4. 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.

Copy link
Contributor Author

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

@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch 2 times, most recently from e80225e to d5ffdf7 Compare June 7, 2019 01:01
@eileenmcnaughton
Copy link
Contributor

@seamuslee001 jenkins wants a word with you

@seamuslee001
Copy link
Contributor Author

@eileenmcnaughton should be sorted now also ping again @totten

http_response_code($response->getStatusCode());
}
else {
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $statusCode->getStatusCode());
Copy link
Contributor

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?

Copy link
Member

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.

@@ -863,4 +863,23 @@ public function synchronizeUsers() {
];
}

/**
* Send an HTTP Response base on PSR HTTP RespnseInterface repsonse.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Contributor

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'

@eileenmcnaughton
Copy link
Contributor

Looks good to me - I made a couple of stylistic comments - hopefully @totten will give it another once over

@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch from 9bea5a5 to bf85cd5 Compare June 7, 2019 04:14
@seamuslee001
Copy link
Contributor Author

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/

// @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());
}
Copy link
Member

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.

// 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'));
Copy link
Member

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.)

@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch from bf85cd5 to ad0862b Compare June 8, 2019 03:08
@seamuslee001
Copy link
Contributor Author

@totten have updated now

@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch from ad0862b to 2131942 Compare June 8, 2019 03:26
@seamuslee001
Copy link
Contributor Author

Confirmed on the latest patches it ran successfully on E2E https://test.civicrm.org/job/CiviCRM-E2E-Matrix/345/

@totten totten changed the title Move content of returning response into CRM_Utils_System and have use… Extract new API, CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. Jun 8, 2019
@totten totten changed the title Extract new API, CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. Add CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. Jun 8, 2019
@totten
Copy link
Member

totten commented Jun 8, 2019

Huzza! Looking good.

I also did some r-run (click-around testing on pages that rely on dynamic assets and curl to check some of the HTTP headers on typical request) and executed the E2E test locally (D7 and WP). All looking good.

Added a small cleanup.

(CiviCRM Review Template WORD-1.2)

  • General standards
    • (r-explain) Needs work: This is both fixing a bug and exposing an improved API for sending AJAXy/non-HTML responses. We should communicate that.
    • (r-user) Pass: No UI impact
    • (r-doc) Pass
    • (r-run) Pass: Diid some click-around testing on pages that rely on dynamic assets. Used curl to fetch some of those assets and verify HTTP header comes through.
  • Developer standards
    • (r-tech) Pass
    • (r-code) Pass
    • (r-maint) Pass: Already has implicit test-coverage.
    • (r-test) Pending: New test-run is active. The relevant E2E_Core_AssetBuilderTests passes on my local D7+WP.

Merge on pass.

@seamuslee001 seamuslee001 force-pushed the e2e_asset_builder_test_wordpress branch from 81b216a to 90f6c8d Compare June 8, 2019 07:38
@seamuslee001 seamuslee001 changed the base branch from master to 5.15 June 8, 2019 07:39
@civibot civibot bot added 5.15 and removed master labels Jun 8, 2019
@seamuslee001 seamuslee001 merged commit b9ba847 into civicrm:5.15 Jun 8, 2019
@seamuslee001 seamuslee001 deleted the e2e_asset_builder_test_wordpress branch June 8, 2019 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants