From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id D5287211BB for ; Wed, 26 Dec 2018 19:57:19 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Subject: [PATCH 2/5] extras/exec_cgi: remove kgio dependency Date: Wed, 26 Dec 2018 19:57:13 +0000 Message-Id: <20181226195716.31202-3-e@80x24.org> In-Reply-To: <20181226195716.31202-1-e@80x24.org> References: <20181226195716.31202-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We don't need non-blocking I/O at all in this module and it's not coupled with the rest of yahns at all. --- extras/exec_cgi.rb | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/extras/exec_cgi.rb b/extras/exec_cgi.rb index b546e1f..7fb5138 100644 --- a/extras/exec_cgi.rb +++ b/extras/exec_cgi.rb @@ -21,18 +21,29 @@ # run ExecCgi.new('/path/to/cgit.cgi') # cgit: https://git.zx2c4.com/cgit/ # class ExecCgi - class MyIO < Kgio::Pipe + class MyIO attr_writer :my_pid attr_writer :body_tip + attr_reader :rd + + def initialize(rd) + @rd = rd + end def each buf = @body_tip || ''.dup if buf.size > 0 yield buf end - while tmp = kgio_read(8192, buf) + + case tmp = @rd.read_nonblock(8192, buf, exception: false) + when :wait_readable + @rd.wait_readable + when nil + break + else # String yield tmp - end + end while true self ensure # do this sooner, since the response body may be buffered, we want @@ -46,8 +57,8 @@ def close # Note: this object (and any client-specific objects) will never # be shared across different threads, so we do not need extra # mutual exclusion here. - return if closed? - super + return if @rd.closed? + @rd.close begin Process.waitpid(@my_pid) rescue Errno::ECHILD @@ -90,20 +101,23 @@ def call(env) cgi_env = { "GATEWAY_INTERFACE" => "CGI/1.1" } PASS_VARS.each { |key| val = env[key] and cgi_env[key] = val } env.each { |key,val| cgi_env[key] = val if key =~ /\AHTTP_/ } - pipe = MyIO.pipe - errbody = pipe[0] - errbody.my_pid = Process.spawn(cgi_env.merge!(@env), *@args, - out: pipe[1], close_others: true) - pipe[1].close - pipe = pipe[0] - if head = pipe.kgio_read(8192) + rd, wr = IO.pipe + io = MyIO.new(rd) + errbody = io + errbody.my_pid = spawn(cgi_env.merge!(@env), *@args, + out: wr, close_others: true) + wr.close + + begin + head = rd.readpartial(8192) until head =~ /\r?\n\r?\n/ - tmp = pipe.kgio_read(8192) or break + tmp = rd.readpartial(8192) head << tmp + tmp.clear end head, body = head.split(/\r?\n\r?\n/, 2) - pipe.body_tip = body + io.body_tip = body env["HTTP_VERSION"] ||= "HTTP/1.0" # stop Rack::Chunked for HTTP/0.9 @@ -117,8 +131,8 @@ def call(env) end status = headers.delete("Status") || 200 errbody = nil - [ status, headers, pipe ] - else + [ status, headers, io ] + rescue EOFError [ 500, { "Content-Length" => "0", "Content-Type" => "text/plain" }, [] ] end ensure -- EW