Added framework for HTTP-conforming contracts

This commit is contained in:
Colin Adams
2012-11-24 11:55:10 +00:00
parent b7379a6289
commit 17ce1be582
4 changed files with 129 additions and 35 deletions

View File

@@ -23,7 +23,7 @@ inherit
WSF_RESOURCE_HANDLER_HELPER WSF_RESOURCE_HANDLER_HELPER
redefine redefine
do_get, do_get_head,
do_post, do_post,
do_put, do_put,
do_delete do_delete
@@ -53,27 +53,21 @@ feature -- API DOC
feature -- HTTP Methods feature -- HTTP Methods
do_get (req: WSF_REQUEST; res: WSF_RESPONSE) do_get_head (a_req: WSF_REQUEST; a_res: WSF_RESPONSE; a_is_get: BOOLEAN)
-- Using GET to retrieve resource information. -- <Precursor>
-- If the GET request is SUCCESS, we response with
-- 200 OK, and a representation of the order
-- 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
local local
id: STRING id: STRING
do do
if attached req.orig_path_info as orig_path then if attached a_req.orig_path_info as orig_path then
id := get_order_id_from_path (orig_path) id := get_order_id_from_path (orig_path)
if attached retrieve_order (id) as l_order then if attached retrieve_order (id) as l_order then
if is_conditional_get (req, l_order) then if is_conditional_get (a_req, l_order) then
handle_resource_not_modified_response ("The resource" + orig_path + "does not change", req, res) handle_resource_not_modified_response ("The resource" + orig_path + "does not change", a_req, a_res)
else else
compute_response_get (req, res, l_order) compute_response_get_head (a_req, a_res, l_order, a_is_get)
end end
else else
handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", req, res) handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", a_req, a_res)
end end
end end
end end
@@ -93,7 +87,7 @@ feature -- HTTP Methods
end end
end end
compute_response_get (req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER) compute_response_get_head (a_req: WSF_REQUEST; a_res: WSF_RESPONSE; l_order: ORDER; a_is_get: BOOLEAN)
local local
h: HTTP_HEADER h: HTTP_HEADER
l_msg : STRING l_msg : STRING
@@ -105,13 +99,15 @@ feature -- HTTP Methods
if attached {JSON_VALUE} json.value (l_order) as jv then if attached {JSON_VALUE} json.value (l_order) as jv then
l_msg := jv.representation l_msg := jv.representation
h.put_content_length (l_msg.count) h.put_content_length (l_msg.count)
if attached req.request_time as time then if attached a_req.request_time as time then
h.add_header ("Date:" + time.formatted_out ("ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2") + " GMT") h.add_header ("Date:" + time.formatted_out ("ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2") + " GMT")
end end
h.add_header ("etag:" + etag_utils.md5_digest (l_order.out)) h.add_header ("etag:" + etag_utils.md5_digest (l_order.out))
res.set_status_code ({HTTP_STATUS_CODE}.ok) a_res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.put_header_text (h.string) a_res.put_header_text (h.string)
res.put_string (l_msg) if a_is_get then
a_res.put_string (l_msg)
end
end end
end end

View File

@@ -0,0 +1,24 @@
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 (a_req: WSF_REQUEST; a_res: WSF_RESPONSE)
-- Respond to `a_req' using `a_res'.
require
a_req_not_void: a_req /= Void
a_res_not_void: a_res /= Void
deferred
ensure
end
end

View File

@@ -0,0 +1,55 @@
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_put
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
end

View File

@@ -4,13 +4,16 @@ note
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class class WSF_RESOURCE_HANDLER_HELPER
WSF_RESOURCE_HANDLER_HELPER
inherit
WSF_METHOD_HANDLERS
feature -- Execute template feature -- Execute template
execute_methods (req: WSF_REQUEST; res: WSF_RESPONSE) execute_methods (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request and dispatch according to the request method -- Execute request and dispatch according to the request method.
local local
m: READABLE_STRING_8 m: READABLE_STRING_8
do do
@@ -44,16 +47,31 @@ feature -- Method Get
do_get (req, res) do_get (req, res)
end end
do_get (req: WSF_REQUEST; res: WSF_RESPONSE) frozen do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Using GET to retrieve resource information. -- Using GET to retrieve resource information.
-- If the GET request is SUCCESS, we response with -- If the GET request is SUCCESS, we respond with
-- 200 OK, and a representation of the person -- 200 OK, and a representation of the resource.
-- If the GET request is not SUCCESS, we response with -- If the GET request is not SUCCESS, we response with
-- 404 Resource not found -- 404 Resource not found
-- If is a Condition GET and the resource does not change we send a -- If is a Condition GET and the resource does not change we send a
-- 304, Resource not modifed -- 304, Resource not modifed
do do
handle_not_implemented ("Method GET not implemented", req, res) do_get_head (req, res, True)
end
do_get_head (a_req: WSF_REQUEST; a_res: WSF_RESPONSE; a_is_get: BOOLEAN)
-- Using GET or HEAD to retrieve resource information.
-- If the GET or HEAD request is SUCCESS, we respond with
-- 200 OK, and WITH/WITHOUT (for GET/HEAD respectively) a representation of the resource
-- If the GET or HEAD request is not SUCCESS, we respond with
-- 404 Resource not found.
-- If Conditional GET or HEAD and the resource does not change we send a
-- 304, Resource not modifed.
do
handle_not_implemented ("Methods GET and HEAD not implemented", a_req, a_res)
ensure
all_postconditions_for_do_get: a_is_get implies True
all_postconditions_for_do_head: not a_is_get implies True
end end
feature -- Method Post feature -- Method Post
@@ -139,16 +157,16 @@ feature -- Method HEAD
do_head (req, res) do_head (req, res)
end end
do_head (req: WSF_REQUEST; res: WSF_RESPONSE) frozen do_head (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Using HEAD to retrieve resource information. -- Using HEAD to retrieve resource information.
-- If the HEAD request is SUCCESS, we response with -- If the HEAD request is SUCCESS, we respond with
-- 200 OK, and WITHOUT a representation of the person -- 200 OK, and WITHOUT a representation of the resource.
-- If the HEAD request is not SUCCESS, we response with -- If the HEAD request is not SUCCESS, we respond with
-- 404 Resource not found -- 404 Resource not found.
-- If is a Condition HEAD and the resource does not change we send a -- If Conditional HEAD and the resource does not change we send a
-- 304, Resource not modifed -- 304, Resource not modifed.
do do
handle_not_implemented ("Method HEAD not implemented", req, res) do_get_head (req, res, False)
end end
feature -- Method OPTIONS feature -- Method OPTIONS
@@ -172,6 +190,7 @@ feature -- Method TRACE
do_trace (req: WSF_REQUEST; res: WSF_RESPONSE) do_trace (req: WSF_REQUEST; res: WSF_RESPONSE)
do do
-- TODO - implement frozen, as there is only one permitted semantic.
handle_not_implemented ("Method TRACE not implemented", req, res) handle_not_implemented ("Method TRACE not implemented", req, res)
end end