Added WSF_CUSTOM_HEADER_FILTER which provide a convenient way to add a custom header from a filter.
Added to wsf_extension WSF_DEBUG_FILTER and WSF_DEBUG_HANDLER that could be convenient to test specific requests Restructured wsf_extension
This commit is contained in:
174
library/server/wsf/extension/handler/wsf_debug_handler.e
Normal file
174
library/server/wsf/extension/handler/wsf_debug_handler.e
Normal file
@@ -0,0 +1,174 @@
|
||||
note
|
||||
description: "Summary description for {WSF_DEBUG_HANDLER}."
|
||||
author: ""
|
||||
date: "$Date: 2013-06-28 16:14:02 +0200 (ven., 28 juin 2013) $"
|
||||
revision: "$Revision: 92754 $"
|
||||
|
||||
class
|
||||
WSF_DEBUG_HANDLER
|
||||
|
||||
inherit
|
||||
WSF_STARTS_WITH_HANDLER
|
||||
rename
|
||||
execute as execute_starts_with
|
||||
end
|
||||
|
||||
WSF_SELF_DOCUMENTED_HANDLER
|
||||
|
||||
SHARED_HTML_ENCODER
|
||||
|
||||
SHARED_WSF_PERCENT_ENCODER
|
||||
rename
|
||||
percent_encoder as url_encoder
|
||||
export
|
||||
{NONE} all
|
||||
end
|
||||
|
||||
SHARED_EXECUTION_ENVIRONMENT
|
||||
export
|
||||
{NONE} all
|
||||
end
|
||||
|
||||
create
|
||||
make,
|
||||
make_hidden
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
do
|
||||
end
|
||||
|
||||
make_hidden
|
||||
do
|
||||
make
|
||||
is_hidden := True
|
||||
end
|
||||
|
||||
is_hidden: BOOLEAN
|
||||
-- Current mapped handler should be hidden from self documentation
|
||||
|
||||
feature -- Documentation
|
||||
|
||||
mapping_documentation (m: WSF_ROUTER_MAPPING; a_request_methods: detachable WSF_REQUEST_METHODS): WSF_ROUTER_MAPPING_DOCUMENTATION
|
||||
-- <Precursor>
|
||||
do
|
||||
create Result.make (m)
|
||||
Result.set_is_hidden (is_hidden)
|
||||
Result.add_description ("Debug handler (mainly to return request information)")
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
execute_starts_with (a_path: READABLE_STRING_8; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
local
|
||||
s: STRING_8
|
||||
p: WSF_PAGE_RESPONSE
|
||||
v: STRING_8
|
||||
do
|
||||
if (create {RT_DEBUGGER}).rt_workbench_wait_for_debugger (1050) then
|
||||
end
|
||||
create s.make (2048)
|
||||
s.append ("**DEBUG**%N")
|
||||
req.set_raw_input_data_recorded (True)
|
||||
|
||||
append_iterable_to ("Meta variables:", req.meta_variables, s)
|
||||
s.append_character ('%N')
|
||||
|
||||
append_iterable_to ("Path parameters", req.path_parameters, s)
|
||||
s.append_character ('%N')
|
||||
|
||||
append_iterable_to ("Query parameters", req.query_parameters, s)
|
||||
s.append_character ('%N')
|
||||
|
||||
append_iterable_to ("Form parameters", req.form_parameters, s)
|
||||
s.append_character ('%N')
|
||||
|
||||
if attached req.content_type as l_type then
|
||||
s.append ("Content: type=" + l_type.debug_output)
|
||||
s.append (" length=")
|
||||
s.append_natural_64 (req.content_length_value)
|
||||
s.append_character ('%N')
|
||||
create v.make (req.content_length_value.to_integer_32)
|
||||
req.read_input_data_into (v)
|
||||
across
|
||||
v.split ('%N') as v_cursor
|
||||
loop
|
||||
s.append (" |")
|
||||
s.append (v_cursor.item)
|
||||
s.append_character ('%N')
|
||||
end
|
||||
end
|
||||
|
||||
create p.make_with_body (s)
|
||||
p.header.put_content_type_text_plain
|
||||
res.send (p)
|
||||
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
append_iterable_to (a_title: READABLE_STRING_8; it: detachable ITERABLE [WSF_VALUE]; s: STRING_8)
|
||||
local
|
||||
n: INTEGER
|
||||
t: READABLE_STRING_8
|
||||
v: READABLE_STRING_8
|
||||
do
|
||||
s.append (a_title)
|
||||
s.append_character (':')
|
||||
if it /= Void then
|
||||
across it as c loop
|
||||
n := n + 1
|
||||
end
|
||||
if n = 0 then
|
||||
s.append (" empty")
|
||||
s.append_character ('%N')
|
||||
else
|
||||
s.append_character ('%N')
|
||||
across
|
||||
it as c
|
||||
loop
|
||||
s.append (" - ")
|
||||
s.append (c.item.url_encoded_name)
|
||||
t := c.item.generating_type
|
||||
if t.same_string ("WSF_STRING") then
|
||||
else
|
||||
s.append_character (' ')
|
||||
s.append_character ('{')
|
||||
s.append (t)
|
||||
s.append_character ('}')
|
||||
end
|
||||
s.append_character ('=')
|
||||
v := c.item.string_representation.as_string_8
|
||||
if v.has ('%N') then
|
||||
s.append_character ('%N')
|
||||
across
|
||||
v.split ('%N') as v_cursor
|
||||
loop
|
||||
s.append (" |")
|
||||
s.append (v_cursor.item)
|
||||
s.append_character ('%N')
|
||||
end
|
||||
else
|
||||
s.append (v)
|
||||
s.append_character ('%N')
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
s.append (" none")
|
||||
s.append_character ('%N')
|
||||
end
|
||||
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
|
||||
96
library/server/wsf/extension/handler/wsf_handler_helper.e
Normal file
96
library/server/wsf/extension/handler/wsf_handler_helper.e
Normal file
@@ -0,0 +1,96 @@
|
||||
note
|
||||
description: "[
|
||||
Provides a few helpful feature to respond predefined message to the client
|
||||
]"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
WSF_HANDLER_HELPER
|
||||
|
||||
inherit
|
||||
ANY
|
||||
|
||||
feature -- Helper
|
||||
|
||||
execute_content_type_not_allowed (req: WSF_REQUEST; res: WSF_RESPONSE; a_content_types: detachable ARRAY [STRING]; a_uri_formats: detachable ARRAY [STRING])
|
||||
local
|
||||
accept_s, uri_s: detachable STRING
|
||||
i, n: INTEGER
|
||||
do
|
||||
if a_content_types /= Void then
|
||||
create accept_s.make (10)
|
||||
from
|
||||
i := a_content_types.lower
|
||||
n := a_content_types.upper
|
||||
until
|
||||
i > n
|
||||
loop
|
||||
accept_s.append_string (a_content_types[i])
|
||||
if i < n then
|
||||
accept_s.append_character (',')
|
||||
accept_s.append_character (' ')
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
else
|
||||
accept_s := "*/*"
|
||||
end
|
||||
|
||||
if a_uri_formats /= Void then
|
||||
create uri_s.make (10)
|
||||
from
|
||||
i := a_uri_formats.lower
|
||||
n := a_uri_formats.upper
|
||||
until
|
||||
i > n
|
||||
loop
|
||||
uri_s.append_string (a_uri_formats[i])
|
||||
if i < n then
|
||||
uri_s.append_character (',')
|
||||
uri_s.append_character (' ')
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
res.put_header ({HTTP_STATUS_CODE}.unsupported_media_type, << ["Content-Type", "text/plain"], ["Accept", accept_s]>>)
|
||||
if accept_s /= Void then
|
||||
res.put_string ("Unsupported request content-type, Accept: " + accept_s + "%N")
|
||||
end
|
||||
if uri_s /= Void then
|
||||
res.put_string ("Unsupported request format from the URI: " + uri_s + "%N")
|
||||
end
|
||||
end
|
||||
|
||||
execute_request_method_not_allowed (req: WSF_REQUEST; res: WSF_RESPONSE; a_methods: ITERABLE [STRING])
|
||||
local
|
||||
s: STRING
|
||||
do
|
||||
create s.make (25)
|
||||
across
|
||||
a_methods as c
|
||||
loop
|
||||
if not s.is_empty then
|
||||
s.append_character (',')
|
||||
s.append_character (' ')
|
||||
end
|
||||
s.append_string (c.item)
|
||||
end
|
||||
res.put_header ({HTTP_STATUS_CODE}.method_not_allowed, <<
|
||||
["Content-Type", {HTTP_MIME_TYPES}.text_plain],
|
||||
["Allow", s]
|
||||
>>)
|
||||
res.put_string ("Unsupported request method, Allow: " + s + "%N")
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, 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
|
||||
@@ -0,0 +1,44 @@
|
||||
note
|
||||
description: "[
|
||||
Summary description for WSF_HANDLER_ROUTES_RECORDER.
|
||||
|
||||
You can inherit from this class from any WSF_HANDLER and redefine `on_handler_mapped'
|
||||
to record the available routes if your handler needs it.
|
||||
]"
|
||||
author: ""
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class
|
||||
WSF_HANDLER_ROUTES_RECORDER
|
||||
|
||||
feature {WSF_HANDLER} -- Routes access
|
||||
|
||||
available_routes: detachable LIST [TUPLE [resource: READABLE_STRING_8; rqst_methods: detachable ARRAY [READABLE_STRING_8]]]
|
||||
-- Available routes
|
||||
|
||||
feature {WSF_ROUTER} -- Routes change
|
||||
|
||||
on_handler_mapped (a_resource: READABLE_STRING_8; a_rqst_methods: detachable ARRAY [READABLE_STRING_8])
|
||||
local
|
||||
l_routes: like available_routes
|
||||
do
|
||||
l_routes := available_routes
|
||||
if l_routes = Void then
|
||||
create {ARRAYED_LIST [like available_routes.item]} l_routes.make (3)
|
||||
available_routes := l_routes
|
||||
end
|
||||
l_routes.force ([a_resource, a_rqst_methods])
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, 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
|
||||
72
library/server/wsf/extension/handler/wsf_method_handler.e
Normal file
72
library/server/wsf/extension/handler/wsf_method_handler.e
Normal file
@@ -0,0 +1,72 @@
|
||||
note
|
||||
|
||||
description: "Conforming handler for any HTTP 1.1 standard method"
|
||||
|
||||
author: "Colin Adams"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class WSF_METHOD_HANDLER
|
||||
|
||||
feature -- Method
|
||||
|
||||
do_method (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Respond to `req' using `res'.
|
||||
require
|
||||
req_not_void: req /= Void
|
||||
res_not_void: res /= Void
|
||||
deferred
|
||||
ensure
|
||||
valid_response_for_http_1_0: is_1_0 (req.server_protocol) implies
|
||||
valid_response_for_http_1_0 (res.status_code)
|
||||
empty_body_for_no_content_response: is_no_content_response (res.status_code) implies is_empty_content (res)
|
||||
end
|
||||
|
||||
feature -- Contract support
|
||||
|
||||
is_1_0 (a_protocol: READABLE_STRING_8): BOOLEAN
|
||||
-- Is `a_protocol' (a variant of) HTTP 1.0?
|
||||
require
|
||||
a_protocol_not_void: a_protocol /= Void
|
||||
do
|
||||
Result := a_protocol.count >= 8 and then
|
||||
a_protocol.substring (1, 8) ~ "HTTP/1.0"
|
||||
end
|
||||
|
||||
valid_response_for_http_1_0 (a_status_code: INTEGER): BOOLEAN
|
||||
-- Is `a_status_code' a valid response to HTTP 1.0?
|
||||
do
|
||||
-- 1XX is forbidden
|
||||
|
||||
-- first approximation
|
||||
Result := a_status_code >= {HTTP_STATUS_CODE}.ok
|
||||
end
|
||||
|
||||
is_no_content_response (a_status_code: INTEGER): BOOLEAN
|
||||
-- Is `a_status_code' one that does not permit an entity in the response?
|
||||
do
|
||||
inspect
|
||||
a_status_code
|
||||
when {HTTP_STATUS_CODE}.no_content then
|
||||
Result := True
|
||||
when {HTTP_STATUS_CODE}.reset_content then
|
||||
Result := True
|
||||
when {HTTP_STATUS_CODE}.not_modified then
|
||||
Result := True
|
||||
when {HTTP_STATUS_CODE}.conflict then
|
||||
Result := True
|
||||
else
|
||||
-- default to False
|
||||
end
|
||||
end
|
||||
|
||||
is_empty_content (res: WSF_RESPONSE): BOOLEAN
|
||||
-- Does `res' not contain an entity?
|
||||
require
|
||||
res_not_void: res /= Void
|
||||
do
|
||||
Result := res.transfered_content_length = 0 -- Is that the right measure?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
83
library/server/wsf/extension/handler/wsf_method_handlers.e
Normal file
83
library/server/wsf/extension/handler/wsf_method_handlers.e
Normal file
@@ -0,0 +1,83 @@
|
||||
note
|
||||
|
||||
description: "Conforming handlers for HTTP 1.1 standard methods"
|
||||
|
||||
author: "Colin Adams"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class WSF_METHOD_HANDLERS
|
||||
|
||||
inherit
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_get
|
||||
select
|
||||
do_get
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_put
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_post
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_delete
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_connect
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_head
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_options
|
||||
end
|
||||
|
||||
WSF_METHOD_HANDLER
|
||||
rename
|
||||
do_method as do_trace
|
||||
end
|
||||
|
||||
feature -- Method
|
||||
|
||||
do_head (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Respond to `req' using `res'.
|
||||
deferred
|
||||
ensure then
|
||||
empty_body: is_empty_content (res)
|
||||
end
|
||||
|
||||
do_post (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Respond to `req' using `res'.
|
||||
deferred
|
||||
ensure then
|
||||
non_empty_body: res.status_code = {HTTP_STATUS_CODE}.created implies
|
||||
not is_empty_content (res)
|
||||
location_header: res.status_code = {HTTP_STATUS_CODE}.created implies True -- WSF_RESPONSE needs enhancing
|
||||
end
|
||||
|
||||
do_trace (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Respond to `req' using `res'.
|
||||
deferred
|
||||
ensure then
|
||||
non_empty_body: res.status_code = {HTTP_STATUS_CODE}.ok implies
|
||||
not is_empty_content (res)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,276 @@
|
||||
note
|
||||
description: "Work in progress Common abstraction to handle RESTfull methods."
|
||||
author: "Olivier Ligot"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class
|
||||
WSF_RESOURCE_CONTEXT_HANDLER_HELPER [C -> WSF_HANDLER_CONTEXT create make end]
|
||||
|
||||
feature -- Basic operations
|
||||
|
||||
execute_methods (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute request and dispatch according to the request method
|
||||
local
|
||||
m: READABLE_STRING_8
|
||||
do
|
||||
m := req.request_method.as_upper
|
||||
if m.same_string ({HTTP_REQUEST_METHODS}.method_get) then
|
||||
execute_get (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_put) then
|
||||
execute_put (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_delete) then
|
||||
execute_delete (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_post) then
|
||||
execute_post (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_trace) then
|
||||
execute_trace (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_options) then
|
||||
execute_options (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_head) then
|
||||
execute_head (ctx, req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_connect) then
|
||||
execute_connect (ctx, req, res)
|
||||
else
|
||||
--| Eventually handle other methods...
|
||||
execute_extension_method (ctx, req, res)
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Method Get
|
||||
|
||||
execute_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_get (ctx, req, res)
|
||||
end
|
||||
|
||||
do_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Using GET to retrieve resource information.
|
||||
-- If the GET request is SUCCESS, we response with
|
||||
-- 200 OK, and a representation of the person
|
||||
-- If the GET request is not SUCCESS, we response with
|
||||
-- 404 Resource not found
|
||||
-- If is a Condition GET and the resource does not change we send a
|
||||
-- 304, Resource not modifed
|
||||
do
|
||||
handle_not_implemented ("Method GET not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method Post
|
||||
|
||||
execute_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
if req.is_chunked_input then
|
||||
do_post (ctx, req, res)
|
||||
else
|
||||
if req.content_length_value > 0 then
|
||||
do_post (ctx, req, res)
|
||||
else
|
||||
handle_bad_request_response ("Bad request, content_length empty", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here the convention is the following.
|
||||
-- POST is used for creation and the server determines the URI
|
||||
-- of the created resource.
|
||||
-- If the request post is SUCCESS, the server will create the order and will response with
|
||||
-- HTTP_RESPONSE 201 CREATED, the Location header will contains the newly created order's URI
|
||||
-- if the request post is not SUCCESS, the server will response with
|
||||
-- HTTP_RESPONSE 400 BAD REQUEST, the client send a bad request
|
||||
-- HTTP_RESPONSE 500 INTERNAL_SERVER_ERROR, when the server can deliver the request
|
||||
do
|
||||
handle_not_implemented ("Method POST not implemented", req, res)
|
||||
end
|
||||
|
||||
feature-- Method Put
|
||||
|
||||
execute_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
if req.is_chunked_input then
|
||||
do_put (ctx, req, res)
|
||||
else
|
||||
if req.content_length_value > 0 then
|
||||
do_put (ctx, req, res)
|
||||
else
|
||||
handle_bad_request_response ("Bad request, content_length empty", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method PUT not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method DELETE
|
||||
|
||||
execute_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_delete (ctx, req, res)
|
||||
end
|
||||
|
||||
do_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here we use DELETE to logically delete a person.
|
||||
-- 204 if is ok
|
||||
-- 404 Resource not found
|
||||
-- 500 if we have an internal server error
|
||||
do
|
||||
handle_not_implemented ("Method DELETE not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method CONNECT
|
||||
|
||||
execute_connect (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_connect (ctx, req, res)
|
||||
end
|
||||
|
||||
do_connect (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method CONNECT not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method HEAD
|
||||
|
||||
execute_head (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_head (ctx, req, res)
|
||||
end
|
||||
|
||||
do_head (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Using HEAD to retrieve resource information.
|
||||
-- If the HEAD request is SUCCESS, we response with
|
||||
-- 200 OK, and WITHOUT a representation of the person
|
||||
-- If the HEAD request is not SUCCESS, we response with
|
||||
-- 404 Resource not found
|
||||
-- If is a Condition HEAD and the resource does not change we send a
|
||||
-- 304, Resource not modifed
|
||||
do
|
||||
handle_not_implemented ("Method HEAD not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method OPTIONS
|
||||
|
||||
execute_options (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_options (ctx, req, res)
|
||||
end
|
||||
|
||||
do_options (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Using OPTIONS to retrieve resource information.
|
||||
-- If the OPTIONS request is SUCCESS, we response with 200 OK
|
||||
do
|
||||
handle_not_implemented ("Method OPTIONS not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method TRACE
|
||||
|
||||
execute_trace (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_trace (ctx, req, res)
|
||||
end
|
||||
|
||||
do_trace (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method TRACE not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method Extension Method
|
||||
|
||||
execute_extension_method (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
do_extension_method (ctx, req, res)
|
||||
end
|
||||
|
||||
do_extension_method (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method extension-method not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Handle responses
|
||||
-- TODO Handle Content negotiation.
|
||||
-- The option : Server-driven negotiation: uses request headers to select a variant
|
||||
-- More info : http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html#sec12
|
||||
|
||||
-- supported_content_types: detachable ARRAY [READABLE_STRING_8]
|
||||
-- -- Supported content types
|
||||
-- -- Can be redefined
|
||||
-- do
|
||||
-- Result := Void
|
||||
-- end
|
||||
|
||||
handle_error (a_description: STRING; a_status_code: INTEGER; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle an error.
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
do
|
||||
create h.make
|
||||
h.put_content_type_text_plain
|
||||
h.put_content_length (a_description.count)
|
||||
h.put_current_date
|
||||
res.set_status_code (a_status_code)
|
||||
res.put_header_text (h.string)
|
||||
res.put_string (a_description)
|
||||
end
|
||||
|
||||
handle_not_implemented (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_implemented, req, res)
|
||||
end
|
||||
|
||||
handle_bad_request_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.bad_request, req, res)
|
||||
end
|
||||
|
||||
handle_resource_not_found_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_found, req, res)
|
||||
end
|
||||
|
||||
handle_forbidden (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle forbidden.
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.forbidden, req, res)
|
||||
end
|
||||
|
||||
feature -- Handle responses: others
|
||||
|
||||
handle_precondition_fail_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE )
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.precondition_failed, req, res)
|
||||
end
|
||||
|
||||
handle_internal_server_error (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE )
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.internal_server_error, req, res)
|
||||
end
|
||||
|
||||
handle_method_not_allowed_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.method_not_allowed, req, res)
|
||||
end
|
||||
|
||||
handle_resource_not_modified_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_modified, req, res)
|
||||
end
|
||||
|
||||
handle_resource_conflict_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.conflict, req, res)
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2012, 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
|
||||
@@ -0,0 +1,386 @@
|
||||
note
|
||||
description: "Work in progress Common abstraction to handle RESTfull methods"
|
||||
author: ""
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class WSF_RESOURCE_HANDLER_HELPER
|
||||
|
||||
inherit
|
||||
|
||||
WSF_METHOD_HANDLERS
|
||||
|
||||
feature -- Execute template
|
||||
|
||||
execute_methods (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute request and dispatch according to the request method.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
local
|
||||
m: READABLE_STRING_8
|
||||
do
|
||||
m := req.request_method.as_upper
|
||||
if m.same_string ({HTTP_REQUEST_METHODS}.method_get) then
|
||||
execute_get (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_put) then
|
||||
execute_put (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_delete) then
|
||||
execute_delete (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_post) then
|
||||
execute_post (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_trace) then
|
||||
execute_trace (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_options) then
|
||||
execute_options (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_head) then
|
||||
execute_head (req, res)
|
||||
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_connect) then
|
||||
execute_connect (req, res)
|
||||
else
|
||||
--| Eventually handle other methods...
|
||||
execute_extension_method (req, res)
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Method Get
|
||||
|
||||
execute_get (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
get_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_get)
|
||||
do
|
||||
do_get (req, res)
|
||||
end
|
||||
|
||||
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Using GET to retrieve resource information.
|
||||
-- If the GET request is SUCCESS, we respond with
|
||||
-- 200 OK, and a representation of the resource.
|
||||
-- If the GET request is not SUCCESS, we response with
|
||||
-- 404 Resource not found.
|
||||
-- If is a Condition GET and the resource does not change we send a
|
||||
-- 304, Resource not modifed.
|
||||
do
|
||||
handle_not_implemented ("Method GET not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method Post
|
||||
|
||||
execute_post (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
post_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_post)
|
||||
do
|
||||
if req.is_chunked_input then
|
||||
do_post (req, res)
|
||||
else
|
||||
if req.content_length_value > 0 then
|
||||
do_post (req, res)
|
||||
else
|
||||
handle_bad_request_response ("Bad request, content_length empty", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do_post (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here the convention is the following.
|
||||
-- POST is used for creation and the server determines the URI
|
||||
-- of the created resource.
|
||||
-- If the request post is SUCCESS, the server will create the order and will response with
|
||||
-- HTTP_RESPONSE 201 CREATED, the Location header will contains the newly created order's URI
|
||||
-- if the request post is not SUCCESS, the server will response with
|
||||
-- HTTP_RESPONSE 400 BAD REQUEST, the client send a bad request
|
||||
-- HTTP_RESPONSE 500 INTERNAL_SERVER_ERROR, when the server can deliver the request
|
||||
do
|
||||
handle_not_implemented ("Method POST not implemented", req, res)
|
||||
end
|
||||
|
||||
feature-- Method Put
|
||||
|
||||
execute_put (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
put_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_put)
|
||||
do
|
||||
if req.is_chunked_input then
|
||||
do_put (req, res)
|
||||
else
|
||||
if req.content_length_value > 0 then
|
||||
do_put (req, res)
|
||||
else
|
||||
handle_bad_request_response ("Bad request, content_length empty", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do_put (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method PUT not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method DELETE
|
||||
|
||||
execute_delete (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
delete_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_delete)
|
||||
do
|
||||
do_delete (req, res)
|
||||
end
|
||||
|
||||
do_delete (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here we use DELETE to logically delete a person.
|
||||
-- 204 if is ok
|
||||
-- 404 Resource not found
|
||||
-- 500 if we have an internal server error
|
||||
do
|
||||
handle_not_implemented ("Method DELETE not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method CONNECT
|
||||
|
||||
execute_connect (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
connect_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_connect)
|
||||
do
|
||||
do_connect (req, res)
|
||||
end
|
||||
|
||||
do_connect (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
handle_not_implemented ("Method CONNECT not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method HEAD
|
||||
|
||||
execute_head (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
head_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_head)
|
||||
do
|
||||
do_head (req, res)
|
||||
end
|
||||
|
||||
do_head (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Using HEAD to retrieve resource information.
|
||||
-- If the HEAD request is SUCCESS, we respond with
|
||||
-- 200 OK, and WITHOUT a representation of the resource.
|
||||
-- If the HEAD request is not SUCCESS, we respond with
|
||||
-- 404 Resource not found.
|
||||
-- If Conditional HEAD and the resource does not change we send a
|
||||
-- 304, Resource not modifed.
|
||||
do
|
||||
handle_not_implemented ("Method HEAD not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method OPTIONS
|
||||
|
||||
execute_options (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
options_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_options)
|
||||
do
|
||||
do_options (req, res)
|
||||
end
|
||||
|
||||
do_options (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
-- TODO - implement a default method that lists the accepted methods for the resource.
|
||||
handle_not_implemented ("Method OPTIONS not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method TRACE
|
||||
|
||||
execute_trace (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
trace_method: req.is_request_method ({HTTP_REQUEST_METHODS}.method_trace)
|
||||
do
|
||||
do_trace (req, res)
|
||||
end
|
||||
|
||||
do_trace (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do
|
||||
-- TODO - implement frozen, as there is only one permitted semantic.
|
||||
handle_not_implemented ("Method TRACE not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Method Extension Method
|
||||
|
||||
execute_extension_method (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
do_extension_method (req, res)
|
||||
end
|
||||
|
||||
do_extension_method (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute `req' responding into `res'.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_not_implemented ("Method extension-method not implemented", req, res)
|
||||
end
|
||||
|
||||
feature -- Retrieve content from WGI_INPUT_STREAM
|
||||
|
||||
retrieve_data (req: WSF_REQUEST): STRING
|
||||
-- Retrieve the content from the input stream.
|
||||
-- Handle different transfers.
|
||||
require
|
||||
req_attached: req /= Void
|
||||
do
|
||||
create Result.make_empty
|
||||
req.read_input_data_into (Result)
|
||||
end
|
||||
|
||||
feature -- Handle responses
|
||||
-- TODO Handle Content negotiation.
|
||||
-- The option : Server-driven negotiation: uses request headers to select a variant
|
||||
-- More info : http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html#sec12
|
||||
|
||||
-- TODO: review HTTP requirements on `a_description' for each individual error code.
|
||||
|
||||
handle_error (a_description: STRING; a_status_code: INTEGER; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle an error.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
a_status_code_valid: a_status_code > 0
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
do
|
||||
create h.make
|
||||
h.put_content_type_text_plain
|
||||
h.put_content_length (a_description.count)
|
||||
h.put_current_date
|
||||
res.set_status_code (a_status_code)
|
||||
res.put_header_text (h.string)
|
||||
res.put_string (a_description)
|
||||
end
|
||||
|
||||
handle_not_implemented (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.not_implemented.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_implemented, req, res)
|
||||
end
|
||||
|
||||
handle_bad_request_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.bad_request.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.bad_request, req, res)
|
||||
end
|
||||
|
||||
handle_resource_not_found_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.not_found.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_found, req, res)
|
||||
end
|
||||
|
||||
handle_forbidden (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.forbidden.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.forbidden, req, res)
|
||||
end
|
||||
|
||||
feature -- Handle responses: others
|
||||
|
||||
handle_precondition_fail_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.precondition_failed.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.precondition_failed, req, res)
|
||||
end
|
||||
|
||||
handle_internal_server_error (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.internal_server_error.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.internal_server_error, req, res)
|
||||
end
|
||||
|
||||
handle_method_not_allowed_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.method_not_allowed.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.method_not_allowed, req, res)
|
||||
end
|
||||
|
||||
handle_resource_not_modified_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.not_modified.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.not_modified, req, res)
|
||||
end
|
||||
|
||||
handle_resource_conflict_response (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle error {HTTP_STATUS_CODE}.conflict.
|
||||
require
|
||||
a_description_attached: a_description /= Void
|
||||
req_attached: req /= Void
|
||||
res_attached: res /= Void
|
||||
do
|
||||
handle_error (a_description, {HTTP_STATUS_CODE}.conflict, req, res)
|
||||
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
|
||||
Reference in New Issue
Block a user