Module: HTTP

Defined in:
lib/lyrics/utils/http.rb

Constant Summary collapse

@@user_agent =

@@user_agent = “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2”

"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.9) Gecko/20071110 Firefox/2.0.0.9"
@@proxy_url =

@@user_agent = “Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080218 Firefox/2.0.0.12”

nil
@@proxy_excluded_urls =
[]
@@proxy_reverse =

if true, excluded_urls list becomes a list with the only urls the proxy should be used for

false

Class Method Summary collapse

Class Method Details

.fetch_page_get(url, headers = nil, follow = 10) ⇒ Object



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
# File 'lib/lyrics/utils/http.rb', line 94

def HTTP.fetch_page_get( url, headers=nil, follow=10 )
	begin

		p_url = HTTP.parse_uri( url )
		return nil, url if p_url.host == nil || p_url.port == nil || p_url.request_uri == nil
		proxy_host, proxy_port, proxy_user, proxy_pass = HTTP.get_url_proxy_settings( url )

		full_headers = {
			"User-Agent" => @@user_agent,
			"Referer" => "#{p_url.scheme}://#{p_url.host}",
		}
		full_headers.merge!( headers ) if headers

		http = Net::HTTP.new( p_url.host, p_url.port, proxy_host, proxy_port, proxy_user, proxy_pass )
		response = http.request_get( p_url.request_uri, full_headers )

		case response
			when Net::HTTPSuccess
			when Net::HTTPRedirection, Net::HTTPFound
				if follow == 0
					response = nil
				elsif follow > 0
					response, url = HTTP.fetch_page_get( response["location"], nil, follow-1 )
				end
			else
				response = nil
		end

		return response, url

	rescue Errno::ETIMEDOUT, Errno::EBADF, EOFError => e
		raise TimeoutError.new( e.to_s )
	end
end

.fetch_page_post(url, params, headers = nil, follow = 10) ⇒ Object



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
# File 'lib/lyrics/utils/http.rb', line 129

def HTTP.fetch_page_post( url, params, headers=nil, follow=10 )
	begin
		p_url = HTTP.parse_uri( url )
		return nil, url if p_url.host == nil || p_url.port == nil || p_url.request_uri == nil
		proxy_host, proxy_port, proxy_user, proxy_pass = HTTP.get_url_proxy_settings( url )

		data, full_headers = URLEncodedFormData.prepare_query( params )
		full_headers["User-Agent"] = @@user_agent
		full_headers["Referer"] = "#{p_url.scheme}://#{p_url.host}"
		full_headers.merge!( headers ) if headers

		http = Net::HTTP.new( p_url.host, p_url.port, proxy_host, proxy_port, proxy_user, proxy_pass )
		response = http.request_post( p_url.request_uri, data, full_headers )

		case response
			when Net::HTTPSuccess
			when Net::HTTPRedirection, Net::HTTPFound
				if follow == 0
					response = nil
				elsif follow > 0
					response, url = HTTP.fetch_page_get( response["location"], nil, follow-1 )
				end
			else
				response = nil
		end

		return response, url

	rescue Errno::ETIMEDOUT, Errno::EBADF, EOFError => e
		raise TimeoutError.new( e.to_s )
	end
end

.fetch_page_post_form_multipart(url, params, headers = nil, follow = 10) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lyrics/utils/http.rb', line 162

def HTTP.fetch_page_post_form_multipart( url, params, headers=nil, follow=10 )
	begin

		p_url = HTTP.parse_uri( url )
		return nil, url if p_url.host == nil || p_url.port == nil || p_url.request_uri == nil
		proxy_host, proxy_port, proxy_user, proxy_pass = HTTP.get_url_proxy_settings( url )

		data, full_headers = MultipartFormData.prepare_query( params )
		full_headers["User-Agent"] = @@user_agent
		full_headers["Referer"] = "#{p_url.scheme}://#{p_url.host}"
		full_headers.merge!( headers ) if headers

		http = Net::HTTP.new( p_url.host, p_url.port, proxy_host, proxy_port, proxy_user, proxy_pass )
		response = http.request_post( p_url.request_uri, data, full_headers )

		case response
			when Net::HTTPSuccess
			when Net::HTTPRedirection, Net::HTTPFound
				if follow == 0
					response = nil
				elsif follow > 0
					response, url = HTTP.fetch_page_get( response["location"], nil, follow-1 )
				end
			else
				response = nil
		end

		return response, url

	rescue Errno::ETIMEDOUT, Errno::EBADF, EOFError => e
		raise TimeoutError.new( e.to_s )
	end
end

.get_proxy_settingsObject



56
57
58
59
60
# File 'lib/lyrics/utils/http.rb', line 56

def HTTP.get_proxy_settings()
	ret = [@@proxy_url ? @@proxy_url.dup : nil, [], @@proxy_reverse]
	@@proxy_excluded_urls.each() { |url| ret[1][ret[1].size] = url.dup  }
	return ret
end

.get_url_proxy_settings(url) ⇒ Object

returns proxy_host, proxy_port, proxy_user, proxy_pass for given url



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lyrics/utils/http.rb', line 63

def HTTP.get_url_proxy_settings( url )

	return nil, nil, nil, nil if ! @@proxy_url
	proxy = HTTP.parse_uri( @@proxy_url )
	return nil, nil, nil, nil if ! proxy.host
	proxy.port = 80 if ! proxy.port

	# check if given url should be treated specially
	exception = false
	@@proxy_excluded_urls.each() do |exception_url|
		if url.index( exception_url ) == 0
			exception = true
			break
		end
	end

	if exception && @@proxy_reverse || ! exception && ! @@proxy_reverse
		return proxy.host, proxy.port, proxy.user, proxy.password
	else
		return nil, nil, nil, nil
	end
end

.normalize_url(url, protocol = "http") ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/lyrics/utils/http.rb', line 34

def HTTP.normalize_url( url, protocol="http" )
	url = url.strip()
	protocol_regexp = /^ *([^: ]+):\/+/
	md = protocol_regexp.match( url )
	return nil if md && md[1] != protocol
	url.gsub!( /\/+$/, "" )				# remove / at the end of the url
	url.gsub!( protocol_regexp, "" )	# remove the protocol part if there was one
	return "#{protocol}://#{url}"		# reinsert protocol part assuring protocol:// form
end

.parse_uri(uri) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/lyrics/utils/http.rb', line 86

def HTTP.parse_uri( uri )
	begin
		return URI.parse( uri )
	rescue URI::InvalidURIError
		return URI.parse( URI.escape( uri ) )
	end
end

.set_proxy_settings(proxy_url, excluded_urls = [], reverse = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lyrics/utils/http.rb', line 44

def HTTP.set_proxy_settings( proxy_url, excluded_urls=[], reverse=false )
	@@proxy_url = proxy_url ? HTTP.normalize_url( proxy_url, "http" ) : nil
	@@proxy_reverse = @@proxy_url ? reverse : false
	@@proxy_excluded_urls = []
	if @@proxy_url
		excluded_urls.each() do |url|
			url = normalize_url( url, "http" )
			@@proxy_excluded_urls.insert( -1, url ) if url && ! @@proxy_excluded_urls.include?( url )
		end
	end
end