Class: HttpRequest

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/http_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ftp(method, options) ⇒ Object

for ftp



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
# File 'lib/http_request.rb', line 134

def self.ftp(method, options)
  require 'net/ftp'
  options = {:close => true}.merge(options)
  options[:url] = "ftp://#{options[:url]}" unless options[:url] =~ /^ftp:\/\//
  uri = URI(options[:url])
  guest_name, guest_pass = 'anonymous', 'guest@' + uri.host
  unless options[:username]
    options[:username], options[:password] = uri.userinfo ? uri.userinfo.split(':') : [guest_name, guest_pass]
  end
  options[:username] = guest_user unless options[:username]
  options[:password] = guest_pass unless options[:password]
  ftp = Net::FTP.open(uri.host, options[:username], options[:password])
  return ftp unless method
  stat = case method.to_sym
         when :get
           options[:to] = File.basename(uri.path) unless options[:to]
           ftp.getbinaryfile(uri.path, options[:to])
         when :put
           ftp.putbinaryfile(options[:from], uri.path)
         when :mkdir, :rmdir, :delete, :size, :mtime, :list, :nlst
           ftp.method(method).call(uri.path)
         when :rename
           ftp.rename(uri.path, options[:to]) if options[:to]
         when :status
           ftp.status
         else
           return ftp
         end
  if options[:close]
    ftp.close
    stat
  else
    ftp.response = stat
    ftp
  end
end

.http_methodsObject



32
33
34
# File 'lib/http_request.rb', line 32

def self.http_methods
   %w{get head post put proppatch lock unlock options propfind delete move copy mkcol trace}
end

.method_missing(method_name, args) ⇒ Object

catch all of http requests



172
173
174
175
176
# File 'lib/http_request.rb', line 172

def self.method_missing(method_name, args)
	method_name = method_name.to_s.downcase
	raise NoHttpMethodException, "No such http method can be called: #{method_name}" unless self.http_methods.include?(method_name)
	self.instance.request(method_name, args)
end

Instance Method Details

#init_args(method, options) ⇒ Object

initialize



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/http_request.rb', line 37

def init_args(method, options)
	options = {:url => options.to_s} if [String, Array].include? options.class
	@options = {
		:ssl_port        => 443,
		:redirect_limits => 5,
		:redirect        => true,
		:url             => nil
	}
	@options.merge!(options)
	@uri = URI(@options[:url])
   @uri.path = '/' if @uri.path.empty?
	@headers = {
     'Host' => @uri.host,
     'Referer' => @options[:url],
     'Accept-Language' => 'en-us,zh-cn;q=0.7,en;q=0.3',
     'Accept-Charset' => 'zh-cn,zh;q=0.5',
     'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
     'Cache-Control' => 'max-age=0',
     'User-Agent' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.04 (hardy) Firefox/3.0.7',
     'Connection' => 'keep-alive'
	}

   # Basic Authenication
   @headers['Authorization'] = "Basic " + [@uri.userinfo].pack('m').delete!("\r\n") if @uri.userinfo

   # headers
   @options[:headers].each {|k, v| @headers[k] = v} if @options[:headers].is_a? Hash

   # add cookies if have
   if @options[:cookies]
     if @options[:cookies].is_a? Hash
       cookies = []
       @options[:cookies].each {|k, v|
         cookies << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
       }
       cookies = cookies.join('; ')
     else
       cookies = @options[:cookies].to_s
     end
     @headers['Cookie'] = cookies unless cookies.empty?
   end

   @redirect_times = 0 if @options[:redirect]
end

#request(method, opt) ⇒ Object

send request



83
84
85
86
87
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
# File 'lib/http_request.rb', line 83

def request(method, opt)
	init_args(method, opt)
	@options[:method] = method

   # for upload files
   if @options[:files].is_a?(Array) && 'post'.eql?(method)
     build_multipart
   else
     if @options[:parameters].is_a? Hash
       @options[:parameters] = @options[:parameters].collect{|k, v| 
         CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
       }.join('&')
     end
   end

	http =  if @options[:proxy_addr]
     if @options[:proxy_user] and @options[:proxy_pass]
       Net::HTTP::Proxy(@options[:proxy_addr], @options[:proxy_port], @options[:proxy_user], @options[:proxy_pass]).new(@u.host, @u.port)
     else
       Net::HTTP::Proxy(@options[:proxy_addr], @options[:proxy_port]).new(@uri.host, @uri.port)
     end
   else
     Net::HTTP.new(@uri.host, @uri.port)
   end

	# ssl support
	http.use_ssl = true if @uri.scheme =~ /^https$/i

	# get data by post or get method.
	response = send_request http

	return response unless @options[:redirect]

	# redirect....===>>>
	case response
	when Net::HTTPRedirection
		@options[:url] = if response['location'] =~ /^http[s]*:\/\//i
       response['location']
     else
       @uri.scheme + '://' + @uri.host + ':' + @uri.port.to_s + response['location']
     end
		@redirect_times = 0 unless @redirect_times
		@redirect_times = @redirect_times.succ
		raise 'too deep redirect...' if @redirect_times > @options[:redirect_limits]
		request('get', @options)
	else
		response
	end
end