New directory structure (variants, results, parsers)

Refactor STRING to READABLE_STRING_8.
Clean code, added documentation and EIS references.
This commit is contained in:
jvelilla
2013-09-17 10:09:46 -03:00
parent f2ee91764d
commit a1245b77fd
20 changed files with 372 additions and 472 deletions

View File

@@ -1,58 +0,0 @@
note
description: "Summary description for {CHARACTER_ENCODING_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
CHARACTER_ENCODING_VARIANT_RESULTS
feature -- Access
character_type: detachable STRING
variant_header: detachable STRING
supported_variants: detachable LIST [STRING]
is_acceptable: BOOLEAN
feature -- Change Element
set_character_type (a_character_type: STRING)
-- Set `character_type' with `a_character_type'
do
character_type := a_character_type
ensure
character_type_set: character_type /= Void implies a_character_type = character_type
end
set_variant_header
-- Set variant header as `Accept-Charset'
do
variant_header := "Accept-Charset"
ensure
variant_header_set: attached variant_header as l_variant_header implies l_variant_header.same_string ("Accept-Charset")
end
set_supported_variants (a_supported: LIST [STRING])
-- Set `supported_variants' with `a_supported'
do
supported_variants := a_supported
ensure
supported_variants_set: supported_variants /= Void implies supported_variants = a_supported
end
set_acceptable (acceptable: BOOLEAN)
-- Set `is_acceptable' with `acceptabe'
do
is_acceptable := acceptable
ensure
is_acceptable_set: is_acceptable = acceptable
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -1,59 +0,0 @@
note
description: "Summary description for {COMPRESSION_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
EIS: "name= Compression", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3", "protocol=uri"
class
COMPRESSION_VARIANT_RESULTS
feature -- Access
compression_type: detachable STRING
variant_header: detachable STRING
supported_variants: detachable LIST [STRING]
is_acceptable: BOOLEAN
feature -- Change Element
set_compression_type (a_compression_type: STRING)
-- Set `compression_type' with `a_compression_type'
do
compression_type := a_compression_type
ensure
compression_type_set: compression_type /= Void implies a_compression_type = compression_type
end
set_variant_header
-- Set variant_header as `Accept-Encoding'
do
variant_header := "Accept-Encoding"
ensure
variant_header_set: attached variant_header as l_variant_header implies l_variant_header.same_string ("Accept-Encoding")
end
set_supported_variants (a_supported: LIST [STRING])
-- Set `supported_variants' with `a_supported'
do
supported_variants := a_supported
ensure
supported_variants_set: supported_variants /= Void implies supported_variants = a_supported
end
set_acceptable (acceptable: BOOLEAN)
-- Set `is_acceptable' with `acceptable'
do
is_acceptable := acceptable
ensure
is_acceptable_set: is_acceptable = acceptable
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -28,7 +28,7 @@ create
feature -- Initialization
make (a_mime: STRING; a_language: STRING; a_charset: STRING; a_encoding: STRING)
make (a_mime: READABLE_STRING_8; a_language: READABLE_STRING_8; a_charset: READABLE_STRING_8; a_encoding: READABLE_STRING_8)
do
set_mime_default (a_mime)
set_language_default (a_language)
@@ -43,29 +43,21 @@ feature -- Initialization
feature -- AccessServer Side Defaults Formats
mime_default: STRING
mime_default: READABLE_STRING_8
-- Media types which are acceptable for the response
language_default: READABLE_STRING_8
-- Set of natural languages that are preferred as a response to the request
charset_default: READABLE_STRING_8
-- Character sets are acceptable for the response.
language_default: STRING
charset_default: STRING
encoding_default: STRING
set_encoding_defautl (an_encoding: STRING)
do
encoding_default := an_encoding
ensure
set_encoding: an_encoding ~ encoding_default
end
encoding_default: READABLE_STRING_8
-- Content-codings that are acceptable in the response.
feature -- Change Element
set_mime_default (a_mime: STRING)
set_mime_default (a_mime: READABLE_STRING_8)
-- set the mime_default with `a_mime'
do
mime_default := a_mime
@@ -73,7 +65,7 @@ feature -- Change Element
set_mime_default: a_mime = mime_default
end
set_language_default (a_language: STRING)
set_language_default (a_language: READABLE_STRING_8)
-- set the language_default with `a_language'
do
language_default := a_language
@@ -81,7 +73,7 @@ feature -- Change Element
set_language: a_language = language_default
end
set_charset_default (a_charset: STRING)
set_charset_default (a_charset: READABLE_STRING_8)
-- set the charset_default with `a_charset'
do
charset_default := a_charset
@@ -89,7 +81,7 @@ feature -- Change Element
set_charset: a_charset = charset_default
end
set_encoding_default (a_encoding: STRING)
set_encoding_default (a_encoding: READABLE_STRING_8)
do
encoding_default := a_encoding
ensure
@@ -100,31 +92,31 @@ feature -- Change Element
feature -- Media Type Negotiation
media_type_preference (mime_types_supported: LIST [STRING]; header: detachable READABLE_STRING_8): MEDIA_TYPE_VARIANT_RESULTS
-- mime_types_supported represent media types supported by the server.
-- header represent the Accept header, ie, the client preferences.
media_type_preference (a_mime_types_supported: LIST [READABLE_STRING_8]; a_header: detachable READABLE_STRING_8): MEDIA_TYPE_VARIANT_RESULTS
-- `a_mime_types_supported' represent media types supported by the server.
-- `a_header represent' the Accept header, ie, the client preferences.
-- Return which media type to use for representaion in a response, if the server support
-- one media type, or empty in other case.
note
EIS: "name=media type", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1", "protocol=uri"
local
mime_match: STRING
l_mime_match: READABLE_STRING_8
do
create Result
if header = Void or else header.is_empty then
if a_header = Void or else a_header.is_empty then
-- the request has no Accept header, ie the header is empty, in this case we use the default format
Result.set_acceptable (TRUE)
Result.set_media_type (mime_default)
else
-- select the best match, server support, client preferences
mime_match := mime.best_match (mime_types_supported, header)
if mime_match.is_empty then
l_mime_match := mime.best_match (a_mime_types_supported, a_header)
if l_mime_match.is_empty then
-- The server does not support any of the media types prefered by the client
Result.set_acceptable (False)
Result.set_supported_variants (mime_types_supported)
Result.set_supported_variants (a_mime_types_supported)
else
-- Set the best match
Result.set_media_type (mime_match)
Result.set_media_type (l_mime_match)
Result.set_acceptable (True)
Result.set_variant_header
end
@@ -133,32 +125,32 @@ feature -- Media Type Negotiation
feature -- Encoding Negotiation
charset_preference (server_charset_supported: LIST [STRING]; header: detachable READABLE_STRING_8): CHARACTER_ENCODING_VARIANT_RESULTS
-- server_charset_supported represent a list of charset supported by the server.
-- header represent the Accept-Charset header, ie, the client preferences.
charset_preference (a_server_charset_supported: LIST [READABLE_STRING_8]; a_header: detachable READABLE_STRING_8): CHARACTER_ENCODING_VARIANT_RESULTS
-- `a_server_charset_supported' represent a list of charset supported by the server.
-- `a_header' represent the Accept-Charset header, ie, the client preferences.
-- Return which Charset to use in a response, if the server support
-- one Charset, or empty in other case.
note
EIS: "name=charset", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2", "protocol=uri"
local
charset_match: STRING
l_charset_match: READABLE_STRING_8
do
create Result
if header = Void or else header.is_empty then
if a_header = Void or else a_header.is_empty then
-- the request has no Accept-Charset header, ie the header is empty, in this case use default charset encoding
-- (UTF-8)
Result.set_acceptable (TRUE)
Result.set_character_type (charset_default)
else
-- select the best match, server support, client preferences
charset_match := common.best_match (server_charset_supported, header)
if charset_match.is_empty then
l_charset_match := common.best_match (a_server_charset_supported, a_header)
if l_charset_match.is_empty then
-- The server does not support any of the compression types prefered by the client
Result.set_acceptable (False)
Result.set_supported_variants (server_charset_supported)
Result.set_supported_variants (a_server_charset_supported)
else
-- Set the best match
Result.set_character_type (charset_match)
Result.set_character_type (l_charset_match)
Result.set_acceptable (True)
Result.set_variant_header
end
@@ -167,31 +159,31 @@ feature -- Encoding Negotiation
feature -- Compression Negotiation
encoding_preference (server_encoding_supported: LIST [STRING]; header: detachable READABLE_STRING_8): COMPRESSION_VARIANT_RESULTS
-- server_encoding_supported represent a list of encoding supported by the server.
-- header represent the Accept-Encoding header, ie, the client preferences.
encoding_preference (a_server_encoding_supported: LIST [READABLE_STRING_8]; a_header: detachable READABLE_STRING_8): COMPRESSION_VARIANT_RESULTS
-- `a_server_encoding_supported' represent a list of encoding supported by the server.
-- `a_header' represent the Accept-Encoding header, ie, the client preferences.
-- Return which Encoding to use in a response, if the server support
-- one Encoding, or empty in other case.
note
EIS: "name=encoding", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3", "protocol=uri"
local
compression_match: STRING
l_compression_match: READABLE_STRING_8
do
create Result
if header = Void or else header.is_empty then
if a_header = Void or else a_header.is_empty then
-- the request has no Accept-Encoding header, ie the header is empty, in this case do not compress representations
Result.set_acceptable (TRUE)
Result.set_compression_type (encoding_default)
else
-- select the best match, server support, client preferences
compression_match := common.best_match (server_encoding_supported, header)
if compression_match.is_empty then
l_compression_match := common.best_match (a_server_encoding_supported, a_header)
if l_compression_match.is_empty then
-- The server does not support any of the compression types prefered by the client
Result.set_acceptable (False)
Result.set_supported_variants (server_encoding_supported)
Result.set_supported_variants (a_server_encoding_supported)
else
-- Set the best match
Result.set_compression_type (compression_match)
Result.set_compression_type (l_compression_match)
Result.set_acceptable (True)
Result.set_variant_header
end
@@ -200,32 +192,32 @@ feature -- Compression Negotiation
feature -- Language Negotiation
language_preference (server_language_supported: LIST [STRING]; header: detachable READABLE_STRING_8): LANGUAGE_VARIANT_RESULTS
-- server_language_supported represent a list of languages supported by the server.
-- header represent the Accept-Language header, ie, the client preferences.
language_preference (a_server_language_supported: LIST [READABLE_STRING_8]; a_header: detachable READABLE_STRING_8): LANGUAGE_VARIANT_RESULTS
-- `a_server_language_supported' represent a list of languages supported by the server.
-- `a_header' represent the Accept-Language header, ie, the client preferences.
-- Return which Language to use in a response, if the server support
-- one Language, or empty in other case.
note
EIS: "name=language", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4", "protocol=uri"
local
language_match: STRING
l_language_match: READABLE_STRING_8
do
create Result
if header = Void or else header.is_empty then
if a_header = Void or else a_header.is_empty then
-- the request has no Accept header, ie the header is empty, in this case we use the default format
Result.set_acceptable (TRUE)
Result.set_language_type (language_default)
else
-- select the best match, server support, client preferences
language_match := language.best_match (server_language_supported, header)
if language_match.is_empty then
l_language_match := language.best_match (a_server_language_supported, a_header)
if l_language_match.is_empty then
-- The server does not support any of the media types prefered by the client
Result.set_acceptable (False)
Result.set_supported_variants (server_language_supported)
Result.set_supported_variants (a_server_language_supported)
else
-- Set the best match
Result.set_language_type (language_match)
Result.set_language_type (l_language_match)
Result.set_acceptable (True)
Result.set_variant_header
end

View File

@@ -1,63 +0,0 @@
note
description: "Summary description for {LANGUAGE_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
LANGUAGE_VARIANT_RESULTS
feature -- Access
language_type: detachable STRING
variant_header: detachable STRING
supported_variants: detachable LIST [STRING]
is_acceptable: BOOLEAN
feature -- Change Element
set_acceptable (acceptable: BOOLEAN)
-- Set `is_acceptable' with `acceptable'
do
is_acceptable := acceptable
ensure
is_acceptable_set: is_acceptable = acceptable
end
set_language_type (a_language_type: STRING)
-- Set `language_type' with `a_language_type'
do
language_type := a_language_type
ensure
language_type_set: language_type/= Void implies a_language_type = language_type
end
set_variant_header
-- Set variant header as 'Accept-Language'
do
variant_header := "Accept-Language"
ensure
variant_header_set: attached variant_header as l_variant_header implies l_variant_header.same_string ("Accept-Language")
end
set_supported_variants (a_supported: LIST [STRING])
-- Set `supported vairants' with `a_supported'
do
supported_variants := a_supported
ensure
set_supported_variants: supported_variants /= Void implies supported_variants = a_supported
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -1,62 +0,0 @@
note
description: "Summary description for {MEDIA_TYPE_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
MEDIA_TYPE_VARIANT_RESULTS
feature -- Access
media_type: detachable STRING
variant_header: detachable STRING
supported_variants: detachable LIST [STRING]
is_acceptable: BOOLEAN
feature -- Change Element
set_media_type (a_media_type: STRING)
-- Set `media_type' as `a_media_type'
do
media_type := a_media_type
ensure
media_type_set: media_type /= Void implies media_type = a_media_type
end
set_variant_header
-- Set variant header as `Accept'
do
variant_header := "Accept"
ensure
variant_header_set: attached variant_header as l_variant_header implies l_variant_header.same_string ("Accept")
end
set_supported_variants (a_supported: LIST [STRING])
-- Set `supported_variants' with `a_supported'
do
supported_variants := a_supported
ensure
supported_variants_variants: supported_variants /= Void implies supported_variants = a_supported
end
set_acceptable (acceptable: BOOLEAN)
-- Set `is_acceptable' with `acceptable'
do
is_acceptable := acceptable
ensure
is_acceptable_set: is_acceptable = acceptable
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -9,19 +9,24 @@ note
class
COMMON_ACCEPT_HEADER_PARSER
inherit {NONE}
STRING_UTILS
feature -- Parser
parse_common (header: STRING): COMMON_RESULTS
parse_common (header: READABLE_STRING_8): COMMON_RESULTS
-- Parses `header' charset/encoding into its component parts.
-- For example, the charset 'iso-8889-5' would get parsed
-- into:
-- ('iso-8889-5', {'q':'1.0'})
local
l_parts: LIST [STRING]
sub_parts: LIST [STRING]
p: STRING
l_parts: LIST [READABLE_STRING_8]
sub_parts: LIST [READABLE_STRING_8]
p: READABLE_STRING_8
i: INTEGER
l_header: STRING
l_header: READABLE_STRING_8
do
create Result.make
l_parts := header.split (';')
@@ -45,7 +50,7 @@ feature -- Parser
Result.set_field (trim (l_header))
end
fitness_and_quality_parsed (a_field: STRING; parsed_charsets: LIST [COMMON_RESULTS]): FITNESS_AND_QUALITY
fitness_and_quality_parsed (a_field: READABLE_STRING_8; parsed_charsets: LIST [COMMON_RESULTS]): FITNESS_AND_QUALITY
-- Find the best match for a given charset/encoding against a list of charsets/encodings
-- that have already been parsed by parse_common. Returns a
-- tuple of the fitness value and the value of the 'q' quality parameter of
@@ -57,7 +62,7 @@ feature -- Parser
best_fit_q: REAL_64
target: COMMON_RESULTS
range: COMMON_RESULTS
element: detachable STRING
element: detachable READABLE_STRING_8
l_fitness: INTEGER
do
best_fitness := -1
@@ -104,7 +109,7 @@ feature -- Parser
create Result.make (best_fitness, best_fit_q)
end
quality_parsed (a_field: STRING; parsed_common: LIST [COMMON_RESULTS]): REAL_64
quality_parsed (a_field: READABLE_STRING_8; parsed_common: LIST [COMMON_RESULTS]): REAL_64
-- Find the best match for a given charset/encoding against a list of charsets/encodings that
-- have already been parsed by parse_charsets(). Returns the 'q' quality
-- parameter of the best match, 0 if no match was found. This function
@@ -113,11 +118,11 @@ feature -- Parser
Result := fitness_and_quality_parsed (a_field, parsed_common).quality
end
quality (a_field: STRING; commons: STRING): REAL_64
quality (a_field: READABLE_STRING_8; commons: READABLE_STRING_8): REAL_64
-- Returns the quality 'q' of a charset/encoding when compared against the
-- a list of charsets/encodings/
local
l_commons: LIST [STRING]
l_commons: LIST [READABLE_STRING_8]
res: ARRAYED_LIST [COMMON_RESULTS]
p_res: COMMON_RESULTS
do
@@ -135,12 +140,12 @@ feature -- Parser
Result := quality_parsed (a_field, res)
end
best_match (supported: LIST [STRING]; header: STRING): STRING
best_match (supported: LIST [READABLE_STRING_8]; header: READABLE_STRING_8): READABLE_STRING_8
-- Choose the accept with the highest fitness score and quality ('q') from a list of candidates.
local
l_header_results: LIST [COMMON_RESULTS]
weighted_matches: LIST [FITNESS_AND_QUALITY]
l_res: LIST [STRING]
l_res: LIST [READABLE_STRING_8]
p_res: COMMON_RESULTS
fitness_and_quality, first_one: detachable FITNESS_AND_QUALITY
do
@@ -245,32 +250,6 @@ feature -- Parser
end
end
feature -- Util
mime_type (s: STRING): STRING
local
p: INTEGER
do
p := s.index_of (';', 1)
if p > 0 then
Result := trim (s.substring (1, p - 1))
else
Result := trim (s.string)
end
end
trim (a_string: STRING): STRING
-- trim whitespace from the beginning and end of a string
require
valid_argument: a_string /= Void
do
a_string.left_adjust
a_string.right_justify
Result := a_string
ensure
result_same_as_argument: a_string = Result
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -8,24 +8,26 @@ note
class
LANGUAGE_PARSE
inherit
inherit {NONE}
STRING_UTILS
REFACTORING_HELPER
feature -- Parser
parse_mime_type (a_mime_type: STRING): LANGUAGE_RESULTS
parse_mime_type (a_mime_type: READABLE_STRING_8): LANGUAGE_RESULTS
-- Parses a mime-type into its component parts.
-- For example, the media range 'application/xhtml;q=0.5' would get parsed
-- into:
-- ('application', 'xhtml', {'q', '0.5'})
local
l_parts: LIST [STRING]
p: STRING
sub_parts: LIST [STRING]
l_parts: LIST [READABLE_STRING_8]
p: READABLE_STRING_8
sub_parts: LIST [READABLE_STRING_8]
i: INTEGER
l_full_type: STRING
l_types: LIST [STRING]
l_full_type: READABLE_STRING_8
l_types: LIST [READABLE_STRING_8]
do
fixme ("Improve code!!!")
create Result.make
@@ -58,7 +60,7 @@ feature -- Parser
end
end
parse_media_range (a_range: STRING): LANGUAGE_RESULTS
parse_media_range (a_range: READABLE_STRING_8): LANGUAGE_RESULTS
-- Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
-- For example, the media range 'application/*;q=0.5' would get parsed into:
-- ('application', '*', {'q', '0.5'})
@@ -83,7 +85,7 @@ feature -- Parser
end
end
fitness_and_quality_parsed (a_mime_type: STRING; parsed_ranges: LIST [LANGUAGE_RESULTS]): FITNESS_AND_QUALITY
fitness_and_quality_parsed (a_mime_type: READABLE_STRING_8; parsed_ranges: LIST [LANGUAGE_RESULTS]): FITNESS_AND_QUALITY
-- Find the best match for a given mimeType against a list of media_ranges
-- that have already been parsed by parse_media_range. Returns a
-- tuple of the fitness value and the value of the 'q' quality parameter of
@@ -95,9 +97,9 @@ feature -- Parser
best_fit_q: REAL_64
target: LANGUAGE_RESULTS
range: LANGUAGE_RESULTS
keys: LIST [STRING]
keys: LIST [READABLE_STRING_8]
param_matches: INTEGER
element: detachable STRING
element: detachable READABLE_STRING_8
l_fitness: INTEGER
do
best_fitness := -1
@@ -161,7 +163,7 @@ feature -- Parser
create Result.make (best_fitness, best_fit_q)
end
quality_parsed (a_mime_type: STRING; parsed_ranges: LIST [LANGUAGE_RESULTS]): REAL_64
quality_parsed (a_mime_type: READABLE_STRING_8; parsed_ranges: LIST [LANGUAGE_RESULTS]): REAL_64
-- Find the best match for a given mime-type against a list of ranges that
-- have already been parsed by parseMediaRange(). Returns the 'q' quality
-- parameter of the best match, 0 if no match was found. This function
@@ -171,11 +173,11 @@ feature -- Parser
Result := fitness_and_quality_parsed (a_mime_type, parsed_ranges).quality
end
quality (a_mime_type: STRING; ranges: STRING): REAL_64
quality (a_mime_type: READABLE_STRING_8; ranges: READABLE_STRING_8): REAL_64
-- Returns the quality 'q' of a mime-type when compared against the
-- mediaRanges in ranges.
local
l_ranges: LIST [STRING]
l_ranges: LIST [READABLE_STRING_8]
res: ARRAYED_LIST [LANGUAGE_RESULTS]
p_res: LANGUAGE_RESULTS
do
@@ -193,15 +195,15 @@ feature -- Parser
Result := quality_parsed (a_mime_type, res)
end
best_match (supported: LIST [STRING]; header: STRING): STRING
best_match (supported: LIST [READABLE_STRING_8]; header: READABLE_STRING_8): READABLE_STRING_8
-- Choose the mime-type with the highest fitness score and quality ('q') from a list of candidates.
local
l_header_results: LIST [LANGUAGE_RESULTS]
weighted_matches: LIST [FITNESS_AND_QUALITY]
l_res: LIST [STRING]
l_res: LIST [READABLE_STRING_8]
p_res: LANGUAGE_RESULTS
fitness_and_quality, first_one: detachable FITNESS_AND_QUALITY
s: STRING
s: READABLE_STRING_8
do
l_res := header.split (',')
create {ARRAYED_LIST [LANGUAGE_RESULTS]} l_header_results.make (l_res.count)
@@ -299,32 +301,6 @@ feature -- Parser
end
end
feature {NONE} -- Implementation
mime_type (s: STRING): STRING
local
p: INTEGER
do
p := s.index_of (';', 1)
if p > 0 then
Result := trim (s.substring (1, p - 1))
else
Result := trim (s.string)
end
end
trim (a_string: STRING): STRING
-- trim whitespace from the beginning and end of a string
require
valid_argument: a_string /= Void
do
a_string.left_adjust
a_string.right_justify
Result := a_string
ensure
result_same_as_argument: a_string = Result
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -7,12 +7,15 @@
class
MIME_PARSE
inherit
inherit {NONE}
STRING_UTILS
REFACTORING_HELPER
feature -- Parser
parse_mime_type (a_mime_type: STRING): HTTP_MEDIA_TYPE
parse_mime_type (a_mime_type: READABLE_STRING_8): HTTP_MEDIA_TYPE
-- Parses a mime-type into its component parts.
-- For example, the media range 'application/xhtml;q=0.5' would get parsed
-- into:
@@ -21,7 +24,7 @@ feature -- Parser
create Result.make_from_string (a_mime_type)
end
parse_media_range (a_range: STRING): HTTP_MEDIA_TYPE
parse_media_range (a_range: READABLE_STRING_8): HTTP_MEDIA_TYPE
-- Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
-- For example, the media range 'application/*;q=0.5' would get parsed into:
-- ('application', '*', {'q', '0.5'})
@@ -51,7 +54,7 @@ feature -- Parser
end
fitness_and_quality_parsed (a_mime_type: STRING; parsed_ranges: LIST [HTTP_MEDIA_TYPE]): FITNESS_AND_QUALITY
fitness_and_quality_parsed (a_mime_type: READABLE_STRING_8; parsed_ranges: LIST [HTTP_MEDIA_TYPE]): FITNESS_AND_QUALITY
-- Find the best match for a given mimeType against a list of media_ranges
-- that have already been parsed by parse_media_range. Returns a
-- tuple of the fitness value and the value of the 'q' quality parameter of
@@ -148,7 +151,7 @@ feature -- Parser
create Result.make (best_fitness, best_fit_q)
end
quality_parsed (a_mime_type: STRING; parsed_ranges: LIST [HTTP_MEDIA_TYPE]): REAL_64
quality_parsed (a_mime_type: READABLE_STRING_8; parsed_ranges: LIST [HTTP_MEDIA_TYPE]): REAL_64
-- Find the best match for a given mime-type against a list of ranges that
-- have already been parsed by parseMediaRange(). Returns the 'q' quality
-- parameter of the best match, 0 if no match was found. This function
@@ -158,11 +161,11 @@ feature -- Parser
Result := fitness_and_quality_parsed (a_mime_type, parsed_ranges).quality
end
quality (a_mime_type: STRING; ranges: STRING): REAL_64
quality (a_mime_type: READABLE_STRING_8; ranges: READABLE_STRING_8): REAL_64
-- Returns the quality 'q' of a mime-type when compared against the
-- mediaRanges in ranges.
local
l_ranges : LIST [STRING]
l_ranges : LIST [READABLE_STRING_8]
res : ARRAYED_LIST [HTTP_MEDIA_TYPE]
p_res : HTTP_MEDIA_TYPE
do
@@ -180,15 +183,15 @@ feature -- Parser
Result := quality_parsed (a_mime_type, res)
end
best_match (supported: LIST [STRING]; header: STRING): STRING
best_match (supported: LIST [READABLE_STRING_8]; header: READABLE_STRING_8): READABLE_STRING_8
-- Choose the mime-type with the highest fitness score and quality ('q') from a list of candidates.
local
l_header_results: LIST [HTTP_MEDIA_TYPE]
weighted_matches: LIST [FITNESS_AND_QUALITY]
l_res: LIST [STRING]
l_res: LIST [READABLE_STRING_8]
p_res: HTTP_MEDIA_TYPE
fitness_and_quality, first_one: detachable FITNESS_AND_QUALITY
s: STRING
s: READABLE_STRING_8
do
l_res := header.split (',')
create {ARRAYED_LIST [HTTP_MEDIA_TYPE]} l_header_results.make (l_res.count)
@@ -285,32 +288,6 @@ feature -- Parser
end
end
feature {NONE} -- Implementation
mime_type (s: STRING): STRING
local
p: INTEGER
do
p := s.index_of (';', 1)
if p > 0 then
Result := trim (s.substring (1, p - 1))
else
Result := trim (s.string)
end
end
trim (a_string: STRING): STRING
-- trim whitespace from the beginning and end of a string
require
valid_argument : a_string /= Void
do
a_string.left_adjust
a_string.right_justify
Result := a_string
ensure
result_same_as_argument: a_string = Result
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -0,0 +1,42 @@
note
description: "Summary description for {STRING_UTILS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
STRING_UTILS
feature {NONE} -- Implementation
mime_type (s: READABLE_STRING_8): READABLE_STRING_8
local
p: INTEGER
do
p := s.index_of (';', 1)
if p > 0 then
Result := trim (s.substring (1, p - 1))
else
Result := trim (s.string)
end
end
trim (a_string: READABLE_STRING_8): READABLE_STRING_8
-- trim whitespace from the beginning and end of a string
require
valid_argument : a_string /= Void
local
l_result: STRING
do
l_result := a_string.as_string_8
l_result.left_adjust
l_result.right_adjust
Result := l_result
ensure
result_same_as_argument: Result.same_string_general (a_string)
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -1,55 +0,0 @@
note
description: "Summary description for {VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
VARIANT_RESULTS
feature -- Mime, Language, Charset and Encoding Results
mime_result: detachable STRING
set_mime_result (a_mime: STRING)
-- set the mime_result with `a_mime'
do
mime_result := a_mime
ensure
set_mime_result: a_mime = mime_result
end
language_result: detachable STRING
set_language_result (a_language: STRING)
-- set the language_result with `a_language'
do
language_result := a_language
ensure
set_language: a_language = language_result
end
charset_result: detachable STRING
set_charset_result (a_charset: STRING)
-- set the charset_result with `a_charset'
do
charset_result := a_charset
ensure
set_charset: a_charset = charset_result
end
encoding_result: detachable STRING
set_encoding_default (an_encoding: STRING)
do
encoding_result := an_encoding
ensure
set_encoding: an_encoding = encoding_result
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,39 @@
note
description: "Summary description for {CHARACTER_ENCODING_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
CHARACTER_ENCODING_VARIANT_RESULTS
inherit
VARIANT_RESULTS
feature -- Access
character_type: detachable READABLE_STRING_8
feature -- Change Element
set_character_type (a_character_type: READABLE_STRING_8)
-- Set `character_type' with `a_character_type'
do
character_type := a_character_type
ensure
character_type_set: character_type /= Void implies a_character_type = character_type
end
set_variant_header
-- Set variant header as `Accept-Charset'
do
variant_header := "Accept-Charset"
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,41 @@
note
description: "Summary description for {COMPRESSION_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
EIS: "name= Compression", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3", "protocol=uri"
class
COMPRESSION_VARIANT_RESULTS
inherit
VARIANT_RESULTS
feature -- Access
compression_type: detachable READABLE_STRING_8
-- Compression variant for the response
feature -- Change Element
set_compression_type (a_compression_type: READABLE_STRING_8)
-- Set `compression_type' with `a_compression_type'
do
compression_type := a_compression_type
ensure
compression_type_set: attached compression_type as l_compression_type implies l_compression_type = a_compression_type
end
set_variant_header
-- Set variant_header as `Accept-Encoding'
do
variant_header := "Accept-Encoding"
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,41 @@
note
description: "Summary description for {LANGUAGE_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
LANGUAGE_VARIANT_RESULTS
inherit
VARIANT_RESULTS
feature -- Access
language_type: detachable READABLE_STRING_8
-- language variant for the response
feature -- Change Element
set_language_type (a_language_type: READABLE_STRING_8)
-- Set `language_type' with `a_language_type'
do
language_type := a_language_type
ensure
language_type_set: attached language_type as l_language_type implies l_language_type = a_language_type
end
set_variant_header
-- Set variant header as 'Accept-Language'
do
variant_header := "Accept-Language"
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,39 @@
note
description: "Summary description for {MEDIA_TYPE_VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
MEDIA_TYPE_VARIANT_RESULTS
inherit
VARIANT_RESULTS
feature -- Access
media_type: detachable READABLE_STRING_8
feature -- Change Element
set_media_type (a_media_type: READABLE_STRING_8)
-- Set `media_type' as `a_media_type'
do
media_type := a_media_type
ensure
media_type_set: attached media_type as l_media_type implies l_media_type = a_media_type
end
set_variant_header
-- Set variant header as `Accept'
do
variant_header := "Accept"
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,71 @@
note
description: "Summary description for {VARIANT_RESULTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
VARIANT_RESULTS
feature -- Access
variant_header: detachable READABLE_STRING_8
-- variant header for the response
supported_variants: detachable LIST [READABLE_STRING_8]
-- Set of supported variants for the response
is_acceptable: BOOLEAN
-- is the current variant accepted?
accept_headers_set: ARRAY[READABLE_STRING_8]
-- Set of valid accept headers headers
note
EIS:"name=Accept", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1", "protocol=uri"
EIS:"name=Accept-Charset", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2", "protocol=uri"
EIS:"name=Accept-Encoding", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3", "protocol=uri"
EIS:"name=Accept-Language", "src=http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4", "protocol=uri"
once
Result:= <<"Accept","Accept-Language","Accept-Encoding","Accept-Charset">>
Result.compare_objects
end
feature -- Status_Report
is_valid_header (a_header: READABLE_STRING_8): BOOLEAN
-- is `a_header' a valid accept header?
do
Result := accept_headers_set.has (a_header)
end
feature -- Change Element
set_acceptable (acceptable: BOOLEAN)
-- Set `is_acceptable' with `acceptable'
do
is_acceptable := acceptable
ensure
is_acceptable_set: is_acceptable = acceptable
end
set_variant_header
-- Set variant header
deferred
ensure
is_valid_header_set : attached variant_header as l_header implies is_valid_header (l_header)
end
set_supported_variants (a_supported: LIST [READABLE_STRING_8])
-- Set `supported vairants' with `a_supported'
do
supported_variants := a_supported
ensure
set_supported_variants: attached supported_variants as l_supported_variants implies l_supported_variants = a_supported
end
note
copyright: "2011-2013, Javier Velilla, Jocelyn Fiat, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -117,7 +117,7 @@ feature -- Test routines
--Scenario 3, the server select the best match, and set the vary header
l_compression := "gzip"
compression_supported := l_compression.split(',')
conneg.set_encoding_defautl ("gzip")
conneg.set_encoding_default("gzip")
compression_variants := conneg.encoding_preference (compression_supported, "compress,gzip;q=0.7")
assert ("Expected Acceptable", compression_variants.is_acceptable)
assert ("Variants is dettached",compression_variants.supported_variants = Void)