net-http 0.3.2 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2201663415fa5809c53f6c1d8a06524739bcee246443e22175aa89cda45c92c
4
- data.tar.gz: 971f571f8d9966a09baf43a5117d9f072ae78b890e6cc4991a958081728671c8
3
+ metadata.gz: a746ed42ea9e1e34ae2ae72383484b19ef4537fcf60a01cd92d17604573bd3b4
4
+ data.tar.gz: 4372f3a8601b83819d8f202af095bfbd2f4e3337e808c6e3488fba37e5b3c477
5
5
  SHA512:
6
- metadata.gz: '086e4def849a69d4ecad94e1a13fe35eee82453f8748be247ea2c133b40ae7d3b419a0f71fa9dcd7efe783d0b725a13d527e287da1ccc406f7daa5700f2ec8e5'
7
- data.tar.gz: 47a0f2d46fe8a9bc328241fb0fc90ea1c4285b0e46460998e40650a930bd96743051911e62b790d4502b39a007a5a04e260affe42d584bb73442ab414b51f1e3
6
+ metadata.gz: 22d42a16ac4309c1098bedf5bece45574cb19aec58b447defebb8f448c09a9398057086e508d8f88ee05fd91e8cf87d39126d973ec83a565fd3fb96e6f1fd7a2
7
+ data.tar.gz: ffd03e1e3db77772c38a31f00a10102c07ca1814c3572ceaec664a0bc03d7c8bac8a8bb5953e6603bf5f0b1d054bce6a565ee8a19810059a78e7731f26807f2d
data/Gemfile CHANGED
@@ -4,4 +4,5 @@ gemspec
4
4
 
5
5
  gem "rake"
6
6
  gem "test-unit"
7
+ gem "test-unit-ruby-core"
7
8
  gem "webrick"
data/Rakefile CHANGED
@@ -7,11 +7,4 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList["test/**/test_*.rb"]
8
8
  end
9
9
 
10
- task :sync_tool do
11
- require 'fileutils'
12
- FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
13
- FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
14
- FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
15
- end
16
-
17
10
  task :default => :test
@@ -10,9 +10,10 @@ Many code examples here use these example websites:
10
10
 
11
11
  Some examples also assume these variables:
12
12
 
13
- uri = URI('https://jsonplaceholder.typicode.com')
13
+ uri = URI('https://jsonplaceholder.typicode.com/')
14
14
  uri.freeze # Examples may not modify.
15
15
  hostname = uri.hostname # => "jsonplaceholder.typicode.com"
16
+ path = uri.path # => "/"
16
17
  port = uri.port # => 443
17
18
 
18
19
  So that example requests may be written as:
@@ -0,0 +1,3 @@
1
+ This class also includes (indirectly) module Net::HTTPHeader,
2
+ which gives access to its
3
+ {methods for getting headers}[rdoc-ref:Net::HTTPHeader@Getters].
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  # for backward compatibility
3
3
 
4
4
  # :enddoc:
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  module Net
3
3
  # Net::HTTP exception class.
4
4
  # You cannot use Net::HTTPExceptions directly; instead, you must use
@@ -1,14 +1,18 @@
1
- # frozen_string_literal: false
2
- # HTTPGenericRequest is the parent of the Net::HTTPRequest class.
3
- # Do not use this directly; use a subclass of Net::HTTPRequest.
1
+ # frozen_string_literal: true
4
2
  #
5
- # Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.
3
+ # \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
4
+ #
5
+ # Do not use this directly; instead, use a subclass of Net::HTTPRequest.
6
+ #
7
+ # == About the Examples
8
+ #
9
+ # :include: doc/net-http/examples.rdoc
6
10
  #
7
11
  class Net::HTTPGenericRequest
8
12
 
9
13
  include Net::HTTPHeader
10
14
 
11
- def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
15
+ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
12
16
  @method = m
13
17
  @request_has_body = reqbody
14
18
  @response_has_body = resbody
@@ -19,7 +23,7 @@ class Net::HTTPGenericRequest
19
23
  raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
20
24
  @uri = uri_or_path.dup
21
25
  host = @uri.hostname.dup
22
- host << ":".freeze << @uri.port.to_s if @uri.port != @uri.default_port
26
+ host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
23
27
  @path = uri_or_path.request_uri
24
28
  raise ArgumentError, "no HTTP request path given" unless @path
25
29
  else
@@ -53,15 +57,47 @@ class Net::HTTPGenericRequest
53
57
  @body_data = nil
54
58
  end
55
59
 
60
+ # Returns the string method name for the request:
61
+ #
62
+ # Net::HTTP::Get.new(uri).method # => "GET"
63
+ # Net::HTTP::Post.new(uri).method # => "POST"
64
+ #
56
65
  attr_reader :method
66
+
67
+ # Returns the string path for the request:
68
+ #
69
+ # Net::HTTP::Get.new(uri).path # => "/"
70
+ # Net::HTTP::Post.new('example.com').path # => "example.com"
71
+ #
57
72
  attr_reader :path
73
+
74
+ # Returns the URI object for the request, or +nil+ if none:
75
+ #
76
+ # Net::HTTP::Get.new(uri).uri
77
+ # # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
78
+ # Net::HTTP::Get.new('example.com').uri # => nil
79
+ #
58
80
  attr_reader :uri
59
81
 
60
- # Automatically set to false if the user sets the Accept-Encoding header.
61
- # This indicates they wish to handle Content-encoding in responses
62
- # themselves.
82
+ # Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
83
+ # has been set manually or deleted
84
+ # (indicating that the user intends to handle encoding in the response),
85
+ # +true+ otherwise:
86
+ #
87
+ # req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
88
+ # req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
89
+ # req.decode_content # => true
90
+ # req['Accept-Encoding'] = 'foo'
91
+ # req.decode_content # => false
92
+ # req.delete('Accept-Encoding')
93
+ # req.decode_content # => false
94
+ #
63
95
  attr_reader :decode_content
64
96
 
97
+ # Returns a string representation of the request:
98
+ #
99
+ # Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
100
+ #
65
101
  def inspect
66
102
  "\#<#{self.class} #{@method}>"
67
103
  end
@@ -76,21 +112,45 @@ class Net::HTTPGenericRequest
76
112
  super key, val
77
113
  end
78
114
 
115
+ # Returns whether the request may have a body:
116
+ #
117
+ # Net::HTTP::Post.new(uri).request_body_permitted? # => true
118
+ # Net::HTTP::Get.new(uri).request_body_permitted? # => false
119
+ #
79
120
  def request_body_permitted?
80
121
  @request_has_body
81
122
  end
82
123
 
124
+ # Returns whether the response may have a body:
125
+ #
126
+ # Net::HTTP::Post.new(uri).response_body_permitted? # => true
127
+ # Net::HTTP::Head.new(uri).response_body_permitted? # => false
128
+ #
83
129
  def response_body_permitted?
84
130
  @response_has_body
85
131
  end
86
132
 
87
- def body_exist?
133
+ def body_exist? # :nodoc:
88
134
  warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
89
135
  response_body_permitted?
90
136
  end
91
137
 
138
+ # Returns the string body for the request, or +nil+ if there is none:
139
+ #
140
+ # req = Net::HTTP::Post.new(uri)
141
+ # req.body # => nil
142
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
143
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
144
+ #
92
145
  attr_reader :body
93
146
 
147
+ # Sets the body for the request:
148
+ #
149
+ # req = Net::HTTP::Post.new(uri)
150
+ # req.body # => nil
151
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
152
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
153
+ #
94
154
  def body=(str)
95
155
  @body = str
96
156
  @body_stream = nil
@@ -98,8 +158,24 @@ class Net::HTTPGenericRequest
98
158
  str
99
159
  end
100
160
 
161
+ # Returns the body stream object for the request, or +nil+ if there is none:
162
+ #
163
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
164
+ # req.body_stream # => nil
165
+ # require 'stringio'
166
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
167
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
168
+ #
101
169
  attr_reader :body_stream
102
170
 
171
+ # Sets the body stream for the request:
172
+ #
173
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
174
+ # req.body_stream # => nil
175
+ # require 'stringio'
176
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
177
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
178
+ #
103
179
  def body_stream=(input)
104
180
  @body = nil
105
181
  @body_stream = input
@@ -136,15 +212,15 @@ class Net::HTTPGenericRequest
136
212
  return unless @uri
137
213
 
138
214
  if ssl
139
- scheme = 'https'.freeze
215
+ scheme = 'https'
140
216
  klass = URI::HTTPS
141
217
  else
142
- scheme = 'http'.freeze
218
+ scheme = 'http'
143
219
  klass = URI::HTTP
144
220
  end
145
221
 
146
222
  if host = self['host']
147
- host.sub!(/:.*/m, ''.freeze)
223
+ host.sub!(/:.*/m, '')
148
224
  elsif host = @uri.host
149
225
  else
150
226
  host = addr
@@ -240,7 +316,7 @@ class Net::HTTPGenericRequest
240
316
  boundary ||= SecureRandom.urlsafe_base64(40)
241
317
  chunked_p = chunked?
242
318
 
243
- buf = ''
319
+ buf = +''
244
320
  params.each do |key, value, h={}|
245
321
  key = quote_string(key, charset)
246
322
  filename =
@@ -325,7 +401,7 @@ class Net::HTTPGenericRequest
325
401
  if /[\r\n]/ =~ reqline
326
402
  raise ArgumentError, "A Request-Line must not contain CR or LF"
327
403
  end
328
- buf = ""
404
+ buf = +''
329
405
  buf << reqline << "\r\n"
330
406
  each_capitalized do |k,v|
331
407
  buf << "#{k}: #{v}\r\n"
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  #
3
3
  # The \HTTPHeader module provides access to \HTTP headers.
4
4
  #
@@ -179,6 +179,8 @@
179
179
  # - #each_value: Passes each string field value to the block.
180
180
  #
181
181
  module Net::HTTPHeader
182
+ MAX_KEY_LENGTH = 1024
183
+ MAX_FIELD_LENGTH = 65536
182
184
 
183
185
  def initialize_http_header(initheader) #:nodoc:
184
186
  @header = {}
@@ -189,6 +191,12 @@ module Net::HTTPHeader
189
191
  warn "net/http: nil HTTP header: #{key}", uplevel: 3 if $VERBOSE
190
192
  else
191
193
  value = value.strip # raise error for invalid byte sequences
194
+ if key.to_s.bytesize > MAX_KEY_LENGTH
195
+ raise ArgumentError, "too long (#{key.bytesize} bytes) header: #{key[0, 30].inspect}..."
196
+ end
197
+ if value.to_s.bytesize > MAX_FIELD_LENGTH
198
+ raise ArgumentError, "header #{key} has too long field value: #{value.bytesize}"
199
+ end
192
200
  if value.count("\r\n") > 0
193
201
  raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
194
202
  end
@@ -691,10 +699,14 @@ module Net::HTTPHeader
691
699
  # res.content_type # => "application/json"
692
700
  #
693
701
  def content_type
694
- return nil unless main_type()
695
- if sub_type()
696
- then "#{main_type()}/#{sub_type()}"
697
- else main_type()
702
+ main = main_type()
703
+ return nil unless main
704
+
705
+ sub = sub_type()
706
+ if sub
707
+ "#{main}/#{sub}"
708
+ else
709
+ main
698
710
  end
699
711
  end
700
712
 
@@ -763,19 +775,38 @@ module Net::HTTPHeader
763
775
 
764
776
  alias content_type= set_content_type
765
777
 
766
- # Set header fields and a body from HTML form data.
767
- # +params+ should be an Array of Arrays or
768
- # a Hash containing HTML form data.
769
- # Optional argument +sep+ means data record separator.
778
+ # Sets the request body to a URL-encoded string derived from argument +params+,
779
+ # and sets request header field <tt>'Content-Type'</tt>
780
+ # to <tt>'application/x-www-form-urlencoded'</tt>.
781
+ #
782
+ # The resulting request is suitable for HTTP request +POST+ or +PUT+.
783
+ #
784
+ # Argument +params+ must be suitable for use as argument +enum+ to
785
+ # {URI.encode_www_form}[https://docs.ruby-lang.org/en/master/URI.html#method-c-encode_www_form].
786
+ #
787
+ # With only argument +params+ given,
788
+ # sets the body to a URL-encoded string with the default separator <tt>'&'</tt>:
770
789
  #
771
- # Values are URL encoded as necessary and the content-type is set to
772
- # application/x-www-form-urlencoded
790
+ # req = Net::HTTP::Post.new('example.com')
773
791
  #
774
- # Example:
792
+ # req.set_form_data(q: 'ruby', lang: 'en')
793
+ # req.body # => "q=ruby&lang=en"
794
+ # req['Content-Type'] # => "application/x-www-form-urlencoded"
775
795
  #
776
- # http.form_data = {"q" => "ruby", "lang" => "en"}
777
- # http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
778
- # http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
796
+ # req.set_form_data([['q', 'ruby'], ['lang', 'en']])
797
+ # req.body # => "q=ruby&lang=en"
798
+ #
799
+ # req.set_form_data(q: ['ruby', 'perl'], lang: 'en')
800
+ # req.body # => "q=ruby&q=perl&lang=en"
801
+ #
802
+ # req.set_form_data([['q', 'ruby'], ['q', 'perl'], ['lang', 'en']])
803
+ # req.body # => "q=ruby&q=perl&lang=en"
804
+ #
805
+ # With string argument +sep+ also given,
806
+ # uses that string as the separator:
807
+ #
808
+ # req.set_form_data({q: 'ruby', lang: 'en'}, '|')
809
+ # req.body # => "q=ruby|lang=en"
779
810
  #
780
811
  # Net::HTTPHeader#form_data= is an alias for Net::HTTPHeader#set_form_data.
781
812
  def set_form_data(params, sep = '&')
@@ -787,56 +818,108 @@ module Net::HTTPHeader
787
818
 
788
819
  alias form_data= set_form_data
789
820
 
790
- # Set an HTML form data set.
791
- # +params+ :: The form data to set, which should be an enumerable.
792
- # See below for more details.
793
- # +enctype+ :: The content type to use to encode the form submission,
794
- # which should be application/x-www-form-urlencoded or
795
- # multipart/form-data.
796
- # +formopt+ :: An options hash, supporting the following options:
797
- # :boundary :: The boundary of the multipart message. If
798
- # not given, a random boundary will be used.
799
- # :charset :: The charset of the form submission. All
800
- # field names and values of non-file fields
801
- # should be encoded with this charset.
802
- #
803
- # Each item of params should respond to +each+ and yield 2-3 arguments,
804
- # or an array of 2-3 elements. The arguments yielded should be:
805
- #
806
- # - The name of the field.
807
- # - The value of the field, it should be a String or a File or IO-like.
808
- # - An options hash, supporting the following options
809
- # (used only for file uploads); entries:
810
- #
811
- # - +:filename+: The name of the file to use.
812
- # - +:content_type+: The content type of the uploaded file.
813
- #
814
- # Each item is a file field or a normal field.
815
- # If +value+ is a File object or the +opt+ hash has a :filename key,
816
- # the item is treated as a file field.
817
- #
818
- # If Transfer-Encoding is set as chunked, this sends the request using
819
- # chunked encoding. Because chunked encoding is HTTP/1.1 feature,
820
- # you should confirm that the server supports HTTP/1.1 before using
821
- # chunked encoding.
822
- #
823
- # Example:
824
- #
825
- # req.set_form([["q", "ruby"], ["lang", "en"]])
826
- #
827
- # req.set_form({"f"=>File.open('/path/to/filename')},
828
- # "multipart/form-data",
829
- # charset: "UTF-8",
830
- # )
831
- #
832
- # req.set_form([["f",
833
- # File.open('/path/to/filename.bar'),
834
- # {filename: "other-filename.foo"}
835
- # ]],
836
- # "multipart/form-data",
837
- # )
838
- #
839
- # See also RFC 2388, RFC 2616, HTML 4.01, and HTML5
821
+ # Stores form data to be used in a +POST+ or +PUT+ request.
822
+ #
823
+ # The form data given in +params+ consists of zero or more fields;
824
+ # each field is:
825
+ #
826
+ # - A scalar value.
827
+ # - A name/value pair.
828
+ # - An IO stream opened for reading.
829
+ #
830
+ # Argument +params+ should be an
831
+ # {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
832
+ # (method <tt>params.map</tt> will be called),
833
+ # and is often an array or hash.
834
+ #
835
+ # First, we set up a request:
836
+ #
837
+ # _uri = uri.dup
838
+ # _uri.path ='/posts'
839
+ # req = Net::HTTP::Post.new(_uri)
840
+ #
841
+ # <b>Argument +params+ As an Array</b>
842
+ #
843
+ # When +params+ is an array,
844
+ # each of its elements is a subarray that defines a field;
845
+ # the subarray may contain:
846
+ #
847
+ # - One string:
848
+ #
849
+ # req.set_form([['foo'], ['bar'], ['baz']])
850
+ #
851
+ # - Two strings:
852
+ #
853
+ # req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
854
+ #
855
+ # - When argument +enctype+ (see below) is given as
856
+ # <tt>'multipart/form-data'</tt>:
857
+ #
858
+ # - A string name and an IO stream opened for reading:
859
+ #
860
+ # require 'stringio'
861
+ # req.set_form([['file', StringIO.new('Ruby is cool.')]])
862
+ #
863
+ # - A string name, an IO stream opened for reading,
864
+ # and an options hash, which may contain these entries:
865
+ #
866
+ # - +:filename+: The name of the file to use.
867
+ # - +:content_type+: The content type of the uploaded file.
868
+ #
869
+ # Example:
870
+ #
871
+ # req.set_form([['file', file, {filename: "other-filename.foo"}]]
872
+ #
873
+ # The various forms may be mixed:
874
+ #
875
+ # req.set_form(['foo', %w[bar 1], ['file', file]])
876
+ #
877
+ # <b>Argument +params+ As a Hash</b>
878
+ #
879
+ # When +params+ is a hash,
880
+ # each of its entries is a name/value pair that defines a field:
881
+ #
882
+ # - The name is a string.
883
+ # - The value may be:
884
+ #
885
+ # - +nil+.
886
+ # - Another string.
887
+ # - An IO stream opened for reading
888
+ # (only when argument +enctype+ -- see below -- is given as
889
+ # <tt>'multipart/form-data'</tt>).
890
+ #
891
+ # Examples:
892
+ #
893
+ # # Nil-valued fields.
894
+ # req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil})
895
+ #
896
+ # # String-valued fields.
897
+ # req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2})
898
+ #
899
+ # # IO-valued field.
900
+ # require 'stringio'
901
+ # req.set_form({'file' => StringIO.new('Ruby is cool.')})
902
+ #
903
+ # # Mixture of fields.
904
+ # req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
905
+ #
906
+ # Optional argument +enctype+ specifies the value to be given
907
+ # to field <tt>'Content-Type'</tt>, and must be one of:
908
+ #
909
+ # - <tt>'application/x-www-form-urlencoded'</tt> (the default).
910
+ # - <tt>'multipart/form-data'</tt>;
911
+ # see {RFC 7578}[https://www.rfc-editor.org/rfc/rfc7578].
912
+ #
913
+ # Optional argument +formopt+ is a hash of options
914
+ # (applicable only when argument +enctype+
915
+ # is <tt>'multipart/form-data'</tt>)
916
+ # that may include the following entries:
917
+ #
918
+ # - +:boundary+: The value is the boundary string for the multipart message.
919
+ # If not given, the boundary is a random string.
920
+ # See {Boundary}[https://www.rfc-editor.org/rfc/rfc7578#section-4.1].
921
+ # - +:charset+: Value is the character set for the form submission.
922
+ # Field names and values of non-file fields should be encoded with this charset.
840
923
  #
841
924
  def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
842
925
  @body_data = params
@@ -852,12 +935,24 @@ module Net::HTTPHeader
852
935
  end
853
936
  end
854
937
 
855
- # Set the Authorization: header for "Basic" authorization.
938
+ # Sets header <tt>'Authorization'</tt> using the given
939
+ # +account+ and +password+ strings:
940
+ #
941
+ # req.basic_auth('my_account', 'my_password')
942
+ # req['Authorization']
943
+ # # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
944
+ #
856
945
  def basic_auth(account, password)
857
946
  @header['authorization'] = [basic_encode(account, password)]
858
947
  end
859
948
 
860
- # Set Proxy-Authorization: header for "Basic" authorization.
949
+ # Sets header <tt>'Proxy-Authorization'</tt> using the given
950
+ # +account+ and +password+ strings:
951
+ #
952
+ # req.proxy_basic_auth('my_account', 'my_password')
953
+ # req['Proxy-Authorization']
954
+ # # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
955
+ #
861
956
  def proxy_basic_auth(account, password)
862
957
  @header['proxy-authorization'] = [basic_encode(account, password)]
863
958
  end
@@ -867,6 +962,7 @@ module Net::HTTPHeader
867
962
  end
868
963
  private :basic_encode
869
964
 
965
+ # Returns whether the HTTP session is to be closed.
870
966
  def connection_close?
871
967
  token = /(?:\A|,)\s*close\s*(?:\z|,)/i
872
968
  @header['connection']&.grep(token) {return true}
@@ -874,6 +970,7 @@ module Net::HTTPHeader
874
970
  false
875
971
  end
876
972
 
973
+ # Returns whether the HTTP session is to be kept alive.
877
974
  def connection_keep_alive?
878
975
  token = /(?:\A|,)\s*keep-alive\s*(?:\z|,)/i
879
976
  @header['connection']&.grep(token) {return true}
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  module Net::HTTP::ProxyDelta #:nodoc: internal use only
3
3
  private
4
4
 
@@ -1,10 +1,55 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
- # This class is the base class for \Net::HTTP request classes;
4
- # it wraps together the request path and the request headers.
5
- #
3
+ # This class is the base class for \Net::HTTP request classes.
6
4
  # The class should not be used directly;
7
- # instead you should use its subclasses.
5
+ # instead you should use its subclasses, listed below.
6
+ #
7
+ # == Creating a Request
8
+ #
9
+ # An request object may be created with either a URI or a string hostname:
10
+ #
11
+ # require 'net/http'
12
+ # uri = URI('https://jsonplaceholder.typicode.com/')
13
+ # req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
14
+ # req = Net::HTTP::Get.new(uri.hostname) # => #<Net::HTTP::Get GET>
15
+ #
16
+ # And with any of the subclasses:
17
+ #
18
+ # req = Net::HTTP::Head.new(uri) # => #<Net::HTTP::Head HEAD>
19
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
20
+ # req = Net::HTTP::Put.new(uri) # => #<Net::HTTP::Put PUT>
21
+ # # ...
22
+ #
23
+ # The new instance is suitable for use as the argument to Net::HTTP#request.
24
+ #
25
+ # == Request Headers
26
+ #
27
+ # A new request object has these header fields by default:
28
+ #
29
+ # req.to_hash
30
+ # # =>
31
+ # {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
32
+ # "accept"=>["*/*"],
33
+ # "user-agent"=>["Ruby"],
34
+ # "host"=>["jsonplaceholder.typicode.com"]}
35
+ #
36
+ # See:
37
+ #
38
+ # - {Request header Accept-Encoding}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Accept-Encoding]
39
+ # and {Compression and Decompression}[rdoc-ref:Net::HTTP@Compression+and+Decompression].
40
+ # - {Request header Accept}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#accept-request-header].
41
+ # - {Request header User-Agent}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#user-agent-request-header].
42
+ # - {Request header Host}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#host-request-header].
43
+ #
44
+ # You can add headers or override default headers:
45
+ #
46
+ # # res = Net::HTTP::Get.new(uri, {'foo' => '0', 'bar' => '1'})
47
+ #
48
+ # This class (and therefore its subclasses) also includes (indirectly)
49
+ # module Net::HTTPHeader, which gives access to its
50
+ # {methods for setting headers}[rdoc-ref:Net::HTTPHeader@Setters].
51
+ #
52
+ # == Request Subclasses
8
53
  #
9
54
  # Subclasses for HTTP requests:
10
55
  #