yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 1/3] extras/proxy_pass: implicit $fullpath expansion for upstreams
Date: Sat, 14 Mar 2015 03:17:56 +0000	[thread overview]
Message-ID: <1426303078-4525-2-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1426303078-4525-1-git-send-email-e@80x24.org>

This is slightly more nginx-style behavior and allows simpler
configuration.
---
 extras/proxy_pass.rb | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/extras/proxy_pass.rb b/extras/proxy_pass.rb
index 15cbae5..d435ebe 100644
--- a/extras/proxy_pass.rb
+++ b/extras/proxy_pass.rb
@@ -112,25 +112,31 @@ class ProxyPass # :nodoc:
 
   def initialize(dest, timeout = 5)
     case dest
-    when %r{\Ahttp://([^/]+)(/.*)\z}
+    when %r{\Ahttp://([^/]+)(/.*)?\z}
       path = $2
       host, port = $1.split(':')
       @sockaddr = Socket.sockaddr_in(port || 80, host)
-
-      # methods from Rack::Request we want:
-      allow = %w(fullpath host_with_port host port url path)
-      @path = path
-      want = path.scan(/\$(\w+)/).flatten! || []
-      diff = want - allow
-      diff.empty? or
-               raise ArgumentError, "vars not allowed: #{diff.uniq.join(' ')}"
     else
       raise ArgumentError, "destination must be an HTTP URL"
     end
+    init_path_vars(path)
     @pool = ConnPool.new
     @timeout = timeout
   end
 
+  def init_path_vars(path)
+    path ||= '$(fullpath)'
+    # methods from Rack::Request we want:
+    allow = %w(fullpath host_with_port host port url path)
+    want = path.scan(/\$(\w+)/).flatten! || []
+    diff = want - allow
+    diff.empty? or
+             raise ArgumentError, "vars not allowed: #{diff.uniq.join(' ')}"
+
+    # kill leading slash just in case...
+    @path = path.gsub(%r{\A/(\$(?:fullpath|path))}, '\1')
+  end
+
   def call(env)
     request_method = env['REQUEST_METHOD']
     req = Rack::Request.new(env)
-- 
EW


  reply	other threads:[~2015-03-14  3:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-14  3:17 [PATCH 0/3] towards becoming a fully-buffering reverse proxy Eric Wong
2015-03-14  3:17 ` Eric Wong [this message]
2015-03-14  3:17 ` [PATCH 2/3] extras/proxy_pass: support Unix domain sockets as backends Eric Wong
2015-03-14  3:17 ` [PATCH 3/3] proxy_pass: officially become a part of yahns Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1426303078-4525-2-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/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).