tests: content-md5 tests shut down connection
[rainbows.git] / t / bin / content-md5-put
blob01d96dd51d68f0ad2aca5bf3d40176545a49c044
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 "Connection: #{ENV["Connection"] || 'close'}\r\n" \
19 "Transfer-Encoding: chunked\r\n" \
20 "Trailer: Content-MD5\r\n" \
21 "\r\n"
23 end
25 digest = Digest::MD5.new
26 if buf = $stdin.readpartial(bs)
27 begin
28 digest.update(buf)
29 $stdout.write("%x\r\n" % [ buf.size ])
30 $stdout.write(buf)
31 $stdout.write("\r\n")
32 end while $stdin.read(bs, buf)
33 end
35 digest = [ digest.digest ].pack('m').strip
36 $stdout.write("0\r\n")
37 $stdout.write("Content-MD5: #{digest}\r\n\r\n")