Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/net
File: http.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# = net/http.rb
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (c) 1999-2007 Yukihiro Matsumoto
[4] Fix | Delete
# Copyright (c) 1999-2007 Minero Aoki
[5] Fix | Delete
# Copyright (c) 2001 GOTOU Yuuzou
[6] Fix | Delete
#
[7] Fix | Delete
# Written and maintained by Minero Aoki <aamine@loveruby.net>.
[8] Fix | Delete
# HTTPS support added by GOTOU Yuuzou <gotoyuzo@notwork.org>.
[9] Fix | Delete
#
[10] Fix | Delete
# This file is derived from "http-access.rb".
[11] Fix | Delete
#
[12] Fix | Delete
# Documented by Minero Aoki; converted to RDoc by William Webber.
[13] Fix | Delete
#
[14] Fix | Delete
# This program is free software. You can re-distribute and/or
[15] Fix | Delete
# modify this program under the same terms of ruby itself ---
[16] Fix | Delete
# Ruby Distribution License or GNU General Public License.
[17] Fix | Delete
#
[18] Fix | Delete
# See Net::HTTP for an overview and examples.
[19] Fix | Delete
#
[20] Fix | Delete
[21] Fix | Delete
require_relative 'protocol'
[22] Fix | Delete
require 'uri'
[23] Fix | Delete
autoload :OpenSSL, 'openssl'
[24] Fix | Delete
[25] Fix | Delete
module Net #:nodoc:
[26] Fix | Delete
[27] Fix | Delete
# :stopdoc:
[28] Fix | Delete
class HTTPBadResponse < StandardError; end
[29] Fix | Delete
class HTTPHeaderSyntaxError < StandardError; end
[30] Fix | Delete
# :startdoc:
[31] Fix | Delete
[32] Fix | Delete
# == An HTTP client API for Ruby.
[33] Fix | Delete
#
[34] Fix | Delete
# Net::HTTP provides a rich library which can be used to build HTTP
[35] Fix | Delete
# user-agents. For more details about HTTP see
[36] Fix | Delete
# [RFC2616](http://www.ietf.org/rfc/rfc2616.txt).
[37] Fix | Delete
#
[38] Fix | Delete
# Net::HTTP is designed to work closely with URI. URI::HTTP#host,
[39] Fix | Delete
# URI::HTTP#port and URI::HTTP#request_uri are designed to work with
[40] Fix | Delete
# Net::HTTP.
[41] Fix | Delete
#
[42] Fix | Delete
# If you are only performing a few GET requests you should try OpenURI.
[43] Fix | Delete
#
[44] Fix | Delete
# == Simple Examples
[45] Fix | Delete
#
[46] Fix | Delete
# All examples assume you have loaded Net::HTTP with:
[47] Fix | Delete
#
[48] Fix | Delete
# require 'net/http'
[49] Fix | Delete
#
[50] Fix | Delete
# This will also require 'uri' so you don't need to require it separately.
[51] Fix | Delete
#
[52] Fix | Delete
# The Net::HTTP methods in the following section do not persist
[53] Fix | Delete
# connections. They are not recommended if you are performing many HTTP
[54] Fix | Delete
# requests.
[55] Fix | Delete
#
[56] Fix | Delete
# === GET
[57] Fix | Delete
#
[58] Fix | Delete
# Net::HTTP.get('example.com', '/index.html') # => String
[59] Fix | Delete
#
[60] Fix | Delete
# === GET by URI
[61] Fix | Delete
#
[62] Fix | Delete
# uri = URI('http://example.com/index.html?count=10')
[63] Fix | Delete
# Net::HTTP.get(uri) # => String
[64] Fix | Delete
#
[65] Fix | Delete
# === GET with Dynamic Parameters
[66] Fix | Delete
#
[67] Fix | Delete
# uri = URI('http://example.com/index.html')
[68] Fix | Delete
# params = { :limit => 10, :page => 3 }
[69] Fix | Delete
# uri.query = URI.encode_www_form(params)
[70] Fix | Delete
#
[71] Fix | Delete
# res = Net::HTTP.get_response(uri)
[72] Fix | Delete
# puts res.body if res.is_a?(Net::HTTPSuccess)
[73] Fix | Delete
#
[74] Fix | Delete
# === POST
[75] Fix | Delete
#
[76] Fix | Delete
# uri = URI('http://www.example.com/search.cgi')
[77] Fix | Delete
# res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
[78] Fix | Delete
# puts res.body
[79] Fix | Delete
#
[80] Fix | Delete
# === POST with Multiple Values
[81] Fix | Delete
#
[82] Fix | Delete
# uri = URI('http://www.example.com/search.cgi')
[83] Fix | Delete
# res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
[84] Fix | Delete
# puts res.body
[85] Fix | Delete
#
[86] Fix | Delete
# == How to use Net::HTTP
[87] Fix | Delete
#
[88] Fix | Delete
# The following example code can be used as the basis of an HTTP user-agent
[89] Fix | Delete
# which can perform a variety of request types using persistent
[90] Fix | Delete
# connections.
[91] Fix | Delete
#
[92] Fix | Delete
# uri = URI('http://example.com/some_path?query=string')
[93] Fix | Delete
#
[94] Fix | Delete
# Net::HTTP.start(uri.host, uri.port) do |http|
[95] Fix | Delete
# request = Net::HTTP::Get.new uri
[96] Fix | Delete
#
[97] Fix | Delete
# response = http.request request # Net::HTTPResponse object
[98] Fix | Delete
# end
[99] Fix | Delete
#
[100] Fix | Delete
# Net::HTTP::start immediately creates a connection to an HTTP server which
[101] Fix | Delete
# is kept open for the duration of the block. The connection will remain
[102] Fix | Delete
# open for multiple requests in the block if the server indicates it
[103] Fix | Delete
# supports persistent connections.
[104] Fix | Delete
#
[105] Fix | Delete
# If you wish to re-use a connection across multiple HTTP requests without
[106] Fix | Delete
# automatically closing it you can use ::new and then call #start and
[107] Fix | Delete
# #finish manually.
[108] Fix | Delete
#
[109] Fix | Delete
# The request types Net::HTTP supports are listed below in the section "HTTP
[110] Fix | Delete
# Request Classes".
[111] Fix | Delete
#
[112] Fix | Delete
# For all the Net::HTTP request objects and shortcut request methods you may
[113] Fix | Delete
# supply either a String for the request path or a URI from which Net::HTTP
[114] Fix | Delete
# will extract the request path.
[115] Fix | Delete
#
[116] Fix | Delete
# === Response Data
[117] Fix | Delete
#
[118] Fix | Delete
# uri = URI('http://example.com/index.html')
[119] Fix | Delete
# res = Net::HTTP.get_response(uri)
[120] Fix | Delete
#
[121] Fix | Delete
# # Headers
[122] Fix | Delete
# res['Set-Cookie'] # => String
[123] Fix | Delete
# res.get_fields('set-cookie') # => Array
[124] Fix | Delete
# res.to_hash['set-cookie'] # => Array
[125] Fix | Delete
# puts "Headers: #{res.to_hash.inspect}"
[126] Fix | Delete
#
[127] Fix | Delete
# # Status
[128] Fix | Delete
# puts res.code # => '200'
[129] Fix | Delete
# puts res.message # => 'OK'
[130] Fix | Delete
# puts res.class.name # => 'HTTPOK'
[131] Fix | Delete
#
[132] Fix | Delete
# # Body
[133] Fix | Delete
# puts res.body if res.response_body_permitted?
[134] Fix | Delete
#
[135] Fix | Delete
# === Following Redirection
[136] Fix | Delete
#
[137] Fix | Delete
# Each Net::HTTPResponse object belongs to a class for its response code.
[138] Fix | Delete
#
[139] Fix | Delete
# For example, all 2XX responses are instances of a Net::HTTPSuccess
[140] Fix | Delete
# subclass, a 3XX response is an instance of a Net::HTTPRedirection
[141] Fix | Delete
# subclass and a 200 response is an instance of the Net::HTTPOK class. For
[142] Fix | Delete
# details of response classes, see the section "HTTP Response Classes"
[143] Fix | Delete
# below.
[144] Fix | Delete
#
[145] Fix | Delete
# Using a case statement you can handle various types of responses properly:
[146] Fix | Delete
#
[147] Fix | Delete
# def fetch(uri_str, limit = 10)
[148] Fix | Delete
# # You should choose a better exception.
[149] Fix | Delete
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
[150] Fix | Delete
#
[151] Fix | Delete
# response = Net::HTTP.get_response(URI(uri_str))
[152] Fix | Delete
#
[153] Fix | Delete
# case response
[154] Fix | Delete
# when Net::HTTPSuccess then
[155] Fix | Delete
# response
[156] Fix | Delete
# when Net::HTTPRedirection then
[157] Fix | Delete
# location = response['location']
[158] Fix | Delete
# warn "redirected to #{location}"
[159] Fix | Delete
# fetch(location, limit - 1)
[160] Fix | Delete
# else
[161] Fix | Delete
# response.value
[162] Fix | Delete
# end
[163] Fix | Delete
# end
[164] Fix | Delete
#
[165] Fix | Delete
# print fetch('http://www.ruby-lang.org')
[166] Fix | Delete
#
[167] Fix | Delete
# === POST
[168] Fix | Delete
#
[169] Fix | Delete
# A POST can be made using the Net::HTTP::Post request class. This example
[170] Fix | Delete
# creates a URL encoded POST body:
[171] Fix | Delete
#
[172] Fix | Delete
# uri = URI('http://www.example.com/todo.cgi')
[173] Fix | Delete
# req = Net::HTTP::Post.new(uri)
[174] Fix | Delete
# req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
[175] Fix | Delete
#
[176] Fix | Delete
# res = Net::HTTP.start(uri.hostname, uri.port) do |http|
[177] Fix | Delete
# http.request(req)
[178] Fix | Delete
# end
[179] Fix | Delete
#
[180] Fix | Delete
# case res
[181] Fix | Delete
# when Net::HTTPSuccess, Net::HTTPRedirection
[182] Fix | Delete
# # OK
[183] Fix | Delete
# else
[184] Fix | Delete
# res.value
[185] Fix | Delete
# end
[186] Fix | Delete
#
[187] Fix | Delete
# To send multipart/form-data use Net::HTTPHeader#set_form:
[188] Fix | Delete
#
[189] Fix | Delete
# req = Net::HTTP::Post.new(uri)
[190] Fix | Delete
# req.set_form([['upload', File.open('foo.bar')]], 'multipart/form-data')
[191] Fix | Delete
#
[192] Fix | Delete
# Other requests that can contain a body such as PUT can be created in the
[193] Fix | Delete
# same way using the corresponding request class (Net::HTTP::Put).
[194] Fix | Delete
#
[195] Fix | Delete
# === Setting Headers
[196] Fix | Delete
#
[197] Fix | Delete
# The following example performs a conditional GET using the
[198] Fix | Delete
# If-Modified-Since header. If the files has not been modified since the
[199] Fix | Delete
# time in the header a Not Modified response will be returned. See RFC 2616
[200] Fix | Delete
# section 9.3 for further details.
[201] Fix | Delete
#
[202] Fix | Delete
# uri = URI('http://example.com/cached_response')
[203] Fix | Delete
# file = File.stat 'cached_response'
[204] Fix | Delete
#
[205] Fix | Delete
# req = Net::HTTP::Get.new(uri)
[206] Fix | Delete
# req['If-Modified-Since'] = file.mtime.rfc2822
[207] Fix | Delete
#
[208] Fix | Delete
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
[209] Fix | Delete
# http.request(req)
[210] Fix | Delete
# }
[211] Fix | Delete
#
[212] Fix | Delete
# open 'cached_response', 'w' do |io|
[213] Fix | Delete
# io.write res.body
[214] Fix | Delete
# end if res.is_a?(Net::HTTPSuccess)
[215] Fix | Delete
#
[216] Fix | Delete
# === Basic Authentication
[217] Fix | Delete
#
[218] Fix | Delete
# Basic authentication is performed according to
[219] Fix | Delete
# [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
[220] Fix | Delete
#
[221] Fix | Delete
# uri = URI('http://example.com/index.html?key=value')
[222] Fix | Delete
#
[223] Fix | Delete
# req = Net::HTTP::Get.new(uri)
[224] Fix | Delete
# req.basic_auth 'user', 'pass'
[225] Fix | Delete
#
[226] Fix | Delete
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
[227] Fix | Delete
# http.request(req)
[228] Fix | Delete
# }
[229] Fix | Delete
# puts res.body
[230] Fix | Delete
#
[231] Fix | Delete
# === Streaming Response Bodies
[232] Fix | Delete
#
[233] Fix | Delete
# By default Net::HTTP reads an entire response into memory. If you are
[234] Fix | Delete
# handling large files or wish to implement a progress bar you can instead
[235] Fix | Delete
# stream the body directly to an IO.
[236] Fix | Delete
#
[237] Fix | Delete
# uri = URI('http://example.com/large_file')
[238] Fix | Delete
#
[239] Fix | Delete
# Net::HTTP.start(uri.host, uri.port) do |http|
[240] Fix | Delete
# request = Net::HTTP::Get.new uri
[241] Fix | Delete
#
[242] Fix | Delete
# http.request request do |response|
[243] Fix | Delete
# open 'large_file', 'w' do |io|
[244] Fix | Delete
# response.read_body do |chunk|
[245] Fix | Delete
# io.write chunk
[246] Fix | Delete
# end
[247] Fix | Delete
# end
[248] Fix | Delete
# end
[249] Fix | Delete
# end
[250] Fix | Delete
#
[251] Fix | Delete
# === HTTPS
[252] Fix | Delete
#
[253] Fix | Delete
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
[254] Fix | Delete
#
[255] Fix | Delete
# uri = URI('https://secure.example.com/some_path?query=string')
[256] Fix | Delete
#
[257] Fix | Delete
# Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
[258] Fix | Delete
# request = Net::HTTP::Get.new uri
[259] Fix | Delete
# response = http.request request # Net::HTTPResponse object
[260] Fix | Delete
# end
[261] Fix | Delete
#
[262] Fix | Delete
# Or if you simply want to make a GET request, you may pass in an URI
[263] Fix | Delete
# object that has an HTTPS URL. Net::HTTP automatically turns on TLS
[264] Fix | Delete
# verification if the URI object has a 'https' URI scheme.
[265] Fix | Delete
#
[266] Fix | Delete
# uri = URI('https://example.com/')
[267] Fix | Delete
# Net::HTTP.get(uri) # => String
[268] Fix | Delete
#
[269] Fix | Delete
# In previous versions of Ruby you would need to require 'net/https' to use
[270] Fix | Delete
# HTTPS. This is no longer true.
[271] Fix | Delete
#
[272] Fix | Delete
# === Proxies
[273] Fix | Delete
#
[274] Fix | Delete
# Net::HTTP will automatically create a proxy from the +http_proxy+
[275] Fix | Delete
# environment variable if it is present. To disable use of +http_proxy+,
[276] Fix | Delete
# pass +nil+ for the proxy address.
[277] Fix | Delete
#
[278] Fix | Delete
# You may also create a custom proxy:
[279] Fix | Delete
#
[280] Fix | Delete
# proxy_addr = 'your.proxy.host'
[281] Fix | Delete
# proxy_port = 8080
[282] Fix | Delete
#
[283] Fix | Delete
# Net::HTTP.new('example.com', nil, proxy_addr, proxy_port).start { |http|
[284] Fix | Delete
# # always proxy via your.proxy.addr:8080
[285] Fix | Delete
# }
[286] Fix | Delete
#
[287] Fix | Delete
# See Net::HTTP.new for further details and examples such as proxies that
[288] Fix | Delete
# require a username and password.
[289] Fix | Delete
#
[290] Fix | Delete
# === Compression
[291] Fix | Delete
#
[292] Fix | Delete
# Net::HTTP automatically adds Accept-Encoding for compression of response
[293] Fix | Delete
# bodies and automatically decompresses gzip and deflate responses unless a
[294] Fix | Delete
# Range header was sent.
[295] Fix | Delete
#
[296] Fix | Delete
# Compression can be disabled through the Accept-Encoding: identity header.
[297] Fix | Delete
#
[298] Fix | Delete
# == HTTP Request Classes
[299] Fix | Delete
#
[300] Fix | Delete
# Here is the HTTP request class hierarchy.
[301] Fix | Delete
#
[302] Fix | Delete
# * Net::HTTPRequest
[303] Fix | Delete
# * Net::HTTP::Get
[304] Fix | Delete
# * Net::HTTP::Head
[305] Fix | Delete
# * Net::HTTP::Post
[306] Fix | Delete
# * Net::HTTP::Patch
[307] Fix | Delete
# * Net::HTTP::Put
[308] Fix | Delete
# * Net::HTTP::Proppatch
[309] Fix | Delete
# * Net::HTTP::Lock
[310] Fix | Delete
# * Net::HTTP::Unlock
[311] Fix | Delete
# * Net::HTTP::Options
[312] Fix | Delete
# * Net::HTTP::Propfind
[313] Fix | Delete
# * Net::HTTP::Delete
[314] Fix | Delete
# * Net::HTTP::Move
[315] Fix | Delete
# * Net::HTTP::Copy
[316] Fix | Delete
# * Net::HTTP::Mkcol
[317] Fix | Delete
# * Net::HTTP::Trace
[318] Fix | Delete
#
[319] Fix | Delete
# == HTTP Response Classes
[320] Fix | Delete
#
[321] Fix | Delete
# Here is HTTP response class hierarchy. All classes are defined in Net
[322] Fix | Delete
# module and are subclasses of Net::HTTPResponse.
[323] Fix | Delete
#
[324] Fix | Delete
# HTTPUnknownResponse:: For unhandled HTTP extensions
[325] Fix | Delete
# HTTPInformation:: 1xx
[326] Fix | Delete
# HTTPContinue:: 100
[327] Fix | Delete
# HTTPSwitchProtocol:: 101
[328] Fix | Delete
# HTTPSuccess:: 2xx
[329] Fix | Delete
# HTTPOK:: 200
[330] Fix | Delete
# HTTPCreated:: 201
[331] Fix | Delete
# HTTPAccepted:: 202
[332] Fix | Delete
# HTTPNonAuthoritativeInformation:: 203
[333] Fix | Delete
# HTTPNoContent:: 204
[334] Fix | Delete
# HTTPResetContent:: 205
[335] Fix | Delete
# HTTPPartialContent:: 206
[336] Fix | Delete
# HTTPMultiStatus:: 207
[337] Fix | Delete
# HTTPIMUsed:: 226
[338] Fix | Delete
# HTTPRedirection:: 3xx
[339] Fix | Delete
# HTTPMultipleChoices:: 300
[340] Fix | Delete
# HTTPMovedPermanently:: 301
[341] Fix | Delete
# HTTPFound:: 302
[342] Fix | Delete
# HTTPSeeOther:: 303
[343] Fix | Delete
# HTTPNotModified:: 304
[344] Fix | Delete
# HTTPUseProxy:: 305
[345] Fix | Delete
# HTTPTemporaryRedirect:: 307
[346] Fix | Delete
# HTTPClientError:: 4xx
[347] Fix | Delete
# HTTPBadRequest:: 400
[348] Fix | Delete
# HTTPUnauthorized:: 401
[349] Fix | Delete
# HTTPPaymentRequired:: 402
[350] Fix | Delete
# HTTPForbidden:: 403
[351] Fix | Delete
# HTTPNotFound:: 404
[352] Fix | Delete
# HTTPMethodNotAllowed:: 405
[353] Fix | Delete
# HTTPNotAcceptable:: 406
[354] Fix | Delete
# HTTPProxyAuthenticationRequired:: 407
[355] Fix | Delete
# HTTPRequestTimeOut:: 408
[356] Fix | Delete
# HTTPConflict:: 409
[357] Fix | Delete
# HTTPGone:: 410
[358] Fix | Delete
# HTTPLengthRequired:: 411
[359] Fix | Delete
# HTTPPreconditionFailed:: 412
[360] Fix | Delete
# HTTPRequestEntityTooLarge:: 413
[361] Fix | Delete
# HTTPRequestURITooLong:: 414
[362] Fix | Delete
# HTTPUnsupportedMediaType:: 415
[363] Fix | Delete
# HTTPRequestedRangeNotSatisfiable:: 416
[364] Fix | Delete
# HTTPExpectationFailed:: 417
[365] Fix | Delete
# HTTPUnprocessableEntity:: 422
[366] Fix | Delete
# HTTPLocked:: 423
[367] Fix | Delete
# HTTPFailedDependency:: 424
[368] Fix | Delete
# HTTPUpgradeRequired:: 426
[369] Fix | Delete
# HTTPPreconditionRequired:: 428
[370] Fix | Delete
# HTTPTooManyRequests:: 429
[371] Fix | Delete
# HTTPRequestHeaderFieldsTooLarge:: 431
[372] Fix | Delete
# HTTPUnavailableForLegalReasons:: 451
[373] Fix | Delete
# HTTPServerError:: 5xx
[374] Fix | Delete
# HTTPInternalServerError:: 500
[375] Fix | Delete
# HTTPNotImplemented:: 501
[376] Fix | Delete
# HTTPBadGateway:: 502
[377] Fix | Delete
# HTTPServiceUnavailable:: 503
[378] Fix | Delete
# HTTPGatewayTimeOut:: 504
[379] Fix | Delete
# HTTPVersionNotSupported:: 505
[380] Fix | Delete
# HTTPInsufficientStorage:: 507
[381] Fix | Delete
# HTTPNetworkAuthenticationRequired:: 511
[382] Fix | Delete
#
[383] Fix | Delete
# There is also the Net::HTTPBadResponse exception which is raised when
[384] Fix | Delete
# there is a protocol error.
[385] Fix | Delete
#
[386] Fix | Delete
class HTTP < Protocol
[387] Fix | Delete
[388] Fix | Delete
# :stopdoc:
[389] Fix | Delete
Revision = %q$Revision$.split[1]
[390] Fix | Delete
HTTPVersion = '1.1'
[391] Fix | Delete
begin
[392] Fix | Delete
require 'zlib'
[393] Fix | Delete
require 'stringio' #for our purposes (unpacking gzip) lump these together
[394] Fix | Delete
HAVE_ZLIB=true
[395] Fix | Delete
rescue LoadError
[396] Fix | Delete
HAVE_ZLIB=false
[397] Fix | Delete
end
[398] Fix | Delete
# :startdoc:
[399] Fix | Delete
[400] Fix | Delete
# Turns on net/http 1.2 (Ruby 1.8) features.
[401] Fix | Delete
# Defaults to ON in Ruby 1.8 or later.
[402] Fix | Delete
def HTTP.version_1_2
[403] Fix | Delete
true
[404] Fix | Delete
end
[405] Fix | Delete
[406] Fix | Delete
# Returns true if net/http is in version 1.2 mode.
[407] Fix | Delete
# Defaults to true.
[408] Fix | Delete
def HTTP.version_1_2?
[409] Fix | Delete
true
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
def HTTP.version_1_1? #:nodoc:
[413] Fix | Delete
false
[414] Fix | Delete
end
[415] Fix | Delete
[416] Fix | Delete
class << HTTP
[417] Fix | Delete
alias is_version_1_1? version_1_1? #:nodoc:
[418] Fix | Delete
alias is_version_1_2? version_1_2? #:nodoc:
[419] Fix | Delete
end
[420] Fix | Delete
[421] Fix | Delete
#
[422] Fix | Delete
# short cut methods
[423] Fix | Delete
#
[424] Fix | Delete
[425] Fix | Delete
#
[426] Fix | Delete
# Gets the body text from the target and outputs it to $stdout. The
[427] Fix | Delete
# target can either be specified as
[428] Fix | Delete
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
[429] Fix | Delete
#
[430] Fix | Delete
# Net::HTTP.get_print URI('http://www.example.com/index.html')
[431] Fix | Delete
#
[432] Fix | Delete
# or:
[433] Fix | Delete
#
[434] Fix | Delete
# Net::HTTP.get_print 'www.example.com', '/index.html'
[435] Fix | Delete
#
[436] Fix | Delete
def HTTP.get_print(uri_or_host, path = nil, port = nil)
[437] Fix | Delete
get_response(uri_or_host, path, port) {|res|
[438] Fix | Delete
res.read_body do |chunk|
[439] Fix | Delete
$stdout.print chunk
[440] Fix | Delete
end
[441] Fix | Delete
}
[442] Fix | Delete
nil
[443] Fix | Delete
end
[444] Fix | Delete
[445] Fix | Delete
# Sends a GET request to the target and returns the HTTP response
[446] Fix | Delete
# as a string. The target can either be specified as
[447] Fix | Delete
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
[448] Fix | Delete
#
[449] Fix | Delete
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
[450] Fix | Delete
#
[451] Fix | Delete
# or:
[452] Fix | Delete
#
[453] Fix | Delete
# print Net::HTTP.get('www.example.com', '/index.html')
[454] Fix | Delete
#
[455] Fix | Delete
def HTTP.get(uri_or_host, path = nil, port = nil)
[456] Fix | Delete
get_response(uri_or_host, path, port).body
[457] Fix | Delete
end
[458] Fix | Delete
[459] Fix | Delete
# Sends a GET request to the target and returns the HTTP response
[460] Fix | Delete
# as a Net::HTTPResponse object. The target can either be specified as
[461] Fix | Delete
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
[462] Fix | Delete
#
[463] Fix | Delete
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
[464] Fix | Delete
# print res.body
[465] Fix | Delete
#
[466] Fix | Delete
# or:
[467] Fix | Delete
#
[468] Fix | Delete
# res = Net::HTTP.get_response('www.example.com', '/index.html')
[469] Fix | Delete
# print res.body
[470] Fix | Delete
#
[471] Fix | Delete
def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
[472] Fix | Delete
if path
[473] Fix | Delete
host = uri_or_host
[474] Fix | Delete
new(host, port || HTTP.default_port).start {|http|
[475] Fix | Delete
return http.request_get(path, &block)
[476] Fix | Delete
}
[477] Fix | Delete
else
[478] Fix | Delete
uri = uri_or_host
[479] Fix | Delete
start(uri.hostname, uri.port,
[480] Fix | Delete
:use_ssl => uri.scheme == 'https') {|http|
[481] Fix | Delete
return http.request_get(uri, &block)
[482] Fix | Delete
}
[483] Fix | Delete
end
[484] Fix | Delete
end
[485] Fix | Delete
[486] Fix | Delete
# Posts data to the specified URI object.
[487] Fix | Delete
#
[488] Fix | Delete
# Example:
[489] Fix | Delete
#
[490] Fix | Delete
# require 'net/http'
[491] Fix | Delete
# require 'uri'
[492] Fix | Delete
#
[493] Fix | Delete
# Net::HTTP.post URI('http://www.example.com/api/search'),
[494] Fix | Delete
# { "q" => "ruby", "max" => "50" }.to_json,
[495] Fix | Delete
# "Content-Type" => "application/json"
[496] Fix | Delete
#
[497] Fix | Delete
def HTTP.post(url, data, header = nil)
[498] Fix | Delete
start(url.hostname, url.port,
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function