Guest User

Untitled

a guest
Feb 21st, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. ################################################################################
  2. # Fetch a URI, using HTTP GET unless you supply <tt>post</tt>.
  3. def fetch (uri, post={}, headers={})
  4. @session.refresh
  5. uri = URI.parse(@session.absolute_uri(uri))
  6.  
  7. post.empty? and cached = @cache.check(uri)
  8. @log.info((cached ? 'C ' : 'N ') + uri.to_s) if @log
  9.  
  10. return cached if cached
  11. sleep(@delay) if @delay != 0
  12.  
  13. path = uri.path.dup
  14. path << "/" if path.empty?
  15. path << "?" + uri.query if uri.query
  16.  
  17. req = post.empty? ? Net::HTTP::Get.new(path) : Net::HTTP::Post.new(path)
  18. req.set_form_data(post) unless post.empty?
  19.  
  20. req['Cookie'] = @session.cookies.to_header
  21. headers.each {|k,v| req[k] = v}
  22.  
  23. res = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req)}
  24.  
  25. if @verbose >= 2
  26. puts "-----------------------------------------------"
  27. puts res.class
  28. res.each_header {|k,v| puts "#{k}: #{v}"}
  29. end
  30.  
  31. # FIXME, what to do about more than one cookie
  32. @session.cookies.from_header(res['set-cookie']) if res.key?('set-cookie')
  33.  
  34. case res
  35. when Net::HTTPRedirection : res = fetch(res['location'], {}, headers)
  36. else
  37. end
  38.  
  39. post.empty? and @cache.update(uri, res.body)
  40. res
  41. end
Add Comment
Please, Sign In to add comment