Added WSF_DEFAULT_*_RESPONSE

Fixed the method not allowed by setting the Allow: header
This commit is contained in:
Jocelyn Fiat
2012-12-14 15:52:35 +01:00
parent 8e31950285
commit cc570f5abf
4 changed files with 124 additions and 15 deletions

View File

@@ -48,22 +48,9 @@ feature -- Execution
execute_default (req: WSF_REQUEST; res: WSF_RESPONSE) execute_default (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Default procedure -- Default procedure
local local
msg: WSF_RESPONSE_MESSAGE msg: WSF_DEFAULT_ROUTER_RESPONSE
not_found: WSF_NOT_FOUND_RESPONSE
not_allowed: WSF_METHOD_NOT_ALLOWED_RESPONSE
trace: WSF_TRACE_RESPONSE
do do
if req.is_request_method ({HTTP_REQUEST_METHODS}.method_trace) then create msg.make_with_router (req, router)
create trace.make (req)
msg := trace
elseif attached router.allowed_methods_for_request (req) as mtds and then not mtds.is_empty then
create not_allowed.make (req)
not_allowed.set_suggested_methods (mtds)
msg := not_allowed
else
create not_found.make (req)
msg := not_found
end
res.send (msg) res.send (msg)
end end

View File

@@ -0,0 +1,53 @@
note
description: "Summary description for {WSF_DEFAULT_RESPONSE}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_DEFAULT_RESPONSE
inherit
WSF_RESPONSE_MESSAGE
create
make
feature {NONE} -- Initialization
make (req: WSF_REQUEST)
-- Initialize Current with request `req'
do
request := req
end
feature -- Access
request: WSF_REQUEST
-- Associated request.
feature {WSF_RESPONSE} -- Output
send_to (res: WSF_RESPONSE)
-- Send Current message to `res'
--
-- This feature should be called via `{WSF_RESPONSE}.send (obj)'
-- where `obj' is the current object
local
msg: WSF_RESPONSE_MESSAGE
req: like request
not_found: WSF_NOT_FOUND_RESPONSE
trace: WSF_TRACE_RESPONSE
do
req := request
if req.is_request_method ({HTTP_REQUEST_METHODS}.method_trace) then
create trace.make (req)
msg := trace
else
create not_found.make (req)
msg := not_found
end
res.send (msg)
end
end

View File

@@ -0,0 +1,63 @@
note
description: "Summary description for {WSF_DEFAULT_ROUTER_RESPONSE}."
date: "$Date$"
revision: "$Revision$"
class
WSF_DEFAULT_ROUTER_RESPONSE
inherit
WSF_DEFAULT_RESPONSE
redefine
send_to
end
create
make_with_router
feature {NONE} -- Initialization
make_with_router (req: WSF_REQUEST; a_router: like router)
-- Initialize Current with request `req' and router `a_router'
-- Initialize Current with request `req'
do
router := a_router
make (req)
end
feature -- Access
router: WSF_ROUTER
-- Associated router.
feature {WSF_RESPONSE} -- Output
send_to (res: WSF_RESPONSE)
-- Send Current message to `res'
--
-- This feature should be called via `{WSF_RESPONSE}.send (obj)'
-- where `obj' is the current object
local
msg: WSF_RESPONSE_MESSAGE
req: like request
not_found: WSF_NOT_FOUND_RESPONSE
not_allowed: WSF_METHOD_NOT_ALLOWED_RESPONSE
trace: WSF_TRACE_RESPONSE
do
req := request
if req.is_request_method ({HTTP_REQUEST_METHODS}.method_trace) then
create trace.make (req)
msg := trace
elseif attached router.allowed_methods_for_request (req) as mtds and then not mtds.is_empty then
create not_allowed.make (req)
not_allowed.set_suggested_methods (mtds)
msg := not_allowed
else
create not_found.make (req)
msg := not_found
end
res.send (msg)
end
end

View File

@@ -54,6 +54,11 @@ feature {WSF_RESPONSE} -- Output
do do
h := header h := header
res.set_status_code ({HTTP_STATUS_CODE}.method_not_allowed) res.set_status_code ({HTTP_STATUS_CODE}.method_not_allowed)
if attached suggested_methods as lst and then not lst.is_empty then
h.put_allow (lst)
end
s := "Not allowed" s := "Not allowed"
if request.is_content_type_accepted ({HTTP_MIME_TYPES}.text_html) then if request.is_content_type_accepted ({HTTP_MIME_TYPES}.text_html) then
@@ -119,6 +124,7 @@ feature {WSF_RESPONSE} -- Output
end end
h.put_content_type_text_plain h.put_content_type_text_plain
end end
h.put_content_length (s.count) h.put_content_length (s.count)
res.put_header_text (h.string) res.put_header_text (h.string)
res.put_string (s) res.put_string (s)