innoveit / play2-pdf

A PDF module for Play Framework 2
MIT License
77 stars 22 forks source link

[BUG] problems with reading images with proxy_pass #37

Open eximius313 opened 5 years ago

eximius313 commented 5 years ago

Let's say we have a template:

<html>
<head>
  <meta charset="UTF-8">
  <title>my title</title>
</head>
<body>
<div class="photo"
      style="background-image: url('@routes.ImageController.myPicture("funnyCat")')"></div>
</body>
</html>

and we use it like this:

  public Result preparePdf() {
    final String host = routes.HomeController.index().absoluteURL(request());
    return ok(pdfGenerator.toBytes(views.html.myPdf.render(), host))
        .withHeader("Content-Disposition", "attachment; filename*=UTF-8''My.pdf")
        .as("application/pdf");
  }

and - what is important - our application.conf looks like this:

play {
  http {
    secret.key="secret_here"
    hostname = "myhost.io"
  }
  filters {
    enabled=[play.filters.hosts.AllowedHostsFilter]
    hosts.allowed=["myhost.io"]
  }
}

If you run stage locally - everything works fine and photos are displayed in PDF. BUT if you are running your server with Apache mod_proxy:

    ProxyRequests Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode

    ProxyPass           /               http://localhost:9009/  nocanon
    ProxyPassReverse    /               http://localhost:9009/

then even if: final String host = routes.HomeController.index().absoluteURL(request()); points correctly to https://myhost.io, PDF is generated but no photos are displayed, nor exception is thrown. It just fails silently!

Workaround that I found after hours of investigation is that you must use http://localhost:9009 for both:

//    final String host = routes.HomeController.index().absoluteURL(request());
    final String host = "http://localhost:9009";

and

  filters {
    enabled=[play.filters.hosts.AllowedHostsFilter]
    hosts.allowed=["myhost.io", "localhost:9009"]
  }

Why play2-pdf reads photos from localhost:9009 even though I'm pointing it specifically to myhost.io?