Skip to content

Commit 72b4b98

Browse files
authored
Plug: Forward requests to callback instead of passing assigns (#283)
This patch refactors the "AssignsPlug" to not only forward assigns, but instead forward the conn to a function for processing the request.
1 parent 9e2929c commit 72b4b98

25 files changed

+330
-230
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
### Added
44

5+
- Add `ChromicPDF.Plug` to forward Chrome requests on an internal endpoint to a template.
56
- Add `Template.options/1` that returns the options for page dimensions, header, and footer, but does not require the content.
6-
- `ChromicPDF.Plug` mechanism to pass assigns to a template printed from `:url`.
77

88
### Changed
99

1010
- Strip styles generated in `Template.styles/1`.
11+
- Cookies set via `:set_cookie` are now `httpOnly: true` by default.
1112

1213
### Removed
1314

lib/chromic_pdf/api/pdf_options.ex

+19-30
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,38 @@ defmodule ChromicPDF.PDFOptions do
44
@moduledoc false
55

66
require EEx
7+
import ChromicPDF.Utils, only: [rendered_to_binary: 1]
78

89
def prepare_input_options(source, opts) do
910
opts
10-
|> set_cookie_for_assigns_plug(source)
1111
|> put_source(source)
1212
|> replace_wait_for_with_evaluate()
1313
|> stringify_map_keys()
14-
|> iolists_to_binary()
14+
|> sanitize_binaries()
1515
end
1616

17-
defp set_cookie_for_assigns_plug(opts, source) do
18-
cond do
19-
!Keyword.has_key?(opts, :assigns) ->
20-
opts
21-
22-
!match?({:url, _}, source) ->
23-
raise(":assigns option invalid with :url source")
24-
25-
Keyword.has_key?(opts, :set_cookie) ->
26-
raise(":assigns option conflicts with :set_cookie")
17+
defp put_source(opts, {:file, source}), do: put_source(opts, {:url, source})
18+
defp put_source(opts, {:path, source}), do: put_source(opts, {:url, source})
19+
defp put_source(opts, {:html, source}), do: put_source(opts, :html, source)
2720

28-
true ->
29-
do_set_cookie_for_assigns_plug(opts, source)
21+
defp put_source(opts, {:plug, plug_opts}) do
22+
if Keyword.has_key?(opts, :set_cookie) do
23+
raise "plug source conflicts with set_cookie"
3024
end
31-
end
3225

33-
defp do_set_cookie_for_assigns_plug(opts, {:url, url}) do
34-
{assigns, rest} = Keyword.pop(opts, :assigns)
26+
{url, plug_opts} = Keyword.pop!(plug_opts, :url)
3527

3628
set_cookie_opts =
37-
assigns
38-
|> ChromicPDF.AssignsPlug.start_agent_and_get_cookie()
29+
plug_opts
30+
|> ChromicPDF.Plug.start_agent_and_get_cookie()
3931
|> Map.put(:url, url)
32+
|> Map.put(:secure, String.starts_with?(url, "https"))
4033

41-
Keyword.put(rest, :set_cookie, set_cookie_opts)
34+
opts
35+
|> Keyword.put(:set_cookie, set_cookie_opts)
36+
|> put_source(:url, url)
4237
end
4338

44-
defp put_source(opts, {:file, source}), do: put_source(opts, {:url, source})
45-
defp put_source(opts, {:path, source}), do: put_source(opts, {:url, source})
46-
defp put_source(opts, {:html, source}), do: put_source(opts, :html, source)
47-
4839
defp put_source(opts, {:url, source}) do
4940
url =
5041
if File.exists?(source) do
@@ -110,19 +101,17 @@ defmodule ChromicPDF.PDFOptions do
110101
Enum.into(map, %{}, fn {k, v} -> {to_string(k), v} end)
111102
end
112103

113-
@iolist_options [
104+
@binary_options [
114105
[:html],
115106
[:print_to_pdf, "headerTemplate"],
116107
[:print_to_pdf, "footerTemplate"]
117108
]
118109

119-
defp iolists_to_binary(opts) do
120-
Enum.reduce(@iolist_options, opts, fn path, acc ->
110+
defp sanitize_binaries(opts) do
111+
Enum.reduce(@binary_options, opts, fn path, acc ->
121112
update_in(acc, path, fn
122113
nil -> ""
123-
{:safe, value} -> :erlang.iolist_to_binary(value)
124-
value when is_list(value) -> :erlang.iolist_to_binary(value)
125-
value -> value
114+
other -> rendered_to_binary(other)
126115
end)
127116
end)
128117
end

lib/chromic_pdf/assigns_plug.ex

-123
This file was deleted.

lib/chromic_pdf/pdf/protocols/navigate.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule ChromicPDF.Navigate do
77

88
steps do
99
if_option :set_cookie do
10-
call(:set_cookie, "Network.setCookie", &Map.fetch!(&1, :set_cookie), %{})
10+
call(:set_cookie, "Network.setCookie", &Map.fetch!(&1, :set_cookie), %{httpOnly: true})
11+
1112
await_response(:cookie_set, [])
1213
end
1314

0 commit comments

Comments
 (0)