From 4c9e7a43317994cefd13b1986cb7a53561b0f658 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Wed, 5 Oct 2011 17:09:16 +0200 Subject: [PATCH] Added `base_url' for REQUEST_ROUTER (and descendants) Fixed implementation of REST_REQUEST_AGENT_HANDLER to avoid wrong path in inherited routine. Allow to build a URI_TEMPLATE from another URI TEMPLATE, this way, if later we have more attribute (status or settings) to URI_TEMPLATE, we'll be able to change the `template' without breaking the settings --- .../protocol/uri_template/src/uri_template.e | 30 +++++++++++++++-- .../rest/src/rest_request_agent_handler.e | 27 +++------------ .../request/router/src/request_router.e | 21 ++++++++++-- .../router/src/uri/request_uri_router_i.e | 23 ++++++++----- .../request_uri_template_router_i.e | 33 +++++++++++-------- 5 files changed, 85 insertions(+), 49 deletions(-) diff --git a/library/protocol/uri_template/src/uri_template.e b/library/protocol/uri_template/src/uri_template.e index 73bd7255..c8d3b843 100644 --- a/library/protocol/uri_template/src/uri_template.e +++ b/library/protocol/uri_template/src/uri_template.e @@ -23,18 +23,44 @@ inherit create make +create {URI_TEMPLATE} + make_from_uri_template + +convert + make ({READABLE_STRING_8}) + feature {NONE} -- Initialization - make (s: STRING) + make (s: READABLE_STRING_8) do template := s end + make_from_uri_template (a_tpl: like Current) + do + template := a_tpl.template.string + end + feature -- Access - template: STRING + template: READABLE_STRING_8 -- URI string representation + duplicate: like Current + -- Duplicate object from Current + do + create Result.make_from_uri_template (Current) + end + +feature -- Element change + + set_template (t: like template) + -- Set `template' to `t' + do + template := t + reset + end + feature -- Status report debug_output: STRING diff --git a/library/server/request/rest/src/rest_request_agent_handler.e b/library/server/request/rest/src/rest_request_agent_handler.e index 299ea78d..9b98e4fc 100644 --- a/library/server/request/rest/src/rest_request_agent_handler.e +++ b/library/server/request/rest/src/rest_request_agent_handler.e @@ -9,14 +9,15 @@ class inherit REQUEST_AGENT_HANDLER [C] - redefine - execute + rename + execute as execute_application end REST_REQUEST_HANDLER [C] - redefine + select execute end + create make @@ -38,26 +39,6 @@ feature {NONE} -- Implementation internal_authentication_required: BOOLEAN -feature -- Execution - - execute (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER) - do - if - authentication_required (req) and then not authenticated (ctx) - then - execute_unauthorized (ctx, req, res) - else - pre_execute (ctx, req, res) - Precursor {REQUEST_AGENT_HANDLER} (ctx, req, res) - post_execute (ctx, req, res) - end - end - - execute_application (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER) - do - check should_not_occur: False end - end - ;note copyright: "Copyright (c) 1984-2011, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" diff --git a/library/server/request/router/src/request_router.e b/library/server/request/router/src/request_router.e index 5f6ee322..901b3e83 100644 --- a/library/server/request/router/src/request_router.e +++ b/library/server/request/router/src/request_router.e @@ -52,6 +52,17 @@ feature -- Mapping end end +feature -- Base url + + base_url: detachable READABLE_STRING_8 + -- Common start of any route url + + set_base_url (a_base_url: like base_url) + -- Set `base_url' to `a_base_url' + do + base_url := a_base_url + end + feature -- Execution dispatch (req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER): BOOLEAN @@ -97,13 +108,19 @@ feature -- Traversing feature {NONE} -- Access: Implementation + source_uri (req: WGI_REQUEST): READABLE_STRING_32 + -- URI to use to find handler. + do + Result := req.path_info + end + handler (req: WGI_REQUEST): detachable TUPLE [handler: H; context: like default_handler_context] -- Handler whose map matched with `req' require - req_valid: req /= Void and then req.path_info /= Void + req_valid: source_uri (req) /= Void deferred ensure - req_path_info_unchanged: req.path_info.same_string (old req.path_info) + source_uri_unchanged: source_uri (req).same_string (old source_uri (req)) end is_matching_request_methods (a_request_method: READABLE_STRING_GENERAL; rqst_methods: like formatted_request_methods): BOOLEAN diff --git a/library/server/request/router/src/uri/request_uri_router_i.e b/library/server/request/router/src/uri/request_uri_router_i.e index c82b2089..2bea6d90 100644 --- a/library/server/request/router/src/uri/request_uri_router_i.e +++ b/library/server/request/router/src/uri/request_uri_router_i.e @@ -24,9 +24,16 @@ feature -- Initialization feature -- Registration map_with_request_methods (p: READABLE_STRING_8; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8]) + local + l_uri: READABLE_STRING_8 do - handlers.force ([h, p, formatted_request_methods (rqst_methods)]) - h.on_handler_mapped (p, rqst_methods) + if attached base_url as l_base_url then + l_uri := l_base_url + p + else + l_uri := p + end + handlers.force ([h, l_uri, formatted_request_methods (rqst_methods)]) + h.on_handler_mapped (l_uri, rqst_methods) end feature {NONE} -- Access: Implementation @@ -36,9 +43,9 @@ feature {NONE} -- Access: Implementation h: detachable H ctx: detachable like default_handler_context do - h := handler_by_path (req.path_info, req.request_method) + h := handler_by_path (source_uri (req), req.request_method) if h = Void then - if attached smart_handler_by_path (req.path_info, req.request_method) as info then + if attached smart_handler_by_path (source_uri (req), req.request_method) as info then h := info.handler ctx := handler_context (info.path, req) end @@ -57,11 +64,11 @@ feature {NONE} -- Access: Implementation smart_handler (req: WGI_REQUEST): detachable TUPLE [path: READABLE_STRING_8; handler: H] require - req_valid: req /= Void and then req.path_info /= Void + req_valid: req /= Void and then source_uri (req) /= Void do - Result := smart_handler_by_path (req.path_info, req.request_method) + Result := smart_handler_by_path (source_uri (req), req.request_method) ensure - req_path_info_unchanged: req.path_info.same_string (old req.path_info) + req_path_info_unchanged: source_uri (req).same_string (old source_uri (req)) end handler_by_path (a_path: READABLE_STRING_GENERAL; rqst_method: READABLE_STRING_GENERAL): detachable H @@ -124,7 +131,7 @@ feature {NONE} -- Context factory if p /= Void then create ctx.make (req, p) else - create ctx.make (req, req.path_info) + create ctx.make (req, source_uri (req)) end Result := ctx end diff --git a/library/server/request/router/src/uri_template/request_uri_template_router_i.e b/library/server/request/router/src/uri_template/request_uri_template_router_i.e index e43e1935..9c3ec225 100644 --- a/library/server/request/router/src/uri_template/request_uri_template_router_i.e +++ b/library/server/request/router/src/uri_template/request_uri_template_router_i.e @@ -30,18 +30,26 @@ feature -- Registration end map_with_uri_template_and_request_methods (uri: URI_TEMPLATE; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8]) + local + l_tpl: like {URI_TEMPLATE}.template + l_uri: URI_TEMPLATE do - handlers.force ([h, uri.template, formatted_request_methods (rqst_methods)]) - templates.force (uri, uri.template) - h.on_handler_mapped (uri.template, rqst_methods) + l_uri := uri + l_tpl := l_uri.template + if attached base_url as l_base_url then + l_uri := l_uri.duplicate + l_uri.set_template (l_base_url + l_tpl) + l_tpl := l_uri.template + end + + handlers.force ([h, l_tpl, formatted_request_methods (rqst_methods)]) + templates.force (l_uri, l_tpl) + h.on_handler_mapped (l_tpl, rqst_methods) end map_with_request_methods (tpl: READABLE_STRING_8; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8]) - local - uri: URI_TEMPLATE do - create uri.make (tpl) - map_with_uri_template_and_request_methods (uri, h, rqst_methods) + map_with_uri_template_and_request_methods (create {URI_TEMPLATE}.make (tpl), h, rqst_methods) end feature {NONE} -- Access: Implementation @@ -54,7 +62,7 @@ feature {NONE} -- Access: Implementation l_req_method: READABLE_STRING_GENERAL l_res: URI_TEMPLATE_MATCH_RESULT do - p := req.path_info + p := source_uri (req) from l_req_method := req.request_method l_handlers := handlers @@ -70,7 +78,7 @@ feature {NONE} -- Access: Implementation p.starts_with (t) then create l_res.make_empty - l_res.path_variables.force (p.substring (t.count, p.count), "path") + l_res.path_variables.force (p.substring (t.count + 1, p.count), "path") Result := [l_info.handler, handler_context (p, req, create {URI_TEMPLATE}.make (t), l_res)] elseif attached templates.item (t) as tpl and then @@ -91,7 +99,7 @@ feature {NONE} -- Context factory if p /= Void then create Result.make (req, tpl, tpl_res, p) else - create Result.make (req, tpl, tpl_res, req.path_info) + create Result.make (req, tpl, tpl_res, source_uri (req)) end end @@ -144,11 +152,8 @@ feature {NONE} -- Default: implementation end default_handler_context (req: WGI_REQUEST): C - local - tpl: URI_TEMPLATE do - create tpl.make ("/") - Result := handler_context ("/", req, tpl, create {URI_TEMPLATE_MATCH_RESULT}.make_empty) + Result := handler_context (Void, req, create {URI_TEMPLATE}.make ("/"), create {URI_TEMPLATE_MATCH_RESULT}.make_empty) end ;note