net-http 0.3.2 → 0.6.0

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.
data/lib/net/http.rb CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  #
3
3
  # = net/http.rb
4
4
  #
@@ -34,72 +34,72 @@ module Net #:nodoc:
34
34
 
35
35
  # \Class \Net::HTTP provides a rich library that implements the client
36
36
  # in a client-server model that uses the \HTTP request-response protocol.
37
- # For information about \HTTP, see
37
+ # For information about \HTTP, see:
38
38
  #
39
39
  # - {Hypertext Transfer Protocol}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol].
40
40
  # - {Technical overview}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview].
41
41
  #
42
- # Note: If you are performing only a few GET requests, consider using
43
- # {OpenURI}[https://docs.ruby-lang.org/en/master/OpenURI.html];
44
- # otherwise, read on.
45
- #
46
- # == Synopsis
47
- #
48
- # If you are already familiar with \HTTP, this synopsis may be helpful.
49
- #
50
- # {Session}[rdoc-ref:Net::HTTP@Sessions] with multiple requests for
51
- # {HTTP methods}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods]:
52
- #
53
- # Net::HTTP.start(hostname) do |http|
54
- # # Session started automatically before block execution.
55
- # http.get(path_or_uri, headers = {})
56
- # http.head(path_or_uri, headers = {})
57
- # http.post(path_or_uri, data, headers = {}) # Can also have a block.
58
- # http.put(path_or_uri, data, headers = {})
59
- # http.delete(path_or_uri, headers = {Depth: 'Infinity'})
60
- # http.options(path_or_uri, headers = {})
61
- # http.trace(path_or_uri, headers = {})
62
- # http.patch(path_or_uri, data, headers = {}) # Can also have a block.
63
- # # Session finished automatically at block exit.
64
- # end
65
- #
66
- # {Session}[rdoc-ref:Net::HTTP@Sessions] with multiple requests for
67
- # {WebDAV methods}[https://en.wikipedia.org/wiki/WebDAV#Implementation]:
68
- #
69
- # Net::HTTP.start(hostname) do |http|
70
- # # Session started automatically before block execution.
71
- # http.copy(path_or_uri, headers = {})
72
- # http.lock(path_or_uri, body, headers = {})
73
- # http.mkcol(path_or_uri, body = nil, headers = {})
74
- # http.move(path_or_uri, headers = {})
75
- # http.propfind(path_or_uri, body = nil, headers = {'Depth' => '0'})
76
- # http.proppatch(path_or_uri, body, headers = {})
77
- # http.unlock(path_or_uri, body, headers = {})
78
- # # Session finished automatically at block exit.
79
- # end
80
- #
81
- # Each of the following methods automatically starts and finishes
82
- # a {session}[rdoc-ref:Net::HTTP@Sessions] that sends a single request:
83
- #
84
- # # Return string response body.
85
- # Net::HTTP.get(hostname, path, port = 80)
86
- # Net::HTTP.get(uri, headers = {}, port = 80)
87
- #
88
- # # Write string response body to $stdout.
89
- # Net::HTTP.get_print(hostname, path_or_uri, port = 80)
90
- # Net::HTTP.get_print(uri, headers = {}, port = 80)
91
- #
92
- # # Return response as Net::HTTPResponse object.
93
- # Net::HTTP.get_response(hostname, path_or_uri, port = 80)
94
- # Net::HTTP.get_response(uri, headers = {}, port = 80)
95
- #
96
- # Net::HTTP.post(uri, data, headers = {})
97
- # Net::HTTP.post_form(uri, params)
98
- #
99
42
  # == About the Examples
100
43
  #
101
44
  # :include: doc/net-http/examples.rdoc
102
45
  #
46
+ # == Strategies
47
+ #
48
+ # - If you will make only a few GET requests,
49
+ # consider using {OpenURI}[https://docs.ruby-lang.org/en/master/OpenURI.html].
50
+ # - If you will make only a few requests of all kinds,
51
+ # consider using the various singleton convenience methods in this class.
52
+ # Each of the following methods automatically starts and finishes
53
+ # a {session}[rdoc-ref:Net::HTTP@Sessions] that sends a single request:
54
+ #
55
+ # # Return string response body.
56
+ # Net::HTTP.get(hostname, path)
57
+ # Net::HTTP.get(uri)
58
+ #
59
+ # # Write string response body to $stdout.
60
+ # Net::HTTP.get_print(hostname, path)
61
+ # Net::HTTP.get_print(uri)
62
+ #
63
+ # # Return response as Net::HTTPResponse object.
64
+ # Net::HTTP.get_response(hostname, path)
65
+ # Net::HTTP.get_response(uri)
66
+ # data = '{"title": "foo", "body": "bar", "userId": 1}'
67
+ # Net::HTTP.post(uri, data)
68
+ # params = {title: 'foo', body: 'bar', userId: 1}
69
+ # Net::HTTP.post_form(uri, params)
70
+ # data = '{"title": "foo", "body": "bar", "userId": 1}'
71
+ # Net::HTTP.put(uri, data)
72
+ #
73
+ # - If performance is important, consider using sessions, which lower request overhead.
74
+ # This {session}[rdoc-ref:Net::HTTP@Sessions] has multiple requests for
75
+ # {HTTP methods}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods]
76
+ # and {WebDAV methods}[https://en.wikipedia.org/wiki/WebDAV#Implementation]:
77
+ #
78
+ # Net::HTTP.start(hostname) do |http|
79
+ # # Session started automatically before block execution.
80
+ # http.get(path)
81
+ # http.head(path)
82
+ # body = 'Some text'
83
+ # http.post(path, body) # Can also have a block.
84
+ # http.put(path, body)
85
+ # http.delete(path)
86
+ # http.options(path)
87
+ # http.trace(path)
88
+ # http.patch(path, body) # Can also have a block.
89
+ # http.copy(path)
90
+ # http.lock(path, body)
91
+ # http.mkcol(path, body)
92
+ # http.move(path)
93
+ # http.propfind(path, body)
94
+ # http.proppatch(path, body)
95
+ # http.unlock(path, body)
96
+ # # Session finished automatically at block exit.
97
+ # end
98
+ #
99
+ # The methods cited above are convenience methods that, via their few arguments,
100
+ # allow minimal control over the requests.
101
+ # For greater control, consider using {request objects}[rdoc-ref:Net::HTTPRequest].
102
+ #
103
103
  # == URIs
104
104
  #
105
105
  # On the internet, a URI
@@ -175,7 +175,7 @@ module Net #:nodoc:
175
175
  # {Request Fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
176
176
  # A host may also accept other custom fields.
177
177
  #
178
- # == Sessions
178
+ # == \HTTP Sessions
179
179
  #
180
180
  # A _session_ is a connection between a server (host) and a client that:
181
181
  #
@@ -183,7 +183,7 @@ module Net #:nodoc:
183
183
  # - May contain any number of requests.
184
184
  # - Is ended by instance method Net::HTTP#finish.
185
185
  #
186
- # See example sessions at the {Synopsis}[rdoc-ref:Net::HTTP@Synopsis].
186
+ # See example sessions at {Strategies}[rdoc-ref:Net::HTTP@Strategies].
187
187
  #
188
188
  # === Session Using \Net::HTTP.start
189
189
  #
@@ -264,66 +264,55 @@ module Net #:nodoc:
264
264
  #
265
265
  # == Following Redirection
266
266
  #
267
- # Each Net::HTTPResponse object belongs to a class for its response code.
268
- #
269
- # For example, all 2XX responses are instances of a Net::HTTPSuccess
270
- # subclass, a 3XX response is an instance of a Net::HTTPRedirection
271
- # subclass and a 200 response is an instance of the Net::HTTPOK class. For
272
- # details of response classes, see the section "HTTP Response Classes"
273
- # below.
267
+ # Each returned response is an instance of a subclass of Net::HTTPResponse.
268
+ # See the {response class hierarchy}[rdoc-ref:Net::HTTPResponse@Response+Subclasses].
274
269
  #
275
- # Using a case statement you can handle various types of responses properly:
270
+ # In particular, class Net::HTTPRedirection is the parent
271
+ # of all redirection classes.
272
+ # This allows you to craft a case statement to handle redirections properly:
276
273
  #
277
- # def fetch(uri_str, limit = 10)
274
+ # def fetch(uri, limit = 10)
278
275
  # # You should choose a better exception.
279
- # raise ArgumentError, 'too many HTTP redirects' if limit == 0
280
- #
281
- # response = Net::HTTP.get_response(URI(uri_str))
282
- #
283
- # case response
284
- # when Net::HTTPSuccess then
285
- # response
286
- # when Net::HTTPRedirection then
287
- # location = response['location']
288
- # warn "redirected to #{location}"
276
+ # raise ArgumentError, 'Too many HTTP redirects' if limit == 0
277
+ #
278
+ # res = Net::HTTP.get_response(URI(uri))
279
+ # case res
280
+ # when Net::HTTPSuccess # Any success class.
281
+ # res
282
+ # when Net::HTTPRedirection # Any redirection class.
283
+ # location = res['Location']
284
+ # warn "Redirected to #{location}"
289
285
  # fetch(location, limit - 1)
290
- # else
291
- # response.value
286
+ # else # Any other class.
287
+ # res.value
292
288
  # end
293
289
  # end
294
290
  #
295
- # print fetch('http://www.ruby-lang.org')
291
+ # fetch(uri)
296
292
  #
297
293
  # == Basic Authentication
298
294
  #
299
295
  # Basic authentication is performed according to
300
- # [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
301
- #
302
- # uri = URI('http://example.com/index.html?key=value')
296
+ # {RFC2617}[http://www.ietf.org/rfc/rfc2617.txt]:
303
297
  #
304
298
  # req = Net::HTTP::Get.new(uri)
305
- # req.basic_auth 'user', 'pass'
306
- #
307
- # res = Net::HTTP.start(uri.hostname, uri.port) {|http|
299
+ # req.basic_auth('user', 'pass')
300
+ # res = Net::HTTP.start(hostname) do |http|
308
301
  # http.request(req)
309
- # }
310
- # puts res.body
302
+ # end
311
303
  #
312
304
  # == Streaming Response Bodies
313
305
  #
314
- # By default Net::HTTP reads an entire response into memory. If you are
306
+ # By default \Net::HTTP reads an entire response into memory. If you are
315
307
  # handling large files or wish to implement a progress bar you can instead
316
308
  # stream the body directly to an IO.
317
309
  #
318
- # uri = URI('http://example.com/large_file')
319
- #
320
- # Net::HTTP.start(uri.host, uri.port) do |http|
321
- # request = Net::HTTP::Get.new uri
322
- #
323
- # http.request request do |response|
324
- # open 'large_file', 'w' do |io|
325
- # response.read_body do |chunk|
326
- # io.write chunk
310
+ # Net::HTTP.start(hostname) do |http|
311
+ # req = Net::HTTP::Get.new(uri)
312
+ # http.request(req) do |res|
313
+ # open('t.tmp', 'w') do |f|
314
+ # res.read_body do |chunk|
315
+ # f.write chunk
327
316
  # end
328
317
  # end
329
318
  # end
@@ -331,56 +320,417 @@ module Net #:nodoc:
331
320
  #
332
321
  # == HTTPS
333
322
  #
334
- # HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
323
+ # HTTPS is enabled for an \HTTP connection by Net::HTTP#use_ssl=:
335
324
  #
336
- # uri = URI('https://secure.example.com/some_path?query=string')
337
- #
338
- # Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
339
- # request = Net::HTTP::Get.new uri
340
- # response = http.request request # Net::HTTPResponse object
325
+ # Net::HTTP.start(hostname, :use_ssl => true) do |http|
326
+ # req = Net::HTTP::Get.new(uri)
327
+ # res = http.request(req)
341
328
  # end
342
329
  #
343
- # Or if you simply want to make a GET request, you may pass in an URI
344
- # object that has an HTTPS URL. Net::HTTP automatically turns on TLS
345
- # verification if the URI object has a 'https' URI scheme.
330
+ # Or if you simply want to make a GET request, you may pass in a URI
331
+ # object that has an \HTTPS URL. \Net::HTTP automatically turns on TLS
332
+ # verification if the URI object has a 'https' URI scheme:
333
+ #
334
+ # uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
335
+ # Net::HTTP.get(uri)
346
336
  #
347
- # uri = URI('https://example.com/')
348
- # Net::HTTP.get(uri) # => String
337
+ # == Proxy Server
349
338
  #
350
- # In previous versions of Ruby you would need to require 'net/https' to use
351
- # HTTPS. This is no longer true.
339
+ # An \HTTP object can have
340
+ # a {proxy server}[https://en.wikipedia.org/wiki/Proxy_server].
352
341
  #
353
- # == Proxies
342
+ # You can create an \HTTP object with a proxy server
343
+ # using method Net::HTTP.new or method Net::HTTP.start.
354
344
  #
355
- # Net::HTTP will automatically create a proxy from the +http_proxy+
356
- # environment variable if it is present. To disable use of +http_proxy+,
357
- # pass +nil+ for the proxy address.
345
+ # The proxy may be defined either by argument +p_addr+
346
+ # or by environment variable <tt>'http_proxy'</tt>.
358
347
  #
359
- # You may also create a custom proxy:
348
+ # === Proxy Using Argument +p_addr+ as a \String
360
349
  #
361
- # proxy_addr = 'your.proxy.host'
362
- # proxy_port = 8080
350
+ # When argument +p_addr+ is a string hostname,
351
+ # the returned +http+ has the given host as its proxy:
363
352
  #
364
- # Net::HTTP.new('example.com', nil, proxy_addr, proxy_port).start { |http|
365
- # # always proxy via your.proxy.addr:8080
366
- # }
353
+ # http = Net::HTTP.new(hostname, nil, 'proxy.example')
354
+ # http.proxy? # => true
355
+ # http.proxy_from_env? # => false
356
+ # http.proxy_address # => "proxy.example"
357
+ # # These use default values.
358
+ # http.proxy_port # => 80
359
+ # http.proxy_user # => nil
360
+ # http.proxy_pass # => nil
367
361
  #
368
- # See Net::HTTP.new for further details and examples such as proxies that
369
- # require a username and password.
362
+ # The port, username, and password for the proxy may also be given:
370
363
  #
371
- # == Compression
364
+ # http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass')
365
+ # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
366
+ # http.proxy? # => true
367
+ # http.proxy_from_env? # => false
368
+ # http.proxy_address # => "proxy.example"
369
+ # http.proxy_port # => 8000
370
+ # http.proxy_user # => "pname"
371
+ # http.proxy_pass # => "ppass"
372
372
  #
373
- # Net::HTTP automatically adds Accept-Encoding for compression of response
374
- # bodies and automatically decompresses gzip and deflate responses unless a
375
- # Range header was sent.
373
+ # === Proxy Using '<tt>ENV['http_proxy']</tt>'
376
374
  #
377
- # Compression can be disabled through the Accept-Encoding: identity header.
375
+ # When environment variable <tt>'http_proxy'</tt>
376
+ # is set to a \URI string,
377
+ # the returned +http+ will have the server at that URI as its proxy;
378
+ # note that the \URI string must have a protocol
379
+ # such as <tt>'http'</tt> or <tt>'https'</tt>:
380
+ #
381
+ # ENV['http_proxy'] = 'http://example.com'
382
+ # http = Net::HTTP.new(hostname)
383
+ # http.proxy? # => true
384
+ # http.proxy_from_env? # => true
385
+ # http.proxy_address # => "example.com"
386
+ # # These use default values.
387
+ # http.proxy_port # => 80
388
+ # http.proxy_user # => nil
389
+ # http.proxy_pass # => nil
390
+ #
391
+ # The \URI string may include proxy username, password, and port number:
392
+ #
393
+ # ENV['http_proxy'] = 'http://pname:ppass@example.com:8000'
394
+ # http = Net::HTTP.new(hostname)
395
+ # http.proxy? # => true
396
+ # http.proxy_from_env? # => true
397
+ # http.proxy_address # => "example.com"
398
+ # http.proxy_port # => 8000
399
+ # http.proxy_user # => "pname"
400
+ # http.proxy_pass # => "ppass"
401
+ #
402
+ # === Filtering Proxies
403
+ #
404
+ # With method Net::HTTP.new (but not Net::HTTP.start),
405
+ # you can use argument +p_no_proxy+ to filter proxies:
406
+ #
407
+ # - Reject a certain address:
408
+ #
409
+ # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
410
+ # http.proxy_address # => nil
411
+ #
412
+ # - Reject certain domains or subdomains:
413
+ #
414
+ # http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
415
+ # http.proxy_address # => nil
416
+ #
417
+ # - Reject certain addresses and port combinations:
418
+ #
419
+ # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234')
420
+ # http.proxy_address # => "proxy.example"
421
+ #
422
+ # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000')
423
+ # http.proxy_address # => nil
424
+ #
425
+ # - Reject a list of the types above delimited using a comma:
426
+ #
427
+ # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
428
+ # http.proxy_address # => nil
429
+ #
430
+ # http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
431
+ # http.proxy_address # => nil
432
+ #
433
+ # == Compression and Decompression
434
+ #
435
+ # \Net::HTTP does not compress the body of a request before sending.
436
+ #
437
+ # By default, \Net::HTTP adds header <tt>'Accept-Encoding'</tt>
438
+ # to a new {request object}[rdoc-ref:Net::HTTPRequest]:
439
+ #
440
+ # Net::HTTP::Get.new(uri)['Accept-Encoding']
441
+ # # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
442
+ #
443
+ # This requests the server to zip-encode the response body if there is one;
444
+ # the server is not required to do so.
445
+ #
446
+ # \Net::HTTP does not automatically decompress a response body
447
+ # if the response has header <tt>'Content-Range'</tt>.
448
+ #
449
+ # Otherwise decompression (or not) depends on the value of header
450
+ # {Content-Encoding}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-encoding-response-header]:
451
+ #
452
+ # - <tt>'deflate'</tt>, <tt>'gzip'</tt>, or <tt>'x-gzip'</tt>:
453
+ # decompresses the body and deletes the header.
454
+ # - <tt>'none'</tt> or <tt>'identity'</tt>:
455
+ # does not decompress the body, but deletes the header.
456
+ # - Any other value:
457
+ # leaves the body and header unchanged.
458
+ #
459
+ # == What's Here
460
+ #
461
+ # First, what's elsewhere. Class Net::HTTP:
462
+ #
463
+ # - Inherits from {class Object}[https://docs.ruby-lang.org/en/master/Object.html#class-Object-label-What-27s+Here].
464
+ #
465
+ # This is a categorized summary of methods and attributes.
466
+ #
467
+ # === \Net::HTTP Objects
468
+ #
469
+ # - {::new}[rdoc-ref:Net::HTTP.new]:
470
+ # Creates a new instance.
471
+ # - {#inspect}[rdoc-ref:Net::HTTP#inspect]:
472
+ # Returns a string representation of +self+.
473
+ #
474
+ # === Sessions
475
+ #
476
+ # - {::start}[rdoc-ref:Net::HTTP.start]:
477
+ # Begins a new session in a new \Net::HTTP object.
478
+ # - {#started?}[rdoc-ref:Net::HTTP#started?]
479
+ # (aliased as {#active?}[rdoc-ref:Net::HTTP#active?]):
480
+ # Returns whether in a session.
481
+ # - {#finish}[rdoc-ref:Net::HTTP#finish]:
482
+ # Ends an active session.
483
+ # - {#start}[rdoc-ref:Net::HTTP#start]:
484
+ # Begins a new session in an existing \Net::HTTP object (+self+).
485
+ #
486
+ # === Connections
487
+ #
488
+ # - {:continue_timeout}[rdoc-ref:Net::HTTP#continue_timeout]:
489
+ # Returns the continue timeout.
490
+ # - {#continue_timeout=}[rdoc-ref:Net::HTTP#continue_timeout=]:
491
+ # Sets the continue timeout seconds.
492
+ # - {:keep_alive_timeout}[rdoc-ref:Net::HTTP#keep_alive_timeout]:
493
+ # Returns the keep-alive timeout.
494
+ # - {:keep_alive_timeout=}[rdoc-ref:Net::HTTP#keep_alive_timeout=]:
495
+ # Sets the keep-alive timeout.
496
+ # - {:max_retries}[rdoc-ref:Net::HTTP#max_retries]:
497
+ # Returns the maximum retries.
498
+ # - {#max_retries=}[rdoc-ref:Net::HTTP#max_retries=]:
499
+ # Sets the maximum retries.
500
+ # - {:open_timeout}[rdoc-ref:Net::HTTP#open_timeout]:
501
+ # Returns the open timeout.
502
+ # - {:open_timeout=}[rdoc-ref:Net::HTTP#open_timeout=]:
503
+ # Sets the open timeout.
504
+ # - {:read_timeout}[rdoc-ref:Net::HTTP#read_timeout]:
505
+ # Returns the open timeout.
506
+ # - {:read_timeout=}[rdoc-ref:Net::HTTP#read_timeout=]:
507
+ # Sets the read timeout.
508
+ # - {:ssl_timeout}[rdoc-ref:Net::HTTP#ssl_timeout]:
509
+ # Returns the ssl timeout.
510
+ # - {:ssl_timeout=}[rdoc-ref:Net::HTTP#ssl_timeout=]:
511
+ # Sets the ssl timeout.
512
+ # - {:write_timeout}[rdoc-ref:Net::HTTP#write_timeout]:
513
+ # Returns the write timeout.
514
+ # - {write_timeout=}[rdoc-ref:Net::HTTP#write_timeout=]:
515
+ # Sets the write timeout.
516
+ #
517
+ # === Requests
518
+ #
519
+ # - {::get}[rdoc-ref:Net::HTTP.get]:
520
+ # Sends a GET request and returns the string response body.
521
+ # - {::get_print}[rdoc-ref:Net::HTTP.get_print]:
522
+ # Sends a GET request and write the string response body to $stdout.
523
+ # - {::get_response}[rdoc-ref:Net::HTTP.get_response]:
524
+ # Sends a GET request and returns a response object.
525
+ # - {::post_form}[rdoc-ref:Net::HTTP.post_form]:
526
+ # Sends a POST request with form data and returns a response object.
527
+ # - {::post}[rdoc-ref:Net::HTTP.post]:
528
+ # Sends a POST request with data and returns a response object.
529
+ # - {::put}[rdoc-ref:Net::HTTP.put]:
530
+ # Sends a PUT request with data and returns a response object.
531
+ # - {#copy}[rdoc-ref:Net::HTTP#copy]:
532
+ # Sends a COPY request and returns a response object.
533
+ # - {#delete}[rdoc-ref:Net::HTTP#delete]:
534
+ # Sends a DELETE request and returns a response object.
535
+ # - {#get}[rdoc-ref:Net::HTTP#get]:
536
+ # Sends a GET request and returns a response object.
537
+ # - {#head}[rdoc-ref:Net::HTTP#head]:
538
+ # Sends a HEAD request and returns a response object.
539
+ # - {#lock}[rdoc-ref:Net::HTTP#lock]:
540
+ # Sends a LOCK request and returns a response object.
541
+ # - {#mkcol}[rdoc-ref:Net::HTTP#mkcol]:
542
+ # Sends a MKCOL request and returns a response object.
543
+ # - {#move}[rdoc-ref:Net::HTTP#move]:
544
+ # Sends a MOVE request and returns a response object.
545
+ # - {#options}[rdoc-ref:Net::HTTP#options]:
546
+ # Sends a OPTIONS request and returns a response object.
547
+ # - {#patch}[rdoc-ref:Net::HTTP#patch]:
548
+ # Sends a PATCH request and returns a response object.
549
+ # - {#post}[rdoc-ref:Net::HTTP#post]:
550
+ # Sends a POST request and returns a response object.
551
+ # - {#propfind}[rdoc-ref:Net::HTTP#propfind]:
552
+ # Sends a PROPFIND request and returns a response object.
553
+ # - {#proppatch}[rdoc-ref:Net::HTTP#proppatch]:
554
+ # Sends a PROPPATCH request and returns a response object.
555
+ # - {#put}[rdoc-ref:Net::HTTP#put]:
556
+ # Sends a PUT request and returns a response object.
557
+ # - {#request}[rdoc-ref:Net::HTTP#request]:
558
+ # Sends a request and returns a response object.
559
+ # - {#request_get}[rdoc-ref:Net::HTTP#request_get]
560
+ # (aliased as {#get2}[rdoc-ref:Net::HTTP#get2]):
561
+ # Sends a GET request and forms a response object;
562
+ # if a block given, calls the block with the object,
563
+ # otherwise returns the object.
564
+ # - {#request_head}[rdoc-ref:Net::HTTP#request_head]
565
+ # (aliased as {#head2}[rdoc-ref:Net::HTTP#head2]):
566
+ # Sends a HEAD request and forms a response object;
567
+ # if a block given, calls the block with the object,
568
+ # otherwise returns the object.
569
+ # - {#request_post}[rdoc-ref:Net::HTTP#request_post]
570
+ # (aliased as {#post2}[rdoc-ref:Net::HTTP#post2]):
571
+ # Sends a POST request and forms a response object;
572
+ # if a block given, calls the block with the object,
573
+ # otherwise returns the object.
574
+ # - {#send_request}[rdoc-ref:Net::HTTP#send_request]:
575
+ # Sends a request and returns a response object.
576
+ # - {#trace}[rdoc-ref:Net::HTTP#trace]:
577
+ # Sends a TRACE request and returns a response object.
578
+ # - {#unlock}[rdoc-ref:Net::HTTP#unlock]:
579
+ # Sends an UNLOCK request and returns a response object.
580
+ #
581
+ # === Responses
582
+ #
583
+ # - {:close_on_empty_response}[rdoc-ref:Net::HTTP#close_on_empty_response]:
584
+ # Returns whether to close connection on empty response.
585
+ # - {:close_on_empty_response=}[rdoc-ref:Net::HTTP#close_on_empty_response=]:
586
+ # Sets whether to close connection on empty response.
587
+ # - {:ignore_eof}[rdoc-ref:Net::HTTP#ignore_eof]:
588
+ # Returns whether to ignore end-of-file when reading a response body
589
+ # with <tt>Content-Length</tt> headers.
590
+ # - {:ignore_eof=}[rdoc-ref:Net::HTTP#ignore_eof=]:
591
+ # Sets whether to ignore end-of-file when reading a response body
592
+ # with <tt>Content-Length</tt> headers.
593
+ # - {:response_body_encoding}[rdoc-ref:Net::HTTP#response_body_encoding]:
594
+ # Returns the encoding to use for the response body.
595
+ # - {#response_body_encoding=}[rdoc-ref:Net::HTTP#response_body_encoding=]:
596
+ # Sets the response body encoding.
597
+ #
598
+ # === Proxies
599
+ #
600
+ # - {:proxy_address}[rdoc-ref:Net::HTTP#proxy_address]:
601
+ # Returns the proxy address.
602
+ # - {:proxy_address=}[rdoc-ref:Net::HTTP#proxy_address=]:
603
+ # Sets the proxy address.
604
+ # - {::proxy_class?}[rdoc-ref:Net::HTTP.proxy_class?]:
605
+ # Returns whether +self+ is a proxy class.
606
+ # - {#proxy?}[rdoc-ref:Net::HTTP#proxy?]:
607
+ # Returns whether +self+ has a proxy.
608
+ # - {#proxy_address}[rdoc-ref:Net::HTTP#proxy_address]
609
+ # (aliased as {#proxyaddr}[rdoc-ref:Net::HTTP#proxyaddr]):
610
+ # Returns the proxy address.
611
+ # - {#proxy_from_env?}[rdoc-ref:Net::HTTP#proxy_from_env?]:
612
+ # Returns whether the proxy is taken from an environment variable.
613
+ # - {:proxy_from_env=}[rdoc-ref:Net::HTTP#proxy_from_env=]:
614
+ # Sets whether the proxy is to be taken from an environment variable.
615
+ # - {:proxy_pass}[rdoc-ref:Net::HTTP#proxy_pass]:
616
+ # Returns the proxy password.
617
+ # - {:proxy_pass=}[rdoc-ref:Net::HTTP#proxy_pass=]:
618
+ # Sets the proxy password.
619
+ # - {:proxy_port}[rdoc-ref:Net::HTTP#proxy_port]:
620
+ # Returns the proxy port.
621
+ # - {:proxy_port=}[rdoc-ref:Net::HTTP#proxy_port=]:
622
+ # Sets the proxy port.
623
+ # - {#proxy_user}[rdoc-ref:Net::HTTP#proxy_user]:
624
+ # Returns the proxy user name.
625
+ # - {:proxy_user=}[rdoc-ref:Net::HTTP#proxy_user=]:
626
+ # Sets the proxy user.
627
+ #
628
+ # === Security
629
+ #
630
+ # - {:ca_file}[rdoc-ref:Net::HTTP#ca_file]:
631
+ # Returns the path to a CA certification file.
632
+ # - {:ca_file=}[rdoc-ref:Net::HTTP#ca_file=]:
633
+ # Sets the path to a CA certification file.
634
+ # - {:ca_path}[rdoc-ref:Net::HTTP#ca_path]:
635
+ # Returns the path of to CA directory containing certification files.
636
+ # - {:ca_path=}[rdoc-ref:Net::HTTP#ca_path=]:
637
+ # Sets the path of to CA directory containing certification files.
638
+ # - {:cert}[rdoc-ref:Net::HTTP#cert]:
639
+ # Returns the OpenSSL::X509::Certificate object to be used for client certification.
640
+ # - {:cert=}[rdoc-ref:Net::HTTP#cert=]:
641
+ # Sets the OpenSSL::X509::Certificate object to be used for client certification.
642
+ # - {:cert_store}[rdoc-ref:Net::HTTP#cert_store]:
643
+ # Returns the X509::Store to be used for verifying peer certificate.
644
+ # - {:cert_store=}[rdoc-ref:Net::HTTP#cert_store=]:
645
+ # Sets the X509::Store to be used for verifying peer certificate.
646
+ # - {:ciphers}[rdoc-ref:Net::HTTP#ciphers]:
647
+ # Returns the available SSL ciphers.
648
+ # - {:ciphers=}[rdoc-ref:Net::HTTP#ciphers=]:
649
+ # Sets the available SSL ciphers.
650
+ # - {:extra_chain_cert}[rdoc-ref:Net::HTTP#extra_chain_cert]:
651
+ # Returns the extra X509 certificates to be added to the certificate chain.
652
+ # - {:extra_chain_cert=}[rdoc-ref:Net::HTTP#extra_chain_cert=]:
653
+ # Sets the extra X509 certificates to be added to the certificate chain.
654
+ # - {:key}[rdoc-ref:Net::HTTP#key]:
655
+ # Returns the OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
656
+ # - {:key=}[rdoc-ref:Net::HTTP#key=]:
657
+ # Sets the OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
658
+ # - {:max_version}[rdoc-ref:Net::HTTP#max_version]:
659
+ # Returns the maximum SSL version.
660
+ # - {:max_version=}[rdoc-ref:Net::HTTP#max_version=]:
661
+ # Sets the maximum SSL version.
662
+ # - {:min_version}[rdoc-ref:Net::HTTP#min_version]:
663
+ # Returns the minimum SSL version.
664
+ # - {:min_version=}[rdoc-ref:Net::HTTP#min_version=]:
665
+ # Sets the minimum SSL version.
666
+ # - {#peer_cert}[rdoc-ref:Net::HTTP#peer_cert]:
667
+ # Returns the X509 certificate chain for the session's socket peer.
668
+ # - {:ssl_version}[rdoc-ref:Net::HTTP#ssl_version]:
669
+ # Returns the SSL version.
670
+ # - {:ssl_version=}[rdoc-ref:Net::HTTP#ssl_version=]:
671
+ # Sets the SSL version.
672
+ # - {#use_ssl=}[rdoc-ref:Net::HTTP#use_ssl=]:
673
+ # Sets whether a new session is to use Transport Layer Security.
674
+ # - {#use_ssl?}[rdoc-ref:Net::HTTP#use_ssl?]:
675
+ # Returns whether +self+ uses SSL.
676
+ # - {:verify_callback}[rdoc-ref:Net::HTTP#verify_callback]:
677
+ # Returns the callback for the server certification verification.
678
+ # - {:verify_callback=}[rdoc-ref:Net::HTTP#verify_callback=]:
679
+ # Sets the callback for the server certification verification.
680
+ # - {:verify_depth}[rdoc-ref:Net::HTTP#verify_depth]:
681
+ # Returns the maximum depth for the certificate chain verification.
682
+ # - {:verify_depth=}[rdoc-ref:Net::HTTP#verify_depth=]:
683
+ # Sets the maximum depth for the certificate chain verification.
684
+ # - {:verify_hostname}[rdoc-ref:Net::HTTP#verify_hostname]:
685
+ # Returns the flags for server the certification verification at the beginning of the SSL/TLS session.
686
+ # - {:verify_hostname=}[rdoc-ref:Net::HTTP#verify_hostname=]:
687
+ # Sets he flags for server the certification verification at the beginning of the SSL/TLS session.
688
+ # - {:verify_mode}[rdoc-ref:Net::HTTP#verify_mode]:
689
+ # Returns the flags for server the certification verification at the beginning of the SSL/TLS session.
690
+ # - {:verify_mode=}[rdoc-ref:Net::HTTP#verify_mode=]:
691
+ # Sets the flags for server the certification verification at the beginning of the SSL/TLS session.
692
+ #
693
+ # === Addresses and Ports
694
+ #
695
+ # - {:address}[rdoc-ref:Net::HTTP#address]:
696
+ # Returns the string host name or host IP.
697
+ # - {::default_port}[rdoc-ref:Net::HTTP.default_port]:
698
+ # Returns integer 80, the default port to use for HTTP requests.
699
+ # - {::http_default_port}[rdoc-ref:Net::HTTP.http_default_port]:
700
+ # Returns integer 80, the default port to use for HTTP requests.
701
+ # - {::https_default_port}[rdoc-ref:Net::HTTP.https_default_port]:
702
+ # Returns integer 443, the default port to use for HTTPS requests.
703
+ # - {#ipaddr}[rdoc-ref:Net::HTTP#ipaddr]:
704
+ # Returns the IP address for the connection.
705
+ # - {#ipaddr=}[rdoc-ref:Net::HTTP#ipaddr=]:
706
+ # Sets the IP address for the connection.
707
+ # - {:local_host}[rdoc-ref:Net::HTTP#local_host]:
708
+ # Returns the string local host used to establish the connection.
709
+ # - {:local_host=}[rdoc-ref:Net::HTTP#local_host=]:
710
+ # Sets the string local host used to establish the connection.
711
+ # - {:local_port}[rdoc-ref:Net::HTTP#local_port]:
712
+ # Returns the integer local port used to establish the connection.
713
+ # - {:local_port=}[rdoc-ref:Net::HTTP#local_port=]:
714
+ # Sets the integer local port used to establish the connection.
715
+ # - {:port}[rdoc-ref:Net::HTTP#port]:
716
+ # Returns the integer port number.
717
+ #
718
+ # === \HTTP Version
719
+ #
720
+ # - {::version_1_2?}[rdoc-ref:Net::HTTP.version_1_2?]
721
+ # (aliased as {::is_version_1_2?}[rdoc-ref:Net::HTTP.is_version_1_2?]
722
+ # and {::version_1_2}[rdoc-ref:Net::HTTP.version_1_2]):
723
+ # Returns true; retained for compatibility.
724
+ #
725
+ # === Debugging
726
+ #
727
+ # - {#set_debug_output}[rdoc-ref:Net::HTTP#set_debug_output]:
728
+ # Sets the output stream for debugging.
378
729
  #
379
730
  class HTTP < Protocol
380
731
 
381
732
  # :stopdoc:
382
- VERSION = "0.3.2"
383
- Revision = %q$Revision$.split[1]
733
+ VERSION = "0.6.0"
384
734
  HTTPVersion = '1.1'
385
735
  begin
386
736
  require 'zlib'
@@ -547,11 +897,44 @@ module Net #:nodoc:
547
897
  }
548
898
  end
549
899
 
900
+ # Sends a PUT request to the server; returns a Net::HTTPResponse object.
901
+ #
902
+ # Argument +url+ must be a URL;
903
+ # argument +data+ must be a string:
550
904
  #
551
- # HTTP session management
905
+ # _uri = uri.dup
906
+ # _uri.path = '/posts'
907
+ # data = '{"title": "foo", "body": "bar", "userId": 1}'
908
+ # headers = {'content-type': 'application/json'}
909
+ # res = Net::HTTP.put(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
910
+ # puts res.body
552
911
  #
912
+ # Output:
913
+ #
914
+ # {
915
+ # "title": "foo",
916
+ # "body": "bar",
917
+ # "userId": 1,
918
+ # "id": 101
919
+ # }
920
+ #
921
+ # Related:
922
+ #
923
+ # - Net::HTTP::Put: request class for \HTTP method +PUT+.
924
+ # - Net::HTTP#put: convenience method for \HTTP method +PUT+.
925
+ #
926
+ def HTTP.put(url, data, header = nil)
927
+ start(url.hostname, url.port,
928
+ :use_ssl => url.scheme == 'https' ) {|http|
929
+ http.put(url, data, header)
930
+ }
931
+ end
553
932
 
554
- # Returns intger +80+, the default port to use for HTTP requests:
933
+ #
934
+ # \HTTP session management
935
+ #
936
+
937
+ # Returns integer +80+, the default port to use for \HTTP requests:
555
938
  #
556
939
  # Net::HTTP.default_port # => 80
557
940
  #
@@ -559,7 +942,7 @@ module Net #:nodoc:
559
942
  http_default_port()
560
943
  end
561
944
 
562
- # Returns integer +80+, the default port to use for HTTP requests:
945
+ # Returns integer +80+, the default port to use for \HTTP requests:
563
946
  #
564
947
  # Net::HTTP.http_default_port # => 80
565
948
  #
@@ -585,14 +968,11 @@ module Net #:nodoc:
585
968
  #
586
969
  # Creates a new \Net::HTTP object, +http+, via \Net::HTTP.new:
587
970
  #
588
- # Net::HTTP.new(address, port, p_addr, p_port, p_user, p_pass)
589
- #
590
- # - For arguments +hostname+ through +p_pass+, see Net::HTTP.new.
971
+ # - For arguments +address+ and +port+, see Net::HTTP.new.
972
+ # - For proxy-defining arguments +p_addr+ through +p_pass+,
973
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
591
974
  # - For argument +opts+, see below.
592
975
  #
593
- # Note: If +port+ is +nil+ and <tt>opts[:use_ssl]</tt> is a truthy value,
594
- # the value passed to +new+ is Net::HTTP.https_default_port, not +port+.
595
- #
596
976
  # With no block given:
597
977
  #
598
978
  # - Calls <tt>http.start</tt> with no block (see #start),
@@ -665,6 +1045,9 @@ module Net #:nodoc:
665
1045
  # - #verify_mode
666
1046
  # - #write_timeout
667
1047
  #
1048
+ # Note: If +port+ is +nil+ and <tt>opts[:use_ssl]</tt> is a truthy value,
1049
+ # the value passed to +new+ is Net::HTTP.https_default_port, not +port+.
1050
+ #
668
1051
  def HTTP.start(address, *arg, &block) # :yield: +http+
669
1052
  arg.pop if opt = Hash.try_convert(arg[-1])
670
1053
  port, p_addr, p_port, p_user, p_pass = *arg
@@ -691,12 +1074,10 @@ module Net #:nodoc:
691
1074
  alias newobj new # :nodoc:
692
1075
  end
693
1076
 
694
- # Returns a new Net::HTTP object +http+
695
- # (but does not open a TCP connection or HTTP session).
696
- #
697
- # <b>No Proxy</b>
1077
+ # Returns a new \Net::HTTP object +http+
1078
+ # (but does not open a TCP connection or \HTTP session).
698
1079
  #
699
- # With only string argument +hostname+ given
1080
+ # With only string argument +address+ given
700
1081
  # (and <tt>ENV['http_proxy']</tt> undefined or +nil+),
701
1082
  # the returned +http+:
702
1083
  #
@@ -719,87 +1100,10 @@ module Net #:nodoc:
719
1100
  # # => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
720
1101
  # http.port # => 8000
721
1102
  #
722
- # <b>Proxy Using Argument +p_addr+ as a \String</b>
723
- #
724
- # When argument +p_addr+ is a string hostname,
725
- # the returned +http+ has a proxy:
726
- #
727
- # http = Net::HTTP.new(hostname, nil, 'proxy.example')
728
- # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
729
- # http.proxy? # => true
730
- # http.proxy_address # => "proxy.example"
731
- # # These use default values.
732
- # http.proxy_port # => 80
733
- # http.proxy_user # => nil
734
- # http.proxy_pass # => nil
735
- #
736
- # The port, username, and password for the proxy may also be given:
737
- #
738
- # http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass')
739
- # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
740
- # http.proxy? # => true
741
- # http.proxy_address # => "proxy.example"
742
- # http.proxy_port # => 8000
743
- # http.proxy_user # => "pname"
744
- # http.proxy_pass # => "ppass"
745
- #
746
- # <b>Proxy Using <tt>ENV['http_proxy']</tt></b>
747
- #
748
- # When environment variable <tt>'http_proxy'</tt>
749
- # is set to a \URI string,
750
- # the returned +http+ will have that URI as its proxy;
751
- # note that the \URI string must have a protocol
752
- # such as <tt>'http'</tt> or <tt>'https'</tt>:
753
- #
754
- # ENV['http_proxy'] = 'http://example.com'
755
- # # => "http://example.com"
756
- # http = Net::HTTP.new(hostname)
757
- # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
758
- # http.proxy? # => true
759
- # http.address # => "jsonplaceholder.typicode.com"
760
- # http.proxy_address # => "example.com"
761
- #
762
- # The \URI string may include proxy username, password, and port number:
763
- #
764
- # ENV['http_proxy'] = 'http://pname:ppass@example.com:8000'
765
- # # => "http://pname:ppass@example.com:8000"
766
- # http = Net::HTTP.new(hostname)
767
- # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
768
- # http.proxy_port # => 8000
769
- # http.proxy_user # => "pname"
770
- # http.proxy_pass # => "ppass"
771
- #
772
- # <b>Argument +p_no_proxy+</b>
773
- #
774
- # You can use argument +p_no_proxy+ to reject certain proxies:
775
- #
776
- # - Reject a certain address:
777
- #
778
- # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
779
- # http.proxy_address # => nil
780
- #
781
- # - Reject certain domains or subdomains:
782
- #
783
- # http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
784
- # http.proxy_address # => nil
785
- #
786
- # - Reject certain addresses and port combinations:
1103
+ # For proxy-defining arguments +p_addr+ through +p_no_proxy+,
1104
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
787
1105
  #
788
- # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234')
789
- # http.proxy_address # => "proxy.example"
790
- #
791
- # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000')
792
- # http.proxy_address # => nil
793
- #
794
- # - Reject a list of the types above delimited using a comma:
795
- #
796
- # http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
797
- # http.proxy_address # => nil
798
- #
799
- # http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
800
- # http.proxy_address # => nil
801
- #
802
- def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
1106
+ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil)
803
1107
  http = super address, port
804
1108
 
805
1109
  if proxy_class? then # from Net::HTTP::Proxy()
@@ -808,10 +1112,11 @@ module Net #:nodoc:
808
1112
  http.proxy_port = @proxy_port
809
1113
  http.proxy_user = @proxy_user
810
1114
  http.proxy_pass = @proxy_pass
1115
+ http.proxy_use_ssl = @proxy_use_ssl
811
1116
  elsif p_addr == :ENV then
812
1117
  http.proxy_from_env = true
813
1118
  else
814
- if p_addr && p_no_proxy && !URI::Generic.use_proxy?(p_addr, p_addr, p_port, p_no_proxy)
1119
+ if p_addr && p_no_proxy && !URI::Generic.use_proxy?(address, address, port, p_no_proxy)
815
1120
  p_addr = nil
816
1121
  p_port = nil
817
1122
  end
@@ -819,34 +1124,67 @@ module Net #:nodoc:
819
1124
  http.proxy_port = p_port || default_port
820
1125
  http.proxy_user = p_user
821
1126
  http.proxy_pass = p_pass
1127
+ http.proxy_use_ssl = p_use_ssl
822
1128
  end
823
1129
 
824
1130
  http
825
1131
  end
826
1132
 
827
- # Creates a new Net::HTTP object for the specified server address,
828
- # without opening the TCP connection or initializing the HTTP session.
1133
+ class << HTTP
1134
+ # Allows to set the default configuration that will be used
1135
+ # when creating a new connection.
1136
+ #
1137
+ # Example:
1138
+ #
1139
+ # Net::HTTP.default_configuration = {
1140
+ # read_timeout: 1,
1141
+ # write_timeout: 1
1142
+ # }
1143
+ # http = Net::HTTP.new(hostname)
1144
+ # http.open_timeout # => 60
1145
+ # http.read_timeout # => 1
1146
+ # http.write_timeout # => 1
1147
+ #
1148
+ attr_accessor :default_configuration
1149
+ end
1150
+
1151
+ # Creates a new \Net::HTTP object for the specified server address,
1152
+ # without opening the TCP connection or initializing the \HTTP session.
829
1153
  # The +address+ should be a DNS hostname or IP address.
830
- def initialize(address, port = nil)
1154
+ def initialize(address, port = nil) # :nodoc:
1155
+ defaults = {
1156
+ keep_alive_timeout: 2,
1157
+ close_on_empty_response: false,
1158
+ open_timeout: 60,
1159
+ read_timeout: 60,
1160
+ write_timeout: 60,
1161
+ continue_timeout: nil,
1162
+ max_retries: 1,
1163
+ debug_output: nil,
1164
+ response_body_encoding: false,
1165
+ ignore_eof: true
1166
+ }
1167
+ options = defaults.merge(self.class.default_configuration || {})
1168
+
831
1169
  @address = address
832
1170
  @port = (port || HTTP.default_port)
833
1171
  @ipaddr = nil
834
1172
  @local_host = nil
835
1173
  @local_port = nil
836
1174
  @curr_http_version = HTTPVersion
837
- @keep_alive_timeout = 2
1175
+ @keep_alive_timeout = options[:keep_alive_timeout]
838
1176
  @last_communicated = nil
839
- @close_on_empty_response = false
1177
+ @close_on_empty_response = options[:close_on_empty_response]
840
1178
  @socket = nil
841
1179
  @started = false
842
- @open_timeout = 60
843
- @read_timeout = 60
844
- @write_timeout = 60
845
- @continue_timeout = nil
846
- @max_retries = 1
847
- @debug_output = nil
848
- @response_body_encoding = false
849
- @ignore_eof = true
1180
+ @open_timeout = options[:open_timeout]
1181
+ @read_timeout = options[:read_timeout]
1182
+ @write_timeout = options[:write_timeout]
1183
+ @continue_timeout = options[:continue_timeout]
1184
+ @max_retries = options[:max_retries]
1185
+ @debug_output = options[:debug_output]
1186
+ @response_body_encoding = options[:response_body_encoding]
1187
+ @ignore_eof = options[:ignore_eof]
850
1188
 
851
1189
  @proxy_from_env = false
852
1190
  @proxy_uri = nil
@@ -854,6 +1192,7 @@ module Net #:nodoc:
854
1192
  @proxy_port = nil
855
1193
  @proxy_user = nil
856
1194
  @proxy_pass = nil
1195
+ @proxy_use_ssl = nil
857
1196
 
858
1197
  @use_ssl = false
859
1198
  @ssl_context = nil
@@ -927,21 +1266,22 @@ module Net #:nodoc:
927
1266
  @debug_output = output
928
1267
  end
929
1268
 
930
- # The DNS host name or IP address to connect to.
1269
+ # Returns the string host name or host IP given as argument +address+ in ::new.
931
1270
  attr_reader :address
932
1271
 
933
- # The port number to connect to.
1272
+ # Returns the integer port number given as argument +port+ in ::new.
934
1273
  attr_reader :port
935
1274
 
936
- # The local host used to establish the connection.
1275
+ # Sets or returns the string local host used to establish the connection;
1276
+ # initially +nil+.
937
1277
  attr_accessor :local_host
938
1278
 
939
- # The local port used to establish the connection.
1279
+ # Sets or returns the integer local port used to establish the connection;
1280
+ # initially +nil+.
940
1281
  attr_accessor :local_port
941
1282
 
942
- # The encoding to use for the response body. If Encoding, uses the
943
- # specified encoding. If other true value, tries to detect the response
944
- # body encoding.
1283
+ # Returns the encoding to use for the response body;
1284
+ # see #response_body_encoding=.
945
1285
  attr_reader :response_body_encoding
946
1286
 
947
1287
  # Sets the encoding to be used for the response body;
@@ -967,11 +1307,27 @@ module Net #:nodoc:
967
1307
  @response_body_encoding = value
968
1308
  end
969
1309
 
1310
+ # Sets whether to determine the proxy from environment variable
1311
+ # '<tt>ENV['http_proxy']</tt>';
1312
+ # see {Proxy Using ENV['http_proxy']}[rdoc-ref:Net::HTTP@Proxy+Using+-27ENV-5B-27http_proxy-27-5D-27].
970
1313
  attr_writer :proxy_from_env
1314
+
1315
+ # Sets the proxy address;
1316
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
971
1317
  attr_writer :proxy_address
1318
+
1319
+ # Sets the proxy port;
1320
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
972
1321
  attr_writer :proxy_port
1322
+
1323
+ # Sets the proxy user;
1324
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
973
1325
  attr_writer :proxy_user
1326
+
1327
+ # Sets the proxy password;
1328
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
974
1329
  attr_writer :proxy_pass
1330
+ attr_writer :proxy_use_ssl
975
1331
 
976
1332
  # Returns the IP address for the connection.
977
1333
  #
@@ -1009,27 +1365,25 @@ module Net #:nodoc:
1009
1365
  @ipaddr = addr
1010
1366
  end
1011
1367
 
1012
- # Number of seconds to wait for the connection to open. Any number
1013
- # may be used, including Floats for fractional seconds. If the HTTP
1014
- # object cannot open a connection in this many seconds, it raises a
1015
- # Net::OpenTimeout exception. The default value is 60 seconds.
1368
+ # Sets or returns the numeric (\Integer or \Float) number of seconds
1369
+ # to wait for a connection to open;
1370
+ # initially 60.
1371
+ # If the connection is not made in the given interval,
1372
+ # an exception is raised.
1016
1373
  attr_accessor :open_timeout
1017
1374
 
1018
- # Number of seconds to wait for one block to be read (via one read(2)
1019
- # call). Any number may be used, including Floats for fractional
1020
- # seconds. If the HTTP object cannot read data in this many seconds,
1021
- # it raises a Net::ReadTimeout exception. The default value is 60 seconds.
1375
+ # Returns the numeric (\Integer or \Float) number of seconds
1376
+ # to wait for one block to be read (via one read(2) call);
1377
+ # see #read_timeout=.
1022
1378
  attr_reader :read_timeout
1023
1379
 
1024
- # Number of seconds to wait for one block to be written (via one write(2)
1025
- # call). Any number may be used, including Floats for fractional
1026
- # seconds. If the HTTP object cannot write data in this many seconds,
1027
- # it raises a Net::WriteTimeout exception. The default value is 60 seconds.
1028
- # Net::WriteTimeout is not raised on Windows.
1380
+ # Returns the numeric (\Integer or \Float) number of seconds
1381
+ # to wait for one block to be written (via one write(2) call);
1382
+ # see #write_timeout=.
1029
1383
  attr_reader :write_timeout
1030
1384
 
1031
1385
  # Sets the maximum number of times to retry an idempotent request in case of
1032
- # Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
1386
+ # \Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
1033
1387
  # Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
1034
1388
  # Timeout::Error.
1035
1389
  # The initial value is 1.
@@ -1048,6 +1402,8 @@ module Net #:nodoc:
1048
1402
  @max_retries = retries
1049
1403
  end
1050
1404
 
1405
+ # Returns the maximum number of times to retry an idempotent request;
1406
+ # see #max_retries=.
1051
1407
  attr_reader :max_retries
1052
1408
 
1053
1409
  # Sets the read timeout, in seconds, for +self+ to integer +sec+;
@@ -1069,52 +1425,90 @@ module Net #:nodoc:
1069
1425
  # Sets the write timeout, in seconds, for +self+ to integer +sec+;
1070
1426
  # the initial value is 60.
1071
1427
  #
1072
- # Argument +sec+ must be a non-negative numeric value.
1428
+ # Argument +sec+ must be a non-negative numeric value:
1429
+ #
1430
+ # _uri = uri.dup
1431
+ # _uri.path = '/posts'
1432
+ # body = 'bar' * 200000
1433
+ # data = <<EOF
1434
+ # {"title": "foo", "body": "#{body}", "userId": "1"}
1435
+ # EOF
1436
+ # headers = {'content-type': 'application/json'}
1437
+ # http = Net::HTTP.new(hostname)
1438
+ # http.write_timeout # => 60
1439
+ # http.post(_uri.path, data, headers)
1440
+ # # => #<Net::HTTPCreated 201 Created readbody=true>
1441
+ # http.write_timeout = 0
1442
+ # http.post(_uri.path, data, headers) # Raises Net::WriteTimeout.
1073
1443
  #
1074
1444
  def write_timeout=(sec)
1075
1445
  @socket.write_timeout = sec if @socket
1076
1446
  @write_timeout = sec
1077
1447
  end
1078
1448
 
1079
- # Seconds to wait for 100 Continue response. If the HTTP object does not
1080
- # receive a response in this many seconds it sends the request body. The
1081
- # default value is +nil+.
1449
+ # Returns the continue timeout value;
1450
+ # see continue_timeout=.
1082
1451
  attr_reader :continue_timeout
1083
1452
 
1084
- # Setter for the continue_timeout attribute.
1453
+ # Sets the continue timeout value,
1454
+ # which is the number of seconds to wait for an expected 100 Continue response.
1455
+ # If the \HTTP object does not receive a response in this many seconds
1456
+ # it sends the request body.
1085
1457
  def continue_timeout=(sec)
1086
1458
  @socket.continue_timeout = sec if @socket
1087
1459
  @continue_timeout = sec
1088
1460
  end
1089
1461
 
1090
- # Seconds to reuse the connection of the previous request.
1091
- # If the idle time is less than this Keep-Alive Timeout,
1092
- # Net::HTTP reuses the TCP/IP socket used by the previous communication.
1093
- # The default value is 2 seconds.
1462
+ # Sets or returns the numeric (\Integer or \Float) number of seconds
1463
+ # to keep the connection open after a request is sent;
1464
+ # initially 2.
1465
+ # If a new request is made during the given interval,
1466
+ # the still-open connection is used;
1467
+ # otherwise the connection will have been closed
1468
+ # and a new connection is opened.
1094
1469
  attr_accessor :keep_alive_timeout
1095
1470
 
1096
- # Whether to ignore EOF when reading response bodies with defined
1097
- # Content-Length headers. For backwards compatibility, the default is true.
1471
+ # Sets or returns whether to ignore end-of-file when reading a response body
1472
+ # with <tt>Content-Length</tt> headers;
1473
+ # initially +true+.
1098
1474
  attr_accessor :ignore_eof
1099
1475
 
1100
- # Returns true if the HTTP session has been started.
1476
+ # Returns +true+ if the \HTTP session has been started:
1477
+ #
1478
+ # http = Net::HTTP.new(hostname)
1479
+ # http.started? # => false
1480
+ # http.start
1481
+ # http.started? # => true
1482
+ # http.finish # => nil
1483
+ # http.started? # => false
1484
+ #
1485
+ # Net::HTTP.start(hostname) do |http|
1486
+ # http.started?
1487
+ # end # => true
1488
+ # http.started? # => false
1489
+ #
1101
1490
  def started?
1102
1491
  @started
1103
1492
  end
1104
1493
 
1105
1494
  alias active? started? #:nodoc: obsolete
1106
1495
 
1496
+ # Sets or returns whether to close the connection when the response is empty;
1497
+ # initially +false+.
1107
1498
  attr_accessor :close_on_empty_response
1108
1499
 
1109
- # Returns true if SSL/TLS is being used with HTTP.
1500
+ # Returns +true+ if +self+ uses SSL, +false+ otherwise.
1501
+ # See Net::HTTP#use_ssl=.
1110
1502
  def use_ssl?
1111
1503
  @use_ssl
1112
1504
  end
1113
1505
 
1114
- # Turn on/off SSL.
1115
- # This flag must be set before starting session.
1116
- # If you change use_ssl value after session started,
1117
- # a Net::HTTP object raises IOError.
1506
+ # Sets whether a new session is to use
1507
+ # {Transport Layer Security}[https://en.wikipedia.org/wiki/Transport_Layer_Security]:
1508
+ #
1509
+ # Raises IOError if attempting to change during a session.
1510
+ #
1511
+ # Raises OpenSSL::SSL::SSLError if the port is not an HTTPS port.
1118
1512
  def use_ssl=(flag)
1119
1513
  flag = flag ? true : false
1120
1514
  if started? and @use_ssl != flag
@@ -1123,23 +1517,6 @@ module Net #:nodoc:
1123
1517
  @use_ssl = flag
1124
1518
  end
1125
1519
 
1126
- SSL_IVNAMES = [
1127
- :@ca_file,
1128
- :@ca_path,
1129
- :@cert,
1130
- :@cert_store,
1131
- :@ciphers,
1132
- :@extra_chain_cert,
1133
- :@key,
1134
- :@ssl_timeout,
1135
- :@ssl_version,
1136
- :@min_version,
1137
- :@max_version,
1138
- :@verify_callback,
1139
- :@verify_depth,
1140
- :@verify_mode,
1141
- :@verify_hostname,
1142
- ]
1143
1520
  SSL_ATTRIBUTES = [
1144
1521
  :ca_file,
1145
1522
  :ca_path,
@@ -1156,64 +1533,69 @@ module Net #:nodoc:
1156
1533
  :verify_depth,
1157
1534
  :verify_mode,
1158
1535
  :verify_hostname,
1159
- ]
1536
+ ] # :nodoc:
1160
1537
 
1161
- # Sets path of a CA certification file in PEM format.
1162
- #
1163
- # The file can contain several CA certificates.
1538
+ SSL_IVNAMES = SSL_ATTRIBUTES.map { |a| "@#{a}".to_sym } # :nodoc:
1539
+
1540
+ # Sets or returns the path to a CA certification file in PEM format.
1164
1541
  attr_accessor :ca_file
1165
1542
 
1166
- # Sets path of a CA certification directory containing certifications in
1167
- # PEM format.
1543
+ # Sets or returns the path of to CA directory
1544
+ # containing certification files in PEM format.
1168
1545
  attr_accessor :ca_path
1169
1546
 
1170
- # Sets an OpenSSL::X509::Certificate object as client certificate.
1171
- # (This method is appeared in Michal Rokos's OpenSSL extension).
1547
+ # Sets or returns the OpenSSL::X509::Certificate object
1548
+ # to be used for client certification.
1172
1549
  attr_accessor :cert
1173
1550
 
1174
- # Sets the X509::Store to verify peer certificate.
1551
+ # Sets or returns the X509::Store to be used for verifying peer certificate.
1175
1552
  attr_accessor :cert_store
1176
1553
 
1177
- # Sets the available ciphers. See OpenSSL::SSL::SSLContext#ciphers=
1554
+ # Sets or returns the available SSL ciphers.
1555
+ # See {OpenSSL::SSL::SSLContext#ciphers=}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-ciphers-3D].
1178
1556
  attr_accessor :ciphers
1179
1557
 
1180
- # Sets the extra X509 certificates to be added to the certificate chain.
1181
- # See OpenSSL::SSL::SSLContext#extra_chain_cert=
1558
+ # Sets or returns the extra X509 certificates to be added to the certificate chain.
1559
+ # See {OpenSSL::SSL::SSLContext#add_certificate}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-add_certificate].
1182
1560
  attr_accessor :extra_chain_cert
1183
1561
 
1184
- # Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
1185
- # (This method is appeared in Michal Rokos's OpenSSL extension.)
1562
+ # Sets or returns the OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
1186
1563
  attr_accessor :key
1187
1564
 
1188
- # Sets the SSL timeout seconds.
1565
+ # Sets or returns the SSL timeout seconds.
1189
1566
  attr_accessor :ssl_timeout
1190
1567
 
1191
- # Sets the SSL version. See OpenSSL::SSL::SSLContext#ssl_version=
1568
+ # Sets or returns the SSL version.
1569
+ # See {OpenSSL::SSL::SSLContext#ssl_version=}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-ssl_version-3D].
1192
1570
  attr_accessor :ssl_version
1193
1571
 
1194
- # Sets the minimum SSL version. See OpenSSL::SSL::SSLContext#min_version=
1572
+ # Sets or returns the minimum SSL version.
1573
+ # See {OpenSSL::SSL::SSLContext#min_version=}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-min_version-3D].
1195
1574
  attr_accessor :min_version
1196
1575
 
1197
- # Sets the maximum SSL version. See OpenSSL::SSL::SSLContext#max_version=
1576
+ # Sets or returns the maximum SSL version.
1577
+ # See {OpenSSL::SSL::SSLContext#max_version=}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-max_version-3D].
1198
1578
  attr_accessor :max_version
1199
1579
 
1200
- # Sets the verify callback for the server certification verification.
1580
+ # Sets or returns the callback for the server certification verification.
1201
1581
  attr_accessor :verify_callback
1202
1582
 
1203
- # Sets the maximum depth for the certificate chain verification.
1583
+ # Sets or returns the maximum depth for the certificate chain verification.
1204
1584
  attr_accessor :verify_depth
1205
1585
 
1206
- # Sets the flags for server the certification verification at beginning of
1207
- # SSL/TLS session.
1208
- #
1586
+ # Sets or returns the flags for server the certification verification
1587
+ # at the beginning of the SSL/TLS session.
1209
1588
  # OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER are acceptable.
1210
1589
  attr_accessor :verify_mode
1211
1590
 
1212
- # Sets to check the server certificate is valid for the hostname.
1213
- # See OpenSSL::SSL::SSLContext#verify_hostname=
1591
+ # Sets or returns whether to verify that the server certificate is valid
1592
+ # for the hostname.
1593
+ # See {OpenSSL::SSL::SSLContext#verify_hostname=}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#attribute-i-verify_mode].
1214
1594
  attr_accessor :verify_hostname
1215
1595
 
1216
- # Returns the X.509 certificates the server presented.
1596
+ # Returns the X509 certificate chain (an array of strings)
1597
+ # for the session's socket peer,
1598
+ # or +nil+ if none.
1217
1599
  def peer_cert
1218
1600
  if not use_ssl? or not @socket
1219
1601
  return nil
@@ -1221,14 +1603,26 @@ module Net #:nodoc:
1221
1603
  @socket.io.peer_cert
1222
1604
  end
1223
1605
 
1224
- # Opens a TCP connection and HTTP session.
1606
+ # Starts an \HTTP session.
1607
+ #
1608
+ # Without a block, returns +self+:
1225
1609
  #
1226
- # When this method is called with a block, it passes the Net::HTTP
1227
- # object to the block, and closes the TCP connection and HTTP session
1228
- # after the block has been executed.
1610
+ # http = Net::HTTP.new(hostname)
1611
+ # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
1612
+ # http.start
1613
+ # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=true>
1614
+ # http.started? # => true
1615
+ # http.finish
1229
1616
  #
1230
- # When called with a block, it returns the return value of the
1231
- # block; otherwise, it returns self.
1617
+ # With a block, calls the block with +self+,
1618
+ # finishes the session when the block exits,
1619
+ # and returns the block's value:
1620
+ #
1621
+ # http.start do |http|
1622
+ # http
1623
+ # end
1624
+ # # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
1625
+ # http.started? # => false
1232
1626
  #
1233
1627
  def start # :yield: http
1234
1628
  raise IOError, 'HTTP session already opened' if @started
@@ -1278,19 +1672,25 @@ module Net #:nodoc:
1278
1672
  debug "opened"
1279
1673
  if use_ssl?
1280
1674
  if proxy?
1281
- plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
1675
+ if @proxy_use_ssl
1676
+ proxy_sock = OpenSSL::SSL::SSLSocket.new(s)
1677
+ ssl_socket_connect(proxy_sock, @open_timeout)
1678
+ else
1679
+ proxy_sock = s
1680
+ end
1681
+ proxy_sock = BufferedIO.new(proxy_sock, read_timeout: @read_timeout,
1282
1682
  write_timeout: @write_timeout,
1283
1683
  continue_timeout: @continue_timeout,
1284
1684
  debug_output: @debug_output)
1285
- buf = "CONNECT #{conn_address}:#{@port} HTTP/#{HTTPVersion}\r\n"
1286
- buf << "Host: #{@address}:#{@port}\r\n"
1685
+ buf = +"CONNECT #{conn_address}:#{@port} HTTP/#{HTTPVersion}\r\n" \
1686
+ "Host: #{@address}:#{@port}\r\n"
1287
1687
  if proxy_user
1288
1688
  credential = ["#{proxy_user}:#{proxy_pass}"].pack('m0')
1289
1689
  buf << "Proxy-Authorization: Basic #{credential}\r\n"
1290
1690
  end
1291
1691
  buf << "\r\n"
1292
- plain_sock.write(buf)
1293
- HTTPResponse.read_new(plain_sock).value
1692
+ proxy_sock.write(buf)
1693
+ HTTPResponse.read_new(proxy_sock).value
1294
1694
  # assuming nothing left in buffers after successful CONNECT response
1295
1695
  end
1296
1696
 
@@ -1364,8 +1764,15 @@ module Net #:nodoc:
1364
1764
  end
1365
1765
  private :on_connect
1366
1766
 
1367
- # Finishes the HTTP session and closes the TCP connection.
1368
- # Raises IOError if the session has not been started.
1767
+ # Finishes the \HTTP session:
1768
+ #
1769
+ # http = Net::HTTP.new(hostname)
1770
+ # http.start
1771
+ # http.started? # => true
1772
+ # http.finish # => nil
1773
+ # http.started? # => false
1774
+ #
1775
+ # Raises IOError if not in a session.
1369
1776
  def finish
1370
1777
  raise IOError, 'HTTP session not yet started' unless started?
1371
1778
  do_finish
@@ -1391,13 +1798,14 @@ module Net #:nodoc:
1391
1798
  @proxy_port = nil
1392
1799
  @proxy_user = nil
1393
1800
  @proxy_pass = nil
1801
+ @proxy_use_ssl = nil
1394
1802
 
1395
- # Creates an HTTP proxy class which behaves like Net::HTTP, but
1803
+ # Creates an \HTTP proxy class which behaves like \Net::HTTP, but
1396
1804
  # performs all access via the specified proxy.
1397
1805
  #
1398
1806
  # This class is obsolete. You may pass these same parameters directly to
1399
- # Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1400
- def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
1807
+ # \Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1808
+ def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil) #:nodoc:
1401
1809
  return self unless p_addr
1402
1810
 
1403
1811
  Class.new(self) {
@@ -1415,35 +1823,45 @@ module Net #:nodoc:
1415
1823
 
1416
1824
  @proxy_user = p_user
1417
1825
  @proxy_pass = p_pass
1826
+ @proxy_use_ssl = p_use_ssl
1418
1827
  }
1419
1828
  end
1420
1829
 
1421
1830
  class << HTTP
1422
- # returns true if self is a class which was created by HTTP::Proxy.
1831
+ # Returns true if self is a class which was created by HTTP::Proxy.
1423
1832
  def proxy_class?
1424
1833
  defined?(@is_proxy_class) ? @is_proxy_class : false
1425
1834
  end
1426
1835
 
1427
- # Address of proxy host. If Net::HTTP does not use a proxy, nil.
1836
+ # Returns the address of the proxy host, or +nil+ if none;
1837
+ # see Net::HTTP@Proxy+Server.
1428
1838
  attr_reader :proxy_address
1429
1839
 
1430
- # Port number of proxy host. If Net::HTTP does not use a proxy, nil.
1840
+ # Returns the port number of the proxy host, or +nil+ if none;
1841
+ # see Net::HTTP@Proxy+Server.
1431
1842
  attr_reader :proxy_port
1432
1843
 
1433
- # User name for accessing proxy. If Net::HTTP does not use a proxy, nil.
1844
+ # Returns the user name for accessing the proxy, or +nil+ if none;
1845
+ # see Net::HTTP@Proxy+Server.
1434
1846
  attr_reader :proxy_user
1435
1847
 
1436
- # User password for accessing proxy. If Net::HTTP does not use a proxy,
1437
- # nil.
1848
+ # Returns the password for accessing the proxy, or +nil+ if none;
1849
+ # see Net::HTTP@Proxy+Server.
1438
1850
  attr_reader :proxy_pass
1851
+
1852
+ # Use SSL when talking to the proxy. If Net::HTTP does not use a proxy, nil.
1853
+ attr_reader :proxy_use_ssl
1439
1854
  end
1440
1855
 
1441
- # True if requests for this connection will be proxied
1856
+ # Returns +true+ if a proxy server is defined, +false+ otherwise;
1857
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1442
1858
  def proxy?
1443
1859
  !!(@proxy_from_env ? proxy_uri : @proxy_address)
1444
1860
  end
1445
1861
 
1446
- # True if the proxy for this connection is determined from the environment
1862
+ # Returns +true+ if the proxy server is defined in the environment,
1863
+ # +false+ otherwise;
1864
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1447
1865
  def proxy_from_env?
1448
1866
  @proxy_from_env
1449
1867
  end
@@ -1452,12 +1870,13 @@ module Net #:nodoc:
1452
1870
  def proxy_uri # :nodoc:
1453
1871
  return if @proxy_uri == false
1454
1872
  @proxy_uri ||= URI::HTTP.new(
1455
- "http".freeze, nil, address, port, nil, nil, nil, nil, nil
1873
+ "http", nil, address, port, nil, nil, nil, nil, nil
1456
1874
  ).find_proxy || false
1457
1875
  @proxy_uri || nil
1458
1876
  end
1459
1877
 
1460
- # The address of the proxy server, if one is configured.
1878
+ # Returns the address of the proxy server, if defined, +nil+ otherwise;
1879
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1461
1880
  def proxy_address
1462
1881
  if @proxy_from_env then
1463
1882
  proxy_uri&.hostname
@@ -1466,7 +1885,8 @@ module Net #:nodoc:
1466
1885
  end
1467
1886
  end
1468
1887
 
1469
- # The port of the proxy server, if one is configured.
1888
+ # Returns the port number of the proxy server, if defined, +nil+ otherwise;
1889
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1470
1890
  def proxy_port
1471
1891
  if @proxy_from_env then
1472
1892
  proxy_uri&.port
@@ -1475,7 +1895,8 @@ module Net #:nodoc:
1475
1895
  end
1476
1896
  end
1477
1897
 
1478
- # The username of the proxy server, if one is configured.
1898
+ # Returns the user name of the proxy server, if defined, +nil+ otherwise;
1899
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1479
1900
  def proxy_user
1480
1901
  if @proxy_from_env
1481
1902
  user = proxy_uri&.user
@@ -1485,7 +1906,8 @@ module Net #:nodoc:
1485
1906
  end
1486
1907
  end
1487
1908
 
1488
- # The password of the proxy server, if one is configured.
1909
+ # Returns the password of the proxy server, if defined, +nil+ otherwise;
1910
+ # see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
1489
1911
  def proxy_pass
1490
1912
  if @proxy_from_env
1491
1913
  pass = proxy_uri&.password
@@ -1533,45 +1955,38 @@ module Net #:nodoc:
1533
1955
 
1534
1956
  public
1535
1957
 
1536
- # Retrieves data from +path+ on the connected-to host which may be an
1537
- # absolute path String or a URI to extract the path from.
1958
+ # :call-seq:
1959
+ # get(path, initheader = nil) {|res| ... }
1538
1960
  #
1539
- # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
1540
- # and it defaults to an empty hash.
1541
- # If +initheader+ doesn't have the key 'accept-encoding', then
1542
- # a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
1543
- # so that gzip compression is used in preference to deflate
1544
- # compression, which is used in preference to no compression.
1545
- # Ruby doesn't have libraries to support the compress (Lempel-Ziv)
1546
- # compression, so that is not supported. The intent of this is
1547
- # to reduce bandwidth by default. If this routine sets up
1548
- # compression, then it does the decompression also, removing
1549
- # the header as well to prevent confusion. Otherwise
1550
- # it leaves the body as it found it.
1961
+ # Sends a GET request to the server;
1962
+ # returns an instance of a subclass of Net::HTTPResponse.
1551
1963
  #
1552
- # This method returns a Net::HTTPResponse object.
1964
+ # The request is based on the Net::HTTP::Get object
1965
+ # created from string +path+ and initial headers hash +initheader+.
1553
1966
  #
1554
- # If called with a block, yields each fragment of the
1555
- # entity body in turn as a string as it is read from
1556
- # the socket. Note that in this case, the returned response
1557
- # object will *not* contain a (meaningful) body.
1967
+ # With a block given, calls the block with the response body:
1558
1968
  #
1559
- # +dest+ argument is obsolete.
1560
- # It still works but you must not use it.
1969
+ # http = Net::HTTP.new(hostname)
1970
+ # http.get('/todos/1') do |res|
1971
+ # p res
1972
+ # end # => #<Net::HTTPOK 200 OK readbody=true>
1561
1973
  #
1562
- # This method never raises an exception.
1974
+ # Output:
1563
1975
  #
1564
- # response = http.get('/index.html')
1976
+ # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}"
1565
1977
  #
1566
- # # using block
1567
- # File.open('result.txt', 'w') {|f|
1568
- # http.get('/~foo/') do |str|
1569
- # f.write str
1570
- # end
1571
- # }
1978
+ # With no block given, simply returns the response object:
1979
+ #
1980
+ # http.get('/') # => #<Net::HTTPOK 200 OK readbody=true>
1981
+ #
1982
+ # Related:
1983
+ #
1984
+ # - Net::HTTP::Get: request class for \HTTP method GET.
1985
+ # - Net::HTTP.get: sends GET request, returns response body.
1572
1986
  #
1573
1987
  def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
1574
1988
  res = nil
1989
+
1575
1990
  request(Get.new(path, initheader)) {|r|
1576
1991
  r.read_body dest, &block
1577
1992
  res = r
@@ -1579,198 +1994,317 @@ module Net #:nodoc:
1579
1994
  res
1580
1995
  end
1581
1996
 
1582
- # Gets only the header from +path+ on the connected-to host.
1583
- # +header+ is a Hash like { 'Accept' => '*/*', ... }.
1584
- #
1585
- # This method returns a Net::HTTPResponse object.
1997
+ # Sends a HEAD request to the server;
1998
+ # returns an instance of a subclass of Net::HTTPResponse.
1586
1999
  #
1587
- # This method never raises an exception.
2000
+ # The request is based on the Net::HTTP::Head object
2001
+ # created from string +path+ and initial headers hash +initheader+:
1588
2002
  #
1589
- # response = nil
1590
- # Net::HTTP.start('some.www.server', 80) {|http|
1591
- # response = http.head('/index.html')
1592
- # }
1593
- # p response['content-type']
2003
+ # res = http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
2004
+ # res.body # => nil
2005
+ # res.to_hash.take(3)
2006
+ # # =>
2007
+ # [["date", ["Wed, 15 Feb 2023 15:25:42 GMT"]],
2008
+ # ["content-type", ["application/json; charset=utf-8"]],
2009
+ # ["connection", ["close"]]]
1594
2010
  #
1595
2011
  def head(path, initheader = nil)
1596
2012
  request(Head.new(path, initheader))
1597
2013
  end
1598
2014
 
1599
- # Posts +data+ (must be a String) to +path+. +header+ must be a Hash
1600
- # like { 'Accept' => '*/*', ... }.
2015
+ # :call-seq:
2016
+ # post(path, data, initheader = nil) {|res| ... }
1601
2017
  #
1602
- # This method returns a Net::HTTPResponse object.
2018
+ # Sends a POST request to the server;
2019
+ # returns an instance of a subclass of Net::HTTPResponse.
1603
2020
  #
1604
- # If called with a block, yields each fragment of the
1605
- # entity body in turn as a string as it is read from
1606
- # the socket. Note that in this case, the returned response
1607
- # object will *not* contain a (meaningful) body.
2021
+ # The request is based on the Net::HTTP::Post object
2022
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
1608
2023
  #
1609
- # +dest+ argument is obsolete.
1610
- # It still works but you must not use it.
2024
+ # With a block given, calls the block with the response body:
1611
2025
  #
1612
- # This method never raises exception.
2026
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2027
+ # http = Net::HTTP.new(hostname)
2028
+ # http.post('/todos', data) do |res|
2029
+ # p res
2030
+ # end # => #<Net::HTTPCreated 201 Created readbody=true>
2031
+ #
2032
+ # Output:
2033
+ #
2034
+ # "{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
1613
2035
  #
1614
- # response = http.post('/cgi-bin/search.rb', 'query=foo')
2036
+ # With no block given, simply returns the response object:
1615
2037
  #
1616
- # # using block
1617
- # File.open('result.txt', 'w') {|f|
1618
- # http.post('/cgi-bin/search.rb', 'query=foo') do |str|
1619
- # f.write str
1620
- # end
1621
- # }
2038
+ # http.post('/todos', data) # => #<Net::HTTPCreated 201 Created readbody=true>
1622
2039
  #
1623
- # You should set Content-Type: header field for POST.
1624
- # If no Content-Type: field given, this method uses
1625
- # "application/x-www-form-urlencoded" by default.
2040
+ # Related:
2041
+ #
2042
+ # - Net::HTTP::Post: request class for \HTTP method POST.
2043
+ # - Net::HTTP.post: sends POST request, returns response body.
1626
2044
  #
1627
2045
  def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
1628
2046
  send_entity(path, data, initheader, dest, Post, &block)
1629
2047
  end
1630
2048
 
1631
- # Sends a PATCH request to the +path+ and gets a response,
1632
- # as an HTTPResponse object.
2049
+ # :call-seq:
2050
+ # patch(path, data, initheader = nil) {|res| ... }
2051
+ #
2052
+ # Sends a PATCH request to the server;
2053
+ # returns an instance of a subclass of Net::HTTPResponse.
2054
+ #
2055
+ # The request is based on the Net::HTTP::Patch object
2056
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
2057
+ #
2058
+ # With a block given, calls the block with the response body:
2059
+ #
2060
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2061
+ # http = Net::HTTP.new(hostname)
2062
+ # http.patch('/todos/1', data) do |res|
2063
+ # p res
2064
+ # end # => #<Net::HTTPOK 200 OK readbody=true>
2065
+ #
2066
+ # Output:
2067
+ #
2068
+ # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false,\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\"\n}"
2069
+ #
2070
+ # With no block given, simply returns the response object:
2071
+ #
2072
+ # http.patch('/todos/1', data) # => #<Net::HTTPCreated 201 Created readbody=true>
2073
+ #
1633
2074
  def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
1634
2075
  send_entity(path, data, initheader, dest, Patch, &block)
1635
2076
  end
1636
2077
 
1637
- def put(path, data, initheader = nil) #:nodoc:
2078
+ # Sends a PUT request to the server;
2079
+ # returns an instance of a subclass of Net::HTTPResponse.
2080
+ #
2081
+ # The request is based on the Net::HTTP::Put object
2082
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
2083
+ #
2084
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2085
+ # http = Net::HTTP.new(hostname)
2086
+ # http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
2087
+ #
2088
+ # Related:
2089
+ #
2090
+ # - Net::HTTP::Put: request class for \HTTP method PUT.
2091
+ # - Net::HTTP.put: sends PUT request, returns response body.
2092
+ #
2093
+ def put(path, data, initheader = nil)
1638
2094
  request(Put.new(path, initheader), data)
1639
2095
  end
1640
2096
 
1641
- # Sends a PROPPATCH request to the +path+ and gets a response,
1642
- # as an HTTPResponse object.
2097
+ # Sends a PROPPATCH request to the server;
2098
+ # returns an instance of a subclass of Net::HTTPResponse.
2099
+ #
2100
+ # The request is based on the Net::HTTP::Proppatch object
2101
+ # created from string +path+, string +body+, and initial headers hash +initheader+.
2102
+ #
2103
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2104
+ # http = Net::HTTP.new(hostname)
2105
+ # http.proppatch('/todos/1', data)
2106
+ #
1643
2107
  def proppatch(path, body, initheader = nil)
1644
2108
  request(Proppatch.new(path, initheader), body)
1645
2109
  end
1646
2110
 
1647
- # Sends a LOCK request to the +path+ and gets a response,
1648
- # as an HTTPResponse object.
2111
+ # Sends a LOCK request to the server;
2112
+ # returns an instance of a subclass of Net::HTTPResponse.
2113
+ #
2114
+ # The request is based on the Net::HTTP::Lock object
2115
+ # created from string +path+, string +body+, and initial headers hash +initheader+.
2116
+ #
2117
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2118
+ # http = Net::HTTP.new(hostname)
2119
+ # http.lock('/todos/1', data)
2120
+ #
1649
2121
  def lock(path, body, initheader = nil)
1650
2122
  request(Lock.new(path, initheader), body)
1651
2123
  end
1652
2124
 
1653
- # Sends a UNLOCK request to the +path+ and gets a response,
1654
- # as an HTTPResponse object.
2125
+ # Sends an UNLOCK request to the server;
2126
+ # returns an instance of a subclass of Net::HTTPResponse.
2127
+ #
2128
+ # The request is based on the Net::HTTP::Unlock object
2129
+ # created from string +path+, string +body+, and initial headers hash +initheader+.
2130
+ #
2131
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2132
+ # http = Net::HTTP.new(hostname)
2133
+ # http.unlock('/todos/1', data)
2134
+ #
1655
2135
  def unlock(path, body, initheader = nil)
1656
2136
  request(Unlock.new(path, initheader), body)
1657
2137
  end
1658
2138
 
1659
- # Sends a OPTIONS request to the +path+ and gets a response,
1660
- # as an HTTPResponse object.
2139
+ # Sends an Options request to the server;
2140
+ # returns an instance of a subclass of Net::HTTPResponse.
2141
+ #
2142
+ # The request is based on the Net::HTTP::Options object
2143
+ # created from string +path+ and initial headers hash +initheader+.
2144
+ #
2145
+ # http = Net::HTTP.new(hostname)
2146
+ # http.options('/')
2147
+ #
1661
2148
  def options(path, initheader = nil)
1662
2149
  request(Options.new(path, initheader))
1663
2150
  end
1664
2151
 
1665
- # Sends a PROPFIND request to the +path+ and gets a response,
1666
- # as an HTTPResponse object.
2152
+ # Sends a PROPFIND request to the server;
2153
+ # returns an instance of a subclass of Net::HTTPResponse.
2154
+ #
2155
+ # The request is based on the Net::HTTP::Propfind object
2156
+ # created from string +path+, string +body+, and initial headers hash +initheader+.
2157
+ #
2158
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2159
+ # http = Net::HTTP.new(hostname)
2160
+ # http.propfind('/todos/1', data)
2161
+ #
1667
2162
  def propfind(path, body = nil, initheader = {'Depth' => '0'})
1668
2163
  request(Propfind.new(path, initheader), body)
1669
2164
  end
1670
2165
 
1671
- # Sends a DELETE request to the +path+ and gets a response,
1672
- # as an HTTPResponse object.
2166
+ # Sends a DELETE request to the server;
2167
+ # returns an instance of a subclass of Net::HTTPResponse.
2168
+ #
2169
+ # The request is based on the Net::HTTP::Delete object
2170
+ # created from string +path+ and initial headers hash +initheader+.
2171
+ #
2172
+ # http = Net::HTTP.new(hostname)
2173
+ # http.delete('/todos/1')
2174
+ #
1673
2175
  def delete(path, initheader = {'Depth' => 'Infinity'})
1674
2176
  request(Delete.new(path, initheader))
1675
2177
  end
1676
2178
 
1677
- # Sends a MOVE request to the +path+ and gets a response,
1678
- # as an HTTPResponse object.
2179
+ # Sends a MOVE request to the server;
2180
+ # returns an instance of a subclass of Net::HTTPResponse.
2181
+ #
2182
+ # The request is based on the Net::HTTP::Move object
2183
+ # created from string +path+ and initial headers hash +initheader+.
2184
+ #
2185
+ # http = Net::HTTP.new(hostname)
2186
+ # http.move('/todos/1')
2187
+ #
1679
2188
  def move(path, initheader = nil)
1680
2189
  request(Move.new(path, initheader))
1681
2190
  end
1682
2191
 
1683
- # Sends a COPY request to the +path+ and gets a response,
1684
- # as an HTTPResponse object.
2192
+ # Sends a COPY request to the server;
2193
+ # returns an instance of a subclass of Net::HTTPResponse.
2194
+ #
2195
+ # The request is based on the Net::HTTP::Copy object
2196
+ # created from string +path+ and initial headers hash +initheader+.
2197
+ #
2198
+ # http = Net::HTTP.new(hostname)
2199
+ # http.copy('/todos/1')
2200
+ #
1685
2201
  def copy(path, initheader = nil)
1686
2202
  request(Copy.new(path, initheader))
1687
2203
  end
1688
2204
 
1689
- # Sends a MKCOL request to the +path+ and gets a response,
1690
- # as an HTTPResponse object.
2205
+ # Sends a MKCOL request to the server;
2206
+ # returns an instance of a subclass of Net::HTTPResponse.
2207
+ #
2208
+ # The request is based on the Net::HTTP::Mkcol object
2209
+ # created from string +path+, string +body+, and initial headers hash +initheader+.
2210
+ #
2211
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
2212
+ # http.mkcol('/todos/1', data)
2213
+ # http = Net::HTTP.new(hostname)
2214
+ #
1691
2215
  def mkcol(path, body = nil, initheader = nil)
1692
2216
  request(Mkcol.new(path, initheader), body)
1693
2217
  end
1694
2218
 
1695
- # Sends a TRACE request to the +path+ and gets a response,
1696
- # as an HTTPResponse object.
2219
+ # Sends a TRACE request to the server;
2220
+ # returns an instance of a subclass of Net::HTTPResponse.
2221
+ #
2222
+ # The request is based on the Net::HTTP::Trace object
2223
+ # created from string +path+ and initial headers hash +initheader+.
2224
+ #
2225
+ # http = Net::HTTP.new(hostname)
2226
+ # http.trace('/todos/1')
2227
+ #
1697
2228
  def trace(path, initheader = nil)
1698
2229
  request(Trace.new(path, initheader))
1699
2230
  end
1700
2231
 
1701
- # Sends a GET request to the +path+.
1702
- # Returns the response as a Net::HTTPResponse object.
2232
+ # Sends a GET request to the server;
2233
+ # forms the response into a Net::HTTPResponse object.
2234
+ #
2235
+ # The request is based on the Net::HTTP::Get object
2236
+ # created from string +path+ and initial headers hash +initheader+.
1703
2237
  #
1704
- # When called with a block, passes an HTTPResponse object to the block.
1705
- # The body of the response will not have been read yet;
1706
- # the block can process it using HTTPResponse#read_body,
1707
- # if desired.
2238
+ # With no block given, returns the response object:
1708
2239
  #
1709
- # Returns the response.
2240
+ # http = Net::HTTP.new(hostname)
2241
+ # http.request_get('/todos') # => #<Net::HTTPOK 200 OK readbody=true>
1710
2242
  #
1711
- # This method never raises Net::* exceptions.
2243
+ # With a block given, calls the block with the response object
2244
+ # and returns the response object:
1712
2245
  #
1713
- # response = http.request_get('/index.html')
1714
- # # The entity body is already read in this case.
1715
- # p response['content-type']
1716
- # puts response.body
2246
+ # http.request_get('/todos') do |res|
2247
+ # p res
2248
+ # end # => #<Net::HTTPOK 200 OK readbody=true>
1717
2249
  #
1718
- # # Using a block
1719
- # http.request_get('/index.html') {|response|
1720
- # p response['content-type']
1721
- # response.read_body do |str| # read body now
1722
- # print str
1723
- # end
1724
- # }
2250
+ # Output:
2251
+ #
2252
+ # #<Net::HTTPOK 200 OK readbody=false>
1725
2253
  #
1726
2254
  def request_get(path, initheader = nil, &block) # :yield: +response+
1727
2255
  request(Get.new(path, initheader), &block)
1728
2256
  end
1729
2257
 
1730
- # Sends a HEAD request to the +path+ and returns the response
1731
- # as a Net::HTTPResponse object.
1732
- #
1733
- # Returns the response.
2258
+ # Sends a HEAD request to the server;
2259
+ # returns an instance of a subclass of Net::HTTPResponse.
1734
2260
  #
1735
- # This method never raises Net::* exceptions.
2261
+ # The request is based on the Net::HTTP::Head object
2262
+ # created from string +path+ and initial headers hash +initheader+.
1736
2263
  #
1737
- # response = http.request_head('/index.html')
1738
- # p response['content-type']
2264
+ # http = Net::HTTP.new(hostname)
2265
+ # http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
1739
2266
  #
1740
2267
  def request_head(path, initheader = nil, &block)
1741
2268
  request(Head.new(path, initheader), &block)
1742
2269
  end
1743
2270
 
1744
- # Sends a POST request to the +path+.
2271
+ # Sends a POST request to the server;
2272
+ # forms the response into a Net::HTTPResponse object.
1745
2273
  #
1746
- # Returns the response as a Net::HTTPResponse object.
2274
+ # The request is based on the Net::HTTP::Post object
2275
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
1747
2276
  #
1748
- # When called with a block, the block is passed an HTTPResponse
1749
- # object. The body of that response will not have been read yet;
1750
- # the block can process it using HTTPResponse#read_body, if desired.
2277
+ # With no block given, returns the response object:
1751
2278
  #
1752
- # Returns the response.
2279
+ # http = Net::HTTP.new(hostname)
2280
+ # http.post('/todos', 'xyzzy')
2281
+ # # => #<Net::HTTPCreated 201 Created readbody=true>
1753
2282
  #
1754
- # This method never raises Net::* exceptions.
2283
+ # With a block given, calls the block with the response body
2284
+ # and returns the response object:
1755
2285
  #
1756
- # # example
1757
- # response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
1758
- # p response.status
1759
- # puts response.body # body is already read in this case
2286
+ # http.post('/todos', 'xyzzy') do |res|
2287
+ # p res
2288
+ # end # => #<Net::HTTPCreated 201 Created readbody=true>
1760
2289
  #
1761
- # # using block
1762
- # http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
1763
- # p response.status
1764
- # p response['content-type']
1765
- # response.read_body do |str| # read body now
1766
- # print str
1767
- # end
1768
- # }
2290
+ # Output:
2291
+ #
2292
+ # "{\n \"xyzzy\": \"\",\n \"id\": 201\n}"
1769
2293
  #
1770
2294
  def request_post(path, data, initheader = nil, &block) # :yield: +response+
1771
2295
  request Post.new(path, initheader), data, &block
1772
2296
  end
1773
2297
 
2298
+ # Sends a PUT request to the server;
2299
+ # returns an instance of a subclass of Net::HTTPResponse.
2300
+ #
2301
+ # The request is based on the Net::HTTP::Put object
2302
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
2303
+ #
2304
+ # http = Net::HTTP.new(hostname)
2305
+ # http.put('/todos/1', 'xyzzy')
2306
+ # # => #<Net::HTTPOK 200 OK readbody=true>
2307
+ #
1774
2308
  def request_put(path, data, initheader = nil, &block) #:nodoc:
1775
2309
  request Put.new(path, initheader), data, &block
1776
2310
  end
@@ -1780,16 +2314,25 @@ module Net #:nodoc:
1780
2314
  alias post2 request_post #:nodoc: obsolete
1781
2315
  alias put2 request_put #:nodoc: obsolete
1782
2316
 
1783
-
1784
- # Sends an HTTP request to the HTTP server.
1785
- # Also sends a DATA string if +data+ is given.
2317
+ # Sends an \HTTP request to the server;
2318
+ # returns an instance of a subclass of Net::HTTPResponse.
1786
2319
  #
1787
- # Returns a Net::HTTPResponse object.
2320
+ # The request is based on the Net::HTTPRequest object
2321
+ # created from string +path+, string +data+, and initial headers hash +header+.
2322
+ # That object is an instance of the
2323
+ # {subclass of Net::HTTPRequest}[rdoc-ref:Net::HTTPRequest@Request+Subclasses],
2324
+ # that corresponds to the given uppercase string +name+,
2325
+ # which must be
2326
+ # an {HTTP request method}[https://en.wikipedia.org/wiki/HTTP#Request_methods]
2327
+ # or a {WebDAV request method}[https://en.wikipedia.org/wiki/WebDAV#Implementation].
1788
2328
  #
1789
- # This method never raises Net::* exceptions.
2329
+ # Examples:
1790
2330
  #
1791
- # response = http.send_request('GET', '/index.html')
1792
- # puts response.body
2331
+ # http = Net::HTTP.new(hostname)
2332
+ # http.send_request('GET', '/todos/1')
2333
+ # # => #<Net::HTTPOK 200 OK readbody=true>
2334
+ # http.send_request('POST', '/todos', 'xyzzy')
2335
+ # # => #<Net::HTTPCreated 201 Created readbody=true>
1793
2336
  #
1794
2337
  def send_request(name, path, data = nil, header = nil)
1795
2338
  has_response_body = name != 'HEAD'
@@ -1797,20 +2340,35 @@ module Net #:nodoc:
1797
2340
  request r, data
1798
2341
  end
1799
2342
 
1800
- # Sends an HTTPRequest object +req+ to the HTTP server.
2343
+ # Sends the given request +req+ to the server;
2344
+ # forms the response into a Net::HTTPResponse object.
1801
2345
  #
1802
- # If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
1803
- # data, the data is also sent. Providing data for a Net::HTTP::Head or
1804
- # Net::HTTP::Get request results in an ArgumentError.
2346
+ # The given +req+ must be an instance of a
2347
+ # {subclass of Net::HTTPRequest}[rdoc-ref:Net::HTTPRequest@Request+Subclasses].
2348
+ # Argument +body+ should be given only if needed for the request.
1805
2349
  #
1806
- # Returns an HTTPResponse object.
2350
+ # With no block given, returns the response object:
2351
+ #
2352
+ # http = Net::HTTP.new(hostname)
1807
2353
  #
1808
- # When called with a block, passes an HTTPResponse object to the block.
1809
- # The body of the response will not have been read yet;
1810
- # the block can process it using HTTPResponse#read_body,
1811
- # if desired.
2354
+ # req = Net::HTTP::Get.new('/todos/1')
2355
+ # http.request(req)
2356
+ # # => #<Net::HTTPOK 200 OK readbody=true>
1812
2357
  #
1813
- # This method never raises Net::* exceptions.
2358
+ # req = Net::HTTP::Post.new('/todos')
2359
+ # http.request(req, 'xyzzy')
2360
+ # # => #<Net::HTTPCreated 201 Created readbody=true>
2361
+ #
2362
+ # With a block given, calls the block with the response and returns the response:
2363
+ #
2364
+ # req = Net::HTTP::Get.new('/todos/1')
2365
+ # http.request(req) do |res|
2366
+ # p res
2367
+ # end # => #<Net::HTTPOK 200 OK readbody=true>
2368
+ #
2369
+ # Output:
2370
+ #
2371
+ # #<Net::HTTPOK 200 OK readbody=false>
1814
2372
  #
1815
2373
  def request(req, body = nil, &block) # :yield: +response+
1816
2374
  unless started?
@@ -1870,7 +2428,10 @@ module Net #:nodoc:
1870
2428
  res
1871
2429
  }
1872
2430
  res.reading_body(@socket, req.response_body_permitted?) {
1873
- yield res if block_given?
2431
+ if block_given?
2432
+ count = max_retries # Don't restart in the middle of a download
2433
+ yield res
2434
+ end
1874
2435
  }
1875
2436
  rescue Net::OpenTimeout
1876
2437
  raise
@@ -1998,6 +2559,11 @@ module Net #:nodoc:
1998
2559
  alias_method :D, :debug
1999
2560
  end
2000
2561
 
2562
+ # for backward compatibility until Ruby 3.5
2563
+ # https://bugs.ruby-lang.org/issues/20900
2564
+ # https://github.com/bblimke/webmock/pull/1081
2565
+ HTTPSession = HTTP
2566
+ deprecate_constant :HTTPSession
2001
2567
  end
2002
2568
 
2003
2569
  require_relative 'http/exceptions'
@@ -2012,5 +2578,3 @@ require_relative 'http/response'
2012
2578
  require_relative 'http/responses'
2013
2579
 
2014
2580
  require_relative 'http/proxy_delta'
2015
-
2016
- require_relative 'http/backward'