Skip to content

Downloading image with escaped spaces in URL via API causes Exception #623

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

Closed
qbixx opened this issue Jun 22, 2018 · 3 comments
Closed

Downloading image with escaped spaces in URL via API causes Exception #623

qbixx opened this issue Jun 22, 2018 · 3 comments

Comments

@qbixx
Copy link

qbixx commented Jun 22, 2018

Short description of the issue

When trying to download an image that has escaped spaces in the URL an Exception is thrown:

Error: Exception: File could not be downloaded (https://www.domain.com/inc/upload/jobs/1428/mobile/detail_jobs1428_Banner op hotel site-3.jpg) 400 Bad Request: 2: file_get_contents(https://www.domain.com/inc/upload/jobs/1428/mobile/detail_jobs1428_Banner op hotel site-3.jpg): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST , Bad Request (tried: fopen) (in /public_html/wire/core/WireHttp.php line 703)

The URL for the image actually has been escaped, the URL of the image is:
https://www.domain.com/inc/upload/jobs/1428/mobile/detail_jobs1428_Banner%20op%20hotel%20site-3.jpg

Expected behavior

Image should be downloaded and added to the page.

Actual behavior

Error: Exception: File could not be downloaded (https://www.domain.com/inc/upload/jobs/1428/mobile/detail_jobs1428_Banner op hotel site-3.jpg) 400 Bad Request: 2: file_get_contents(https://www.domain.com/inc/upload/jobs/1428/mobile/detail_jobs1428_Banner op hotel site-3.jpg): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST
, Bad Request (tried: fopen) (in /public_html/wire/core/WireHttp.php line 703)

#0 /public_html/wire/core/Pagefile.php(144): ProcessWire\WireHttp->download('https://www....', '/public...')
#1 /public_html/wire/core/Pageimage.php(1246): ProcessWire\Pagefile->___install('https://www....')
#2 /public_html/wire/core/Wire.php(383): ProcessWire\Pageimage->___install('https://www....')

This error message was shown because: you are logged in as a Superuser. Error has been logged.

Optional: Suggestion for a possible fix

I think the escaped URL is being parsed by ProcessWire before the actual download starts. This results in an invalid URL with spaces.

Steps to reproduce the issue

Try to set the image URL above through the API.

Setup/Environment

CentOS 7, NGINX, php-fpm

ProcessWire version: 3.0.98
(Optional) PHP version: 7.2
(Optional) MySQL version: MySQL (latest)

@Toutouwai
Copy link

WireHttp uses Sanitizer::url and encoded characters in the URL are replaced here. PHP's file_get_contents() needs spaces in a supplied URL to be encoded.

@ryancramerdesign, maybe Sanitizer::url could have a new option that allows encoded characters, and WireHttp could then use that option?

ryancramerdesign added a commit to processwire/processwire that referenced this issue Jun 27, 2018
…() method was not working with URLs having encoded spaces when downloading with the "fopen" option (the "curl" option was not affected). Added a couple new $options to $sanitizer->url() method for dictating how encoded characters should be handled. Added WireHttp::setValidateURLOptions() method for cases where you want to manually specify different options for validating the URL in WireHttp.
@ryancramerdesign
Copy link
Member

@Toutouwai That's a good idea, thanks. I have done that.

@qbixx Thanks for the report, I have pushed a fix for this issue.

@netcarver
Copy link
Collaborator

@qbixx Could you please close this issue if the fix Ryan has pushed resolves things for you. Many thanks!

@qbixx qbixx closed this as completed Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants