Now WGI_RESPONSE.set_status_code (..) has a new argument to pass optional custom reason phrase.

This is a minor breaking change (but prior to the first release, so acceptable)
   And then it is now possible to precise a custom reason phrase (useful for 4xx and 5xx response)

At the WSF_RESPONSE level, the status code is now sent only when the header are sent.
thus, it is possible to change the status code as long as no header is sent.
(in the future, we should also try to delay the sending of headers)

Removed WGI_RESPONSE.put_header_lines (..) which was not used, and WGI is not meant to provide such user friendly features
Now this is available directly on WSF_RESPONSE
This commit is contained in:
Jocelyn Fiat
2012-04-12 11:19:41 +02:00
parent 082def2b70
commit b541efcc8f
14 changed files with 149 additions and 70 deletions

View File

@@ -48,7 +48,7 @@ feature -- Execution
if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then
if res /= Void then
if not res.status_is_set then
res.set_status_code ({HTTP_STATUS_CODE}.internal_server_error)
res.set_status_code ({HTTP_STATUS_CODE}.internal_server_error, Void)
end
if res.message_writable then
res.put_string ("<pre>" + l_trace + "</pre>")

View File

@@ -36,21 +36,24 @@ feature {NONE} -- Initialization
feature -- Status writing
put_status_line (a_code: INTEGER)
-- Put status code line for `a_code'
--| Note this is a default implementation, and could be redefined
--| for instance in relation to NPH CGI script
put_status_line (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- <Precursor>
local
s: STRING
m: detachable READABLE_STRING_8
do
if a_code /= 200 then
create s.make (16)
s.append ("Status:")
s.append_character (' ')
s.append_integer (a_code)
if attached http_status_code_message (a_code) as l_status_message then
m := a_reason_phrase
if m = Void then
m := http_status_code_message (a_code)
end
if m /= Void then
s.append_character (' ')
s.append_string (l_status_message)
s.append_string (m)
end
put_header_line (s)
end

View File

@@ -69,7 +69,7 @@ feature -- Execution
if attached (create {EXCEPTION_MANAGER}).last_exception as e and then attached e.exception_trace as l_trace then
if res /= Void then
if not res.status_is_set then
res.set_status_code ({HTTP_STATUS_CODE}.internal_server_error)
res.set_status_code ({HTTP_STATUS_CODE}.internal_server_error, Void)
end
if res.message_writable then
res.put_string ("<pre>" + l_trace + "</pre>")

View File

@@ -40,23 +40,24 @@ feature -- Status report
feature -- Status writing
put_status_line (a_code: INTEGER)
-- Put status code line for `a_code'
--| Note this is a default implementation, and could be redefined
--| for instance in relation to NPH CGI script
put_status_line (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- <Precursor>
local
s: STRING
m: detachable READABLE_STRING_8
do
--| Do not send any Status line back to the FastCGI client
--| According to http://www.fastcgi.com/docs/faq.html#httpstatus
if a_code /= 200 then
create s.make (16)
s.append ("Status:")
s.append_character (' ')
s.append_integer (a_code)
if attached http_status_code_message (a_code) as l_status_message then
m := a_reason_phrase
if m = Void then
m := http_status_code_message (a_code)
end
if m /= Void then
s.append_character (' ')
s.append_string (l_status_message)
s.append_string (m)
end
put_header_line (s)
end

View File

@@ -37,20 +37,23 @@ feature {WGI_NINO_CONNECTOR, WGI_SERVICE} -- Nino
feature -- Status writing
put_status_line (a_code: INTEGER)
-- Put status code line for `a_code'
--| Note this is a default implementation, and could be redefined
--| for instance in relation to NPH CGI script
put_status_line (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- <Precursor>
local
s: STRING
m: detachable READABLE_STRING_8
do
create s.make (16)
s.append ({HTTP_CONSTANTS}.http_version_1_1)
s.append_character (' ')
s.append_integer (a_code)
if attached http_status_code_message (a_code) as l_status_message then
m := a_reason_phrase
if m = Void then
m := http_status_code_message (a_code)
end
if m /= Void then
s.append_character (' ')
s.append_string (l_status_message)
s.append_string (m)
end
put_header_line (s)
end

View File

@@ -49,15 +49,17 @@ feature -- Status setting
deferred
end
set_status_code (a_code: INTEGER)
-- Set response status code
set_status_code (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- Set response status code with custom `a_reason_phrase' if precised
-- Should be done before sending any data back to the client
require
a_code_positive: a_code > 0
status_not_set: not status_committed
header_not_committed: not header_committed
deferred
ensure
status_code_set: status_code = a_code
status_reason_phrase_set: status_reason_phrase = a_reason_phrase
status_set: status_is_set
end
@@ -66,6 +68,13 @@ feature -- Status setting
deferred
end
status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase for the Response (optional)
deferred
ensure
Result /= Void implies status_is_set
end
feature -- Header output operation
put_header_text (a_text: READABLE_STRING_8)
@@ -79,17 +88,7 @@ feature -- Header output operation
deferred
ensure
status_set: status_is_set
header_committed: header_committed
message_writable: message_writable
end
put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
require
status_set: status_is_set
header_not_committed: not header_committed
deferred
ensure
status_set: status_is_set
status_committed: status_committed
header_committed: header_committed
message_writable: message_writable
end

View File

@@ -62,10 +62,13 @@ feature -- Specific output
feature -- Status writing
put_status_line (a_code: INTEGER)
put_status_line (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- Put status code line for `a_code'
-- with custom `a_reason_phrase' if precised
--| Note this is a default implementation, and could be redefined
--| for instance in relation to NPH CGI script
require
a_code_positive: a_code > 0
deferred
end

View File

@@ -54,21 +54,25 @@ feature -- Status setting
status_is_set: BOOLEAN
-- Is status set?
do
Result := status_code /= 0
Result := status_code > 0
end
set_status_code (a_code: INTEGER)
set_status_code (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- Set response status code
-- Should be done before sending any data back to the client
do
status_code := a_code
output.put_status_line (a_code)
status_reason_phrase := a_reason_phrase
output.put_status_line (a_code, a_reason_phrase)
status_committed := True
end
status_code: INTEGER
-- Response status
status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase for the Response (optional)
feature -- Header output operation
put_header_text (a_text: READABLE_STRING_8)
@@ -78,24 +82,6 @@ feature -- Header output operation
header_committed := True
end
put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
local
h: STRING_8
do
create h.make (256)
across
a_lines as c
loop
h.append (c.item.name)
h.append_character (':')
h.append_character (' ')
h.append (c.item.value)
h.append_character ('%R')
h.append_character ('%N')
end
put_header_text (h)
end
feature -- Output operation
put_character (c: CHARACTER_8)

View File

@@ -56,35 +56,57 @@ feature -- Status setting
status_is_set: BOOLEAN
-- Is status set?
do
Result := wgi_response.status_is_set
Result := status_code > 0
end
set_status_code (a_code: INTEGER)
-- Set response status code
-- Should be done before sending any data back to the client
--| note: the status is really sent when the header are set
require
a_code_valid: a_code > 0
status_not_set: not status_committed
header_not_committed: not header_committed
do
wgi_response.set_status_code (a_code)
status_code := a_code
status_reason_phrase := Void
ensure
status_code_set: status_code = a_code
status_set: status_is_set
end
set_status_code_with_reason_phrase (a_code: INTEGER; a_reason_phrase: READABLE_STRING_8)
-- Set response status code
-- Should be done before sending any data back to the client
--| note: the status is really sent when the header are set
require
a_code_valid: a_code > 0
status_not_set: not status_committed
header_not_committed: not header_committed
do
set_status_code (a_code)
status_reason_phrase := a_reason_phrase
ensure
status_code_set: status_code = a_code
status_reason_phrase_set: status_reason_phrase = a_reason_phrase
status_set: status_is_set
end
status_code: INTEGER
-- Response status
do
Result := wgi_response.status_code
end
status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase (optional)
feature -- Header output operation
put_header_text (a_headers: READABLE_STRING_8)
-- Sent `a_headers' and just before send the status code
require
status_set: status_is_set
header_not_committed: not header_committed
do
wgi_response.set_status_code (status_code, status_reason_phrase)
wgi_response.put_header_text (a_headers)
ensure
status_set: status_is_set
@@ -93,7 +115,7 @@ feature -- Header output operation
message_writable: message_writable
end
put_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [key: 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'
require
status_not_committed: not status_committed
@@ -107,14 +129,28 @@ feature -- Header output operation
put_header_text (h.string)
end
ensure
header_committed: header_committed
status_set: status_is_set
header_committed: header_committed
message_writable: message_writable
end
put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Send headers from `a_lines'
local
h: STRING_8
do
wgi_response.put_header_lines (a_lines)
create h.make (256)
across
a_lines as c
loop
h.append (c.item.name)
h.append_character (':')
h.append_character (' ')
h.append (c.item.value)
h.append_character ('%R')
h.append_character ('%N')
end
put_header_text (h)
end
feature -- Output report