yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
blob 47fc231f5094d4f0124d9b177ed5109f0559fa28 13442 bytes (raw)
name: test/test_proxy_pass.rb 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
 
# Copyright (C) 2015 all contributors <yahns-public@yhbt.net>
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'
require 'json'

class TestProxyPass < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper
  OMFG = 'a' * (1024 * 1024 * 32)
  TRUNCATE_BODY = "HTTP/1.1 200 OK\r\n" \
                  "Content-Length: 7\r\n" \
                  "Content-Type: text/PAIN\r\n\r\nshort".freeze
  TRUNCATE_HEAD = "HTTP/1.1 200 OK\r\n" \
                  "Content-Length: 666\r\n".freeze

  # not too big, or kcar will reject
  BIG_HEADER = [%w(Content-Type text/plain), %W(Content-Length #{OMFG.size})]
  3000.times { |i| BIG_HEADER << %W(X-#{i} BIG-HEADER!!!!!!!!!!!!!!) }
  BIG_HEADER.freeze

  class ProxiedApp
    def call(env)
      h = [ %w(Content-Length 3), %w(Content-Type text/plain) ]
      case env['REQUEST_METHOD']
      when 'GET'
        case env['PATH_INFO']
        when '/giant-body'
          h = [ %W(Content-Length #{OMFG.size}), %w(Content-Type text/plain) ]
          [ 200, h, [ OMFG ] ]
        when '/big-headers'
          [ 200, BIG_HEADER, [ OMFG ] ]
        when '/oversize-headers'
          100000.times { |x| h << %W(X-TOOBIG-#{x} #{x}) }
          [ 200, h, [ "big" ] ]
        when %r{\A/slow-headers-(\d+(?:\.\d+)?)\z}
          delay = $1.to_f
          io = env['rack.hijack'].call
          [ "HTTP/1.1 200 OK\r\n",
            "Content-Length: 7\r\n",
            "Content-Type: text/PAIN\r\n",
            "connection: close\r\n\r\n",
            "HIHIHI!"
          ].each do |l|
            io.write(l)
            sleep delay
          end
          io.close
        when '/truncate-body'
          io = env['rack.hijack'].call
          io.write(TRUNCATE_BODY)
          io.close
        when '/truncate-head'
          io = env['rack.hijack'].call
          io.write(TRUNCATE_HEAD)
          io.close
        when '/immediate-EOF'
          env['rack.hijack'].call.close
        when %r{\A/chunky-slow-(\d+(?:\.\d+)?)\z}
          delay = $1.to_f
          chunky = Object.new
          chunky.instance_variable_set(:@delay, delay)
          def chunky.each
            sleep @delay
            yield "3\r\nHI!\r\n"
            sleep @delay
            yield "0\r\n\r\n"
          end
          h = [ %w(Content-Type text/pain), %w(Transfer-Encoding chunked) ]
          [ 200, h, chunky ]
        else
          [ 200, h, [ "hi\n"] ]
        end
      when 'HEAD'
        case env['PATH_INFO']
        when '/big-headers'
          [ 200, BIG_HEADER, [] ]
        else
          [ 200, h, [] ]
        end
      when 'PUT'
        buf = env['rack.input'].read
        [ 201, {
          'Content-Length' => buf.bytesize.to_s,
          'Content-Type' => 'text/plain',
          }, [ buf ] ]
      end
    end
  end

  def setup
    @srv2 = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
    server_helper_setup
  end

  def teardown
    @srv2.close if defined?(@srv2) && !@srv2.closed?
    server_helper_teardown
  end

  def test_unix_socket_no_path
    tmpdir = Dir.mktmpdir
    unix_path = "#{tmpdir}/proxy_pass.sock"
    unix_srv = UNIXServer.new(unix_path)
    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
    host2, port2 = @srv2.addr[3], @srv2.addr[1]
    pid = mkserver(cfg) do
      @srv.autoclose = @srv2.autoclose = false
      ENV["YAHNS_FD"] = "#{@srv.fileno},#{@srv2.fileno}"
      require 'yahns/proxy_pass'
      cfg.instance_eval do
        app(:rack, Yahns::ProxyPass.new("unix:#{unix_path}:/$fullpath")) do
          listen "#{host}:#{port}"
        end
        app(:rack, Yahns::ProxyPass.new("unix:#{unix_path}:/foo$fullpath")) do
          listen "#{host2}:#{port2}"
        end
        stderr_path err.path
      end
    end

    pid2 = mkserver(cfg, unix_srv) do
      @srv.close
      @srv2.close
      cfg.instance_eval do
        rapp = lambda do |env|
          body = env.to_json
          hdr = {
            'Content-Length' => body.bytesize.to_s,
            'Content-Type' => 'application/json',
          }
          [ 200, hdr, [ body ] ]
        end
        app(:rack, rapp) { listen unix_path }
        stderr_path err.path
      end
    end

    Net::HTTP.start(host, port) do |http|
      res = http.request(Net::HTTP::Get.new('/f00'))
      assert_equal 200, res.code.to_i
      body = JSON.parse(res.body)
      assert_equal '/f00', body['PATH_INFO']

      res = http.request(Net::HTTP::Get.new('/f00foo'))
      assert_equal 200, res.code.to_i
      body = JSON.parse(res.body)
      assert_equal '/f00foo', body['PATH_INFO']
    end

    Net::HTTP.start(host2, port2) do |http|
      res = http.request(Net::HTTP::Get.new('/Foo'))
      assert_equal 200, res.code.to_i
      body = JSON.parse(res.body)
      assert_equal '/foo/Foo', body['PATH_INFO']

      res = http.request(Net::HTTP::Get.new('/Foofoo'))
      assert_equal 200, res.code.to_i
      body = JSON.parse(res.body)
      assert_equal '/foo/Foofoo', body['PATH_INFO']
    end
  ensure
    quit_wait(pid)
    quit_wait(pid2)
    unix_srv.close if unix_srv
    FileUtils.rm_rf(tmpdir) if tmpdir
  end

  def test_proxy_pass
    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
    host2, port2 = @srv2.addr[3], @srv2.addr[1]
    pid = mkserver(cfg) do
      require 'yahns/proxy_pass'
      @srv2.close
      cfg.instance_eval do
        app(:rack, Yahns::ProxyPass.new("http://#{host2}:#{port2}")) do
          listen "#{host}:#{port}", sndbuf: 16384
          client_max_body_size nil
        end
        stderr_path err.path
      end
    end

    pid2 = mkserver(cfg, @srv2) do
      @srv.close
      cfg.instance_eval do
        app(:rack, ProxiedApp.new) do
          listen "#{host2}:#{port2}"
          client_max_body_size nil
        end
        stderr_path err.path
      end
    end

    check_pipelining(host, port)

    gplv3 = File.open('COPYING')

    Net::HTTP.start(host, port) do |http|
      res = http.request(Net::HTTP::Get.new('/'))
      assert_equal 200, res.code.to_i
      n = res.body.bytesize
      assert_operator n, :>, 1
      res = http.request(Net::HTTP::Head.new('/'))
      assert_equal 200, res.code.to_i
      assert_equal n, res['Content-Length'].to_i
      assert_nil res.body

      # chunked encoding (PUT)
      req = Net::HTTP::Put.new('/')
      req.body_stream = gplv3
      req.content_type = 'application/octet-stream'
      req['Transfer-Encoding'] = 'chunked'
      res = http.request(req)
      gplv3.rewind
      assert_equal gplv3.read, res.body
      assert_equal 201, res.code.to_i

      # chunked encoding (GET)
      res = http.request(Net::HTTP::Get.new('/chunky-slow-0.1'))
      assert_equal 200, res.code.to_i
      assert_equal 'chunked', res['Transfer-encoding']
      assert_equal "HI!", res.body

      # slow headers (GET)
      res = http.request(Net::HTTP::Get.new('/slow-headers-0.01'))
      assert_equal 200, res.code.to_i
      assert_equal 'text/PAIN', res['Content-Type']
      assert_equal 'HIHIHI!', res.body

      # normal content-length (PUT)
      gplv3.rewind
      req = Net::HTTP::Put.new('/')
      req.body_stream = gplv3
      req.content_type = 'application/octet-stream'
      req.content_length = gplv3.size
      res = http.request(req)
      gplv3.rewind
      assert_equal gplv3.read, res.body
      assert_equal 201, res.code.to_i

      # giant body
      res = http.request(Net::HTTP::Get.new('/giant-body'))
      assert_equal 200, res.code.to_i
      assert_equal OMFG, res.body

      # giant PUT content-length
      req = Net::HTTP::Put.new('/')
      req.body_stream = StringIO.new(OMFG)
      req.content_type = 'application/octet-stream'
      req.content_length = OMFG.size
      res = http.request(req)
      assert_equal OMFG, res.body
      assert_equal 201, res.code.to_i

      # giant PUT chunked encoding
      req = Net::HTTP::Put.new('/')
      req.body_stream = StringIO.new(OMFG)
      req.content_type = 'application/octet-stream'
      req['Transfer-Encoding'] = 'chunked'
      res = http.request(req)
      assert_equal OMFG, res.body
      assert_equal 201, res.code.to_i

      # sometimes upstream feeds kcar too much
      req = Net::HTTP::Get.new('/oversize-headers')
      res = http.request(req)
      errs = File.readlines(@err.path).grep(/ERROR/)
      assert_equal 1, errs.size
      assert_match %r{upstream response error:}, errs[0]
      @err.truncate(0)
    end

    # ensure we do not chunk responses back to an HTTP/1.0 client even if
    # the proxy <-> upstream connection is chunky
    %w(0 0.1).each do |delay|
      begin
        h10 = TCPSocket.new(host, port)
        h10.write "GET /chunky-slow-#{delay} HTTP/1.0\r\n\r\n"
        res = Timeout.timeout(60) { h10.read }
        assert_match %r{^Connection: close\r\n}, res
        assert_match %r{^Content-Type: text/pain\r\n}, res
        assert_match %r{\r\n\r\nHI!\z}, res
        refute_match %r{^Transfer-Encoding:}, res
        refute_match %r{\r0\r\n}, res
      ensure
        h10.close
      end
    end
    check_truncated_upstream(host, port)
    check_slow_giant_body(host, port)
    check_slow_read_headers(host, port)
  ensure
    gplv3.close if gplv3
    quit_wait pid
    quit_wait pid2
  end

  def check_pipelining(host, port)
    pl = TCPSocket.new(host, port)
    r1 = ''
    r2 = ''
    r3 = ''
    Timeout.timeout(60) do
      pl.write "GET / HTTP/1.1\r\nHost: example.com\r\n\r\nGET /"
      until r1 =~ /hi\n/
        r1 << pl.readpartial(666)
      end

      pl.write "chunky-slow-0.1 HTTP/1.1\r\nHost: example.com\r\n\r\nP"
      until r2 =~ /\r\n3\r\nHI!\r\n0\r\n\r\n/
        r2 << pl.readpartial(666)
      end

      pl.write "UT / HTTP/1.1\r\nHost: example.com\r\n"
      pl.write "Transfer-Encoding: chunked\r\n\r\n"
      pl.write "6\r\nchunky\r\n"
      pl.write "0\r\n\r\n"

      until r3 =~ /chunky/
        r3 << pl.readpartial(666)
      end

      # ensure stuff still works after a chunked upload:
      pl.write "GET / HTTP/1.1\r\nHost: example.com\r\n\r\nP"
      after_up = ''
      until after_up =~ /hi\n/
        after_up << pl.readpartial(666)
      end
      re = /^Date:[^\r\n]+/
      assert_equal after_up.sub(re, ''), r1.sub(re, '')

      # another upload, this time without chunking
      pl.write "UT / HTTP/1.1\r\nHost: example.com\r\n"
      pl.write "Content-Length: 8\r\n\r\n"
      pl.write "identity"
      identity = ''

      until identity =~ /identity/
        identity << pl.readpartial(666)
      end
      assert_match %r{identity\z}, identity
      assert_match %r{\AHTTP/1\.1 201\b}, identity

      # ensure stuff still works after an identity upload:
      pl.write "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
      after_up = ''
      until after_up =~ /hi\n/
        after_up << pl.readpartial(666)
      end
      re = /^Date:[^\r\n]+/
      assert_equal after_up.sub(re, ''), r1.sub(re, '')
    end
    r1 = r1.split("\r\n").reject { |x| x =~ /^Date: / }
    r2 = r2.split("\r\n").reject { |x| x =~ /^Date: / }
    assert_equal 'HTTP/1.1 200 OK', r1[0]
    assert_equal 'HTTP/1.1 200 OK', r2[0]
    assert_match %r{\r\n\r\nchunky\z}, r3
    assert_match %r{\AHTTP/1\.1 201 Created\r\n}, r3
  rescue => e
    warn [ e.class, e.message ].inspect
    warn e.backtrace.join("\n")
  ensure
    pl.close
  end

  def check_truncated_upstream(host, port)
    # we want to make sure we show the truncated response without extra headers
    s = TCPSocket.new(host, port)
    check_err
    res = Timeout.timeout(60) do
      s.write "GET /truncate-body HTTP/1.1\r\nHost: example.com\r\n\r\n"
      s.read
    end
    s.close

    exp = "HTTP/1.1 200 OK\r\n" \
          "Content-Length: 7\r\n" \
          "Content-Type: text/PAIN\r\n" \
          "Connection: keep-alive\r\n" \
          "\r\nshort"
    assert_equal exp, res
    errs = File.readlines(@err.path).grep(/\bERROR\b/)
    assert_equal 1, errs.size
    assert_match(/premature upstream EOF/, errs[0])
    @err.truncate(0)

    # truncated headers or no response at all...
    # Send a 502 error
    %w(immediate-EOF truncate-head).each do |path|
      s = TCPSocket.new(host, port)
      check_err
      res = Timeout.timeout(60) do
        s.write "GET /#{path} HTTP/1.1\r\nHost: example.com\r\n\r\n"
        s.read(1024)
      end
      assert_match %r{\AHTTP/1.1 502\s+}, res
      s.close
      errs = File.readlines(@err.path).grep(/\bERROR\b/)
      assert_equal 1, errs.size
      assert_match(/premature upstream EOF/, errs[0])
      @err.truncate(0)
    end
  end

  def check_slow_giant_body(host, port)
    s = TCPSocket.new(host, port)
    s.write "GET /giant-body HTTP/1.0\r\n\r\n"
    sleep 0.1
    str = ''
    buf = ''
    assert_raises(EOFError) { loop { str << s.readpartial(400, buf) } }
    h, b = str.split(/\r\n\r\n/, 2)
    assert_equal OMFG, b
    assert_match %r{\AHTTP/1\.1 200\b}, h
  ensure
    s.close if s
  end

  def check_slow_read_headers(host, port)
    s = TCPSocket.new(host, port)
    s.write "GET /big-headers HTTP/1.1\r\nHost: example.com\r\n\r\n"
    s.write "HEAD /big-headers HTTP/1.0\r\n\r\n"
    buf = ''
    res = ''
    sleep 0.1
    begin
      res << s.readpartial(32786, buf)
    rescue EOFError
      break
    end while true
    # res = Timeout.timeout(60) { s.read }
    assert_match %r{\r\n\r\n\z}, res
    assert_match %r{\AHTTP/1\.1 200 OK}, res
  ensure
    s.close if s
  end
end

debug log:

solving 47fc231 ...
found 47fc231 in https://yhbt.net/yahns.git/

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).