126126# Various convenience methods retrieve values, set values, query values,
127127# set form values, or iterate over fields.
128128#
129+ # === Setters
130+ #
131+ # \Method #[]= can set any field, but does little to validate the new value;
132+ # some of the other setter methods provide some validation:
133+ #
134+ # - #[]=: Sets the string or array value for the given key.
135+ # - #add_field: Creates or adds to the array value for the given key.
136+ # - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
137+ # - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
138+ # - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
139+ # - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
140+ # - #set_range: Sets the value for field <tt>'Range'</tt>.
141+ #
142+ # === Form Setters
143+ #
144+ # - #set_form: Sets an HTML form data set.
145+ # - #set_form_data: Sets header fields and a body from HTML form data.
146+ #
129147# === Getters
130148#
131- # - #[]: Returns the string value for the given field.
149+ # \Method #[] can retrieve the value of any field that exists,
150+ # but always as a string;
151+ # some of the other getter methods return something different
152+ # from the simple string value:
153+ #
154+ # - #[]: Returns the string field value for the given key.
132155# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
133156# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
134157# - #content_type: Returns the string value of field <tt>'Content-Type'</tt>.
158+ # - #fetch: Returns the string field value for the given key.
159+ # - #get_fields: Returns the array field value for the given +key+.
135160# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
136161# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
137162# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
138163# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
139164# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
140165#
141- # === Setters
142- #
143- # - #[]=: Sets the string or array value for the given field.
144- # - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
145- # - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
146- # - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
147- # - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
148- # - #set_range: Sets the value for field <tt>'Range'</tt>.
149- #
150166# === Queries
151167#
152168# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
153169# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
154170# - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>.
155- # - #key?: Returns whether a given field exists.
156- #
157- # === Form Setters
158- #
159- # - #set_form: Sets an HTML form data set.
160- # - #set_form_data: Sets header fields and a body from HTML form data.
171+ # - #key?: Returns whether a given key exists.
161172#
162173# === Iterators
163174#
164175# - #each_capitalized: Passes each field capitalized-name/value pair to the block.
165176# - #each_capitalized_name: Passes each capitalized field name to the block.
166177# - #each_header: Passes each field name/value pair to the block.
167178# - #each_name: Passes each field name to the block.
168- # - #each_value: Passes each field value to the block.
179+ # - #each_value: Passes each string field value to the block.
169180#
170181module Net ::HTTPHeader
171182
@@ -196,12 +207,14 @@ def size #:nodoc: obsolete
196207 # or +nil+ if there is no such key;
197208 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
198209 #
199- # req = Net::HTTP::Get.new(uri)
200- # req['Accept'] # => "*/*"
201- # req['Foo'] = %w[bar baz bat]
202- # req['Foo '] # => "bar, baz, bat "
203- # res['Nosuch'] # => nil
210+ # res = Net::HTTP.start(hostname) do |http|
211+ # http.get('/todos/1')
212+ # end
213+ # res['Connection '] # => "keep-alive "
214+ # res['Nosuch'] # => nil
204215 #
216+ # Note that some field values may be retrieved via convenience methods;
217+ # see {Getters}[rdoc-ref:Net::HTTPHeader@Getters].
205218 def []( key )
206219 a = @header [ key . downcase . to_s ] or return nil
207220 a . join ( ', ' )
@@ -216,6 +229,8 @@ def [](key)
216229 # req['Accept'] = 'text/html'
217230 # req['Accept'] # => "text/html"
218231 #
232+ # Note that some field values may be set via convenience methods;
233+ # see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
219234 def []=( key , val )
220235 unless val
221236 @header . delete key . downcase . to_s
@@ -278,12 +293,11 @@ def add_field(key, val)
278293 # or +nil+ if there is no such field;
279294 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
280295 #
281- # req = Net::HTTP::Get.new(uri)
282- # req['Foo'] = 'bar'
283- # req.get_fields('Foo') # => ["bar"]
284- # req.add_field('Foo', 'baz')
285- # req.get_fields('Foo') # => ["bar", "baz"]
286- # req.get_fields('Nosuch') # => nil
296+ # res = Net::HTTP.start(hostname) do |http|
297+ # http.get('/todos/1')
298+ # end
299+ # res.get_fields('Connection') # => ["keep-alive"]
300+ # res.get_fields('Nosuch') # => nil
287301 #
288302 def get_fields ( key )
289303 stringified_downcased_key = key . downcase . to_s
@@ -300,18 +314,27 @@ def get_fields(key)
300314 # ignores the +default_val+;
301315 # see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
302316 #
303- # req = Net::HTTP::Get.new(uri)
304- # req['Foo'] = 'bar'
305- # req.fetch('Foo') {|key| key.capitalize } # => "bar"
306- # req.fetch('Nosuch') {|key| key.capitalize } # => "Nosuch"
317+ # res = Net::HTTP.start(hostname) do |http|
318+ # http.get('/todos/1')
319+ # end
320+ #
321+ # # Field exists; block not called.
322+ # res.fetch('Connection') do |value|
323+ # fail 'Cannot happen'
324+ # end # => "keep-alive"
325+ #
326+ # # Field does not exist; block called.
327+ # res.fetch('Nosuch') do |value|
328+ # value.downcase
329+ # end # => "nosuch"
307330 #
308331 # With no block, returns the string value for +key+ if it exists;
309332 # otherwise, returns +default_val+ if it was given;
310333 # otherwise raises an exception:
311334 #
312- # req .fetch('Foo') # => "bar "
313- # req .fetch('Nosuch', :baz) # => :baz
314- # req .fetch('Nosuch') # Raises KeyError.
335+ # res .fetch('Connection', ' Foo') # => "keep-alive "
336+ # res .fetch('Nosuch', 'Foo') # => "Foo"
337+ # res .fetch('Nosuch') # Raises KeyError.
315338 #
316339 def fetch ( key , *args , &block ) #:yield: +key+
317340 a = @header . fetch ( key . downcase . to_s , *args , &block )
@@ -320,15 +343,20 @@ def fetch(key, *args, &block) #:yield: +key+
320343
321344 # Calls the block with each key/value pair:
322345 #
323- # req = Net::HTTP::Get.new(uri)
324- # req.each_header {|key, value| p [key, value] }
346+ # res = Net::HTTP.start(hostname) do |http|
347+ # http.get('/todos/1')
348+ # end
349+ # res.each_header do |key, value|
350+ # p [key, value] if key.start_with?('c')
351+ # end
325352 #
326353 # Output:
327354 #
328- # ["accept-encoding", "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]
329- # ["accept", "*/*"]
330- # ["user-agent", "Ruby"]
331- # ["host", "jsonplaceholder.typicode.com"]
355+ # ["content-type", "application/json; charset=utf-8"]
356+ # ["connection", "keep-alive"]
357+ # ["cache-control", "max-age=43200"]
358+ # ["cf-cache-status", "HIT"]
359+ # ["cf-ray", "771d17e9bc542cf5-ORD"]
332360 #
333361 # Returns an enumerator if no block is given.
334362 #
@@ -344,15 +372,20 @@ def each_header #:yield: +key+, +value+
344372
345373 # Calls the block with each field key:
346374 #
347- # req = Net::HTTP::Get.new(uri)
348- # req.each_key {|key| p key }
375+ # res = Net::HTTP.start(hostname) do |http|
376+ # http.get('/todos/1')
377+ # end
378+ # res.each_key do |key|
379+ # p key if key.start_with?('c')
380+ # end
349381 #
350382 # Output:
351383 #
352- # "accept-encoding"
353- # "accept"
354- # "user-agent"
355- # "host"
384+ # "content-type"
385+ # "connection"
386+ # "cache-control"
387+ # "cf-cache-status"
388+ # "cf-ray"
356389 #
357390 # Returns an enumerator if no block is given.
358391 #
@@ -366,15 +399,20 @@ def each_name(&block) #:yield: +key+
366399
367400 # Calls the block with each capitalized field name:
368401 #
369- # req = Net::HTTP::Get.new(uri)
370- # req.each_capitalized_name {|key| p key }
402+ # res = Net::HTTP.start(hostname) do |http|
403+ # http.get('/todos/1')
404+ # end
405+ # res.each_capitalized_name do |key|
406+ # p key if key.start_with?('C')
407+ # end
371408 #
372409 # Output:
373410 #
374- # "Accept-Encoding"
375- # "Accept"
376- # "User-Agent"
377- # "Host"
411+ # "Content-Type"
412+ # "Connection"
413+ # "Cache-Control"
414+ # "Cf-Cache-Status"
415+ # "Cf-Ray"
378416 #
379417 # The capitalization is system-dependent;
380418 # see {Case Mapping}[https://docs.ruby-lang.org/en/master/case_mapping_rdoc.html].
@@ -387,17 +425,20 @@ def each_capitalized_name #:yield: +key+
387425 end
388426 end
389427
390- # Calls the block with each field value:
428+ # Calls the block with each string field value:
391429 #
392- # req = Net::HTTP::Get.new(uri)
393- # req.each_value {|value| p value }
430+ # res = Net::HTTP.start(hostname) do |http|
431+ # http.get('/todos/1')
432+ # end
433+ # res.each_value do |value|
434+ # p value if value.start_with?('c')
435+ # end
394436 #
395437 # Output:
396438 #
397- # "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
398- # "*/*"
399- # "Ruby"
400- # "jsonplaceholder.typicode.com"
439+ # "chunked"
440+ # "cf-q-config;dur=6.0000002122251e-06"
441+ # "cloudflare"
401442 #
402443 # Returns an enumerator if no block is given.
403444 def each_value #:yield: +value+
@@ -465,6 +506,7 @@ def capitalize(name)
465506 # or +nil+ if there is no such field;
466507 # see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
467508 #
509+ # req = Net::HTTP::Get.new(uri)
468510 # req['Range'] = 'bytes=0-99,200-299,400-499'
469511 # req.range # => [0..99, 200..299, 400..499]
470512 # req.delete('Range')
@@ -522,6 +564,7 @@ def range
522564 #
523565 # With argument +length+:
524566 #
567+ # req = Net::HTTP::Get.new(uri)
525568 # req.set_range(100) # => 100
526569 # req['Range'] # => "bytes=0-99"
527570 #
0 commit comments