Removed any "format" related query from router lib, this is too application specific to be there.
Better handling of base_url for REQUEST_ROUTER
This commit is contained in:
@@ -10,7 +10,115 @@ deferred class
|
|||||||
inherit
|
inherit
|
||||||
REQUEST_HANDLER_CONTEXT
|
REQUEST_HANDLER_CONTEXT
|
||||||
|
|
||||||
note
|
feature -- Accept: Content-Type
|
||||||
|
|
||||||
|
accepted_content_type: detachable READABLE_STRING_8
|
||||||
|
do
|
||||||
|
if internal_accepted_content_type = Void then
|
||||||
|
get_accepted_content_type (Void)
|
||||||
|
end
|
||||||
|
Result := internal_accepted_content_type
|
||||||
|
end
|
||||||
|
|
||||||
|
get_accepted_content_type (a_supported_content_types: detachable ARRAY [STRING_8])
|
||||||
|
do
|
||||||
|
if internal_accepted_content_type = Void then
|
||||||
|
internal_accepted_content_type := request_accepted_content_type (a_supported_content_types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Format name
|
||||||
|
|
||||||
|
accepted_format_name: detachable READABLE_STRING_8
|
||||||
|
do
|
||||||
|
if internal_accepted_format_name = Void then
|
||||||
|
get_accepted_format_name (Void, Void)
|
||||||
|
end
|
||||||
|
Result := internal_accepted_format_name
|
||||||
|
end
|
||||||
|
|
||||||
|
get_accepted_format_name (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [STRING_8])
|
||||||
|
do
|
||||||
|
if internal_accepted_format_name = Void then
|
||||||
|
internal_accepted_format_name := request_accepted_format (a_format_variable_name, a_supported_content_types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Format id
|
||||||
|
|
||||||
|
accepted_format_id: INTEGER
|
||||||
|
do
|
||||||
|
if internal_accepted_format_id = 0 then
|
||||||
|
get_accepted_format_id (Void, Void)
|
||||||
|
end
|
||||||
|
Result := internal_accepted_format_id
|
||||||
|
end
|
||||||
|
|
||||||
|
get_accepted_format_id (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [STRING_8])
|
||||||
|
do
|
||||||
|
if internal_accepted_format_id = 0 then
|
||||||
|
internal_accepted_format_id := request_accepted_format_id (a_format_variable_name, a_supported_content_types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
feature {NONE} -- Constants
|
||||||
|
|
||||||
|
Format_constants: HTTP_FORMAT_CONSTANTS
|
||||||
|
once
|
||||||
|
create Result
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Format
|
||||||
|
|
||||||
|
request_accepted_format (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
|
||||||
|
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
|
||||||
|
do
|
||||||
|
if a_format_variable_name /= Void and then attached string_parameter (a_format_variable_name) as ctx_format then
|
||||||
|
Result := ctx_format.as_string_8
|
||||||
|
else
|
||||||
|
Result := request_format_from_content_type (request_accepted_content_type (a_supported_content_types))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
request_accepted_format_id (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [READABLE_STRING_8]): INTEGER
|
||||||
|
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
|
||||||
|
do
|
||||||
|
if attached request_accepted_format (a_format_variable_name, a_supported_content_types) as l_format then
|
||||||
|
Result := Format_constants.format_id (l_format)
|
||||||
|
else
|
||||||
|
Result := 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
feature {NONE} -- Format/Content type implementation
|
||||||
|
|
||||||
|
request_format_from_content_type (a_content_type: detachable READABLE_STRING_8): detachable READABLE_STRING_8
|
||||||
|
-- `a_content_type' converted into a request format name
|
||||||
|
do
|
||||||
|
if a_content_type /= Void then
|
||||||
|
if a_content_type.same_string ({HTTP_MIME_TYPES}.text_json) then
|
||||||
|
Result := {HTTP_FORMAT_CONSTANTS}.json_name
|
||||||
|
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.application_json) then
|
||||||
|
Result := {HTTP_FORMAT_CONSTANTS}.json_name
|
||||||
|
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_xml) then
|
||||||
|
Result := {HTTP_FORMAT_CONSTANTS}.xml_name
|
||||||
|
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_html) then
|
||||||
|
Result := {HTTP_FORMAT_CONSTANTS}.html_name
|
||||||
|
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_plain) then
|
||||||
|
Result := {HTTP_FORMAT_CONSTANTS}.text_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
feature {NONE} -- Internal
|
||||||
|
|
||||||
|
internal_accepted_content_type: like accepted_content_type
|
||||||
|
|
||||||
|
internal_accepted_format_id: like accepted_format_id
|
||||||
|
|
||||||
|
internal_accepted_format_name: like accepted_format_name
|
||||||
|
|
||||||
|
;note
|
||||||
copyright: "Copyright (c) 1984-2012, Eiffel Software and others"
|
copyright: "Copyright (c) 1984-2012, 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: "[
|
||||||
|
|||||||
@@ -13,10 +13,11 @@ inherit
|
|||||||
REST_REQUEST_ROUTER [H, C]
|
REST_REQUEST_ROUTER [H, C]
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
note
|
note
|
||||||
copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
|
copyright: "Copyright (c) 1984-2012, 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
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ inherit
|
|||||||
end
|
end
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
feature -- Status report
|
feature -- Status report
|
||||||
|
|
||||||
@@ -53,7 +54,7 @@ feature {NONE} -- Routing
|
|||||||
router: REST_REQUEST_URI_TEMPLATE_ROUTER_I [H, C]
|
router: REST_REQUEST_URI_TEMPLATE_ROUTER_I [H, C]
|
||||||
|
|
||||||
;note
|
;note
|
||||||
copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
|
copyright: "Copyright (c) 1984-2012, 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
|
||||||
|
|||||||
@@ -66,82 +66,32 @@ feature -- Url Query
|
|||||||
result_attached: Result /= Void
|
result_attached: Result /= Void
|
||||||
end
|
end
|
||||||
|
|
||||||
feature {NONE} -- Constants
|
|
||||||
|
|
||||||
Format_constants: HTTP_FORMAT_CONSTANTS
|
|
||||||
once
|
|
||||||
create Result
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Query
|
feature -- Query
|
||||||
|
|
||||||
request_format (a_format_variable_name: detachable READABLE_STRING_8; content_type_supported: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
|
request_accepted_content_type (a_supported_content_types: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
|
||||||
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
|
-- Accepted content-type for the request, among the supported content types `a_supported_content_types'
|
||||||
do
|
|
||||||
if a_format_variable_name /= Void and then attached string_parameter (a_format_variable_name) as ctx_format then
|
|
||||||
Result := ctx_format.as_string_8
|
|
||||||
else
|
|
||||||
Result := content_type_to_request_format (request_content_type (content_type_supported))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
request_format_id (a_format_variable_name: detachable READABLE_STRING_8; content_type_supported: detachable ARRAY [READABLE_STRING_8]): INTEGER
|
|
||||||
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
|
|
||||||
do
|
|
||||||
if attached request_format (a_format_variable_name, content_type_supported) as l_format then
|
|
||||||
Result := Format_constants.format_id (l_format)
|
|
||||||
else
|
|
||||||
Result := 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
content_type_to_request_format (a_content_type: detachable READABLE_STRING_8): detachable READABLE_STRING_8
|
|
||||||
-- `a_content_type' converted into a request format name
|
|
||||||
do
|
|
||||||
if a_content_type /= Void then
|
|
||||||
if a_content_type.same_string ({HTTP_MIME_TYPES}.text_json) then
|
|
||||||
Result := {HTTP_FORMAT_CONSTANTS}.json_name
|
|
||||||
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.application_json) then
|
|
||||||
Result := {HTTP_FORMAT_CONSTANTS}.json_name
|
|
||||||
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_xml) then
|
|
||||||
Result := {HTTP_FORMAT_CONSTANTS}.xml_name
|
|
||||||
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_html) then
|
|
||||||
Result := {HTTP_FORMAT_CONSTANTS}.html_name
|
|
||||||
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_plain) then
|
|
||||||
Result := {HTTP_FORMAT_CONSTANTS}.text_name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
request_content_type (content_type_supported: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
|
|
||||||
local
|
local
|
||||||
s: detachable READABLE_STRING_8
|
s: detachable READABLE_STRING_8
|
||||||
i,n: INTEGER
|
i,n: INTEGER
|
||||||
l_accept_lst: detachable ARRAYED_LIST [READABLE_STRING_8]
|
|
||||||
do
|
do
|
||||||
l_accept_lst := accepted_content_types (request)
|
if
|
||||||
if attached request.content_type as ct then
|
attached accepted_content_types (request) as l_accept_lst and then
|
||||||
if l_accept_lst /= Void then
|
not l_accept_lst.is_empty
|
||||||
l_accept_lst.put_front (ct)
|
then
|
||||||
else
|
|
||||||
Result := ct
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if Result = Void and l_accept_lst /= Void then
|
|
||||||
from
|
from
|
||||||
l_accept_lst.start
|
l_accept_lst.start
|
||||||
until
|
until
|
||||||
l_accept_lst.after or Result /= Void
|
l_accept_lst.after or Result /= Void
|
||||||
loop
|
loop
|
||||||
s := l_accept_lst.item
|
s := l_accept_lst.item
|
||||||
if content_type_supported /= Void then
|
if a_supported_content_types /= Void then
|
||||||
from
|
from
|
||||||
i := content_type_supported.lower
|
i := a_supported_content_types.lower
|
||||||
n := content_type_supported.upper
|
n := a_supported_content_types.upper
|
||||||
until
|
until
|
||||||
i > n or Result /= Void
|
i > n or Result /= Void
|
||||||
loop
|
loop
|
||||||
if content_type_supported[i].same_string (s) then
|
if a_supported_content_types [i].same_string (s) then
|
||||||
Result := s
|
Result := s
|
||||||
end
|
end
|
||||||
i := i + 1
|
i := i + 1
|
||||||
|
|||||||
@@ -60,10 +60,19 @@ feature -- Base url
|
|||||||
base_url: detachable READABLE_STRING_8
|
base_url: detachable READABLE_STRING_8
|
||||||
-- Common start of any route url
|
-- Common start of any route url
|
||||||
|
|
||||||
|
feature -- Element change
|
||||||
|
|
||||||
set_base_url (a_base_url: like base_url)
|
set_base_url (a_base_url: like base_url)
|
||||||
-- Set `base_url' to `a_base_url'
|
-- Set `base_url' to `a_base_url'
|
||||||
|
-- make sure no map is already added (i.e: count = 0)
|
||||||
|
require
|
||||||
|
no_handler_set: count = 0
|
||||||
do
|
do
|
||||||
base_url := a_base_url
|
if a_base_url = Void or else a_base_url.is_empty then
|
||||||
|
base_url := Void
|
||||||
|
else
|
||||||
|
base_url := a_base_url
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Execution
|
feature -- Execution
|
||||||
@@ -100,6 +109,22 @@ feature -- Execution
|
|||||||
result_void_implie_no_default: Result = Void implies default_handler = Void
|
result_void_implie_no_default: Result = Void implies default_handler = Void
|
||||||
end
|
end
|
||||||
|
|
||||||
|
feature -- status report
|
||||||
|
|
||||||
|
count: INTEGER
|
||||||
|
-- Count of maps handled by current
|
||||||
|
do
|
||||||
|
across
|
||||||
|
Current as curs
|
||||||
|
loop
|
||||||
|
if attached {REQUEST_ROUTING_HANDLER [H, C]} curs.item.handler as rh then
|
||||||
|
Result := Result + rh.count + 1 --| +1 for the handler itself
|
||||||
|
else
|
||||||
|
Result := Result + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Traversing
|
feature -- Traversing
|
||||||
|
|
||||||
new_cursor: ITERATION_CURSOR [TUPLE [handler: H; resource: READABLE_STRING_8; request_methods: detachable ARRAY [READABLE_STRING_8]]]
|
new_cursor: ITERATION_CURSOR [TUPLE [handler: H; resource: READABLE_STRING_8; request_methods: detachable ARRAY [READABLE_STRING_8]]]
|
||||||
|
|||||||
@@ -11,6 +11,30 @@ deferred class
|
|||||||
inherit
|
inherit
|
||||||
REQUEST_HANDLER [C]
|
REQUEST_HANDLER [C]
|
||||||
|
|
||||||
|
feature -- Access
|
||||||
|
|
||||||
|
count: INTEGER
|
||||||
|
-- Count of maps handled by current
|
||||||
|
do
|
||||||
|
Result := router.count
|
||||||
|
end
|
||||||
|
|
||||||
|
base_url: detachable READABLE_STRING_8
|
||||||
|
do
|
||||||
|
Result := router.base_url
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Element change
|
||||||
|
|
||||||
|
set_base_url (a_base_url: like base_url)
|
||||||
|
-- Set `base_url' to `a_base_url'
|
||||||
|
-- make sure no map is already added (i.e: count = 0)
|
||||||
|
require
|
||||||
|
no_handler_set: count = 0
|
||||||
|
do
|
||||||
|
router.set_base_url (a_base_url)
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Execution
|
feature -- Execution
|
||||||
|
|
||||||
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ inherit
|
|||||||
REQUEST_ROUTER [H, C]
|
REQUEST_ROUTER [H, C]
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
feature -- Initialization
|
feature -- Initialization
|
||||||
|
|
||||||
@@ -21,6 +22,14 @@ feature -- Initialization
|
|||||||
handlers.compare_objects
|
handlers.compare_objects
|
||||||
end
|
end
|
||||||
|
|
||||||
|
make_with_base_url (n: INTEGER; a_base_url: like base_url)
|
||||||
|
-- Make router allocated for at least `n' maps,
|
||||||
|
-- and use `a_base_url' as base_url
|
||||||
|
do
|
||||||
|
make (n)
|
||||||
|
set_base_url (a_base_url)
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Registration
|
feature -- Registration
|
||||||
|
|
||||||
map_with_request_methods (p: READABLE_STRING_8; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8])
|
map_with_request_methods (p: READABLE_STRING_8; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8])
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ inherit
|
|||||||
end
|
end
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
feature -- Mapping
|
feature -- Mapping
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ inherit
|
|||||||
REQUEST_ROUTER [H, C]
|
REQUEST_ROUTER [H, C]
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
feature -- Initialization
|
feature -- Initialization
|
||||||
|
|
||||||
@@ -22,6 +23,14 @@ feature -- Initialization
|
|||||||
handlers.compare_objects
|
handlers.compare_objects
|
||||||
end
|
end
|
||||||
|
|
||||||
|
make_with_base_url (n: INTEGER; a_base_url: like base_url)
|
||||||
|
-- Make router allocated for at least `n' maps,
|
||||||
|
-- and use `a_base_url' as base_url
|
||||||
|
do
|
||||||
|
make (n)
|
||||||
|
set_base_url (a_base_url)
|
||||||
|
end
|
||||||
|
|
||||||
feature -- Registration
|
feature -- Registration
|
||||||
|
|
||||||
map_with_uri_template (uri: URI_TEMPLATE; h: H)
|
map_with_uri_template (uri: URI_TEMPLATE; h: H)
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ inherit
|
|||||||
REQUEST_ROUTING_HANDLER [H, C]
|
REQUEST_ROUTING_HANDLER [H, C]
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make,
|
||||||
|
make_with_base_url
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
feature {NONE} -- Initialization
|
||||||
|
|
||||||
@@ -21,6 +22,13 @@ feature {NONE} -- Initialization
|
|||||||
create router.make (n)
|
create router.make (n)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
make_with_base_url (n: INTEGER; a_base_url: like base_url)
|
||||||
|
-- Make allocated for at least `n' maps,
|
||||||
|
-- and use `a_base_url' as base_url
|
||||||
|
do
|
||||||
|
create router.make_with_base_url (n, a_base_url)
|
||||||
|
end
|
||||||
|
|
||||||
feature {NONE} -- Routing
|
feature {NONE} -- Routing
|
||||||
|
|
||||||
router: REQUEST_URI_TEMPLATE_ROUTER_I [H, C]
|
router: REQUEST_URI_TEMPLATE_ROUTER_I [H, C]
|
||||||
|
|||||||
Reference in New Issue
Block a user