代理上的 Rails HTTP 请求中继器并返回响应

问题描述

我有一个要求,我必须将传入的 HTTP 请求中继到代理,然后按原样返回来自代理的响应。我使用了状态为 redirect_to307,它对 browsers 非常有效,但是 mobile 客户端失败了。我必须执行一些 business logic 才能决定进行重定向,这就是中继必须位于应用程序内部的原因。

因此,我需要某种中继器,以便将请求转发到新位置,并返回从新位置获得的响应。

假设我有两个应用,main.myapp.comproxy.myapp.com

以下适用于 browser agents,但不适用于 mobile apps

https://main.myapp.com/handler 完成的请求被中继到 https://proxy.myapp.com/handler,来自 proxy.myapp.com 的响应作为来自 main.myapp.com 的响应返回。

# controller on `main.myapp.com`
class SomeController < ApplicationController

  def handler
     redirect_to "https://proxy.myapp.com/handler",status: 307
  end

我想做类似的事情

class SomeController < ApplicationController

  def handler

    use_proxy = true/false

    if use_proxy
      # forward the request to the proxy server
      new_url = "https://proxy.myapp.com/handler"

      # relays and synchronously waits for response
      response = relay_request(request,url)

      # passes response as it is or some modified version
      render :response_status,json: :response_body
   elsif
     ...
   end
  end

如何实现 relay_request(request,url) 方法?只有位置会改变,一切(请求 cookie / 请求参数)将保持不变。它可以是 GETPOSTPUTDELETE 请求。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)