protocol-http 0.18.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8d2f576f6e7e6ce261d80fbdc0e38e2775b2f22ac156958b1e1b91472fdfec3
4
- data.tar.gz: b450d401f5791db371761fd52a6c1adb32807a13659570a1ad0ef0d5ef3dd8d5
3
+ metadata.gz: 3618371cb38127c249c684947eed20383453d437b5fcf00bbcadcb81cce6e96e
4
+ data.tar.gz: a948eb7f70ffe1824491d354acd4b42f6540100f9bb8a788ed85bf5f30f538a8
5
5
  SHA512:
6
- metadata.gz: 0be25839110052bd336f729800768eb8d8ef51bf15c2852b9bc526e20a6998177dadfc4e71b889681b16ba263f25dc14ea42e0835ce4f87de7d343936ff91ea8
7
- data.tar.gz: bf48aa19e331906fcf518eed7c6c94243656856f034d6c44a90ecf41c0bd1807c1a540d836fc61d63d6cd3acc2a769b37f1898b608296578edc45866747659a6
6
+ metadata.gz: 062aaccaeb9e2fc71505490fb263d78bbc1201983adf12011837d89194d22f0b6a75618caf3083e7ea93aa122603d8b348a53e28565560f5983d0eaa85f31b05
7
+ data.tar.gz: f0d7eed0eb10015c066751781c2d3f64e0c965a3f0cd4dafa12504fca56013344134a9e513a36f0b3d2259f2c1c1e86fc8d496bb9ca19a1ee6d1b7c950a76f88
@@ -56,6 +56,7 @@ module Protocol
56
56
  @length = length
57
57
 
58
58
  @index = 0
59
+ @digest = nil
59
60
  end
60
61
 
61
62
  attr :chunks
@@ -72,6 +73,11 @@ module Protocol
72
73
  @index >= @chunks.length
73
74
  end
74
75
 
76
+ # A buffered response is always ready.
77
+ def ready?
78
+ true
79
+ end
80
+
75
81
  def read
76
82
  if chunk = @chunks[@index]
77
83
  @index += 1
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -25,8 +25,8 @@ require_relative 'wrapper'
25
25
  module Protocol
26
26
  module HTTP
27
27
  module Body
28
- # Invokes a callback once the body has finished reading.
29
- class Streamable < Wrapper
28
+ # Invokes a callback once the body has completed, either successfully or due to an error.
29
+ class Completable < Wrapper
30
30
  def self.wrap(message, &block)
31
31
  if body = message&.body and !body.empty?
32
32
  message.body = self.new(message.body, block)
@@ -65,6 +65,10 @@ module Protocol
65
65
  @remaining == 0
66
66
  end
67
67
 
68
+ def ready?
69
+ true
70
+ end
71
+
68
72
  def rewind
69
73
  @file.seek(@offset)
70
74
  end
@@ -42,6 +42,10 @@ module Protocol
42
42
  true
43
43
  end
44
44
 
45
+ def ready?
46
+ true
47
+ end
48
+
45
49
  def length
46
50
  @length
47
51
  end
@@ -44,6 +44,13 @@ module Protocol
44
44
  false
45
45
  end
46
46
 
47
+ # Whether calling read will block.
48
+ # We prefer pessimistic implementation, and thus default to `false`.
49
+ # @return [Boolean]
50
+ def ready?
51
+ false
52
+ end
53
+
47
54
  def length
48
55
  nil
49
56
  end
@@ -53,6 +60,21 @@ module Protocol
53
60
  nil
54
61
  end
55
62
 
63
+ # Should the internal mechanism prefer to use {call}?
64
+ # @returns [Boolean]
65
+ def stream?
66
+ false
67
+ end
68
+
69
+ # Write the body to the given stream.
70
+ def call(stream)
71
+ while chunk = self.read
72
+ stream.write(chunk)
73
+ end
74
+ ensure
75
+ stream.close($!)
76
+ end
77
+
56
78
  # Read all remaining chunks into a buffered body and close the underlying input.
57
79
  def finish
58
80
  # Internally, this invokes `self.each` which then invokes `self.close`.
@@ -39,6 +39,10 @@ module Protocol
39
39
  (@index >= @chunks.size) && super
40
40
  end
41
41
 
42
+ def ready?
43
+ (@index < @chunks.size) || super
44
+ end
45
+
42
46
  # A rewindable body wraps some other body. Convert it to a buffered body
43
47
  def buffered
44
48
  Buffered.new(@chunks)
@@ -55,6 +55,10 @@ module Protocol
55
55
  @body.empty?
56
56
  end
57
57
 
58
+ def ready?
59
+ @body.ready?
60
+ end
61
+
58
62
  def length
59
63
  @body.length
60
64
  end
@@ -67,6 +71,14 @@ module Protocol
67
71
  def inspect
68
72
  @body.inspect
69
73
  end
74
+
75
+ def stream?
76
+ @body.stream?
77
+ end
78
+
79
+ def call(stream)
80
+ @body.call(stream)
81
+ end
70
82
  end
71
83
  end
72
84
  end
@@ -40,7 +40,7 @@ module Protocol
40
40
  URL.escape(@value)
41
41
  end
42
42
 
43
- def to_str
43
+ def to_s
44
44
  buffer = String.new.b
45
45
 
46
46
  buffer << encoded_name << '=' << encoded_value
@@ -26,21 +26,23 @@ module Protocol
26
26
  module HTTP
27
27
  module Header
28
28
  # Used for basic authorization.
29
- # @example headers.add('authorization', Authorization.new("samuel", "password"))
30
- class Authorization
31
- KEY = "Authorization"
32
-
33
- def initialize(username, password)
34
- @username = username
35
- @password = password
36
- end
37
-
38
- def encoded
39
- "#{@username}:#{@password}"
29
+ #
30
+ # ~~~ ruby
31
+ # headers.add('authorization', Authorization.basic("my_username", "my_password"))
32
+ # ~~~
33
+ class Authorization < String
34
+ # Splits the header and
35
+ # @return [Tuple(String, String)]
36
+ def credentials
37
+ self.split(/\s+/, 2)
40
38
  end
41
39
 
42
- def to_str
43
- 'Basic %s' % Base64.strict_encode64(self.encoded)
40
+ def self.basic(username, password)
41
+ encoded = "#{username}:#{password}"
42
+
43
+ self.new(
44
+ "Basic #{Base64.strict_encode64(encoded)}"
45
+ )
44
46
  end
45
47
  end
46
48
  end
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require_relative 'split'
24
-
25
23
  module Protocol
26
24
  module HTTP
27
25
  module Header
@@ -28,6 +28,7 @@ require_relative 'header/cache_control'
28
28
  require_relative 'header/etag'
29
29
  require_relative 'header/etags'
30
30
  require_relative 'header/vary'
31
+ require_relative 'header/authorization'
31
32
 
32
33
  module Protocol
33
34
  module HTTP
@@ -36,7 +37,7 @@ module Protocol
36
37
  Split = Header::Split
37
38
  Multiple = Header::Multiple
38
39
 
39
- TRAILERS = 'trailers'
40
+ TRAILER = 'trailer'
40
41
 
41
42
  # Construct an instance from a headers Array or Hash. No-op if already an instance of `Headers`. If the underlying array is frozen, it will be duped.
42
43
  # @return [Headers] an instance of headers.
@@ -66,7 +67,7 @@ module Protocol
66
67
  @fields = fields
67
68
  @indexed = indexed
68
69
 
69
- # Marks where trailers start in the @fields array.
70
+ # Marks where trailer start in the @fields array.
70
71
  @tail = nil
71
72
  end
72
73
 
@@ -83,10 +84,10 @@ module Protocol
83
84
  @tail = nil
84
85
  end
85
86
 
86
- # Flatten trailers into the headers.
87
+ # Flatten trailer into the headers.
87
88
  def flatten!
88
89
  if @tail
89
- self.delete(TRAILERS)
90
+ self.delete(TRAILER)
90
91
  @tail = nil
91
92
  end
92
93
 
@@ -100,27 +101,27 @@ module Protocol
100
101
  # An array of `[key, value]` pairs.
101
102
  attr :fields
102
103
 
103
- # @return the trailers if there are any.
104
- def trailers?
104
+ # @return the trailer if there are any.
105
+ def trailer?
105
106
  @tail != nil
106
107
  end
107
108
 
108
- # Record the current headers, and prepare to receive trailers.
109
- def trailers!(&block)
110
- return nil unless self.include?(TRAILERS)
109
+ # Record the current headers, and prepare to receive trailer.
110
+ def trailer!(&block)
111
+ return nil unless self.include?(TRAILER)
111
112
 
112
113
  @tail ||= @fields.size
113
114
 
114
- return to_enum(:trailers!) unless block_given?
115
+ return to_enum(:trailer!) unless block_given?
115
116
 
116
117
  if @tail
117
118
  @fields.drop(@tail).each(&block)
118
119
  end
119
120
  end
120
121
 
121
- # Enumerate all trailers, if there are any.
122
- def trailers(&block)
123
- return to_enum(:trailers) unless block_given?
122
+ # Enumerate all headers in the trailer, if there are any.
123
+ def trailer(&block)
124
+ return to_enum(:trailer) unless block_given?
124
125
 
125
126
  if @tail
126
127
  @fields.drop(@tail).each(&block)
@@ -170,6 +171,7 @@ module Protocol
170
171
  end
171
172
 
172
173
  # Add the specified header key value pair.
174
+ #
173
175
  # @param key [String] the header key.
174
176
  # @param value [String] the header value to assign.
175
177
  def add(key, value)
@@ -216,8 +218,6 @@ module Protocol
216
218
  'user-agent' => false,
217
219
  'referer' => false,
218
220
  'host' => false,
219
- 'authorization' => false,
220
- 'proxy-authorization' => false,
221
221
  'if-modified-since' => false,
222
222
  'if-unmodified-since' => false,
223
223
  'from' => false,
@@ -233,6 +233,10 @@ module Protocol
233
233
  'via' => Split,
234
234
  'x-forwarded-for' => Split,
235
235
 
236
+ # Authorization headers:
237
+ 'authorization' => Header::Authorization,
238
+ 'proxy-authorization' => Header::Authorization,
239
+
236
240
  # Cache validations:
237
241
  'etag' => Header::ETag,
238
242
  'if-match' => Header::ETags,
@@ -53,7 +53,7 @@ module Protocol
53
53
  self.each do |name, value|
54
54
  define_method(name.downcase) do |location, headers = nil, body = nil|
55
55
  self.call(
56
- Request[value, location.to_str, Headers[headers], body]
56
+ Request[value, location.to_s, Headers[headers], body]
57
57
  )
58
58
  end
59
59
  end
@@ -110,12 +110,10 @@ module Protocol
110
110
  return buffer
111
111
  end
112
112
 
113
- def to_str
113
+ def to_s
114
114
  append(String.new)
115
115
  end
116
116
 
117
- alias to_s to_str
118
-
119
117
  # Merges two references as specified by RFC2396, similar to `URI.join`.
120
118
  def + other
121
119
  other = self.class[other]
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Protocol
24
24
  module HTTP
25
- VERSION = "0.18.0"
25
+ VERSION = "0.22.0"
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-08 00:00:00.000000000 Z
11
+ date: 2021-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: covered
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bundler
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +25,7 @@ dependencies:
39
25
  - !ruby/object:Gem::Version
40
26
  version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
- name: bake-bundler
28
+ name: covered
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
@@ -66,22 +52,16 @@ dependencies:
66
52
  - - ">="
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
- description:
55
+ description:
70
56
  email:
71
- - samuel.williams@oriontransfer.co.nz
72
57
  executables: []
73
58
  extensions: []
74
59
  extra_rdoc_files: []
75
60
  files:
76
- - ".editorconfig"
77
- - ".gitignore"
78
- - ".rspec"
79
- - ".travis.yml"
80
- - Gemfile
81
- - README.md
82
61
  - lib/protocol/http.rb
83
62
  - lib/protocol/http/accept_encoding.rb
84
63
  - lib/protocol/http/body/buffered.rb
64
+ - lib/protocol/http/body/completable.rb
85
65
  - lib/protocol/http/body/deflate.rb
86
66
  - lib/protocol/http/body/digestable.rb
87
67
  - lib/protocol/http/body/file.rb
@@ -91,7 +71,6 @@ files:
91
71
  - lib/protocol/http/body/reader.rb
92
72
  - lib/protocol/http/body/rewindable.rb
93
73
  - lib/protocol/http/body/stream.rb
94
- - lib/protocol/http/body/streamable.rb
95
74
  - lib/protocol/http/body/wrapper.rb
96
75
  - lib/protocol/http/content_encoding.rb
97
76
  - lib/protocol/http/cookie.rb
@@ -114,18 +93,17 @@ files:
114
93
  - lib/protocol/http/response.rb
115
94
  - lib/protocol/http/url.rb
116
95
  - lib/protocol/http/version.rb
117
- - protocol-http.gemspec
118
96
  homepage: https://github.com/socketry/protocol-http
119
97
  licenses:
120
98
  - MIT
121
99
  metadata: {}
122
- post_install_message:
100
+ post_install_message:
123
101
  rdoc_options: []
124
102
  require_paths:
125
103
  - lib
126
104
  required_ruby_version: !ruby/object:Gem::Requirement
127
105
  requirements:
128
- - - "~>"
106
+ - - ">="
129
107
  - !ruby/object:Gem::Version
130
108
  version: '2.5'
131
109
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -135,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
113
  version: '0'
136
114
  requirements: []
137
115
  rubygems_version: 3.1.2
138
- signing_key:
116
+ signing_key:
139
117
  specification_version: 4
140
118
  summary: Provides abstractions to handle HTTP protocols.
141
119
  test_files: []
data/.editorconfig DELETED
@@ -1,6 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- indent_style = tab
5
- indent_size = 2
6
-
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
- Gemfile.lock
13
- .covered.db
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --warnings
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,22 +0,0 @@
1
- language: ruby
2
- dist: xenial
3
- cache: bundler
4
-
5
- script: bundle exec rspec
6
-
7
- matrix:
8
- include:
9
- - rvm: 2.5
10
- - rvm: 2.6
11
- - rvm: 2.7
12
- - rvm: 2.6
13
- env: COVERAGE=PartialSummary,Coveralls
14
- - rvm: 2.7
15
- - rvm: truffleruby
16
- - rvm: jruby-head
17
- env: JRUBY_OPTS="--debug -X+O"
18
- - rvm: ruby-head
19
- allow_failures:
20
- - rvm: truffleruby
21
- - rvm: ruby-head
22
- - rvm: jruby-head
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in protocol-http.gemspec
6
- gemspec
7
-
8
- group :test do
9
- gem 'async-io'
10
- gem 'async-rspec'
11
- end
data/README.md DELETED
@@ -1,90 +0,0 @@
1
- # Protocol::HTTP
2
-
3
- Provides abstractions for working with the HTTP protocol.
4
-
5
- [![Build Status](https://travis-ci.com/socketry/protocol-http.svg?branch=master)](http://travis-ci.com/socketry/protocol-http)
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'protocol-http'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install protocol-http
22
-
23
- ## Usage
24
-
25
- ### Headers
26
-
27
- ```ruby
28
- require 'protocol/http/headers'
29
-
30
- headers = Protocol::HTTP::Headers.new
31
-
32
- headers['Content-Type'] = "image/jpeg"
33
-
34
- headers['content-type']
35
- # => "image/jpeg"
36
- ```
37
-
38
- ### Reference
39
-
40
- ```ruby
41
- require 'protocol/http/reference'
42
-
43
- reference = Protocol::HTTP::Reference.new("/search", q: 'kittens')
44
-
45
- reference.to_s
46
- # => "/search?q=kittens"
47
- ```
48
-
49
- ### URL
50
-
51
- ```ruby
52
- require 'protocol/http/url'
53
-
54
- reference = Protocol::HTTP::Reference.parse("/search?q=kittens")
55
-
56
- parameters = Protocol::HTTP::URL.decode(reference.query_string)
57
- # => {"q"=>"kittens"}
58
- ```
59
-
60
- ## Contributing
61
-
62
- 1. Fork it
63
- 2. Create your feature branch (`git checkout -b my-new-feature`)
64
- 3. Commit your changes (`git commit -am 'Add some feature'`)
65
- 4. Push to the branch (`git push origin my-new-feature`)
66
- 5. Create new Pull Request
67
-
68
- ## License
69
-
70
- Released under the MIT license.
71
-
72
- Copyright, 2019, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
73
-
74
- Permission is hereby granted, free of charge, to any person obtaining a copy
75
- of this software and associated documentation files (the "Software"), to deal
76
- in the Software without restriction, including without limitation the rights
77
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
- copies of the Software, and to permit persons to whom the Software is
79
- furnished to do so, subject to the following conditions:
80
-
81
- The above copyright notice and this permission notice shall be included in
82
- all copies or substantial portions of the Software.
83
-
84
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
90
- THE SOFTWARE.
@@ -1,26 +0,0 @@
1
-
2
- require_relative "lib/protocol/http/version"
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = "protocol-http"
6
- spec.version = Protocol::HTTP::VERSION
7
- spec.authors = ["Samuel Williams"]
8
- spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
-
10
- spec.summary = "Provides abstractions to handle HTTP protocols."
11
- spec.homepage = "https://github.com/socketry/protocol-http"
12
- spec.license = "MIT"
13
-
14
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
- f.match(%r{^(test|spec|features)/})
16
- end
17
-
18
- spec.required_ruby_version = '~> 2.5'
19
-
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_development_dependency "covered"
23
- spec.add_development_dependency "bundler"
24
- spec.add_development_dependency "bake-bundler"
25
- spec.add_development_dependency "rspec"
26
- end