FIX: ensure proper header transfer (except for cache control)

allows discourse special headers to be visible on hijacked reqs
This commit is contained in:
Sam 2018-01-21 14:26:42 +11:00
parent 7b05b12d3c
commit fc36f095a7
2 changed files with 23 additions and 5 deletions

View File

@ -23,6 +23,8 @@ module Hijack
io = hijack.call io = hijack.call
original_headers = response.headers
Scheduler::Defer.later("hijack #{params["controller"]} #{params["action"]}") do Scheduler::Defer.later("hijack #{params["controller"]} #{params["action"]}") do
MethodProfiler.start(transfer_timings) MethodProfiler.start(transfer_timings)
@ -39,6 +41,12 @@ module Hijack
instance.response = response instance.response = response
instance.request = request_copy instance.request = request_copy
original_headers&.each do |k, v|
# hash special handling so skip
if k != "Cache-Control"
instance.response.headers[k] = v
end
end
begin begin
instance.instance_eval(&blk) instance.instance_eval(&blk)
@ -68,13 +76,13 @@ module Hijack
status_string = Rack::Utils::HTTP_STATUS_CODES[response.status.to_i] || "Unknown" status_string = Rack::Utils::HTTP_STATUS_CODES[response.status.to_i] || "Unknown"
io.write "#{response.status} #{status_string}\r\n" io.write "#{response.status} #{status_string}\r\n"
headers.each do |name, val|
io.write "#{name}: #{val}\r\n"
end
timings = MethodProfiler.stop timings = MethodProfiler.stop
if timings && duration = timings[:total_duration] if timings && duration = timings[:total_duration]
io.write "X-Runtime: #{"%0.6f" % duration}\r\n" headers["X-Runtime"] = "#{"%0.6f" % duration}"
end
headers.each do |name, val|
io.write "#{name}: #{val}\r\n"
end end
io.write "\r\n" io.write "\r\n"

View File

@ -114,6 +114,16 @@ describe Hijack do
expect(headers).to eq(expected) expect(headers).to eq(expected)
end end
it "handles transfers headers" do
tester.response.headers["Hello-World"] = "sam"
tester.hijack_test do
expires_in 1.year
render body: "hello world", status: 402
end
expect(tester.io.string).to include("Hello-World: sam")
end
it "handles expires_in" do it "handles expires_in" do
tester.hijack_test do tester.hijack_test do
expires_in 1.year expires_in 1.year