tep 0.11.8 → 0.11.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 921acd4cd8862832f077f0b68d852564f01c8564c2da0f3e9501ece51b665541
4
- data.tar.gz: 3a8c979189165b441605e276d0c40227a3709d4a401acf7a46bdeeb918184509
3
+ metadata.gz: 82c269cf0ea0f3d9fa48c4eb2eb9c74ab91e6b503698713171ad45ce18a7c737
4
+ data.tar.gz: 8624fd74d1c1e183a511c73dc3b65d45d32d3d721787a727b019ac6cd661eb6a
5
5
  SHA512:
6
- metadata.gz: 4ebec6a9b5803077abd49597f26f9603f7427cb80eb517e1e42fc083d7104b24809e55f09d72856745ea3776ea1be2c66f29347a3b2f16639567284f569f0ec6
7
- data.tar.gz: 422f12aea359f8d56d80e660925a74aa5353eed91f6e35cf23c6a0d6e33172bf79d1b9fc6a524227fe111c60fa51ebd228f8133f372fa49aa72e57b2d31f2bea
6
+ metadata.gz: 05456c12d7608845efeaafb6d1cd2f64b48fb54f70aa11033fed32027d0a4fab70d6a3ee223bc64091ebce3d8e561e23995a188aae060bc9a7f68d1f087be264
7
+ data.tar.gz: df4bc58f06c7959603c88b4cd0bde44a4ee3b3773f910acbdc4454997b33628f5d21b7a501e6df8a7c02b6dcc34952694f1842bae51aee131584f03b9b08318e
data/lib/tep/http.rb CHANGED
@@ -183,7 +183,7 @@ module Tep
183
183
  head = head + k + ": " + v + "\r\n"
184
184
  end
185
185
  if body.length > 0
186
- head = head + "Content-Length: " + body.length.to_s + "\r\n"
186
+ head = head + "Content-Length: " + body.bytesize.to_s + "\r\n"
187
187
  end
188
188
  head = head + "\r\n"
189
189
  if Sock.sphttp_write_str(fd, head) < 0
@@ -229,7 +229,7 @@ module Tep
229
229
  head = head + k + ": " + v + "\r\n"
230
230
  end
231
231
  if body.length > 0
232
- head = head + "Content-Length: " + body.length.to_s + "\r\n"
232
+ head = head + "Content-Length: " + body.bytesize.to_s + "\r\n"
233
233
  end
234
234
  head = head + "\r\n"
235
235
 
@@ -335,7 +335,7 @@ module Tep
335
335
  head = head + k + ": " + v + "\r\n"
336
336
  end
337
337
  if body.length > 0
338
- head = head + "Content-Length: " + body.length.to_s + "\r\n"
338
+ head = head + "Content-Length: " + body.bytesize.to_s + "\r\n"
339
339
  end
340
340
  head = head + "\r\n"
341
341
 
data/lib/tep/llm.rb CHANGED
@@ -111,7 +111,7 @@ module Tep
111
111
  if @api_key.length > 0
112
112
  head = head + "Authorization: Bearer " + @api_key + "\r\n"
113
113
  end
114
- head = head + "Content-Length: " + body.length.to_s + "\r\n" +
114
+ head = head + "Content-Length: " + body.bytesize.to_s + "\r\n" +
115
115
  "Connection: close\r\n\r\n" + body
116
116
  if Sock.sphttp_write_str(fd, head) < 0
117
117
  Sock.sphttp_close(fd)
data/lib/tep/proxy.rb CHANGED
@@ -450,7 +450,7 @@ module Tep
450
450
  head = head + k + ": " + v + "\r\n"
451
451
  end
452
452
  if ureq.body.length > 0
453
- head = head + "Content-Length: " + ureq.body.length.to_s + "\r\n"
453
+ head = head + "Content-Length: " + ureq.body.bytesize.to_s + "\r\n"
454
454
  end
455
455
  head = head + "\r\n"
456
456
  if Sock.sphttp_write_str(fd, head) < 0
data/lib/tep/request.rb CHANGED
@@ -137,7 +137,7 @@ module Tep
137
137
  # want to consume it directly.
138
138
  def consume_body(client_fd)
139
139
  cl = content_length
140
- already = @raw_body.length
140
+ already = @raw_body.bytesize
141
141
  if cl > already
142
142
  rest = Sock.sphttp_drain_body(client_fd, cl - already)
143
143
  @raw_body = @raw_body + rest
@@ -153,18 +153,18 @@ module Tep
153
153
  # request but never sent the body gets dropped instead of
154
154
  # hanging the fiber forever.
155
155
  #
156
- # Returns @raw_body.length after the drain. Body parsing
156
+ # Returns @raw_body.bytesize after the drain. Body parsing
157
157
  # (form / multipart -> @params) happens at the end via
158
158
  # parse_form_body, same shape as consume_body.
159
159
  def consume_body_via_scheduler(client_fd)
160
160
  cl = content_length
161
- while @raw_body.length < cl
161
+ while @raw_body.bytesize < cl
162
162
  ready = Tep::Scheduler.io_wait(client_fd, Tep::Scheduler::READ, 5)
163
163
  if ready == 0
164
164
  break # timeout -- client never finished sending
165
165
  end
166
- chunk = Sock.sphttp_recv_some(client_fd, cl - @raw_body.length)
167
- if chunk.length == 0
166
+ chunk = Sock.sphttp_recv_some(client_fd, cl - @raw_body.bytesize)
167
+ if chunk.bytesize == 0
168
168
  # Over TLS an empty read can be a partial record (SSL_read
169
169
  # WANT_READ/WANT_WRITE) rather than a peer close -- re-park on
170
170
  # the indicated direction and retry instead of truncating the
data/lib/tep/server.rb CHANGED
@@ -238,7 +238,9 @@ module Tep
238
238
  if !res.headers.key?("Content-Type") && res.status != 204 && res.status != 304
239
239
  res.headers["Content-Type"] = "text/html; charset=utf-8"
240
240
  end
241
- res.headers["Content-Length"] = res.body.length.to_s
241
+ # bytesize, not length: a multibyte body would otherwise announce
242
+ # fewer bytes than it sends and desync keep-alive clients.
243
+ res.headers["Content-Length"] = res.body.bytesize.to_s
242
244
  if keep_alive
243
245
  res.headers["Connection"] = "keep-alive"
244
246
  else
@@ -247,7 +249,7 @@ module Tep
247
249
 
248
250
  head = build_head(req, res)
249
251
  Sock.sphttp_write_str(client, head)
250
- if res.body.length > 0
252
+ if res.body.bytesize > 0
251
253
  Sock.sphttp_write_str(client, res.body)
252
254
  end
253
255
  end
@@ -272,7 +274,7 @@ module Tep
272
274
  body = "<h1>" + status.to_s + " " + reason + "</h1><p>" + msg + "</p>\n"
273
275
  head = "HTTP/1.0 " + status.to_s + " " + reason + "\r\n" +
274
276
  "Content-Type: text/html; charset=utf-8\r\n" +
275
- "Content-Length: " + body.length.to_s + "\r\n" +
277
+ "Content-Length: " + body.bytesize.to_s + "\r\n" +
276
278
  "Connection: close\r\n\r\n"
277
279
  Sock.sphttp_write_str(client, head + body)
278
280
  end
@@ -356,9 +356,13 @@ module Tep
356
356
  Sock.sphttp_write_str(client, head)
357
357
  Sock.sphttp_sendfile(client, res.file_path)
358
358
  else
359
- head = head + "Content-Length: " + res.body.length.to_s + "\r\n\r\n"
359
+ # bytesize, not length: `length` counts characters, so a
360
+ # multibyte (UTF-8) body would announce fewer bytes than it
361
+ # sends. A keep-alive client then reads the surplus as the
362
+ # start of the next response and drops the connection.
363
+ head = head + "Content-Length: " + res.body.bytesize.to_s + "\r\n\r\n"
360
364
  Sock.sphttp_write_str(client, head)
361
- if res.body.length > 0
365
+ if res.body.bytesize > 0
362
366
  Sock.sphttp_write_str(client, res.body)
363
367
  end
364
368
  end
@@ -368,7 +372,7 @@ module Tep
368
372
  def self.send_simple(client, status, msg)
369
373
  reason = Tep.reason(status)
370
374
  head = "HTTP/1.0 " + status.to_s + " " + reason + "\r\n" +
371
- "Content-Length: " + msg.length.to_s + "\r\n" +
375
+ "Content-Length: " + msg.bytesize.to_s + "\r\n" +
372
376
  "Connection: close\r\n\r\n" + msg
373
377
  Sock.sphttp_write_str(client, head)
374
378
  0
data/lib/tep/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tep
2
- VERSION = "0.11.8"
2
+ VERSION = "0.11.9"
3
3
  end
data/test/test_http.rb CHANGED
@@ -68,6 +68,14 @@ class TestHttp < TepTest
68
68
  "status=" + r.status.to_s + " body=" + r.body
69
69
  end
70
70
 
71
+ # POST with a multibyte body: Content-Length must count bytes or the
72
+ # target reads a truncated body.
73
+ get '/post_echo_utf8/:port' do
74
+ c = Tep::Http.new("http://127.0.0.1:" + params[:port])
75
+ r = c.do_post("/echo_body", "héllo wörld — ü")
76
+ "status=" + r.status.to_s + " body=" + r.body
77
+ end
78
+
71
79
  # Non-2xx round trip.
72
80
  get '/teapot_from/:port' do
73
81
  r = Tep::Http.get("http://127.0.0.1:" + params[:port] + "/teapot")
@@ -113,6 +121,12 @@ class TestHttp < TepTest
113
121
  assert_equal "status=200 body=round trip body", res.body
114
122
  end
115
123
 
124
+ def test_multibyte_post_body_round_trips
125
+ res = get("/post_echo_utf8/#{@port}")
126
+ assert_equal "200", res.code
127
+ assert_equal "status=200 body=héllo wörld — ü", res.body.force_encoding("UTF-8")
128
+ end
129
+
116
130
  def test_non_2xx_status_propagates
117
131
  res = get("/teapot_from/#{@port}")
118
132
  assert_equal "200", res.code
@@ -52,6 +52,14 @@ class TestResponse < TepTest
52
52
  get '/halt-bare' do
53
53
  halt 418
54
54
  end
55
+
56
+ get '/utf8' do
57
+ "héllo wörld — ü"
58
+ end
59
+
60
+ post '/echo_body' do
61
+ request.body
62
+ end
55
63
  RB
56
64
 
57
65
  def test_default_200
@@ -120,4 +128,27 @@ class TestResponse < TepTest
120
128
  res = get("/ok")
121
129
  assert_equal "2", res["content-length"]
122
130
  end
131
+
132
+ def test_multibyte_content_length_counts_bytes
133
+ res = get("/utf8")
134
+ assert_equal "héllo wörld — ü", res.body.force_encoding("UTF-8")
135
+ assert_equal "héllo wörld — ü".bytesize.to_s, res["content-length"]
136
+ end
137
+
138
+ def test_multibyte_request_body_is_read_in_full_without_stalling
139
+ body = "héllo wörld — ü"
140
+ res = post("/echo_body", body, { "Content-Type" => "text/plain" })
141
+ assert_equal "200", res.code
142
+ assert_equal body, res.body.force_encoding("UTF-8")
143
+ end
144
+
145
+ def test_multibyte_body_keeps_keepalive_stream_in_sync
146
+ Net::HTTP.start("127.0.0.1", @port, read_timeout: 3) do |http|
147
+ r1 = http.get("/utf8")
148
+ r2 = http.get("/ok")
149
+ assert_equal "200", r1.code
150
+ assert_equal "200", r2.code
151
+ assert_equal "ok", r2.body
152
+ end
153
+ end
123
154
  end
@@ -22,6 +22,14 @@ class TestServerScheduled < TepTest
22
22
  post "/upper" do
23
23
  request.body.upcase
24
24
  end
25
+
26
+ get "/utf8" do
27
+ "héllo wörld — ü"
28
+ end
29
+
30
+ post "/echo_body" do
31
+ request.body
32
+ end
25
33
  RB
26
34
 
27
35
  def test_basic_get_through_scheduler
@@ -53,4 +61,34 @@ class TestServerScheduled < TepTest
53
61
  assert_equal "pong", r1.body
54
62
  assert_equal "two", r2.body
55
63
  end
64
+
65
+ def test_multibyte_body_content_length_counts_bytes
66
+ res = get("/utf8")
67
+ assert_equal "200", res.code
68
+ assert_equal "héllo wörld — ü", res.body.force_encoding("UTF-8")
69
+ assert_equal "héllo wörld — ü".bytesize.to_s, res["content-length"]
70
+ end
71
+
72
+ def test_multibyte_request_body_is_read_in_full_without_stalling
73
+ # The client's Content-Length is in bytes; counting the body in
74
+ # characters made the server wait for bytes that never come (5s
75
+ # io_wait timeout) before answering.
76
+ body = "héllo wörld — ü"
77
+ res = post("/echo_body", body, { "Content-Type" => "text/plain" })
78
+ assert_equal "200", res.code
79
+ assert_equal body, res.body.force_encoding("UTF-8")
80
+ end
81
+
82
+ def test_multibyte_body_keeps_keepalive_stream_in_sync
83
+ # One persistent connection, a multibyte body first. If Content-Length
84
+ # is counted in characters the body's trailing bytes spill into the
85
+ # next response's status line and the client sees a corrupt stream.
86
+ Net::HTTP.start("127.0.0.1", @port, read_timeout: 3) do |http|
87
+ r1 = http.get("/utf8")
88
+ r2 = http.get("/ping")
89
+ assert_equal "200", r1.code
90
+ assert_equal "200", r2.code
91
+ assert_equal "pong", r2.body
92
+ end
93
+ end
56
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.8
4
+ version: 0.11.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ori Pekelman