You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Various convenience methods retrieve values, set values, query values,
111
+
# set form values, or iterate over fields.
112
+
#
113
+
# === Getters
114
+
#
115
+
# - #[]: Returns the string value for the given field.
116
+
# - #content_length: Returns the integer value of field +:content-length+.
117
+
# - #content_range: Returns the Range value of field +:content-range+.
118
+
# - #content_type: Returns the string value of field +:content-type+.
119
+
# - #main_type: Returns first part of the string value of field +:content-type+.
120
+
# - #sub_type: Returns second part of the string value of field +:content-type+.
121
+
# - #range: Returns an array of Range objects, or +nil+.
122
+
# - #range_length: Returns the integer length of the range given in field +:range+.
123
+
# - #type_params: Returns the string parameters for +:content-type+.
124
+
#
125
+
# === Setters
126
+
#
127
+
# - #[]=: Sets the string or array value for the given field.
128
+
# - #basic_auth: Sets the string authorization header for +:Basic+ authorization.
129
+
# - #content_length=: Sets the integer length for field +:content-length+.
130
+
# - #content_type=: Sets the string value for field +:content-type+.
131
+
# - #proxy_basic_auth: Set Proxy-Authorization: header for “Basic” authorization.
132
+
# - #range=: Sets the HTTP Range: header. Accepts either a Range object as a single argument, or a beginning index and a length from that index. Example:
133
+
#
134
+
# === Queries
135
+
#
136
+
# - #chunked?: Returns whether field +:transfer-encoding+ is set to <tt>'chunked'</tt>.
137
+
# - #connection_close?: Returns whether field +:connection+ is set to <tt>'close'</tt>.
138
+
# - #connection_keep_alive?: Returns whether field +:connection+ is set to <tt>'keep-alive'</tt>.
139
+
# - #key?: Returns whether a given field exists.
140
+
#
141
+
# === Form Setters
142
+
#
143
+
# - #set_form: Sets an HTML form data set.
144
+
# - #set_form_data: Set header fields and a body from HTML form data.
145
+
#
146
+
# === Iterators
147
+
#
148
+
# - #each_capitalized: Passes each field capitalized-name/value pair to the block.
149
+
# - #each_capitalized_name: Passes each capitalized field name to the block.
150
+
# - #each_header: Passes each field name/value pair to the block.
151
+
# - #each_name: Passes each field name to the block.
152
+
# - #each_value: Passes each field value to the block.
10
153
#
11
154
moduleNet::HTTPHeader
12
155
13
-
definitialize_http_header(initheader)
156
+
definitialize_http_header(initheader)#:nodoc:
14
157
@header={}
15
158
returnunlessinitheader
16
159
initheader.eachdo |key,value|
@@ -33,14 +176,30 @@ def size #:nodoc: obsolete
33
176
34
177
aliaslengthsize#:nodoc: obsolete
35
178
36
-
# Returns the header field corresponding to the case-insensitive key.
37
-
# For example, a key of "Content-Type" might return "text/html"
179
+
# Returns the string field value for the case-insensitive field +key+,
180
+
# or +nil+ if there is no such key;
181
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
182
+
#
183
+
# req = Net::HTTP::Get.new(uri)
184
+
# req[:accept] # => "*/*"
185
+
# req[:foo] = %w[bar baz bat]
186
+
# req[:foo] # => "bar, baz, bat"
187
+
# res[:nosuch] # => nil
188
+
#
38
189
def [](key)
39
190
a=@header[key.downcase.to_s]orreturnnil
40
191
a.join(', ')
41
192
end
42
193
43
-
# Sets the header field corresponding to the case-insensitive key.
194
+
# Sets the value for the case-insensitive +key+ to +val+,
195
+
# overwriting the previous value if the field exists;
196
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
197
+
#
198
+
# req = Net::HTTP::Get.new(uri)
199
+
# req[:accept] # => "*/*"
200
+
# req[:accept] = 'text/html'
201
+
# req[:accept] # => "text/html"
202
+
#
44
203
def []=(key,val)
45
204
unlessval
46
205
@header.deletekey.downcase.to_s
@@ -49,20 +208,18 @@ def []=(key, val)
49
208
set_field(key,val)
50
209
end
51
210
52
-
# [Ruby 1.8.3]
53
-
# Adds a value to a named header field, instead of replacing its value.
54
-
# Second argument +val+ must be a String.
55
-
# See also #[]=, #[] and #get_fields.
211
+
# Adds value +val+ to the value array for field +key+ if the field exists;
212
+
# creates the field with the given +key+ and +val+ if it does not exist.
213
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
56
214
#
57
-
# request.add_field 'X-My-Header', 'a'
58
-
# p request['X-My-Header'] #=> "a"
59
-
# p request.get_fields('X-My-Header') #=> ["a"]
60
-
# request.add_field 'X-My-Header', 'b'
61
-
# p request['X-My-Header'] #=> "a, b"
62
-
# p request.get_fields('X-My-Header') #=> ["a", "b"]
63
-
# request.add_field 'X-My-Header', 'c'
64
-
# p request['X-My-Header'] #=> "a, b, c"
65
-
# p request.get_fields('X-My-Header') #=> ["a", "b", "c"]
0 commit comments