Comments

sutharsan’s picture

StatusFileSize
new3.57 KB

Converting the drupal_http_request to Guzzle. The patch removes the functionality to detect a redirect and store the resulting uri. This code came from the original l10n_update module, but no other core implementation of drupal_http_redirect does this and Guzzle (as it is used here) does not return any header information when a 301 occurs.

berdir’s picture

There seems to be a getLocation() method on the Response object, does this return that information?

This feature has been removed in the aggregator implementation, but I actually think that this is incorrect, as I've written in #1447736-187: Adopt Guzzle library to replace drupal_http_request().

sutharsan’s picture

I've tried both getLocation() and getRawHeaders() and both return not data (NULL and not the expected header info). I've tested with two kinds of 301 redirects: a redirecting drupal URL (Redirect module on a D7 site) and a "Redirect 301 ..." in .htaccess. The getStatusCode() method returns 200 in all cases. So I think I have found a bug in Guzzle (implementation). I will investigate further.

berdir’s picture

Ah right. Because the final response *is* a 200 OK and has no Location header, obviously.

I think that's what getPreviousResponse() is for, you could call that and check if it is a redirect/has a Location.

sutharsan’s picture

StatusFileSize
new1.61 KB
new3.9 KB

Thanks, I found getPreviousResponse too.

sutharsan’s picture

StatusFileSize
new844 bytes
new3.92 KB

A slightly more selective one, only store the final url on 301.

yesct’s picture

#6: locale-guzzle-1875614-6.patch queued for re-testing.

sutharsan’s picture

StatusFileSize
new2.61 KB
new4.68 KB

With the new CurlException the exception handling van be better grouped. Although not essentially different form the previous patch, I think the code is more clear this way. It uses three catch methods:

  catch (CurlException $e) {
    // Handle connection problems and cURL specific errors.
    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $e->getMessage()));
  }
  catch (BadResponseException $e) {
    // Handle 4xx and 5xx http responses.
    ...
    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $response->getReasonPhrase()));
  }
  catch (RequestException $e) {
    // Handle other http related errors.
    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $e->getMessage()));
  }
sutharsan’s picture

Ehm, CurlException is not new. It was only the error message that was in improved in the latest release.

berdir’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,49 @@ function locale_translation_batch_fetch_finished($success, $results) {
+  catch (CurlException $e) {
+    // Handle connection problems and cURL specific errors.
+    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $e->getMessage()));
...
+  catch (RequestException $e) {
+    // Handle other http related errors.
+    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $e->getMessage()));

CurlException is a childclass of RequestException, so you don't need the CurlException block unless you actually want to do something different with it.

Otherwise, this is a nice example of when you actually have a case for handling those exceptions differently.

sutharsan’s picture

StatusFileSize
new1.41 KB
new4.53 KB

@berdir, unfortunately I don't have a case to handle curl exceptions differently ;)
Removed the CurlException handling.

Status: Needs review » Needs work

The last submitted patch, locale-guzzle-1875614-11.patch, failed testing.

berdir’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -5,6 +5,9 @@
+use Guzzle\Http\Exception\BadResponseException;
+use Guzzle\Http\Exception\CurlException;

Should have a use for RequestException instead now.

sutharsan’s picture

Status: Needs work » Needs review
StatusFileSize
new552 bytes
new4.54 KB

Done.

berdir’s picture

Status: Needs review » Needs work
+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
+  $request = drupal_container()->get('http_default_client')->head($uri);

This should now use Drupal::httpClient().

yesct’s picture

Status: Needs work » Needs review
StatusFileSize
new907 bytes
new4.54 KB
+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
  * @param string $uri
  *   URI of remote file.
- * @param array $headers
- *   HTTP request headers.
- * @return stdClass
- *   Result object containing the HTTP request headers, response code, headers,
- *   data, redirect status and updated timestamp.
+ * @return array
+ *   Array containing the last modified timestamp of the translation file.
  *   TRUE if the file is not found.
  *   FALSE if a fault occured.

looks like the true false lines are inaccurate now.

(also missing newline before @return)

I tried to address #15
and looked at the sample code in other guzzle conversions and the change notice:
http://drupal.org/node/1862446

But I'm still not sure.

Also, I didn't know if, like the change notice sample, if the setup should happen before the try {} and then in the try, do the ->send.

Status: Needs review » Needs work

The last submitted patch, locale-guzzle-1875614-16.patch, failed testing.

berdir’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -436,16 +436,17 @@ function locale_translation_batch_fetch_finished($success, $results) {
-    $response = $request->send();
+    $response = Drupal::httpClient()
+      ->get('http_default_client')
+      ->head($uri)

get() was a method on the drupal_container(), this just needs Drupal::httpClient()->head()->send().

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new4.54 KB
new1.01 KB

This should work.

sutharsan’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
+    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase()));

Why a string concatenation like this? It fails in rtl languages. Also the content returned by getReasonPhrase() may not be safe as it can contain the response message body. See Response->getMessage().

berdir’s picture

That's how we did in all other places where we logged the reason. It's an untranslated string anyway (e.g. "403 Access Denied") so LTR/RTL shouldn't really matter?

I don't see how getReasonPhrase could contain the body? getMessage() doesn't do anything with the reason phrase and if you look at getRawHeaders(), it's done in a very similar way there.

ParisLiakos’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
- *   TRUE if the file is not found.
- *   FALSE if a fault occured.

why are we removing those from docblock? i see the behavior is the same after the patch

+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
+ *   Array containing the last modified timestamp of the translation file.

maybe add: and the file location if a redirect occurs

sutharsan’s picture

@berdir, I agree that the reason phrase is a hardcoded status text and not message body text. I can't reproduce how I got to 'getMessage' but it is indeed not called and not related.
For RTL languages it does matter. With this concatenation we place the reason phrase in front of the "HTTP request to @url ..." sentence, and not behind as in LTR languages. It is hard coded, it can not be changed. When using t() the order can be changed by the translator, that is the whole difference. Alternatively we can contatenate the strings with a <br />.

berdir’s picture

+++ b/core/modules/locale/locale.batch.incundefined
@@ -433,39 +436,46 @@ function locale_translation_batch_fetch_finished($success, $results) {
+    watchdog('locale', 'HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase()));

Sorry but I still don't get what you mean.

The status code and reason phrase *is* a t() argument that is injected into a placeholder.

So a RTL language would display it as "403 Access denied rorre htiw deliaf lru@ ot tseuqer PTTH" (Yes, I actually reversed the english string ;)), if the placeholder is on the left side. So the only thing we could do is split it up and have to placeholders as @errorcode @errormessage so that they could reverse that too. But given that were talking about a string that is very likely in English, I'm not sure that makes sense? If you think it does, feel free to change :)

sutharsan’s picture

Oops, now I see. Yes it is a t() argument. Sorry for the confusion, I must have been away from core development too long ;)

berdir’s picture

@Sutharsan: Great, can you explain what's the deal with those @return documentation changes pointed out in #22? Once that's updated then we can get this in, this is one of two remaining conversion issues now and the last that's not RTBC :)

sutharsan’s picture

StatusFileSize
new994 bytes
new4.79 KB

Comment changed as suggested by rootatwc.

ParisLiakos’s picture

Status: Needs review » Reviewed & tested by the community

looks good to go now, thanks:)

webchick’s picture

Status: Reviewed & tested by the community » Fixed

All right, let's get these out of the way so we can close a major!

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.