Jump to content

WireHttp: One Call, Two Requests [SOLVED]


FireWire
 Share

Recommended Posts

That's right, it's a 2 for 1 special.

I am using WireHttp to make a GET request but it is actually making 2 requests when calling WireHttp::get() only once.

It doesn't matter where I make this call, it always sends 2 requests. I stripped this down and wrote the request code in init.php to test with the bare minimum of ProcessWire booted. Here's my code:

<?php namespace ProcessWire;

/**
 * ProcessWire Bootstrap Initialization
 * ====================================
 * This init.php file is called during ProcessWire bootstrap initialization process.
 * This occurs after all autoload modules have been initialized, but before the current page
 * has been determined. This is a good place to attach hooks. You may place whatever you'd
 * like in this file. For example:
 *
 * $wire->addHookAfter('Page::render', function($event) {
 *   $event->return = str_replace("</body>", "<p>Hello World</p></body>", $event->return);
 * });
 *
 */

if (!defined("PROCESSWIRE")) die;

$http = new WireHttp;

// Create unique URL with random number to track method call vs. requests made.
$url = 'https://renovaenergy.ngrok.io/processwire/test/init/' . rand(1, 9999);

$http->get($url);

die; 

Here's the requests I'm receiving after loading the page once:

1173643480_Screenshotfrom2021-12-0221-15-25.png.4b3cafa86b3cf8cb028c3091ebe2e690.png

That test was done with all modules removed from the modules directory and the module cache cleared. I can't think of what is causing one get() call to create two requests.

Running PW v3.0.184

Edited by FireWire
Updated title to note as solved.
Link to comment
Share on other sites

When you look at the WireHTTP code you can see that when you're leaving the `use` options to `auto` (and no `fallback`) it will first try to send a cURL request and, should it fail, send another one using socket. In your case that's exactly what's happening since your tests return 404s.

  • Like 4
Link to comment
Share on other sites

3 hours ago, monollonom said:

When you look at the WireHTTP code you can see that when you're leaving the `use` options to `auto` (and no `fallback`) it will first try to send a cURL request and, should it fail, send another one using socket. In your case that's exactly what's happening since your tests return 404s.

Got it. This introduces something that isn't noticeable even when reading the method documentation. In HTTP terms a 404 status isn't a failure, it's a successful response from the remote server that should be interpreted by the application making the request. When I read fallback, I assumed that it's because the request attempt failed, for example if CURL isn't available on the system it falls back to fopen.

A use case I have is to check that a page exists and then let the user know, so a 404 is useful because the user is shown the error, they can fix the URL, and then the new URL is tested. Sending via 2 different methods the user must wait 3x longer.

1599443178_Screenshotfrom2021-12-0304-06-23.png.3d472a8dd939fe5c727e3b1a64d9abae.png

 The immediate solution is to manually add parameters to every WireHttp call to disable the default configuration, but that seems like an extra step to keep WireHttp from ignoring a valid response. All that said, does this have some sort of benefit I'm not seeing? When should a 404 be treated like a failure rather than an informative response to a successful request?

First time using WireHttp. I've always written my own CURL methods, just trying to make sure I'm not missing a benefit that this has.

  • Like 1
Link to comment
Share on other sites

Honestly I'm not that familiar with WireHttp, it was just a way for me to have a look at the source code and learn something as well 😅

Looking again at the code it seems you can get more information about the error by calling print_r($http->error). Maybe it's not the 404 triggering an error in the cURL call but something else ? Again no expert, just trying to help.

  • Like 1
Link to comment
Share on other sites

Agree with @FireWire. This looks like the behaviour is wrong and an issue should be raised in the issues repository. The problem seems to lie in this line: https://github.com/processwire/processwire/blob/d78276e2c265f6b70384a13eb4febd4811a1db77/wire/core/WireHttp.php#L563

Every statuscode should be treated as a successful response, as long as the server was able to contact the remote server.

 

EDIT:
The comment on top of the line of code linked above says `no need to fallback to sockets`, but then the $result is set to false. I think this is meant to be a `$result = true;` instead of `$result = false;`. Then the rest of the logic would seem to be correct again.

  • Like 3
Link to comment
Share on other sites

2 hours ago, monollonom said:

Honestly I'm not that familiar with WireHttp, it was just a way for me to have a look at the source code and learn something as well 😅

Looking again at the code it seems you can get more information about the error by calling print_r($http->error). Maybe it's not the 404 triggering an error in the cURL call but something else ? Again no expert, just trying to help.

Really appreciate your help as always! This was just a little bit of a deeper dive than usual and I was doing a lot of thinking out loud so to speak. Just putting the big questions out there.

As for the `$http->error`, that property is only available after it tries two times.

Link to comment
Share on other sites

  • FireWire changed the title to WireHttp: One Call, Two Requests [SOLVED]

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...