async-http 0.34.1 → 0.34.2

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: 1aa49b687eb945cd14b93ae8f77eb75d0c225fe7b22fba5eb861e46893673388
4
- data.tar.gz: e1f7785a689202683b11a84a01a6cc9a09ebefe52ebc77af935cb1e03f8fb171
3
+ metadata.gz: 29e346514b130a63e1de8fc367ff5f85ae00469e3c9e7b41790a78381efa5a41
4
+ data.tar.gz: 43b4568e84df9e1c2f836dece89bb569cbf777fe5bcf81f82850b1e36c8f4a0a
5
5
  SHA512:
6
- metadata.gz: 5b64daffc3f2a30110f6b9d21d0db85917fb9c590f73cc8174fb9885821b92408d5abb904ae7574eefccd10fd6293ea74148416190f6b5dda6d15f751b9def36
7
- data.tar.gz: c32f1e620e59f385c84f76be707f2373b69df262f3d6b70e172fe007e49a90d7a0f16ac9e8cb34839720b6b5978f35386cf48b7dc3d71dacf0ff350a02fa6ae4
6
+ metadata.gz: 37a89c1db15f3228c5d2cf45ae1c31e83d2532e1157fa5cb9eb61c79ccb6a008d1b21c4e6672ff6dc55fdcbd9138251c9be1df2ef6da73c50ec2ca6904c59643
7
+ data.tar.gz: 6d2a879574d21d1c7025a1e45458cc5350c9e0fb9b6d6ee08935fa6ca517b6af3343aefd946cd6be37687c665592e5eca0ae91cac71ba87f903403ea3f109746
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __dir__)
4
+
5
+ require 'async'
6
+ require 'async/http/body/file'
7
+ require 'async/http/body/delayed'
8
+ require 'async/http/client'
9
+ require 'async/http/url_endpoint'
10
+
11
+ Async.run do
12
+ endpoint = Async::HTTP::URLEndpoint.parse("http://localhost:9222")
13
+ client = Async::HTTP::Client.new(endpoint, Async::HTTP::Protocol::HTTP2)
14
+
15
+ headers = [
16
+ ['accept', 'text/plain'],
17
+ ]
18
+
19
+ body = Async::HTTP::Body::Delayed.new(Async::HTTP::Body::File.open("data.txt", block_size: 32))
20
+
21
+ response = client.post(endpoint.path, headers, body)
22
+
23
+ puts response.status
24
+
25
+ # response.read -> string
26
+ # response.each {|chunk| ...}
27
+ # response.close (forcefully ignore data)
28
+ # body = response.finish (read and buffer response)
29
+ # response.save("echo.txt")
30
+
31
+ response.each do |chunk|
32
+ puts chunk.inspect
33
+ end
34
+
35
+ ensure
36
+ client.close if client
37
+ end
38
+
39
+ puts "Done."
@@ -0,0 +1,41 @@
1
+ The Parable of the Two Programmers
2
+
3
+ Neil W. Rickert
4
+
5
+ Once upon a time, unbeknownst to each other, the "Automated Accounting Applications Association" and the "Consolidated Computerized Capital Corporation" decided that they needed the identical program to perform a certain service.
6
+
7
+ Automated hired a programmer-analyst, Alan, to solve their problem.
8
+
9
+ Meanwhile, Consolidated decided to ask a newly-hired entry-level programmer, Charles, to tackle the job, to see if he was as good as he pretended.
10
+
11
+ Alan, having had experience in difficult programming projects, decided to use the PQR structured design methodology. With this in mind he asked his department manager to assign another three programmers as a programming team. Then the team went to work, churning out preliminary reports and problem analyses.
12
+
13
+ Back at Consolidated, Charles spent some time thinking about the problem. His fellow employees noticed that Charles often sat with his feet on the desk, drinking coffee. He was occasionally seen at his computer terminal, but his office mate could tell from the rhythmic striking of keys that he was actually playing Space Invaders.
14
+
15
+ By now, the team at Automated was starting to write code. The programmers were spending about half their time writing and compiling code, and the rest of their time in conference, discussing the interfaces between the various modules.
16
+
17
+ His office mate noticed that Charles had finally given up on Space Invaders. Instead he now divided his time between drinking coffee with his feet on the table, and scribbling on little scraps of paper. His scribbling didn't seem to be Tic-Tac-Toe, but it didn't exactly make much sense, either.
18
+
19
+ Two months have gone by. The team at Automated finally releases an implementation timetable. In another two months they will have a test version of the program. Then a two month period of testing and enhancing should yield a completed version.
20
+
21
+ The manager of Charles has by now tired of seeing him goof off. He decides to confront him. But as he walks into Charles' office, he is surprised to see Charles busy entering code at his terminal. He decides to postpone the confrontation, so makes some small talk and then leaves. However, he begins to keep a closer watch on Charles, so that when the opportunity presents itself he can confront him. Not looking forward to an unpleasant conversation, he is pleased to notice that Charles seems to be busy most of the time. He has even been seen to delay his lunch, and to stay after work two or three days a week.
22
+
23
+ At the end of three months, Charles announces he has completed the project. He submits a 500-line program. The program appears to be clearly written, and when tested it does everything required in the specifications. In fact, it even has a few additional convenience features which might significantly improve the usability of the program. The program is put into test, and except for one quickly corrected oversight, performs well.
24
+
25
+ The team at Automated has by now completed two of the four major modules required for their program. These modules are now undergoing testing while the other modules are completed.
26
+
27
+ After another three weeks, Alan announces that the preliminary version is ready one week ahead of schedule. He supplies a list of the deficiencies that he expects to correct. The program is placed under test. The users find a number of bugs and deficiencies other than those listed. As Alan explains, this is no surprise. After all, this is a preliminary version in which bugs were expected.
28
+
29
+ After about two more months, the team has completed its production version of the program. It consists of about 2,500 lines of code. When tested, it seems to satisfy most of the original specifications. It has omitted one or two features, and is very fussy about the format of its input data. However, the company decides to install the program. They can always train their data-entry staff to enter data in the strict format required. The program is handed over to some maintenance programmers to eventually incorporate the missing features.
30
+
31
+ Sequel
32
+
33
+ At first Charles' supervisor was impressed. But as he read through the source code, he realized that the project was really much simpler than he had originally thought. It now seemed apparent that this was not much of a challenge even for a beginning programmer.
34
+
35
+ Charles did produce about five lines of code per day. This is perhaps a little above average. However, considering the simplicity of the program, it was nothing exceptional. Also, his supervisor remembered his two months of goofing off.
36
+
37
+ At his next salary review Charles was given a raise which was about half the inflation over the period. He was not given a promotion. After about a year he became discouraged and left Consolidated.
38
+
39
+ At Automated, Alan was complimented for completing his project on schedule. His supervisor looked over the program. Within a few minutes of thumbing through he saw that the company standards about structured programming were being observed. He quickly gave up attempting to read the program; however, it seemed quite incomprehensible. He realized by now that the project was really much more complex than he had originally assumed, and he congratulated Alan again on his achievement.
40
+
41
+ The team had produced over three lines of code per programmer per day. This was about average, but considering the complexity of the problem, could be considered to be exceptional. Alan was given a hefty pay raise, and promoted to Systems Analyst as a reward for his achievement.
@@ -0,0 +1,19 @@
1
+
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __dir__)
3
+
4
+ require 'async'
5
+ require 'async/http/server'
6
+ require 'async/http/url_endpoint'
7
+
8
+ protocol = Async::HTTP::Protocol::HTTP2
9
+ endpoint = Async::HTTP::URLEndpoint.parse('http://127.0.0.1:9222', reuse_port: true)
10
+
11
+ Async.logger.level = Logger::DEBUG
12
+
13
+ Async.run do
14
+ server = Async::HTTP::Server.for(endpoint, protocol) do |request|
15
+ Async::HTTP::Response[200, {}, request.body]
16
+ end
17
+
18
+ server.run
19
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'async'
4
+ require 'async/http/body/file'
5
+ require 'async/http/internet'
6
+
7
+ Async.run do
8
+ internet = Async::HTTP::Internet.new
9
+
10
+ headers = [
11
+ ['accept', 'text/plain'],
12
+ ]
13
+
14
+ body = Async::HTTP::Body::File.open("data.txt")
15
+
16
+ response = internet.post("https://www.codeotaku.com/journal/2018-10/async-http-client-for-ruby/echo", headers, body)
17
+
18
+ # response.read -> string
19
+ # response.each {|chunk| ...}
20
+ # response.close (forcefully ignore data)
21
+ # body = response.finish (read and buffer response)
22
+ response.save("echo.txt")
23
+
24
+ ensure
25
+ internet.close
26
+ end
@@ -0,0 +1,41 @@
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
+ class Delayed < Async::HTTP::Body::Wrapper
27
+ def initialize(body, delay = 0.01)
28
+ super(body)
29
+
30
+ @delay = delay
31
+ end
32
+
33
+ def read
34
+ Async::Task.current.sleep(@delay)
35
+
36
+ return super
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -55,10 +55,6 @@ module Async
55
55
  def read
56
56
  @body.read
57
57
  end
58
-
59
- def inspect
60
- return @body.inspect
61
- end
62
58
  end
63
59
  end
64
60
  end
@@ -40,6 +40,7 @@ module Async
40
40
  # Once we start writing the body, we can't recover if the request fails. That's because the body might be generated dynamically, streaming, etc.
41
41
  self.write_body(request.body)
42
42
 
43
+ # This won't return the response until the entire body is written.
43
44
  return Response.new(self, request)
44
45
  rescue
45
46
  # This will ensure that #reusable? returns false.
@@ -38,13 +38,17 @@ module Async
38
38
  end
39
39
 
40
40
  def wait
41
- @notification.wait
41
+ # If you call wait after the headers were already received, it should return immediately.
42
+ if @notification
43
+ @notification.wait
44
+ end
42
45
 
43
46
  if @exception
44
47
  raise @exception
45
48
  end
46
49
  end
47
50
 
51
+ # This should be invoked from the background reader, and notifies the task waiting for the headers that we are done.
48
52
  def receive_headers(stream, headers, end_stream)
49
53
  headers.each do |key, value|
50
54
  if key == STATUS
@@ -61,7 +65,10 @@ module Async
61
65
  end
62
66
 
63
67
  # We are ready for processing:
64
- @notification.signal
68
+ if @notification
69
+ @notification.signal
70
+ @notification = nil
71
+ end
65
72
  end
66
73
 
67
74
  def receive_data(stream, data, end_stream)
@@ -32,15 +32,20 @@ module Async
32
32
 
33
33
  @body = body
34
34
  @remainder = nil
35
+
36
+ @task = nil
35
37
  end
36
38
 
37
39
  attr_accessor :delegate
38
40
  attr :body
39
41
 
40
- def send_body(body)
41
- @body = body
42
-
43
- window_updated
42
+ def send_body(body, task: Async::Task.current)
43
+ # TODO Might need to stop this task when body is cancelled.
44
+ @task = task.async do
45
+ @body = body
46
+
47
+ window_updated
48
+ end
44
49
  end
45
50
 
46
51
  def send_chunk
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require 'async/io/endpoint'
22
+ require 'async/io/stream'
22
23
 
23
24
  require_relative 'protocol'
24
25
  require_relative 'response'
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module HTTP
23
- VERSION = "0.34.1"
23
+ VERSION = "0.34.2"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.1
4
+ version: 0.34.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-18 00:00:00.000000000 Z
11
+ date: 2018-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -143,12 +143,17 @@ files:
143
143
  - examples/fetch/public/index.html
144
144
  - examples/fetch/public/stream.js
145
145
  - examples/request.rb
146
+ - examples/upload/client.rb
147
+ - examples/upload/data.txt
148
+ - examples/upload/server.rb
149
+ - examples/upload/upload.rb
146
150
  - lib/async/http.rb
147
151
  - lib/async/http/accept_encoding.rb
148
152
  - lib/async/http/body.rb
149
153
  - lib/async/http/body/buffered.rb
150
154
  - lib/async/http/body/chunked.rb
151
155
  - lib/async/http/body/deflate.rb
156
+ - lib/async/http/body/delayed.rb
152
157
  - lib/async/http/body/file.rb
153
158
  - lib/async/http/body/fixed.rb
154
159
  - lib/async/http/body/inflate.rb