From d65f9573279e0f8f6838c4003c4683d53545521a Mon Sep 17 00:00:00 2001 From: Sebastian Jambor Date: Wed, 1 Feb 2023 19:44:24 +0100 Subject: [PATCH] make json-ld endpoint non-blocking --- app/controllers/api/v1/json_ld_controller.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/json_ld_controller.rb b/app/controllers/api/v1/json_ld_controller.rb index 41bae6b66..9acecb8f9 100644 --- a/app/controllers/api/v1/json_ld_controller.rb +++ b/app/controllers/api/v1/json_ld_controller.rb @@ -12,10 +12,20 @@ class Api::V1::JsonLdController < Api::BaseController def show url = params[:url] - api_response = Faraday.get(url, nil, {'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'}) + request.env['rack.hijack'].call + io = request.env['rack.hijack_io'] + Thread.new { + begin + api_response = Faraday.get(url, nil, {'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'}) - response.headers['Content-Type'] = api_response.headers['Content-Type'] - - render body: api_response.body, status: api_response.status + io.write("HTTP/1.1 #{api_response.status}\r\n") + io.write("Content-Type: #{api_response.headers['Content-Type']}\r\n") + io.write("Connection: close\r\n") + io.write("\r\n") + io.write(api_response.body) + ensure + io.close + end + } end end