net-http 0.1.1 → 0.4.1
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/Gemfile +2 -0
- data/README.md +1 -1
- data/Rakefile +0 -7
- data/doc/net-http/examples.rdoc +31 -0
- data/doc/net-http/included_getters.rdoc +3 -0
- data/lib/net/http/backward.rb +27 -13
- data/lib/net/http/exceptions.rb +28 -27
- data/lib/net/http/generic_request.rb +96 -21
- data/lib/net/http/header.rb +628 -163
- data/lib/net/http/proxy_delta.rb +1 -1
- data/lib/net/http/request.rb +73 -6
- data/lib/net/http/requests.rb +327 -25
- data/lib/net/http/response.rb +339 -28
- data/lib/net/http/responses.rb +1090 -223
- data/lib/net/http/status.rb +7 -6
- data/lib/net/http.rb +1458 -668
- data/lib/net/https.rb +1 -1
- data/net-http.gemspec +9 -6
- metadata +5 -20
- data/.github/workflows/test.yml +0 -24
- data/.gitignore +0 -8
- data/Gemfile.lock +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf933c8a700d19b030c85a6bbd7dc2d9dd5756d50076a13fabbc44284b76af6c
|
4
|
+
data.tar.gz: 6ed388df8dfe9f603c7a838a274d52940b2494fb35a7fe2eb5514fd0bfbc7e0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb250a681c61405474f625099b62ff0eaad5334e12ffaca3ce1ad9781bcfc61b289cae035fdbe7850f17255d117b918c2339c6b504b2783899dce64c14fc1291
|
7
|
+
data.tar.gz: 735ae93466d2c3e019b5c277549751a68708400a7d3ff88a55c1d17d3bfcc6c07d6147023f21cf138812dbca5a1a5b24ed5cd54b3c919a8723f24d88e69b8997
|
data/Gemfile
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -7,11 +7,4 @@ Rake::TestTask.new(:test) do |t|
|
|
7
7
|
t.test_files = FileList["test/**/test_*.rb"]
|
8
8
|
end
|
9
9
|
|
10
|
-
task :sync_tool do
|
11
|
-
require 'fileutils'
|
12
|
-
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
|
13
|
-
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
|
14
|
-
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
|
15
|
-
end
|
16
|
-
|
17
10
|
task :default => :test
|
@@ -0,0 +1,31 @@
|
|
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
|
+
path = uri.path # => "/"
|
17
|
+
port = uri.port # => 443
|
18
|
+
|
19
|
+
So that example requests may be written as:
|
20
|
+
|
21
|
+
Net::HTTP.get(uri)
|
22
|
+
Net::HTTP.get(hostname, '/index.html')
|
23
|
+
Net::HTTP.start(hostname) do |http|
|
24
|
+
http.get('/todos/1')
|
25
|
+
http.get('/todos/2')
|
26
|
+
end
|
27
|
+
|
28
|
+
An example that needs a modified URI first duplicates +uri+, then modifies the duplicate:
|
29
|
+
|
30
|
+
_uri = uri.dup
|
31
|
+
_uri.path = '/todos/1'
|
data/lib/net/http/backward.rb
CHANGED
@@ -1,26 +1,40 @@
|
|
1
|
-
# frozen_string_literal:
|
1
|
+
# frozen_string_literal: true
|
2
2
|
# for backward compatibility
|
3
3
|
|
4
4
|
# :enddoc:
|
5
5
|
|
6
6
|
class Net::HTTP
|
7
7
|
ProxyMod = ProxyDelta
|
8
|
-
|
9
|
-
|
10
|
-
module Net
|
11
|
-
HTTPSession = Net::HTTP
|
8
|
+
deprecate_constant :ProxyMod
|
12
9
|
end
|
13
10
|
|
14
11
|
module Net::NetPrivate
|
15
12
|
HTTPRequest = ::Net::HTTPRequest
|
13
|
+
deprecate_constant :HTTPRequest
|
16
14
|
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
Net::HTTPRedirectionCode = Net::HTTPRedirection
|
21
|
-
Net::HTTPRetriableCode = Net::HTTPRedirection
|
22
|
-
Net::HTTPClientErrorCode = Net::HTTPClientError
|
23
|
-
Net::HTTPFatalErrorCode = Net::HTTPClientError
|
24
|
-
Net::HTTPServerErrorCode = Net::HTTPServerError
|
25
|
-
Net::HTTPResponceReceiver = Net::HTTPResponse
|
16
|
+
module Net
|
17
|
+
HTTPSession = HTTP
|
26
18
|
|
19
|
+
HTTPInformationCode = HTTPInformation
|
20
|
+
HTTPSuccessCode = HTTPSuccess
|
21
|
+
HTTPRedirectionCode = HTTPRedirection
|
22
|
+
HTTPRetriableCode = HTTPRedirection
|
23
|
+
HTTPClientErrorCode = HTTPClientError
|
24
|
+
HTTPFatalErrorCode = HTTPClientError
|
25
|
+
HTTPServerErrorCode = HTTPServerError
|
26
|
+
HTTPResponseReceiver = HTTPResponse
|
27
|
+
|
28
|
+
HTTPResponceReceiver = HTTPResponse # Typo since 2001
|
29
|
+
|
30
|
+
deprecate_constant :HTTPSession,
|
31
|
+
:HTTPInformationCode,
|
32
|
+
:HTTPSuccessCode,
|
33
|
+
:HTTPRedirectionCode,
|
34
|
+
:HTTPRetriableCode,
|
35
|
+
:HTTPClientErrorCode,
|
36
|
+
:HTTPFatalErrorCode,
|
37
|
+
:HTTPServerErrorCode,
|
38
|
+
:HTTPResponseReceiver,
|
39
|
+
:HTTPResponceReceiver
|
40
|
+
end
|
data/lib/net/http/exceptions.rb
CHANGED
@@ -1,33 +1,34 @@
|
|
1
|
-
# frozen_string_literal:
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Net
|
3
|
+
# Net::HTTP exception class.
|
4
|
+
# You cannot use Net::HTTPExceptions directly; instead, you must use
|
5
|
+
# its subclasses.
|
6
|
+
module HTTPExceptions
|
7
|
+
def initialize(msg, res) #:nodoc:
|
8
|
+
super msg
|
9
|
+
@response = res
|
10
|
+
end
|
11
|
+
attr_reader :response
|
12
|
+
alias data response #:nodoc: obsolete
|
9
13
|
end
|
10
|
-
attr_reader :response
|
11
|
-
alias data response #:nodoc: obsolete
|
12
|
-
end
|
13
|
-
class Net::HTTPError < Net::ProtocolError
|
14
|
-
include Net::HTTPExceptions
|
15
|
-
end
|
16
|
-
class Net::HTTPRetriableError < Net::ProtoRetriableError
|
17
|
-
include Net::HTTPExceptions
|
18
|
-
end
|
19
|
-
class Net::HTTPServerException < Net::ProtoServerError
|
20
|
-
# We cannot use the name "HTTPServerError", it is the name of the response.
|
21
|
-
include Net::HTTPExceptions
|
22
|
-
end
|
23
14
|
|
24
|
-
|
25
|
-
|
15
|
+
class HTTPError < ProtocolError
|
16
|
+
include HTTPExceptions
|
17
|
+
end
|
26
18
|
|
27
|
-
class
|
28
|
-
|
29
|
-
end
|
19
|
+
class HTTPRetriableError < ProtoRetriableError
|
20
|
+
include HTTPExceptions
|
21
|
+
end
|
30
22
|
|
31
|
-
|
23
|
+
class HTTPClientException < ProtoServerError
|
24
|
+
include HTTPExceptions
|
25
|
+
end
|
26
|
+
|
27
|
+
class HTTPFatalError < ProtoFatalError
|
28
|
+
include HTTPExceptions
|
29
|
+
end
|
30
|
+
|
31
|
+
# We cannot use the name "HTTPServerError", it is the name of the response.
|
32
|
+
HTTPServerException = HTTPClientException # :nodoc:
|
32
33
|
deprecate_constant(:HTTPServerException)
|
33
34
|
end
|
@@ -1,24 +1,29 @@
|
|
1
|
-
# frozen_string_literal:
|
2
|
-
# HTTPGenericRequest is the parent of the Net::HTTPRequest class.
|
3
|
-
# Do not use this directly; use a subclass of Net::HTTPRequest.
|
1
|
+
# frozen_string_literal: true
|
4
2
|
#
|
5
|
-
#
|
3
|
+
# \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
|
4
|
+
#
|
5
|
+
# Do not use this directly; instead, use a subclass of Net::HTTPRequest.
|
6
|
+
#
|
7
|
+
# == About the Examples
|
8
|
+
#
|
9
|
+
# :include: doc/net-http/examples.rdoc
|
6
10
|
#
|
7
11
|
class Net::HTTPGenericRequest
|
8
12
|
|
9
13
|
include Net::HTTPHeader
|
10
14
|
|
11
|
-
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
|
15
|
+
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
|
12
16
|
@method = m
|
13
17
|
@request_has_body = reqbody
|
14
18
|
@response_has_body = resbody
|
15
19
|
|
16
20
|
if URI === uri_or_path then
|
17
21
|
raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path
|
18
|
-
|
22
|
+
hostname = uri_or_path.hostname
|
23
|
+
raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
|
19
24
|
@uri = uri_or_path.dup
|
20
25
|
host = @uri.hostname.dup
|
21
|
-
host << ":"
|
26
|
+
host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
|
22
27
|
@path = uri_or_path.request_uri
|
23
28
|
raise ArgumentError, "no HTTP request path given" unless @path
|
24
29
|
else
|
@@ -31,12 +36,12 @@ class Net::HTTPGenericRequest
|
|
31
36
|
|
32
37
|
@decode_content = false
|
33
38
|
|
34
|
-
if
|
39
|
+
if Net::HTTP::HAVE_ZLIB then
|
35
40
|
if !initheader ||
|
36
41
|
!initheader.keys.any? { |k|
|
37
42
|
%w[accept-encoding range].include? k.downcase
|
38
43
|
} then
|
39
|
-
@decode_content = true
|
44
|
+
@decode_content = true if @response_has_body
|
40
45
|
initheader = initheader ? initheader.dup : {}
|
41
46
|
initheader["accept-encoding"] =
|
42
47
|
"gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
|
@@ -52,15 +57,47 @@ class Net::HTTPGenericRequest
|
|
52
57
|
@body_data = nil
|
53
58
|
end
|
54
59
|
|
60
|
+
# Returns the string method name for the request:
|
61
|
+
#
|
62
|
+
# Net::HTTP::Get.new(uri).method # => "GET"
|
63
|
+
# Net::HTTP::Post.new(uri).method # => "POST"
|
64
|
+
#
|
55
65
|
attr_reader :method
|
66
|
+
|
67
|
+
# Returns the string path for the request:
|
68
|
+
#
|
69
|
+
# Net::HTTP::Get.new(uri).path # => "/"
|
70
|
+
# Net::HTTP::Post.new('example.com').path # => "example.com"
|
71
|
+
#
|
56
72
|
attr_reader :path
|
73
|
+
|
74
|
+
# Returns the URI object for the request, or +nil+ if none:
|
75
|
+
#
|
76
|
+
# Net::HTTP::Get.new(uri).uri
|
77
|
+
# # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
|
78
|
+
# Net::HTTP::Get.new('example.com').uri # => nil
|
79
|
+
#
|
57
80
|
attr_reader :uri
|
58
81
|
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
82
|
+
# Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
|
83
|
+
# has been set manually or deleted
|
84
|
+
# (indicating that the user intends to handle encoding in the response),
|
85
|
+
# +true+ otherwise:
|
86
|
+
#
|
87
|
+
# req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
|
88
|
+
# req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
|
89
|
+
# req.decode_content # => true
|
90
|
+
# req['Accept-Encoding'] = 'foo'
|
91
|
+
# req.decode_content # => false
|
92
|
+
# req.delete('Accept-Encoding')
|
93
|
+
# req.decode_content # => false
|
94
|
+
#
|
62
95
|
attr_reader :decode_content
|
63
96
|
|
97
|
+
# Returns a string representation of the request:
|
98
|
+
#
|
99
|
+
# Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
|
100
|
+
#
|
64
101
|
def inspect
|
65
102
|
"\#<#{self.class} #{@method}>"
|
66
103
|
end
|
@@ -75,21 +112,45 @@ class Net::HTTPGenericRequest
|
|
75
112
|
super key, val
|
76
113
|
end
|
77
114
|
|
115
|
+
# Returns whether the request may have a body:
|
116
|
+
#
|
117
|
+
# Net::HTTP::Post.new(uri).request_body_permitted? # => true
|
118
|
+
# Net::HTTP::Get.new(uri).request_body_permitted? # => false
|
119
|
+
#
|
78
120
|
def request_body_permitted?
|
79
121
|
@request_has_body
|
80
122
|
end
|
81
123
|
|
124
|
+
# Returns whether the response may have a body:
|
125
|
+
#
|
126
|
+
# Net::HTTP::Post.new(uri).response_body_permitted? # => true
|
127
|
+
# Net::HTTP::Head.new(uri).response_body_permitted? # => false
|
128
|
+
#
|
82
129
|
def response_body_permitted?
|
83
130
|
@response_has_body
|
84
131
|
end
|
85
132
|
|
86
|
-
def body_exist?
|
133
|
+
def body_exist? # :nodoc:
|
87
134
|
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
|
88
135
|
response_body_permitted?
|
89
136
|
end
|
90
137
|
|
138
|
+
# Returns the string body for the request, or +nil+ if there is none:
|
139
|
+
#
|
140
|
+
# req = Net::HTTP::Post.new(uri)
|
141
|
+
# req.body # => nil
|
142
|
+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
|
143
|
+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
|
144
|
+
#
|
91
145
|
attr_reader :body
|
92
146
|
|
147
|
+
# Sets the body for the request:
|
148
|
+
#
|
149
|
+
# req = Net::HTTP::Post.new(uri)
|
150
|
+
# req.body # => nil
|
151
|
+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
|
152
|
+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
|
153
|
+
#
|
93
154
|
def body=(str)
|
94
155
|
@body = str
|
95
156
|
@body_stream = nil
|
@@ -97,8 +158,24 @@ class Net::HTTPGenericRequest
|
|
97
158
|
str
|
98
159
|
end
|
99
160
|
|
161
|
+
# Returns the body stream object for the request, or +nil+ if there is none:
|
162
|
+
#
|
163
|
+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
|
164
|
+
# req.body_stream # => nil
|
165
|
+
# require 'stringio'
|
166
|
+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
|
167
|
+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
|
168
|
+
#
|
100
169
|
attr_reader :body_stream
|
101
170
|
|
171
|
+
# Sets the body stream for the request:
|
172
|
+
#
|
173
|
+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
|
174
|
+
# req.body_stream # => nil
|
175
|
+
# require 'stringio'
|
176
|
+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
|
177
|
+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
|
178
|
+
#
|
102
179
|
def body_stream=(input)
|
103
180
|
@body = nil
|
104
181
|
@body_stream = input
|
@@ -135,15 +212,15 @@ class Net::HTTPGenericRequest
|
|
135
212
|
return unless @uri
|
136
213
|
|
137
214
|
if ssl
|
138
|
-
scheme = 'https'
|
215
|
+
scheme = 'https'
|
139
216
|
klass = URI::HTTPS
|
140
217
|
else
|
141
|
-
scheme = 'http'
|
218
|
+
scheme = 'http'
|
142
219
|
klass = URI::HTTP
|
143
220
|
end
|
144
221
|
|
145
222
|
if host = self['host']
|
146
|
-
host.sub!(/:.*/
|
223
|
+
host.sub!(/:.*/m, '')
|
147
224
|
elsif host = @uri.host
|
148
225
|
else
|
149
226
|
host = addr
|
@@ -202,9 +279,7 @@ class Net::HTTPGenericRequest
|
|
202
279
|
IO.copy_stream(f, chunker)
|
203
280
|
chunker.finish
|
204
281
|
else
|
205
|
-
|
206
|
-
# If sock.io is an SSLSocket, copy_stream will hit SSL_write()
|
207
|
-
IO.copy_stream(f, sock.io)
|
282
|
+
IO.copy_stream(f, sock)
|
208
283
|
end
|
209
284
|
end
|
210
285
|
|
@@ -241,7 +316,7 @@ class Net::HTTPGenericRequest
|
|
241
316
|
boundary ||= SecureRandom.urlsafe_base64(40)
|
242
317
|
chunked_p = chunked?
|
243
318
|
|
244
|
-
buf = ''
|
319
|
+
buf = +''
|
245
320
|
params.each do |key, value, h={}|
|
246
321
|
key = quote_string(key, charset)
|
247
322
|
filename =
|
@@ -326,7 +401,7 @@ class Net::HTTPGenericRequest
|
|
326
401
|
if /[\r\n]/ =~ reqline
|
327
402
|
raise ArgumentError, "A Request-Line must not contain CR or LF"
|
328
403
|
end
|
329
|
-
buf =
|
404
|
+
buf = +''
|
330
405
|
buf << reqline << "\r\n"
|
331
406
|
each_capitalized do |k,v|
|
332
407
|
buf << "#{k}: #{v}\r\n"
|