net-protocol 0.2.2 → 0.4.0

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: 7931f6f4ad36cdf9dffca852a9e6c224bb229269da14ac94b6ea06e030ad58e3
4
- data.tar.gz: acec2eea018af73a5d7c809230a91cf2017f7486cdf96d89960c529e3956da39
3
+ metadata.gz: f819b1703cd2631eb0c7d41fff4ca6a74dff9e88d740c5bfd93980ba2e8b3873
4
+ data.tar.gz: c4ac03ea98b451e2ddc9f38b0ff6b73d4341a6b09d01c923683ae354d9adcbb2
5
5
  SHA512:
6
- metadata.gz: 0524fe9693038d5da5bd8920ab921bacf8b641301c215986bbeca494bec701cfb6e313e6abd68db520e5c763c984818f47e0cf0da2d4f4ca2b9e25a497b49bd6
7
- data.tar.gz: aa4c7acb2896d4c2e22f23e25d4f6219c0126b110de700e6282b976edc327247f67dcf064d4d31eb9dfc084bdbca21da8802f382b72cb25971fc486d34e22368
6
+ metadata.gz: f9f88f0b96250c90ced8c159a4941efb190bb1de64b9bbed3cb393b295aaf9a60dc607038fcfcf7a2d39d983aa27a2ca9b61f07e04c8d7f8750c700dcb76ceda
7
+ data.tar.gz: 395718803914996cc958d50d30a56d1cb3856be7c940961059e19316476f5ae476366b42e38cd348508bc1850e13f51965e5a4631524683c63e252ff1088d571
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ BSDL
2
+ COPYING
3
+ README.md
4
+ lib/
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions
5
5
  are met:
6
6
  1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
7
+ notice, this list of conditions and the following disclaimer.
8
8
  2. Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
11
 
12
12
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
13
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/lib/net/protocol.rb CHANGED
@@ -21,12 +21,12 @@
21
21
 
22
22
  require 'socket'
23
23
  require 'timeout'
24
- require 'io/wait'
24
+ require 'io/wait' if RUBY_VERSION < '3.2'
25
25
 
26
26
  module Net # :nodoc:
27
27
 
28
28
  class Protocol #:nodoc: internal use only
29
- VERSION = "0.2.2"
29
+ VERSION = "0.4.0"
30
30
 
31
31
  private
32
32
  def Protocol.protocol_param(name, val)
@@ -54,9 +54,20 @@ module Net # :nodoc:
54
54
  s.connect
55
55
  end
56
56
  end
57
+
58
+ tcp_socket_parameters = TCPSocket.instance_method(:initialize).parameters
59
+ TCP_SOCKET_NEW_HAS_OPEN_TIMEOUT = if tcp_socket_parameters != [[:rest]]
60
+ tcp_socket_parameters.include?([:key, :open_timeout])
61
+ else
62
+ # Use Socket.tcp to find out since there is no parameters information for TCPSocket#initialize
63
+ # See discussion in https://github.com/ruby/net-http/pull/224
64
+ Socket.method(:tcp).parameters.include?([:key, :open_timeout])
65
+ end
66
+ private_constant :TCP_SOCKET_NEW_HAS_OPEN_TIMEOUT
57
67
  end
58
68
 
59
69
 
70
+ # :stopdoc:
60
71
  class ProtocolError < StandardError; end
61
72
  class ProtoSyntaxError < ProtocolError; end
62
73
  class ProtoFatalError < ProtocolError; end
@@ -66,6 +77,29 @@ module Net # :nodoc:
66
77
  class ProtoCommandError < ProtocolError; end
67
78
  class ProtoRetriableError < ProtocolError; end
68
79
  ProtocRetryError = ProtoRetriableError
80
+ # :startdoc:
81
+
82
+ ##
83
+ # ReadLimitExceeded, a subclass of ProtocolError, is raised if the
84
+ # terminator is not found within the byte limit given to
85
+ # Net::BufferedIO#readuntil.
86
+ #
87
+ # The limit is the largest result readuntil may return, counting the
88
+ # terminator itself, so a limit of 4 accepts "abc\n" and rejects
89
+ # "abcd\n". Unlike the limit of IO#gets it never truncates a result to
90
+ # fit. Either the whole thing comes back or this is raised, except
91
+ # under ignore_eof, which still returns what was buffered when the
92
+ # stream ended. The count is in bytes while the IO hands back binary
93
+ # strings, which every real one does.
94
+ #
95
+ # It bounds one call, not a connection. The unconsumed buffer can
96
+ # still run one BUFSIZE past the limit, and a peer sending endless
97
+ # short lines is not bounded at all.
98
+ #
99
+ # Nothing is consumed when this is raised, so the usual response is to
100
+ # close the connection rather than read on under a wider limit.
101
+
102
+ class ReadLimitExceeded < ProtocolError; end
69
103
 
70
104
  ##
71
105
  # OpenTimeout, a subclass of Timeout::Error, is raised if a connection cannot
@@ -78,6 +112,7 @@ module Net # :nodoc:
78
112
  # response cannot be read within the read_timeout.
79
113
 
80
114
  class ReadTimeout < Timeout::Error
115
+ # :stopdoc:
81
116
  def initialize(io = nil)
82
117
  @io = io
83
118
  end
@@ -97,6 +132,7 @@ module Net # :nodoc:
97
132
  # response cannot be written within the write_timeout. Not raised on Windows.
98
133
 
99
134
  class WriteTimeout < Timeout::Error
135
+ # :stopdoc:
100
136
  def initialize(io = nil)
101
137
  @io = io
102
138
  end
@@ -153,6 +189,7 @@ module Net # :nodoc:
153
189
  public
154
190
 
155
191
  def read(len, dest = ''.b, ignore_eof = false)
192
+ raise ArgumentError, "negative length #{len} given" if len < 0
156
193
  LOG "reading #{len} bytes..."
157
194
  read_bytes = 0
158
195
  begin
@@ -191,14 +228,31 @@ module Net # :nodoc:
191
228
  dest
192
229
  end
193
230
 
194
- def readuntil(terminator, ignore_eof = false)
231
+ def readuntil(terminator, ignore_eof = false, limit: nil)
232
+ unless limit.nil? || (Integer === limit && limit > 0)
233
+ # Integer === calls nothing on limit, and only an Integer is
234
+ # echoed back, so validation never runs the caller's code.
235
+ got = Integer === limit ? limit : "a non-Integer"
236
+ raise ArgumentError, "limit must be a positive Integer, got #{got}"
237
+ end
195
238
  offset = @rbuf_offset
196
239
  begin
197
240
  until idx = @rbuf.index(terminator, offset)
198
- offset = @rbuf.bytesize
241
+ if limit && rbuf_size > limit
242
+ raise ReadLimitExceeded, "exceeded the #{limit} byte read limit"
243
+ end
244
+ # Rewind so a terminator split across reads is still found. The
245
+ # floor guards two things. String#index reads a negative offset
246
+ # as counting from the end, and an offset below @rbuf_offset
247
+ # matches inside bytes already returned.
248
+ offset = [@rbuf.bytesize - terminator.bytesize + 1, @rbuf_offset].max
199
249
  rbuf_fill
200
250
  end
201
- return rbuf_consume(idx + terminator.bytesize - @rbuf_offset)
251
+ len = idx + terminator.bytesize - @rbuf_offset
252
+ if limit && len > limit
253
+ raise ReadLimitExceeded, "exceeded the #{limit} byte read limit"
254
+ end
255
+ return rbuf_consume(len)
202
256
  rescue EOFError
203
257
  raise unless ignore_eof
204
258
  return rbuf_consume
@@ -303,9 +357,9 @@ module Net # :nodoc:
303
357
  @debug_output << '<- ' if @debug_output
304
358
  yield
305
359
  @debug_output << "\n" if @debug_output
306
- bytes = @written_bytes
360
+ @written_bytes
361
+ ensure
307
362
  @written_bytes = nil
308
- bytes
309
363
  end
310
364
 
311
365
  def write0(*strs)
@@ -331,6 +385,9 @@ module Net # :nodoc:
331
385
  # next string
332
386
  end
333
387
  # continue looping
388
+ when :wait_readable
389
+ (io = @io.to_io).wait_readable(@write_timeout) or raise Net::WriteTimeout.new(io)
390
+ # continue looping
334
391
  when :wait_writable
335
392
  (io = @io.to_io).wait_writable(@write_timeout) or raise Net::WriteTimeout.new(io)
336
393
  # continue looping
@@ -373,12 +430,15 @@ module Net # :nodoc:
373
430
  def each_message_chunk
374
431
  LOG 'reading message...'
375
432
  LOG_off()
376
- read_bytes = 0
377
- while (line = readuntil("\r\n")) != ".\r\n"
378
- read_bytes += line.size
379
- yield line.delete_prefix('.')
433
+ begin
434
+ read_bytes = 0
435
+ while (line = readuntil("\r\n")) != ".\r\n"
436
+ read_bytes += line.size
437
+ yield line.delete_prefix('.')
438
+ end
439
+ ensure
440
+ LOG_on()
380
441
  end
381
- LOG_on()
382
442
  LOG "read message (#{read_bytes} bytes)"
383
443
  end
384
444
 
@@ -404,12 +464,15 @@ module Net # :nodoc:
404
464
  def write_message(src)
405
465
  LOG "writing message from #{src.class}"
406
466
  LOG_off()
407
- len = writing {
408
- using_each_crlf_line {
409
- write_message_0 src
467
+ begin
468
+ len = writing {
469
+ using_each_crlf_line {
470
+ write_message_0 src
471
+ }
410
472
  }
411
- }
412
- LOG_on()
473
+ ensure
474
+ LOG_on()
475
+ end
413
476
  LOG "wrote #{len} bytes"
414
477
  len
415
478
  end
@@ -417,16 +480,19 @@ module Net # :nodoc:
417
480
  def write_message_by_block(&block)
418
481
  LOG 'writing message from block'
419
482
  LOG_off()
420
- len = writing {
421
- using_each_crlf_line {
422
- begin
423
- block.call(WriteAdapter.new(self.method(:write_message_0)))
424
- rescue LocalJumpError
425
- # allow `break' from writer block
426
- end
483
+ begin
484
+ len = writing {
485
+ using_each_crlf_line {
486
+ begin
487
+ block.call(WriteAdapter.new(self.method(:write_message_0)))
488
+ rescue LocalJumpError
489
+ # allow `break' from writer block
490
+ end
491
+ }
427
492
  }
428
- }
429
- LOG_on()
493
+ ensure
494
+ LOG_on()
495
+ end
430
496
  LOG "wrote #{len} bytes"
431
497
  len
432
498
  end
@@ -446,6 +512,8 @@ module Net # :nodoc:
446
512
  write0 "\r\n"
447
513
  end
448
514
  write0 ".\r\n"
515
+ nil
516
+ ensure
449
517
  @wbuf = nil
450
518
  end
451
519
 
@@ -484,6 +552,7 @@ module Net # :nodoc:
484
552
  # The writer adapter class
485
553
  #
486
554
  class WriteAdapter
555
+ # :stopdoc:
487
556
  def initialize(writer)
488
557
  @writer = writer
489
558
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-11-07 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: timeout
@@ -31,17 +30,11 @@ executables: []
31
30
  extensions: []
32
31
  extra_rdoc_files: []
33
32
  files:
34
- - ".github/dependabot.yml"
35
- - ".github/workflows/test.yml"
36
- - ".gitignore"
37
- - Gemfile
38
- - LICENSE.txt
33
+ - ".document"
34
+ - BSDL
35
+ - COPYING
39
36
  - README.md
40
- - Rakefile
41
- - bin/console
42
- - bin/setup
43
37
  - lib/net/protocol.rb
44
- - net-protocol.gemspec
45
38
  homepage: https://github.com/ruby/net-protocol
46
39
  licenses:
47
40
  - Ruby
@@ -49,7 +42,7 @@ licenses:
49
42
  metadata:
50
43
  homepage_uri: https://github.com/ruby/net-protocol
51
44
  source_code_uri: https://github.com/ruby/net-protocol
52
- post_install_message:
45
+ changelog_uri: https://github.com/ruby/net-protocol/releases
53
46
  rdoc_options: []
54
47
  require_paths:
55
48
  - lib
@@ -64,8 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
57
  - !ruby/object:Gem::Version
65
58
  version: '0'
66
59
  requirements: []
67
- rubygems_version: 3.5.0.dev
68
- signing_key:
60
+ rubygems_version: 3.6.9
69
61
  specification_version: 4
70
62
  summary: The abstract interface for net-* client.
71
63
  test_files: []
@@ -1,6 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: 'github-actions'
4
- directory: '/'
5
- schedule:
6
- interval: 'weekly'
@@ -1,30 +0,0 @@
1
- name: test
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- ruby-verions:
7
- uses: ruby/actions/.github/workflows/ruby_versions.yml@master
8
- with:
9
- min_version: 2.6
10
-
11
- test:
12
- needs: ruby-verions
13
- name: build (${{ matrix.ruby }} / ${{ matrix.os }})
14
- strategy:
15
- matrix:
16
- ruby: ${{ fromJson(needs.ruby-verions.outputs.versions) }}
17
- os: [ ubuntu-latest, macos-latest, windows-latest ]
18
- exclude:
19
- - { os: windows-latest, ruby: truffleruby-head }
20
- - { os: windows-latest, ruby: truffleruby }
21
- runs-on: ${{ matrix.os }}
22
- steps:
23
- - uses: actions/checkout@v4
24
- - name: Set up Ruby
25
- uses: ruby/setup-ruby@v1
26
- with:
27
- ruby-version: ${{ matrix.ruby }}
28
- bundler-cache: true # 'bundle install' and cache gems
29
- - name: Run test
30
- run: bundle exec rake test
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
4
-
5
- gem "rake"
6
- gem "test-unit"
7
- gem "test-unit-ruby-core"
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test/lib"
6
- t.ruby_opts << "-rhelper"
7
- t.test_files = FileList["test/**/test_*.rb"]
8
- end
9
-
10
- task :default => :test
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "net/protocol"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/net-protocol.gemspec DELETED
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- name = File.basename(__FILE__, ".gemspec")
4
- version = ["lib", Array.new(name.count("-"), "..").join("/")].find do |dir|
5
- break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
6
- /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1
7
- end rescue nil
8
- end
9
-
10
- Gem::Specification.new do |spec|
11
- spec.name = name
12
- spec.version = version
13
- spec.authors = ["Yukihiro Matsumoto"]
14
- spec.email = ["matz@ruby-lang.org"]
15
-
16
- spec.summary = %q{The abstract interface for net-* client.}
17
- spec.description = %q{The abstract interface for net-* client.}
18
- spec.homepage = "https://github.com/ruby/net-protocol"
19
- spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
20
- spec.licenses = ["Ruby", "BSD-2-Clause"]
21
-
22
- spec.metadata["homepage_uri"] = spec.homepage
23
- spec.metadata["source_code_uri"] = spec.homepage
24
-
25
- # Specify which files should be added to the gem when it is released.
26
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
- `git ls-files -z 2>#{IO::NULL}`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
- end
30
- spec.require_paths = ["lib"]
31
-
32
- spec.add_dependency "timeout"
33
- end