yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
* [PUSHED] extras/proxy_pass updates
@ 2015-03-03  7:59 Eric Wong
  2015-03-03  7:59 ` [PATCH 1/3] extras/proxy_pass: do not name unused variable Eric Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Wong @ 2015-03-03  7:59 UTC (permalink / raw)
  To: yahns-public

Minor updates to the proxy_pass extra which I've been using to
proxy an Apache mpm_prefork instance behind yahns (which hosts
the HTTP mailing list archives via public-inbox).  I'll probably
turn this into some sort of reusable API based on rack.hijack
soon, so it's more efficient and allow M:N thread:connection
handling instead of the current 1:1 model.

Eric Wong (3):
      extras/proxy_pass: do not name unused variable
      extras/proxy_pass: log exceptions leading to 502
      extras/proxy_pass: flesh out upload support + tests

 extras/proxy_pass.rb           |  15 +++----
 test/server_helper.rb          |   6 +--
 test/test_extras_proxy_pass.rb | 100 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 11 deletions(-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] extras/proxy_pass: do not name unused variable
  2015-03-03  7:59 [PUSHED] extras/proxy_pass updates Eric Wong
@ 2015-03-03  7:59 ` Eric Wong
  2015-03-03  7:59 ` [PATCH 2/3] extras/proxy_pass: log exceptions leading to 502 Eric Wong
  2015-03-03  7:59 ` [PATCH 3/3] extras/proxy_pass: flesh out upload support + tests Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-03-03  7:59 UTC (permalink / raw)
  To: yahns-public

"ruby -w" warns on it.
---
 extras/proxy_pass.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/extras/proxy_pass.rb b/extras/proxy_pass.rb
index b1697db..8a32cac 100644
--- a/extras/proxy_pass.rb
+++ b/extras/proxy_pass.rb
@@ -170,7 +170,7 @@ class ProxyPass # :nodoc:
     send_body(env["rack.input"], ures, chunked) if chunked || clen
 
     # wait for the response here
-    status, header, body = res = ures.rack
+    _, header, body = res = ures.rack
 
     # don't let the upstream Connection and Keep-Alive headers leak through
     header.delete_if do |k,_|
-- 
EW


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] extras/proxy_pass: log exceptions leading to 502
  2015-03-03  7:59 [PUSHED] extras/proxy_pass updates Eric Wong
  2015-03-03  7:59 ` [PATCH 1/3] extras/proxy_pass: do not name unused variable Eric Wong
@ 2015-03-03  7:59 ` Eric Wong
  2015-03-03  7:59 ` [PATCH 3/3] extras/proxy_pass: flesh out upload support + tests Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-03-03  7:59 UTC (permalink / raw)
  To: yahns-public

It may be useful for us to track down potential errors in
our code or log when an upstream misbehaves.
---
 extras/proxy_pass.rb           |  4 +++
 test/server_helper.rb          |  6 ++--
 test/test_extras_proxy_pass.rb | 78 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 test/test_extras_proxy_pass.rb

diff --git a/extras/proxy_pass.rb b/extras/proxy_pass.rb
index 8a32cac..de6a2b1 100644
--- a/extras/proxy_pass.rb
+++ b/extras/proxy_pass.rb
@@ -189,6 +189,10 @@ class ProxyPass # :nodoc:
     res
   rescue => e
     retry if ures && ures.fail_retryable? && request_method != "POST"
+    if defined?(Yahns::Log)
+      logger = env['rack.logger'] and
+        Yahns::Log.exception(logger, 'proxy_pass', e)
+    end
     ERROR_502
   end
 
diff --git a/test/server_helper.rb b/test/server_helper.rb
index e2641a9..1ce2f29 100644
--- a/test/server_helper.rb
+++ b/test/server_helper.rb
@@ -72,10 +72,10 @@ module ServerHelper
     @ru = nil
   end
 
-  def mkserver(cfg)
+  def mkserver(cfg, srv = @srv)
     fork do
-      ENV["YAHNS_FD"] = @srv.fileno.to_s
-      @srv.autoclose = false
+      ENV["YAHNS_FD"] = srv.fileno.to_s
+      srv.autoclose = false
       yield if block_given?
       Yahns::Server.new(cfg).start.join
     end
diff --git a/test/test_extras_proxy_pass.rb b/test/test_extras_proxy_pass.rb
new file mode 100644
index 0000000..513e574
--- /dev/null
+++ b/test/test_extras_proxy_pass.rb
@@ -0,0 +1,78 @@
+# Copyright (C) 2015 all contributors <yahns-public@yhbt.net>
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative 'server_helper'
+
+class TestExtrasProxyPass < Testcase
+  ENV["N"].to_i > 1 and parallelize_me!
+  include ServerHelper
+
+  class ProxiedApp
+    def call(env)
+      h = [ %w(Content-Length 3), %w(Content-Type text/plain) ]
+      case env['REQUEST_METHOD']
+      when 'GET'
+        [ 200, h, [ "hi\n"] ]
+      when 'HEAD'
+        [ 200, h, [] ]
+      when 'PUT'
+        buf = env['rack.input'].read
+        [ 201, {
+          'Content-Length' => buf.bytesize.to_s,
+          'Content-Type' => 'text/plain',
+          }, [ buf ] ]
+      end
+    end
+  end
+
+  def setup
+    @srv2 = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
+    server_helper_setup
+  end
+
+  def teardown
+    @srv2.close if defined?(@srv2) && !@srv2.closed?
+    server_helper_teardown
+  end
+
+  def test_proxy_pass
+    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+    host2, port2 = @srv2.addr[3], @srv2.addr[1]
+    pid = mkserver(cfg) do
+      $LOAD_PATH.unshift "#{Dir.pwd}/extras"
+      require 'proxy_pass'
+      @srv2.close
+      cfg.instance_eval do
+        app(:rack, ProxyPass.new("http://#{host2}:#{port2}/")) do
+          listen "#{host}:#{port}"
+        end
+        stderr_path err.path
+      end
+    end
+
+    pid2 = mkserver(cfg, @srv2) do
+      @srv.close
+      cfg.instance_eval do
+        app(:rack, ProxiedApp.new) do
+          listen "#{host2}:#{port2}"
+        end
+        stderr_path err.path
+      end
+    end
+
+    Net::HTTP.start(host, port) do |http|
+      res = http.request(Net::HTTP::Get.new('/'))
+      assert_equal 200, res.code.to_i
+      n = res.body.bytesize
+      assert_operator n, :>, 1
+      res = http.request(Net::HTTP::Head.new('/'))
+      assert_equal 200, res.code.to_i
+      assert_equal n, res['Content-Length'].to_i
+      assert_nil res.body
+
+      res = http.put(Net::HTTP::Put.new('/'))
+    end
+  ensure
+    quit_wait pid
+    quit_wait pid2
+  end
+end
-- 
EW


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] extras/proxy_pass: flesh out upload support + tests
  2015-03-03  7:59 [PUSHED] extras/proxy_pass updates Eric Wong
  2015-03-03  7:59 ` [PATCH 1/3] extras/proxy_pass: do not name unused variable Eric Wong
  2015-03-03  7:59 ` [PATCH 2/3] extras/proxy_pass: log exceptions leading to 502 Eric Wong
@ 2015-03-03  7:59 ` Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-03-03  7:59 UTC (permalink / raw)
  To: yahns-public

This module will probably become an official part of yahns
soon, so finally add tests for this module.
---
 extras/proxy_pass.rb           |  9 ++-------
 test/test_extras_proxy_pass.rb | 24 +++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/extras/proxy_pass.rb b/extras/proxy_pass.rb
index de6a2b1..0bec4ee 100644
--- a/extras/proxy_pass.rb
+++ b/extras/proxy_pass.rb
@@ -135,12 +135,7 @@ class ProxyPass # :nodoc:
   end
 
   def call(env)
-    case request_method = env["REQUEST_METHOD"]
-    when "GET", "HEAD" # OK
-    else
-      return [ 405, [%w(Content-Length 0), %w(Content-Length 0)], [] ]
-    end
-
+    request_method = env['REQUEST_METHOD']
     req = Rack::Request.new(env)
     path = @path.gsub(/\$(\w+)/) { req.__send__($1.to_sym) }
     req = "#{request_method} #{path} HTTP/1.1\r\n" \
@@ -204,7 +199,7 @@ class ProxyPass # :nodoc:
         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")
+      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)
diff --git a/test/test_extras_proxy_pass.rb b/test/test_extras_proxy_pass.rb
index 513e574..8842683 100644
--- a/test/test_extras_proxy_pass.rb
+++ b/test/test_extras_proxy_pass.rb
@@ -59,6 +59,8 @@ class TestExtrasProxyPass < Testcase
       end
     end
 
+    gplv3 = File.open('COPYING')
+
     Net::HTTP.start(host, port) do |http|
       res = http.request(Net::HTTP::Get.new('/'))
       assert_equal 200, res.code.to_i
@@ -69,9 +71,29 @@ class TestExtrasProxyPass < Testcase
       assert_equal n, res['Content-Length'].to_i
       assert_nil res.body
 
-      res = http.put(Net::HTTP::Put.new('/'))
+      # chunked encoding
+      req = Net::HTTP::Put.new('/')
+      req.body_stream = gplv3
+      req.content_type = 'application/octet-stream'
+      req['Transfer-Encoding'] = 'chunked'
+      res = http.request(req)
+      gplv3.rewind
+      assert_equal gplv3.read, res.body
+      assert_equal 201, res.code.to_i
+
+      # normal content-length
+      gplv3.rewind
+      req = Net::HTTP::Put.new('/')
+      req.body_stream = gplv3
+      req.content_type = 'application/octet-stream'
+      req.content_length = gplv3.size
+      res = http.request(req)
+      gplv3.rewind
+      assert_equal gplv3.read, res.body
+      assert_equal 201, res.code.to_i
     end
   ensure
+    gplv3.close if gplv3
     quit_wait pid
     quit_wait pid2
   end
-- 
EW


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-03  7:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-03  7:59 [PUSHED] extras/proxy_pass updates Eric Wong
2015-03-03  7:59 ` [PATCH 1/3] extras/proxy_pass: do not name unused variable Eric Wong
2015-03-03  7:59 ` [PATCH 2/3] extras/proxy_pass: log exceptions leading to 502 Eric Wong
2015-03-03  7:59 ` [PATCH 3/3] extras/proxy_pass: flesh out upload support + tests Eric Wong

Code repositories for project(s) associated with this inbox:

	../../../yahns.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).