Refactor REQUEST_RESOURCE_HANDLER_HELPER to figure out the

transfer encoding: Chunked.
Added a new method to retrieve_data independently if the
transfer is chunked or not.
Updated ORDER_HANLDER to use this new feature.
Sync with Jocelyn repo
This commit is contained in:
jvelilla
2012-02-17 08:48:27 -03:00
parent f1fe5464c6
commit 468b2851be
4 changed files with 41 additions and 12 deletions

View File

@@ -23,17 +23,19 @@ feature {NONE} -- Initialization
do do
create h.make create h.make
sess := h.new_session ("http://127.0.0.1:9090") sess := h.new_session ("http://127.0.0.1:9090")
-- Create Order -- Create Order
print ("%N Create Order %N") print ("%N Create Order %N")
resp := create_order (sess) resp := create_order (sess)
-- Read the Order -- Read the Order
print ("%N Read Order %N") print ("%N Read Order %N")
l_location := resp.header ("Location") l_location := resp.header ("Location")
resp := read_order (sess, l_location) resp := read_order (sess, l_location)
-- Update the Order
-- Update the Order
if attached resp.body as l_body then if attached resp.body as l_body then
body := l_body.as_string_8 body := l_body.as_string_8
body.replace_substring_all ("takeAway", "in Shop") body.replace_substring_all ("takeAway", "in Shop")
@@ -43,11 +45,15 @@ feature {NONE} -- Initialization
end end
update_order ( sess: HTTP_CLIENT_SESSION; uri : detachable READABLE_STRING_8; a_body : STRING) : HTTP_CLIENT_RESPONSE update_order ( sess: HTTP_CLIENT_SESSION; uri : detachable READABLE_STRING_8; a_body : STRING) : HTTP_CLIENT_RESPONSE
local
context : HTTP_CLIENT_REQUEST_CONTEXT
do do
create Result.make create Result.make
if attached uri as l_uri then if attached uri as l_uri then
sess.set_base_url (l_uri) sess.set_base_url (l_uri)
Result := sess.put ("", Void, a_body ) create context.make
context.headers.put ("application/json", "Content-Type")
Result := sess.put ("", context, a_body )
-- Show headers -- Show headers
across across
Result.headers as l_headers Result.headers as l_headers

View File

@@ -114,8 +114,7 @@ feature -- HTTP Methods
do do
if attached req.orig_path_info as orig_path then if attached req.orig_path_info as orig_path then
id := get_order_id_from_path (orig_path) id := get_order_id_from_path (orig_path)
req.input.read_string (req.content_length_value.as_integer_32) l_put := retrieve_data (req)
l_put := req.input.last_string
l_order := extract_order_request(l_put) l_order := extract_order_request(l_put)
if l_order /= Void and then db_access.orders.has_key (id) then if l_order /= Void and then db_access.orders.has_key (id) then
l_order.set_id (id) l_order.set_id (id)
@@ -234,8 +233,7 @@ feature -- HTTP Methods
local local
l_post: STRING l_post: STRING
do do
req.input.read_string (req.content_length_value.as_integer_32) l_post := retrieve_data (req)
l_post := req.input.last_string
if attached extract_order_request (l_post) as l_order then if attached extract_order_request (l_post) as l_order then
save_order (l_order) save_order (l_order)
compute_response_post (ctx, req, res, l_order) compute_response_post (ctx, req, res, l_order)
@@ -358,6 +356,6 @@ feature {NONE} -- Implementation Repository Layer
end end
note note
copyright: "2011-2011, Javier Velilla and others" copyright: "2011-2012, Javier Velilla 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)"
end end

View File

@@ -43,10 +43,14 @@ feature -- Method Post
execute_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE) execute_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do do
if req.content_length_value > 0 then if req.is_chunked_input then
do_post (ctx, req, res) do_post (ctx, req, res)
else else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res) if req.content_length_value > 0 then
do_post (ctx, req, res)
else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res)
end
end end
end end
@@ -59,10 +63,14 @@ feature-- Method Put
execute_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE) execute_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do do
if req.content_length_value > 0 then if req.is_chunked_input then
do_put (ctx, req, res) do_put (ctx, req, res)
else else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res) if req.content_length_value > 0 then
do_put (ctx, req, res)
else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res)
end
end end
end end
@@ -155,6 +163,23 @@ feature -- Method Extension Method
handle_not_implemented ("Method extension-method not implemented", ctx, req, res) handle_not_implemented ("Method extension-method not implemented", ctx, req, res)
end end
feature -- Retrieve content from WGI_INPUT_STREAM
retrieve_data ( req : WSF_REQUEST) : STRING
-- retrieve the content from the input stream
-- handle differents transfers
do
Result := ""
if req.is_chunked_input then
if attached req.chunked_input as l_chunked_input then
Result := l_chunked_input.data.as_string_8
end
else
req.input.read_string (req.content_length_value.as_integer_32)
Result := req.input.last_string
end
end
feature -- Handle responses feature -- Handle responses
-- TODO Handle Content negotiation. -- TODO Handle Content negotiation.
-- The option : Server-driven negotiation: uses request headers to select a variant -- The option : Server-driven negotiation: uses request headers to select a variant