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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a306a97039eef1800016a611dee36fa0d342b5f234bf6e0a512d981abd12ce8d
4
- data.tar.gz: 39583accf1ec3b888c4e368eb0d3cf41add46ebfd18a6d7dcbea1cc69712f219
3
+ metadata.gz: cf933c8a700d19b030c85a6bbd7dc2d9dd5756d50076a13fabbc44284b76af6c
4
+ data.tar.gz: 6ed388df8dfe9f603c7a838a274d52940b2494fb35a7fe2eb5514fd0bfbc7e0f
5
5
  SHA512:
6
- metadata.gz: 21ab016c4d12100c405aec7b8426d81f4a88ac2bc7019353d729837d17e3a95899df04818f787319c32f29b349b7527b7903a65a897e8353382d5461070465c7
7
- data.tar.gz: 38593be710550d52aaf31d50d746e7f5da92fb1c3d4b451968bda3c0af9b033acefc2fd404f6a083025d340d8abebd7d2061fa77343f081b78bd48c19aa82b39
6
+ metadata.gz: cb250a681c61405474f625099b62ff0eaad5334e12ffaca3ce1ad9781bcfc61b289cae035fdbe7850f17255d117b918c2339c6b504b2783899dce64c14fc1291
7
+ data.tar.gz: 735ae93466d2c3e019b5c277549751a68708400a7d3ff88a55c1d17d3bfcc6c07d6147023f21cf138812dbca5a1a5b24ed5cd54b3c919a8723f24d88e69b8997
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ gemspec
4
4
 
5
5
  gem "rake"
6
6
  gem "test-unit"
7
+ gem "test-unit-ruby-core"
8
+ gem "webrick"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Net::Http
1
+ # Net::HTTP
2
2
 
3
3
  Net::HTTP provides a rich library which can be used to build HTTP
4
4
  user-agents. For more details about HTTP see
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'
@@ -0,0 +1,3 @@
1
+ This class also includes (indirectly) module Net::HTTPHeader,
2
+ which gives access to its
3
+ {methods for getting headers}[rdoc-ref:Net::HTTPHeader@Getters].
@@ -1,26 +1,40 @@
1
- # frozen_string_literal: false
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
- end
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
- Net::HTTPInformationCode = Net::HTTPInformation
19
- Net::HTTPSuccessCode = Net::HTTPSuccess
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
@@ -1,33 +1,34 @@
1
- # frozen_string_literal: false
2
- # Net::HTTP exception class.
3
- # You cannot use Net::HTTPExceptions directly; instead, you must use
4
- # its subclasses.
5
- module Net::HTTPExceptions
6
- def initialize(msg, res) #:nodoc:
7
- super msg
8
- @response = res
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
- # for compatibility
25
- Net::HTTPClientException = Net::HTTPServerException
15
+ class HTTPError < ProtocolError
16
+ include HTTPExceptions
17
+ end
26
18
 
27
- class Net::HTTPFatalError < Net::ProtoFatalError
28
- include Net::HTTPExceptions
29
- end
19
+ class HTTPRetriableError < ProtoRetriableError
20
+ include HTTPExceptions
21
+ end
30
22
 
31
- module Net
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: false
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
- # Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.
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
- raise ArgumentError, "no host component for URI" unless uri_or_path.hostname
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 << ":".freeze << @uri.port.to_s if @uri.port != @uri.default_port
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 @response_has_body and Net::HTTP::HAVE_ZLIB then
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
- # Automatically set to false if the user sets the Accept-Encoding header.
60
- # This indicates they wish to handle Content-encoding in responses
61
- # themselves.
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'.freeze
215
+ scheme = 'https'
139
216
  klass = URI::HTTPS
140
217
  else
141
- scheme = 'http'.freeze
218
+ scheme = 'http'
142
219
  klass = URI::HTTP
143
220
  end
144
221
 
145
222
  if host = self['host']
146
- host.sub!(/:.*/s, ''.freeze)
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
- # copy_stream can sendfile() to sock.io unless we use SSL.
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"