unicorn 4.6.0pre1 - hijacking support
[unicorn.git] / t / bin / content-md5-put
blob01da0bb7e05991725aa1be5cbe7cc18b1f8fcbd8
1 #!/usr/bin/env ruby
2 # -*- encoding: binary -*-
3 # simple chunked HTTP PUT request generator (and just that),
4 # it reads stdin and writes to stdout so socat can write to a
5 # UNIX or TCP socket (or to another filter or file) along with
6 # a Content-MD5 trailer.
7 require 'digest/md5'
8 $stdout.sync = $stderr.sync = true
9 $stdout.binmode
10 $stdin.binmode
12 bs = ENV['bs'] ? ENV['bs'].to_i : 4096
14 if ARGV.grep("--no-headers").empty?
15 $stdout.write(
16 "PUT / HTTP/1.1\r\n" \
17 "Host: example.com\r\n" \
18 "Transfer-Encoding: chunked\r\n" \
19 "Trailer: Content-MD5\r\n" \
20 "\r\n"
22 end
24 digest = Digest::MD5.new
25 if buf = $stdin.readpartial(bs)
26 begin
27 digest.update(buf)
28 $stdout.write("%x\r\n" % [ buf.size ])
29 $stdout.write(buf)
30 $stdout.write("\r\n")
31 end while $stdin.read(bs, buf)
32 end
34 digest = [ digest.digest ].pack('m').strip
35 $stdout.write("0\r\n")
36 $stdout.write("Content-MD5: #{digest}\r\n\r\n")