aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/rack/legacy_session.rb
blob: b5da1a356343a374ea9b5feadd043ae2940e585f (plain)
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
# -*- frozen-string-literal: true -*-
using Plum::BinaryString

module Plum
  module Rack
    class LegacySession
      def initialize(svc, e, sock)
        @svc = svc
        @e = e
        @sock = sock
        @config = svc.config
      end

      def run
        if @config[:fallback_legacy_host]
          @svc.logger.info "legacy HTTP: fallbacking to: #{@config[:fallback_legacy_host]}:#{@config[:fallback_legacy_port]}"
          upstream = TCPSocket.open(@config[:fallback_legacy_host], @config[:fallback_legacy_port])
          upstream.write(@e.buf) if @e.buf
          loop do
            ret = IO.select([@sock, upstream])
            ret[0].each { |s|
              a = s.readpartial(65536)
              if s == upstream
                @sock.write(a)
              else
                upstream.write(a)
              end
            }
          end
        end
      ensure
        upstream.close if upstream
      end
    end
  end
end