Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 40f5a6e9af
Fetching contributors…

Cannot retrieve contributors at this time

executable file 583 lines (514 sloc) 16.013 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 81 82 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 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 161 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#!/usr/bin/env ruby
#
# == Description
#
# This is a small, lightweight, powerful HttpRequest class based on the 'net/http' and 'net/ftp' libraries,
# it's easy to use to send http request and get response, also can use it as a shell script on command line in some cases.
#
# == Example
#
# Please read README.rdoc
#
# == Version
#
# v1.1.7
#
# Last Change: 18 Aug, 2010
#
# == Author
#
# xianhua.zhou<xianhua.zhou@gmail.com>
#
#
require 'cgi'
require 'net/http'
require 'net/https'
require 'net/ftp'
require 'singleton'
require 'digest/md5'
require 'stringio'

class HttpRequest
include Singleton
class << self
# version
VERSION = '1.1.7'.freeze
def version;VERSION;end

# avaiabled http methods
def http_methods
%w{get head post put proppatch lock unlock options propfind delete move copy mkcol trace}
end

# return data with or without block
def data(response, &block)
response.url = @@__url if defined? @@__url
block_given? ? block.call(response) : response
end

# update cookies
def update_cookies(response)
return unless response.header['set-cookie']
      response.get_fields('set-cookie').each {|k|
        k, v = k.split(';')[0].split('=')
        @@__cookies[k] = v
      }
      # response.header['set-cookie'].each_line {|k|
      # k, v = k.split(';')[0].split('=')
      # @@__cookies[k] = v
      # }
end

# return cookies
def cookies
@@__cookies
end

# check the http resource whether or not available
def available?(url, timeout = 5)
timeout(timeout) {
u = URI(url)
s = TCPSocket.new(u.host, u.port)
s.close
}
return true
rescue Exception => e
return false
end
end

def data(response, &block)
self.class.data(response, &block)
end

# send request by some given parameters
def request(method, options, &block)

# parse the @options
parse_options(method, options)

# parse and merge for the options[:parameters]
parse_parameters

# send http request and get the response
response = send_request_and_get_response

return data(response, &block) unless @options[:redirect]

# redirect?
process_redirection response, &block
end

# catch all available http requests
def self.method_missing(method_name, *options, &block)
        options = if options.size.eql? 2
                      options.last.merge({:url => options.first})
                  else
                      options.first
                  end
        @@redirect_times = 0
# we need to retrieve the cookies from last http response before reset cookies if it's a Net::HTTPResponse
options[:cookies] = options[:cookies].cookies if options[:cookies].is_a? Net::HTTPResponse
@@__cookies = {}
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, options, &block)
end

# for ftp, no plan to add new features to this method except bug fixing
def self.ftp(method, options, &block)
options = {:url => options} if options.is_a? String
options = {:close => true}.merge(options)
@@__url = 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_name unless options[:username]
options[:password] = guest_pass if options[:password].nil?
ftp = Net::FTP.open(uri.host, options[:username], options[:password])
return data(ftp, &block) unless method
stat = case method.to_sym
when :get_as_string
require 'tempfile'
tmp = Tempfile.new('http_request_ftp')
ftp.getbinaryfile(uri.path, tmp.path)
ftp.response = tmp.read
tmp.close
unless block_given?
ftp.close
return ftp.response
end
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] and not block_given?
ftp.close
stat
else
ftp.response = stat unless ftp.response
data(ftp, &block)
end
end

private

def md5(string)
Digest::MD5.hexdigest string
end

# get params for the Digest auth
# see: http://www.rooftopsolutions.nl/article/223
def get_params_for_digest
return '' unless @options[:auth_username] and @options[:auth_password]
user, passwd = @options[:auth_username], @options[:auth_password]
hr = self.class.send @options[:method],
@uri.userinfo ?
@@__url.sub(/:\/\/(.+?)@/, '://') :
@@__url

params = HttpRequestParams.parse hr['WWW-Authenticate'].split(' ', 2).last
method = @options[:method].upcase
cnonce = md5(rand.to_s + Time.new.to_s)
nc = "00000001"

data = []
data << md5("#{user}:#{params['realm']}:#{passwd}") \
<< params['nonce'] \
<< ('%08x' % nc) \
<< cnonce \
<< params['qop'] \
<< md5("#{method}:#{@uri.path}")

params = params.update({
:username => user,
:nc => nc,
:cnonce => cnonce,
:uri => @uri.path,
:method => method,
:response => md5(data.join(":"))
})

headers = []
params.each {|k, v| headers << "#{k}=\"#{v}\"" }
headers.join(", ")
rescue
''
end

# for the Http Auth if need
def parse_http_auth
if @options[:auth] or
@uri.userinfo or
(@options[:auth_username] and @options[:auth_password])

if @options[:auth].is_a? Hash
@options[:auth_username] = @options[:auth][:username]
@options[:auth_password] = @options[:auth][:password]
@options[:auth] = @options[:auth][:type]
elsif @uri.userinfo and (!@options[:auth_username] or !@options[:auth_password])
@options[:auth_username], @options[:auth_password] = @uri.userinfo.split(/:/, 2)
end

if @options[:auth_username] and @options[:auth_password]
# Digest Auth
if @options[:auth].to_s == 'digest'
digest = get_params_for_digest
@headers['Authorization'] = "Digest " + digest unless digest.empty?
else
# Basic Auth
@headers['Authorization'] = "Basic " +
["#{@options[:auth_username]}:#{@options[:auth_password]}"].pack('m').delete!("\r\n")
end
end

end
end

# initialize for the http request
def parse_options(method, options)
options = {:url => options.to_s} if [String, Array].include? options.class
@options = {
:ssl_port => 443,
:redirect_limits => 5,
:redirect => true,
:url => nil,
:ajax => false,
:xhr => false,
:method => method
}
@options.merge!(options)
@@__url = @options[:url]
@uri = URI(@options[:url])
@uri.path = '/' if @uri.path.empty?
@headers = {
'Host' => @uri.host,
'Referer' => @options[:url],
'User-Agent' => 'HttpRequest.rb ' + self.class.version
}

# support gzip
begin; require 'zlib'; rescue LoadError; end
@headers['Accept-Encoding'] = 'gzip,deflate' if defined? ::Zlib

# ajax calls?
@headers['X-Requested-With'] = 'XMLHttpRequest' if @options[:ajax] or @options[:xhr]

# Http Authenication
parse_http_auth

# 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
end

# parse parameters for the options[:parameters] and @uri.query
def parse_parameters
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
@options[:parameters] = '' if @options[:parameters].nil?
if not @uri.query.to_s.empty?
@options[:parameters] << (@options[:parameters].empty? ? @uri.query : "&#{@uri.query}")
end

# for uploading files
build_multipart if @options[:files].is_a?(Array) and 'post'.eql?(@options[:method])
end

# for uploading files
def build_multipart
boundary = md5(rand.to_s).to_s[0..5]
@headers['Content-type'] = "multipart/form-data, boundary=#{boundary}"
multipart = []
if @options[:parameters]
@options[:parameters] = CGI.parse(@options[:parameters]) if @options[:parameters].is_a? String
if @options[:parameters].is_a? Hash
@options[:parameters].each {|k, v|
multipart << "--#{boundary}"
multipart << "Content-disposition: form-data; name=\"#{CGI.escape(k.to_s)}\""
multipart << "\r\n#{v.to_s}"
# if k.to_s == "timestamp" # just for open taobaoapi
# multipart << "\r\n#{v.to_s}"
# else
# multipart << "\r\n#{CGI.escape(v.to_s)}"
# end
}
end
end
@options[:files].each_with_index {|f, index|
f[:field_name] ||= "files[]"
f[:file_name] ||= "#{boundary}_#{index}"
f[:transfer_encoding] ||= "binary"
f[:content_type] ||= 'application/octet-stream'
multipart << "--#{boundary}"
multipart << "Content-disposition: form-data; name=\"#{f[:field_name]}\"; filename=\"#{f[:file_name]}\""
multipart << "Content-type: #{f[:content_type]}"
multipart << "Content-Transfer-Encoding: #{f[:transfer_encoding]}"
multipart << "\r\n#{f[:file_content]}"
}
multipart << "--#{boundary}--"
multipart = multipart.join("\r\n")
@headers['Content-length'] = "#{multipart.size}"
@options[:parameters] = multipart
end

# send request and get the response by some options
def send_request_and_get_response
# for proxy
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

# sending request and get response
send_request http
end

# send http request
def send_request(http)
# xml data?
if @options[:parameters].to_s[0..4].eql?('<?xml') and @options[:method].eql? 'post'
@headers['Content-Type'] = 'application/xml'
@headers['Content-Length'] = @options[:parameters].size.to_s
@headers['Content-MD5'] = md5(@options[:parameters]).to_s
end

# GO !!
if @options[:method] =~ /^(get|head|options|delete|move|copy|trace|)$/
@options[:parameters] = "?#{@options[:parameters]}" if @options[:parameters]
            path = if @options[:parameters] =~ /^\?+$/
                       @uri.path
                   else
                       @uri.path + @options[:parameters]
                   end
            h = http.method(@options[:method]).call(path, @headers)
else
h = http.method(@options[:method]).call(@uri.path, @options[:parameters], @headers)
end

self.class.update_cookies h
h
end

# process the redirectation if need
def process_redirection(response, &block)
case response
when Net::HTTPRedirection
url = "#{@uri.scheme}://#{@uri.host}#{':' + @uri.port.to_s if @uri.port != 80}"
            last_url = @options[:url]
@options[:url] = case response['location']
when /^https?:\/\//i
response['location']
when /^\//
url + response['location']
when /^(\.\.\/|\.\/)/
paths = (File.dirname(@uri.path) + '/' + response['location']).split('/')
location = []
paths.each {|path|
next if path.empty? || path.eql?('.')
path == '..' ? location.pop : location.push(path)
}
url + '/' + location.join('/')
else
url + File.dirname(@uri.path) + '/' + response['location']
end
            return data(response, &block) if @@redirect_times > 2 and @options[:url].eql? last_url
@@redirect_times += 1
raise 'too many redirects...' if @@redirect_times > @options[:redirect_limits]
if @options[:cookies].nil?
@options[:cookies] = self.class.cookies
else
@options[:cookies] = @options[:cookies].update self.class.cookies
end
            @options.delete :parameters
            @options.delete :method
request('get', @options, &block)
else
data(response, &block)
end
end

end

module Net
class HTTPResponse

attr_accessor :url

# get cookies as a hash
def cookies
HttpRequest.cookies
end

# for gzipped body
def body
bd = read_body()
return bd unless bd
if (self['content-encoding'] == 'gzip') and defined?(::Zlib)
::Zlib::GzipReader.new(StringIO.new(bd)).read
else
bd
end
end

# body
def raw_body
read_body()
end

# detect the response code
#
# Example:
# puts HttpRequest.get('http://www.example.com').code_200?
# puts HttpRequest.get('http://www.example.com').code_2xx?
# HttpRequest.get('http://www.example.com/404.html') {|http|
# puts "IS 4xx" if http.code_4xx?
# puts "IS 404" if http.code_404?
# }
#
# supported methods
# code_1xx? code_2xx? code_3xx? code_4xx? code_5xx?
# code_100? code_101? code_200? code_201? ... code_505?
def method_missing(method_name)
case method_name.to_s
when /^(code|status)_([0-9])xx\?$/
not CODE_CLASS_TO_OBJ[$2].nil? and is_a? CODE_CLASS_TO_OBJ[$2]
when /^(code|status)_([0-9]+)\?$/
not CODE_TO_OBJ[$2].nil? and is_a? CODE_TO_OBJ[$2]
else
raise NoHttpMethodException, 'Unknown method of response code!'
end
end

end
end

# for ftp response
class Net::FTP
def response=(response)
@_response = response
end

def response
@_response
end
end

# from Rack, parsing parameters for the Digest auth
class HttpRequestParams < Hash
def self.parse(str)
split_header_value(str).inject(new) do |header, param|
k, v = param.split('=', 2)
header[k] = dequote(v)
header
end
end

def self.dequote(str) # From WEBrick::HTTPUtils
ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
ret.gsub!(/\\(.)/, "\\1")
ret
end

def self.split_header_value(str)
str.scan( /(\w+\=(?:"[^\"]+"|[^,]+))/n ).collect{ |v| v[0] }
end

def initialize
super

yield self if block_given?
end

def [](k)
super k.to_s
end

def []=(k, v)
super k.to_s, v.to_s
end

UNQUOTED = ['qop', 'nc', 'stale']

def to_s
inject([]) do |parts, (k, v)|
parts << "#{k}=" + (UNQUOTED.include?(k) ? v.to_s : quote(v))
parts
end.join(', ')
end

def quote(str) # From WEBrick::HTTPUtils
'"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
end
end


# exception
class NoHttpMethodException < Exception; end

# for command line
if __FILE__.eql? $0
method, url, params = ARGV
exit unless method
source_method = method
method = method.split('_')[0] if method.include? '_'

# fix path of the url
url = "http://#{url}" unless url =~ /^(https?:\/\/)/i

params = if params
"{:url => '#{url}', :parameters => '" + params + "'}"
else
"'#{url}'"
end

if HttpRequest.http_methods.include?(method) && url
http = eval("HttpRequest.#{method}(#{params})")
case source_method
when /_only_header$/
http.each{|k,v| puts "#{k}: #{v}"}
when /_with_header$/
http.each{|k,v| puts "#{k}: #{v}"}
print http.body unless http.body.to_s.empty?
else
print http.body unless http.body.to_s.empty?
end
end

end
Something went wrong with that request. Please try again.