While using subrequests I ran into the issue that my responses din't contain the correct base url but always referred to localhost.

For example images were listed as http://localhost/sites/default/files/... instead of the correct http://website_base_url/sites/default/files/... This wasn't the case in individual requests but only appeared while using subrequests.

After some experimenting I noticed that besides the X-Consumer-ID (*) it's also needed to include a Host in the request headers. After adding a host in my request headers the correct base url was determined based on my Host parameter.

(*) Besides that the X-Consumer-ID is needed to retrieve the correct image styles while using the consumer_image_styles module.

This isn't a bug or issue but worth mentioning for others running into this in the future.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sander Hofman created an issue. See original summary.

e0ipso’s picture

Can you please post:

  1. The blueprint that had the localhost problem along with the response?
  2. The blueprint with the problem fixed along with the response?
  3. Other relevant modules that are causing possible inteactions?

Thanks for the report!

smanhoff’s picture

First I used following request to retrieve a node based on url, while using the decoupled router module:

[{
    "requestId": "resolvedRoute",
    "action": "view",
    "uri": "/router/translate-path?path=/homepage&_format=json",
    "headers": {
        "Content-Type": "application/vnd.api+json"
    }
}]

But this only got me the following response:

"body": "{"resolved":"http://localhost/homepage","entity":{"canonical":"http://localhost/homepage","type":"node","bundle":"homepage","id":"9","uuid":"4e1f5a9b-5ed6-484f-ac21-583612fa21df"},"jsonapi":{"individual":"http://localhost/jsonapi/node/homepage/4e1f5a9b-5ed6-484f-ac21-583612fa21df","resourceName":"node--homepage","pathPrefix":"jsonapi"}}"

As you can see it always uses localhost instead of the correct base_url. When using the exact same request without subrequests this doesn't occur. So after some digging I figured out that this could be resolved by applying the Host parameter in my request header:

[{
    "requestId": "resolvedRoute",
    "action": "view",
    "uri": "/router/translate-path?path=/homepage&_format=json",
    "headers": {
        "Content-Type": "application/vnd.api+json",
        "Host": "host_url"
    }
}]

And now based on the Host parameter the correct base url is used throughout the response:

"body": "{"resolved":"host_url/homepage","entity":{"canonical":"host_url/homepage","type":"node","bundle":"homepage","id":"9","uuid":"4e1f5a9b-5ed6-484f-ac21-583612fa21df"},"jsonapi":{"individual":"host_url/jsonapi/node/homepage/4e1f5a9b-5ed6-484f-ac21-583612fa21df","resourceName":"node--homepage","pathPrefix":"jsonapi"}}"

e0ipso’s picture

I think it makes sense to forward the Host header to subrequests.

e0ipso’s picture

Status: Active » Needs review
FileSize
1.82 KB

@Sander Hofman can you please see if this patch solves the issue?

Many thanks!

e0ipso’s picture

There was a typo in the patch above. Correcting.

smanhoff’s picture

@e0ipso

Above patch doesn't seem to resolve the issue.

It seems the if statement never gets executed due to the host (localhost) is always included in the $to headers.

    if (!$to->headers->get($name)) {
      $master_header = $from->headers->get($name);
      $to->headers->set($name, $master_header);
    }

If I always forward the master host to the subrequest it seems to work fine but this may cause some unexpected behavior maybe?

e0ipso’s picture

:-( you are right, Symfony must be setting that under the hood. We need to check the presence of the header in the blueprint. But in that point it's hard to inject the parent's header.

e0ipso’s picture

@Sander Hofman can you test this one? I don't have a proper environment currently so I'm shooting in the dark.

Status: Needs review » Needs work

The last submitted patch, 9: 2938013--fwd-host-header--9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ziomizar’s picture

@e0ipso i tested the patch in #9 and it works for me. Thanks

Without patch i have localhost in the response. After apply the patch applyed i get the correct base url of the site.

ziomizar’s picture

Status: Needs work » Reviewed & tested by the community

  • e0ipso committed 415f6dd on 8.x-2.x
    Issue #2938013 by e0ipso, Sander Hofman, ziomizar: Include host in...
e0ipso’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Status: Fixed » Closed (fixed)

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