Class: Medusa::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/medusa/http.rb

Constant Summary collapse

REDIRECT_LIMIT =

Maximum number of redirects to follow on each get_response

5
RETRY_LIMIT =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HTTP

Returns a new instance of HTTP.



14
15
16
17
# File 'lib/medusa/http.rb', line 14

def initialize(opts = {})
  @opts = opts
  @cookie_store = CookieStore.new(@opts[:cookies])
end

Instance Attribute Details

CookieStore for this HTTP client



12
13
14
# File 'lib/medusa/http.rb', line 12

def cookie_store
  @cookie_store
end

Instance Method Details

#accept_cookies?Boolean

Does this HTTP client accept cookies from the server?

Returns:

  • (Boolean)


69
70
71
# File 'lib/medusa/http.rb', line 69

def accept_cookies?
  @opts[:accept_cookies]
end

#fetch_page(url, referer = nil, depth = nil) ⇒ Object

Fetch a single Page from the response of an HTTP request to url. Just gets the final destination page.



23
24
25
# File 'lib/medusa/http.rb', line 23

def fetch_page(url, referer = nil, depth = nil)
  fetch_pages(url, referer, depth).last
end

#fetch_pages(url, referer = nil, depth = nil) ⇒ Object

Create new Pages from the response of an HTTP request to url, including redirects



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/medusa/http.rb', line 31

def fetch_pages(url, referer = nil, depth = nil)
  pages = []
  begin
    url = URI(url) unless url.is_a?(URI)
    get(url, referer) do |response, headers, code, location, redirect_to, response_time|
      pages << Page.new(location, :body => response,
                                  :headers => headers,
                                  :code => code,
                                  :referer => referer,
                                  :depth => depth,
                                  :redirect_to => redirect_to,
                                  :response_time => response_time)
    end

    return pages
  rescue StandardError => e
    return pages << Page.new(url, error: e)
  end
end

#http_basic_authenticationObject

The http authentication options as in www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/OpenURI/OpenRead.html userinfo is deprecated [RFC3986]



77
78
79
# File 'lib/medusa/http.rb', line 77

def http_basic_authentication
  @opts[:http_basic_authentication]
end

#proxyObject



91
92
93
# File 'lib/medusa/http.rb', line 91

def proxy
  @opts[:proxy]
end

#proxy_hostObject

The proxy address string



98
99
100
# File 'lib/medusa/http.rb', line 98

def proxy_host
  @opts[:proxy_host]
end

#proxy_http_basic_authenticationObject



84
85
86
# File 'lib/medusa/http.rb', line 84

def proxy_http_basic_authentication
  @opts[:proxy_http_basic_authentication]
end

#proxy_portObject

The proxy port



105
106
107
# File 'lib/medusa/http.rb', line 105

def proxy_port
  @opts[:proxy_port]
end

#read_timeoutObject

HTTP read timeout in seconds



112
113
114
# File 'lib/medusa/http.rb', line 112

def read_timeout
  @opts[:read_timeout]
end

#redirect_limitObject

The maximum number of redirects to follow



54
55
56
# File 'lib/medusa/http.rb', line 54

def redirect_limit
  @opts[:redirect_limit] || REDIRECT_LIMIT
end

#user_agentObject

The user-agent string which will be sent with each request, or nil if no such option is set



62
63
64
# File 'lib/medusa/http.rb', line 62

def user_agent
  @opts[:user_agent]
end