2017-11-24 12:31:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
require_dependency 'method_profiler'
|
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
# This module allows us to hijack a request and send it to the client in the deferred job queue
|
|
|
|
# For cases where we are making remote calls like onebox or proxying files and so on this helps
|
|
|
|
# free up a unicorn worker while the remote IO is happening
|
|
|
|
module Hijack
|
2017-11-27 11:50:57 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
def hijack(&blk)
|
2017-11-27 14:43:24 +08:00
|
|
|
controller_class = self.class
|
2017-11-28 06:28:40 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
if hijack = request.env['rack.hijack']
|
|
|
|
|
2017-11-28 13:47:20 +08:00
|
|
|
request.env['discourse.request_tracker.skip'] = true
|
|
|
|
request_tracker = request.env['discourse.request_tracker']
|
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
# in the past unicorn would recycle env, this is not longer the case
|
|
|
|
env = request.env
|
|
|
|
request_copy = ActionDispatch::Request.new(env)
|
2017-11-28 13:47:20 +08:00
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
transfer_timings = MethodProfiler.transfer
|
2017-11-28 13:47:20 +08:00
|
|
|
|
2017-11-28 15:21:45 +08:00
|
|
|
io = hijack.call
|
|
|
|
|
|
|
|
Scheduler::Defer.later("hijack #{params["controller"]} #{params["action"]}") do
|
2017-11-24 12:31:23 +08:00
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
MethodProfiler.start(transfer_timings)
|
2017-11-24 12:31:23 +08:00
|
|
|
begin
|
2018-01-19 05:26:18 +08:00
|
|
|
Thread.current[Logster::Logger::LOGSTER_ENV] = env
|
2017-11-24 12:31:23 +08:00
|
|
|
# do this first to confirm we have a working connection
|
|
|
|
# before doing any work
|
|
|
|
io.write "HTTP/1.1 "
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
# this trick avoids double render, also avoids any litter that the controller hooks
|
|
|
|
# place on the response
|
|
|
|
instance = controller_class.new
|
|
|
|
response = ActionDispatch::Response.new
|
|
|
|
instance.response = response
|
2017-11-28 07:59:53 +08:00
|
|
|
|
2017-11-28 06:28:40 +08:00
|
|
|
instance.request = request_copy
|
2017-11-27 14:43:24 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
begin
|
2017-11-27 14:43:24 +08:00
|
|
|
instance.instance_eval(&blk)
|
2017-11-24 12:31:23 +08:00
|
|
|
rescue => e
|
2017-12-01 13:23:21 +08:00
|
|
|
# TODO we need to reuse our exception handling in ApplicationController
|
2018-01-19 05:26:18 +08:00
|
|
|
Discourse.warn_exception(e, message: "Failed to process hijacked response correctly", env: env)
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
|
|
|
|
2017-11-28 07:59:53 +08:00
|
|
|
unless instance.response_body || response.committed?
|
2017-11-27 14:43:24 +08:00
|
|
|
instance.status = 500
|
|
|
|
end
|
|
|
|
|
|
|
|
response.commit!
|
|
|
|
|
|
|
|
body = response.body
|
|
|
|
|
|
|
|
headers = response.headers
|
2017-12-07 07:30:50 +08:00
|
|
|
# add cors if needed
|
2018-01-19 05:26:18 +08:00
|
|
|
if cors_origins = env[Discourse::Cors::ORIGINS_ENV]
|
|
|
|
Discourse::Cors.apply_headers(cors_origins, env, headers)
|
2017-12-07 07:30:50 +08:00
|
|
|
end
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
headers['Content-Length'] = body.bytesize
|
|
|
|
headers['Content-Type'] = response.content_type || "text/plain"
|
2017-11-27 11:50:57 +08:00
|
|
|
headers['Connection'] = "close"
|
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
status_string = Rack::Utils::HTTP_STATUS_CODES[response.status.to_i] || "Unknown"
|
|
|
|
io.write "#{response.status} #{status_string}\r\n"
|
2017-11-27 11:50:57 +08:00
|
|
|
|
|
|
|
headers.each do |name, val|
|
|
|
|
io.write "#{name}: #{val}\r\n"
|
|
|
|
end
|
|
|
|
|
2018-01-19 05:26:18 +08:00
|
|
|
timings = MethodProfiler.stop
|
|
|
|
if timings && duration = timings[:total_duration]
|
|
|
|
io.write "X-Runtime: #{"%0.6f" % duration}\r\n"
|
|
|
|
end
|
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
io.write "\r\n"
|
2017-11-27 14:43:24 +08:00
|
|
|
io.write body
|
2017-11-24 12:31:23 +08:00
|
|
|
rescue Errno::EPIPE, IOError
|
|
|
|
# happens if client terminated before we responded, ignore
|
2017-11-29 08:54:20 +08:00
|
|
|
io = nil
|
2017-11-28 13:47:20 +08:00
|
|
|
ensure
|
2018-01-19 05:26:18 +08:00
|
|
|
MethodProfiler.clear
|
2017-12-01 13:23:21 +08:00
|
|
|
Thread.current[Logster::Logger::LOGSTER_ENV] = nil
|
|
|
|
|
2017-11-29 08:54:20 +08:00
|
|
|
io.close if io rescue nil
|
|
|
|
|
2017-11-28 13:47:20 +08:00
|
|
|
if request_tracker
|
2018-01-19 07:37:27 +08:00
|
|
|
status = response.status rescue 500
|
2018-01-19 05:26:18 +08:00
|
|
|
request_tracker.log_request_info(env, [status, headers || {}, []], timings)
|
2017-11-28 13:47:20 +08:00
|
|
|
end
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# not leaked out, we use 418 ... I am a teapot to denote that we are hijacked
|
|
|
|
render plain: "", status: 418
|
|
|
|
else
|
2017-11-27 14:43:24 +08:00
|
|
|
blk.call
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|