http_request.rb 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Changelog +5 -0
  2. data/README.rdoc +13 -3
  3. data/lib/http_request.rb +78 -24
  4. metadata +2 -2
data/Changelog CHANGED
@@ -1,3 +1,8 @@
1
+ v1.0.3
2
+ * add some dynamic methods such as code_1xx?, code_200?, code_403? etc. for check the http status
3
+ * decompressing gzipped body automatically, the 'Accept-Encoding: gzip,deflate' has been added into the header for sending request automatically.
4
+ * fixed retrieve cookies problem which is if a cookie name is a keyword of the cookie's info such as path, expire etc. then it doesn't work.
5
+
1
6
  v1.0.2
2
7
  * Proc.call (block) style supported and some small changes
3
8
  * improve ftp protocol and add a new method get_as_string for the ftp calls
data/README.rdoc CHANGED
@@ -189,18 +189,28 @@ download multiple files from a directory
189
189
  http.cookies.each {|k, v| puts "#{k} => #{v}" }
190
190
  }
191
191
 
192
- HttpRequest.ftp(:get, {"ftp://user:pass@localhost", :to => }) {|ftp|
192
+ HttpRequest.ftp(:get, {"ftp://user:pass@localhost/soft.zip", :to => '/path/to/soft.zip'}) {|ftp|
193
193
  puts ftp.ls
194
194
  ftp.chdir('soft')
195
195
  ftp.getbinaryfile('ruby.exe', '/path/to/local/ruby.exe');
196
196
  }
197
197
 
198
+ == check the http status (since v1.0.3)
199
+
200
+ HttpRequest.get('http://www.example.com/it_doesnot_exists.page').code_4xx? # true
201
+ HttpRequest.get('http://www.example.com/it_doesnot_exists.page').code_404? # true
202
+ HttpRequest.get('http://www.rubyonrails.com/').code_404? # false
203
+ HttpRequest.head('http://www.rubyonrails.com/').code_2xx? # true
204
+ HttpRequest.head('http://www.ruby-lang.org/').code_200? # true
205
+
206
+ supported methods: code_1xx? code_2xx? code_3xx? code_4xx? code_5xx? code_101? code_200? ...
207
+
198
208
  == TODO
199
209
 
200
- ......
210
+ bug fixing, testing and testing...
201
211
 
202
212
  == LATEST VERSION
203
- 1.0.2
213
+ 1.0.3
204
214
 
205
215
  == Author
206
216
 
data/lib/http_request.rb CHANGED
@@ -11,9 +11,9 @@
11
11
  #
12
12
  # == Version
13
13
  #
14
- # v1.0.2
14
+ # v1.0.3
15
15
  #
16
- # Last Change: 2 May, 2009
16
+ # Last Change: 27 May, 2009
17
17
  #
18
18
  # == Author
19
19
  #
@@ -21,16 +21,19 @@
21
21
  # homepage: http://my.cnzxh.net
22
22
  #
23
23
  #
24
+ require 'cgi'
24
25
  require 'net/http'
25
26
  require 'net/https'
26
- require 'cgi'
27
+ require 'net/ftp'
27
28
  require 'singleton'
29
+ require 'md5'
30
+ require 'stringio'
28
31
 
29
32
  class HttpRequest
30
33
  include Singleton
31
34
 
32
35
  # version
33
- VERSION = '1.0.2'.freeze
36
+ VERSION = '1.0.3'.freeze
34
37
  def self.version;VERSION;end
35
38
 
36
39
  # avaiabled http methods
@@ -43,6 +46,18 @@ class HttpRequest
43
46
  block_given? ? block.call(response) : response
44
47
  end
45
48
 
49
+ # check the http resource whether or not available
50
+ def self.available?(url, timeout = 5)
51
+ timeout(timeout) {
52
+ u = URI(url)
53
+ s = TCPSocket.new(u.host, u.port)
54
+ s.close
55
+ }
56
+ return true
57
+ rescue Exception => e
58
+ return false
59
+ end
60
+
46
61
  # send request by some given parameters
47
62
  def request(method, opt, &block)
48
63
  init_args(method, opt)
@@ -115,7 +130,6 @@ class HttpRequest
115
130
 
116
131
  # for ftp
117
132
  def self.ftp(method, options, &block)
118
- require 'net/ftp'
119
133
  options = {:url => options} if options.is_a? String
120
134
  options = {:close => true}.merge(options)
121
135
  options[:url] = "ftp://#{options[:url]}" unless options[:url] =~ /^ftp:\/\//
@@ -177,9 +191,10 @@ class HttpRequest
177
191
  @uri = URI(@options[:url])
178
192
  @uri.path = '/' if @uri.path.empty?
179
193
  @headers = {
180
- 'Host' => @uri.host,
181
- 'Referer' => @options[:url],
182
- 'User-Agent' => 'HttpRequest.rb ' + VERSION
194
+ 'Host' => @uri.host,
195
+ 'Accept-Encoding' => 'gzip,deflate',
196
+ 'Referer' => @options[:url],
197
+ 'User-Agent' => 'HttpRequest.rb ' + VERSION
183
198
  }
184
199
 
185
200
  # Basic Authenication
@@ -207,7 +222,6 @@ class HttpRequest
207
222
 
208
223
  # for upload files by post method
209
224
  def build_multipart
210
- require 'md5'
211
225
  boundary = MD5.md5(rand.to_s).to_s[0..5]
212
226
  @headers['Content-type'] = "multipart/form-data, boundary=#{boundary}"
213
227
  multipart = []
@@ -264,20 +278,60 @@ class HttpRequest
264
278
 
265
279
  end
266
280
 
267
- # get cookies as hash
268
- class Net::HTTPResponse
269
- def cookies
270
- cookies = {}
271
- ignored_cookie_names = %w{expires domain path secure httponly}
272
- self['set-cookie'].split(/[;,]/).each {|it|
273
- next unless it.include? '='
274
- eq = it.index('=')
275
- key = it[0...eq].strip
276
- value = it[eq.succ..-1]
277
- next if ignored_cookie_names.include? key.downcase
278
- cookies[key] = value
279
- }
280
- cookies
281
+ module Net
282
+ class HTTPResponse
283
+
284
+ # get cookies as a hash
285
+ def cookies
286
+ cookies = {}
287
+ return cookies unless @header['set-cookie']
288
+ @header['set-cookie'].each {|k|
289
+ k, v = k.split(';')[0].split('=')
290
+ cookies[k] = v
291
+ }
292
+ cookies
293
+ end
294
+
295
+ # for gzipped body
296
+ def body
297
+ bd = read_body()
298
+ return bd unless bd
299
+ unless self['content-encoding'].eql? 'gzip'
300
+ bd
301
+ else
302
+ ::Zlib::GzipReader.new(StringIO.new(bd)).read
303
+ end
304
+ end
305
+
306
+ # body
307
+ def raw_body
308
+ read_body()
309
+ end
310
+
311
+ # detect the response code
312
+ #
313
+ # Example:
314
+ # puts HttpRequest.get('http://www.example.com').code_200?
315
+ # puts HttpRequest.get('http://www.example.com').code_2xx?
316
+ # HttpRequest.get('http://www.example.com/404.html') {|http|
317
+ # puts "IS 4xx" if http.code_4xx?
318
+ # puts "IS 404" if http.code_404?
319
+ # }
320
+ #
321
+ # supported methods
322
+ # code_1xx? code_2xx? code_3xx? code_4xx? code_5xx?
323
+ # code_100? code_101? code_200? code_201? ... code_505?
324
+ def method_missing(method_name)
325
+ case method_name.to_s
326
+ when /^code_([0-9])xx\?$/
327
+ is_a? CODE_CLASS_TO_OBJ[$1]
328
+ when /^code_([0-9]+)\?$/
329
+ is_a? CODE_TO_OBJ[$1]
330
+ else
331
+ raise NoHttpMethodException, 'Unknown method of response code!'
332
+ end
333
+ end
334
+
281
335
  end
282
336
  end
283
337
 
@@ -309,7 +363,7 @@ if __FILE__.eql? $0
309
363
  "{:url => '#{url}', :parameters => '" + params + "'}"
310
364
  else
311
365
  "'#{url}'"
312
- end
366
+ end
313
367
 
314
368
  if HttpRequest.http_methods.include?(method) && url
315
369
  http = eval("HttpRequest.#{method}(#{params})")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_request.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - xianhua.zhou
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-02 00:00:00 +08:00
12
+ date: 2009-05-27 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15