* [PATCH] wbuf_common: close body proxies on sendfile abort
@ 2014-12-15 5:30 7% Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2014-12-15 5:30 UTC (permalink / raw)
To: yahns-public
If we're streaming large files and sendfile fails (due to a
client aborting the connection), we need to ensure middleware
proxies are closed to ensure proper logging of a partial request.
This affects users of the "clogger" gem serving static files.
Unfortunately with clogger (or any Rack API-compliant middleware
using "to_path"), we still cannot log the amount of bytes
transferred for a static file.
---
Pushed to master as commit cf171301f3f8bc030825db7107151f06db9a00d5
lib/yahns/wbuf_common.rb | 3 +++
test/test_serve_static.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/lib/yahns/wbuf_common.rb b/lib/yahns/wbuf_common.rb
index 01e20bb..69fd00d 100644
--- a/lib/yahns/wbuf_common.rb
+++ b/lib/yahns/wbuf_common.rb
@@ -32,6 +32,9 @@ module Yahns::WbufCommon # :nodoc:
"sf_offset=#@sf_offset sf_count=#@sf_count"
end while @sf_count > 0
wbuf_close(client)
+ rescue
+ wbuf_close(client)
+ raise
end
def wbuf_close_common(client)
diff --git a/test/test_serve_static.rb b/test/test_serve_static.rb
index edd700c..b6875bd 100644
--- a/test/test_serve_static.rb
+++ b/test/test_serve_static.rb
@@ -85,6 +85,67 @@ class TestServeStatic < Testcase
[ off + 1, sparse ]
end
+ class ToPathClose
+ attr_reader :closed_p
+
+ def initialize(app, tmpdir)
+ @app = app
+ @tmpdir = tmpdir
+ @log = "#@tmpdir/to_path--close"
+ @body = nil
+ @closed_p = false
+ end
+
+ def call(env)
+ s, h, b = @app.call(env)
+ @body = b
+ [ s, h, self ]
+ end
+
+ def each
+ raise "ToPathClose#each should not be called"
+ end
+
+ def to_path
+ File.open(@log, "a") { |fp| fp.write("to_path\n") }
+ "#@tmpdir/sparse"
+ end
+
+ def close
+ File.open(@log, "a") { |fp| fp.write("close\n") }
+ raise "Double close" if @closed_p
+ @closed_p = true
+ nil
+ end
+ end
+
+ def test_aborted_sendfile_closes_opened_path
+ tmpdir = Dir.mktmpdir
+ mksparse(tmpdir)
+ fifo = "#{tmpdir}/to_path--close"
+ assert system("mkfifo", fifo), "mkfifo"
+ err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+ pid = mkserver(cfg) do
+ cfg.instance_eval do
+ app = Rack::Builder.new do
+ use ToPathClose, tmpdir
+ run Rack::File.new(tmpdir)
+ end
+ app(:rack, app) { listen "#{host}:#{port}" }
+ stderr_path err.path
+ end
+ end
+ c = get_tcp_client(host, port)
+ c.write "GET /sparse HTTP/1.1\r\nHost: example.com\r\n\r\n"
+ assert_equal "to_path\n", File.read(fifo)
+ wait_for_full(c)
+ assert_nil c.close
+ Timeout.timeout(30) { assert_equal "close\n", File.read(fifo) }
+ ensure
+ quit_wait(pid)
+ FileUtils.rm_rf(tmpdir)
+ end
+
def test_truncated_sendfile
tmpdir = Dir.mktmpdir
size, sparse = mksparse(tmpdir)
--
EW
^ permalink raw reply related [relevance 7%]
* [ANN] yahns 1.5.0 - initial OpenSSL support and bugfixes
@ 2014-12-21 2:25 6% Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2014-12-21 2:25 UTC (permalink / raw)
To: yahns-public
This release adds basic OpenSSL support for HTTPS connections.
Users must supply a OpenSSL::SSL::SSLContext object which yahns will use
as-is. yahns will only support HTTPS on Ruby 2.1 and later, as we rely
on "exception: false" in the read_nonblock and write_nonblock methods in
OpenSSL::SSL::SSLSocket.
See the Ruby standard library documentation on how to configure
OpenSSL::SSL::SSLContext objects to pass to the yahns "listen" directive
Editing the yahns config file to use OpenSSL goes something like this:
require 'openssl' # we will not do this for the user, even
ctx = OpenSSL::SSL::SSLContext.new
# user must configure ctx here...
listen 443, ssl_ctx: ctx
Note: yahns developers are not responsible for bugs in OpenSSL itself
or misconfigured SSLContext objects created by users. However, our
support of OpenSSL sockets is barely-tested and likely buggy, too.
Furthermore, the "sendfile" (or "kgio-sendfile") gem is no longer a
required dependency. It is currently impossible to use zero-copy
system calls with TLS sockets.
There are also minor cleanups and a bugfix to ensure body#close is
called for folks using body#to_path where `body' is the Rack
response body. This bug affected logging using the 'clogger' gem
when serving static files.
Shortlog of changes since 1.4.0
save around 1500 bytes of memory on x86-64
http_response: remove arg for Array#join
remove unused client_max_header_size config
config: use literal symbol array for now
http_response: reduce constants for 100 responses
favor Array#map! for freshly-split arrays
sendfile_compat: remove dependency on pread
extras/autoindex: simplify checking non-.gz
Rakefile: kill more useless gsub use
initial cut at OpenSSL support
test/test_ssl: skip test if SSL on older Rubies
wbuf_common: close body proxies on sendfile abort
bump published Ruby version requirement to 2.0
make sendfile an optional dependency
openssl_client: ignore SSL_accept errors during negotiation
Disclaimer: the yahns project does not and will never endorse
any commercial entities, including certificate authorities.
Shpx Nhgubevgl.
--
EW
^ permalink raw reply [relevance 6%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2014-12-15 5:30 7% [PATCH] wbuf_common: close body proxies on sendfile abort Eric Wong
2014-12-21 2:25 6% [ANN] yahns 1.5.0 - initial OpenSSL support and bugfixes Eric Wong
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).