yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
671387e5189ba7ca0c6648ac83b027a7c1105c3c blob 2888 bytes (raw)

 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
 
# Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'server_helper'

class TestRackHijack < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper
  alias setup server_helper_setup
  alias teardown server_helper_teardown

  HIJACK_APP = lambda { |env|
    case env["PATH_INFO"]
    when "/hijack_input"
      io = env["rack.hijack"].call
      env["rack.hijack_io"].write("HTTP/1.0 201 OK\r\n\r\n")
      io.write("rack.input contents: #{env['rack.input'].read}")
      io.close
      return [ 500, {}, DieIfUsed.new ]
    when "/hijack_req"
      io = env["rack.hijack"].call
      if io.respond_to?(:read_nonblock) &&
         env["rack.hijack_io"].respond_to?(:read_nonblock)

        # exercise both, since we Rack::Lint may use different objects
        env["rack.hijack_io"].write("HTTP/1.0 200 OK\r\n\r\n")
        io.write("request.hijacked")
        io.close
        return [ 500, {}, DieIfUsed.new ]
      end
      [ 500, {}, [ "hijack BAD\n" ] ]
    when "/hijack_res"
      r = "response.hijacked"
      [ 200,
        {
          "X-Test" => "zzz",
          "Content-Length" => r.bytesize.to_s,
          "rack.hijack" => proc { |x| x.write(r); x.close }
        },
        DieIfUsed.new
      ]
    end
  }

  def test_hijack
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      GTL.synchronize { app(:rack, HIJACK_APP) { listen "#{host}:#{port}" } }
      logger(Logger.new(err.path))
      stderr_path err.path
    end
    pid = mkserver(cfg)
    res = Net::HTTP.start(host, port) { |h| h.get("/hijack_req") }

    wait_for_msg = lambda do |n|
      tries = 10000
      begin
        Thread.new { Thread.pass }.join # calls sched_yield() on MRI
      end until File.readlines(err.path).grep(/DieIfUsed/).size >= n ||
                (tries -= 1) < 0
    end
    assert_equal "request.hijacked", res.body
    assert_equal 200, res.code.to_i
    assert_equal "1.0", res.http_version

    wait_for_msg.call(1)

    res = Net::HTTP.start(host, port) { |h| h.get("/hijack_res") }
    assert_equal "response.hijacked", res.body
    assert_equal 200, res.code.to_i
    assert_equal "zzz", res["X-Test"]
    assert_equal "1.1", res.http_version

    wait_for_msg.call(2)

    errs = File.readlines(err.path).grep(/DieIfUsed/)
    assert_equal([ "INFO #{pid} closed DieIfUsed 1\n",
                   "INFO #{pid} closed DieIfUsed 2\n" ], errs)

    res = Net::HTTP.start(host, port) do |h|
      hdr = { "Content-Type" => 'application/octet-stream' }
      h.put("/hijack_input", "BLAH", hdr)
    end
    assert_equal "rack.input contents: BLAH", res.body
    assert_equal 201, res.code.to_i
    assert_equal "1.0", res.http_version
  ensure
    quit_wait(pid)
  end
end
debug log:

solving 671387e ...
found 671387e in https://yhbt.net/yahns.git

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

	../../../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).