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

@@ -34,7 +34,12 @@ create
feature {NONE} -- Initialization
initialize
local
conn: like connector
do
create on_launched_actions
create on_stopped_actions
port_number := 80 --| Default, but quite often, this port is already used ...
base_url := ""
@@ -64,14 +69,15 @@ feature {NONE} -- Initialization
verbose := l_verbose_str.as_lower.same_string ("true")
end
end
create connector.make (Current)
if attached connector as conn then
conn.set_base (base_url)
if single_threaded then
conn.configuration.set_force_single_threaded (True)
end
conn.configuration.set_is_verbose (verbose)
create conn.make (Current)
conn.on_launched_actions.extend (agent on_launched)
conn.on_stopped_actions.extend (agent on_stopped)
connector := conn
conn.set_base (base_url)
if single_threaded then
conn.configuration.set_force_single_threaded (True)
end
conn.configuration.set_is_verbose (verbose)
end
feature -- Execution
@@ -104,8 +110,26 @@ feature -- Execution
end
end
feature -- Callback
on_launched_actions: ACTION_SEQUENCE [TUPLE [WGI_CONNECTOR]]
-- Actions triggered when launched
on_stopped_actions: ACTION_SEQUENCE [TUPLE [WGI_CONNECTOR]]
-- Actions triggered when stopped
feature {NONE} -- Implementation
on_launched (conn: WGI_CONNECTOR)
do
on_launched_actions.call ([conn])
end
on_stopped (conn: WGI_CONNECTOR)
do
on_stopped_actions.call ([conn])
end
port_number: INTEGER
server_name: detachable READABLE_STRING_8
@@ -123,11 +147,11 @@ feature -- Status report
launchable: BOOLEAN
do
Result := Precursor and port_number > 0
Result := Precursor and port_number >= 0
end
;note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software

View File

@@ -203,7 +203,9 @@ feature -- Execution
end
if
attached req.meta_string_variable ("HTTP_IF_MODIFIED_SINCE") as s_if_modified_since and then
attached file_date (f) as f_date and then (f_date >= rfc1123_http_date_format_to_date (s_if_modified_since))
attached http_date_format_to_date (s_if_modified_since) as l_if_modified_since_date and then
attached file_date (f) as f_date and then
f_date <= l_if_modified_since_date
then
process_not_modified (f_date, req, res)
else
@@ -425,35 +427,59 @@ feature {NONE} -- Implementation
feature {NONE} -- implementation: date time
date_time_utility: HTTP_DATE_TIME_UTILITIES
once
create Result
end
file_date (f: FILE): DATE_TIME
do
Result := timestamp_to_date (f.date)
end
rfc1123_http_date_format_to_date (s: STRING): DATE_TIME
http_date_format_to_date (s: READABLE_STRING_8): detachable DATE_TIME
-- String representation of `dt' using the RFC 1123
-- HTTP-date = rfc1123-date | rfc850-date | asctime-date
-- rfc1123-date = wkday "," SP date1 SP time SP "GMT"
-- rfc850-date = weekday "," SP date2 SP time SP "GMT"
-- asctime-date = wkday SP date3 SP time SP 4DIGIT
-- date1 = 2DIGIT SP month SP 4DIGIT
-- ; day month year (e.g., 02 Jun 1982)
-- date2 = 2DIGIT "-" month "-" 2DIGIT
-- ; day-month-year (e.g., 02-Jun-82)
-- date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
-- ; month day (e.g., Jun 2)
-- time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
-- ; 00:00:00 - 23:59:59
-- wkday = "Mon" | "Tue" | "Wed"
-- | "Thu" | "Fri" | "Sat" | "Sun"
-- weekday = "Monday" | "Tuesday" | "Wednesday"
-- | "Thursday" | "Friday" | "Saturday" | "Sunday"
-- month = "Jan" | "Feb" | "Mar" | "Apr"
-- | "May" | "Jun" | "Jul" | "Aug"
-- | "Sep" | "Oct" | "Nov" | "Dec"
--| Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
--| Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
--| Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
--|
--| "ddd, [0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2"
--| ex: "WED, 30 JAN 2013 21:34:33 "
note
EIS: "name=RFC2616", "protocol=URI", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html"
local
t: STRING
d: HTTP_DATE
do
t := s
if t.ends_with ("GMT") then
t := t.substring (1, t.count - 4)
create d.make_from_string (s)
if not d.has_error then
Result := d.date_time
end
create Result.make_from_string (t, "ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2")
end
timestamp_to_date (n: INTEGER): DATE_TIME
local
d: HTTP_DATE
do
Result := date_time_utility.unix_time_stamp_to_date_time (n)
create d.make_from_timestamp (n)
Result := d.date_time
end
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

@@ -151,7 +151,9 @@ feature {NONE} -- Storage
build
end
rescue
io.error.put_string ("Error while loading Cookie session...!%N")
debug ("wsf")
io.error.put_string ("Error while loading Cookie session...!%N")
end
end
build

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