Improved redirect_now_custom to allow custom status code, custom header, and custom content
This commit is contained in:
@@ -33,7 +33,8 @@ inherit
|
|||||||
create
|
create
|
||||||
make,
|
make,
|
||||||
make_with_count,
|
make_with_count,
|
||||||
make_from_array
|
make_from_array,
|
||||||
|
make_from_header
|
||||||
|
|
||||||
convert
|
convert
|
||||||
make_from_array ({ARRAY [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]]}),
|
make_from_array ({ARRAY [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]]}),
|
||||||
@@ -54,6 +55,7 @@ feature {NONE} -- Initialization
|
|||||||
end
|
end
|
||||||
|
|
||||||
make_from_array (a_headers: ARRAY [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]])
|
make_from_array (a_headers: ARRAY [TUPLE [key: READABLE_STRING_8; value: READABLE_STRING_8]])
|
||||||
|
-- Create HEADER from array of pair (key,value)
|
||||||
do
|
do
|
||||||
if a_headers.is_empty then
|
if a_headers.is_empty then
|
||||||
make_with_count (0)
|
make_with_count (0)
|
||||||
@@ -67,6 +69,20 @@ feature {NONE} -- Initialization
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
make_from_header (a_header: HTTP_HEADER)
|
||||||
|
-- Create Current from existing HEADER `a_header'
|
||||||
|
local
|
||||||
|
lst: like headers
|
||||||
|
do
|
||||||
|
lst := a_header.headers
|
||||||
|
make_with_count (lst.count)
|
||||||
|
across
|
||||||
|
lst as c
|
||||||
|
loop
|
||||||
|
add_header (c.item.string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Recycle
|
feature -- Recycle
|
||||||
|
|
||||||
recycle
|
recycle
|
||||||
|
|||||||
@@ -234,9 +234,10 @@ feature -- Helper
|
|||||||
|
|
||||||
feature -- Redirect
|
feature -- Redirect
|
||||||
|
|
||||||
redirect_now_with_custom_status_code (a_url: READABLE_STRING_8; a_status_code: INTEGER)
|
redirect_now_custom (a_url: READABLE_STRING_8; a_status_code: INTEGER; a_header: detachable HTTP_HEADER; a_content: detachable TUPLE [body: READABLE_STRING_8; type: READABLE_STRING_8])
|
||||||
-- Redirect to the given url `a_url' and precise custom `a_status_code'
|
-- Redirect to the given url `a_url' and precise custom `a_status_code', custom header and content
|
||||||
-- Please see http://www.faqs.org/rfcs/rfc2616 to use proper status code.
|
-- Please see http://www.faqs.org/rfcs/rfc2616 to use proper status code.
|
||||||
|
-- if `a_status_code' is 0, use the default {HTTP_STATUS_CODE}.moved_permanently
|
||||||
require
|
require
|
||||||
header_not_committed: not header_committed
|
header_not_committed: not header_committed
|
||||||
local
|
local
|
||||||
@@ -246,11 +247,26 @@ feature -- Redirect
|
|||||||
-- This might be a trouble about content-length
|
-- This might be a trouble about content-length
|
||||||
put_string ("Headers already sent.%NCannot redirect, for now please follow this <a %"href=%"" + a_url + "%">link</a> instead%N")
|
put_string ("Headers already sent.%NCannot redirect, for now please follow this <a %"href=%"" + a_url + "%">link</a> instead%N")
|
||||||
else
|
else
|
||||||
create h.make_with_count (1)
|
if a_header /= Void then
|
||||||
|
create h.make_from_header (a_header)
|
||||||
|
else
|
||||||
|
create h.make_with_count (1)
|
||||||
|
end
|
||||||
h.put_location (a_url)
|
h.put_location (a_url)
|
||||||
h.put_content_length (0)
|
if a_status_code = 0 then
|
||||||
set_status_code (a_status_code)
|
set_status_code ({HTTP_STATUS_CODE}.moved_permanently)
|
||||||
put_header_text (h.string)
|
else
|
||||||
|
set_status_code (a_status_code)
|
||||||
|
end
|
||||||
|
if a_content /= Void then
|
||||||
|
h.put_content_length (a_content.body.count)
|
||||||
|
h.put_content_type (a_content.type)
|
||||||
|
put_header_text (h.string)
|
||||||
|
put_string (a_content.body)
|
||||||
|
else
|
||||||
|
h.put_content_length (0)
|
||||||
|
put_header_text (h.string)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -259,26 +275,13 @@ feature -- Redirect
|
|||||||
require
|
require
|
||||||
header_not_committed: not header_committed
|
header_not_committed: not header_committed
|
||||||
do
|
do
|
||||||
redirect_now_with_custom_status_code (a_url, {HTTP_STATUS_CODE}.moved_permanently)
|
redirect_now_custom (a_url, {HTTP_STATUS_CODE}.moved_permanently, Void, Void)
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_now_with_content (a_url: READABLE_STRING_8; a_content: READABLE_STRING_8; a_content_type: READABLE_STRING_8)
|
redirect_now_with_content (a_url: READABLE_STRING_8; a_content: READABLE_STRING_8; a_content_type: READABLE_STRING_8)
|
||||||
-- Redirect to the given url `a_url'
|
-- Redirect to the given url `a_url'
|
||||||
local
|
|
||||||
h: HTTP_HEADER
|
|
||||||
do
|
do
|
||||||
if header_committed then
|
redirect_now_custom (a_url, {HTTP_STATUS_CODE}.moved_permanently, Void, [a_content, a_content_type])
|
||||||
-- This might be a trouble about content-length
|
|
||||||
put_string ("Headers already sent.%NCannot redirect, for now please follow this <a %"href=%"" + a_url + "%">link</a> instead%N")
|
|
||||||
else
|
|
||||||
create h.make_with_count (1)
|
|
||||||
h.put_location (a_url)
|
|
||||||
h.put_content_length (a_content.count)
|
|
||||||
h.put_content_type (a_content_type)
|
|
||||||
set_status_code ({HTTP_STATUS_CODE}.moved_permanently)
|
|
||||||
put_header_text (h.string)
|
|
||||||
put_string (a_content)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
note
|
note
|
||||||
|
|||||||
Reference in New Issue
Block a user