@@ -207,9 +207,7 @@ def size #:nodoc: obsolete
207
207
# or +nil+ if there is no such key;
208
208
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
209
209
#
210
- # res = Net::HTTP.start(hostname) do |http|
211
- # http.get('/todos/1')
212
- # end
210
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
213
211
# res['Connection'] # => "keep-alive"
214
212
# res['Nosuch'] # => nil
215
213
#
@@ -293,9 +291,7 @@ def add_field(key, val)
293
291
# or +nil+ if there is no such field;
294
292
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
295
293
#
296
- # res = Net::HTTP.start(hostname) do |http|
297
- # http.get('/todos/1')
298
- # end
294
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
299
295
# res.get_fields('Connection') # => ["keep-alive"]
300
296
# res.get_fields('Nosuch') # => nil
301
297
#
@@ -314,9 +310,7 @@ def get_fields(key)
314
310
# ignores the +default_val+;
315
311
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
316
312
#
317
- # res = Net::HTTP.start(hostname) do |http|
318
- # http.get('/todos/1')
319
- # end
313
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
320
314
#
321
315
# # Field exists; block not called.
322
316
# res.fetch('Connection') do |value|
@@ -343,9 +337,7 @@ def fetch(key, *args, &block) #:yield: +key+
343
337
344
338
# Calls the block with each key/value pair:
345
339
#
346
- # res = Net::HTTP.start(hostname) do |http|
347
- # http.get('/todos/1')
348
- # end
340
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
349
341
# res.each_header do |key, value|
350
342
# p [key, value] if key.start_with?('c')
351
343
# end
@@ -372,20 +364,18 @@ def each_header #:yield: +key+, +value+
372
364
373
365
# Calls the block with each field key:
374
366
#
375
- # res = Net::HTTP.start(hostname) do |http|
376
- # http.get('/todos/1')
377
- # end
367
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
378
368
# res.each_key do |key|
379
369
# p key if key.start_with?('c')
380
370
# end
381
371
#
382
372
# Output:
383
373
#
384
- # "content-type"
385
- # "connection"
386
- # "cache-control"
387
- # "cf-cache-status"
388
- # "cf-ray"
374
+ # "content-type"
375
+ # "connection"
376
+ # "cache-control"
377
+ # "cf-cache-status"
378
+ # "cf-ray"
389
379
#
390
380
# Returns an enumerator if no block is given.
391
381
#
@@ -399,9 +389,7 @@ def each_name(&block) #:yield: +key+
399
389
400
390
# Calls the block with each capitalized field name:
401
391
#
402
- # res = Net::HTTP.start(hostname) do |http|
403
- # http.get('/todos/1')
404
- # end
392
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
405
393
# res.each_capitalized_name do |key|
406
394
# p key if key.start_with?('C')
407
395
# end
@@ -427,9 +415,7 @@ def each_capitalized_name #:yield: +key+
427
415
428
416
# Calls the block with each string field value:
429
417
#
430
- # res = Net::HTTP.start(hostname) do |http|
431
- # http.get('/todos/1')
432
- # end
418
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
433
419
# res.each_value do |value|
434
420
# p value if value.start_with?('c')
435
421
# end
@@ -610,15 +596,36 @@ def set_range(r, e = nil)
610
596
611
597
alias range = set_range
612
598
613
- # Returns an Integer object which represents the HTTP Content-Length:
614
- # header field, or +nil+ if that field was not provided.
599
+ # Returns the value of field <tt>'Content-Length'</tt> as an integer,
600
+ # or +nil+ if there is no such field;
601
+ # see {Content-Length request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-request-header]:
602
+ #
603
+ # res = Net::HTTP.get_response(hostname, '/nosuch/1')
604
+ # res.content_length # => 2
605
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
606
+ # res.content_length # => nil
607
+ #
615
608
def content_length
616
609
return nil unless key? ( 'Content-Length' )
617
610
len = self [ 'Content-Length' ] . slice ( /\d +/ ) or
618
611
raise Net ::HTTPHeaderSyntaxError , 'wrong Content-Length format'
619
612
len . to_i
620
613
end
621
614
615
+ # Sets the value of field <tt>'Content-Length'</tt> to the given numeric;
616
+ # see {Content-Length response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-response-header]:
617
+ #
618
+ # _uri = uri.dup
619
+ # hostname = _uri.hostname # => "jsonplaceholder.typicode.com"
620
+ # _uri.path = '/posts' # => "/posts"
621
+ # req = Net::HTTP::Post.new(_uri) # => #<Net::HTTP::Post POST>
622
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
623
+ # req.content_length = req.body.size # => 42
624
+ # req.content_type = 'application/json'
625
+ # res = Net::HTTP.start(hostname) do |http|
626
+ # http.request(req)
627
+ # end # => #<Net::HTTPCreated 201 Created readbody=true>
628
+ #
622
629
def content_length = ( len )
623
630
unless len
624
631
@header . delete 'content-length'
@@ -627,20 +634,31 @@ def content_length=(len)
627
634
@header [ 'content-length' ] = [ len . to_i . to_s ]
628
635
end
629
636
630
- # Returns "true" if the "transfer-encoding" header is present and
631
- # set to "chunked". This is an HTTP/1.1 feature, allowing
632
- # the content to be sent in "chunks" without at the outset
633
- # stating the entire content length.
637
+ # Returns +true+ if field <tt>'Transfer-Encoding'</tt>
638
+ # exists and has value <tt>'chunked'</tt>,
639
+ # +false+ otherwise;
640
+ # see {Transfer-Encoding response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#transfer-encoding-response-header]:
641
+ #
642
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
643
+ # res['Transfer-Encoding'] # => "chunked"
644
+ # res.chunked? # => true
645
+ #
634
646
def chunked?
635
647
return false unless @header [ 'transfer-encoding' ]
636
648
field = self [ 'Transfer-Encoding' ]
637
649
( /(?:\A |[^\- \w ])chunked(?![\- \w ])/i =~ field ) ? true : false
638
650
end
639
651
640
- # Returns a Range object which represents the value of the Content-Range:
641
- # header field.
642
- # For a partial entity body, this indicates where this fragment
643
- # fits inside the full entity body, as range of byte offsets.
652
+ # Returns a Range object representing the value of field
653
+ # <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
654
+ # see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
655
+ #
656
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
657
+ # res['Content-Range'] # => nil
658
+ # res['Content-Range'] = 'bytes 0-499/1000'
659
+ # res['Content-Range'] # => "bytes 0-499/1000"
660
+ # res.content_range # => 0..499
661
+ #
644
662
def content_range
645
663
return nil unless @header [ 'content-range' ]
646
664
m = %r<\A \s *(\w +)\s +(\d +)-(\d +)/(\d +|\* )> . match ( self [ 'Content-Range' ] ) or
@@ -649,14 +667,29 @@ def content_range
649
667
m [ 2 ] . to_i .. m [ 3 ] . to_i
650
668
end
651
669
652
- # The length of the range represented in Content-Range: header.
670
+ # Returns the integer representing length of the value of field
671
+ # <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
672
+ # see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
673
+ #
674
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
675
+ # res['Content-Range'] # => nil
676
+ # res['Content-Range'] = 'bytes 0-499/1000'
677
+ # res.range_length # => 500
678
+ #
653
679
def range_length
654
680
r = content_range ( ) or return nil
655
681
r . end - r . begin + 1
656
682
end
657
683
658
- # Returns a content type string such as "text/html".
659
- # This method returns nil if Content-Type: header field does not exist.
684
+ # Returns the {media type}[https://en.wikipedia.org/wiki/Media_type]
685
+ # from the value of field <tt>'Content-Type'</tt>,
686
+ # or +nil+ if no such field exists;
687
+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
688
+ #
689
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
690
+ # res['content-type'] # => "application/json; charset=utf-8"
691
+ # res.content_type # => "application/json"
692
+ #
660
693
def content_type
661
694
return nil unless main_type ( )
662
695
if sub_type ( )
@@ -665,26 +698,46 @@ def content_type
665
698
end
666
699
end
667
700
668
- # Returns a content type string such as "text".
669
- # This method returns nil if Content-Type: header field does not exist.
701
+ # Returns the leading ('type') part of the
702
+ # {media type}[https://en.wikipedia.org/wiki/Media_type]
703
+ # from the value of field <tt>'Content-Type'</tt>,
704
+ # or +nil+ if no such field exists;
705
+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
706
+ #
707
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
708
+ # res['content-type'] # => "application/json; charset=utf-8"
709
+ # res.main_type # => "application"
710
+ #
670
711
def main_type
671
712
return nil unless @header [ 'content-type' ]
672
713
self [ 'Content-Type' ] . split ( ';' ) . first . to_s . split ( '/' ) [ 0 ] . to_s . strip
673
714
end
674
715
675
- # Returns a content type string such as "html".
676
- # This method returns nil if Content-Type: header field does not exist
677
- # or sub-type is not given (e.g. "Content-Type: text").
716
+ # Returns the trailing ('subtype') part of the
717
+ # {media type}[https://en.wikipedia.org/wiki/Media_type]
718
+ # from the value of field <tt>'Content-Type'</tt>,
719
+ # or +nil+ if no such field exists;
720
+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
721
+ #
722
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
723
+ # res['content-type'] # => "application/json; charset=utf-8"
724
+ # res.sub_type # => "json"
725
+ #
678
726
def sub_type
679
727
return nil unless @header [ 'content-type' ]
680
728
_ , sub = *self [ 'Content-Type' ] . split ( ';' ) . first . to_s . split ( '/' )
681
729
return nil unless sub
682
730
sub . strip
683
731
end
684
732
685
- # Any parameters specified for the content type, returned as a Hash.
686
- # For example, a header of Content-Type: text/html; charset=EUC-JP
687
- # would result in type_params returning {'charset' => 'EUC-JP'}
733
+ # Returns the trailing ('parameters') part of the value of field <tt>'Content-Type'</tt>,
734
+ # or +nil+ if no such field exists;
735
+ # see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
736
+ #
737
+ # res = Net::HTTP.get_response(hostname, '/todos/1')
738
+ # res['content-type'] # => "application/json; charset=utf-8"
739
+ # res.type_params # => {"charset"=>"utf-8"}
740
+ #
688
741
def type_params
689
742
result = { }
690
743
list = self [ 'Content-Type' ] . to_s . split ( ';' )
@@ -696,10 +749,12 @@ def type_params
696
749
result
697
750
end
698
751
699
- # Sets the content type in an HTTP header.
700
- # The +type+ should be a full HTTP content type, e.g. "text/html".
701
- # The +params+ are an optional Hash of parameters to add after the
702
- # content type, e.g. {'charset' => 'iso-8859-1'}.
752
+ # Sets the value of field <tt>'Content-Type'</tt>;
753
+ # returns the new value;
754
+ # see {Content-Type request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-request-header]:
755
+ #
756
+ # req = Net::HTTP::Get.new(uri)
757
+ # req.set_content_type('application/json') # => ["application/json"]
703
758
#
704
759
# Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
705
760
def set_content_type ( type , params = { } )
0 commit comments