From 885195dbaa196b6e70c7489238e618a056ef724c Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Mon, 24 Oct 2016 12:47:33 +0200 Subject: [PATCH] Added `WSF_RESPONSE.put_file_content (f: FILE, a_offset: INTEGER; a_count: INTEGER)` to allow potential future optimization. --- .../response/wgi_filter_response.e | 6 +++ .../specification/response/wgi_response.e | 8 ++++ .../specification/stream/wgi_output_stream.e | 43 +++++++++++++++++++ .../src/implementation/wgi_response_stream.e | 6 +++ .../wsf_wgi_delayed_header_response.e | 8 ++++ library/server/wsf/src/wsf_response.e | 12 +++++- 6 files changed, 82 insertions(+), 1 deletion(-) diff --git a/library/server/ewsgi/specification/response/wgi_filter_response.e b/library/server/ewsgi/specification/response/wgi_filter_response.e index f9b14d70..1cea7bed 100644 --- a/library/server/ewsgi/specification/response/wgi_filter_response.e +++ b/library/server/ewsgi/specification/response/wgi_filter_response.e @@ -131,6 +131,12 @@ feature -- Output operation wgi_response.put_substring (s, a_begin_index, a_end_index) end + put_file_content (f: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `f' starting at offset `a_offset'. + do + wgi_response.put_file_content (f, a_offset, a_count) + end + flush -- Flush if it makes sense do diff --git a/library/server/ewsgi/specification/response/wgi_response.e b/library/server/ewsgi/specification/response/wgi_response.e index 4a6041b9..df690dd2 100644 --- a/library/server/ewsgi/specification/response/wgi_response.e +++ b/library/server/ewsgi/specification/response/wgi_response.e @@ -142,6 +142,14 @@ feature -- Output operation deferred end + put_file_content (f: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `f' starting at offset `a_offset'. + require + message_writable: message_writable + not_too_big: a_offset + a_count <= f.count + deferred + end + flush -- Flush if it makes sense deferred diff --git a/library/server/ewsgi/specification/stream/wgi_output_stream.e b/library/server/ewsgi/specification/stream/wgi_output_stream.e index 8d02bdea..195208ad 100644 --- a/library/server/ewsgi/specification/stream/wgi_output_stream.e +++ b/library/server/ewsgi/specification/stream/wgi_output_stream.e @@ -37,6 +37,49 @@ feature -- Output end end + put_file_content (a_file: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `a_file' starting at offset `a_offset'. + --| Could be redefine for optimization. + require + is_open_write: is_open_write + a_file_not_void: a_file /= Void + local + l_close_needed: BOOLEAN + n: INTEGER + s: STRING + do + if a_file.exists and then a_file.is_access_readable then + if a_file.is_open_read then + l_close_needed := False + else + l_close_needed := True + a_file.open_read + end + from + n := a_count + if a_offset > 0 then + a_file.move (a_offset) + end + until + a_file.exhausted or n <= 0 + loop + a_file.read_stream (n.min (4_096)) + s := a_file.last_string + if s.is_empty then + -- network error? + n := 0 + else + n := n - s.count + check n >= 0 end + put_string (s) + end + end + if l_close_needed then + a_file.close + end + end + end + put_character (c: CHARACTER_8) -- Write `c' to output stream. --| Could be redefined for optimization diff --git a/library/server/ewsgi/src/implementation/wgi_response_stream.e b/library/server/ewsgi/src/implementation/wgi_response_stream.e index 73edf89a..82bf3a6b 100644 --- a/library/server/ewsgi/src/implementation/wgi_response_stream.e +++ b/library/server/ewsgi/src/implementation/wgi_response_stream.e @@ -103,6 +103,12 @@ feature -- Output operation output.put_substring (s, start_index, end_index) end + put_file_content (f: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `f' starting at offset `a_offset'. + do + output.put_file_content (f, a_offset, a_count) + end + flush do output.flush diff --git a/library/server/wsf/src/implementation/wsf_wgi_delayed_header_response.e b/library/server/wsf/src/implementation/wsf_wgi_delayed_header_response.e index b5aeb6fc..e10f839b 100644 --- a/library/server/wsf/src/implementation/wsf_wgi_delayed_header_response.e +++ b/library/server/wsf/src/implementation/wsf_wgi_delayed_header_response.e @@ -13,6 +13,7 @@ inherit put_character, put_string, put_substring, + put_file_content, flush, message_writable, message_committed @@ -108,6 +109,13 @@ feature -- Output operation Precursor (s, a_begin_index, a_end_index) end + put_file_content (f: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `f' starting at offset `a_offset'. + do + process_header + Precursor (f, a_offset, a_count) + end + flush -- Flush if it makes sense do diff --git a/library/server/wsf/src/wsf_response.e b/library/server/wsf/src/wsf_response.e index 0ca3530c..faf40a4f 100644 --- a/library/server/wsf/src/wsf_response.e +++ b/library/server/wsf/src/wsf_response.e @@ -383,6 +383,16 @@ feature -- Body increment_transfered_content_length (a_end_index - a_begin_index + 1) end + put_file_content (f: FILE; a_offset: INTEGER; a_count: INTEGER) + -- Send `a_count' bytes from the content of file `f' starting at offset `a_offset'. + require + message_writable: message_writable + not_too_big: a_offset + a_count <= f.count + do + wgi_response.put_file_content (f, a_offset, a_count) + increment_transfered_content_length (a_count) + end + feature -- Chunk body put_chunk (a_content: READABLE_STRING_8; a_ext: detachable READABLE_STRING_8) @@ -572,7 +582,7 @@ feature {NONE} -- Implemenation end note - copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others" + copyright: "2011-2016, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software