about summary refs log tree commit homepage
path: root/examples
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-06-05 22:16:47 -0700
committerEric Wong <normalperson@yhbt.net>2009-06-06 02:56:53 -0700
commita64913eafbee3501a677b1232470838a4ad0fc65 (patch)
tree13d87b48cbe6c153d93b060bab03ca7ba621eeda /examples
parent6945342a1f0a4caaa918f2b0b1efef88824439e0 (diff)
downloadunicorn-a64913eafbee3501a677b1232470838a4ad0fc65.tar.gz
This includes an example of tunneling the git protocol inside a
TE:chunked HTTP request.  The example is unfortunately contrived
in that it relies on the custom examples/cat-chunk-proxy.rb
script in the client.  My initial wish was to have a generic
tool like curl(1) operate like this:

  cat > ~/bin/cat-chunk-proxy.sh <<EOF
  #!/bin/sh
  exec curl -sfNT- http://$1:$2/
  EOF
  chmod +x ~/bin/cat-chunk-proxy.sh
  GIT_PROXY_COMMAND=cat-chunk-proxy.sh git clone git://0:8080/foo

Unfortunately, curl will attempt a blocking read on stdin before
reading the TCP socket; causing the git-clone consumer to
starve.  This does not appear to be a problem with the new
server code for handling chunked requests.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/cat-chunk-proxy.rb60
-rw-r--r--examples/git.ru9
2 files changed, 69 insertions, 0 deletions
diff --git a/examples/cat-chunk-proxy.rb b/examples/cat-chunk-proxy.rb
new file mode 100755
index 0000000..3a5921f
--- /dev/null
+++ b/examples/cat-chunk-proxy.rb
@@ -0,0 +1,60 @@
+#!/home/ew/bin/ruby
+# I wish I could just use curl -sfNT- http://host:port/, but
+# unfortunately curl will attempt to read stdin in blocking mode,
+# preventing it from getting responses from the server until
+# stdin has been written to.
+#
+# Usage: GIT_PROXY_COMMAND=/path/to/here git clone git://host:port/project
+#
+# Where host:port is what the Unicorn server is bound to
+
+require 'socket'
+require 'unicorn'
+require 'unicorn/chunked_reader'
+
+$stdin.sync = $stdout.sync = $stderr.sync = true
+$stdin.binmode
+$stdout.binmode
+
+usage = "#$0 HOST PORT"
+host = ARGV.shift or abort usage
+port = ARGV.shift or abort usage
+s = TCPSocket.new(host, port.to_i)
+s.sync = true
+s.write("PUT / HTTP/1.1\r\n" \
+        "Host: #{host}\r\n" \
+        "Transfer-Encoding: chunked\r\n\r\n")
+buf = s.readpartial(16384)
+while /\r\n\r\n/ !~ buf
+  buf << s.readpartial(16384)
+end
+
+head, body = buf.split(/\r\n\r\n/, 2)
+
+input = fork {
+  $0 = "input #$0"
+  begin
+    loop {
+      $stdin.readpartial(16384, buf)
+      s.write("#{'%x' % buf.size}\r\n#{buf}\r\n")
+    }
+  rescue EOFError,Errno::EPIPE,Errno::EBADF,Errno::EINVAL => e
+  end
+  s.write("0\r\n\r\n")
+}
+
+output = fork {
+  $0 = "output #$0"
+
+  c = Unicorn::ChunkedReader.new
+  c.reopen(s, body)
+  begin
+    loop { $stdout.write(c.readpartial(16384, buf)) }
+  rescue EOFError,Errno::EPIPE,Errno::EBADF,Errno::EINVAL => e
+  end
+}
+
+2.times {
+  pid, status = Process.waitpid2
+  $stderr.write("reaped: #{status.inspect}\n") unless status.success?
+}
diff --git a/examples/git.ru b/examples/git.ru
new file mode 100644
index 0000000..3762d3d
--- /dev/null
+++ b/examples/git.ru
@@ -0,0 +1,9 @@
+#\-E none
+require 'unicorn/app/inetd'
+
+use Rack::Lint
+use Rack::Chunked
+# run Unicorn::App::Inetd.new('tee', '/tmp/tee.out')
+run Unicorn::App::Inetd.new(
+ *%w(git daemon --verbose --inetd --export-all --base-path=/home/ew/unicorn)
+)