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

Bug: curlrequest not using baseURI on localhost #4707

Closed
lumin888 opened this issue May 19, 2021 · 2 comments · Fixed by #4715
Closed

Bug: curlrequest not using baseURI on localhost #4707

lumin888 opened this issue May 19, 2021 · 2 comments · Fixed by #4715
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@lumin888
Copy link

Describe the bug
I've just updated to the latest release (from v4.0.4) and noticed my previously working curlrequest code is broken on my development environment.

The baseURI I supply in the options is not used. Instead, it uses the CI project's URL as the baseURI.

This issue occurs when the baseURI is on localhost. Changing the baseURI to something other than localhost works as expected.

Has something changed in the way curlrequest works on localhost?

CodeIgniter 4 version
CI 4.1.2 - failed
CI 4.0.4 - working

Affected module(s)
Unsure

Expected behavior, and steps to reproduce if appropriate
My CI controller is located at appstarter/app/Controllers/example.php

It contains this fragment:

$options = [
        'baseURI' => 'http://localhost/api/v1/'
];

$client = \Config\Services::curlrequest($options);

$response = $client->get('some/function/');

If I execute via http://localhost/appstarter/public/example, the get request attempts to fetch http://localhost/appstarter/public/api/v1/some/function, which does not exist.

I would have expected the get request to fetch http://localhost/api/v1/some/function

Context

  • OS: Windows 10
  • Web server: Xammp 7.4.18
  • PHP version: 7.4.18
@lumin888 lumin888 added the bug Verified issues on the current code behavior or pull requests that will fix them label May 19, 2021
@MGatner
Copy link
Member

MGatner commented May 19, 2021

This was actually a problem introduced in 4.0.5 that we attempted to mitigate in 4.1.2. When URI is cast as a string it assumed it should be relative to the App config settings. In 4.1.2 this was restricted only to instance when the host matched:

// If the hosts matches then assume this should be relative to baseURL
if ($this->getHost() === $baseUri->getHost())
{
// Check for additional segments
$basePath = trim($baseUri->getPath(), '/') . '/';
$trimPath = ltrim($path, '/');
if ($basePath !== '/' && strpos($trimPath, $basePath) !== 0)
{
$path = $basePath . $trimPath;
}
// Check for forced HTTPS
if ($config->forceGlobalSecureRequests)
{
$scheme = 'https';
}
}

It looks like me like URI is identifying your CURL URL as a relative one, even though it isn't. Probably the framework fix would be to have CURLRequest::prepareURL() to use URI:: createURIString() instead of the cast (which I consider problematic).

In your case, you could try altering your baseURL in app/Config/App.php or .env, assuming this isn't a browsable web app anyway.

@lumin888
Copy link
Author

Thanks for the explanation of the problem.

I was able to restore the functionality on my dev environment, by using 127.0.0.1 in the curl options instead of localhost to prevent a match.

Let me know if you want me to close this issue or leave it open.

MGatner added a commit to MGatner/CodeIgniter4 that referenced this issue May 21, 2021
@MGatner MGatner mentioned this issue May 21, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants