async-http 0.21.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: de4dff870c7d93652b036dfc3a5bfd80c9b88d899877f566b3a10a3e52a2a9f2
4
- data.tar.gz: da2eea4408999ebcef45259231095d531e89f4ae59f287ff9b3fe8b888d2fa77
3
+ metadata.gz: 9747cc8b9879031adf8d9e6086942e69955ff33ca04a598bac893f6a04588680
4
+ data.tar.gz: d4dca36355247bfdd1c5162493813ffc2eaf815b1a9d8c6297fed08069a3c15e
5
5
  SHA512:
6
- metadata.gz: 41b9c69088c19277328694962dfa01431af5f33cc058ce348aa15327cda5e83d8e2ad57c38a9f67632b441f95ff463774c8f6e5048f9b23d7ddf8d0debe2106d
7
- data.tar.gz: cf6842c7be2895859a8bcc17ff0710d45f519658af83bead6facb94b8f25f3ada10eba70c7e2febd0f4418a52c3ad13131e598e689eb519acbb9f27114ca67c5
6
+ metadata.gz: d3060ef8cf5a1dc5524c8170437cfdc1a027446737ab199daed129d9d5cb8230e3df0b228c4a8259c2b2f70d0221419c1261707097d9f0a32104a13980716aef
7
+ data.tar.gz: 9ef10b59d8e2b27295dccc8eef3db0b60052bcf01497d124e1eae16e4d2a9d129e29acd29de71f17ffedf0ae431b2f31b1fa1ba8ec404c88663fc2aecb92982c
@@ -0,0 +1,105 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Async
22
+ module HTTP
23
+ class Headers
24
+ def initialize(fields = [])
25
+ @fields = fields
26
+ @indexed = to_h
27
+ end
28
+
29
+ attr :fields
30
+
31
+ def freeze
32
+ return if frozen?
33
+
34
+ @indexed = to_h
35
+
36
+ super
37
+ end
38
+
39
+ def each(&block)
40
+ @fields.each(&block)
41
+ end
42
+
43
+ def include? key
44
+ self[key] != nil
45
+ end
46
+
47
+ # Delete all headers with the given key, and return the value of the last one, if any.
48
+ def delete(key)
49
+ values, @fields = @fields.partition do |field|
50
+ field.first.downcase == key
51
+ end
52
+
53
+ if @indexed
54
+ @indexed.delete(key)
55
+ end
56
+
57
+ if field = values.last
58
+ return field.last
59
+ end
60
+ end
61
+
62
+ def []= key, value
63
+ @fields << [key, value]
64
+
65
+ if @indexed
66
+ key = key.downcase
67
+
68
+ if current_value = @indexed[key]
69
+ @indexed[key] = Array(current_value) << value
70
+ else
71
+ @indexed[key] = value
72
+ end
73
+ end
74
+ end
75
+
76
+ def [] key
77
+ @indexed ||= to_h
78
+
79
+ @indexed[key]
80
+ end
81
+
82
+ def to_h
83
+ @fields.inject({}) do |hash, (key, value)|
84
+ key = key.downcase
85
+
86
+ if current_value = hash[key]
87
+ hash[key] = Array(current_value) << value
88
+ else
89
+ hash[key] = value
90
+ end
91
+
92
+ hash
93
+ end
94
+ end
95
+
96
+ def == other
97
+ if other.is_a? Hash
98
+ to_h == other
99
+ else
100
+ @fields == other.fields
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -22,6 +22,7 @@ require 'async/io/protocol/line'
22
22
 
23
23
  require_relative '../request'
24
24
  require_relative '../response'
25
+ require_relative '../headers'
25
26
 
26
27
  require_relative '../body/chunked'
27
28
  require_relative '../body/fixed'
@@ -155,17 +156,18 @@ module Async
155
156
  end
156
157
  end
157
158
 
158
- def read_headers(headers = {})
159
- # Parsing headers:
159
+ def read_headers
160
+ fields = []
161
+
160
162
  each_line do |line|
161
163
  if line =~ /^([a-zA-Z\-]+):\s*(.+?)\s*$/
162
- headers[$1.downcase] = $2
164
+ fields << [$1, $2]
163
165
  else
164
166
  break
165
167
  end
166
168
  end
167
169
 
168
- return headers
170
+ return Headers.new(fields)
169
171
  end
170
172
 
171
173
  def write_body(body, chunked = true)
@@ -20,6 +20,7 @@
20
20
 
21
21
  require_relative '../request'
22
22
  require_relative '../response'
23
+ require_relative '../headers'
23
24
  require_relative '../body/writable'
24
25
 
25
26
  require 'async/notification'
@@ -112,12 +113,11 @@ module Async
112
113
 
113
114
  request = Request.new
114
115
  request.version = self.version
115
- request.headers = {}
116
+ request.headers = Headers.new
116
117
  body = Body::Writable.new
117
118
  request.body = body
118
119
 
119
120
  stream.on(:headers) do |headers|
120
- # puts "Got request headers: #{headers.inspect}"
121
121
  headers.each do |key, value|
122
122
  if key == METHOD
123
123
  request.method = value
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module HTTP
23
- VERSION = "0.21.0"
23
+ VERSION = "0.22.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -136,6 +136,7 @@ files:
136
136
  - lib/async/http/body/writable.rb
137
137
  - lib/async/http/client.rb
138
138
  - lib/async/http/content_encoding.rb
139
+ - lib/async/http/headers.rb
139
140
  - lib/async/http/middleware.rb
140
141
  - lib/async/http/middleware/builder.rb
141
142
  - lib/async/http/pool.rb