net-http 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/doc/net-http/examples.rdoc +30 -0
- data/lib/net/http/header.rb +470 -103
- 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/responses.rb +479 -69
- data/lib/net/http.rb +599 -346
- metadata +3 -2
data/lib/net/http.rb
CHANGED
@@ -32,110 +32,237 @@ module Net #:nodoc:
|
|
32
32
|
class HTTPHeaderSyntaxError < StandardError; end
|
33
33
|
# :startdoc:
|
34
34
|
|
35
|
-
#
|
35
|
+
# \Class \Net::HTTP provides a rich library that implements the client
|
36
|
+
# in a client-server model that uses the \HTTP request-response protocol.
|
37
|
+
# For information about \HTTP, see
|
38
|
+
#
|
39
|
+
# - {Hypertext Transfer Protocol}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol].
|
40
|
+
# - {Technical overview}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview].
|
41
|
+
#
|
42
|
+
# Note: If you are performing only a few GET requests, consider using
|
43
|
+
# {OpenURI}[https://docs.ruby-lang.org/en/master/OpenURI.html];
|
44
|
+
# otherwise, read on.
|
45
|
+
#
|
46
|
+
# == Synopsis
|
47
|
+
#
|
48
|
+
# If you are already familiar with \HTTP, this synopsis may be helpful.
|
49
|
+
#
|
50
|
+
# {Session}[rdoc-ref:Net::HTTP@Sessions] with multiple requests for
|
51
|
+
# {HTTP methods}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods]:
|
52
|
+
#
|
53
|
+
# Net::HTTP.start(hostname) do |http|
|
54
|
+
# # Session started automatically before block execution.
|
55
|
+
# http.get(path_or_uri, headers = {})
|
56
|
+
# http.head(path_or_uri, headers = {})
|
57
|
+
# http.post(path_or_uri, data, headers = {}) # Can also have a block.
|
58
|
+
# http.put(path_or_uri, data, headers = {})
|
59
|
+
# http.delete(path_or_uri, headers = {Depth: 'Infinity'})
|
60
|
+
# http.options(path_or_uri, headers = {})
|
61
|
+
# http.trace(path_or_uri, headers = {})
|
62
|
+
# http.patch(path_or_uri, data, headers = {}) # Can also have a block.
|
63
|
+
# # Session finished automatically at block exit.
|
64
|
+
# end
|
36
65
|
#
|
37
|
-
# Net::HTTP
|
38
|
-
#
|
39
|
-
#
|
66
|
+
# {Session}[rdoc-ref:Net::HTTP@Sessions] with multiple requests for
|
67
|
+
# {WebDAV methods}[https://en.wikipedia.org/wiki/WebDAV#Implementation]:
|
68
|
+
#
|
69
|
+
# Net::HTTP.start(hostname) do |http|
|
70
|
+
# # Session started automatically before block execution.
|
71
|
+
# http.copy(path_or_uri, headers = {})
|
72
|
+
# http.lock(path_or_uri, body, headers = {})
|
73
|
+
# http.mkcol(path_or_uri, body = nil, headers = {})
|
74
|
+
# http.move(path_or_uri, headers = {})
|
75
|
+
# http.propfind(path_or_uri, body = nil, headers = {'Depth' => '0'})
|
76
|
+
# http.proppatch(path_or_uri, body, headers = {})
|
77
|
+
# http.unlock(path_or_uri, body, headers = {})
|
78
|
+
# # Session finished automatically at block exit.
|
79
|
+
# end
|
40
80
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# Net::HTTP.
|
81
|
+
# Each of the following methods automatically starts and finishes
|
82
|
+
# a {session}[rdoc-ref:Net::HTTP@Sessions] that sends a single request:
|
44
83
|
#
|
45
|
-
#
|
84
|
+
# # Return string response body.
|
85
|
+
# Net::HTTP.get(hostname, path, port = 80)
|
86
|
+
# Net::HTTP.get(uri, headers = {}, port = 80)
|
46
87
|
#
|
47
|
-
#
|
88
|
+
# # Write string response body to $stdout.
|
89
|
+
# Net::HTTP.get_print(hostname, path_or_uri, port = 80)
|
90
|
+
# Net::HTTP.get_print(uri, headers = {}, port = 80)
|
48
91
|
#
|
49
|
-
#
|
92
|
+
# # Return response as Net::HTTPResponse object.
|
93
|
+
# Net::HTTP.get_response(hostname, path_or_uri, port = 80)
|
94
|
+
# Net::HTTP.get_response(uri, headers = {}, port = 80)
|
50
95
|
#
|
51
|
-
#
|
96
|
+
# Net::HTTP.post(uri, data, headers = {})
|
97
|
+
# Net::HTTP.post_form(uri, params)
|
52
98
|
#
|
53
|
-
#
|
99
|
+
# == About the Examples
|
54
100
|
#
|
55
|
-
#
|
56
|
-
# connections. They are not recommended if you are performing many HTTP
|
57
|
-
# requests.
|
101
|
+
# :include: doc/net-http/examples.rdoc
|
58
102
|
#
|
59
|
-
#
|
103
|
+
# == URIs
|
60
104
|
#
|
61
|
-
#
|
105
|
+
# On the internet, a URI
|
106
|
+
# ({Universal Resource Identifier}[https://en.wikipedia.org/wiki/Uniform_Resource_Identifier])
|
107
|
+
# is a string that identifies a particular resource.
|
108
|
+
# It consists of some or all of: scheme, hostname, path, query, and fragment;
|
109
|
+
# see {URI syntax}[https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax].
|
62
110
|
#
|
63
|
-
#
|
111
|
+
# A Ruby {URI::Generic}[https://docs.ruby-lang.org/en/master/URI/Generic.html] object
|
112
|
+
# represents an internet URI.
|
113
|
+
# It provides, among others, methods
|
114
|
+
# +scheme+, +hostname+, +path+, +query+, and +fragment+.
|
64
115
|
#
|
65
|
-
#
|
66
|
-
# Net::HTTP.get(uri) # => String
|
116
|
+
# === Schemes
|
67
117
|
#
|
68
|
-
#
|
118
|
+
# An internet \URI has
|
119
|
+
# a {scheme}[https://en.wikipedia.org/wiki/List_of_URI_schemes].
|
69
120
|
#
|
70
|
-
#
|
71
|
-
# params = { :limit => 10, :page => 3 }
|
72
|
-
# uri.query = URI.encode_www_form(params)
|
121
|
+
# The two schemes supported in \Net::HTTP are <tt>'https'</tt> and <tt>'http'</tt>:
|
73
122
|
#
|
74
|
-
#
|
75
|
-
#
|
123
|
+
# uri.scheme # => "https"
|
124
|
+
# URI('http://example.com').scheme # => "http"
|
76
125
|
#
|
77
|
-
# ===
|
126
|
+
# === Hostnames
|
78
127
|
#
|
79
|
-
#
|
80
|
-
# res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
|
81
|
-
# puts res.body
|
128
|
+
# A hostname identifies a server (host) to which requests may be sent:
|
82
129
|
#
|
83
|
-
#
|
130
|
+
# hostname = uri.hostname # => "jsonplaceholder.typicode.com"
|
131
|
+
# Net::HTTP.start(hostname) do |http|
|
132
|
+
# # Some HTTP stuff.
|
133
|
+
# end
|
84
134
|
#
|
85
|
-
#
|
86
|
-
# res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
|
87
|
-
# puts res.body
|
135
|
+
# === Paths
|
88
136
|
#
|
89
|
-
#
|
137
|
+
# A host-specific path identifies a resource on the host:
|
90
138
|
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
139
|
+
# _uri = uri.dup
|
140
|
+
# _uri.path = '/todos/1'
|
141
|
+
# hostname = _uri.hostname
|
142
|
+
# path = _uri.path
|
143
|
+
# Net::HTTP.get(hostname, path)
|
94
144
|
#
|
95
|
-
#
|
145
|
+
# === Queries
|
96
146
|
#
|
97
|
-
#
|
98
|
-
# request = Net::HTTP::Get.new uri
|
147
|
+
# A host-specific query adds name/value pairs to the URI:
|
99
148
|
#
|
100
|
-
#
|
101
|
-
#
|
149
|
+
# _uri = uri.dup
|
150
|
+
# params = {userId: 1, completed: false}
|
151
|
+
# _uri.query = URI.encode_www_form(params)
|
152
|
+
# _uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com?userId=1&completed=false>
|
153
|
+
# Net::HTTP.get(_uri)
|
102
154
|
#
|
103
|
-
#
|
104
|
-
# is kept open for the duration of the block. The connection will remain
|
105
|
-
# open for multiple requests in the block if the server indicates it
|
106
|
-
# supports persistent connections.
|
155
|
+
# === Fragments
|
107
156
|
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
157
|
+
# A {URI fragment}[https://en.wikipedia.org/wiki/URI_fragment] has no effect
|
158
|
+
# in \Net::HTTP;
|
159
|
+
# the same data is returned, regardless of whether a fragment is included.
|
111
160
|
#
|
112
|
-
#
|
113
|
-
# Request Classes".
|
161
|
+
# == Request Headers
|
114
162
|
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
163
|
+
# Request headers may be used to pass additional information to the host,
|
164
|
+
# similar to arguments passed in a method call;
|
165
|
+
# each header is a name/value pair.
|
118
166
|
#
|
119
|
-
#
|
167
|
+
# Each of the \Net::HTTP methods that sends a request to the host
|
168
|
+
# has optional argument +headers+,
|
169
|
+
# where the headers are expressed as a hash of field-name/value pairs:
|
120
170
|
#
|
121
|
-
#
|
122
|
-
#
|
171
|
+
# headers = {Accept: 'application/json', Connection: 'Keep-Alive'}
|
172
|
+
# Net::HTTP.get(uri, headers)
|
123
173
|
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
# res.to_hash['set-cookie'] # => Array
|
128
|
-
# puts "Headers: #{res.to_hash.inspect}"
|
174
|
+
# See lists of both standard request fields and common request fields at
|
175
|
+
# {Request Fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
|
176
|
+
# A host may also accept other custom fields.
|
129
177
|
#
|
130
|
-
#
|
131
|
-
# puts res.code # => '200'
|
132
|
-
# puts res.message # => 'OK'
|
133
|
-
# puts res.class.name # => 'HTTPOK'
|
178
|
+
# == Sessions
|
134
179
|
#
|
135
|
-
#
|
136
|
-
#
|
180
|
+
# A _session_ is a connection between a server (host) and a client that:
|
181
|
+
#
|
182
|
+
# - Is begun by instance method Net::HTTP#start.
|
183
|
+
# - May contain any number of requests.
|
184
|
+
# - Is ended by instance method Net::HTTP#finish.
|
185
|
+
#
|
186
|
+
# See example sessions at the {Synopsis}[rdoc-ref:Net::HTTP@Synopsis].
|
187
|
+
#
|
188
|
+
# === Session Using \Net::HTTP.start
|
189
|
+
#
|
190
|
+
# If you have many requests to make to a single host (and port),
|
191
|
+
# consider using singleton method Net::HTTP.start with a block;
|
192
|
+
# the method handles the session automatically by:
|
193
|
+
#
|
194
|
+
# - Calling #start before block execution.
|
195
|
+
# - Executing the block.
|
196
|
+
# - Calling #finish after block execution.
|
197
|
+
#
|
198
|
+
# In the block, you can use these instance methods,
|
199
|
+
# each of which that sends a single request:
|
200
|
+
#
|
201
|
+
# - {HTTP methods}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods]:
|
202
|
+
#
|
203
|
+
# - #get, #request_get: GET.
|
204
|
+
# - #head, #request_head: HEAD.
|
205
|
+
# - #post, #request_post: POST.
|
206
|
+
# - #delete: DELETE.
|
207
|
+
# - #options: OPTIONS.
|
208
|
+
# - #trace: TRACE.
|
209
|
+
# - #patch: PATCH.
|
210
|
+
#
|
211
|
+
# - {WebDAV methods}[https://en.wikipedia.org/wiki/WebDAV#Implementation]:
|
137
212
|
#
|
138
|
-
#
|
213
|
+
# - #copy: COPY.
|
214
|
+
# - #lock: LOCK.
|
215
|
+
# - #mkcol: MKCOL.
|
216
|
+
# - #move: MOVE.
|
217
|
+
# - #propfind: PROPFIND.
|
218
|
+
# - #proppatch: PROPPATCH.
|
219
|
+
# - #unlock: UNLOCK.
|
220
|
+
#
|
221
|
+
# === Session Using \Net::HTTP.start and \Net::HTTP.finish
|
222
|
+
#
|
223
|
+
# You can manage a session manually using methods #start and #finish:
|
224
|
+
#
|
225
|
+
# http = Net::HTTP.new(hostname)
|
226
|
+
# http.start
|
227
|
+
# http.get('/todos/1')
|
228
|
+
# http.get('/todos/2')
|
229
|
+
# http.delete('/posts/1')
|
230
|
+
# http.finish # Needed to free resources.
|
231
|
+
#
|
232
|
+
# === Single-Request Session
|
233
|
+
#
|
234
|
+
# Certain convenience methods automatically handle a session by:
|
235
|
+
#
|
236
|
+
# - Creating an \HTTP object
|
237
|
+
# - Starting a session.
|
238
|
+
# - Sending a single request.
|
239
|
+
# - Finishing the session.
|
240
|
+
# - Destroying the object.
|
241
|
+
#
|
242
|
+
# Such methods that send GET requests:
|
243
|
+
#
|
244
|
+
# - ::get: Returns the string response body.
|
245
|
+
# - ::get_print: Writes the string response body to $stdout.
|
246
|
+
# - ::get_response: Returns a Net::HTTPResponse object.
|
247
|
+
#
|
248
|
+
# Such methods that send POST requests:
|
249
|
+
#
|
250
|
+
# - ::post: Posts data to the host.
|
251
|
+
# - ::post_form: Posts form data to the host.
|
252
|
+
#
|
253
|
+
# == \HTTP Requests and Responses
|
254
|
+
#
|
255
|
+
# Many of the methods above are convenience methods,
|
256
|
+
# each of which sends a request and returns a string
|
257
|
+
# without directly using \Net::HTTPRequest and \Net::HTTPResponse objects.
|
258
|
+
#
|
259
|
+
# You can, however, directly create a request object, send the request,
|
260
|
+
# and retrieve the response object; see:
|
261
|
+
#
|
262
|
+
# - Net::HTTPRequest.
|
263
|
+
# - Net::HTTPResponse.
|
264
|
+
#
|
265
|
+
# == Following Redirection
|
139
266
|
#
|
140
267
|
# Each Net::HTTPResponse object belongs to a class for its response code.
|
141
268
|
#
|
@@ -167,56 +294,7 @@ module Net #:nodoc:
|
|
167
294
|
#
|
168
295
|
# print fetch('http://www.ruby-lang.org')
|
169
296
|
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
# A POST can be made using the Net::HTTP::Post request class. This example
|
173
|
-
# creates a URL encoded POST body:
|
174
|
-
#
|
175
|
-
# uri = URI('http://www.example.com/todo.cgi')
|
176
|
-
# req = Net::HTTP::Post.new(uri)
|
177
|
-
# req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
|
178
|
-
#
|
179
|
-
# res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
180
|
-
# http.request(req)
|
181
|
-
# end
|
182
|
-
#
|
183
|
-
# case res
|
184
|
-
# when Net::HTTPSuccess, Net::HTTPRedirection
|
185
|
-
# # OK
|
186
|
-
# else
|
187
|
-
# res.value
|
188
|
-
# end
|
189
|
-
#
|
190
|
-
# To send multipart/form-data use Net::HTTPHeader#set_form:
|
191
|
-
#
|
192
|
-
# req = Net::HTTP::Post.new(uri)
|
193
|
-
# req.set_form([['upload', File.open('foo.bar')]], 'multipart/form-data')
|
194
|
-
#
|
195
|
-
# Other requests that can contain a body such as PUT can be created in the
|
196
|
-
# same way using the corresponding request class (Net::HTTP::Put).
|
197
|
-
#
|
198
|
-
# === Setting Headers
|
199
|
-
#
|
200
|
-
# The following example performs a conditional GET using the
|
201
|
-
# If-Modified-Since header. If the files has not been modified since the
|
202
|
-
# time in the header a Not Modified response will be returned. See RFC 2616
|
203
|
-
# section 9.3 for further details.
|
204
|
-
#
|
205
|
-
# uri = URI('http://example.com/cached_response')
|
206
|
-
# file = File.stat 'cached_response'
|
207
|
-
#
|
208
|
-
# req = Net::HTTP::Get.new(uri)
|
209
|
-
# req['If-Modified-Since'] = file.mtime.rfc2822
|
210
|
-
#
|
211
|
-
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
|
212
|
-
# http.request(req)
|
213
|
-
# }
|
214
|
-
#
|
215
|
-
# open 'cached_response', 'w' do |io|
|
216
|
-
# io.write res.body
|
217
|
-
# end if res.is_a?(Net::HTTPSuccess)
|
218
|
-
#
|
219
|
-
# === Basic Authentication
|
297
|
+
# == Basic Authentication
|
220
298
|
#
|
221
299
|
# Basic authentication is performed according to
|
222
300
|
# [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
|
@@ -231,7 +309,7 @@ module Net #:nodoc:
|
|
231
309
|
# }
|
232
310
|
# puts res.body
|
233
311
|
#
|
234
|
-
#
|
312
|
+
# == Streaming Response Bodies
|
235
313
|
#
|
236
314
|
# By default Net::HTTP reads an entire response into memory. If you are
|
237
315
|
# handling large files or wish to implement a progress bar you can instead
|
@@ -251,7 +329,7 @@ module Net #:nodoc:
|
|
251
329
|
# end
|
252
330
|
# end
|
253
331
|
#
|
254
|
-
#
|
332
|
+
# == HTTPS
|
255
333
|
#
|
256
334
|
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
|
257
335
|
#
|
@@ -272,7 +350,7 @@ module Net #:nodoc:
|
|
272
350
|
# In previous versions of Ruby you would need to require 'net/https' to use
|
273
351
|
# HTTPS. This is no longer true.
|
274
352
|
#
|
275
|
-
#
|
353
|
+
# == Proxies
|
276
354
|
#
|
277
355
|
# Net::HTTP will automatically create a proxy from the +http_proxy+
|
278
356
|
# environment variable if it is present. To disable use of +http_proxy+,
|
@@ -290,7 +368,7 @@ module Net #:nodoc:
|
|
290
368
|
# See Net::HTTP.new for further details and examples such as proxies that
|
291
369
|
# require a username and password.
|
292
370
|
#
|
293
|
-
#
|
371
|
+
# == Compression
|
294
372
|
#
|
295
373
|
# Net::HTTP automatically adds Accept-Encoding for compression of response
|
296
374
|
# bodies and automatically decompresses gzip and deflate responses unless a
|
@@ -298,106 +376,10 @@ module Net #:nodoc:
|
|
298
376
|
#
|
299
377
|
# Compression can be disabled through the Accept-Encoding: identity header.
|
300
378
|
#
|
301
|
-
# == HTTP Request Classes
|
302
|
-
#
|
303
|
-
# Here is the HTTP request class hierarchy.
|
304
|
-
#
|
305
|
-
# * Net::HTTPRequest
|
306
|
-
# * Net::HTTP::Get
|
307
|
-
# * Net::HTTP::Head
|
308
|
-
# * Net::HTTP::Post
|
309
|
-
# * Net::HTTP::Patch
|
310
|
-
# * Net::HTTP::Put
|
311
|
-
# * Net::HTTP::Proppatch
|
312
|
-
# * Net::HTTP::Lock
|
313
|
-
# * Net::HTTP::Unlock
|
314
|
-
# * Net::HTTP::Options
|
315
|
-
# * Net::HTTP::Propfind
|
316
|
-
# * Net::HTTP::Delete
|
317
|
-
# * Net::HTTP::Move
|
318
|
-
# * Net::HTTP::Copy
|
319
|
-
# * Net::HTTP::Mkcol
|
320
|
-
# * Net::HTTP::Trace
|
321
|
-
#
|
322
|
-
# == HTTP Response Classes
|
323
|
-
#
|
324
|
-
# Here is HTTP response class hierarchy. All classes are defined in Net
|
325
|
-
# module and are subclasses of Net::HTTPResponse.
|
326
|
-
#
|
327
|
-
# HTTPUnknownResponse:: For unhandled HTTP extensions
|
328
|
-
# HTTPInformation:: 1xx
|
329
|
-
# HTTPContinue:: 100
|
330
|
-
# HTTPSwitchProtocol:: 101
|
331
|
-
# HTTPProcessing:: 102
|
332
|
-
# HTTPEarlyHints:: 103
|
333
|
-
# HTTPSuccess:: 2xx
|
334
|
-
# HTTPOK:: 200
|
335
|
-
# HTTPCreated:: 201
|
336
|
-
# HTTPAccepted:: 202
|
337
|
-
# HTTPNonAuthoritativeInformation:: 203
|
338
|
-
# HTTPNoContent:: 204
|
339
|
-
# HTTPResetContent:: 205
|
340
|
-
# HTTPPartialContent:: 206
|
341
|
-
# HTTPMultiStatus:: 207
|
342
|
-
# HTTPAlreadyReported:: 208
|
343
|
-
# HTTPIMUsed:: 226
|
344
|
-
# HTTPRedirection:: 3xx
|
345
|
-
# HTTPMultipleChoices:: 300
|
346
|
-
# HTTPMovedPermanently:: 301
|
347
|
-
# HTTPFound:: 302
|
348
|
-
# HTTPSeeOther:: 303
|
349
|
-
# HTTPNotModified:: 304
|
350
|
-
# HTTPUseProxy:: 305
|
351
|
-
# HTTPTemporaryRedirect:: 307
|
352
|
-
# HTTPPermanentRedirect:: 308
|
353
|
-
# HTTPClientError:: 4xx
|
354
|
-
# HTTPBadRequest:: 400
|
355
|
-
# HTTPUnauthorized:: 401
|
356
|
-
# HTTPPaymentRequired:: 402
|
357
|
-
# HTTPForbidden:: 403
|
358
|
-
# HTTPNotFound:: 404
|
359
|
-
# HTTPMethodNotAllowed:: 405
|
360
|
-
# HTTPNotAcceptable:: 406
|
361
|
-
# HTTPProxyAuthenticationRequired:: 407
|
362
|
-
# HTTPRequestTimeOut:: 408
|
363
|
-
# HTTPConflict:: 409
|
364
|
-
# HTTPGone:: 410
|
365
|
-
# HTTPLengthRequired:: 411
|
366
|
-
# HTTPPreconditionFailed:: 412
|
367
|
-
# HTTPRequestEntityTooLarge:: 413
|
368
|
-
# HTTPRequestURITooLong:: 414
|
369
|
-
# HTTPUnsupportedMediaType:: 415
|
370
|
-
# HTTPRequestedRangeNotSatisfiable:: 416
|
371
|
-
# HTTPExpectationFailed:: 417
|
372
|
-
# HTTPMisdirectedRequest:: 421
|
373
|
-
# HTTPUnprocessableEntity:: 422
|
374
|
-
# HTTPLocked:: 423
|
375
|
-
# HTTPFailedDependency:: 424
|
376
|
-
# HTTPUpgradeRequired:: 426
|
377
|
-
# HTTPPreconditionRequired:: 428
|
378
|
-
# HTTPTooManyRequests:: 429
|
379
|
-
# HTTPRequestHeaderFieldsTooLarge:: 431
|
380
|
-
# HTTPUnavailableForLegalReasons:: 451
|
381
|
-
# HTTPServerError:: 5xx
|
382
|
-
# HTTPInternalServerError:: 500
|
383
|
-
# HTTPNotImplemented:: 501
|
384
|
-
# HTTPBadGateway:: 502
|
385
|
-
# HTTPServiceUnavailable:: 503
|
386
|
-
# HTTPGatewayTimeOut:: 504
|
387
|
-
# HTTPVersionNotSupported:: 505
|
388
|
-
# HTTPVariantAlsoNegotiates:: 506
|
389
|
-
# HTTPInsufficientStorage:: 507
|
390
|
-
# HTTPLoopDetected:: 508
|
391
|
-
# HTTPNotExtended:: 510
|
392
|
-
# HTTPNetworkAuthenticationRequired:: 511
|
393
|
-
#
|
394
|
-
# There is also the Net::HTTPBadResponse exception which is raised when
|
395
|
-
# there is a protocol error.
|
396
|
-
#
|
397
379
|
class HTTP < Protocol
|
398
380
|
|
399
381
|
# :stopdoc:
|
400
|
-
VERSION = "0.3.
|
382
|
+
VERSION = "0.3.2"
|
401
383
|
Revision = %q$Revision$.split[1]
|
402
384
|
HTTPVersion = '1.1'
|
403
385
|
begin
|
@@ -408,18 +390,17 @@ module Net #:nodoc:
|
|
408
390
|
end
|
409
391
|
# :startdoc:
|
410
392
|
|
411
|
-
#
|
412
|
-
# Defaults to ON in Ruby 1.8 or later.
|
393
|
+
# Returns +true+; retained for compatibility.
|
413
394
|
def HTTP.version_1_2
|
414
395
|
true
|
415
396
|
end
|
416
397
|
|
417
|
-
# Returns true
|
418
|
-
# Defaults to true.
|
398
|
+
# Returns +true+; retained for compatibility.
|
419
399
|
def HTTP.version_1_2?
|
420
400
|
true
|
421
401
|
end
|
422
402
|
|
403
|
+
# Returns +false+; retained for compatibility.
|
423
404
|
def HTTP.version_1_1? #:nodoc:
|
424
405
|
false
|
425
406
|
end
|
@@ -429,25 +410,12 @@ module Net #:nodoc:
|
|
429
410
|
alias is_version_1_2? version_1_2? #:nodoc:
|
430
411
|
end
|
431
412
|
|
413
|
+
# :call-seq:
|
414
|
+
# Net::HTTP.get_print(hostname, path, port = 80) -> nil
|
415
|
+
# Net::HTTP:get_print(uri, headers = {}, port = uri.port) -> nil
|
432
416
|
#
|
433
|
-
#
|
434
|
-
#
|
435
|
-
|
436
|
-
#
|
437
|
-
# Gets the body text from the target and outputs it to $stdout. The
|
438
|
-
# target can either be specified as
|
439
|
-
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
|
440
|
-
#
|
441
|
-
# Net::HTTP.get_print URI('http://www.example.com/index.html')
|
442
|
-
#
|
443
|
-
# or:
|
444
|
-
#
|
445
|
-
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
446
|
-
#
|
447
|
-
# you can also specify request headers:
|
448
|
-
#
|
449
|
-
# Net::HTTP.get_print URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' }
|
450
|
-
#
|
417
|
+
# Like Net::HTTP.get, but writes the returned body to $stdout;
|
418
|
+
# returns +nil+.
|
451
419
|
def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
|
452
420
|
get_response(uri_or_host, path_or_headers, port) {|res|
|
453
421
|
res.read_body do |chunk|
|
@@ -457,40 +425,48 @@ module Net #:nodoc:
|
|
457
425
|
nil
|
458
426
|
end
|
459
427
|
|
460
|
-
#
|
461
|
-
#
|
462
|
-
#
|
428
|
+
# :call-seq:
|
429
|
+
# Net::HTTP.get(hostname, path, port = 80) -> body
|
430
|
+
# Net::HTTP:get(uri, headers = {}, port = uri.port) -> body
|
463
431
|
#
|
464
|
-
#
|
432
|
+
# Sends a GET request and returns the \HTTP response body as a string.
|
465
433
|
#
|
466
|
-
#
|
434
|
+
# With string arguments +hostname+ and +path+:
|
467
435
|
#
|
468
|
-
#
|
436
|
+
# hostname = 'jsonplaceholder.typicode.com'
|
437
|
+
# path = '/todos/1'
|
438
|
+
# puts Net::HTTP.get(hostname, path)
|
469
439
|
#
|
470
|
-
#
|
440
|
+
# Output:
|
471
441
|
#
|
472
|
-
#
|
442
|
+
# {
|
443
|
+
# "userId": 1,
|
444
|
+
# "id": 1,
|
445
|
+
# "title": "delectus aut autem",
|
446
|
+
# "completed": false
|
447
|
+
# }
|
473
448
|
#
|
474
|
-
|
475
|
-
get_response(uri_or_host, path_or_headers, port).body
|
476
|
-
end
|
477
|
-
|
478
|
-
# Sends a GET request to the target and returns the HTTP response
|
479
|
-
# as a Net::HTTPResponse object. The target can either be specified as
|
480
|
-
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
|
449
|
+
# With URI object +uri+ and optional hash argument +headers+:
|
481
450
|
#
|
482
|
-
#
|
483
|
-
#
|
451
|
+
# uri = URI('https://jsonplaceholder.typicode.com/todos/1')
|
452
|
+
# headers = {'Content-type' => 'application/json; charset=UTF-8'}
|
453
|
+
# Net::HTTP.get(uri, headers)
|
484
454
|
#
|
485
|
-
#
|
455
|
+
# Related:
|
486
456
|
#
|
487
|
-
#
|
488
|
-
#
|
457
|
+
# - Net::HTTP::Get: request class for \HTTP method +GET+.
|
458
|
+
# - Net::HTTP#get: convenience method for \HTTP method +GET+.
|
489
459
|
#
|
490
|
-
|
491
|
-
|
492
|
-
|
460
|
+
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
|
461
|
+
get_response(uri_or_host, path_or_headers, port).body
|
462
|
+
end
|
463
|
+
|
464
|
+
# :call-seq:
|
465
|
+
# Net::HTTP.get_response(hostname, path, port = 80) -> http_response
|
466
|
+
# Net::HTTP:get_response(uri, headers = {}, port = uri.port) -> http_response
|
493
467
|
#
|
468
|
+
# Like Net::HTTP.get, but returns a Net::HTTPResponse object
|
469
|
+
# instead of the body string.
|
494
470
|
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
|
495
471
|
if path_or_headers && !path_or_headers.is_a?(Hash)
|
496
472
|
host = uri_or_host
|
@@ -508,16 +484,31 @@ module Net #:nodoc:
|
|
508
484
|
end
|
509
485
|
end
|
510
486
|
|
511
|
-
# Posts data to
|
487
|
+
# Posts data to a host; returns a Net::HTTPResponse object.
|
512
488
|
#
|
513
|
-
#
|
489
|
+
# Argument +url+ must be a URL;
|
490
|
+
# argument +data+ must be a string:
|
491
|
+
#
|
492
|
+
# _uri = uri.dup
|
493
|
+
# _uri.path = '/posts'
|
494
|
+
# data = '{"title": "foo", "body": "bar", "userId": 1}'
|
495
|
+
# headers = {'content-type': 'application/json'}
|
496
|
+
# res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
|
497
|
+
# puts res.body
|
498
|
+
#
|
499
|
+
# Output:
|
514
500
|
#
|
515
|
-
#
|
516
|
-
#
|
501
|
+
# {
|
502
|
+
# "title": "foo",
|
503
|
+
# "body": "bar",
|
504
|
+
# "userId": 1,
|
505
|
+
# "id": 101
|
506
|
+
# }
|
517
507
|
#
|
518
|
-
#
|
519
|
-
#
|
520
|
-
#
|
508
|
+
# Related:
|
509
|
+
#
|
510
|
+
# - Net::HTTP::Post: request class for \HTTP method +POST+.
|
511
|
+
# - Net::HTTP#post: convenience method for \HTTP method +POST+.
|
521
512
|
#
|
522
513
|
def HTTP.post(url, data, header = nil)
|
523
514
|
start(url.hostname, url.port,
|
@@ -526,22 +517,25 @@ module Net #:nodoc:
|
|
526
517
|
}
|
527
518
|
end
|
528
519
|
|
529
|
-
# Posts
|
530
|
-
# The form data must be provided as a Hash mapping from String to String.
|
531
|
-
# Example:
|
520
|
+
# Posts data to a host; returns a Net::HTTPResponse object.
|
532
521
|
#
|
533
|
-
#
|
522
|
+
# Argument +url+ must be a URI;
|
523
|
+
# argument +data+ must be a hash:
|
534
524
|
#
|
535
|
-
#
|
536
|
-
#
|
537
|
-
#
|
525
|
+
# _uri = uri.dup
|
526
|
+
# _uri.path = '/posts'
|
527
|
+
# data = {title: 'foo', body: 'bar', userId: 1}
|
528
|
+
# res = Net::HTTP.post_form(_uri, data) # => #<Net::HTTPCreated 201 Created readbody=true>
|
529
|
+
# puts res.body
|
538
530
|
#
|
539
|
-
#
|
540
|
-
#
|
541
|
-
# require 'net/http'
|
531
|
+
# Output:
|
542
532
|
#
|
543
|
-
#
|
544
|
-
#
|
533
|
+
# {
|
534
|
+
# "title": "foo",
|
535
|
+
# "body": "bar",
|
536
|
+
# "userId": "1",
|
537
|
+
# "id": 101
|
538
|
+
# }
|
545
539
|
#
|
546
540
|
def HTTP.post_form(url, params)
|
547
541
|
req = Post.new(url)
|
@@ -557,17 +551,26 @@ module Net #:nodoc:
|
|
557
551
|
# HTTP session management
|
558
552
|
#
|
559
553
|
|
560
|
-
#
|
554
|
+
# Returns intger +80+, the default port to use for HTTP requests:
|
555
|
+
#
|
556
|
+
# Net::HTTP.default_port # => 80
|
557
|
+
#
|
561
558
|
def HTTP.default_port
|
562
559
|
http_default_port()
|
563
560
|
end
|
564
561
|
|
565
|
-
#
|
562
|
+
# Returns integer +80+, the default port to use for HTTP requests:
|
563
|
+
#
|
564
|
+
# Net::HTTP.http_default_port # => 80
|
565
|
+
#
|
566
566
|
def HTTP.http_default_port
|
567
567
|
80
|
568
568
|
end
|
569
569
|
|
570
|
-
#
|
570
|
+
# Returns integer +443+, the default port to use for HTTPS requests:
|
571
|
+
#
|
572
|
+
# Net::HTTP.https_default_port # => 443
|
573
|
+
#
|
571
574
|
def HTTP.https_default_port
|
572
575
|
443
|
573
576
|
end
|
@@ -577,35 +580,91 @@ module Net #:nodoc:
|
|
577
580
|
end
|
578
581
|
|
579
582
|
# :call-seq:
|
580
|
-
# HTTP.start(address, port, p_addr, p_port, p_user, p_pass,
|
581
|
-
# HTTP.start(address, port=nil, p_addr
|
582
|
-
#
|
583
|
-
# Creates a new Net::HTTP object,
|
584
|
-
#
|
585
|
-
#
|
586
|
-
#
|
587
|
-
#
|
588
|
-
#
|
589
|
-
#
|
590
|
-
#
|
591
|
-
#
|
592
|
-
#
|
593
|
-
#
|
594
|
-
#
|
595
|
-
#
|
596
|
-
#
|
597
|
-
#
|
598
|
-
#
|
599
|
-
#
|
600
|
-
#
|
601
|
-
#
|
602
|
-
#
|
603
|
-
#
|
604
|
-
#
|
605
|
-
#
|
606
|
-
#
|
607
|
-
#
|
608
|
-
#
|
583
|
+
# HTTP.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) -> http
|
584
|
+
# HTTP.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) {|http| ... } -> object
|
585
|
+
#
|
586
|
+
# Creates a new \Net::HTTP object, +http+, via \Net::HTTP.new:
|
587
|
+
#
|
588
|
+
# Net::HTTP.new(address, port, p_addr, p_port, p_user, p_pass)
|
589
|
+
#
|
590
|
+
# - For arguments +hostname+ through +p_pass+, see Net::HTTP.new.
|
591
|
+
# - For argument +opts+, see below.
|
592
|
+
#
|
593
|
+
# Note: If +port+ is +nil+ and <tt>opts[:use_ssl]</tt> is a truthy value,
|
594
|
+
# the value passed to +new+ is Net::HTTP.https_default_port, not +port+.
|
595
|
+
#
|
596
|
+
# With no block given:
|
597
|
+
#
|
598
|
+
# - Calls <tt>http.start</tt> with no block (see #start),
|
599
|
+
# which opens a TCP connection and \HTTP session.
|
600
|
+
# - Returns +http+.
|
601
|
+
# - The caller should call #finish to close the session:
|
602
|
+
#
|
603
|
+
# http = Net::HTTP.start(hostname)
|
604
|
+
# http.started? # => true
|
605
|
+
# http.finish
|
606
|
+
# http.started? # => false
|
607
|
+
#
|
608
|
+
# With a block given:
|
609
|
+
#
|
610
|
+
# - Calls <tt>http.start</tt> with the block (see #start), which:
|
611
|
+
#
|
612
|
+
# - Opens a TCP connection and \HTTP session.
|
613
|
+
# - Calls the block,
|
614
|
+
# which may make any number of requests to the host.
|
615
|
+
# - Closes the \HTTP session and TCP connection on block exit.
|
616
|
+
# - Returns the block's value +object+.
|
617
|
+
#
|
618
|
+
# - Returns +object+.
|
619
|
+
#
|
620
|
+
# Example:
|
621
|
+
#
|
622
|
+
# hostname = 'jsonplaceholder.typicode.com'
|
623
|
+
# Net::HTTP.start(hostname) do |http|
|
624
|
+
# puts http.get('/todos/1').body
|
625
|
+
# puts http.get('/todos/2').body
|
626
|
+
# end
|
627
|
+
#
|
628
|
+
# Output:
|
629
|
+
#
|
630
|
+
# {
|
631
|
+
# "userId": 1,
|
632
|
+
# "id": 1,
|
633
|
+
# "title": "delectus aut autem",
|
634
|
+
# "completed": false
|
635
|
+
# }
|
636
|
+
# {
|
637
|
+
# "userId": 1,
|
638
|
+
# "id": 2,
|
639
|
+
# "title": "quis ut nam facilis et officia qui",
|
640
|
+
# "completed": false
|
641
|
+
# }
|
642
|
+
#
|
643
|
+
# If the last argument given is a hash, it is the +opts+ hash,
|
644
|
+
# where each key is a method or accessor to be called,
|
645
|
+
# and its value is the value to be set.
|
646
|
+
#
|
647
|
+
# The keys may include:
|
648
|
+
#
|
649
|
+
# - #ca_file
|
650
|
+
# - #ca_path
|
651
|
+
# - #cert
|
652
|
+
# - #cert_store
|
653
|
+
# - #ciphers
|
654
|
+
# - #close_on_empty_response
|
655
|
+
# - +ipaddr+ (calls #ipaddr=)
|
656
|
+
# - #keep_alive_timeout
|
657
|
+
# - #key
|
658
|
+
# - #open_timeout
|
659
|
+
# - #read_timeout
|
660
|
+
# - #ssl_timeout
|
661
|
+
# - #ssl_version
|
662
|
+
# - +use_ssl+ (calls #use_ssl=)
|
663
|
+
# - #verify_callback
|
664
|
+
# - #verify_depth
|
665
|
+
# - #verify_mode
|
666
|
+
# - #write_timeout
|
667
|
+
#
|
609
668
|
def HTTP.start(address, *arg, &block) # :yield: +http+
|
610
669
|
arg.pop if opt = Hash.try_convert(arg[-1])
|
611
670
|
port, p_addr, p_port, p_user, p_pass = *arg
|
@@ -632,25 +691,113 @@ module Net #:nodoc:
|
|
632
691
|
alias newobj new # :nodoc:
|
633
692
|
end
|
634
693
|
|
635
|
-
#
|
636
|
-
# HTTP session.
|
694
|
+
# Returns a new Net::HTTP object +http+
|
695
|
+
# (but does not open a TCP connection or HTTP session).
|
696
|
+
#
|
697
|
+
# <b>No Proxy</b>
|
698
|
+
#
|
699
|
+
# With only string argument +hostname+ given
|
700
|
+
# (and <tt>ENV['http_proxy']</tt> undefined or +nil+),
|
701
|
+
# the returned +http+:
|
702
|
+
#
|
703
|
+
# - Has the given address.
|
704
|
+
# - Has the default port number, Net::HTTP.default_port (80).
|
705
|
+
# - Has no proxy.
|
706
|
+
#
|
707
|
+
# Example:
|
708
|
+
#
|
709
|
+
# http = Net::HTTP.new(hostname)
|
710
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
|
711
|
+
# http.address # => "jsonplaceholder.typicode.com"
|
712
|
+
# http.port # => 80
|
713
|
+
# http.proxy? # => false
|
714
|
+
#
|
715
|
+
# With integer argument +port+ also given,
|
716
|
+
# the returned +http+ has the given port:
|
717
|
+
#
|
718
|
+
# http = Net::HTTP.new(hostname, 8000)
|
719
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
|
720
|
+
# http.port # => 8000
|
721
|
+
#
|
722
|
+
# <b>Proxy Using Argument +p_addr+ as a \String</b>
|
723
|
+
#
|
724
|
+
# When argument +p_addr+ is a string hostname,
|
725
|
+
# the returned +http+ has a proxy:
|
726
|
+
#
|
727
|
+
# http = Net::HTTP.new(hostname, nil, 'proxy.example')
|
728
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
|
729
|
+
# http.proxy? # => true
|
730
|
+
# http.proxy_address # => "proxy.example"
|
731
|
+
# # These use default values.
|
732
|
+
# http.proxy_port # => 80
|
733
|
+
# http.proxy_user # => nil
|
734
|
+
# http.proxy_pass # => nil
|
735
|
+
#
|
736
|
+
# The port, username, and password for the proxy may also be given:
|
737
|
+
#
|
738
|
+
# http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass')
|
739
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
|
740
|
+
# http.proxy? # => true
|
741
|
+
# http.proxy_address # => "proxy.example"
|
742
|
+
# http.proxy_port # => 8000
|
743
|
+
# http.proxy_user # => "pname"
|
744
|
+
# http.proxy_pass # => "ppass"
|
745
|
+
#
|
746
|
+
# <b>Proxy Using <tt>ENV['http_proxy']</tt></b>
|
747
|
+
#
|
748
|
+
# When environment variable <tt>'http_proxy'</tt>
|
749
|
+
# is set to a \URI string,
|
750
|
+
# the returned +http+ will have that URI as its proxy;
|
751
|
+
# note that the \URI string must have a protocol
|
752
|
+
# such as <tt>'http'</tt> or <tt>'https'</tt>:
|
753
|
+
#
|
754
|
+
# ENV['http_proxy'] = 'http://example.com'
|
755
|
+
# # => "http://example.com"
|
756
|
+
# http = Net::HTTP.new(hostname)
|
757
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
|
758
|
+
# http.proxy? # => true
|
759
|
+
# http.address # => "jsonplaceholder.typicode.com"
|
760
|
+
# http.proxy_address # => "example.com"
|
761
|
+
#
|
762
|
+
# The \URI string may include proxy username, password, and port number:
|
763
|
+
#
|
764
|
+
# ENV['http_proxy'] = 'http://pname:ppass@example.com:8000'
|
765
|
+
# # => "http://pname:ppass@example.com:8000"
|
766
|
+
# http = Net::HTTP.new(hostname)
|
767
|
+
# # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
|
768
|
+
# http.proxy_port # => 8000
|
769
|
+
# http.proxy_user # => "pname"
|
770
|
+
# http.proxy_pass # => "ppass"
|
637
771
|
#
|
638
|
-
#
|
639
|
-
# port the server operates on. If no +port+ is given the default port for
|
640
|
-
# HTTP or HTTPS is used.
|
772
|
+
# <b>Argument +p_no_proxy+</b>
|
641
773
|
#
|
642
|
-
#
|
643
|
-
# taken from the +http_proxy+ environment variable (or its uppercase
|
644
|
-
# equivalent) if present. If the proxy requires authentication you must
|
645
|
-
# supply it by hand. See URI::Generic#find_proxy for details of proxy
|
646
|
-
# detection from the environment. To disable proxy detection set +p_addr+
|
647
|
-
# to nil.
|
774
|
+
# You can use argument +p_no_proxy+ to reject certain proxies:
|
648
775
|
#
|
649
|
-
#
|
650
|
-
#
|
651
|
-
#
|
652
|
-
#
|
653
|
-
#
|
776
|
+
# - Reject a certain address:
|
777
|
+
#
|
778
|
+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
|
779
|
+
# http.proxy_address # => nil
|
780
|
+
#
|
781
|
+
# - Reject certain domains or subdomains:
|
782
|
+
#
|
783
|
+
# http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
|
784
|
+
# http.proxy_address # => nil
|
785
|
+
#
|
786
|
+
# - Reject certain addresses and port combinations:
|
787
|
+
#
|
788
|
+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234')
|
789
|
+
# http.proxy_address # => "proxy.example"
|
790
|
+
#
|
791
|
+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000')
|
792
|
+
# http.proxy_address # => nil
|
793
|
+
#
|
794
|
+
# - Reject a list of the types above delimited using a comma:
|
795
|
+
#
|
796
|
+
# http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
|
797
|
+
# http.proxy_address # => nil
|
798
|
+
#
|
799
|
+
# http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
|
800
|
+
# http.proxy_address # => nil
|
654
801
|
#
|
655
802
|
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
|
656
803
|
http = super address, port
|
@@ -717,6 +864,11 @@ module Net #:nodoc:
|
|
717
864
|
end
|
718
865
|
end
|
719
866
|
|
867
|
+
# Returns a string representation of +self+:
|
868
|
+
#
|
869
|
+
# Net::HTTP.new(hostname).inspect
|
870
|
+
# # => "#<Net::HTTP jsonplaceholder.typicode.com:80 open=false>"
|
871
|
+
#
|
720
872
|
def inspect
|
721
873
|
"#<#{self.class} #{@address}:#{@port} open=#{started?}>"
|
722
874
|
end
|
@@ -724,11 +876,51 @@ module Net #:nodoc:
|
|
724
876
|
# *WARNING* This method opens a serious security hole.
|
725
877
|
# Never use this method in production code.
|
726
878
|
#
|
727
|
-
# Sets
|
879
|
+
# Sets the output stream for debugging:
|
728
880
|
#
|
729
881
|
# http = Net::HTTP.new(hostname)
|
730
|
-
#
|
731
|
-
#
|
882
|
+
# File.open('t.tmp', 'w') do |file|
|
883
|
+
# http.set_debug_output(file)
|
884
|
+
# http.start
|
885
|
+
# http.get('/nosuch/1')
|
886
|
+
# http.finish
|
887
|
+
# end
|
888
|
+
# puts File.read('t.tmp')
|
889
|
+
#
|
890
|
+
# Output:
|
891
|
+
#
|
892
|
+
# opening connection to jsonplaceholder.typicode.com:80...
|
893
|
+
# opened
|
894
|
+
# <- "GET /nosuch/1 HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nHost: jsonplaceholder.typicode.com\r\n\r\n"
|
895
|
+
# -> "HTTP/1.1 404 Not Found\r\n"
|
896
|
+
# -> "Date: Mon, 12 Dec 2022 21:14:11 GMT\r\n"
|
897
|
+
# -> "Content-Type: application/json; charset=utf-8\r\n"
|
898
|
+
# -> "Content-Length: 2\r\n"
|
899
|
+
# -> "Connection: keep-alive\r\n"
|
900
|
+
# -> "X-Powered-By: Express\r\n"
|
901
|
+
# -> "X-Ratelimit-Limit: 1000\r\n"
|
902
|
+
# -> "X-Ratelimit-Remaining: 999\r\n"
|
903
|
+
# -> "X-Ratelimit-Reset: 1670879660\r\n"
|
904
|
+
# -> "Vary: Origin, Accept-Encoding\r\n"
|
905
|
+
# -> "Access-Control-Allow-Credentials: true\r\n"
|
906
|
+
# -> "Cache-Control: max-age=43200\r\n"
|
907
|
+
# -> "Pragma: no-cache\r\n"
|
908
|
+
# -> "Expires: -1\r\n"
|
909
|
+
# -> "X-Content-Type-Options: nosniff\r\n"
|
910
|
+
# -> "Etag: W/\"2-vyGp6PvFo4RvsFtPoIWeCReyIC8\"\r\n"
|
911
|
+
# -> "Via: 1.1 vegur\r\n"
|
912
|
+
# -> "CF-Cache-Status: MISS\r\n"
|
913
|
+
# -> "Server-Timing: cf-q-config;dur=1.3000000762986e-05\r\n"
|
914
|
+
# -> "Report-To: {\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=yOr40jo%2BwS1KHzhTlVpl54beJ5Wx2FcG4gGV0XVrh3X9OlR5q4drUn2dkt5DGO4GDcE%2BVXT7CNgJvGs%2BZleIyMu8CLieFiDIvOviOY3EhHg94m0ZNZgrEdpKD0S85S507l1vsEwEHkoTm%2Ff19SiO\"}],\"group\":\"cf-nel\",\"max_age\":604800}\r\n"
|
915
|
+
# -> "NEL: {\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}\r\n"
|
916
|
+
# -> "Server: cloudflare\r\n"
|
917
|
+
# -> "CF-RAY: 778977dc484ce591-DFW\r\n"
|
918
|
+
# -> "alt-svc: h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400\r\n"
|
919
|
+
# -> "\r\n"
|
920
|
+
# reading 2 bytes...
|
921
|
+
# -> "{}"
|
922
|
+
# read 2 bytes
|
923
|
+
# Conn keep-alive
|
732
924
|
#
|
733
925
|
def set_debug_output(output)
|
734
926
|
warn 'Net::HTTP#set_debug_output called after HTTP started', uplevel: 1 if started?
|
@@ -752,8 +944,24 @@ module Net #:nodoc:
|
|
752
944
|
# body encoding.
|
753
945
|
attr_reader :response_body_encoding
|
754
946
|
|
755
|
-
#
|
756
|
-
# the
|
947
|
+
# Sets the encoding to be used for the response body;
|
948
|
+
# returns the encoding.
|
949
|
+
#
|
950
|
+
# The given +value+ may be:
|
951
|
+
#
|
952
|
+
# - An Encoding object.
|
953
|
+
# - The name of an encoding.
|
954
|
+
# - An alias for an encoding name.
|
955
|
+
#
|
956
|
+
# See {Encoding}[https://docs.ruby-lang.org/en/master/Encoding.html].
|
957
|
+
#
|
958
|
+
# Examples:
|
959
|
+
#
|
960
|
+
# http = Net::HTTP.new(hostname)
|
961
|
+
# http.response_body_encoding = Encoding::US_ASCII # => #<Encoding:US-ASCII>
|
962
|
+
# http.response_body_encoding = 'US-ASCII' # => "US-ASCII"
|
963
|
+
# http.response_body_encoding = 'ASCII' # => "ASCII"
|
964
|
+
#
|
757
965
|
def response_body_encoding=(value)
|
758
966
|
value = Encoding.find(value) if value.is_a?(String)
|
759
967
|
@response_body_encoding = value
|
@@ -765,12 +973,37 @@ module Net #:nodoc:
|
|
765
973
|
attr_writer :proxy_user
|
766
974
|
attr_writer :proxy_pass
|
767
975
|
|
768
|
-
#
|
976
|
+
# Returns the IP address for the connection.
|
977
|
+
#
|
978
|
+
# If the session has not been started,
|
979
|
+
# returns the value set by #ipaddr=,
|
980
|
+
# or +nil+ if it has not been set:
|
981
|
+
#
|
982
|
+
# http = Net::HTTP.new(hostname)
|
983
|
+
# http.ipaddr # => nil
|
984
|
+
# http.ipaddr = '172.67.155.76'
|
985
|
+
# http.ipaddr # => "172.67.155.76"
|
986
|
+
#
|
987
|
+
# If the session has been started,
|
988
|
+
# returns the IP address from the socket:
|
989
|
+
#
|
990
|
+
# http = Net::HTTP.new(hostname)
|
991
|
+
# http.start
|
992
|
+
# http.ipaddr # => "172.67.155.76"
|
993
|
+
# http.finish
|
994
|
+
#
|
769
995
|
def ipaddr
|
770
996
|
started? ? @socket.io.peeraddr[3] : @ipaddr
|
771
997
|
end
|
772
998
|
|
773
|
-
#
|
999
|
+
# Sets the IP address for the connection:
|
1000
|
+
#
|
1001
|
+
# http = Net::HTTP.new(hostname)
|
1002
|
+
# http.ipaddr # => nil
|
1003
|
+
# http.ipaddr = '172.67.155.76'
|
1004
|
+
# http.ipaddr # => "172.67.155.76"
|
1005
|
+
#
|
1006
|
+
# The IP address may not be set if the session has been started.
|
774
1007
|
def ipaddr=(addr)
|
775
1008
|
raise IOError, "ipaddr value changed, but session already started" if started?
|
776
1009
|
@ipaddr = addr
|
@@ -795,12 +1028,18 @@ module Net #:nodoc:
|
|
795
1028
|
# Net::WriteTimeout is not raised on Windows.
|
796
1029
|
attr_reader :write_timeout
|
797
1030
|
|
798
|
-
#
|
1031
|
+
# Sets the maximum number of times to retry an idempotent request in case of
|
799
1032
|
# Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
|
800
1033
|
# Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
|
801
1034
|
# Timeout::Error.
|
802
|
-
#
|
803
|
-
#
|
1035
|
+
# The initial value is 1.
|
1036
|
+
#
|
1037
|
+
# Argument +retries+ must be a non-negative numeric value:
|
1038
|
+
#
|
1039
|
+
# http = Net::HTTP.new(hostname)
|
1040
|
+
# http.max_retries = 2 # => 2
|
1041
|
+
# http.max_retries # => 2
|
1042
|
+
#
|
804
1043
|
def max_retries=(retries)
|
805
1044
|
retries = retries.to_int
|
806
1045
|
if retries < 0
|
@@ -811,13 +1050,27 @@ module Net #:nodoc:
|
|
811
1050
|
|
812
1051
|
attr_reader :max_retries
|
813
1052
|
|
814
|
-
#
|
1053
|
+
# Sets the read timeout, in seconds, for +self+ to integer +sec+;
|
1054
|
+
# the initial value is 60.
|
1055
|
+
#
|
1056
|
+
# Argument +sec+ must be a non-negative numeric value:
|
1057
|
+
#
|
1058
|
+
# http = Net::HTTP.new(hostname)
|
1059
|
+
# http.read_timeout # => 60
|
1060
|
+
# http.get('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
|
1061
|
+
# http.read_timeout = 0
|
1062
|
+
# http.get('/todos/1') # Raises Net::ReadTimeout.
|
1063
|
+
#
|
815
1064
|
def read_timeout=(sec)
|
816
1065
|
@socket.read_timeout = sec if @socket
|
817
1066
|
@read_timeout = sec
|
818
1067
|
end
|
819
1068
|
|
820
|
-
#
|
1069
|
+
# Sets the write timeout, in seconds, for +self+ to integer +sec+;
|
1070
|
+
# the initial value is 60.
|
1071
|
+
#
|
1072
|
+
# Argument +sec+ must be a non-negative numeric value.
|
1073
|
+
#
|
821
1074
|
def write_timeout=(sec)
|
822
1075
|
@socket.write_timeout = sec if @socket
|
823
1076
|
@write_timeout = sec
|
@@ -1144,7 +1397,7 @@ module Net #:nodoc:
|
|
1144
1397
|
#
|
1145
1398
|
# This class is obsolete. You may pass these same parameters directly to
|
1146
1399
|
# Net::HTTP.new. See Net::HTTP.new for details of the arguments.
|
1147
|
-
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
|
1400
|
+
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
|
1148
1401
|
return self unless p_addr
|
1149
1402
|
|
1150
1403
|
Class.new(self) {
|