http_request.rb 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/Changelog +4 -0
  2. data/README.rdoc +14 -0
  3. data/lib/http_request.rb +25 -15
  4. metadata +2 -2
data/Changelog CHANGED
@@ -1,3 +1,7 @@
1
+ v1.0.4
2
+ * support gzipped content only if the zlib library is loaded
3
+ * post xml data via the POST method
4
+
1
5
  v1.0.3
2
6
  * add some dynamic methods such as code_1xx?, code_200?, code_403? etc. for check the http status
3
7
  * decompressing gzipped body automatically, the 'Accept-Encoding: gzip,deflate' has been added into the header for sending request automatically.
data/README.rdoc CHANGED
@@ -205,6 +205,20 @@ download multiple files from a directory
205
205
 
206
206
  supported methods: code_1xx? code_2xx? code_3xx? code_4xx? code_5xx? code_101? code_200? ...
207
207
 
208
+ == check whether or not the remote site is available (since v1.0.3)
209
+
210
+ # return true if can access to the website with socket connecation even it is a 500 or 404 page, otherwise, return false
211
+ HttpRequest.available?('http://www.github.com/')
212
+
213
+ == send XML data via the post method (since v1.0.4)
214
+
215
+ xml = '<?xml version="1.0" encoding="utf-8" ?>
216
+ <items>
217
+ <item id="it1">item one</item>
218
+ <item id="it2">item two</item>
219
+ </items>'
220
+ HttpRequest.post(:url => 'http://localhost/xml.php', :parameters = xml)
221
+
208
222
  == TODO
209
223
 
210
224
  bug fixing, testing and testing...
data/lib/http_request.rb CHANGED
@@ -11,9 +11,9 @@
11
11
  #
12
12
  # == Version
13
13
  #
14
- # v1.0.3
14
+ # v1.0.4
15
15
  #
16
- # Last Change: 27 May, 2009
16
+ # Last Change: 9 June, 2009
17
17
  #
18
18
  # == Author
19
19
  #
@@ -33,7 +33,7 @@ class HttpRequest
33
33
  include Singleton
34
34
 
35
35
  # version
36
- VERSION = '1.0.3'.freeze
36
+ VERSION = '1.0.4'.freeze
37
37
  def self.version;VERSION;end
38
38
 
39
39
  # avaiabled http methods
@@ -192,11 +192,14 @@ class HttpRequest
192
192
  @uri.path = '/' if @uri.path.empty?
193
193
  @headers = {
194
194
  'Host' => @uri.host,
195
- 'Accept-Encoding' => 'gzip,deflate',
196
195
  'Referer' => @options[:url],
197
196
  'User-Agent' => 'HttpRequest.rb ' + VERSION
198
197
  }
199
198
 
199
+ # support gzip
200
+ begin; require 'zlib'; rescue LoadError; end
201
+ @headers['Accept-Encoding'] = 'gzip,deflate' if defined? Zlib
202
+
200
203
  # Basic Authenication
201
204
  @headers['Authorization'] = "Basic " + [@uri.userinfo].pack('m').delete!("\r\n") if @uri.userinfo
202
205
 
@@ -255,14 +258,21 @@ class HttpRequest
255
258
  # send http request
256
259
  def send_request(http)
257
260
 
258
- # merge parameters
259
- parameters = @options[:parameters].to_s
260
- @options[:parameters] = "#{@uri.query}" if @uri.query
261
- if parameters
262
- if @options[:parameters]
263
- @options[:parameters] << "&#{parameters}"
264
- else
265
- @options[:parameters] = "#{parameters}"
261
+ # xml data?
262
+ if @options[:parameters].to_s[0..4].eql?('<?xml') and @options[:method].eql? 'post'
263
+ @headers['Content-Type'] = 'application/xml'
264
+ @headers['Content-Length'] = @options[:parameters].size.to_s
265
+ @headers['Content-MD5'] = MD5.md5(@options[:parameters]).to_s
266
+ else
267
+ # merge parameters
268
+ parameters = @options[:parameters].to_s
269
+ @options[:parameters] = "#{@uri.query}" if @uri.query
270
+ if parameters
271
+ if @options[:parameters]
272
+ @options[:parameters] << "&#{parameters}"
273
+ else
274
+ @options[:parameters] = "#{parameters}"
275
+ end
266
276
  end
267
277
  end
268
278
 
@@ -296,10 +306,10 @@ module Net
296
306
  def body
297
307
  bd = read_body()
298
308
  return bd unless bd
299
- unless self['content-encoding'].eql? 'gzip'
300
- bd
301
- else
309
+ if (self['content-encoding'] == 'gzip') and defined?(Zlib)
302
310
  ::Zlib::GzipReader.new(StringIO.new(bd)).read
311
+ else
312
+ bd
303
313
  end
304
314
  end
305
315
 
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.3
4
+ version: 1.0.4
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-27 00:00:00 +08:00
12
+ date: 2009-06-13 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15