Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################################################################
- # Fetch a URI, using HTTP GET unless you supply <tt>post</tt>.
- def fetch (uri, post={}, headers={})
- @session.refresh
- uri = URI.parse(@session.absolute_uri(uri))
- post.empty? and cached = @cache.check(uri)
- @log.info((cached ? 'C ' : 'N ') + uri.to_s) if @log
- return cached if cached
- sleep(@delay) if @delay != 0
- path = uri.path.dup
- path << "/" if path.empty?
- path << "?" + uri.query if uri.query
- req = post.empty? ? Net::HTTP::Get.new(path) : Net::HTTP::Post.new(path)
- req.set_form_data(post) unless post.empty?
- req['Cookie'] = @session.cookies.to_header
- headers.each {|k,v| req[k] = v}
- res = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req)}
- if @verbose >= 2
- puts "-----------------------------------------------"
- puts res.class
- res.each_header {|k,v| puts "#{k}: #{v}"}
- end
- # FIXME, what to do about more than one cookie
- @session.cookies.from_header(res['set-cookie']) if res.key?('set-cookie')
- case res
- when Net::HTTPRedirection : res = fetch(res['location'], {}, headers)
- else
- end
- post.empty? and @cache.update(uri, res.body)
- res
- end
Add Comment
Please, Sign In to add comment