126
126
# Various convenience methods retrieve values, set values, query values,
127
127
# set form values, or iterate over fields.
128
128
#
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
+ #
129
147
# === Getters
130
148
#
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.
132
155
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
133
156
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
134
157
# - #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+.
135
160
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
136
161
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
137
162
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
138
163
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
139
164
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
140
165
#
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
- #
150
166
# === Queries
151
167
#
152
168
# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
153
169
# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
154
170
# - #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.
161
172
#
162
173
# === Iterators
163
174
#
164
175
# - #each_capitalized: Passes each field capitalized-name/value pair to the block.
165
176
# - #each_capitalized_name: Passes each capitalized field name to the block.
166
177
# - #each_header: Passes each field name/value pair to the block.
167
178
# - #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.
169
180
#
170
181
module Net ::HTTPHeader
171
182
@@ -196,12 +207,14 @@ def size #:nodoc: obsolete
196
207
# or +nil+ if there is no such key;
197
208
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
198
209
#
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
204
215
#
216
+ # Note that some field values may be retrieved via convenience methods;
217
+ # see {Getters}[rdoc-ref:Net::HTTPHeader@Getters].
205
218
def []( key )
206
219
a = @header [ key . downcase . to_s ] or return nil
207
220
a . join ( ', ' )
@@ -216,6 +229,8 @@ def [](key)
216
229
# req['Accept'] = 'text/html'
217
230
# req['Accept'] # => "text/html"
218
231
#
232
+ # Note that some field values may be set via convenience methods;
233
+ # see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
219
234
def []=( key , val )
220
235
unless val
221
236
@header . delete key . downcase . to_s
@@ -278,12 +293,11 @@ def add_field(key, val)
278
293
# or +nil+ if there is no such field;
279
294
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
280
295
#
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
287
301
#
288
302
def get_fields ( key )
289
303
stringified_downcased_key = key . downcase . to_s
@@ -300,18 +314,27 @@ def get_fields(key)
300
314
# ignores the +default_val+;
301
315
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
302
316
#
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"
307
330
#
308
331
# With no block, returns the string value for +key+ if it exists;
309
332
# otherwise, returns +default_val+ if it was given;
310
333
# otherwise raises an exception:
311
334
#
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.
315
338
#
316
339
def fetch ( key , *args , &block ) #:yield: +key+
317
340
a = @header . fetch ( key . downcase . to_s , *args , &block )
@@ -320,15 +343,20 @@ def fetch(key, *args, &block) #:yield: +key+
320
343
321
344
# Calls the block with each key/value pair:
322
345
#
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
325
352
#
326
353
# Output:
327
354
#
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"]
332
360
#
333
361
# Returns an enumerator if no block is given.
334
362
#
@@ -344,15 +372,20 @@ def each_header #:yield: +key+, +value+
344
372
345
373
# Calls the block with each field key:
346
374
#
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
349
381
#
350
382
# Output:
351
383
#
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"
356
389
#
357
390
# Returns an enumerator if no block is given.
358
391
#
@@ -366,15 +399,20 @@ def each_name(&block) #:yield: +key+
366
399
367
400
# Calls the block with each capitalized field name:
368
401
#
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
371
408
#
372
409
# Output:
373
410
#
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"
378
416
#
379
417
# The capitalization is system-dependent;
380
418
# see {Case Mapping}[https://docs.ruby-lang.org/en/master/case_mapping_rdoc.html].
@@ -387,17 +425,20 @@ def each_capitalized_name #:yield: +key+
387
425
end
388
426
end
389
427
390
- # Calls the block with each field value:
428
+ # Calls the block with each string field value:
391
429
#
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
394
436
#
395
437
# Output:
396
438
#
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"
401
442
#
402
443
# Returns an enumerator if no block is given.
403
444
def each_value #:yield: +value+
@@ -465,6 +506,7 @@ def capitalize(name)
465
506
# or +nil+ if there is no such field;
466
507
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
467
508
#
509
+ # req = Net::HTTP::Get.new(uri)
468
510
# req['Range'] = 'bytes=0-99,200-299,400-499'
469
511
# req.range # => [0..99, 200..299, 400..499]
470
512
# req.delete('Range')
@@ -522,6 +564,7 @@ def range
522
564
#
523
565
# With argument +length+:
524
566
#
567
+ # req = Net::HTTP::Get.new(uri)
525
568
# req.set_range(100) # => 100
526
569
# req['Range'] # => "bytes=0-99"
527
570
#
0 commit comments