net-http 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/doc/net-http/examples.rdoc +30 -0
- data/lib/net/http/header.rb +381 -73
- data/lib/net/http/request.rb +27 -5
- data/lib/net/http/requests.rb +296 -24
- data/lib/net/http/response.rb +127 -11
- data/lib/net/http.rb +311 -295
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76efd58d8b7892a8c7cd99b94ba163b3e7af2c7c213710d09d4f0ff5751fb01c
|
4
|
+
data.tar.gz: 69b629454de4153eac43b49c1e622889a69a90b8b574be7a91af0668240c7b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf1fe9f92f33a2b2c32ae4ca1c896076ab22df06551b7e669a21c616a2c89d36942a18a3520d3ecb3fd5db387481ac44abbd7c70dc8b39bce4987a681819ce1f
|
7
|
+
data.tar.gz: 143b8dc3a5f8788c552e17ac45ee20ed0313e16bccb16de94d9a22d96b69a23e24d264c2978388b2b01e7f43d84834d027c65c901e3ed6d8fd668763eb422375
|
data/.gitignore
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
Examples here assume that <tt>net/http</tt> has been required
|
2
|
+
(which also requires +uri+):
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
Many code examples here use these example websites:
|
7
|
+
|
8
|
+
- https://jsonplaceholder.typicode.com.
|
9
|
+
- http://example.com.
|
10
|
+
|
11
|
+
Some examples also assume these variables:
|
12
|
+
|
13
|
+
uri = URI('https://jsonplaceholder.typicode.com')
|
14
|
+
uri.freeze # Examples may not modify.
|
15
|
+
hostname = uri.hostname # => "jsonplaceholder.typicode.com"
|
16
|
+
port = uri.port # => 443
|
17
|
+
|
18
|
+
So that example requests may be written as:
|
19
|
+
|
20
|
+
Net::HTTP.get(uri)
|
21
|
+
Net::HTTP.get(hostname, '/index.html')
|
22
|
+
Net::HTTP.start(hostname) do |http|
|
23
|
+
http.get('/todos/1')
|
24
|
+
http.get('/todos/2')
|
25
|
+
end
|
26
|
+
|
27
|
+
An example that needs a modified URI first duplicates +uri+, then modifies the duplicate:
|
28
|
+
|
29
|
+
_uri = uri.dup
|
30
|
+
_uri.path = '/todos/1'
|
data/lib/net/http/header.rb
CHANGED
@@ -1,16 +1,186 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
|
-
# The HTTPHeader module defines methods for reading and writing
|
3
|
-
# HTTP headers.
|
4
2
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
3
|
+
# The \HTTPHeader module provides access to \HTTP headers.
|
4
|
+
#
|
5
|
+
# The module is included in:
|
6
|
+
#
|
7
|
+
# - Net::HTTPGenericRequest (and therefore Net::HTTPRequest).
|
8
|
+
# - Net::HTTPResponse.
|
9
|
+
#
|
10
|
+
# The headers are a hash-like collection of key/value pairs called _fields_.
|
11
|
+
#
|
12
|
+
# == Request and Response Fields
|
13
|
+
#
|
14
|
+
# Headers may be included in:
|
15
|
+
#
|
16
|
+
# - A Net::HTTPRequest object:
|
17
|
+
# the object's headers will be sent with the request.
|
18
|
+
# Any fields may be defined in the request;
|
19
|
+
# see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
|
20
|
+
# - A Net::HTTPResponse object:
|
21
|
+
# the objects headers are usually those returned from the host.
|
22
|
+
# Fields may be retrieved from the object;
|
23
|
+
# see {Getters}[rdoc-ref:Net::HTTPHeader@Getters]
|
24
|
+
# and {Iterators}[rdoc-ref:Net::HTTPHeader@Iterators].
|
25
|
+
#
|
26
|
+
# Exactly which fields should be sent or expected depends on the host;
|
27
|
+
# see:
|
28
|
+
#
|
29
|
+
# - {Request fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
|
30
|
+
# - {Response fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields].
|
31
|
+
#
|
32
|
+
# == About the Examples
|
33
|
+
#
|
34
|
+
# :include: doc/net-http/examples.rdoc
|
35
|
+
#
|
36
|
+
# == Fields
|
37
|
+
#
|
38
|
+
# A header field is a key/value pair.
|
39
|
+
#
|
40
|
+
# === Field Keys
|
41
|
+
#
|
42
|
+
# A field key may be:
|
43
|
+
#
|
44
|
+
# - A string: Key <tt>'Accept'</tt> is treated as if it were
|
45
|
+
# <tt>'Accept'.downcase</tt>; i.e., <tt>'accept'</tt>.
|
46
|
+
# - A symbol: Key <tt>:Accept</tt> is treated as if it were
|
47
|
+
# <tt>:Accept.to_s.downcase</tt>; i.e., <tt>'accept'</tt>.
|
48
|
+
#
|
49
|
+
# Examples:
|
50
|
+
#
|
51
|
+
# req = Net::HTTP::Get.new(uri)
|
52
|
+
# req[:accept] # => "*/*"
|
53
|
+
# req['Accept'] # => "*/*"
|
54
|
+
# req['ACCEPT'] # => "*/*"
|
55
|
+
#
|
56
|
+
# req['accept'] = 'text/html'
|
57
|
+
# req[:accept] = 'text/html'
|
58
|
+
# req['ACCEPT'] = 'text/html'
|
59
|
+
#
|
60
|
+
# === Field Values
|
61
|
+
#
|
62
|
+
# A field value may be returned as an array of strings or as a string:
|
63
|
+
#
|
64
|
+
# - These methods return field values as arrays:
|
65
|
+
#
|
66
|
+
# - #get_fields: Returns the array value for the given key,
|
67
|
+
# or +nil+ if it does not exist.
|
68
|
+
# - #to_hash: Returns a hash of all header fields:
|
69
|
+
# each key is a field name; its value is the array value for the field.
|
70
|
+
#
|
71
|
+
# - These methods return field values as string;
|
72
|
+
# the string value for a field is equivalent to
|
73
|
+
# <tt>self[key.downcase.to_s].join(', '))</tt>:
|
74
|
+
#
|
75
|
+
# - #[]: Returns the string value for the given key,
|
76
|
+
# or +nil+ if it does not exist.
|
77
|
+
# - #fetch: Like #[], but accepts a default value
|
78
|
+
# to be returned if the key does not exist.
|
79
|
+
#
|
80
|
+
# The field value may be set:
|
81
|
+
#
|
82
|
+
# - #[]=: Sets the value for the given key;
|
83
|
+
# the given value may be a string, a symbol, an array, or a hash.
|
84
|
+
# - #add_field: Adds a given value to a value for the given key
|
85
|
+
# (not overwriting the existing value).
|
86
|
+
# - #delete: Deletes the field for the given key.
|
87
|
+
#
|
88
|
+
# Example field values:
|
89
|
+
#
|
90
|
+
# - \String:
|
91
|
+
#
|
92
|
+
# req['Accept'] = 'text/html' # => "text/html"
|
93
|
+
# req['Accept'] # => "text/html"
|
94
|
+
# req.get_fields('Accept') # => ["text/html"]
|
95
|
+
#
|
96
|
+
# - \Symbol:
|
97
|
+
#
|
98
|
+
# req['Accept'] = :text # => :text
|
99
|
+
# req['Accept'] # => "text"
|
100
|
+
# req.get_fields('Accept') # => ["text"]
|
101
|
+
#
|
102
|
+
# - Simple array:
|
103
|
+
#
|
104
|
+
# req[:foo] = %w[bar baz bat]
|
105
|
+
# req[:foo] # => "bar, baz, bat"
|
106
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat"]
|
107
|
+
#
|
108
|
+
# - Simple hash:
|
109
|
+
#
|
110
|
+
# req[:foo] = {bar: 0, baz: 1, bat: 2}
|
111
|
+
# req[:foo] # => "bar, 0, baz, 1, bat, 2"
|
112
|
+
# req.get_fields(:foo) # => ["bar", "0", "baz", "1", "bat", "2"]
|
113
|
+
#
|
114
|
+
# - Nested:
|
115
|
+
#
|
116
|
+
# req[:foo] = [%w[bar baz], {bat: 0, bam: 1}]
|
117
|
+
# req[:foo] # => "bar, baz, bat, 0, bam, 1"
|
118
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat", "0", "bam", "1"]
|
119
|
+
#
|
120
|
+
# req[:foo] = {bar: %w[baz bat], bam: {bah: 0, bad: 1}}
|
121
|
+
# req[:foo] # => "bar, baz, bat, bam, bah, 0, bad, 1"
|
122
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat", "bam", "bah", "0", "bad", "1"]
|
123
|
+
#
|
124
|
+
# == Convenience Methods
|
125
|
+
#
|
126
|
+
# Various convenience methods retrieve values, set values, query values,
|
127
|
+
# set form values, or iterate over fields.
|
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
|
+
#
|
147
|
+
# === Getters
|
148
|
+
#
|
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.
|
155
|
+
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
|
156
|
+
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
|
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+.
|
160
|
+
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
|
161
|
+
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
|
162
|
+
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
|
163
|
+
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
|
164
|
+
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
|
165
|
+
#
|
166
|
+
# === Queries
|
167
|
+
#
|
168
|
+
# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
|
169
|
+
# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
|
170
|
+
# - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>.
|
171
|
+
# - #key?: Returns whether a given key exists.
|
172
|
+
#
|
173
|
+
# === Iterators
|
174
|
+
#
|
175
|
+
# - #each_capitalized: Passes each field capitalized-name/value pair to the block.
|
176
|
+
# - #each_capitalized_name: Passes each capitalized field name to the block.
|
177
|
+
# - #each_header: Passes each field name/value pair to the block.
|
178
|
+
# - #each_name: Passes each field name to the block.
|
179
|
+
# - #each_value: Passes each string field value to the block.
|
10
180
|
#
|
11
181
|
module Net::HTTPHeader
|
12
182
|
|
13
|
-
def initialize_http_header(initheader)
|
183
|
+
def initialize_http_header(initheader) #:nodoc:
|
14
184
|
@header = {}
|
15
185
|
return unless initheader
|
16
186
|
initheader.each do |key, value|
|
@@ -33,14 +203,34 @@ module Net::HTTPHeader
|
|
33
203
|
|
34
204
|
alias length size #:nodoc: obsolete
|
35
205
|
|
36
|
-
# Returns the
|
37
|
-
#
|
206
|
+
# Returns the string field value for the case-insensitive field +key+,
|
207
|
+
# or +nil+ if there is no such key;
|
208
|
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
|
209
|
+
#
|
210
|
+
# res = Net::HTTP.start(hostname) do |http|
|
211
|
+
# http.get('/todos/1')
|
212
|
+
# end
|
213
|
+
# res['Connection'] # => "keep-alive"
|
214
|
+
# res['Nosuch'] # => nil
|
215
|
+
#
|
216
|
+
# Note that some field values may be retrieved via convenience methods;
|
217
|
+
# see {Getters}[rdoc-ref:Net::HTTPHeader@Getters].
|
38
218
|
def [](key)
|
39
219
|
a = @header[key.downcase.to_s] or return nil
|
40
220
|
a.join(', ')
|
41
221
|
end
|
42
222
|
|
43
|
-
# Sets the
|
223
|
+
# Sets the value for the case-insensitive +key+ to +val+,
|
224
|
+
# overwriting the previous value if the field exists;
|
225
|
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
|
226
|
+
#
|
227
|
+
# req = Net::HTTP::Get.new(uri)
|
228
|
+
# req['Accept'] # => "*/*"
|
229
|
+
# req['Accept'] = 'text/html'
|
230
|
+
# req['Accept'] # => "text/html"
|
231
|
+
#
|
232
|
+
# Note that some field values may be set via convenience methods;
|
233
|
+
# see {Setters}[rdoc-ref:Net::HTTPHeader@Setters].
|
44
234
|
def []=(key, val)
|
45
235
|
unless val
|
46
236
|
@header.delete key.downcase.to_s
|
@@ -49,20 +239,18 @@ module Net::HTTPHeader
|
|
49
239
|
set_field(key, val)
|
50
240
|
end
|
51
241
|
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
# See also #[]=, #[] and #get_fields.
|
242
|
+
# Adds value +val+ to the value array for field +key+ if the field exists;
|
243
|
+
# creates the field with the given +key+ and +val+ if it does not exist.
|
244
|
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
|
56
245
|
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
# p request.get_fields('X-My-Header') #=> ["a", "b", "c"]
|
246
|
+
# req = Net::HTTP::Get.new(uri)
|
247
|
+
# req.add_field('Foo', 'bar')
|
248
|
+
# req['Foo'] # => "bar"
|
249
|
+
# req.add_field('Foo', 'baz')
|
250
|
+
# req['Foo'] # => "bar, baz"
|
251
|
+
# req.add_field('Foo', %w[baz bam])
|
252
|
+
# req['Foo'] # => "bar, baz, baz, bam"
|
253
|
+
# req.get_fields('Foo') # => ["bar", "baz", "baz", "bam"]
|
66
254
|
#
|
67
255
|
def add_field(key, val)
|
68
256
|
stringified_downcased_key = key.downcase.to_s
|
@@ -101,16 +289,15 @@ module Net::HTTPHeader
|
|
101
289
|
end
|
102
290
|
end
|
103
291
|
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
# header fields without any processing. See also #[].
|
292
|
+
# Returns the array field value for the given +key+,
|
293
|
+
# or +nil+ if there is no such field;
|
294
|
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
|
108
295
|
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
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
|
114
301
|
#
|
115
302
|
def get_fields(key)
|
116
303
|
stringified_downcased_key = key.downcase.to_s
|
@@ -118,24 +305,62 @@ module Net::HTTPHeader
|
|
118
305
|
@header[stringified_downcased_key].dup
|
119
306
|
end
|
120
307
|
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
308
|
+
# :call-seq
|
309
|
+
# fetch(key, default_val = nil) {|key| ... } -> object
|
310
|
+
# fetch(key, default_val = nil) -> value or default_val
|
311
|
+
#
|
312
|
+
# With a block, returns the string value for +key+ if it exists;
|
313
|
+
# otherwise returns the value of the block;
|
314
|
+
# ignores the +default_val+;
|
315
|
+
# see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]:
|
316
|
+
#
|
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"
|
330
|
+
#
|
331
|
+
# With no block, returns the string value for +key+ if it exists;
|
332
|
+
# otherwise, returns +default_val+ if it was given;
|
333
|
+
# otherwise raises an exception:
|
334
|
+
#
|
335
|
+
# res.fetch('Connection', 'Foo') # => "keep-alive"
|
336
|
+
# res.fetch('Nosuch', 'Foo') # => "Foo"
|
337
|
+
# res.fetch('Nosuch') # Raises KeyError.
|
338
|
+
#
|
125
339
|
def fetch(key, *args, &block) #:yield: +key+
|
126
340
|
a = @header.fetch(key.downcase.to_s, *args, &block)
|
127
341
|
a.kind_of?(Array) ? a.join(', ') : a
|
128
342
|
end
|
129
343
|
|
130
|
-
#
|
131
|
-
# and value to the code block supplied.
|
344
|
+
# Calls the block with each key/value pair:
|
132
345
|
#
|
133
|
-
#
|
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
|
134
352
|
#
|
135
|
-
#
|
353
|
+
# Output:
|
354
|
+
#
|
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"]
|
136
360
|
#
|
137
|
-
#
|
361
|
+
# Returns an enumerator if no block is given.
|
138
362
|
#
|
363
|
+
# Net::HTTPHeader#each is an alias for Net::HTTPHeader#each_header.
|
139
364
|
def each_header #:yield: +key+, +value+
|
140
365
|
block_given? or return enum_for(__method__) { @header.size }
|
141
366
|
@header.each do |k,va|
|
@@ -145,10 +370,26 @@ module Net::HTTPHeader
|
|
145
370
|
|
146
371
|
alias each each_header
|
147
372
|
|
148
|
-
#
|
149
|
-
#
|
373
|
+
# Calls the block with each field key:
|
374
|
+
#
|
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
|
381
|
+
#
|
382
|
+
# Output:
|
383
|
+
#
|
384
|
+
# "content-type"
|
385
|
+
# "connection"
|
386
|
+
# "cache-control"
|
387
|
+
# "cf-cache-status"
|
388
|
+
# "cf-ray"
|
150
389
|
#
|
151
390
|
# Returns an enumerator if no block is given.
|
391
|
+
#
|
392
|
+
# Net::HTTPHeader#each_name is an alias for Net::HTTPHeader#each_key.
|
152
393
|
def each_name(&block) #:yield: +key+
|
153
394
|
block_given? or return enum_for(__method__) { @header.size }
|
154
395
|
@header.each_key(&block)
|
@@ -156,12 +397,25 @@ module Net::HTTPHeader
|
|
156
397
|
|
157
398
|
alias each_key each_name
|
158
399
|
|
159
|
-
#
|
160
|
-
#
|
400
|
+
# Calls the block with each capitalized field name:
|
401
|
+
#
|
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
|
161
408
|
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
409
|
+
# Output:
|
410
|
+
#
|
411
|
+
# "Content-Type"
|
412
|
+
# "Connection"
|
413
|
+
# "Cache-Control"
|
414
|
+
# "Cf-Cache-Status"
|
415
|
+
# "Cf-Ray"
|
416
|
+
#
|
417
|
+
# The capitalization is system-dependent;
|
418
|
+
# see {Case Mapping}[https://docs.ruby-lang.org/en/master/case_mapping_rdoc.html].
|
165
419
|
#
|
166
420
|
# Returns an enumerator if no block is given.
|
167
421
|
def each_capitalized_name #:yield: +key+
|
@@ -171,8 +425,20 @@ module Net::HTTPHeader
|
|
171
425
|
end
|
172
426
|
end
|
173
427
|
|
174
|
-
#
|
175
|
-
#
|
428
|
+
# Calls the block with each string field value:
|
429
|
+
#
|
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
|
436
|
+
#
|
437
|
+
# Output:
|
438
|
+
#
|
439
|
+
# "chunked"
|
440
|
+
# "cf-q-config;dur=6.0000002122251e-06"
|
441
|
+
# "cloudflare"
|
176
442
|
#
|
177
443
|
# Returns an enumerator if no block is given.
|
178
444
|
def each_value #:yield: +value+
|
@@ -182,32 +448,45 @@ module Net::HTTPHeader
|
|
182
448
|
end
|
183
449
|
end
|
184
450
|
|
185
|
-
# Removes
|
451
|
+
# Removes the header for the given case-insensitive +key+
|
452
|
+
# (see {Fields}[rdoc-ref:Net::HTTPHeader@Fields]);
|
453
|
+
# returns the deleted value, or +nil+ if no such field exists:
|
454
|
+
#
|
455
|
+
# req = Net::HTTP::Get.new(uri)
|
456
|
+
# req.delete('Accept') # => ["*/*"]
|
457
|
+
# req.delete('Nosuch') # => nil
|
458
|
+
#
|
186
459
|
def delete(key)
|
187
460
|
@header.delete(key.downcase.to_s)
|
188
461
|
end
|
189
462
|
|
190
|
-
# true if +key+
|
463
|
+
# Returns +true+ if the field for the case-insensitive +key+ exists, +false+ otherwise:
|
464
|
+
#
|
465
|
+
# req = Net::HTTP::Get.new(uri)
|
466
|
+
# req.key?('Accept') # => true
|
467
|
+
# req.key?('Nosuch') # => false
|
468
|
+
#
|
191
469
|
def key?(key)
|
192
470
|
@header.key?(key.downcase.to_s)
|
193
471
|
end
|
194
472
|
|
195
|
-
# Returns a
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
473
|
+
# Returns a hash of the key/value pairs:
|
474
|
+
#
|
475
|
+
# req = Net::HTTP::Get.new(uri)
|
476
|
+
# req.to_hash
|
477
|
+
# # =>
|
478
|
+
# {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
|
479
|
+
# "accept"=>["*/*"],
|
480
|
+
# "user-agent"=>["Ruby"],
|
481
|
+
# "host"=>["jsonplaceholder.typicode.com"]}
|
482
|
+
#
|
200
483
|
def to_hash
|
201
484
|
@header.dup
|
202
485
|
end
|
203
486
|
|
204
|
-
#
|
205
|
-
#
|
206
|
-
# Note that header names are capitalized systematically;
|
207
|
-
# capitalization may not match that used by the remote HTTP
|
208
|
-
# server in its response.
|
487
|
+
# Like #each_header, but the keys are returned in capitalized form.
|
209
488
|
#
|
210
|
-
#
|
489
|
+
# Net::HTTPHeader#canonical_each is an alias for Net::HTTPHeader#each_capitalized.
|
211
490
|
def each_capitalized
|
212
491
|
block_given? or return enum_for(__method__) { @header.size }
|
213
492
|
@header.each do |k,v|
|
@@ -222,8 +501,17 @@ module Net::HTTPHeader
|
|
222
501
|
end
|
223
502
|
private :capitalize
|
224
503
|
|
225
|
-
# Returns an
|
226
|
-
#
|
504
|
+
# Returns an array of Range objects that represent
|
505
|
+
# the value of field <tt>'Range'</tt>,
|
506
|
+
# or +nil+ if there is no such field;
|
507
|
+
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
|
508
|
+
#
|
509
|
+
# req = Net::HTTP::Get.new(uri)
|
510
|
+
# req['Range'] = 'bytes=0-99,200-299,400-499'
|
511
|
+
# req.range # => [0..99, 200..299, 400..499]
|
512
|
+
# req.delete('Range')
|
513
|
+
# req.range # # => nil
|
514
|
+
#
|
227
515
|
def range
|
228
516
|
return nil unless @header['range']
|
229
517
|
|
@@ -266,14 +554,31 @@ module Net::HTTPHeader
|
|
266
554
|
result
|
267
555
|
end
|
268
556
|
|
269
|
-
#
|
270
|
-
#
|
271
|
-
#
|
272
|
-
#
|
557
|
+
# :call-seq:
|
558
|
+
# set_range(length) -> length
|
559
|
+
# set_range(offset, length) -> range
|
560
|
+
# set_range(begin..length) -> range
|
561
|
+
#
|
562
|
+
# Sets the value for field <tt>'Range'</tt>;
|
563
|
+
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
|
564
|
+
#
|
565
|
+
# With argument +length+:
|
273
566
|
#
|
274
|
-
# req
|
275
|
-
# req.set_range
|
567
|
+
# req = Net::HTTP::Get.new(uri)
|
568
|
+
# req.set_range(100) # => 100
|
569
|
+
# req['Range'] # => "bytes=0-99"
|
276
570
|
#
|
571
|
+
# With arguments +offset+ and +length+:
|
572
|
+
#
|
573
|
+
# req.set_range(100, 100) # => 100...200
|
574
|
+
# req['Range'] # => "bytes=100-199"
|
575
|
+
#
|
576
|
+
# With argument +range+:
|
577
|
+
#
|
578
|
+
# req.set_range(100..199) # => 100..199
|
579
|
+
# req['Range'] # => "bytes=100-199"
|
580
|
+
#
|
581
|
+
# Net::HTTPHeader#range= is an alias for Net::HTTPHeader#set_range.
|
277
582
|
def set_range(r, e = nil)
|
278
583
|
unless r
|
279
584
|
@header.delete 'range'
|
@@ -394,7 +699,9 @@ module Net::HTTPHeader
|
|
394
699
|
# Sets the content type in an HTTP header.
|
395
700
|
# The +type+ should be a full HTTP content type, e.g. "text/html".
|
396
701
|
# The +params+ are an optional Hash of parameters to add after the
|
397
|
-
# content type, e.g. {'charset' => 'iso-8859-1'}
|
702
|
+
# content type, e.g. {'charset' => 'iso-8859-1'}.
|
703
|
+
#
|
704
|
+
# Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
|
398
705
|
def set_content_type(type, params = {})
|
399
706
|
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
|
400
707
|
end
|
@@ -414,6 +721,7 @@ module Net::HTTPHeader
|
|
414
721
|
# http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
|
415
722
|
# http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
|
416
723
|
#
|
724
|
+
# Net::HTTPHeader#form_data= is an alias for Net::HTTPHeader#set_form_data.
|
417
725
|
def set_form_data(params, sep = '&')
|
418
726
|
query = URI.encode_www_form(params)
|
419
727
|
query.gsub!(/&/, sep) if sep != '&'
|
data/lib/net/http/request.rb
CHANGED
@@ -1,8 +1,31 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
|
-
|
3
|
-
# This class
|
4
|
-
#
|
5
|
-
#
|
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
|
+
#
|
6
|
+
# The class should not be used directly;
|
7
|
+
# instead you should use its subclasses.
|
8
|
+
#
|
9
|
+
# Subclasses for HTTP requests:
|
10
|
+
#
|
11
|
+
# - Net::HTTP::Get
|
12
|
+
# - Net::HTTP::Head
|
13
|
+
# - Net::HTTP::Post
|
14
|
+
# - Net::HTTP::Put
|
15
|
+
# - Net::HTTP::Delete
|
16
|
+
# - Net::HTTP::Options
|
17
|
+
# - Net::HTTP::Trace
|
18
|
+
# - Net::HTTP::Patch
|
19
|
+
#
|
20
|
+
# Subclasses for WebDAV requests:
|
21
|
+
#
|
22
|
+
# - Net::HTTP::Propfind
|
23
|
+
# - Net::HTTP::Proppatch
|
24
|
+
# - Net::HTTP::Mkcol
|
25
|
+
# - Net::HTTP::Copy
|
26
|
+
# - Net::HTTP::Move
|
27
|
+
# - Net::HTTP::Lock
|
28
|
+
# - Net::HTTP::Unlock
|
6
29
|
#
|
7
30
|
class Net::HTTPRequest < Net::HTTPGenericRequest
|
8
31
|
# Creates an HTTP request object for +path+.
|
@@ -18,4 +41,3 @@ class Net::HTTPRequest < Net::HTTPGenericRequest
|
|
18
41
|
path, initheader
|
19
42
|
end
|
20
43
|
end
|
21
|
-
|