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



36
37
38
39
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 36

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



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 41

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



54
55
56
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 54

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

.input_header(name, filename = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 21

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



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
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 86

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
    Log.low "Streaming input #{stream_input.to_s}"
    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|
      Log.low "Starting connection for streaming job"
      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



17
18
19
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 17

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbbt/util/misc/multipart_payload.rb', line 58

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