protocol-rack 0.1.3 → 0.1.6

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: 49a30005b471894675e712aa002435c47a19744e184bd61dcbfbd552cb87e3a3
4
- data.tar.gz: 0fe2c629bc8cc0484e460ad9cd707bda6eaf94dbcf3498b2e6f8304c536e3dd2
3
+ metadata.gz: 2826bd60593f65a9f2f5350e714c2436b266b9d393d8afb594e59d18ab9168f9
4
+ data.tar.gz: 4c5051edaf485a0413d5090641c6347ead593a3b5210fcb9b2538808218d7a63
5
5
  SHA512:
6
- metadata.gz: ed9396014d93e549539bdad9e1d24e65f947fc900222edabc6ff326f10a9993ebdd4a44cc19dda4e2d32944e6570e1b74147188aecfd4705cbade8f2f625e550
7
- data.tar.gz: bcbd5528e66527c9c2afabbcb23583e7cd87437c3fe49f03d89fd8556d7f550df7d5e9d187fd9343967697ed12b607973b3eab715d20da9d4c437db3b69fc379
6
+ metadata.gz: df9fa485dd38739b5ff6a400cf047f7dd24237211b9e092252f927ea6bda819f383b08b8dc44be434cf4d44a1828661542aaa60058cac56d8d49c376355bb0ef
7
+ data.tar.gz: f2a4c166cf5466f230a4cbdf0b1e221d328d06462dee18a7c2d6951ced7530e58528519f5627ee3905449bb4e53cd7dfd563f5f6606a7e93511df1daac5867ba
checksums.yaml.gz.sig CHANGED
Binary file
@@ -103,7 +103,9 @@ module Protocol
103
103
 
104
104
  status, headers, body = @app.call(env)
105
105
 
106
- return Response.wrap(status, headers, body, request)
106
+ headers, meta = self.wrap_headers(headers)
107
+
108
+ return Response.wrap(status, headers, meta, body, request)
107
109
  rescue => exception
108
110
  Console.logger.error(self) {exception}
109
111
 
@@ -90,11 +90,34 @@ module Protocol
90
90
  return env
91
91
  end
92
92
 
93
+ # Process the rack response headers into into a {Protocol::HTTP::Headers} instance, along with any extra `rack.` metadata.
94
+ # @returns [Tuple(Protocol::HTTP::Headers, Hash)]
95
+ def wrap_headers(fields)
96
+ headers = ::Protocol::HTTP::Headers.new
97
+ meta = {}
98
+
99
+ fields.each do |key, value|
100
+ key = key.downcase
101
+
102
+ if key.start_with?('rack.')
103
+ meta[key] = value
104
+ else
105
+ value.split("\n").each do |value|
106
+ headers[key] = value
107
+ end
108
+ end
109
+ end
110
+
111
+ return headers, meta
112
+ end
113
+
93
114
  def self.make_response(env, response)
94
115
  # These interfaces should be largely compatible:
95
116
  headers = response.headers.to_h
117
+
96
118
  if protocol = response.protocol
97
119
  headers['rack.protocol'] = protocol
120
+ # headers['upgrade'] = protocol
98
121
  end
99
122
 
100
123
  if body = response.body and body.stream?
@@ -103,7 +126,11 @@ module Protocol
103
126
  body = []
104
127
  end
105
128
  end
106
-
129
+
130
+ headers.transform_values! do |value|
131
+ value.is_a?(Array) ? value.join("\n") : value
132
+ end
133
+
107
134
  [response.status, headers, body]
108
135
  end
109
136
  end
@@ -76,6 +76,29 @@ module Protocol
76
76
  return env
77
77
  end
78
78
 
79
+ # Process the rack response headers into into a {Protocol::HTTP::Headers} instance, along with any extra `rack.` metadata.
80
+ # @returns [Tuple(Protocol::HTTP::Headers, Hash)]
81
+ def wrap_headers(fields)
82
+ headers = ::Protocol::HTTP::Headers.new
83
+ meta = {}
84
+
85
+ fields.each do |key, value|
86
+ key = key.downcase
87
+
88
+ if key.start_with?('rack.')
89
+ meta[key] = value
90
+ elsif value.is_a?(Array)
91
+ value.each do |value|
92
+ headers[key] = value
93
+ end
94
+ else
95
+ headers[key] = value
96
+ end
97
+ end
98
+
99
+ return headers, meta
100
+ end
101
+
79
102
  def self.make_response(env, response)
80
103
  # These interfaces should be largely compatible:
81
104
  headers = response.headers.to_h
@@ -44,34 +44,23 @@ module Protocol
44
44
  # @attribute [Protocol::HTTP::Body::Readable]
45
45
  attr :body
46
46
 
47
+ include Protocol::HTTP::Body::Stream::Reader
48
+
49
+ alias gets read_partial
50
+
47
51
  # Enumerate chunks of the request body.
48
52
  # @yields {|chunk| ...}
49
53
  # @parameter chunk [String]
50
54
  def each(&block)
51
55
  return to_enum unless block_given?
52
56
 
53
- while chunk = gets
57
+ return if closed?
58
+
59
+ while chunk = read_partial
54
60
  yield chunk
55
61
  end
56
62
  end
57
63
 
58
- include Protocol::HTTP::Body::Stream::Reader
59
-
60
- # Read the next chunk of data from the input stream.
61
- #
62
- # `gets` must be called without arguments and return a `String`, or `nil` when the input stream has no more data.
63
- #
64
- # @returns [String | Nil] The next chunk from the body.
65
- def gets
66
- if @buffer.nil?
67
- return read_next
68
- else
69
- buffer = @buffer
70
- @buffer = nil
71
- return buffer
72
- end
73
- end
74
-
75
64
  # Close the input and output bodies.
76
65
  def close(error = nil)
77
66
  if @body
@@ -116,7 +105,6 @@ module Protocol
116
105
  if @body
117
106
  @body.read
118
107
  else
119
- @body = nil
120
108
  raise IOError, "Stream is not readable, input has been closed!"
121
109
  end
122
110
  end
@@ -21,6 +21,7 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'protocol/http/request'
24
+ require 'protocol/http/headers'
24
25
 
25
26
  require_relative 'body/input_wrapper'
26
27
 
@@ -51,38 +51,14 @@ module Protocol
51
51
  'upgrade',
52
52
  ]
53
53
 
54
- # Process the rack response headers into into a {Protocol::HTTP::Headers} instance, along with any extra `rack.` metadata.
55
- # @returns [Tuple(Protocol::HTTP::Headers, Hash)]
56
- def self.wrap_headers(fields)
57
- headers = ::Protocol::HTTP::Headers.new
58
- meta = {}
59
-
60
- fields.each do |key, value|
61
- key = key.downcase
62
-
63
- if key.start_with?('rack.')
64
- meta[key] = value
65
- elsif value.is_a?(Array)
66
- value.each do |value|
67
- headers[key] = value
68
- end
69
- else
70
- headers[key] = value
71
- end
72
- end
73
-
74
- return headers, meta
75
- end
76
-
77
54
  # Wrap a rack response.
78
55
  # @parameter status [Integer] The rack response status.
79
56
  # @parameter headers [Duck(:each)] The rack response headers.
80
57
  # @parameter body [Duck(:each, :close) | Nil] The rack response body.
81
58
  # @parameter request [Protocol::HTTP::Request] The original request.
82
- def self.wrap(status, headers, body, request = nil)
83
- headers, meta = wrap_headers(headers)
84
-
59
+ def self.wrap(status, headers, meta, body, request = nil)
85
60
  ignored = headers.extract(HOP_HEADERS)
61
+
86
62
  unless ignored.empty?
87
63
  Console.logger.warn(self, "Ignoring protocol-level headers: #{ignored.inspect}")
88
64
  end
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Protocol
24
24
  module Rack
25
- VERSION = "0.1.3"
25
+ VERSION = "0.1.6"
26
26
  end
27
27
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file