various alternative implementation for response

This commit is contained in:
Jocelyn Fiat
2011-07-25 12:37:50 +02:00
parent 5a155e0cee
commit 2c6196017e
3 changed files with 273 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
note
description: "Summary description for {GW_IN_MEMORY_RESPONSE}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GW_IN_MEMORY_RESPONSE
inherit
GW_RESPONSE
redefine
commit
end
create {GW_APPLICATION}
make
feature {NONE} -- Initialization
make
do
create header.make
create body.make (100)
end
header: GW_HEADER
body: STRING_8
feature -- Status setting
set_status_code (c: INTEGER)
-- Set the status code of the response
do
header.put_status (c)
end
feature -- Output operation
write_string (s: STRING)
-- Send the content of `s'
do
body.append (s)
end
write_file_content (fn: STRING)
-- Send the content of file `fn'
local
f: RAW_FILE
do
create f.make (fn)
if f.exists and then f.is_readable then
f.open_read
from
until
f.exhausted
loop
f.read_stream (1024)
write_string (f.last_string)
end
f.close
end
end
write_header_object (h: GW_HEADER)
-- Send `header' to `output'.
do
header := h
end
feature {GW_APPLICATION} -- Commit
commit (a_output: GW_OUTPUT_STREAM)
do
header.send_to (a_output)
write_string (body)
Precursor (a_output)
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,53 @@
note
description: "Summary description for {GW_IN_MEMORY_RESPONSE_APPLICATION}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
GW_IN_MEMORY_RESPONSE_APPLICATION
inherit
GW_APPLICATION
redefine
process
end
feature -- Access
request_count: INTEGER
-- Request count
feature -- Execution
process (env: GW_ENVIRONMENT; a_input: GW_INPUT_STREAM; a_output: GW_OUTPUT_STREAM)
-- Process request with environment `env', and i/o streams `a_input' and `a_output'
do
request_count := request_count + 1
Precursor (env, a_input, a_output)
end
feature -- Factory
new_request (env: GW_ENVIRONMENT; a_input: GW_INPUT_STREAM): GW_REQUEST
do
create {GW_REQUEST_IMP} Result.make (env, a_input)
Result.execution_variables.set_variable (request_count.out, "REQUEST_COUNT")
end
new_response (a_output: GW_OUTPUT_STREAM): GW_IN_MEMORY_RESPONSE
do
create {GW_IN_MEMORY_RESPONSE} Result.make
end
note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,129 @@
note
description: "Summary description for {GW_BUFFERED_RESPONSE}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GW_BUFFERED_RESPONSE
inherit
GW_RESPONSE
redefine
commit
end
create {GW_APPLICATION}
make
feature {NONE} -- Initialization
make (a_output: like output; a_buffer_size: INTEGER)
do
output := a_output
buffer_capacity := a_buffer_size
create buffer.make (a_buffer_size)
end
output: GW_OUTPUT_STREAM
buffer: STRING_8
buffer_capacity: INTEGER
buffer_count: INTEGER
feature -- Status setting
set_status_code (c: INTEGER)
-- Set the status code of the response
do
header.put_status (c)
end
feature -- Output operation
write_string (s: STRING)
-- Send the content of `s'
local
buf: like buffer
len_b, len_s: INTEGER
do
buf := buffer
len_s := s.count
len_b := buffer_count
if len_b + len_s >= buffer_capacity then
flush_buffer
if len_s >= buffer_capacity then
-- replace buffer by `s'
buffer := s
buffer_count := len_s
flush_buffer
-- restore buffer with `buf'
buffer := buf
else
buf.append (s)
buffer_count := len_s
end
else
buf.append (s)
buffer_count := len_b + len_s
end
end
write_file_content (fn: STRING)
-- Send the content of file `fn'
local
f: RAW_FILE
do
create f.make (fn)
if f.exists and then f.is_readable then
f.open_read
from
until
f.exhausted
loop
f.read_stream (buffer_capacity)
write_string (f.last_string)
end
f.close
end
end
write_header_object (h: GW_HEADER)
-- Send `header' to `output'.
do
header := h
end
feature {NONE} -- Implementation
flush_buffer
require
buffer_count_match_buffer: buffer_count = buffer.count
do
output.put_string (buffer)
buffer_count := 0
ensure
buffer_flushed: buffer_count = 0 and buffer.count = 0
end
feature {GW_APPLICATION} -- Commit
commit (a_output: GW_OUTPUT_STREAM)
do
flush_buffer
Precursor (a_output)
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end