Implemented more user friendly WSF_RESPONSE

i.e allow to change the status code and the header as long as no content is really sent back to the client

This requires an addition WGI_RESPONSE, new post_commit_action: PROCEDURE [...]
This commit is contained in:
Jocelyn Fiat
2013-03-12 16:52:45 +01:00
parent 82784529fe
commit 86777d75ea
8 changed files with 342 additions and 32 deletions

View File

@@ -44,6 +44,7 @@ feature -- Execution
create req.make ((create {EXECUTION_ENVIRONMENT}).starting_environment_variables, create {WGI_CGI_INPUT_STREAM}.make, Current) create req.make ((create {EXECUTION_ENVIRONMENT}).starting_environment_variables, create {WGI_CGI_INPUT_STREAM}.make, Current)
create res.make (create {WGI_CGI_OUTPUT_STREAM}.make, create {WGI_CGI_ERROR_STREAM}.make) create res.make (create {WGI_CGI_OUTPUT_STREAM}.make, create {WGI_CGI_ERROR_STREAM}.make)
service.execute (req, res) service.execute (req, res)
res.push
else else
if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then
if res /= Void then if res /= Void then
@@ -53,6 +54,7 @@ feature -- Execution
if res.message_writable then if res.message_writable then
res.put_string ("<pre>" + l_trace + "</pre>") res.put_string ("<pre>" + l_trace + "</pre>")
end end
res.push
end end
end end
end end

View File

@@ -65,6 +65,7 @@ feature -- Execution
create req.make (vars, a_input, Current) create req.make (vars, a_input, Current)
create res.make (a_output, a_output) create res.make (a_output, a_output)
service.execute (req, res) service.execute (req, res)
res.push
else else
if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then
if res /= Void then if res /= Void then
@@ -74,6 +75,7 @@ feature -- Execution
if res.message_writable then if res.message_writable then
res.put_string ("<pre>" + l_trace + "</pre>") res.put_string ("<pre>" + l_trace + "</pre>")
end end
res.push
end end
end end
end end

View File

@@ -133,10 +133,10 @@ feature -- Server
res: detachable WGI_NINO_RESPONSE_STREAM res: detachable WGI_NINO_RESPONSE_STREAM
do do
create req.make (env, create {WGI_NINO_INPUT_STREAM}.make (a_socket), Current) create req.make (env, create {WGI_NINO_INPUT_STREAM}.make (a_socket), Current)
create res.make (create {WGI_NINO_OUTPUT_STREAM}.make (a_socket), Void) create res.make (create {WGI_NINO_OUTPUT_STREAM}.make (a_socket), create {WGI_NINO_ERROR_STREAM}.make_stderr (a_socket.descriptor.out))
req.set_meta_string_variable ("RAW_HEADER_DATA", a_headers_text) req.set_meta_string_variable ("RAW_HEADER_DATA", a_headers_text)
service.execute (req, res) service.execute (req, res)
res.commit res.push
end end
note note

View File

@@ -0,0 +1,70 @@
note
description: "Summary description for WGI_CGI_ERROR_STREAM."
legal: "See notice at end of class."
status: "See notice at end of class."
date: "$Date$"
revision: "$Revision$"
class
WGI_NINO_ERROR_STREAM
inherit
WGI_ERROR_STREAM
create
make,
make_stderr,
make_stdout
feature {NONE} -- Initialization
make (a_identifier: READABLE_STRING_8; a_file: PLAIN_TEXT_FILE)
do
identifier := a_identifier
output := a_file
end
make_stderr (a_identifier: READABLE_STRING_8)
do
make (a_identifier, io.error)
end
make_stdout (a_identifier: READABLE_STRING_8)
do
make (a_identifier, io.error)
end
feature -- Access
identifier: READABLE_STRING_8
output: FILE
feature -- Error
put_error (a_message: READABLE_STRING_8)
local
s: STRING
do
create s.make (a_message.count + identifier.count + 4)
s.append_character ('[')
s.append (identifier)
s.append_character (']')
s.append_character (' ')
s.append (a_message)
s.append_character ('%N')
-- Display it at once.
output.put_string (s)
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

@@ -17,6 +17,7 @@ feature {NONE} -- Initialization
make_with_response (res: WGI_RESPONSE) make_with_response (res: WGI_RESPONSE)
do do
wgi_response := res wgi_response := res
res.set_post_commit_action (agent commit)
end end
wgi_response: WGI_RESPONSE wgi_response: WGI_RESPONSE
@@ -26,7 +27,7 @@ feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit
commit commit
-- Commit the current response -- Commit the current response
do do
-- do nothing, this will be done internal on the original `wgi_response' object wgi_response.set_post_commit_action (Void)
end end
feature -- Status report feature -- Status report
@@ -129,7 +130,7 @@ feature -- Error reporting
end end
note note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -8,6 +8,15 @@ deferred class
feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit
push
-- Commit and push response
do
commit
if attached post_commit_action as act then
act.call (Void)
end
end
commit commit
-- Commit the current response -- Commit the current response
deferred deferred
@@ -17,6 +26,21 @@ feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit
message_committed: message_committed message_committed: message_committed
end end
feature -- Access: commit
post_commit_action: detachable PROCEDURE [ANY, TUPLE]
-- Action associated with the final `commit' execution
-- Note: useful to trigger action just after the
-- response is transfered to the client.
feature -- Change: commit
set_post_commit_action (act: like post_commit_action)
-- Assign `act' to `post_commit_action'
do
post_commit_action := act
end
feature -- Status report feature -- Status report
status_committed: BOOLEAN status_committed: BOOLEAN
@@ -131,7 +155,7 @@ feature -- Error reporting
end end
note note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -0,0 +1,108 @@
note
description: "Summary description for {WGI_DELAYED_HEADER_RESPONSE}."
date: "$Date$"
revision: "$Revision$"
class
WSF_WGI_DELAYED_HEADER_RESPONSE
inherit
WGI_FILTER_RESPONSE
redefine
commit,
put_character,
put_string,
put_substring,
flush,
message_writable
end
WSF_RESPONSE_EXPORTER
create
make
feature {NONE} -- Initialization
make (r: WGI_RESPONSE; res: WSF_RESPONSE)
do
wsf_response := res
make_with_response (r)
end
feature {NONE} -- Implementation
wsf_response: WSF_RESPONSE
commit
do
Precursor
if not header_committed then
process_header
end
end
process_header
require
header_not_committed: not header_committed
do
-- If no content is sent, the final `{WGI_REPONSE}.push' will call `process_header'
-- via `{WGI_RESPONSE}.post_commit_action'
wgi_response.set_post_commit_action (Void)
-- commit status code and reason phrase
-- commit header text
wsf_response.process_header
-- update wgi_response on wsf_response to send content directly
wsf_response.set_wgi_response (wgi_response)
ensure
header_committed: header_committed
end
feature -- Status report
message_writable: BOOLEAN = True
-- Can message be written?
feature -- Output operation
put_character (c: CHARACTER_8)
-- Send the character `c'
do
process_header
Precursor (c)
end
put_string (s: READABLE_STRING_8)
-- Send the string `s'
do
process_header
Precursor (s)
end
put_substring (s: READABLE_STRING_8; a_begin_index, a_end_index: INTEGER)
-- Send the substring `s[a_begin_index:a_end_index]'
do
process_header
Precursor (s, a_begin_index, a_end_index)
end
flush
-- Flush if it makes sense
do
process_header
Precursor
end
note
copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Olivier Ligot, 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

@@ -28,15 +28,32 @@ convert
feature {NONE} -- Initialization feature {NONE} -- Initialization
make_from_wgi (r: WGI_RESPONSE) make_from_wgi (r: WGI_RESPONSE)
local
wres: detachable WSF_WGI_DELAYED_HEADER_RESPONSE
do do
transfered_content_length := 0 transfered_content_length := 0
create header.make
wgi_response := r wgi_response := r
create wres.make (r, Current)
wgi_response := wres
set_status_code ({HTTP_STATUS_CODE}.ok) -- Default value
end end
feature {WSF_RESPONSE_EXPORTER} -- Properties feature {WSF_RESPONSE_EXPORTER} -- Properties
wgi_response: WGI_RESPONSE wgi_response: WGI_RESPONSE
-- Associated WGI_RESPONSE -- Associated WGI_RESPONSE.
header: WSF_HEADER
-- Associated response header.
feature {WSF_RESPONSE_EXPORTER} -- Change
set_wgi_response (res: WGI_RESPONSE)
-- Set associated WGI_RESPONSE
do
wgi_response := res
end
feature -- Status report feature -- Status report
@@ -76,6 +93,7 @@ feature -- Status setting
-- Set response status code -- Set response status code
-- Should be done before sending any data back to the client -- Should be done before sending any data back to the client
--| note: the status is really sent when the header are set --| note: the status is really sent when the header are set
--| Default value might be set to 200 {HTTP_HEADER}.ok.
require require
a_code_valid: a_code > 0 a_code_valid: a_code > 0
status_not_set: not status_committed status_not_set: not status_committed
@@ -112,61 +130,146 @@ feature -- Status setting
status_reason_phrase: detachable READABLE_STRING_8 status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase (optional) -- Custom status reason phrase (optional)
feature {WSF_RESPONSE_EXPORTER} -- Header output operation
process_header
require
header_not_committed: not header_committed
do
if not header_committed then
-- commit status code and reason phrase
wgi_response.set_status_code (status_code, status_reason_phrase)
-- commit header text
wgi_response.put_header_text (header.string)
end
ensure
status_committed: status_committed
header_committed: header_committed
end
report_content_already_sent_and_header_ignored
do
put_error ("Content already sent, new header text ignored!")
end
feature -- Header output operation feature -- Header output operation
put_header_text (a_text: READABLE_STRING_8) put_header_line (h: READABLE_STRING_8)
-- Sent `a_text' and just before send the status code -- Put header `h'
-- Replace any existing value
require
header_not_committed: not header_committed
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.put_header (h)
end
end
add_header_line (h: READABLE_STRING_8)
-- Add header `h'
-- This can lead to duplicated header entries
require
header_not_committed: not header_committed
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.add_header (h)
end
end
put_header_text (a_text: READABLE_STRING_8)
-- Put the multiline header `a_text'
-- Overwite potential existing header
require require
status_set: status_is_set
header_not_committed: not header_committed header_not_committed: not header_committed
a_text_ends_with_single_crlf: a_text.count > 2 implies not a_text.substring (a_text.count - 2, a_text.count).same_string ("%R%N") a_text_ends_with_single_crlf: a_text.count > 2 implies not a_text.substring (a_text.count - 2, a_text.count).same_string ("%R%N")
a_text_does_not_end_with_double_crlf: a_text.count > 4 implies not a_text.substring (a_text.count - 4, a_text.count).same_string ("%R%N%R%N") a_text_does_not_end_with_double_crlf: a_text.count > 4 implies not a_text.substring (a_text.count - 4, a_text.count).same_string ("%R%N%R%N")
do do
wgi_response.set_status_code (status_code, status_reason_phrase) if header_committed then
wgi_response.put_header_text (a_text) report_content_already_sent_and_header_ignored
else
header.append_raw_header_data (a_text)
end
ensure ensure
status_set: status_is_set
status_committed: status_committed
header_committed: header_committed
message_writable: message_writable message_writable: message_writable
end end
add_header_text (a_text: READABLE_STRING_8)
-- Add the multiline header `a_text'
-- Does not replace existing header with same name
-- This could leads to multiple header with the same name
require
header_not_committed: not header_committed
a_text_ends_with_single_crlf: a_text.count > 2 implies not a_text.substring (a_text.count - 2, a_text.count).same_string ("%R%N")
a_text_does_not_end_with_double_crlf: a_text.count > 4 implies not a_text.substring (a_text.count - 4, a_text.count).same_string ("%R%N%R%N")
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.append_raw_header_data (a_text)
end
ensure
status_set: status_is_set
message_writable: message_writable
end
feature -- Header output operation: helpers
put_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]]) put_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Send headers with status `a_status', and headers from `a_headers' -- Put headers with status `a_status', and headers from `a_headers'
require require
status_not_committed: not status_committed status_not_committed: not status_committed
header_not_committed: not header_committed header_not_committed: not header_committed
local local
h: HTTP_HEADER h: HTTP_HEADER
do do
set_status_code (a_status_code)
if a_headers /= Void then if a_headers /= Void then
create h.make_from_array (a_headers) create h.make_from_array (a_headers)
put_header_text (h.string) put_header_text (h.string)
end end
ensure ensure
status_set: status_is_set status_set: status_is_set
header_committed: header_committed message_writable: message_writable
end
add_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Put headers with status `a_status', and headers from `a_headers'
require
status_not_committed: not status_committed
header_not_committed: not header_committed
local
h: HTTP_HEADER
do
if a_headers /= Void then
create h.make_from_array (a_headers)
add_header_text (h.string)
end
ensure
status_set: status_is_set
message_writable: message_writable message_writable: message_writable
end end
put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]]) put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Send headers from `a_lines' -- Put headers from `a_lines'
local require
h: STRING_8 header_not_committed: not header_committed
do do
create h.make (256) across a_lines as c loop
across put_header_line (c.item.name + ": " + c.item.value)
a_lines as c end
loop end
h.append (c.item.name)
h.append_character (':') add_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
h.append_character (' ') -- Add headers from `a_lines'
h.append (c.item.value) require
h.append_character ('%R') header_not_committed: not header_committed
h.append_character ('%N') do
across a_lines as c loop
add_header_line (c.item.name + ": " + c.item.value)
end end
put_header_text (h)
end end
feature -- Output report feature -- Output report
@@ -376,7 +479,7 @@ feature -- Error reporting
end end
note note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Eiffel Software and others" copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software