async-http 0.18.0 → 0.19.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.
@@ -0,0 +1,106 @@
1
+ # Copyright, 2018, 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
+ require_relative 'wrapper'
22
+
23
+ require 'zlib'
24
+
25
+ module Async
26
+ module HTTP
27
+ module Body
28
+ class ZStream < Wrapper
29
+ DEFAULT_LEVEL = 7
30
+
31
+ DEFLATE = -Zlib::MAX_WBITS
32
+ GZIP = Zlib::MAX_WBITS | 16
33
+
34
+ ENCODINGS = {
35
+ 'deflate' => DEFLATE,
36
+ 'gzip' => GZIP,
37
+ }
38
+
39
+ def self.encoding_name(window_size)
40
+ if window_size <= -8
41
+ return 'deflate'
42
+ elsif window_size >= 16
43
+ return 'gzip'
44
+ else
45
+ return 'compress'
46
+ end
47
+ end
48
+
49
+ def initialize(body, stream)
50
+ super(body)
51
+
52
+ @stream = stream
53
+
54
+ @input_size = 0
55
+ @output_size = 0
56
+ end
57
+
58
+ attr :input_size
59
+ attr :output_size
60
+
61
+ def ratio
62
+ @output_size.to_f / @input_size.to_f
63
+ end
64
+
65
+ def stop(error)
66
+ # There are two ways for the stream to be closed. Either #read returns nil or #stop is called.
67
+ @stream.close unless @stream.closed?
68
+
69
+ super
70
+ end
71
+
72
+ def inspect
73
+ "#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
74
+ end
75
+ end
76
+
77
+ class Deflate < ZStream
78
+ def self.for(body, window_size = GZIP, level = DEFAULT_LEVEL)
79
+ self.new(body, Zlib::Deflate.new(level, window_size))
80
+ end
81
+
82
+ def read
83
+ return if @stream.closed?
84
+
85
+ if chunk = super
86
+ @input_size += chunk.bytesize
87
+
88
+ chunk = @stream.deflate(chunk, Zlib::SYNC_FLUSH)
89
+
90
+ @output_size += chunk.bytesize
91
+
92
+ return chunk
93
+ else
94
+ chunk = @stream.finish
95
+
96
+ @output_size += chunk.bytesize
97
+
98
+ @stream.close
99
+
100
+ return chunk.empty? ? nil : chunk
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,83 @@
1
+ # Copyright, 2018, 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
+ require_relative 'readable'
22
+
23
+ module Async
24
+ module HTTP
25
+ module Body
26
+ class Fixed < Readable
27
+ def initialize(stream, length)
28
+ @stream = stream
29
+ @length = length
30
+ @remaining = length
31
+ end
32
+
33
+ def empty?
34
+ @remaining == 0
35
+ end
36
+
37
+ def read
38
+ if @remaining > 0
39
+ if chunk = @stream.read(@remaining)
40
+ @remaining -= chunk.bytesize
41
+
42
+ return chunk
43
+ end
44
+ end
45
+ end
46
+
47
+ def join
48
+ buffer = @stream.read(@remaining)
49
+
50
+ @remaining = 0
51
+
52
+ return buffer
53
+ end
54
+
55
+ def inspect
56
+ "\#<#{self.class} length=#{@length} remaining=#{@remaining}>"
57
+ end
58
+ end
59
+
60
+ class Remainder < Readable
61
+ def initialize(stream)
62
+ @stream = stream
63
+ end
64
+
65
+ def empty?
66
+ @stream.closed?
67
+ end
68
+
69
+ def read
70
+ @stream.read unless @stream.closed?
71
+ end
72
+
73
+ def join
74
+ read
75
+ end
76
+
77
+ def inspect
78
+ "\#<#{self.class} #{@stream.closed? ? 'closed' : 'open'}>"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,55 @@
1
+ # Copyright, 2018, 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
+ require 'zlib'
22
+
23
+ require_relative 'deflate'
24
+
25
+ module Async
26
+ module HTTP
27
+ module Body
28
+ class Inflate < ZStream
29
+ def self.for(body, encoding = GZIP)
30
+ self.new(body, Zlib::Inflate.new(encoding))
31
+ end
32
+
33
+ def read
34
+ return if @stream.closed?
35
+
36
+ if chunk = super
37
+ @input_size += chunk.bytesize
38
+
39
+ chunk = @stream.inflate(chunk)
40
+
41
+ @output_size += chunk.bytesize
42
+ else
43
+ chunk = @stream.finish
44
+
45
+ @output_size += chunk.bytesize
46
+
47
+ @stream.close
48
+ end
49
+
50
+ return chunk.empty? ? nil : chunk
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,73 @@
1
+ # Copyright, 2018, 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
+ require 'async/io/binary_string'
22
+
23
+ module Async
24
+ module HTTP
25
+ module Body
26
+ # A generic base class for wrapping body instances. Typically you'd override `#read`.
27
+ class Readable
28
+ # Buffer any remaining body.
29
+ def close
30
+ Buffered.for(self)
31
+ end
32
+
33
+ # Will read return any data?
34
+ def empty?
35
+ false
36
+ end
37
+
38
+ # Read the next available chunk.
39
+ def read
40
+ nil
41
+ end
42
+
43
+ # The consumer can call stop to signal that the stream output has terminated.
44
+ def stop(error)
45
+ end
46
+
47
+ # Enumerate all chunks until finished. If an error is thrown, #stop will be invoked.
48
+ def each
49
+ return to_enum unless block_given?
50
+
51
+ while chunk = self.read
52
+ yield chunk
53
+ end
54
+ rescue
55
+ stop($!)
56
+
57
+ raise
58
+ end
59
+
60
+ # Read all remaining chunks into a single binary string.
61
+ def join
62
+ buffer = IO::BinaryString.new
63
+
64
+ self.each do |chunk|
65
+ buffer << chunk
66
+ end
67
+
68
+ return buffer
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright, 2018, 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
+ require_relative 'wrapper'
22
+
23
+ module Async
24
+ module HTTP
25
+ module Body
26
+ # Invokes a callback once the body has finished reading.
27
+ class Streamable < Wrapper
28
+ def self.wrap(message, &block)
29
+ if message and message.body
30
+ message.body = self.new(message.body, block)
31
+ else
32
+ yield
33
+ end
34
+ end
35
+
36
+ def initialize(body, callback)
37
+ super(body)
38
+
39
+ @callback = callback
40
+ end
41
+
42
+ def read
43
+ unless chunk = super
44
+ @callback.call
45
+ end
46
+
47
+ return chunk
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,58 @@
1
+ # Copyright, 2018, 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
+ require_relative 'readable'
22
+
23
+ module Async
24
+ module HTTP
25
+ module Body
26
+ # Wrapping body instance. Typically you'd override `#read`.
27
+ class Wrapper < Readable
28
+ def initialize(body)
29
+ @body = body
30
+ end
31
+
32
+ def empty?
33
+ @body.empty?
34
+ end
35
+
36
+ # Buffer any remaining body.
37
+ def close
38
+ @body = @body.close
39
+
40
+ return self
41
+ end
42
+
43
+ # Read the next available chunk.
44
+ def read
45
+ @body.read
46
+ end
47
+
48
+ def stop(error)
49
+ @body.stop(error)
50
+ end
51
+
52
+ def inspect
53
+ return @body.inspect
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,81 @@
1
+ # Copyright, 2018, 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
+ require_relative 'readable'
22
+
23
+ require 'async/queue'
24
+
25
+ module Async
26
+ module HTTP
27
+ module Body
28
+ # A dynamic body which you can write to and read from.
29
+ class Writable < Readable
30
+ def initialize
31
+ @queue = Async::Queue.new
32
+
33
+ @count = 0
34
+
35
+ @finished = false
36
+ @stopped = nil
37
+ end
38
+
39
+ def empty?
40
+ @finished
41
+ end
42
+
43
+ # Read the next available chunk.
44
+ def read
45
+ return if @finished
46
+
47
+ unless chunk = @queue.dequeue
48
+ @finished = true
49
+ end
50
+
51
+ return chunk
52
+ end
53
+
54
+ def stop(error)
55
+ @stopped = error
56
+ end
57
+
58
+ # Write a single chunk to the body. Signal completion by calling `#finish`.
59
+ def write(chunk)
60
+ if @stopped
61
+ raise @stopped
62
+ end
63
+
64
+ # TODO should this yield if the queue is full?
65
+
66
+ @count += 1
67
+ @queue.enqueue(chunk)
68
+ end
69
+
70
+ # Signal that output has finished.
71
+ def finish
72
+ @queue.enqueue(nil)
73
+ end
74
+
75
+ def inspect
76
+ "\#<#{self.class} #{@count} chunks written>"
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end