Module: RbbtMutiplartPayload

Defined in:
lib/rbbt/util/misc/multipart_payload.rb

Constant Summary collapse

BOUNDARY =
"Rbbt_Param_Stream"
EOL =
"\r\n"

Class Method Summary collapse

Class Method Details

.add_input(name, content, filename = nil) ⇒ Object



27
28
29
30
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 27

def self.add_input(name, content, filename = nil)
  header = input_header(name, filename)
  "--" + BOUNDARY + EOL + header + EOL + content + EOL
end

.add_stream(io, name, content, filename = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 32

def self.add_stream(io, name, content, filename = nil)
  header = input_header(name, filename)
  io.write "--" + BOUNDARY + EOL + header + EOL

  begin
    while c = content.readpartial(Misc::BLOCK_SIZE)
      io.write c
    end
  rescue EOFError
    io.write "\r\n"
  end
end

.close_stream(io) ⇒ Object



45
46
47
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 45

def self.close_stream(io)
  io.write "--" + BOUNDARY + "--" + EOL + EOL
end

.input_header(name, filename = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 12

def self.input_header(name, filename = nil)

  if filename
    head_text = 'Content-Disposition: form-data; name="' + name + '"; filename="' + filename + '"'
  else
    head_text = 'Content-Disposition: form-data; name="' + name + '"'
  end

  content_transfer_text = "Content-Transfer-Encoding: binary"

  content_type_text = 'Content-Type: text/plain'

  head_text + EOL + content_transfer_text + EOL + content_type_text + EOL
end

.issue(url, inputs = nil, stream_input = nil, stream_io = nil, stream_filename = nil, report_type = false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 77

def self.issue(url, inputs = nil, stream_input = nil, stream_io = nil, stream_filename = nil, report_type = false)

  uri = URI(url)
  IndiferentHash.setup(inputs)

  if stream_input
    stream_io ||= TSV.get_stream inputs[stream_input]
    stream_filename ||= case inputs[stream_input]
                        when String
                          inputs[stream_input]
                        when File
                          inputs[stream_input].path
                        else
                          'file-rand-' + rand(10000000).to_s
                        end
  end

  post_data_stream = RbbtMutiplartPayload.post_data_stream inputs, stream_input, stream_io, stream_filename

  jobname = inputs["jobname"] 

  req = Net::HTTP::Post.new(uri.path)
  if stream_input
    req.content_type = "multipart/form-data; boundary=" + RbbtMutiplartPayload::BOUNDARY + '; stream=' + stream_input.to_s
    req.body_stream = post_data_stream
  else
    req.content_type = "multipart/form-data; boundary=" + RbbtMutiplartPayload::BOUNDARY
    req.body = post_data_stream.read
  end

  req.add_field "Transfer-Encoding", 'chunked'
  req.add_field "RBBT_ID", (jobname || "No name")
  timeout_minutes = 60
  timeout = 60 * timeout_minutes
  Misc.open_pipe do |sin|
    Net::HTTP.start(uri.hostname, uri.port, :read_timeout => timeout) do |http|
      http.request(req) do |res|
        if Net::HTTPSuccess === res
          url_path = res["RBBT-STREAMING-JOB-URL"]
          if Net::HTTPRedirection === res
            Log.medium "Response recieved REDIRECT: #{ url_path }"
            sin.puts "LOCATION" if report_type
            sin.write res["location"]
          elsif stream_input and url_path
            Log.medium "Response recieved STREAM: #{ url_path }"
            url = URI::HTTP.build(:host => uri.hostname, :port => uri.port, :path => url_path)
            sin.puts "STREAM: #{url.to_s}" if report_type
            Log.medium "Read body: #{ url_path }"
            res.read_body(sin)
            Log.medium "Read body DONE: #{ url_path }"
          else
            Log.medium "Response recieved BULK: #{ url_path }"
            sin.puts "BULK" if report_type
            sin.write res.body
          end
        else
          raise "Error: #{res.code}"
        end
      end
    end
  end
end

.mutexObject



8
9
10
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 8

def self.mutex
  @@mutex ||= Mutex.new
end

.post_data_stream(inputs = nil, stream_input = nil, stream_io = nil, stream_filename = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 49

def self.post_data_stream(inputs = nil, stream_input = nil, stream_io = nil, stream_filename = nil)
  Misc.open_pipe do |sin|
    inputs.each do |input,content|
      input = input.to_s
      next if stream_input and input == stream_input.to_s
      content_str = case content
                    when String
                      if Misc.is_filename?(content) and File.exist?(content)
                        File.read(content)
                      else
                        content
                      end
                    when File, IO
                      content.read
                    when nil
                      "nil"
                    else
                      content.to_s
                    end
      str = RbbtMutiplartPayload.add_input(input, content_str)
      sin.write str
    end

    RbbtMutiplartPayload.add_stream(sin, stream_input.to_s, stream_io, stream_filename) if stream_input
    RbbtMutiplartPayload.close_stream(sin)
  end
end