http-protocol 0.5.0 → 0.6.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: 242ba5105135ba4ed2bff4ccef1e1a77fb416788fbf527eb6221de8eb0026225
4
- data.tar.gz: 9bbcd87adf08d36f38b44d581a82243494a68adf66d6e63fb6e559de60bb6ac6
3
+ metadata.gz: 9327c9adf284058c58e7e426b736ff72e3752fecd81197fe6b924a1c6df5c283
4
+ data.tar.gz: a34c59791a0aaa73fa92ac2414142ce3b0fc8fcb0eeaffc681b470dd81a239ee
5
5
  SHA512:
6
- metadata.gz: 9a57f0e967f1d74a52c04112eb8d617b974f836f5c898ced1846e29ead3e4fb3a82f503bf7926f8948920637de895ccd9aef7c54066410b2ba9e801cc962352d
7
- data.tar.gz: 8bd4615ca2b519412ec0c164639af7dd99e005aaf3cae3f0b7aa8bfb64ba99d57fa12ff3adda731878017b4d2424fae4af0dab51873d9422b101c0aa4c36a1f5
6
+ metadata.gz: eaac214ab838e2e1a96a86ccd4756af6a7213b79157f738c43cbdaf2754e36b2dd08e04da675d49cd3f1d199db5bcfa9e468381e48ac8155508b920ef48e3e88
7
+ data.tar.gz: 8f1dc78539df7034795ba5d407213d3905f354878bf3758e4261b0ac7cbc61b88c6338c924ac4abd23462266e2d8e94e56b5fe26b6cb93d0dbbcd1fa97db6b03
@@ -22,32 +22,54 @@ require_relative '../error'
22
22
 
23
23
  module HTTP
24
24
  module Protocol
25
- module HTTP11
25
+ module HTTP1
26
26
  class Connection
27
27
  CRLF = "\r\n".freeze
28
- VERSION = "HTTP/1.1".freeze
28
+ HTTP10 = "HTTP/1.0".freeze
29
+ HTTP11 = "HTTP/1.1".freeze
29
30
 
30
- def initialize(stream, persistent = true)
31
+ def initialize(stream, version = HTTP11, persistent = true)
31
32
  @stream = stream
32
33
 
34
+ @version = version
33
35
  @persistent = persistent
34
36
 
35
37
  @count = 0
36
38
  end
37
39
 
38
40
  attr :stream
41
+
42
+ # The default HTTP version to use (if none specified).
43
+ attr :version
44
+
45
+ # Whether the connection is persistent.
39
46
  attr :persistent
47
+
48
+ # The number of requests processed.
40
49
  attr :count
41
50
 
42
- def version
43
- VERSION
51
+ def persistent?(version, headers)
52
+ if version == HTTP10
53
+ if connection = headers[CONNECTION]
54
+ return connection.include?(KEEP_ALIVE)
55
+ else
56
+ return false
57
+ end
58
+ else
59
+ if connection = headers[CONNECTION]
60
+ return !connection.include?(CLOSE)
61
+ else
62
+ return true
63
+ end
64
+ end
44
65
  end
45
66
 
46
- def persistent?(headers)
47
- if connection = headers[CONNECTION]
48
- return !connection.include?(CLOSE)
67
+ # Write the appropriate header for connection persistence.
68
+ def write_persistent_header(version)
69
+ if version == HTTP10
70
+ @stream.write("connection: keep-alive\r\n") if @persistent
49
71
  else
50
- return true
72
+ @stream.write("connection: close\r\n") unless @persistent
51
73
  end
52
74
  end
53
75
 
@@ -61,11 +83,6 @@ module HTTP
61
83
  return @stream
62
84
  end
63
85
 
64
- # Write the appropriate header for connection persistence.
65
- def write_persistent_header
66
- @stream.write("connection: keep-alive\r\n") if @persistent
67
- end
68
-
69
86
  # Close the connection and underlying stream.
70
87
  def close
71
88
  @stream.close
@@ -76,18 +93,21 @@ module HTTP
76
93
  @stream.write("host: #{authority}\r\n")
77
94
 
78
95
  write_headers(headers)
96
+ write_persistent_header(version)
79
97
 
80
98
  @stream.flush
81
99
  end
82
100
 
83
101
  def write_response(version, status, headers, body = nil, head = false)
84
102
  @stream.write("#{version} #{status}\r\n")
103
+
85
104
  write_headers(headers)
105
+ write_persistent_header(version)
86
106
 
87
107
  if head
88
108
  write_body_head(body)
89
109
  else
90
- write_body(body)
110
+ write_body(body, version == HTTP11)
91
111
  end
92
112
 
93
113
  @stream.flush
@@ -119,7 +139,7 @@ module HTTP
119
139
  method, path, version = read_line.split(/\s+/, 3)
120
140
  headers = read_headers
121
141
 
122
- @persistent = persistent?(headers)
142
+ @persistent = persistent?(version, headers)
123
143
 
124
144
  body = read_request_body(headers)
125
145
 
@@ -134,7 +154,7 @@ module HTTP
134
154
 
135
155
  headers = read_headers
136
156
 
137
- @persistent = persistent?(headers)
157
+ @persistent = persistent?(version, headers)
138
158
 
139
159
  body = read_response_body(method, status, headers)
140
160
 
@@ -187,8 +207,6 @@ module HTTP
187
207
  end
188
208
 
189
209
  def write_empty_body(body)
190
- # Write empty body:
191
- write_persistent_header
192
210
  @stream.write("content-length: 0\r\n\r\n")
193
211
 
194
212
  body.read if body
@@ -197,7 +215,6 @@ module HTTP
197
215
  end
198
216
 
199
217
  def write_fixed_length_body(body, length)
200
- write_persistent_header
201
218
  @stream.write("content-length: #{length}\r\n\r\n")
202
219
 
203
220
  chunk_length = 0
@@ -219,7 +236,6 @@ module HTTP
219
236
  end
220
237
 
221
238
  def write_chunked_body(body)
222
- write_persistent_header
223
239
  @stream.write("transfer-encoding: chunked\r\n\r\n")
224
240
 
225
241
  body.each do |chunk|
@@ -238,7 +254,6 @@ module HTTP
238
254
  def write_body_and_close(body)
239
255
  # We can't be persistent because we don't know the data length:
240
256
  @persistent = false
241
- write_persistent_header
242
257
 
243
258
  @stream.write("\r\n")
244
259
 
@@ -263,8 +278,6 @@ module HTTP
263
278
  end
264
279
 
265
280
  def write_body_head(body)
266
- write_persistent_header
267
-
268
281
  if body.nil? or body.empty?
269
282
  @stream.write("content-length: 0\r\n\r\n")
270
283
  elsif length = body.length
@@ -20,6 +20,6 @@
20
20
 
21
21
  module HTTP
22
22
  module Protocol
23
- VERSION = "0.5.0"
23
+ VERSION = "0.6.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
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-09-30 00:00:00.000000000 Z
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-hpack
@@ -83,8 +83,7 @@ files:
83
83
  - lib/http/protocol.rb
84
84
  - lib/http/protocol/error.rb
85
85
  - lib/http/protocol/headers.rb
86
- - lib/http/protocol/http10/connection.rb
87
- - lib/http/protocol/http11/connection.rb
86
+ - lib/http/protocol/http1/connection.rb
88
87
  - lib/http/protocol/http2/client.rb
89
88
  - lib/http/protocol/http2/connection.rb
90
89
  - lib/http/protocol/http2/continuation_frame.rb
@@ -1,52 +0,0 @@
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 '../http11/connection'
22
-
23
- module HTTP
24
- module Protocol
25
- module HTTP10
26
- class Connection < HTTP11::Connection
27
- VERSION = "HTTP/1.0".freeze
28
-
29
- def version
30
- VERSION
31
- end
32
-
33
- def persistent?(headers)
34
- if connection = headers[CONNECTION]
35
- return connection.include?(KEEP_ALIVE)
36
- else
37
- return false
38
- end
39
- end
40
-
41
- def write_persistent_header
42
- @stream.write("connection: keep-alive\r\n") if @persistent
43
- end
44
-
45
- def write_body(body, chunked = false)
46
- # We don't support chunked encoding by default.
47
- super(body, chunked)
48
- end
49
- end
50
- end
51
- end
52
- end