Class: HttpRequest
Class Method Summary collapse
-
.ftp(method, options) ⇒ Object
for ftp.
- .http_methods ⇒ Object
-
.method_missing(method_name, args) ⇒ Object
catch all of http requests.
Instance Method Summary collapse
-
#init_args(method, options) ⇒ Object
initialize.
-
#request(method, opt) ⇒ Object
send request.
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, ) require 'net/ftp' = {:close => true}.merge() [:url] = "ftp://#{[:url]}" unless [:url] =~ /^ftp:\/\// uri = URI([:url]) guest_name, guest_pass = 'anonymous', 'guest@' + uri.host unless [:username] [:username], [:password] = uri.userinfo ? uri.userinfo.split(':') : [guest_name, guest_pass] end [:username] = guest_user unless [:username] [:password] = guest_pass unless [:password] ftp = Net::FTP.open(uri.host, [:username], [:password]) return ftp unless method stat = case method.to_sym when :get [:to] = File.basename(uri.path) unless [:to] ftp.getbinaryfile(uri.path, [:to]) when :put ftp.putbinaryfile([:from], uri.path) when :mkdir, :rmdir, :delete, :size, :mtime, :list, :nlst ftp.method(method).call(uri.path) when :rename ftp.rename(uri.path, [:to]) if [:to] when :status ftp.status else return ftp end if [:close] ftp.close stat else ftp.response = stat ftp end end |
.http_methods ⇒ Object
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, ) = {:url => .to_s} if [String, Array].include? .class @options = { :ssl_port => 443, :redirect_limits => 5, :redirect => true, :url => nil } @options.merge!() @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 = [] @options[:cookies].each {|k, v| << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" } = .join('; ') else = @options[:cookies].to_s end @headers['Cookie'] = unless .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 |