Method: Curl::Multi.http

Defined in:
lib/curl/multi.rb

.http(urls_with_config, multi_options = {}, &blk) ⇒ Object

call-seq:

Curl::Multi.http( [

{ :url => 'url1', :method => :post,
  :post_fields => {'field1' => 'value1', 'field2' => 'value2'} },
{ :url => 'url2', :method => :get,
  :follow_location => true, :max_redirects => 3 },
{ :url => 'url3', :method => :put, :put_data => File.open('file.txt','rb') },
{ :url => 'url4', :method => :head }

], => Curl::CURLPIPE_HTTP1)

Blocking call to issue multiple HTTP requests with varying verb’s.

urls_with_config: is a hash of url’s pointing to the easy handle options as well as the special option :method, that can by one of [:get, :post, :put, :delete, :head], when no verb is provided e.g. :method => nil -> GET is used multi_options: options for the multi handle blk: a callback, that yeilds when a handle is completed



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/curl/multi.rb', line 88

def http(urls_with_config, multi_options={}, &blk)
  m = Curl::Multi.new

  # maintain a sane number of easy handles
  multi_options[:max_connects] = max_connects = multi_options.key?(:max_connects) ? multi_options[:max_connects] : 10

  free_handles = [] # keep a list of free easy handles

  # configure the multi handle
  multi_options.each { |k,v| m.send("#{k}=", v) }
  callbacks = [:on_progress,:on_debug,:on_failure,:on_success,:on_redirect,:on_missing,:on_body,:on_header]

  add_free_handle = proc do|conf, easy|
    c       = conf.dup # avoid being destructive to input
    url     = c.delete(:url)
    method  = c.delete(:method)
    headers = c.delete(:headers)

    easy    = Curl::Easy.new if easy.nil?

    easy.url = url

    # assign callbacks
    callbacks.each do |cb|
      cbproc = c.delete(cb)
      easy.send(cb,&cbproc) if cbproc
    end

    case method
    when :post
      fields = c.delete(:post_fields)
      # set the post post using the url fields
      easy.post_body = fields.map{|f,k| "#{easy.escape(f)}=#{easy.escape(k)}"}.join('&')
    when :put
      easy.put_data = c.delete(:put_data)
    when :head
      easy.head = true
    when :delete
      easy.delete = true
    when :get
    else
      # XXX: nil is treated like a GET
    end

    # headers is a special key
    headers.each {|k,v| easy.headers[k] = v } if headers

    #
    # use the remaining options as specific configuration to the easy handle
    # bad options should raise an undefined method error
    #
    c.each { |k,v| easy.send("#{k}=",v) }

    easy.on_complete {|curl|
      free_handles << curl
      blk.call(curl,curl.response_code,method) if blk
    }
    m.add(easy)
  end

  max_connects.times do
    conf = urls_with_config.pop
    add_free_handle.call(conf, nil) if conf
    break if urls_with_config.empty?
  end

  consume_free_handles = proc do
    # as we idle consume free handles
    if urls_with_config.size > 0 && free_handles.size > 0
      easy = free_handles.pop
      conf = urls_with_config.pop
      add_free_handle.call(conf, easy) if conf
    end
  end

  begin
    if urls_with_config.empty?
      m.perform
    else
      until urls_with_config.empty?
        m.perform do
          consume_free_handles.call
        end
        consume_free_handles.call
      end
      free_handles = nil
    end
  ensure
    m.close
  end

end