protocol-http 0.19.0 → 0.22.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: 0af75765a43c5fb19b3071937a6162477ebdb206811be34f5d74c7881688117e
4
- data.tar.gz: 7eef98e2de4c86ddb2f4cd5a77d61f8a2ca3998e30d8bef61d95d7c4bb902c34
3
+ metadata.gz: 52e91627263ea1f1b1c1744372ca276042bc0b901b3eef0fbb45cb79e49daae8
4
+ data.tar.gz: 4bbf0d84e477382c6a42551f25b73bd2adac17ea78f649490beba2b0f8462866
5
5
  SHA512:
6
- metadata.gz: 7f42ab4cd4bf8bf144ced1758316ab181a78687b89882e6d63d10ac190112dfc6aaa02800af6048d7ee12f510d8c805736a412bdb32926c81baaf85f46994dd3
7
- data.tar.gz: 020bc0c0ff0d2b687117cbbdd09fe38e0c7e5510f3b94f96f3de6e3eed3ecf32fad35cc408cd3fd3af63fdb0500aae841fba67c6835272b9a06b2b7d0c36b7f9
6
+ metadata.gz: c07409b15f91f5b3d3fcdaccaf2410fefd0fd513c1657dde0209c1aa4001a7c4ec15fc7843c4a403a089c37f0d2952c53300961e4bb1b8fd70b3a04876c31e6a
7
+ data.tar.gz: 3fb7f131549020cdfe0b79a0348fd14a8ee96f7acdbdd4f3faf9ce0781f9d48d21a006fbb2338764884795b822c7e6b0b10426a77a31d79f7110d721c1044f88
@@ -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
@@ -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)
@@ -60,6 +60,21 @@ module Protocol
60
60
  nil
61
61
  end
62
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
+
63
78
  # Read all remaining chunks into a buffered body and close the underlying input.
64
79
  def finish
65
80
  # Internally, this invokes `self.each` which then invokes `self.close`.
@@ -71,6 +71,14 @@ module Protocol
71
71
  def inspect
72
72
  @body.inspect
73
73
  end
74
+
75
+ def stream?
76
+ @body.stream?
77
+ end
78
+
79
+ def call(stream)
80
+ @body.call(stream)
81
+ end
74
82
  end
75
83
  end
76
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
@@ -35,15 +35,13 @@ module Protocol
35
35
  end
36
36
 
37
37
  class Builder
38
- def initialize(default_app = NotFound, &block)
38
+ def initialize(default_app = NotFound)
39
39
  @use = []
40
40
  @app = default_app
41
-
42
- instance_eval(&block) if block_given?
43
41
  end
44
42
 
45
- def use(middleware, *args, &block)
46
- @use << proc {|app| middleware.new(app, *args, &block)}
43
+ def use(middleware, *arguments, **options, &block)
44
+ @use << proc {|app| middleware.new(app, *arguments, **options, &block)}
47
45
  end
48
46
 
49
47
  def run(app)
@@ -56,7 +54,11 @@ module Protocol
56
54
  end
57
55
 
58
56
  def self.build(&block)
59
- Builder.new(&block).to_app
57
+ builder = Builder.new
58
+
59
+ builder.instance_eval(&block)
60
+
61
+ return builder.to_app
60
62
  end
61
63
  end
62
64
  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.19.0"
25
+ VERSION = "0.22.1"
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.19.0
4
+ version: 0.22.1
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-05-05 00:00:00.000000000 Z
11
+ date: 2021-06-05 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
@@ -134,8 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
112
  - !ruby/object:Gem::Version
135
113
  version: '0'
136
114
  requirements: []
137
- rubygems_version: 3.1.2
138
- signing_key:
115
+ rubygems_version: 3.2.15
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