-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
64 lines (58 loc) · 1.67 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'rubygems'
require 'bundler'
Bundler.require
# Monkey-patching Rack::Request to add custom helper methods
module Rack
class Request
def supported_method?
get? || post?
end
def valid?
path_info.start_with?(
'/proxy/',
) && supported_method?
end
def request_headers
{
'Content-Type' => media_type,
'User-Agent' => env['HTTP_USER_AGENT'],
'X-Forwarded-For' => env['HTTP_X_FORWARDED_FOR'],
}.select{|k,v| !v.nil? }
end
end
end
class HttpProxy
def initialize
@patron_pool = ConnectionPool.new(size: 200, timeout: 15) do
Patron::Session.new do |s|
s.timeout = 1200
s.connect_timeout = 5
end
end
end
def call(env)
begin
req = Rack::Request.new(env)
if req.valid?
url = req.path_info[7..-1]
url += "?#{req.query_string}" unless req.query_string.empty?
body = req.body.read
options = {max_redirects: 0}.merge(body.nil? ? {} : { data: body, multipart: false })
@patron_pool.with do |session|
response = session.request req.request_method, url, req.request_headers, options
remapped_headers = response.headers.select{|k,v| k != "Transfer-Encoding"}
.map {|k,v| [k, v.respond_to?(:join) ? v.join(",") : v] }
[response.status, remapped_headers, [response.body]]
end
else
[ 400, {}, ["Unsupported URL"] ]
end
rescue => exception
[ 400, {}, ["#{exception.class}: #{exception.message}"] ]
end
end
end
Rack::Server.start(
app: HttpProxy.new, Port: 8000, Host: "0.0.0.0"
)