about summary refs log tree commit homepage
path: root/lib/yahns/proxy_pass.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/yahns/proxy_pass.rb')
-rw-r--r--lib/yahns/proxy_pass.rb276
1 files changed, 150 insertions, 126 deletions
diff --git a/lib/yahns/proxy_pass.rb b/lib/yahns/proxy_pass.rb
index 9c12e99..09fb884 100644
--- a/lib/yahns/proxy_pass.rb
+++ b/lib/yahns/proxy_pass.rb
@@ -5,110 +5,168 @@ require 'socket'
 require 'kgio'
 require 'kcar' # gem install kcar
 require 'rack/request'
-require 'thread'
 require 'timeout'
 
-# Totally synchronous and Rack 1.1-compatible, this will probably be rewritten.
-# to take advantage of rack.hijack and use the non-blocking I/O facilities
-# in yahns.  yahns may have to grow a supported API for that...
-# For now, we this blocks a worker thread; fortunately threads are reasonably
-# cheap on GNU/Linux...
+require_relative 'proxy_http_response'
+
 class Yahns::ProxyPass # :nodoc:
-  class ConnPool
-    def initialize
-      @mtx = Mutex.new
-      @objs = []
-    end
+  class ReqRes < Kgio::Socket
+    attr_writer :resbuf
 
-    def get
-      @mtx.synchronize { @objs.pop }
+    def req_start(c, req, input, chunked)
+      @hdr = @resbuf = nil
+      @yahns_client = c
+      @rrstate = input ? [ req, input, chunked ] : req
+      Thread.current[:yahns_queue].queue_add(self, Yahns::Queue::QEV_WR)
     end
 
-    def put(obj)
-      @mtx.synchronize { @objs << obj }
+    # we must reinitialize the thread-local rbuf if it may get beyond the
+    # current thread
+    def detach_rbuf!
+      Thread.current[:yahns_rbuf] = ''
     end
-  end
 
-  class UpstreamSocket < Kgio::Socket # :nodoc:
-    attr_writer :expiry
+    def yahns_step # yahns event loop entry point
+      case req = @rrstate
+      when Kcar::Parser # reading response...
+        buf = Thread.current[:yahns_rbuf]
+        c = @yahns_client
 
-    # called automatically by kgio_read!
-    def kgio_wait_readable(timeout = nil)
-      super(timeout || wait_time)
-    end
+        case resbuf = @resbuf # where are we at the response?
+        when nil # common case, catch the response header in a single read
 
-    def wait_time
-      tout = @expiry ? @expiry - Yahns.now : @timeout
-      raise Timeout::Error, "request timed out", [] if tout < 0
-      tout
-    end
+          case rv = kgio_tryread(0x2000, buf)
+          when String
+            if res = req.headers(@hdr = [], rv)
+              return c.proxy_response_start(res, rv, req, self)
+            else # ugh, big headers or tricked response
+              buf = detach_rbuf!
+              @resbuf = rv
+            end
+            # continue looping in middle "case @resbuf" loop
+          when :wait_readable
+            return rv # spurious wakeup
+          when nil then return c.proxy_err_response(502, self, nil, nil)
+          end # NOT looping here
 
-    def readpartial(bytes, buf = Thread.current[:yahns_rbuf] ||= "")
-      case rv = kgio_read!(bytes, buf)
-      when String
-        @expiry += @timeout # bump expiry when we succeed
-      end
-      rv
-    end
+        when String # continue reading trickled response headers from upstream
 
-    def req_write(buf, timeout)
-      @timeout = timeout
-      @expiry = Yahns.now + timeout
-      case rv = kgio_trywrite(buf)
-      when :wait_writable
-        kgio_wait_writable(wait_time)
-      when nil
-        return
-      when String
-        buf = rv
-      end while true
+          case rv = kgio_tryread(0x2000, buf)
+          when String then res = req.headers(@hdr, resbuf << rv) and break
+          when :wait_readable then return rv
+          when nil then return c.proxy_err_response(502, self, nil, nil)
+          end while true
+
+          return c.proxy_response_start(res, resbuf, req, self)
+
+        when Yahns::WbufCommon # streaming/buffering the response body
+
+          return c.proxy_response_finish(req, resbuf, self)
+
+        end while true # case @resbuf
+
+      when Array # [ (str|vec), rack.input, chunked? ]
+        send_req_body(req) # returns nil or :wait_writable
+      when String # buffered request header
+        send_req_buf(req)
+      end
+    rescue => e
+      c.proxy_err_response(502, self, e, nil)
     end
-  end # class UpstreamSocket
 
-  class UpstreamResponse < Kcar::Response # :nodoc:
     # Called by the Rack server at the end of a successful response
     def close
-      reusable = @parser.keepalive? && @parser.body_eof?
+      @hdr = @yahns_client = @rrstate = nil
       super
-      @pool.put(self) if reusable
-      nil
     end
 
-    # req is just a string buffer of HTTP headers
-    def req_write(req, timeout)
-      @sock.req_write(req, timeout)
-    end
+    # returns :wait_readable if complete, :wait_writable if not
+    def send_req_body(req)
+      buf, input, chunked = req
 
-    # returns true if the socket is still alive, nil if dead
-    def sock_alive?
-      @reused = (:wait_readable == (@sock.kgio_tryread(1) rescue nil)) ?
-                true : @sock.close
-    end
+      # get the first buffered chunk or vector
+      case rv = String === buf ? kgio_trywrite(buf) : kgio_trywritev(buf)
+      when String, Array
+        buf = rv # retry inner loop
+      when :wait_writable
+        req[0] = buf
+        return :wait_writable
+      when nil
+        break # onto writing body
+      end while true
 
-    # returns true if the socket was reused and thus retryable
-    def fail_retryable?
-      @sock.close
-      @reused
+      buf = Thread.current[:yahns_rbuf]
+
+      # Note: input (env['rack.input']) is fully-buffered by default so
+      # we should not be waiting on a slow network resource when reading
+      # input.  However, some weird configs may disable this on LANs
+
+      if chunked
+        while input.read(0x2000, buf)
+          vec = [ "#{buf.size.to_s(16)}\r\n", buf, "\r\n".freeze ]
+          case rv = kgio_trywritev(vec)
+          when Array
+            vec = rv # partial write, retry in case loop
+          when :wait_writable
+            buf = detach_rbuf!
+            req[0] = vec
+            return :wait_writable
+          when nil
+            break # continue onto reading next chunk
+          end while true
+        end
+        close_req_body(input)
+
+        # note: we do not send any trailer, they are folded into the header
+        # because this relies on full request buffering
+        send_req_buf("0\r\n\r\n".freeze)
+      else # identity request, easy:
+        while input.read(0x2000, buf)
+          case rv = kgio_trywrite(buf)
+          when String
+            buf = rv # partial write, retry in case loop
+          when :wait_writable
+            buf = detach_rbuf!
+            req[0] = buf
+            return :wait_writable
+          when nil
+            break # continue onto reading next block
+          end while true
+        end
+
+        close_req_body(input)
+        prepare_wait_readable
+      end
     end
 
-    def initialize(sock, pool)
-      super(sock)
-      @reused = false
-      @pool = pool
+    def prepare_wait_readable
+      @rrstate = Kcar::Parser.new
+      :wait_readable # all done sending the request, wait for response
     end
-  end # class UpstreamResponse
 
-  # take a responder from the pool, we'll add the object back to the
-  # pool in UpstreamResponse#close
-  def responder_get
-    while obj = @pool.get
-      return obj if obj.sock_alive?
+    def close_req_body(input)
+      case input
+      when Yahns::TeeInput, IO, StringIO
+        input.close
+      end
     end
 
-    UpstreamResponse.new(UpstreamSocket.start(@sockaddr), @pool)
-  end
+    # n.b. buf must be a detached string not shared with
+    # Thread.current[:yahns_rbuf] of any thread
+    def send_req_buf(buf)
+      case rv = kgio_trywrite(buf)
+      when String
+        buf = rv # retry inner loop
+      when :wait_writable
+        @rrstate = buf
+        return :wait_writable
+      when nil
+        return prepare_wait_readable
+      end while true
+    end
+  end # class ReqRes
 
-  def initialize(dest, timeout = 5)
+  def initialize(dest)
     case dest
     when %r{\Aunix:([^:]+)(?::(/.*))?\z}
       path = $2
@@ -121,8 +179,6 @@ class Yahns::ProxyPass # :nodoc:
       raise ArgumentError, "destination must be an HTTP URL or unix: path"
     end
     init_path_vars(path)
-    @pool = ConnPool.new
-    @timeout = timeout
   end
 
   def init_path_vars(path)
@@ -139,10 +195,15 @@ class Yahns::ProxyPass # :nodoc:
   end
 
   def call(env)
-    request_method = env['REQUEST_METHOD']
+    # 3-way handshake for TCP backends while we generate the request header
+    rr = ReqRes.start(@sockaddr)
+    c = env['rack.hijack'].call
+
     req = Rack::Request.new(env)
-    path = @path.gsub(/\$(\w+)/) { req.__send__($1) }
-    req = "#{request_method} #{path} HTTP/1.1\r\n" \
+    req = @path.gsub(/\$(\w+)/) { req.__send__($1) }
+
+    # start the connection asynchronously and early so TCP can do a
+    req = "#{env['REQUEST_METHOD']} #{req} HTTP/1.1\r\n" \
           "X-Forwarded-For: #{env["REMOTE_ADDR"]}\r\n"
 
     # pass most HTTP_* headers through as-is
@@ -150,61 +211,24 @@ class Yahns::ProxyPass # :nodoc:
     env.each do |key, val|
       %r{\AHTTP_(\w+)\z} =~ key or next
       key = $1
-      next if %r{\A(?:VERSION|CONNECTION|KEEP_ALIVE|X_FORWARDED_FOR)} =~ key
+      # trailers are folded into the header, so do not send the Trailer:
+      # header in the request
+      next if /\A(?:VERSION|CONNECTION|KEEP_ALIVE|X_FORWARDED_FOR|TRAILER)/ =~
+         key
       chunked = true if %r{\ATRANSFER_ENCODING} =~ key && val =~ /\bchunked\b/i
-      key.tr!("_", "-")
+      key.tr!('_'.freeze, '-'.freeze)
       req << "#{key}: #{val}\r\n"
     end
 
     # special cases which Rack does not prefix:
     ctype = env["CONTENT_TYPE"] and req << "Content-Type: #{ctype}\r\n"
     clen = env["CONTENT_LENGTH"] and req << "Content-Length: #{clen}\r\n"
-    req << "\r\n"
-
-    # get an open socket and send the headers
-    ures = responder_get
-    ures.req_write(req, @timeout)
+    input = chunked || (clen && clen.to_i > 0) ? env['rack.input'] : nil
 
-    # send the request body if there was one
-    send_body(env["rack.input"], ures, chunked) if chunked || clen
-
-    # wait for the response here
-    _, header, body = res = ures.rack
-
-    # don't let the upstream Connection and Keep-Alive headers leak through
-    header.delete_if do |k,_|
-      k =~ /\A(?:Connection|Keep-Alive)\z/i
-    end
-
-    case request_method
-    when "HEAD"
-      # kcar doesn't know if it's a HEAD or GET response, and HEAD
-      # responses have Content-Length in it which fools kcar...
-      body.parser.body_bytes_left = 0
-      res[1] = header.dup
-      body.close # clobbers original header
-      res[2] = body = []
-    end
-    res
+    # finally, prepare to emit the headers
+    rr.req_start(c, req << "\r\n".freeze, input, chunked)
   rescue => e
-    retry if ures && ures.fail_retryable? && request_method != "POST"
     Yahns::Log.exception(env['rack.logger'], 'proxy_pass', e)
     [ 502, [ %w(Content-Length 0), %w(Content-Type text/plain) ], [] ]
   end
-
-  def send_body(input, ures, chunked)
-    buf = Thread.current[:yahns_rbuf] ||= ""
-
-    if chunked # unlikely
-      while input.read(16384, buf)
-        buf.replace("#{buf.size.to_s(16)}\r\n#{buf}\r\n")
-        ures.req_write(buf, @timeout)
-      end
-      ures.req_write("0\r\n\r\n", @timeout)
-    else # common if we hit uploads
-      while input.read(16384, buf)
-        ures.req_write(buf, @timeout)
-      end
-    end
-  end
 end