Merge remote-tracking branch 'jocelyn/flexible_response' into cors

Conflicts:
	examples/filter/filter-safe.ecf
	examples/filter/src/filter_server.e
	library/network/protocol/http/src/http_header.e
	library/server/wsf/src/wsf_response.e
This commit is contained in:
Olivier Ligot
2013-03-15 13:33:13 +01:00
370 changed files with 9238 additions and 35495 deletions

View File

@@ -0,0 +1,108 @@
note
description: "Summary description for {WGI_DELAYED_HEADER_RESPONSE}."
date: "$Date$"
revision: "$Revision$"
class
WSF_WGI_DELAYED_HEADER_RESPONSE
inherit
WGI_FILTER_RESPONSE
redefine
commit,
put_character,
put_string,
put_substring,
flush,
message_writable
end
WSF_RESPONSE_EXPORTER
create
make
feature {NONE} -- Initialization
make (r: WGI_RESPONSE; res: WSF_RESPONSE)
do
wsf_response := res
make_with_response (r)
end
feature {NONE} -- Implementation
wsf_response: WSF_RESPONSE
commit
do
Precursor
if not header_committed then
process_header
end
end
process_header
require
header_not_committed: not header_committed
do
-- If no content is sent, the final `{WGI_REPONSE}.push' will call `process_header'
-- via `{WGI_RESPONSE}.post_commit_action'
wgi_response.set_post_commit_action (Void)
-- commit status code and reason phrase
-- commit header text
wsf_response.process_header
-- update wgi_response on wsf_response to send content directly
wsf_response.set_wgi_response (wgi_response)
ensure
header_committed: header_committed
end
feature -- Status report
message_writable: BOOLEAN = True
-- Can message be written?
feature -- Output operation
put_character (c: CHARACTER_8)
-- Send the character `c'
do
process_header
Precursor (c)
end
put_string (s: READABLE_STRING_8)
-- Send the string `s'
do
process_header
Precursor (s)
end
put_substring (s: READABLE_STRING_8; a_begin_index, a_end_index: INTEGER)
-- Send the substring `s[a_begin_index:a_end_index]'
do
process_header
Precursor (s, a_begin_index, a_end_index)
end
flush
-- Flush if it makes sense
do
process_header
Precursor
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

View File

@@ -26,7 +26,7 @@ feature {NONE} -- Initialization
make_with_value (a_value: WSF_VALUE)
do
name := a_value.name
create {LINKED_LIST [WSF_STRING]} values.make
create {ARRAYED_LIST [WSF_STRING]} values.make (3)
add_value (a_value)
end
@@ -59,6 +59,12 @@ feature -- Access
name: READABLE_STRING_32
url_encoded_name: READABLE_STRING_8
-- URL encoded string of `name'.
do
Result := url_encoder.encoded_string (name)
end
values: LIST [WSF_STRING]
frozen string_values: like values
@@ -174,7 +180,7 @@ invariant
string_values_not_empty: values.count >= 1
note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Eiffel Software and others"
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

View File

@@ -16,6 +16,11 @@ feature -- Access
deferred
end
url_encoded_name: READABLE_STRING_8
-- URL encoded string of `name'.
deferred
end
frozen key: like name
do
Result := name

View File

@@ -347,7 +347,7 @@ feature {NONE} -- Access: global variable
end
end
feature -- Access: global variable
feature -- Access: global variables
items: ITERABLE [WSF_VALUE]
do
@@ -380,7 +380,11 @@ feature -- Access: global variable
string_array_item (a_name: READABLE_STRING_GENERAL): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for path parameter `a_name' if relevant.
do
Result := string_array_item_for (a_name, agent item)
if attached {WSF_TABLE} item (a_name) as tb then
Result := tb.as_array_of_string
else
Result := string_array_item_for (a_name, agent item)
end
end
string_array_item_for (a_name: READABLE_STRING_GENERAL; a_item_fct: FUNCTION [ANY, TUPLE [READABLE_STRING_GENERAL], detachable WSF_VALUE]): detachable ARRAY [READABLE_STRING_32]
@@ -407,6 +411,43 @@ feature -- Access: global variable
Result.keep_head (n - 1)
end
feature -- Helpers: global variables
items_as_string_items: ITERABLE [TUPLE [name: READABLE_STRING_32; value: detachable READABLE_STRING_32]]
-- `items' as strings items
-- i.e: flatten any table or related into multiple string items
local
res: ARRAYED_LIST [TUPLE [name: READABLE_STRING_32; value: detachable READABLE_STRING_32]]
do
if attached items_table as tb then
create res.make (tb.count)
across
tb as c
loop
append_value_as_string_items_to (c.item, res)
end
else
create res.make (0)
end
Result := res
end
append_value_as_string_items_to (v: WSF_VALUE; a_target: LIST [TUPLE [name: READABLE_STRING_32; value: detachable READABLE_STRING_32]])
-- Append value `v' to `a_target' as multiple string items
do
if attached {WSF_STRING} v as s then
a_target.force ([s.name, s.value])
elseif attached {ITERABLE [WSF_VALUE]} v as lst then
across
lst as c
loop
append_value_as_string_items_to (c.item, a_target)
end
else
a_target.force ([v.name, v.string_representation])
end
end
feature -- Execution variables
execution_variable (a_name: READABLE_STRING_GENERAL): detachable ANY
@@ -1796,7 +1837,7 @@ invariant
wgi_request.content_type /= Void implies content_type /= Void
note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Eiffel Software and others"
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

View File

@@ -28,16 +28,32 @@ convert
feature {NONE} -- Initialization
make_from_wgi (r: WGI_RESPONSE)
local
wres: detachable WSF_WGI_DELAYED_HEADER_RESPONSE
do
transfered_content_length := 0
wgi_response := r
create header.make
wgi_response := r
create wres.make (r, Current)
wgi_response := wres
set_status_code ({HTTP_STATUS_CODE}.ok) -- Default value
end
feature {WSF_RESPONSE_EXPORTER} -- Properties
wgi_response: WGI_RESPONSE
-- Associated WGI_RESPONSE
-- Associated WGI_RESPONSE.
header: WSF_HEADER
-- Associated response header.
feature {WSF_RESPONSE_EXPORTER} -- Change
set_wgi_response (res: WGI_RESPONSE)
-- Set associated WGI_RESPONSE
do
wgi_response := res
end
feature -- Status report
@@ -77,6 +93,7 @@ feature -- Status setting
-- Set response status code
-- Should be done before sending any data back to the client
--| note: the status is really sent when the header are set
--| Default value might be set to 200 {HTTP_HEADER}.ok.
require
a_code_valid: a_code > 0
status_not_set: not status_committed
@@ -113,19 +130,60 @@ feature -- Status setting
status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase (optional)
feature {WSF_RESPONSE_EXPORTER} -- Header output operation
process_header
require
header_not_committed: not header_committed
do
if not header_committed then
-- commit status code and reason phrase
wgi_response.set_status_code (status_code, status_reason_phrase)
-- commit header text
wgi_response.put_header_text (header.string)
end
ensure
status_committed: status_committed
header_committed: header_committed
end
report_content_already_sent_and_header_ignored
do
put_error ("Content already sent, new header text ignored!")
end
feature -- Header output operation
header: HTTP_HEADER
-- Header
-- This is useful when we want to fill the `header'
-- in two pass (i.e. in two different classes).
-- We first call features of `header', and finally
-- we call `put_header_text'
put_header_line (h: READABLE_STRING_8)
-- Put header `h'
-- Replace any existing value
require
header_not_committed: not header_committed
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.put_header (h)
end
end
add_header_line (h: READABLE_STRING_8)
-- Add header `h'
-- This can lead to duplicated header entries
require
header_not_committed: not header_committed
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.add_header (h)
end
end
put_header_text (a_text: READABLE_STRING_8)
-- Sent `a_text' and just before send the status code
-- Put the multiline header `a_text'
-- Overwite potential existing header
require
status_set: status_is_set
header_not_committed: not header_committed
a_text_ends_with_single_crlf: a_text.count > 2 implies not a_text.substring (a_text.count - 2, a_text.count).same_string ("%R%N")
a_text_does_not_end_with_double_crlf: a_text.count > 4 implies not a_text.substring (a_text.count - 4, a_text.count).same_string ("%R%N%R%N")
@@ -133,62 +191,88 @@ feature -- Header output operation
l_text: READABLE_STRING_8
l_header: HTTP_HEADER
do
wgi_response.set_status_code (status_code, status_reason_phrase)
if header.is_empty then
l_text := a_text
if header_committed then
report_content_already_sent_and_header_ignored
else
create l_header.make_from_raw_header_data (a_text)
across
l_header as c
loop
header.put_header (c.item.string)
end
l_text := header.string
header.append_raw_header_data (a_text)
end
wgi_response.put_header_text (l_text)
ensure
status_set: status_is_set
status_committed: status_committed
header_committed: header_committed
message_writable: message_writable
end
add_header_text (a_text: READABLE_STRING_8)
-- Add the multiline header `a_text'
-- Does not replace existing header with same name
-- This could leads to multiple header with the same name
require
header_not_committed: not header_committed
a_text_ends_with_single_crlf: a_text.count > 2 implies not a_text.substring (a_text.count - 2, a_text.count).same_string ("%R%N")
a_text_does_not_end_with_double_crlf: a_text.count > 4 implies not a_text.substring (a_text.count - 4, a_text.count).same_string ("%R%N%R%N")
do
if header_committed then
report_content_already_sent_and_header_ignored
else
header.append_raw_header_data (a_text)
end
ensure
status_set: status_is_set
message_writable: message_writable
end
feature -- Header output operation: helpers
put_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Send headers with status `a_status', and headers from `a_headers'
-- Put headers with status `a_status', and headers from `a_headers'
require
status_not_committed: not status_committed
header_not_committed: not header_committed
local
h: HTTP_HEADER
do
set_status_code (a_status_code)
if a_headers /= Void then
create h.make_from_array (a_headers)
put_header_text (h.string)
end
ensure
status_set: status_is_set
header_committed: header_committed
message_writable: message_writable
end
add_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Put headers with status `a_status', and headers from `a_headers'
require
status_not_committed: not status_committed
header_not_committed: not header_committed
local
h: HTTP_HEADER
do
if a_headers /= Void then
create h.make_from_array (a_headers)
add_header_text (h.string)
end
ensure
status_set: status_is_set
message_writable: message_writable
end
put_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Send headers from `a_lines'
local
h: STRING_8
-- Put headers from `a_lines'
require
header_not_committed: not header_committed
do
create h.make (256)
across
a_lines as c
loop
h.append (c.item.name)
h.append_character (':')
h.append_character (' ')
h.append (c.item.value)
h.append_character ('%R')
h.append_character ('%N')
across a_lines as c loop
put_header_line (c.item.name + ": " + c.item.value)
end
end
add_header_lines (a_lines: ITERABLE [TUPLE [name: READABLE_STRING_8; value: READABLE_STRING_8]])
-- Add headers from `a_lines'
require
header_not_committed: not header_committed
do
across a_lines as c loop
add_header_line (c.item.name + ": " + c.item.value)
end
put_header_text (h)
end
feature -- Output report